1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Elists
; use Elists
;
32 with Errout
; use Errout
;
33 with Exp_Aggr
; use Exp_Aggr
;
34 with Exp_Ch6
; use Exp_Ch6
;
35 with Exp_Ch7
; use Exp_Ch7
;
36 with Exp_Ch11
; use Exp_Ch11
;
37 with Exp_Dbug
; use Exp_Dbug
;
38 with Exp_Pakd
; use Exp_Pakd
;
39 with Exp_Tss
; use Exp_Tss
;
40 with Exp_Util
; use Exp_Util
;
41 with Inline
; use Inline
;
42 with Namet
; use Namet
;
43 with Nlists
; use Nlists
;
44 with Nmake
; use Nmake
;
46 with Restrict
; use Restrict
;
47 with Rident
; use Rident
;
48 with Rtsfind
; use Rtsfind
;
49 with Sinfo
; use Sinfo
;
51 with Sem_Aux
; use Sem_Aux
;
52 with Sem_Ch3
; use Sem_Ch3
;
53 with Sem_Ch8
; use Sem_Ch8
;
54 with Sem_Ch13
; use Sem_Ch13
;
55 with Sem_Eval
; use Sem_Eval
;
56 with Sem_Res
; use Sem_Res
;
57 with Sem_Util
; use Sem_Util
;
58 with Snames
; use Snames
;
59 with Stand
; use Stand
;
60 with Stringt
; use Stringt
;
61 with Targparm
; use Targparm
;
62 with Tbuild
; use Tbuild
;
63 with Uintp
; use Uintp
;
64 with Validsw
; use Validsw
;
66 package body Exp_Ch5
is
68 procedure Build_Formal_Container_Iteration
70 Container
: Entity_Id
;
73 Advance
: out Node_Id
;
74 New_Loop
: out Node_Id
);
75 -- Utility to create declarations and loop statement for both forms
76 -- of formal container iterators.
78 function Change_Of_Representation
(N
: Node_Id
) return Boolean;
79 -- Determine if the right hand side of assignment N is a type conversion
80 -- which requires a change of representation. Called only for the array
83 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
);
84 -- N is an assignment which assigns an array value. This routine process
85 -- the various special cases and checks required for such assignments,
86 -- including change of representation. Rhs is normally simply the right
87 -- hand side of the assignment, except that if the right hand side is a
88 -- type conversion or a qualified expression, then the RHS is the actual
89 -- expression inside any such type conversions or qualifications.
91 function Expand_Assign_Array_Loop
98 Rev
: Boolean) return Node_Id
;
99 -- N is an assignment statement which assigns an array value. This routine
100 -- expands the assignment into a loop (or nested loops for the case of a
101 -- multi-dimensional array) to do the assignment component by component.
102 -- Larray and Rarray are the entities of the actual arrays on the left
103 -- hand and right hand sides. L_Type and R_Type are the types of these
104 -- arrays (which may not be the same, due to either sliding, or to a
105 -- change of representation case). Ndim is the number of dimensions and
106 -- the parameter Rev indicates if the loops run normally (Rev = False),
107 -- or reversed (Rev = True). The value returned is the constructed
108 -- loop statement. Auxiliary declarations are inserted before node N
109 -- using the standard Insert_Actions mechanism.
111 procedure Expand_Assign_Record
(N
: Node_Id
);
112 -- N is an assignment of an untagged record value. This routine handles
113 -- the case where the assignment must be made component by component,
114 -- either because the target is not byte aligned, or there is a change
115 -- of representation, or when we have a tagged type with a representation
116 -- clause (this last case is required because holes in the tagged type
117 -- might be filled with components from child types).
119 procedure Expand_Formal_Container_Loop
(N
: Node_Id
);
120 -- Use the primitives specified in an Iterable aspect to expand a loop
121 -- over a so-called formal container, primarily for SPARK usage.
123 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
);
124 -- Same, for an iterator of the form " For E of C". In this case the
125 -- iterator provides the name of the element, and the cursor is generated
128 procedure Expand_Iterator_Loop
(N
: Node_Id
);
129 -- Expand loop over arrays and containers that uses the form "for X of C"
130 -- with an optional subtype mark, or "for Y in C".
132 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
);
133 -- Expand loop over arrays that uses the form "for X of C"
135 procedure Expand_Predicated_Loop
(N
: Node_Id
);
136 -- Expand for loop over predicated subtype
138 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
;
139 -- Generate the necessary code for controlled and tagged assignment, that
140 -- is to say, finalization of the target before, adjustment of the target
141 -- after and save and restore of the tag and finalization pointers which
142 -- are not 'part of the value' and must not be changed upon assignment. N
143 -- is the original Assignment node.
145 --------------------------------------
146 -- Build_Formal_Container_iteration --
147 --------------------------------------
149 procedure Build_Formal_Container_Iteration
151 Container
: Entity_Id
;
154 Advance
: out Node_Id
;
155 New_Loop
: out Node_Id
)
157 Loc
: constant Source_Ptr
:= Sloc
(N
);
158 Stats
: constant List_Id
:= Statements
(N
);
159 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
160 First_Op
: constant Entity_Id
:=
161 Get_Iterable_Type_Primitive
(Typ
, Name_First
);
162 Next_Op
: constant Entity_Id
:=
163 Get_Iterable_Type_Primitive
(Typ
, Name_Next
);
165 Has_Element_Op
: constant Entity_Id
:=
166 Get_Iterable_Type_Primitive
(Typ
, Name_Has_Element
);
168 -- Declaration for Cursor
171 Make_Object_Declaration
(Loc
,
172 Defining_Identifier
=> Cursor
,
173 Object_Definition
=> New_Occurrence_Of
(Etype
(First_Op
), Loc
),
175 Make_Function_Call
(Loc
,
176 Name
=> New_Occurrence_Of
(First_Op
, Loc
),
177 Parameter_Associations
=> New_List
(
178 New_Occurrence_Of
(Container
, Loc
))));
180 -- Statement that advances cursor in loop
183 Make_Assignment_Statement
(Loc
,
184 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
186 Make_Function_Call
(Loc
,
187 Name
=> New_Occurrence_Of
(Next_Op
, Loc
),
188 Parameter_Associations
=> New_List
(
189 New_Occurrence_Of
(Container
, Loc
),
190 New_Occurrence_Of
(Cursor
, Loc
))));
192 -- Iterator is rewritten as a while_loop
195 Make_Loop_Statement
(Loc
,
197 Make_Iteration_Scheme
(Loc
,
199 Make_Function_Call
(Loc
,
200 Name
=> New_Occurrence_Of
(Has_Element_Op
, Loc
),
201 Parameter_Associations
=> New_List
(
202 New_Occurrence_Of
(Container
, Loc
),
203 New_Occurrence_Of
(Cursor
, Loc
)))),
206 end Build_Formal_Container_Iteration
;
208 ------------------------------
209 -- Change_Of_Representation --
210 ------------------------------
212 function Change_Of_Representation
(N
: Node_Id
) return Boolean is
213 Rhs
: constant Node_Id
:= Expression
(N
);
216 Nkind
(Rhs
) = N_Type_Conversion
218 not Same_Representation
(Etype
(Rhs
), Etype
(Expression
(Rhs
)));
219 end Change_Of_Representation
;
221 -------------------------
222 -- Expand_Assign_Array --
223 -------------------------
225 -- There are two issues here. First, do we let Gigi do a block move, or
226 -- do we expand out into a loop? Second, we need to set the two flags
227 -- Forwards_OK and Backwards_OK which show whether the block move (or
228 -- corresponding loops) can be legitimately done in a forwards (low to
229 -- high) or backwards (high to low) manner.
231 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
) is
232 Loc
: constant Source_Ptr
:= Sloc
(N
);
234 Lhs
: constant Node_Id
:= Name
(N
);
236 Act_Lhs
: constant Node_Id
:= Get_Referenced_Object
(Lhs
);
237 Act_Rhs
: Node_Id
:= Get_Referenced_Object
(Rhs
);
239 L_Type
: constant Entity_Id
:=
240 Underlying_Type
(Get_Actual_Subtype
(Act_Lhs
));
241 R_Type
: Entity_Id
:=
242 Underlying_Type
(Get_Actual_Subtype
(Act_Rhs
));
244 L_Slice
: constant Boolean := Nkind
(Act_Lhs
) = N_Slice
;
245 R_Slice
: constant Boolean := Nkind
(Act_Rhs
) = N_Slice
;
247 Crep
: constant Boolean := Change_Of_Representation
(N
);
252 Ndim
: constant Pos
:= Number_Dimensions
(L_Type
);
254 Loop_Required
: Boolean := False;
255 -- This switch is set to True if the array move must be done using
256 -- an explicit front end generated loop.
258 procedure Apply_Dereference
(Arg
: Node_Id
);
259 -- If the argument is an access to an array, and the assignment is
260 -- converted into a procedure call, apply explicit dereference.
262 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean;
263 -- Test if Exp is a reference to an array whose declaration has
264 -- an address clause, or it is a slice of such an array.
266 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean;
267 -- Test if Exp is a reference to an array which is either a formal
268 -- parameter or a slice of a formal parameter. These are the cases
269 -- where hidden aliasing can occur.
271 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean;
272 -- Determine if Exp is a reference to an array variable which is other
273 -- than an object defined in the current scope, or a slice of such
274 -- an object. Such objects can be aliased to parameters (unlike local
275 -- array references).
277 -----------------------
278 -- Apply_Dereference --
279 -----------------------
281 procedure Apply_Dereference
(Arg
: Node_Id
) is
282 Typ
: constant Entity_Id
:= Etype
(Arg
);
284 if Is_Access_Type
(Typ
) then
285 Rewrite
(Arg
, Make_Explicit_Dereference
(Loc
,
286 Prefix
=> Relocate_Node
(Arg
)));
287 Analyze_And_Resolve
(Arg
, Designated_Type
(Typ
));
289 end Apply_Dereference
;
291 ------------------------
292 -- Has_Address_Clause --
293 ------------------------
295 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean is
298 (Is_Entity_Name
(Exp
) and then
299 Present
(Address_Clause
(Entity
(Exp
))))
301 (Nkind
(Exp
) = N_Slice
and then Has_Address_Clause
(Prefix
(Exp
)));
302 end Has_Address_Clause
;
304 ---------------------
305 -- Is_Formal_Array --
306 ---------------------
308 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean is
311 (Is_Entity_Name
(Exp
) and then Is_Formal
(Entity
(Exp
)))
313 (Nkind
(Exp
) = N_Slice
and then Is_Formal_Array
(Prefix
(Exp
)));
316 ------------------------
317 -- Is_Non_Local_Array --
318 ------------------------
320 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean is
322 return (Is_Entity_Name
(Exp
)
323 and then Scope
(Entity
(Exp
)) /= Current_Scope
)
324 or else (Nkind
(Exp
) = N_Slice
325 and then Is_Non_Local_Array
(Prefix
(Exp
)));
326 end Is_Non_Local_Array
;
328 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
330 Lhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Lhs
);
331 Rhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Rhs
);
333 Lhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Lhs
);
334 Rhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Rhs
);
336 -- Start of processing for Expand_Assign_Array
339 -- Deal with length check. Note that the length check is done with
340 -- respect to the right hand side as given, not a possible underlying
341 -- renamed object, since this would generate incorrect extra checks.
343 Apply_Length_Check
(Rhs
, L_Type
);
345 -- We start by assuming that the move can be done in either direction,
346 -- i.e. that the two sides are completely disjoint.
348 Set_Forwards_OK
(N
, True);
349 Set_Backwards_OK
(N
, True);
351 -- Normally it is only the slice case that can lead to overlap, and
352 -- explicit checks for slices are made below. But there is one case
353 -- where the slice can be implicit and invisible to us: when we have a
354 -- one dimensional array, and either both operands are parameters, or
355 -- one is a parameter (which can be a slice passed by reference) and the
356 -- other is a non-local variable. In this case the parameter could be a
357 -- slice that overlaps with the other operand.
359 -- However, if the array subtype is a constrained first subtype in the
360 -- parameter case, then we don't have to worry about overlap, since
361 -- slice assignments aren't possible (other than for a slice denoting
364 -- Note: No overlap is possible if there is a change of representation,
365 -- so we can exclude this case.
370 ((Lhs_Formal
and Rhs_Formal
)
372 (Lhs_Formal
and Rhs_Non_Local_Var
)
374 (Rhs_Formal
and Lhs_Non_Local_Var
))
376 (not Is_Constrained
(Etype
(Lhs
))
377 or else not Is_First_Subtype
(Etype
(Lhs
)))
379 -- In the case of compiling for the Java or .NET Virtual Machine,
380 -- slices are always passed by making a copy, so we don't have to
381 -- worry about overlap. We also want to prevent generation of "<"
382 -- comparisons for array addresses, since that's a meaningless
383 -- operation on the VM.
385 and then VM_Target
= No_VM
387 Set_Forwards_OK
(N
, False);
388 Set_Backwards_OK
(N
, False);
390 -- Note: the bit-packed case is not worrisome here, since if we have
391 -- a slice passed as a parameter, it is always aligned on a byte
392 -- boundary, and if there are no explicit slices, the assignment
393 -- can be performed directly.
396 -- If either operand has an address clause clear Backwards_OK and
397 -- Forwards_OK, since we cannot tell if the operands overlap. We
398 -- exclude this treatment when Rhs is an aggregate, since we know
399 -- that overlap can't occur.
401 if (Has_Address_Clause
(Lhs
) and then Nkind
(Rhs
) /= N_Aggregate
)
402 or else Has_Address_Clause
(Rhs
)
404 Set_Forwards_OK
(N
, False);
405 Set_Backwards_OK
(N
, False);
408 -- We certainly must use a loop for change of representation and also
409 -- we use the operand of the conversion on the right hand side as the
410 -- effective right hand side (the component types must match in this
414 Act_Rhs
:= Get_Referenced_Object
(Rhs
);
415 R_Type
:= Get_Actual_Subtype
(Act_Rhs
);
416 Loop_Required
:= True;
418 -- We require a loop if the left side is possibly bit unaligned
420 elsif Possible_Bit_Aligned_Component
(Lhs
)
422 Possible_Bit_Aligned_Component
(Rhs
)
424 Loop_Required
:= True;
426 -- Arrays with controlled components are expanded into a loop to force
427 -- calls to Adjust at the component level.
429 elsif Has_Controlled_Component
(L_Type
) then
430 Loop_Required
:= True;
432 -- If object is atomic, we cannot tolerate a loop
434 elsif Is_Atomic_Object
(Act_Lhs
)
436 Is_Atomic_Object
(Act_Rhs
)
440 -- Loop is required if we have atomic components since we have to
441 -- be sure to do any accesses on an element by element basis.
443 elsif Has_Atomic_Components
(L_Type
)
444 or else Has_Atomic_Components
(R_Type
)
445 or else Is_Atomic
(Component_Type
(L_Type
))
446 or else Is_Atomic
(Component_Type
(R_Type
))
448 Loop_Required
:= True;
450 -- Case where no slice is involved
452 elsif not L_Slice
and not R_Slice
then
454 -- The following code deals with the case of unconstrained bit packed
455 -- arrays. The problem is that the template for such arrays contains
456 -- the bounds of the actual source level array, but the copy of an
457 -- entire array requires the bounds of the underlying array. It would
458 -- be nice if the back end could take care of this, but right now it
459 -- does not know how, so if we have such a type, then we expand out
460 -- into a loop, which is inefficient but works correctly. If we don't
461 -- do this, we get the wrong length computed for the array to be
462 -- moved. The two cases we need to worry about are:
464 -- Explicit dereference of an unconstrained packed array type as in
465 -- the following example:
468 -- type BITS is array(INTEGER range <>) of BOOLEAN;
469 -- pragma PACK(BITS);
470 -- type A is access BITS;
473 -- P1 := new BITS (1 .. 65_535);
474 -- P2 := new BITS (1 .. 65_535);
478 -- A formal parameter reference with an unconstrained bit array type
479 -- is the other case we need to worry about (here we assume the same
480 -- BITS type declared above):
482 -- procedure Write_All (File : out BITS; Contents : BITS);
484 -- File.Storage := Contents;
487 -- We expand to a loop in either of these two cases
489 -- Question for future thought. Another potentially more efficient
490 -- approach would be to create the actual subtype, and then do an
491 -- unchecked conversion to this actual subtype ???
493 Check_Unconstrained_Bit_Packed_Array
: declare
495 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean;
496 -- Function to perform required test for the first case, above
497 -- (dereference of an unconstrained bit packed array).
499 -----------------------
500 -- Is_UBPA_Reference --
501 -----------------------
503 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean is
504 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Opnd
));
506 Des_Type
: Entity_Id
;
509 if Present
(Packed_Array_Impl_Type
(Typ
))
510 and then Is_Array_Type
(Packed_Array_Impl_Type
(Typ
))
511 and then not Is_Constrained
(Packed_Array_Impl_Type
(Typ
))
515 elsif Nkind
(Opnd
) = N_Explicit_Dereference
then
516 P_Type
:= Underlying_Type
(Etype
(Prefix
(Opnd
)));
518 if not Is_Access_Type
(P_Type
) then
522 Des_Type
:= Designated_Type
(P_Type
);
524 Is_Bit_Packed_Array
(Des_Type
)
525 and then not Is_Constrained
(Des_Type
);
531 end Is_UBPA_Reference
;
533 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
536 if Is_UBPA_Reference
(Lhs
)
538 Is_UBPA_Reference
(Rhs
)
540 Loop_Required
:= True;
542 -- Here if we do not have the case of a reference to a bit packed
543 -- unconstrained array case. In this case gigi can most certainly
544 -- handle the assignment if a forwards move is allowed.
546 -- (could it handle the backwards case also???)
548 elsif Forwards_OK
(N
) then
551 end Check_Unconstrained_Bit_Packed_Array
;
553 -- The back end can always handle the assignment if the right side is a
554 -- string literal (note that overlap is definitely impossible in this
555 -- case). If the type is packed, a string literal is always converted
556 -- into an aggregate, except in the case of a null slice, for which no
557 -- aggregate can be written. In that case, rewrite the assignment as a
558 -- null statement, a length check has already been emitted to verify
559 -- that the range of the left-hand side is empty.
561 -- Note that this code is not executed if we have an assignment of a
562 -- string literal to a non-bit aligned component of a record, a case
563 -- which cannot be handled by the backend.
565 elsif Nkind
(Rhs
) = N_String_Literal
then
566 if String_Length
(Strval
(Rhs
)) = 0
567 and then Is_Bit_Packed_Array
(L_Type
)
569 Rewrite
(N
, Make_Null_Statement
(Loc
));
575 -- If either operand is bit packed, then we need a loop, since we can't
576 -- be sure that the slice is byte aligned. Similarly, if either operand
577 -- is a possibly unaligned slice, then we need a loop (since the back
578 -- end cannot handle unaligned slices).
580 elsif Is_Bit_Packed_Array
(L_Type
)
581 or else Is_Bit_Packed_Array
(R_Type
)
582 or else Is_Possibly_Unaligned_Slice
(Lhs
)
583 or else Is_Possibly_Unaligned_Slice
(Rhs
)
585 Loop_Required
:= True;
587 -- If we are not bit-packed, and we have only one slice, then no overlap
588 -- is possible except in the parameter case, so we can let the back end
591 elsif not (L_Slice
and R_Slice
) then
592 if Forwards_OK
(N
) then
597 -- If the right-hand side is a string literal, introduce a temporary for
598 -- it, for use in the generated loop that will follow.
600 if Nkind
(Rhs
) = N_String_Literal
then
602 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Rhs
);
607 Make_Object_Declaration
(Loc
,
608 Defining_Identifier
=> Temp
,
609 Object_Definition
=> New_Occurrence_Of
(L_Type
, Loc
),
610 Expression
=> Relocate_Node
(Rhs
));
612 Insert_Action
(N
, Decl
);
613 Rewrite
(Rhs
, New_Occurrence_Of
(Temp
, Loc
));
614 R_Type
:= Etype
(Temp
);
618 -- Come here to complete the analysis
620 -- Loop_Required: Set to True if we know that a loop is required
621 -- regardless of overlap considerations.
623 -- Forwards_OK: Set to False if we already know that a forwards
624 -- move is not safe, else set to True.
626 -- Backwards_OK: Set to False if we already know that a backwards
627 -- move is not safe, else set to True
629 -- Our task at this stage is to complete the overlap analysis, which can
630 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
631 -- then generating the final code, either by deciding that it is OK
632 -- after all to let Gigi handle it, or by generating appropriate code
636 L_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(L_Type
));
637 R_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(R_Type
));
639 Left_Lo
: constant Node_Id
:= Type_Low_Bound
(L_Index_Typ
);
640 Left_Hi
: constant Node_Id
:= Type_High_Bound
(L_Index_Typ
);
641 Right_Lo
: constant Node_Id
:= Type_Low_Bound
(R_Index_Typ
);
642 Right_Hi
: constant Node_Id
:= Type_High_Bound
(R_Index_Typ
);
644 Act_L_Array
: Node_Id
;
645 Act_R_Array
: Node_Id
;
651 Cresult
: Compare_Result
;
654 -- Get the expressions for the arrays. If we are dealing with a
655 -- private type, then convert to the underlying type. We can do
656 -- direct assignments to an array that is a private type, but we
657 -- cannot assign to elements of the array without this extra
658 -- unchecked conversion.
660 -- Note: We propagate Parent to the conversion nodes to generate
661 -- a well-formed subtree.
663 if Nkind
(Act_Lhs
) = N_Slice
then
664 Larray
:= Prefix
(Act_Lhs
);
668 if Is_Private_Type
(Etype
(Larray
)) then
670 Par
: constant Node_Id
:= Parent
(Larray
);
674 (Underlying_Type
(Etype
(Larray
)), Larray
);
675 Set_Parent
(Larray
, Par
);
680 if Nkind
(Act_Rhs
) = N_Slice
then
681 Rarray
:= Prefix
(Act_Rhs
);
685 if Is_Private_Type
(Etype
(Rarray
)) then
687 Par
: constant Node_Id
:= Parent
(Rarray
);
691 (Underlying_Type
(Etype
(Rarray
)), Rarray
);
692 Set_Parent
(Rarray
, Par
);
697 -- If both sides are slices, we must figure out whether it is safe
698 -- to do the move in one direction or the other. It is always safe
699 -- if there is a change of representation since obviously two arrays
700 -- with different representations cannot possibly overlap.
702 if (not Crep
) and L_Slice
and R_Slice
then
703 Act_L_Array
:= Get_Referenced_Object
(Prefix
(Act_Lhs
));
704 Act_R_Array
:= Get_Referenced_Object
(Prefix
(Act_Rhs
));
706 -- If both left and right hand arrays are entity names, and refer
707 -- to different entities, then we know that the move is safe (the
708 -- two storage areas are completely disjoint).
710 if Is_Entity_Name
(Act_L_Array
)
711 and then Is_Entity_Name
(Act_R_Array
)
712 and then Entity
(Act_L_Array
) /= Entity
(Act_R_Array
)
716 -- Otherwise, we assume the worst, which is that the two arrays
717 -- are the same array. There is no need to check if we know that
718 -- is the case, because if we don't know it, we still have to
721 -- Generally if the same array is involved, then we have an
722 -- overlapping case. We will have to really assume the worst (i.e.
723 -- set neither of the OK flags) unless we can determine the lower
724 -- or upper bounds at compile time and compare them.
729 (Left_Lo
, Right_Lo
, Assume_Valid
=> True);
731 if Cresult
= Unknown
then
734 (Left_Hi
, Right_Hi
, Assume_Valid
=> True);
738 when LT | LE | EQ
=> Set_Backwards_OK
(N
, False);
739 when GT | GE
=> Set_Forwards_OK
(N
, False);
740 when NE | Unknown
=> Set_Backwards_OK
(N
, False);
741 Set_Forwards_OK
(N
, False);
746 -- If after that analysis Loop_Required is False, meaning that we
747 -- have not discovered some non-overlap reason for requiring a loop,
748 -- then the outcome depends on the capabilities of the back end.
750 if not Loop_Required
then
752 -- The GCC back end can deal with all cases of overlap by falling
753 -- back to memmove if it cannot use a more efficient approach.
755 if VM_Target
= No_VM
and not AAMP_On_Target
then
758 -- Assume other back ends can handle it if Forwards_OK is set
760 elsif Forwards_OK
(N
) then
763 -- If Forwards_OK is not set, the back end will need something
764 -- like memmove to handle the move. For now, this processing is
765 -- activated using the .s debug flag (-gnatd.s).
767 elsif Debug_Flag_Dot_S
then
772 -- At this stage we have to generate an explicit loop, and we have
773 -- the following cases:
775 -- Forwards_OK = True
777 -- Rnn : right_index := right_index'First;
778 -- for Lnn in left-index loop
779 -- left (Lnn) := right (Rnn);
780 -- Rnn := right_index'Succ (Rnn);
783 -- Note: the above code MUST be analyzed with checks off, because
784 -- otherwise the Succ could overflow. But in any case this is more
787 -- Forwards_OK = False, Backwards_OK = True
789 -- Rnn : right_index := right_index'Last;
790 -- for Lnn in reverse left-index loop
791 -- left (Lnn) := right (Rnn);
792 -- Rnn := right_index'Pred (Rnn);
795 -- Note: the above code MUST be analyzed with checks off, because
796 -- otherwise the Pred could overflow. But in any case this is more
799 -- Forwards_OK = Backwards_OK = False
801 -- This only happens if we have the same array on each side. It is
802 -- possible to create situations using overlays that violate this,
803 -- but we simply do not promise to get this "right" in this case.
805 -- There are two possible subcases. If the No_Implicit_Conditionals
806 -- restriction is set, then we generate the following code:
809 -- T : constant <operand-type> := rhs;
814 -- If implicit conditionals are permitted, then we generate:
816 -- if Left_Lo <= Right_Lo then
817 -- <code for Forwards_OK = True above>
819 -- <code for Backwards_OK = True above>
822 -- In order to detect possible aliasing, we examine the renamed
823 -- expression when the source or target is a renaming. However,
824 -- the renaming may be intended to capture an address that may be
825 -- affected by subsequent code, and therefore we must recover
826 -- the actual entity for the expansion that follows, not the
827 -- object it renames. In particular, if source or target designate
828 -- a portion of a dynamically allocated object, the pointer to it
829 -- may be reassigned but the renaming preserves the proper location.
831 if Is_Entity_Name
(Rhs
)
833 Nkind
(Parent
(Entity
(Rhs
))) = N_Object_Renaming_Declaration
834 and then Nkind
(Act_Rhs
) = N_Slice
839 if Is_Entity_Name
(Lhs
)
841 Nkind
(Parent
(Entity
(Lhs
))) = N_Object_Renaming_Declaration
842 and then Nkind
(Act_Lhs
) = N_Slice
847 -- Cases where either Forwards_OK or Backwards_OK is true
849 if Forwards_OK
(N
) or else Backwards_OK
(N
) then
850 if Needs_Finalization
(Component_Type
(L_Type
))
851 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
853 and then not No_Ctrl_Actions
(N
)
856 Proc
: constant Entity_Id
:=
857 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
861 Apply_Dereference
(Larray
);
862 Apply_Dereference
(Rarray
);
863 Actuals
:= New_List
(
864 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
865 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
866 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
867 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
868 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
869 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
873 Boolean_Literals
(not Forwards_OK
(N
)), Loc
));
876 Make_Procedure_Call_Statement
(Loc
,
877 Name
=> New_Occurrence_Of
(Proc
, Loc
),
878 Parameter_Associations
=> Actuals
));
883 Expand_Assign_Array_Loop
884 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
885 Rev
=> not Forwards_OK
(N
)));
888 -- Case of both are false with No_Implicit_Conditionals
890 elsif Restriction_Active
(No_Implicit_Conditionals
) then
892 T
: constant Entity_Id
:=
893 Make_Defining_Identifier
(Loc
, Chars
=> Name_T
);
897 Make_Block_Statement
(Loc
,
898 Declarations
=> New_List
(
899 Make_Object_Declaration
(Loc
,
900 Defining_Identifier
=> T
,
901 Constant_Present
=> True,
903 New_Occurrence_Of
(Etype
(Rhs
), Loc
),
904 Expression
=> Relocate_Node
(Rhs
))),
906 Handled_Statement_Sequence
=>
907 Make_Handled_Sequence_Of_Statements
(Loc
,
908 Statements
=> New_List
(
909 Make_Assignment_Statement
(Loc
,
910 Name
=> Relocate_Node
(Lhs
),
911 Expression
=> New_Occurrence_Of
(T
, Loc
))))));
914 -- Case of both are false with implicit conditionals allowed
917 -- Before we generate this code, we must ensure that the left and
918 -- right side array types are defined. They may be itypes, and we
919 -- cannot let them be defined inside the if, since the first use
920 -- in the then may not be executed.
922 Ensure_Defined
(L_Type
, N
);
923 Ensure_Defined
(R_Type
, N
);
925 -- We normally compare addresses to find out which way round to
926 -- do the loop, since this is reliable, and handles the cases of
927 -- parameters, conversions etc. But we can't do that in the bit
928 -- packed case or the VM case, because addresses don't work there.
930 if not Is_Bit_Packed_Array
(L_Type
) and then VM_Target
= No_VM
then
934 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
935 Make_Attribute_Reference
(Loc
,
937 Make_Indexed_Component
(Loc
,
939 Duplicate_Subexpr_Move_Checks
(Larray
, True),
940 Expressions
=> New_List
(
941 Make_Attribute_Reference
(Loc
,
945 Attribute_Name
=> Name_First
))),
946 Attribute_Name
=> Name_Address
)),
949 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
950 Make_Attribute_Reference
(Loc
,
952 Make_Indexed_Component
(Loc
,
954 Duplicate_Subexpr_Move_Checks
(Rarray
, True),
955 Expressions
=> New_List
(
956 Make_Attribute_Reference
(Loc
,
960 Attribute_Name
=> Name_First
))),
961 Attribute_Name
=> Name_Address
)));
963 -- For the bit packed and VM cases we use the bounds. That's OK,
964 -- because we don't have to worry about parameters, since they
965 -- cannot cause overlap. Perhaps we should worry about weird slice
971 Cleft_Lo
:= New_Copy_Tree
(Left_Lo
);
972 Cright_Lo
:= New_Copy_Tree
(Right_Lo
);
974 -- If the types do not match we add an implicit conversion
975 -- here to ensure proper match
977 if Etype
(Left_Lo
) /= Etype
(Right_Lo
) then
979 Unchecked_Convert_To
(Etype
(Left_Lo
), Cright_Lo
);
982 -- Reset the Analyzed flag, because the bounds of the index
983 -- type itself may be universal, and must must be reanalyzed
984 -- to acquire the proper type for the back end.
986 Set_Analyzed
(Cleft_Lo
, False);
987 Set_Analyzed
(Cright_Lo
, False);
991 Left_Opnd
=> Cleft_Lo
,
992 Right_Opnd
=> Cright_Lo
);
995 if Needs_Finalization
(Component_Type
(L_Type
))
996 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
998 and then not No_Ctrl_Actions
(N
)
1001 -- Call TSS procedure for array assignment, passing the
1002 -- explicit bounds of right and left hand sides.
1005 Proc
: constant Entity_Id
:=
1006 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
1010 Apply_Dereference
(Larray
);
1011 Apply_Dereference
(Rarray
);
1012 Actuals
:= New_List
(
1013 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1014 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1015 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
1016 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
1017 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
1018 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
1022 Right_Opnd
=> Condition
));
1025 Make_Procedure_Call_Statement
(Loc
,
1026 Name
=> New_Occurrence_Of
(Proc
, Loc
),
1027 Parameter_Associations
=> Actuals
));
1032 Make_Implicit_If_Statement
(N
,
1033 Condition
=> Condition
,
1035 Then_Statements
=> New_List
(
1036 Expand_Assign_Array_Loop
1037 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1040 Else_Statements
=> New_List
(
1041 Expand_Assign_Array_Loop
1042 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1047 Analyze
(N
, Suppress
=> All_Checks
);
1051 when RE_Not_Available
=>
1053 end Expand_Assign_Array
;
1055 ------------------------------
1056 -- Expand_Assign_Array_Loop --
1057 ------------------------------
1059 -- The following is an example of the loop generated for the case of a
1060 -- two-dimensional array:
1063 -- R2b : Tm1X1 := 1;
1065 -- for L1b in 1 .. 100 loop
1067 -- R4b : Tm1X2 := 1;
1069 -- for L3b in 1 .. 100 loop
1070 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
1071 -- R4b := Tm1X2'succ(R4b);
1074 -- R2b := Tm1X1'succ(R2b);
1078 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
1079 -- side. The declarations of R2b and R4b are inserted before the original
1080 -- assignment statement.
1082 function Expand_Assign_Array_Loop
1089 Rev
: Boolean) return Node_Id
1091 Loc
: constant Source_Ptr
:= Sloc
(N
);
1093 Lnn
: array (1 .. Ndim
) of Entity_Id
;
1094 Rnn
: array (1 .. Ndim
) of Entity_Id
;
1095 -- Entities used as subscripts on left and right sides
1097 L_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1098 R_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1099 -- Left and right index types
1106 function Build_Step
(J
: Nat
) return Node_Id
;
1107 -- The increment step for the index of the right-hand side is written
1108 -- as an attribute reference (Succ or Pred). This function returns
1109 -- the corresponding node, which is placed at the end of the loop body.
1115 function Build_Step
(J
: Nat
) return Node_Id
is
1127 Make_Assignment_Statement
(Loc
,
1128 Name
=> New_Occurrence_Of
(Rnn
(J
), Loc
),
1130 Make_Attribute_Reference
(Loc
,
1132 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1133 Attribute_Name
=> S_Or_P
,
1134 Expressions
=> New_List
(
1135 New_Occurrence_Of
(Rnn
(J
), Loc
))));
1137 -- Note that on the last iteration of the loop, the index is increased
1138 -- (or decreased) past the corresponding bound. This is consistent with
1139 -- the C semantics of the back-end, where such an off-by-one value on a
1140 -- dead index variable is OK. However, in CodePeer mode this leads to
1141 -- spurious warnings, and thus we place a guard around the attribute
1142 -- reference. For obvious reasons we only do this for CodePeer.
1144 if CodePeer_Mode
then
1146 Make_If_Statement
(Loc
,
1149 Left_Opnd
=> New_Occurrence_Of
(Lnn
(J
), Loc
),
1151 Make_Attribute_Reference
(Loc
,
1152 Prefix
=> New_Occurrence_Of
(L_Index_Type
(J
), Loc
),
1153 Attribute_Name
=> Lim
)),
1154 Then_Statements
=> New_List
(Step
));
1160 -- Start of processing for Expand_Assign_Array_Loop
1164 F_Or_L
:= Name_Last
;
1165 S_Or_P
:= Name_Pred
;
1167 F_Or_L
:= Name_First
;
1168 S_Or_P
:= Name_Succ
;
1171 -- Setup index types and subscript entities
1178 L_Index
:= First_Index
(L_Type
);
1179 R_Index
:= First_Index
(R_Type
);
1181 for J
in 1 .. Ndim
loop
1182 Lnn
(J
) := Make_Temporary
(Loc
, 'L');
1183 Rnn
(J
) := Make_Temporary
(Loc
, 'R');
1185 L_Index_Type
(J
) := Etype
(L_Index
);
1186 R_Index_Type
(J
) := Etype
(R_Index
);
1188 Next_Index
(L_Index
);
1189 Next_Index
(R_Index
);
1193 -- Now construct the assignment statement
1196 ExprL
: constant List_Id
:= New_List
;
1197 ExprR
: constant List_Id
:= New_List
;
1200 for J
in 1 .. Ndim
loop
1201 Append_To
(ExprL
, New_Occurrence_Of
(Lnn
(J
), Loc
));
1202 Append_To
(ExprR
, New_Occurrence_Of
(Rnn
(J
), Loc
));
1206 Make_Assignment_Statement
(Loc
,
1208 Make_Indexed_Component
(Loc
,
1209 Prefix
=> Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1210 Expressions
=> ExprL
),
1212 Make_Indexed_Component
(Loc
,
1213 Prefix
=> Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1214 Expressions
=> ExprR
));
1216 -- We set assignment OK, since there are some cases, e.g. in object
1217 -- declarations, where we are actually assigning into a constant.
1218 -- If there really is an illegality, it was caught long before now,
1219 -- and was flagged when the original assignment was analyzed.
1221 Set_Assignment_OK
(Name
(Assign
));
1223 -- Propagate the No_Ctrl_Actions flag to individual assignments
1225 Set_No_Ctrl_Actions
(Assign
, No_Ctrl_Actions
(N
));
1228 -- Now construct the loop from the inside out, with the last subscript
1229 -- varying most rapidly. Note that Assign is first the raw assignment
1230 -- statement, and then subsequently the loop that wraps it up.
1232 for J
in reverse 1 .. Ndim
loop
1234 Make_Block_Statement
(Loc
,
1235 Declarations
=> New_List
(
1236 Make_Object_Declaration
(Loc
,
1237 Defining_Identifier
=> Rnn
(J
),
1238 Object_Definition
=>
1239 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1241 Make_Attribute_Reference
(Loc
,
1242 Prefix
=> New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1243 Attribute_Name
=> F_Or_L
))),
1245 Handled_Statement_Sequence
=>
1246 Make_Handled_Sequence_Of_Statements
(Loc
,
1247 Statements
=> New_List
(
1248 Make_Implicit_Loop_Statement
(N
,
1250 Make_Iteration_Scheme
(Loc
,
1251 Loop_Parameter_Specification
=>
1252 Make_Loop_Parameter_Specification
(Loc
,
1253 Defining_Identifier
=> Lnn
(J
),
1254 Reverse_Present
=> Rev
,
1255 Discrete_Subtype_Definition
=>
1256 New_Occurrence_Of
(L_Index_Type
(J
), Loc
))),
1258 Statements
=> New_List
(Assign
, Build_Step
(J
))))));
1262 end Expand_Assign_Array_Loop
;
1264 --------------------------
1265 -- Expand_Assign_Record --
1266 --------------------------
1268 procedure Expand_Assign_Record
(N
: Node_Id
) is
1269 Lhs
: constant Node_Id
:= Name
(N
);
1270 Rhs
: Node_Id
:= Expression
(N
);
1271 L_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Lhs
));
1274 -- If change of representation, then extract the real right hand side
1275 -- from the type conversion, and proceed with component-wise assignment,
1276 -- since the two types are not the same as far as the back end is
1279 if Change_Of_Representation
(N
) then
1280 Rhs
:= Expression
(Rhs
);
1282 -- If this may be a case of a large bit aligned component, then proceed
1283 -- with component-wise assignment, to avoid possible clobbering of other
1284 -- components sharing bits in the first or last byte of the component to
1287 elsif Possible_Bit_Aligned_Component
(Lhs
)
1289 Possible_Bit_Aligned_Component
(Rhs
)
1293 -- If we have a tagged type that has a complete record representation
1294 -- clause, we must do we must do component-wise assignments, since child
1295 -- types may have used gaps for their components, and we might be
1296 -- dealing with a view conversion.
1298 elsif Is_Fully_Repped_Tagged_Type
(L_Typ
) then
1301 -- If neither condition met, then nothing special to do, the back end
1302 -- can handle assignment of the entire component as a single entity.
1308 -- At this stage we know that we must do a component wise assignment
1311 Loc
: constant Source_Ptr
:= Sloc
(N
);
1312 R_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Rhs
));
1313 Decl
: constant Node_Id
:= Declaration_Node
(R_Typ
);
1317 function Find_Component
1319 Comp
: Entity_Id
) return Entity_Id
;
1320 -- Find the component with the given name in the underlying record
1321 -- declaration for Typ. We need to use the actual entity because the
1322 -- type may be private and resolution by identifier alone would fail.
1324 function Make_Component_List_Assign
1326 U_U
: Boolean := False) return List_Id
;
1327 -- Returns a sequence of statements to assign the components that
1328 -- are referenced in the given component list. The flag U_U is
1329 -- used to force the usage of the inferred value of the variant
1330 -- part expression as the switch for the generated case statement.
1332 function Make_Field_Assign
1334 U_U
: Boolean := False) return Node_Id
;
1335 -- Given C, the entity for a discriminant or component, build an
1336 -- assignment for the corresponding field values. The flag U_U
1337 -- signals the presence of an Unchecked_Union and forces the usage
1338 -- of the inferred discriminant value of C as the right hand side
1339 -- of the assignment.
1341 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
;
1342 -- Given CI, a component items list, construct series of statements
1343 -- for fieldwise assignment of the corresponding components.
1345 --------------------
1346 -- Find_Component --
1347 --------------------
1349 function Find_Component
1351 Comp
: Entity_Id
) return Entity_Id
1353 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
1357 C
:= First_Entity
(Utyp
);
1358 while Present
(C
) loop
1359 if Chars
(C
) = Chars
(Comp
) then
1366 raise Program_Error
;
1369 --------------------------------
1370 -- Make_Component_List_Assign --
1371 --------------------------------
1373 function Make_Component_List_Assign
1375 U_U
: Boolean := False) return List_Id
1377 CI
: constant List_Id
:= Component_Items
(CL
);
1378 VP
: constant Node_Id
:= Variant_Part
(CL
);
1388 Result
:= Make_Field_Assigns
(CI
);
1390 if Present
(VP
) then
1391 V
:= First_Non_Pragma
(Variants
(VP
));
1393 while Present
(V
) loop
1395 DC
:= First
(Discrete_Choices
(V
));
1396 while Present
(DC
) loop
1397 Append_To
(DCH
, New_Copy_Tree
(DC
));
1402 Make_Case_Statement_Alternative
(Loc
,
1403 Discrete_Choices
=> DCH
,
1405 Make_Component_List_Assign
(Component_List
(V
))));
1406 Next_Non_Pragma
(V
);
1409 -- If we have an Unchecked_Union, use the value of the inferred
1410 -- discriminant of the variant part expression as the switch
1411 -- for the case statement. The case statement may later be
1416 New_Copy
(Get_Discriminant_Value
(
1419 Discriminant_Constraint
(Etype
(Rhs
))));
1422 Make_Selected_Component
(Loc
,
1423 Prefix
=> Duplicate_Subexpr
(Rhs
),
1425 Make_Identifier
(Loc
, Chars
(Name
(VP
))));
1429 Make_Case_Statement
(Loc
,
1431 Alternatives
=> Alts
));
1435 end Make_Component_List_Assign
;
1437 -----------------------
1438 -- Make_Field_Assign --
1439 -----------------------
1441 function Make_Field_Assign
1443 U_U
: Boolean := False) return Node_Id
1449 -- In the case of an Unchecked_Union, use the discriminant
1450 -- constraint value as on the right hand side of the assignment.
1454 New_Copy
(Get_Discriminant_Value
(C
,
1456 Discriminant_Constraint
(Etype
(Rhs
))));
1459 Make_Selected_Component
(Loc
,
1460 Prefix
=> Duplicate_Subexpr
(Rhs
),
1461 Selector_Name
=> New_Occurrence_Of
(C
, Loc
));
1465 Make_Assignment_Statement
(Loc
,
1467 Make_Selected_Component
(Loc
,
1468 Prefix
=> Duplicate_Subexpr
(Lhs
),
1470 New_Occurrence_Of
(Find_Component
(L_Typ
, C
), Loc
)),
1471 Expression
=> Expr
);
1473 -- Set Assignment_OK, so discriminants can be assigned
1475 Set_Assignment_OK
(Name
(A
), True);
1477 if Componentwise_Assignment
(N
)
1478 and then Nkind
(Name
(A
)) = N_Selected_Component
1479 and then Chars
(Selector_Name
(Name
(A
))) = Name_uParent
1481 Set_Componentwise_Assignment
(A
);
1485 end Make_Field_Assign
;
1487 ------------------------
1488 -- Make_Field_Assigns --
1489 ------------------------
1491 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
is
1499 while Present
(Item
) loop
1501 -- Look for components, but exclude _tag field assignment if
1502 -- the special Componentwise_Assignment flag is set.
1504 if Nkind
(Item
) = N_Component_Declaration
1505 and then not (Is_Tag
(Defining_Identifier
(Item
))
1506 and then Componentwise_Assignment
(N
))
1509 (Result
, Make_Field_Assign
(Defining_Identifier
(Item
)));
1516 end Make_Field_Assigns
;
1518 -- Start of processing for Expand_Assign_Record
1521 -- Note that we use the base types for this processing. This results
1522 -- in some extra work in the constrained case, but the change of
1523 -- representation case is so unusual that it is not worth the effort.
1525 -- First copy the discriminants. This is done unconditionally. It
1526 -- is required in the unconstrained left side case, and also in the
1527 -- case where this assignment was constructed during the expansion
1528 -- of a type conversion (since initialization of discriminants is
1529 -- suppressed in this case). It is unnecessary but harmless in
1532 if Has_Discriminants
(L_Typ
) then
1533 F
:= First_Discriminant
(R_Typ
);
1534 while Present
(F
) loop
1536 -- If we are expanding the initialization of a derived record
1537 -- that constrains or renames discriminants of the parent, we
1538 -- must use the corresponding discriminant in the parent.
1545 and then Present
(Corresponding_Discriminant
(F
))
1547 CF
:= Corresponding_Discriminant
(F
);
1552 if Is_Unchecked_Union
(Base_Type
(R_Typ
)) then
1554 -- Within an initialization procedure this is the
1555 -- assignment to an unchecked union component, in which
1556 -- case there is no discriminant to initialize.
1558 if Inside_Init_Proc
then
1562 -- The assignment is part of a conversion from a
1563 -- derived unchecked union type with an inferable
1564 -- discriminant, to a parent type.
1566 Insert_Action
(N
, Make_Field_Assign
(CF
, True));
1570 Insert_Action
(N
, Make_Field_Assign
(CF
));
1573 Next_Discriminant
(F
);
1578 -- We know the underlying type is a record, but its current view
1579 -- may be private. We must retrieve the usable record declaration.
1581 if Nkind_In
(Decl
, N_Private_Type_Declaration
,
1582 N_Private_Extension_Declaration
)
1583 and then Present
(Full_View
(R_Typ
))
1585 RDef
:= Type_Definition
(Declaration_Node
(Full_View
(R_Typ
)));
1587 RDef
:= Type_Definition
(Decl
);
1590 if Nkind
(RDef
) = N_Derived_Type_Definition
then
1591 RDef
:= Record_Extension_Part
(RDef
);
1594 if Nkind
(RDef
) = N_Record_Definition
1595 and then Present
(Component_List
(RDef
))
1597 if Is_Unchecked_Union
(R_Typ
) then
1599 Make_Component_List_Assign
(Component_List
(RDef
), True));
1602 (N
, Make_Component_List_Assign
(Component_List
(RDef
)));
1605 Rewrite
(N
, Make_Null_Statement
(Loc
));
1608 end Expand_Assign_Record
;
1610 -----------------------------------
1611 -- Expand_N_Assignment_Statement --
1612 -----------------------------------
1614 -- This procedure implements various cases where an assignment statement
1615 -- cannot just be passed on to the back end in untransformed state.
1617 procedure Expand_N_Assignment_Statement
(N
: Node_Id
) is
1618 Loc
: constant Source_Ptr
:= Sloc
(N
);
1619 Crep
: constant Boolean := Change_Of_Representation
(N
);
1620 Lhs
: constant Node_Id
:= Name
(N
);
1621 Rhs
: constant Node_Id
:= Expression
(N
);
1622 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Lhs
));
1626 -- Special case to check right away, if the Componentwise_Assignment
1627 -- flag is set, this is a reanalysis from the expansion of the primitive
1628 -- assignment procedure for a tagged type, and all we need to do is to
1629 -- expand to assignment of components, because otherwise, we would get
1630 -- infinite recursion (since this looks like a tagged assignment which
1631 -- would normally try to *call* the primitive assignment procedure).
1633 if Componentwise_Assignment
(N
) then
1634 Expand_Assign_Record
(N
);
1638 -- Defend against invalid subscripts on left side if we are in standard
1639 -- validity checking mode. No need to do this if we are checking all
1642 -- Note that we do this right away, because there are some early return
1643 -- paths in this procedure, and this is required on all paths.
1645 if Validity_Checks_On
1646 and then Validity_Check_Default
1647 and then not Validity_Check_Subscripts
1649 Check_Valid_Lvalue_Subscripts
(Lhs
);
1652 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1654 -- Rewrite an assignment to X'Priority into a run-time call
1656 -- For example: X'Priority := New_Prio_Expr;
1657 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1659 -- Note that although X'Priority is notionally an object, it is quite
1660 -- deliberately not defined as an aliased object in the RM. This means
1661 -- that it works fine to rewrite it as a call, without having to worry
1662 -- about complications that would other arise from X'Priority'Access,
1663 -- which is illegal, because of the lack of aliasing.
1665 if Ada_Version
>= Ada_2005
then
1668 Conctyp
: Entity_Id
;
1671 RT_Subprg_Name
: Node_Id
;
1674 -- Handle chains of renamings
1677 while Nkind
(Ent
) in N_Has_Entity
1678 and then Present
(Entity
(Ent
))
1679 and then Present
(Renamed_Object
(Entity
(Ent
)))
1681 Ent
:= Renamed_Object
(Entity
(Ent
));
1684 -- The attribute Priority applied to protected objects has been
1685 -- previously expanded into a call to the Get_Ceiling run-time
1688 if Nkind
(Ent
) = N_Function_Call
1689 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
1691 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
))
1693 -- Look for the enclosing concurrent type
1695 Conctyp
:= Current_Scope
;
1696 while not Is_Concurrent_Type
(Conctyp
) loop
1697 Conctyp
:= Scope
(Conctyp
);
1700 pragma Assert
(Is_Protected_Type
(Conctyp
));
1702 -- Generate the first actual of the call
1704 Subprg
:= Current_Scope
;
1705 while not Present
(Protected_Body_Subprogram
(Subprg
)) loop
1706 Subprg
:= Scope
(Subprg
);
1709 -- Select the appropriate run-time call
1711 if Number_Entries
(Conctyp
) = 0 then
1713 New_Occurrence_Of
(RTE
(RE_Set_Ceiling
), Loc
);
1716 New_Occurrence_Of
(RTE
(RO_PE_Set_Ceiling
), Loc
);
1720 Make_Procedure_Call_Statement
(Loc
,
1721 Name
=> RT_Subprg_Name
,
1722 Parameter_Associations
=> New_List
(
1723 New_Copy_Tree
(First
(Parameter_Associations
(Ent
))),
1724 Relocate_Node
(Expression
(N
))));
1733 -- Deal with assignment checks unless suppressed
1735 if not Suppress_Assignment_Checks
(N
) then
1737 -- First deal with generation of range check if required
1739 if Do_Range_Check
(Rhs
) then
1740 Generate_Range_Check
(Rhs
, Typ
, CE_Range_Check_Failed
);
1743 -- Then generate predicate check if required
1745 Apply_Predicate_Check
(Rhs
, Typ
);
1748 -- Check for a special case where a high level transformation is
1749 -- required. If we have either of:
1754 -- where P is a reference to a bit packed array, then we have to unwind
1755 -- the assignment. The exact meaning of being a reference to a bit
1756 -- packed array is as follows:
1758 -- An indexed component whose prefix is a bit packed array is a
1759 -- reference to a bit packed array.
1761 -- An indexed component or selected component whose prefix is a
1762 -- reference to a bit packed array is itself a reference ot a
1763 -- bit packed array.
1765 -- The required transformation is
1767 -- Tnn : prefix_type := P;
1768 -- Tnn.field := rhs;
1773 -- Tnn : prefix_type := P;
1774 -- Tnn (subscr) := rhs;
1777 -- Since P is going to be evaluated more than once, any subscripts
1778 -- in P must have their evaluation forced.
1780 if Nkind_In
(Lhs
, N_Indexed_Component
, N_Selected_Component
)
1781 and then Is_Ref_To_Bit_Packed_Array
(Prefix
(Lhs
))
1784 BPAR_Expr
: constant Node_Id
:= Relocate_Node
(Prefix
(Lhs
));
1785 BPAR_Typ
: constant Entity_Id
:= Etype
(BPAR_Expr
);
1786 Tnn
: constant Entity_Id
:=
1787 Make_Temporary
(Loc
, 'T', BPAR_Expr
);
1790 -- Insert the post assignment first, because we want to copy the
1791 -- BPAR_Expr tree before it gets analyzed in the context of the
1792 -- pre assignment. Note that we do not analyze the post assignment
1793 -- yet (we cannot till we have completed the analysis of the pre
1794 -- assignment). As usual, the analysis of this post assignment
1795 -- will happen on its own when we "run into" it after finishing
1796 -- the current assignment.
1799 Make_Assignment_Statement
(Loc
,
1800 Name
=> New_Copy_Tree
(BPAR_Expr
),
1801 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
1803 -- At this stage BPAR_Expr is a reference to a bit packed array
1804 -- where the reference was not expanded in the original tree,
1805 -- since it was on the left side of an assignment. But in the
1806 -- pre-assignment statement (the object definition), BPAR_Expr
1807 -- will end up on the right hand side, and must be reexpanded. To
1808 -- achieve this, we reset the analyzed flag of all selected and
1809 -- indexed components down to the actual indexed component for
1810 -- the packed array.
1814 Set_Analyzed
(Exp
, False);
1817 (Exp
, N_Selected_Component
, N_Indexed_Component
)
1819 Exp
:= Prefix
(Exp
);
1825 -- Now we can insert and analyze the pre-assignment
1827 -- If the right-hand side requires a transient scope, it has
1828 -- already been placed on the stack. However, the declaration is
1829 -- inserted in the tree outside of this scope, and must reflect
1830 -- the proper scope for its variable. This awkward bit is forced
1831 -- by the stricter scope discipline imposed by GCC 2.97.
1834 Uses_Transient_Scope
: constant Boolean :=
1836 and then N
= Node_To_Be_Wrapped
;
1839 if Uses_Transient_Scope
then
1840 Push_Scope
(Scope
(Current_Scope
));
1843 Insert_Before_And_Analyze
(N
,
1844 Make_Object_Declaration
(Loc
,
1845 Defining_Identifier
=> Tnn
,
1846 Object_Definition
=> New_Occurrence_Of
(BPAR_Typ
, Loc
),
1847 Expression
=> BPAR_Expr
));
1849 if Uses_Transient_Scope
then
1854 -- Now fix up the original assignment and continue processing
1856 Rewrite
(Prefix
(Lhs
),
1857 New_Occurrence_Of
(Tnn
, Loc
));
1859 -- We do not need to reanalyze that assignment, and we do not need
1860 -- to worry about references to the temporary, but we do need to
1861 -- make sure that the temporary is not marked as a true constant
1862 -- since we now have a generated assignment to it.
1864 Set_Is_True_Constant
(Tnn
, False);
1868 -- When we have the appropriate type of aggregate in the expression (it
1869 -- has been determined during analysis of the aggregate by setting the
1870 -- delay flag), let's perform in place assignment and thus avoid
1871 -- creating a temporary.
1873 if Is_Delayed_Aggregate
(Rhs
) then
1874 Convert_Aggr_In_Assignment
(N
);
1875 Rewrite
(N
, Make_Null_Statement
(Loc
));
1880 -- Apply discriminant check if required. If Lhs is an access type to a
1881 -- designated type with discriminants, we must always check. If the
1882 -- type has unknown discriminants, more elaborate processing below.
1884 if Has_Discriminants
(Etype
(Lhs
))
1885 and then not Has_Unknown_Discriminants
(Etype
(Lhs
))
1887 -- Skip discriminant check if change of representation. Will be
1888 -- done when the change of representation is expanded out.
1891 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1894 -- If the type is private without discriminants, and the full type
1895 -- has discriminants (necessarily with defaults) a check may still be
1896 -- necessary if the Lhs is aliased. The private discriminants must be
1897 -- visible to build the discriminant constraints.
1899 -- Only an explicit dereference that comes from source indicates
1900 -- aliasing. Access to formals of protected operations and entries
1901 -- create dereferences but are not semantic aliasings.
1903 elsif Is_Private_Type
(Etype
(Lhs
))
1904 and then Has_Discriminants
(Typ
)
1905 and then Nkind
(Lhs
) = N_Explicit_Dereference
1906 and then Comes_From_Source
(Lhs
)
1909 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1910 Ubt
: Entity_Id
:= Base_Type
(Typ
);
1913 -- In the case of an expander-generated record subtype whose base
1914 -- type still appears private, Typ will have been set to that
1915 -- private type rather than the underlying record type (because
1916 -- Underlying type will have returned the record subtype), so it's
1917 -- necessary to apply Underlying_Type again to the base type to
1918 -- get the record type we need for the discriminant check. Such
1919 -- subtypes can be created for assignments in certain cases, such
1920 -- as within an instantiation passed this kind of private type.
1921 -- It would be good to avoid this special test, but making changes
1922 -- to prevent this odd form of record subtype seems difficult. ???
1924 if Is_Private_Type
(Ubt
) then
1925 Ubt
:= Underlying_Type
(Ubt
);
1928 Set_Etype
(Lhs
, Ubt
);
1929 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Ubt
), Rhs
));
1930 Apply_Discriminant_Check
(Rhs
, Ubt
, Lhs
);
1931 Set_Etype
(Lhs
, Lt
);
1934 -- If the Lhs has a private type with unknown discriminants, it may
1935 -- have a full view with discriminants, but those are nameable only
1936 -- in the underlying type, so convert the Rhs to it before potential
1937 -- checking. Convert Lhs as well, otherwise the actual subtype might
1938 -- not be constructible.
1940 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1941 and then Has_Discriminants
(Typ
)
1943 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1944 Rewrite
(Lhs
, OK_Convert_To
(Base_Type
(Typ
), Lhs
));
1945 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1947 -- In the access type case, we need the same discriminant check, and
1948 -- also range checks if we have an access to constrained array.
1950 elsif Is_Access_Type
(Etype
(Lhs
))
1951 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1953 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1955 -- Skip discriminant check if change of representation. Will be
1956 -- done when the change of representation is expanded out.
1959 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1962 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1963 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1965 if Is_Constrained
(Etype
(Lhs
)) then
1966 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1969 if Nkind
(Rhs
) = N_Allocator
then
1971 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1972 C_Es
: Check_Result
;
1979 Etype
(Designated_Type
(Etype
(Lhs
))));
1991 -- Apply range check for access type case
1993 elsif Is_Access_Type
(Etype
(Lhs
))
1994 and then Nkind
(Rhs
) = N_Allocator
1995 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1997 Analyze_And_Resolve
(Expression
(Rhs
));
1999 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
2002 -- Ada 2005 (AI-231): Generate the run-time check
2004 if Is_Access_Type
(Typ
)
2005 and then Can_Never_Be_Null
(Etype
(Lhs
))
2006 and then not Can_Never_Be_Null
(Etype
(Rhs
))
2008 -- If an actual is an out parameter of a null-excluding access
2009 -- type, there is access check on entry, so we set the flag
2010 -- Suppress_Assignment_Checks on the generated statement to
2011 -- assign the actual to the parameter block, and we do not want
2012 -- to generate an additional check at this point.
2014 and then not Suppress_Assignment_Checks
(N
)
2016 Apply_Constraint_Check
(Rhs
, Etype
(Lhs
));
2019 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
2020 -- stand-alone obj of an anonymous access type.
2022 if Is_Access_Type
(Typ
)
2023 and then Is_Entity_Name
(Lhs
)
2024 and then Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
2027 function Lhs_Entity
return Entity_Id
;
2028 -- Look through renames to find the underlying entity.
2029 -- For assignment to a rename, we don't care about the
2030 -- Enclosing_Dynamic_Scope of the rename declaration.
2036 function Lhs_Entity
return Entity_Id
is
2037 Result
: Entity_Id
:= Entity
(Lhs
);
2040 while Present
(Renamed_Object
(Result
)) loop
2042 -- Renamed_Object must return an Entity_Name here
2043 -- because of preceding "Present (E_E_A (...))" test.
2045 Result
:= Entity
(Renamed_Object
(Result
));
2051 -- Local Declarations
2053 Access_Check
: constant Node_Id
:=
2054 Make_Raise_Program_Error
(Loc
,
2058 Dynamic_Accessibility_Level
(Rhs
),
2060 Make_Integer_Literal
(Loc
,
2063 (Enclosing_Dynamic_Scope
2065 Reason
=> PE_Accessibility_Check_Failed
);
2067 Access_Level_Update
: constant Node_Id
:=
2068 Make_Assignment_Statement
(Loc
,
2071 (Effective_Extra_Accessibility
2072 (Entity
(Lhs
)), Loc
),
2074 Dynamic_Accessibility_Level
(Rhs
));
2077 if not Accessibility_Checks_Suppressed
(Entity
(Lhs
)) then
2078 Insert_Action
(N
, Access_Check
);
2081 Insert_Action
(N
, Access_Level_Update
);
2085 -- Case of assignment to a bit packed array element. If there is a
2086 -- change of representation this must be expanded into components,
2087 -- otherwise this is a bit-field assignment.
2089 if Nkind
(Lhs
) = N_Indexed_Component
2090 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
2092 -- Normal case, no change of representation
2095 Expand_Bit_Packed_Element_Set
(N
);
2098 -- Change of representation case
2101 -- Generate the following, to force component-by-component
2102 -- assignments in an efficient way. Otherwise each component
2103 -- will require a temporary and two bit-field manipulations.
2110 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2116 Make_Object_Declaration
(Loc
,
2117 Defining_Identifier
=> Tnn
,
2118 Object_Definition
=>
2119 New_Occurrence_Of
(Etype
(Lhs
), Loc
)),
2120 Make_Assignment_Statement
(Loc
,
2121 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
2122 Expression
=> Relocate_Node
(Rhs
)),
2123 Make_Assignment_Statement
(Loc
,
2124 Name
=> Relocate_Node
(Lhs
),
2125 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
2127 Insert_Actions
(N
, Stats
);
2128 Rewrite
(N
, Make_Null_Statement
(Loc
));
2133 -- Build-in-place function call case. Note that we're not yet doing
2134 -- build-in-place for user-written assignment statements (the assignment
2135 -- here came from an aggregate.)
2137 elsif Ada_Version
>= Ada_2005
2138 and then Is_Build_In_Place_Function_Call
(Rhs
)
2140 Make_Build_In_Place_Call_In_Assignment
(N
, Rhs
);
2142 elsif Is_Tagged_Type
(Typ
) and then Is_Value_Type
(Etype
(Lhs
)) then
2144 -- Nothing to do for valuetypes
2145 -- ??? Set_Scope_Is_Transient (False);
2149 elsif Is_Tagged_Type
(Typ
)
2150 or else (Needs_Finalization
(Typ
) and then not Is_Array_Type
(Typ
))
2152 Tagged_Case
: declare
2153 L
: List_Id
:= No_List
;
2154 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
2157 -- In the controlled case, we ensure that function calls are
2158 -- evaluated before finalizing the target. In all cases, it makes
2159 -- the expansion easier if the side-effects are removed first.
2161 Remove_Side_Effects
(Lhs
);
2162 Remove_Side_Effects
(Rhs
);
2164 -- Avoid recursion in the mechanism
2168 -- If dispatching assignment, we need to dispatch to _assign
2170 if Is_Class_Wide_Type
(Typ
)
2172 -- If the type is tagged, we may as well use the predefined
2173 -- primitive assignment. This avoids inlining a lot of code
2174 -- and in the class-wide case, the assignment is replaced
2175 -- by a dispatching call to _assign. It is suppressed in the
2176 -- case of assignments created by the expander that correspond
2177 -- to initializations, where we do want to copy the tag
2178 -- (Expand_Ctrl_Actions flag is set False in this case). It is
2179 -- also suppressed if restriction No_Dispatching_Calls is in
2180 -- force because in that case predefined primitives are not
2183 or else (Is_Tagged_Type
(Typ
)
2184 and then not Is_Value_Type
(Etype
(Lhs
))
2185 and then Chars
(Current_Scope
) /= Name_uAssign
2186 and then Expand_Ctrl_Actions
2188 not Restriction_Active
(No_Dispatching_Calls
))
2190 if Is_Limited_Type
(Typ
) then
2192 -- This can happen in an instance when the formal is an
2193 -- extension of a limited interface, and the actual is
2194 -- limited. This is an error according to AI05-0087, but
2195 -- is not caught at the point of instantiation in earlier
2198 -- This is wrong, error messages cannot be issued during
2199 -- expansion, since they would be missed in -gnatc mode ???
2201 Error_Msg_N
("assignment not available on limited type", N
);
2205 -- Fetch the primitive op _assign and proper type to call it.
2206 -- Because of possible conflicts between private and full view,
2207 -- fetch the proper type directly from the operation profile.
2210 Op
: constant Entity_Id
:=
2211 Find_Prim_Op
(Typ
, Name_uAssign
);
2212 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
2215 -- If the assignment is dispatching, make sure to use the
2218 if Is_Class_Wide_Type
(Typ
) then
2219 F_Typ
:= Class_Wide_Type
(F_Typ
);
2224 -- In case of assignment to a class-wide tagged type, before
2225 -- the assignment we generate run-time check to ensure that
2226 -- the tags of source and target match.
2228 if not Tag_Checks_Suppressed
(Typ
)
2229 and then Is_Class_Wide_Type
(Typ
)
2230 and then Is_Tagged_Type
(Typ
)
2231 and then Is_Tagged_Type
(Underlying_Type
(Etype
(Rhs
)))
2234 Make_Raise_Constraint_Error
(Loc
,
2238 Make_Selected_Component
(Loc
,
2239 Prefix
=> Duplicate_Subexpr
(Lhs
),
2241 Make_Identifier
(Loc
, Name_uTag
)),
2243 Make_Selected_Component
(Loc
,
2244 Prefix
=> Duplicate_Subexpr
(Rhs
),
2246 Make_Identifier
(Loc
, Name_uTag
))),
2247 Reason
=> CE_Tag_Check_Failed
));
2251 Left_N
: Node_Id
:= Duplicate_Subexpr
(Lhs
);
2252 Right_N
: Node_Id
:= Duplicate_Subexpr
(Rhs
);
2255 -- In order to dispatch the call to _assign the type of
2256 -- the actuals must match. Add conversion (if required).
2258 if Etype
(Lhs
) /= F_Typ
then
2259 Left_N
:= Unchecked_Convert_To
(F_Typ
, Left_N
);
2262 if Etype
(Rhs
) /= F_Typ
then
2263 Right_N
:= Unchecked_Convert_To
(F_Typ
, Right_N
);
2267 Make_Procedure_Call_Statement
(Loc
,
2268 Name
=> New_Occurrence_Of
(Op
, Loc
),
2269 Parameter_Associations
=> New_List
(
2271 Node2
=> Right_N
)));
2276 L
:= Make_Tag_Ctrl_Assignment
(N
);
2278 -- We can't afford to have destructive Finalization Actions in
2279 -- the Self assignment case, so if the target and the source
2280 -- are not obviously different, code is generated to avoid the
2281 -- self assignment case:
2283 -- if lhs'address /= rhs'address then
2284 -- <code for controlled and/or tagged assignment>
2287 -- Skip this if Restriction (No_Finalization) is active
2289 if not Statically_Different
(Lhs
, Rhs
)
2290 and then Expand_Ctrl_Actions
2291 and then not Restriction_Active
(No_Finalization
)
2294 Make_Implicit_If_Statement
(N
,
2298 Make_Attribute_Reference
(Loc
,
2299 Prefix
=> Duplicate_Subexpr
(Lhs
),
2300 Attribute_Name
=> Name_Address
),
2303 Make_Attribute_Reference
(Loc
,
2304 Prefix
=> Duplicate_Subexpr
(Rhs
),
2305 Attribute_Name
=> Name_Address
)),
2307 Then_Statements
=> L
));
2310 -- We need to set up an exception handler for implementing
2311 -- 7.6.1(18). The remaining adjustments are tackled by the
2312 -- implementation of adjust for record_controllers (see
2315 -- This is skipped if we have no finalization
2317 if Expand_Ctrl_Actions
2318 and then not Restriction_Active
(No_Finalization
)
2321 Make_Block_Statement
(Loc
,
2322 Handled_Statement_Sequence
=>
2323 Make_Handled_Sequence_Of_Statements
(Loc
,
2325 Exception_Handlers
=> New_List
(
2326 Make_Handler_For_Ctrl_Operation
(Loc
)))));
2331 Make_Block_Statement
(Loc
,
2332 Handled_Statement_Sequence
=>
2333 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
2335 -- If no restrictions on aborts, protect the whole assignment
2336 -- for controlled objects as per 9.8(11).
2338 if Needs_Finalization
(Typ
)
2339 and then Expand_Ctrl_Actions
2340 and then Abort_Allowed
2343 Blk
: constant Entity_Id
:=
2345 (E_Block
, Current_Scope
, Sloc
(N
), 'B');
2346 AUD
: constant Entity_Id
:= RTE
(RE_Abort_Undefer_Direct
);
2349 Set_Scope
(Blk
, Current_Scope
);
2350 Set_Etype
(Blk
, Standard_Void_Type
);
2351 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
2353 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2354 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
2355 New_Occurrence_Of
(AUD
, Loc
));
2357 -- Present the Abort_Undefer_Direct function to the backend
2358 -- so that it can inline the call to the function.
2360 Add_Inlined_Body
(AUD
, N
);
2362 Expand_At_End_Handler
2363 (Handled_Statement_Sequence
(N
), Blk
);
2367 -- N has been rewritten to a block statement for which it is
2368 -- known by construction that no checks are necessary: analyze
2369 -- it with all checks suppressed.
2371 Analyze
(N
, Suppress
=> All_Checks
);
2377 elsif Is_Array_Type
(Typ
) then
2379 Actual_Rhs
: Node_Id
:= Rhs
;
2382 while Nkind_In
(Actual_Rhs
, N_Type_Conversion
,
2383 N_Qualified_Expression
)
2385 Actual_Rhs
:= Expression
(Actual_Rhs
);
2388 Expand_Assign_Array
(N
, Actual_Rhs
);
2394 elsif Is_Record_Type
(Typ
) then
2395 Expand_Assign_Record
(N
);
2398 -- Scalar types. This is where we perform the processing related to the
2399 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2402 elsif Is_Scalar_Type
(Typ
) then
2404 -- Case where right side is known valid
2406 if Expr_Known_Valid
(Rhs
) then
2408 -- Here the right side is valid, so it is fine. The case to deal
2409 -- with is when the left side is a local variable reference whose
2410 -- value is not currently known to be valid. If this is the case,
2411 -- and the assignment appears in an unconditional context, then
2412 -- we can mark the left side as now being valid if one of these
2413 -- conditions holds:
2415 -- The expression of the right side has Do_Range_Check set so
2416 -- that we know a range check will be performed. Note that it
2417 -- can be the case that a range check is omitted because we
2418 -- make the assumption that we can assume validity for operands
2419 -- appearing in the right side in determining whether a range
2420 -- check is required
2422 -- The subtype of the right side matches the subtype of the
2423 -- left side. In this case, even though we have not checked
2424 -- the range of the right side, we know it is in range of its
2425 -- subtype if the expression is valid.
2427 if Is_Local_Variable_Reference
(Lhs
)
2428 and then not Is_Known_Valid
(Entity
(Lhs
))
2429 and then In_Unconditional_Context
(N
)
2431 if Do_Range_Check
(Rhs
)
2432 or else Etype
(Lhs
) = Etype
(Rhs
)
2434 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2438 -- Case where right side may be invalid in the sense of the RM
2439 -- reference above. The RM does not require that we check for the
2440 -- validity on an assignment, but it does require that the assignment
2441 -- of an invalid value not cause erroneous behavior.
2443 -- The general approach in GNAT is to use the Is_Known_Valid flag
2444 -- to avoid the need for validity checking on assignments. However
2445 -- in some cases, we have to do validity checking in order to make
2446 -- sure that the setting of this flag is correct.
2449 -- Validate right side if we are validating copies
2451 if Validity_Checks_On
2452 and then Validity_Check_Copies
2454 -- Skip this if left hand side is an array or record component
2455 -- and elementary component validity checks are suppressed.
2457 if Nkind_In
(Lhs
, N_Selected_Component
, N_Indexed_Component
)
2458 and then not Validity_Check_Components
2465 -- We can propagate this to the left side where appropriate
2467 if Is_Local_Variable_Reference
(Lhs
)
2468 and then not Is_Known_Valid
(Entity
(Lhs
))
2469 and then In_Unconditional_Context
(N
)
2471 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2474 -- Otherwise check to see what should be done
2476 -- If left side is a local variable, then we just set its flag to
2477 -- indicate that its value may no longer be valid, since we are
2478 -- copying a potentially invalid value.
2480 elsif Is_Local_Variable_Reference
(Lhs
) then
2481 Set_Is_Known_Valid
(Entity
(Lhs
), False);
2483 -- Check for case of a nonlocal variable on the left side which
2484 -- is currently known to be valid. In this case, we simply ensure
2485 -- that the right side is valid. We only play the game of copying
2486 -- validity status for local variables, since we are doing this
2487 -- statically, not by tracing the full flow graph.
2489 elsif Is_Entity_Name
(Lhs
)
2490 and then Is_Known_Valid
(Entity
(Lhs
))
2492 -- Note: If Validity_Checking mode is set to none, we ignore
2493 -- the Ensure_Valid call so don't worry about that case here.
2497 -- In all other cases, we can safely copy an invalid value without
2498 -- worrying about the status of the left side. Since it is not a
2499 -- variable reference it will not be considered
2500 -- as being known to be valid in any case.
2509 when RE_Not_Available
=>
2511 end Expand_N_Assignment_Statement
;
2513 ------------------------------
2514 -- Expand_N_Block_Statement --
2515 ------------------------------
2517 -- Encode entity names defined in block statement
2519 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
2521 Qualify_Entity_Names
(N
);
2522 end Expand_N_Block_Statement
;
2524 -----------------------------
2525 -- Expand_N_Case_Statement --
2526 -----------------------------
2528 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
2529 Loc
: constant Source_Ptr
:= Sloc
(N
);
2530 Expr
: constant Node_Id
:= Expression
(N
);
2538 -- Check for the situation where we know at compile time which branch
2541 if Compile_Time_Known_Value
(Expr
) then
2542 Alt
:= Find_Static_Alternative
(N
);
2544 -- Do not consider controlled objects found in a case statement which
2545 -- actually models a case expression because their early finalization
2546 -- will affect the result of the expression.
2548 if not From_Conditional_Expression
(N
) then
2549 Process_Statements_For_Controlled_Objects
(Alt
);
2552 -- Move statements from this alternative after the case statement.
2553 -- They are already analyzed, so will be skipped by the analyzer.
2555 Insert_List_After
(N
, Statements
(Alt
));
2557 -- That leaves the case statement as a shell. So now we can kill all
2558 -- other alternatives in the case statement.
2560 Kill_Dead_Code
(Expression
(N
));
2566 -- Loop through case alternatives, skipping pragmas, and skipping
2567 -- the one alternative that we select (and therefore retain).
2569 Dead_Alt
:= First
(Alternatives
(N
));
2570 while Present
(Dead_Alt
) loop
2572 and then Nkind
(Dead_Alt
) = N_Case_Statement_Alternative
2574 Kill_Dead_Code
(Statements
(Dead_Alt
), Warn_On_Deleted_Code
);
2581 Rewrite
(N
, Make_Null_Statement
(Loc
));
2585 -- Here if the choice is not determined at compile time
2588 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
2590 Others_Present
: Boolean;
2591 Others_Node
: Node_Id
;
2593 Then_Stms
: List_Id
;
2594 Else_Stms
: List_Id
;
2597 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
2598 Others_Present
:= True;
2599 Others_Node
:= Last_Alt
;
2601 Others_Present
:= False;
2604 -- First step is to worry about possible invalid argument. The RM
2605 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2606 -- outside the base range), then Constraint_Error must be raised.
2608 -- Case of validity check required (validity checks are on, the
2609 -- expression is not known to be valid, and the case statement
2610 -- comes from source -- no need to validity check internally
2611 -- generated case statements).
2613 if Validity_Check_Default
then
2614 Ensure_Valid
(Expr
);
2617 -- If there is only a single alternative, just replace it with the
2618 -- sequence of statements since obviously that is what is going to
2619 -- be executed in all cases.
2621 Len
:= List_Length
(Alternatives
(N
));
2625 -- We still need to evaluate the expression if it has any side
2628 Remove_Side_Effects
(Expression
(N
));
2629 Alt
:= First
(Alternatives
(N
));
2631 -- Do not consider controlled objects found in a case statement
2632 -- which actually models a case expression because their early
2633 -- finalization will affect the result of the expression.
2635 if not From_Conditional_Expression
(N
) then
2636 Process_Statements_For_Controlled_Objects
(Alt
);
2639 Insert_List_After
(N
, Statements
(Alt
));
2641 -- That leaves the case statement as a shell. The alternative that
2642 -- will be executed is reset to a null list. So now we can kill
2643 -- the entire case statement.
2645 Kill_Dead_Code
(Expression
(N
));
2646 Rewrite
(N
, Make_Null_Statement
(Loc
));
2649 -- An optimization. If there are only two alternatives, and only
2650 -- a single choice, then rewrite the whole case statement as an
2651 -- if statement, since this can result in subsequent optimizations.
2652 -- This helps not only with case statements in the source of a
2653 -- simple form, but also with generated code (discriminant check
2654 -- functions in particular).
2656 -- Note: it is OK to do this before expanding out choices for any
2657 -- static predicates, since the if statement processing will handle
2658 -- the static predicate case fine.
2661 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2663 if List_Length
(Chlist
) = 1 then
2664 Choice
:= First
(Chlist
);
2666 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2667 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2669 -- For TRUE, generate "expression", not expression = true
2671 if Nkind
(Choice
) = N_Identifier
2672 and then Entity
(Choice
) = Standard_True
2674 Cond
:= Expression
(N
);
2676 -- For FALSE, generate "expression" and switch then/else
2678 elsif Nkind
(Choice
) = N_Identifier
2679 and then Entity
(Choice
) = Standard_False
2681 Cond
:= Expression
(N
);
2682 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2683 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2685 -- For a range, generate "expression in range"
2687 elsif Nkind
(Choice
) = N_Range
2688 or else (Nkind
(Choice
) = N_Attribute_Reference
2689 and then Attribute_Name
(Choice
) = Name_Range
)
2690 or else (Is_Entity_Name
(Choice
)
2691 and then Is_Type
(Entity
(Choice
)))
2695 Left_Opnd
=> Expression
(N
),
2696 Right_Opnd
=> Relocate_Node
(Choice
));
2698 -- A subtype indication is not a legal operator in a membership
2699 -- test, so retrieve its range.
2701 elsif Nkind
(Choice
) = N_Subtype_Indication
then
2704 Left_Opnd
=> Expression
(N
),
2707 (Range_Expression
(Constraint
(Choice
))));
2709 -- For any other subexpression "expression = value"
2714 Left_Opnd
=> Expression
(N
),
2715 Right_Opnd
=> Relocate_Node
(Choice
));
2718 -- Now rewrite the case as an IF
2721 Make_If_Statement
(Loc
,
2723 Then_Statements
=> Then_Stms
,
2724 Else_Statements
=> Else_Stms
));
2730 -- If the last alternative is not an Others choice, replace it with
2731 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2732 -- the modified case statement, since it's only effect would be to
2733 -- compute the contents of the Others_Discrete_Choices which is not
2734 -- needed by the back end anyway.
2736 -- The reason for this is that the back end always needs some default
2737 -- for a switch, so if we have not supplied one in the processing
2738 -- above for validity checking, then we need to supply one here.
2740 if not Others_Present
then
2741 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2742 Set_Others_Discrete_Choices
2743 (Others_Node
, Discrete_Choices
(Last_Alt
));
2744 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2747 -- Deal with possible declarations of controlled objects, and also
2748 -- with rewriting choice sequences for static predicate references.
2750 Alt
:= First_Non_Pragma
(Alternatives
(N
));
2751 while Present
(Alt
) loop
2753 -- Do not consider controlled objects found in a case statement
2754 -- which actually models a case expression because their early
2755 -- finalization will affect the result of the expression.
2757 if not From_Conditional_Expression
(N
) then
2758 Process_Statements_For_Controlled_Objects
(Alt
);
2761 if Has_SP_Choice
(Alt
) then
2762 Expand_Static_Predicates_In_Choices
(Alt
);
2765 Next_Non_Pragma
(Alt
);
2768 end Expand_N_Case_Statement
;
2770 -----------------------------
2771 -- Expand_N_Exit_Statement --
2772 -----------------------------
2774 -- The only processing required is to deal with a possible C/Fortran
2775 -- boolean value used as the condition for the exit statement.
2777 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2779 Adjust_Condition
(Condition
(N
));
2780 end Expand_N_Exit_Statement
;
2782 ----------------------------------
2783 -- Expand_Formal_Container_Loop --
2784 ----------------------------------
2786 procedure Expand_Formal_Container_Loop
(N
: Node_Id
) is
2787 Loc
: constant Source_Ptr
:= Sloc
(N
);
2788 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2789 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2790 Cursor
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2791 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2792 Stats
: constant List_Id
:= Statements
(N
);
2800 -- The expansion resembles the one for Ada containers, but the
2801 -- primitives mention the domain of iteration explicitly, and
2802 -- function First applied to the container yields a cursor directly.
2804 -- Cursor : Cursor_type := First (Container);
2805 -- while Has_Element (Cursor, Container) loop
2806 -- <original loop statements>
2807 -- Cursor := Next (Container, Cursor);
2810 Build_Formal_Container_Iteration
2811 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2813 Set_Ekind
(Cursor
, E_Variable
);
2814 Append_To
(Stats
, Advance
);
2816 -- Build block to capture declaration of cursor entity.
2819 Make_Block_Statement
(Loc
,
2820 Declarations
=> New_List
(Init
),
2821 Handled_Statement_Sequence
=>
2822 Make_Handled_Sequence_Of_Statements
(Loc
,
2823 Statements
=> New_List
(New_Loop
)));
2825 Rewrite
(N
, Blk_Nod
);
2827 end Expand_Formal_Container_Loop
;
2829 ------------------------------------------
2830 -- Expand_Formal_Container_Element_Loop --
2831 ------------------------------------------
2833 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
) is
2834 Loc
: constant Source_Ptr
:= Sloc
(N
);
2835 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2836 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2837 Element
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2838 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2839 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
2840 Stats
: constant List_Id
:= Statements
(N
);
2842 Cursor
: constant Entity_Id
:=
2843 Make_Defining_Identifier
(Loc
,
2844 Chars
=> New_External_Name
(Chars
(Element
), 'C'));
2845 Elmt_Decl
: Node_Id
;
2848 Element_Op
: constant Entity_Id
:=
2849 Get_Iterable_Type_Primitive
(Container_Typ
, Name_Element
);
2856 -- For an element iterator, the Element aspect must be present,
2857 -- (this is checked during analysis) and the expansion takes the form:
2859 -- Cursor : Cursor_type := First (Container);
2860 -- Elmt : Element_Type;
2861 -- while Has_Element (Cursor, Container) loop
2862 -- Elmt := Element (Container, Cursor);
2863 -- <original loop statements>
2864 -- Cursor := Next (Container, Cursor);
2867 Build_Formal_Container_Iteration
2868 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2870 Set_Ekind
(Cursor
, E_Variable
);
2871 Insert_Action
(N
, Init
);
2873 -- Declaration for Element.
2876 Make_Object_Declaration
(Loc
,
2877 Defining_Identifier
=> Element
,
2878 Object_Definition
=> New_Occurrence_Of
(Etype
(Element_Op
), Loc
));
2880 -- The element is only modified in expanded code, so it appears as
2881 -- unassigned to the warning machinery. We must suppress this spurious
2882 -- warning explicitly.
2884 Set_Warnings_Off
(Element
);
2887 Make_Assignment_Statement
(Loc
,
2888 Name
=> New_Occurrence_Of
(Element
, Loc
),
2890 Make_Function_Call
(Loc
,
2891 Name
=> New_Occurrence_Of
(Element_Op
, Loc
),
2892 Parameter_Associations
=> New_List
(
2893 New_Occurrence_Of
(Container
, Loc
),
2894 New_Occurrence_Of
(Cursor
, Loc
))));
2896 Prepend
(Elmt_Ref
, Stats
);
2897 Append_To
(Stats
, Advance
);
2899 -- The loop is rewritten as a block, to hold the element declaration
2902 Make_Block_Statement
(Loc
,
2903 Declarations
=> New_List
(Elmt_Decl
),
2904 Handled_Statement_Sequence
=>
2905 Make_Handled_Sequence_Of_Statements
(Loc
,
2906 Statements
=> New_List
(New_Loop
)));
2908 Rewrite
(N
, New_Loop
);
2910 -- The loop parameter is declared by an object declaration, but within
2911 -- the loop we must prevent user assignments to it, so we analyze the
2912 -- declaration and reset the entity kind, before analyzing the rest of
2915 Analyze
(Elmt_Decl
);
2916 Set_Ekind
(Defining_Identifier
(Elmt_Decl
), E_Loop_Parameter
);
2917 Set_Assignment_OK
(Name
(Elmt_Ref
));
2920 end Expand_Formal_Container_Element_Loop
;
2922 -----------------------------
2923 -- Expand_N_Goto_Statement --
2924 -----------------------------
2926 -- Add poll before goto if polling active
2928 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2930 Generate_Poll_Call
(N
);
2931 end Expand_N_Goto_Statement
;
2933 ---------------------------
2934 -- Expand_N_If_Statement --
2935 ---------------------------
2937 -- First we deal with the case of C and Fortran convention boolean values,
2938 -- with zero/non-zero semantics.
2940 -- Second, we deal with the obvious rewriting for the cases where the
2941 -- condition of the IF is known at compile time to be True or False.
2943 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2944 -- rewrite as independent if statements. For example:
2955 -- <<condition actions of y>>
2961 -- This rewriting is needed if at least one elsif part has a non-empty
2962 -- Condition_Actions list. We also do the same processing if there is a
2963 -- constant condition in an elsif part (in conjunction with the first
2964 -- processing step mentioned above, for the recursive call made to deal
2965 -- with the created inner if, this deals with properly optimizing the
2966 -- cases of constant elsif conditions).
2968 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2969 Loc
: constant Source_Ptr
:= Sloc
(N
);
2974 Warn_If_Deleted
: constant Boolean :=
2975 Warn_On_Deleted_Code
and then Comes_From_Source
(N
);
2976 -- Indicates whether we want warnings when we delete branches of the
2977 -- if statement based on constant condition analysis. We never want
2978 -- these warnings for expander generated code.
2981 -- Do not consider controlled objects found in an if statement which
2982 -- actually models an if expression because their early finalization
2983 -- will affect the result of the expression.
2985 if not From_Conditional_Expression
(N
) then
2986 Process_Statements_For_Controlled_Objects
(N
);
2989 Adjust_Condition
(Condition
(N
));
2991 -- The following loop deals with constant conditions for the IF. We
2992 -- need a loop because as we eliminate False conditions, we grab the
2993 -- first elsif condition and use it as the primary condition.
2995 while Compile_Time_Known_Value
(Condition
(N
)) loop
2997 -- If condition is True, we can simply rewrite the if statement now
2998 -- by replacing it by the series of then statements.
3000 if Is_True
(Expr_Value
(Condition
(N
))) then
3002 -- All the else parts can be killed
3004 Kill_Dead_Code
(Elsif_Parts
(N
), Warn_If_Deleted
);
3005 Kill_Dead_Code
(Else_Statements
(N
), Warn_If_Deleted
);
3007 Hed
:= Remove_Head
(Then_Statements
(N
));
3008 Insert_List_After
(N
, Then_Statements
(N
));
3012 -- If condition is False, then we can delete the condition and
3013 -- the Then statements
3016 -- We do not delete the condition if constant condition warnings
3017 -- are enabled, since otherwise we end up deleting the desired
3018 -- warning. Of course the backend will get rid of this True/False
3019 -- test anyway, so nothing is lost here.
3021 if not Constant_Condition_Warnings
then
3022 Kill_Dead_Code
(Condition
(N
));
3025 Kill_Dead_Code
(Then_Statements
(N
), Warn_If_Deleted
);
3027 -- If there are no elsif statements, then we simply replace the
3028 -- entire if statement by the sequence of else statements.
3030 if No
(Elsif_Parts
(N
)) then
3031 if No
(Else_Statements
(N
))
3032 or else Is_Empty_List
(Else_Statements
(N
))
3035 Make_Null_Statement
(Sloc
(N
)));
3037 Hed
:= Remove_Head
(Else_Statements
(N
));
3038 Insert_List_After
(N
, Else_Statements
(N
));
3044 -- If there are elsif statements, the first of them becomes the
3045 -- if/then section of the rebuilt if statement This is the case
3046 -- where we loop to reprocess this copied condition.
3049 Hed
:= Remove_Head
(Elsif_Parts
(N
));
3050 Insert_Actions
(N
, Condition_Actions
(Hed
));
3051 Set_Condition
(N
, Condition
(Hed
));
3052 Set_Then_Statements
(N
, Then_Statements
(Hed
));
3054 -- Hed might have been captured as the condition determining
3055 -- the current value for an entity. Now it is detached from
3056 -- the tree, so a Current_Value pointer in the condition might
3057 -- need to be updated.
3059 Set_Current_Value_Condition
(N
);
3061 if Is_Empty_List
(Elsif_Parts
(N
)) then
3062 Set_Elsif_Parts
(N
, No_List
);
3068 -- Loop through elsif parts, dealing with constant conditions and
3069 -- possible condition actions that are present.
3071 if Present
(Elsif_Parts
(N
)) then
3072 E
:= First
(Elsif_Parts
(N
));
3073 while Present
(E
) loop
3075 -- Do not consider controlled objects found in an if statement
3076 -- which actually models an if expression because their early
3077 -- finalization will affect the result of the expression.
3079 if not From_Conditional_Expression
(N
) then
3080 Process_Statements_For_Controlled_Objects
(E
);
3083 Adjust_Condition
(Condition
(E
));
3085 -- If there are condition actions, then rewrite the if statement
3086 -- as indicated above. We also do the same rewrite for a True or
3087 -- False condition. The further processing of this constant
3088 -- condition is then done by the recursive call to expand the
3089 -- newly created if statement
3091 if Present
(Condition_Actions
(E
))
3092 or else Compile_Time_Known_Value
(Condition
(E
))
3094 -- Note this is not an implicit if statement, since it is part
3095 -- of an explicit if statement in the source (or of an implicit
3096 -- if statement that has already been tested).
3099 Make_If_Statement
(Sloc
(E
),
3100 Condition
=> Condition
(E
),
3101 Then_Statements
=> Then_Statements
(E
),
3102 Elsif_Parts
=> No_List
,
3103 Else_Statements
=> Else_Statements
(N
));
3105 -- Elsif parts for new if come from remaining elsif's of parent
3107 while Present
(Next
(E
)) loop
3108 if No
(Elsif_Parts
(New_If
)) then
3109 Set_Elsif_Parts
(New_If
, New_List
);
3112 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
3115 Set_Else_Statements
(N
, New_List
(New_If
));
3117 if Present
(Condition_Actions
(E
)) then
3118 Insert_List_Before
(New_If
, Condition_Actions
(E
));
3123 if Is_Empty_List
(Elsif_Parts
(N
)) then
3124 Set_Elsif_Parts
(N
, No_List
);
3130 -- No special processing for that elsif part, move to next
3138 -- Some more optimizations applicable if we still have an IF statement
3140 if Nkind
(N
) /= N_If_Statement
then
3144 -- Another optimization, special cases that can be simplified
3146 -- if expression then
3152 -- can be changed to:
3154 -- return expression;
3158 -- if expression then
3164 -- can be changed to:
3166 -- return not (expression);
3168 -- Only do these optimizations if we are at least at -O1 level and
3169 -- do not do them if control flow optimizations are suppressed.
3171 if Optimization_Level
> 0
3172 and then not Opt
.Suppress_Control_Flow_Optimizations
3174 if Nkind
(N
) = N_If_Statement
3175 and then No
(Elsif_Parts
(N
))
3176 and then Present
(Else_Statements
(N
))
3177 and then List_Length
(Then_Statements
(N
)) = 1
3178 and then List_Length
(Else_Statements
(N
)) = 1
3181 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
3182 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
3185 if Nkind
(Then_Stm
) = N_Simple_Return_Statement
3187 Nkind
(Else_Stm
) = N_Simple_Return_Statement
3190 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
3191 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
3194 if Nkind
(Then_Expr
) = N_Identifier
3196 Nkind
(Else_Expr
) = N_Identifier
3198 if Entity
(Then_Expr
) = Standard_True
3199 and then Entity
(Else_Expr
) = Standard_False
3202 Make_Simple_Return_Statement
(Loc
,
3203 Expression
=> Relocate_Node
(Condition
(N
))));
3207 elsif Entity
(Then_Expr
) = Standard_False
3208 and then Entity
(Else_Expr
) = Standard_True
3211 Make_Simple_Return_Statement
(Loc
,
3215 Relocate_Node
(Condition
(N
)))));
3225 end Expand_N_If_Statement
;
3227 --------------------------
3228 -- Expand_Iterator_Loop --
3229 --------------------------
3231 procedure Expand_Iterator_Loop
(N
: Node_Id
) is
3232 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3233 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3234 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3235 Loc
: constant Source_Ptr
:= Sloc
(N
);
3237 Container
: constant Node_Id
:= Name
(I_Spec
);
3238 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
3239 I_Kind
: constant Entity_Kind
:= Ekind
(Id
);
3241 Iterator
: Entity_Id
;
3243 Stats
: List_Id
:= Statements
(N
);
3246 -- Processing for arrays
3248 if Is_Array_Type
(Container_Typ
) then
3249 Expand_Iterator_Loop_Over_Array
(N
);
3252 elsif Has_Aspect
(Container_Typ
, Aspect_Iterable
) then
3253 if Of_Present
(I_Spec
) then
3254 Expand_Formal_Container_Element_Loop
(N
);
3256 Expand_Formal_Container_Loop
(N
);
3262 -- Processing for containers
3264 -- For an "of" iterator the name is a container expression, which
3265 -- is transformed into a call to the default iterator.
3267 -- For an iterator of the form "in" the name is a function call
3268 -- that delivers an iterator type.
3270 -- In both cases, analysis of the iterator has introduced an object
3271 -- declaration to capture the domain, so that Container is an entity.
3273 -- The for loop is expanded into a while loop which uses a container
3274 -- specific cursor to desgnate each element.
3276 -- Iter : Iterator_Type := Container.Iterate;
3277 -- Cursor : Cursor_type := First (Iter);
3278 -- while Has_Element (Iter) loop
3280 -- -- The block is added when Element_Type is controlled
3282 -- Obj : Pack.Element_Type := Element (Cursor);
3283 -- -- for the "of" loop form
3285 -- <original loop statements>
3288 -- Cursor := Iter.Next (Cursor);
3291 -- If "reverse" is present, then the initialization of the cursor
3292 -- uses Last and the step becomes Prev. Pack is the name of the
3293 -- scope where the container package is instantiated.
3296 Element_Type
: constant Entity_Id
:= Etype
(Id
);
3297 Iter_Type
: Entity_Id
;
3300 Name_Init
: Name_Id
;
3301 Name_Step
: Name_Id
;
3304 -- The type of the iterator is the return type of the Iterate
3305 -- function used. For the "of" form this is the default iterator
3306 -- for the type, otherwise it is the type of the explicit
3307 -- function used in the iterator specification. The most common
3308 -- case will be an Iterate function in the container package.
3310 -- The primitive operations of the container type may not be
3311 -- use-visible, so we introduce the name of the enclosing package
3312 -- in the declarations below. The Iterator type is declared in a
3313 -- an instance within the container package itself.
3315 -- If the container type is a derived type, the cursor type is
3316 -- found in the package of the parent type.
3318 if Is_Derived_Type
(Container_Typ
) then
3319 Pack
:= Scope
(Root_Type
(Container_Typ
));
3321 Pack
:= Scope
(Container_Typ
);
3324 Iter_Type
:= Etype
(Name
(I_Spec
));
3326 -- The "of" case uses an internally generated cursor whose type
3327 -- is found in the container package. The domain of iteration
3328 -- is expanded into a call to the default Iterator function, but
3329 -- this expansion does not take place in quantified expressions
3330 -- that are analyzed with expansion disabled, and in that case the
3331 -- type of the iterator must be obtained from the aspect.
3333 if Of_Present
(I_Spec
) then
3335 Default_Iter
: Entity_Id
;
3336 Container_Arg
: Node_Id
;
3339 function Get_Default_Iterator
3340 (T
: Entity_Id
) return Entity_Id
;
3341 -- If the container is a derived type, the aspect holds the
3342 -- parent operation. The required one is a primitive of the
3343 -- derived type and is either inherited or overridden.
3345 --------------------------
3346 -- Get_Default_Iterator --
3347 --------------------------
3349 function Get_Default_Iterator
3350 (T
: Entity_Id
) return Entity_Id
3352 Iter
: constant Entity_Id
:=
3353 Entity
(Find_Value_Of_Aspect
(T
, Aspect_Default_Iterator
));
3358 Container_Arg
:= New_Copy_Tree
(Container
);
3360 -- A previous version of GNAT allowed indexing aspects to
3361 -- be redefined on derived container types, while the
3362 -- default iterator was inherited from the aprent type.
3363 -- This non-standard extension is preserved temporarily for
3364 -- use by the modelling project under debug flag d.X.
3366 if Debug_Flag_Dot_XX
then
3367 if Base_Type
(Etype
(Container
)) /=
3368 Base_Type
(Etype
(First_Formal
(Iter
)))
3371 Make_Type_Conversion
(Loc
,
3374 (Etype
(First_Formal
(Iter
)), Loc
),
3375 Expression
=> Container_Arg
);
3380 elsif Is_Derived_Type
(T
) then
3382 -- The default iterator must be a primitive operation
3383 -- of the type, at the same dispatch slot position.
3385 Prim
:= First_Elmt
(Primitive_Operations
(T
));
3386 while Present
(Prim
) loop
3389 if Chars
(Op
) = Chars
(Iter
)
3390 and then DT_Position
(Op
) = DT_Position
(Iter
)
3398 -- default iterator must exist.
3400 pragma Assert
(False);
3402 else -- not a derived type
3405 end Get_Default_Iterator
;
3407 -- Start of processing for Handle_Of
3410 if Is_Class_Wide_Type
(Container_Typ
) then
3412 Get_Default_Iterator
(Etype
(Base_Type
(Container_Typ
)));
3415 Default_Iter
:= Get_Default_Iterator
(Etype
(Container
));
3418 Cursor
:= Make_Temporary
(Loc
, 'C');
3420 -- For an container element iterator, the iterator type
3421 -- is obtained from the corresponding aspect, whose return
3422 -- type is descended from the corresponding interface type
3423 -- in some instance of Ada.Iterator_Interfaces. The actuals
3424 -- of that instantiation are Cursor and Has_Element.
3426 Iter_Type
:= Etype
(Default_Iter
);
3428 -- The iterator type, which is a class_wide type, may itself
3429 -- be derived locally, so the desired instantiation is the
3430 -- scope of the root type of the iterator type.
3432 Pack
:= Scope
(Root_Type
(Etype
(Iter_Type
)));
3434 -- Rewrite domain of iteration as a call to the default
3435 -- iterator for the container type.
3437 Rewrite
(Name
(I_Spec
),
3438 Make_Function_Call
(Loc
,
3439 Name
=> New_Occurrence_Of
(Default_Iter
, Loc
),
3440 Parameter_Associations
=>
3441 New_List
(Container_Arg
)));
3442 Analyze_And_Resolve
(Name
(I_Spec
));
3444 -- Find cursor type in proper iterator package, which is an
3445 -- instantiation of Iterator_Interfaces.
3447 Ent
:= First_Entity
(Pack
);
3448 while Present
(Ent
) loop
3449 if Chars
(Ent
) = Name_Cursor
then
3450 Set_Etype
(Cursor
, Etype
(Ent
));
3457 -- Id : Element_Type renames Container (Cursor);
3458 -- This assumes that the container type has an indexing
3459 -- operation with Cursor. The check that this operation
3460 -- exists is performed in Check_Container_Indexing.
3463 Make_Object_Renaming_Declaration
(Loc
,
3464 Defining_Identifier
=> Id
,
3466 New_Occurrence_Of
(Element_Type
, Loc
),
3468 Make_Indexed_Component
(Loc
,
3469 Prefix
=> Relocate_Node
(Container_Arg
),
3471 New_List
(New_Occurrence_Of
(Cursor
, Loc
))));
3473 -- The defining identifier in the iterator is user-visible
3474 -- and must be visible in the debugger.
3476 Set_Debug_Info_Needed
(Id
);
3478 -- If the container does not have a variable indexing aspect,
3479 -- the element is a constant in the loop.
3481 if No
(Find_Value_Of_Aspect
3482 (Container_Typ
, Aspect_Variable_Indexing
))
3484 Set_Ekind
(Id
, E_Constant
);
3487 -- If the container holds controlled objects, wrap the loop
3488 -- statements and element renaming declaration with a block.
3489 -- This ensures that the result of Element (Cusor) is
3490 -- cleaned up after each iteration of the loop.
3492 if Needs_Finalization
(Element_Type
) then
3496 -- Id : Element_Type := Element (curosr);
3498 -- <original loop statements>
3502 Make_Block_Statement
(Loc
,
3503 Declarations
=> New_List
(Decl
),
3504 Handled_Statement_Sequence
=>
3505 Make_Handled_Sequence_Of_Statements
(Loc
,
3506 Statements
=> Stats
)));
3508 -- Elements do not need finalization
3511 Prepend_To
(Stats
, Decl
);
3515 -- X in Iterate (S) : type of iterator is type of explicitly
3516 -- given Iterate function, and the loop variable is the cursor.
3517 -- It will be assigned in the loop and must be a variable.
3523 Iterator
:= Make_Temporary
(Loc
, 'I');
3525 -- Determine the advancement and initialization steps for the
3528 -- Analysis of the expanded loop will verify that the container
3529 -- has a reverse iterator.
3531 if Reverse_Present
(I_Spec
) then
3532 Name_Init
:= Name_Last
;
3533 Name_Step
:= Name_Previous
;
3536 Name_Init
:= Name_First
;
3537 Name_Step
:= Name_Next
;
3540 -- For both iterator forms, add a call to the step operation to
3541 -- advance the cursor. Generate:
3543 -- Cursor := Iterator.Next (Cursor);
3547 -- Cursor := Next (Cursor);
3554 Make_Function_Call
(Loc
,
3556 Make_Selected_Component
(Loc
,
3557 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3558 Selector_Name
=> Make_Identifier
(Loc
, Name_Step
)),
3559 Parameter_Associations
=> New_List
(
3560 New_Occurrence_Of
(Cursor
, Loc
)));
3563 Make_Assignment_Statement
(Loc
,
3564 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
3565 Expression
=> Rhs
));
3566 Set_Assignment_OK
(Name
(Last
(Stats
)));
3570 -- while Iterator.Has_Element loop
3574 -- Has_Element is the second actual in the iterator package
3577 Make_Loop_Statement
(Loc
,
3579 Make_Iteration_Scheme
(Loc
,
3581 Make_Function_Call
(Loc
,
3584 Next_Entity
(First_Entity
(Pack
)), Loc
),
3585 Parameter_Associations
=>
3586 New_List
(New_Occurrence_Of
(Cursor
, Loc
)))),
3588 Statements
=> Stats
,
3589 End_Label
=> Empty
);
3591 -- If present, preserve identifier of loop, which can be used in
3592 -- an exit statement in the body.
3594 if Present
(Identifier
(N
)) then
3595 Set_Identifier
(New_Loop
, Relocate_Node
(Identifier
(N
)));
3598 -- Create the declarations for Iterator and cursor and insert them
3599 -- before the source loop. Given that the domain of iteration is
3600 -- already an entity, the iterator is just a renaming of that
3601 -- entity. Possible optimization ???
3604 -- I : Iterator_Type renames Container;
3605 -- C : Cursor_Type := Container.[First | Last];
3608 Make_Object_Renaming_Declaration
(Loc
,
3609 Defining_Identifier
=> Iterator
,
3610 Subtype_Mark
=> New_Occurrence_Of
(Iter_Type
, Loc
),
3611 Name
=> Relocate_Node
(Name
(I_Spec
))));
3613 -- Create declaration for cursor
3620 Make_Object_Declaration
(Loc
,
3621 Defining_Identifier
=> Cursor
,
3622 Object_Definition
=>
3623 New_Occurrence_Of
(Etype
(Cursor
), Loc
),
3625 Make_Selected_Component
(Loc
,
3626 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3628 Make_Identifier
(Loc
, Name_Init
)));
3630 -- The cursor is only modified in expanded code, so it appears
3631 -- as unassigned to the warning machinery. We must suppress
3632 -- this spurious warning explicitly. The cursor's kind is that of
3633 -- the original loop parameter (it is a constant if the domain of
3634 -- iteration is constant).
3636 Set_Warnings_Off
(Cursor
);
3637 Set_Assignment_OK
(Decl
);
3639 Insert_Action
(N
, Decl
);
3640 Set_Ekind
(Cursor
, I_Kind
);
3643 -- If the range of iteration is given by a function call that
3644 -- returns a container, the finalization actions have been saved
3645 -- in the Condition_Actions of the iterator. Insert them now at
3646 -- the head of the loop.
3648 if Present
(Condition_Actions
(Isc
)) then
3649 Insert_List_Before
(N
, Condition_Actions
(Isc
));
3653 Rewrite
(N
, New_Loop
);
3655 end Expand_Iterator_Loop
;
3657 -------------------------------------
3658 -- Expand_Iterator_Loop_Over_Array --
3659 -------------------------------------
3661 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
) is
3662 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3663 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3664 Array_Node
: constant Node_Id
:= Name
(I_Spec
);
3665 Array_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Array_Node
));
3666 Array_Dim
: constant Pos
:= Number_Dimensions
(Array_Typ
);
3667 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3668 Loc
: constant Source_Ptr
:= Sloc
(N
);
3669 Stats
: constant List_Id
:= Statements
(N
);
3670 Core_Loop
: Node_Id
;
3672 Iterator
: Entity_Id
;
3674 -- Start of processing for Expand_Iterator_Loop_Over_Array
3677 -- for Element of Array loop
3679 -- This case requires an internally generated cursor to iterate over
3682 if Of_Present
(I_Spec
) then
3683 Iterator
:= Make_Temporary
(Loc
, 'C');
3686 -- Element : Component_Type renames Array (Iterator);
3689 Make_Indexed_Component
(Loc
,
3690 Prefix
=> Relocate_Node
(Array_Node
),
3691 Expressions
=> New_List
(New_Occurrence_Of
(Iterator
, Loc
)));
3694 Make_Object_Renaming_Declaration
(Loc
,
3695 Defining_Identifier
=> Id
,
3697 New_Occurrence_Of
(Component_Type
(Array_Typ
), Loc
),
3700 -- Mark the loop variable as needing debug info, so that expansion
3701 -- of the renaming will result in Materialize_Entity getting set via
3702 -- Debug_Renaming_Declaration. (This setting is needed here because
3703 -- the setting in Freeze_Entity comes after the expansion, which is
3706 Set_Debug_Info_Needed
(Id
);
3708 -- for Index in Array loop
3710 -- This case utilizes the already given iterator name
3718 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3719 -- Element : Component_Type renames Array (Iterator);
3720 -- <original loop statements>
3724 Make_Loop_Statement
(Loc
,
3726 Make_Iteration_Scheme
(Loc
,
3727 Loop_Parameter_Specification
=>
3728 Make_Loop_Parameter_Specification
(Loc
,
3729 Defining_Identifier
=> Iterator
,
3730 Discrete_Subtype_Definition
=>
3731 Make_Attribute_Reference
(Loc
,
3732 Prefix
=> Relocate_Node
(Array_Node
),
3733 Attribute_Name
=> Name_Range
,
3734 Expressions
=> New_List
(
3735 Make_Integer_Literal
(Loc
, Array_Dim
))),
3736 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3737 Statements
=> Stats
,
3738 End_Label
=> Empty
);
3740 -- Processing for multidimensional array
3742 if Array_Dim
> 1 then
3743 for Dim
in 1 .. Array_Dim
- 1 loop
3744 Iterator
:= Make_Temporary
(Loc
, 'C');
3746 -- Generate the dimension loops starting from the innermost one
3748 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3753 Make_Loop_Statement
(Loc
,
3755 Make_Iteration_Scheme
(Loc
,
3756 Loop_Parameter_Specification
=>
3757 Make_Loop_Parameter_Specification
(Loc
,
3758 Defining_Identifier
=> Iterator
,
3759 Discrete_Subtype_Definition
=>
3760 Make_Attribute_Reference
(Loc
,
3761 Prefix
=> Relocate_Node
(Array_Node
),
3762 Attribute_Name
=> Name_Range
,
3763 Expressions
=> New_List
(
3764 Make_Integer_Literal
(Loc
, Array_Dim
- Dim
))),
3765 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3766 Statements
=> New_List
(Core_Loop
),
3767 End_Label
=> Empty
);
3769 -- Update the previously created object renaming declaration with
3770 -- the new iterator.
3772 Prepend_To
(Expressions
(Ind_Comp
),
3773 New_Occurrence_Of
(Iterator
, Loc
));
3777 -- Inherit the loop identifier from the original loop. This ensures that
3778 -- the scope stack is consistent after the rewriting.
3780 if Present
(Identifier
(N
)) then
3781 Set_Identifier
(Core_Loop
, Relocate_Node
(Identifier
(N
)));
3784 Rewrite
(N
, Core_Loop
);
3786 end Expand_Iterator_Loop_Over_Array
;
3788 -----------------------------
3789 -- Expand_N_Loop_Statement --
3790 -----------------------------
3792 -- 1. Remove null loop entirely
3793 -- 2. Deal with while condition for C/Fortran boolean
3794 -- 3. Deal with loops with a non-standard enumeration type range
3795 -- 4. Deal with while loops where Condition_Actions is set
3796 -- 5. Deal with loops over predicated subtypes
3797 -- 6. Deal with loops with iterators over arrays and containers
3798 -- 7. Insert polling call if required
3800 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
3801 Loc
: constant Source_Ptr
:= Sloc
(N
);
3802 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
3808 if Is_Null_Loop
(N
) then
3809 Rewrite
(N
, Make_Null_Statement
(Loc
));
3813 -- Deal with condition for C/Fortran Boolean
3815 if Present
(Scheme
) then
3816 Adjust_Condition
(Condition
(Scheme
));
3819 -- Generate polling call
3821 if Is_Non_Empty_List
(Statements
(N
)) then
3822 Generate_Poll_Call
(First
(Statements
(N
)));
3825 -- Nothing more to do for plain loop with no iteration scheme
3830 -- Case of for loop (Loop_Parameter_Specification present)
3832 -- Note: we do not have to worry about validity checking of the for loop
3833 -- range bounds here, since they were frozen with constant declarations
3834 -- and it is during that process that the validity checking is done.
3836 elsif Present
(Loop_Parameter_Specification
(Scheme
)) then
3838 LPS
: constant Node_Id
:=
3839 Loop_Parameter_Specification
(Scheme
);
3840 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3841 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3842 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
3848 -- Deal with loop over predicates
3850 if Is_Discrete_Type
(Ltype
)
3851 and then Present
(Predicate_Function
(Ltype
))
3853 Expand_Predicated_Loop
(N
);
3855 -- Handle the case where we have a for loop with the range type
3856 -- being an enumeration type with non-standard representation.
3857 -- In this case we expand:
3859 -- for x in [reverse] a .. b loop
3865 -- for xP in [reverse] integer
3866 -- range etype'Pos (a) .. etype'Pos (b)
3869 -- x : constant etype := Pos_To_Rep (xP);
3875 elsif Is_Enumeration_Type
(Btype
)
3876 and then Present
(Enum_Pos_To_Rep
(Btype
))
3879 Make_Defining_Identifier
(Loc
,
3880 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
3882 -- If the type has a contiguous representation, successive
3883 -- values can be generated as offsets from the first literal.
3885 if Has_Contiguous_Rep
(Btype
) then
3887 Unchecked_Convert_To
(Btype
,
3890 Make_Integer_Literal
(Loc
,
3891 Enumeration_Rep
(First_Literal
(Btype
))),
3892 Right_Opnd
=> New_Occurrence_Of
(New_Id
, Loc
)));
3894 -- Use the constructed array Enum_Pos_To_Rep
3897 Make_Indexed_Component
(Loc
,
3899 New_Occurrence_Of
(Enum_Pos_To_Rep
(Btype
), Loc
),
3901 New_List
(New_Occurrence_Of
(New_Id
, Loc
)));
3904 -- Build declaration for loop identifier
3908 Make_Object_Declaration
(Loc
,
3909 Defining_Identifier
=> Loop_Id
,
3910 Constant_Present
=> True,
3911 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
3912 Expression
=> Expr
));
3915 Make_Loop_Statement
(Loc
,
3916 Identifier
=> Identifier
(N
),
3919 Make_Iteration_Scheme
(Loc
,
3920 Loop_Parameter_Specification
=>
3921 Make_Loop_Parameter_Specification
(Loc
,
3922 Defining_Identifier
=> New_Id
,
3923 Reverse_Present
=> Reverse_Present
(LPS
),
3925 Discrete_Subtype_Definition
=>
3926 Make_Subtype_Indication
(Loc
,
3929 New_Occurrence_Of
(Standard_Natural
, Loc
),
3932 Make_Range_Constraint
(Loc
,
3937 Make_Attribute_Reference
(Loc
,
3939 New_Occurrence_Of
(Btype
, Loc
),
3941 Attribute_Name
=> Name_Pos
,
3943 Expressions
=> New_List
(
3945 (Type_Low_Bound
(Ltype
)))),
3948 Make_Attribute_Reference
(Loc
,
3950 New_Occurrence_Of
(Btype
, Loc
),
3952 Attribute_Name
=> Name_Pos
,
3954 Expressions
=> New_List
(
3959 Statements
=> New_List
(
3960 Make_Block_Statement
(Loc
,
3961 Declarations
=> Decls
,
3962 Handled_Statement_Sequence
=>
3963 Make_Handled_Sequence_Of_Statements
(Loc
,
3964 Statements
=> Statements
(N
)))),
3966 End_Label
=> End_Label
(N
)));
3968 -- The loop parameter's entity must be removed from the loop
3969 -- scope's entity list and rendered invisible, since it will
3970 -- now be located in the new block scope. Any other entities
3971 -- already associated with the loop scope, such as the loop
3972 -- parameter's subtype, will remain there.
3974 -- In an element loop, the loop will contain a declaration for
3975 -- a cursor variable; otherwise the loop id is the first entity
3976 -- in the scope constructed for the loop.
3978 if Comes_From_Source
(Loop_Id
) then
3979 pragma Assert
(First_Entity
(Scope
(Loop_Id
)) = Loop_Id
);
3983 Set_First_Entity
(Scope
(Loop_Id
), Next_Entity
(Loop_Id
));
3984 Remove_Homonym
(Loop_Id
);
3986 if Last_Entity
(Scope
(Loop_Id
)) = Loop_Id
then
3987 Set_Last_Entity
(Scope
(Loop_Id
), Empty
);
3992 -- Nothing to do with other cases of for loops
3999 -- Second case, if we have a while loop with Condition_Actions set, then
4000 -- we change it into a plain loop:
4009 -- <<condition actions>>
4014 elsif Present
(Scheme
)
4015 and then Present
(Condition_Actions
(Scheme
))
4016 and then Present
(Condition
(Scheme
))
4023 Make_Exit_Statement
(Sloc
(Condition
(Scheme
)),
4025 Make_Op_Not
(Sloc
(Condition
(Scheme
)),
4026 Right_Opnd
=> Condition
(Scheme
)));
4028 Prepend
(ES
, Statements
(N
));
4029 Insert_List_Before
(ES
, Condition_Actions
(Scheme
));
4031 -- This is not an implicit loop, since it is generated in response
4032 -- to the loop statement being processed. If this is itself
4033 -- implicit, the restriction has already been checked. If not,
4034 -- it is an explicit loop.
4037 Make_Loop_Statement
(Sloc
(N
),
4038 Identifier
=> Identifier
(N
),
4039 Statements
=> Statements
(N
),
4040 End_Label
=> End_Label
(N
)));
4045 -- Here to deal with iterator case
4047 elsif Present
(Scheme
)
4048 and then Present
(Iterator_Specification
(Scheme
))
4050 Expand_Iterator_Loop
(N
);
4052 -- An iterator loop may generate renaming declarations for elements
4053 -- that require debug information. This is the case in particular
4054 -- with element iterators, where debug information must be generated
4055 -- for the temporary that holds the element value. These temporaries
4056 -- are created within a transient block whose local declarations are
4057 -- transferred to the loop, which now has non-trivial local objects.
4059 if Nkind
(N
) = N_Loop_Statement
4060 and then Present
(Identifier
(N
))
4062 Qualify_Entity_Names
(N
);
4066 -- When the iteration scheme mentiones attribute 'Loop_Entry, the loop
4067 -- is transformed into a conditional block where the original loop is
4068 -- the sole statement. Inspect the statements of the nested loop for
4069 -- controlled objects.
4073 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
4074 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
4077 Process_Statements_For_Controlled_Objects
(Stmt
);
4078 end Expand_N_Loop_Statement
;
4080 ----------------------------
4081 -- Expand_Predicated_Loop --
4082 ----------------------------
4084 -- Note: the expander can handle generation of loops over predicated
4085 -- subtypes for both the dynamic and static cases. Depending on what
4086 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
4087 -- mode, the semantic analyzer may disallow one or both forms.
4089 procedure Expand_Predicated_Loop
(N
: Node_Id
) is
4090 Loc
: constant Source_Ptr
:= Sloc
(N
);
4091 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
4092 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
4093 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
4094 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
4095 Stat
: constant List_Id
:= Static_Discrete_Predicate
(Ltype
);
4096 Stmts
: constant List_Id
:= Statements
(N
);
4099 -- Case of iteration over non-static predicate, should not be possible
4100 -- since this is not allowed by the semantics and should have been
4101 -- caught during analysis of the loop statement.
4104 raise Program_Error
;
4106 -- If the predicate list is empty, that corresponds to a predicate of
4107 -- False, in which case the loop won't run at all, and we rewrite the
4108 -- entire loop as a null statement.
4110 elsif Is_Empty_List
(Stat
) then
4111 Rewrite
(N
, Make_Null_Statement
(Loc
));
4114 -- For expansion over a static predicate we generate the following
4117 -- J : Ltype := min-val;
4122 -- when endpoint => J := startpoint;
4123 -- when endpoint => J := startpoint;
4125 -- when max-val => exit;
4126 -- when others => J := Lval'Succ (J);
4131 -- with min-val replaced by max-val and Succ replaced by Pred if the
4132 -- loop parameter specification carries a Reverse indicator.
4134 -- To make this a little clearer, let's take a specific example:
4136 -- type Int is range 1 .. 10;
4137 -- subtype StaticP is Int with
4138 -- predicate => StaticP in 3 | 10 | 5 .. 7;
4140 -- for L in StaticP loop
4141 -- Put_Line ("static:" & J'Img);
4144 -- In this case, the loop is transformed into
4151 -- when 3 => J := 5;
4152 -- when 7 => J := 10;
4154 -- when others => J := L'Succ (J);
4160 Static_Predicate
: declare
4167 function Lo_Val
(N
: Node_Id
) return Node_Id
;
4168 -- Given static expression or static range, returns an identifier
4169 -- whose value is the low bound of the expression value or range.
4171 function Hi_Val
(N
: Node_Id
) return Node_Id
;
4172 -- Given static expression or static range, returns an identifier
4173 -- whose value is the high bound of the expression value or range.
4179 function Hi_Val
(N
: Node_Id
) return Node_Id
is
4181 if Is_OK_Static_Expression
(N
) then
4182 return New_Copy
(N
);
4184 pragma Assert
(Nkind
(N
) = N_Range
);
4185 return New_Copy
(High_Bound
(N
));
4193 function Lo_Val
(N
: Node_Id
) return Node_Id
is
4195 if Is_OK_Static_Expression
(N
) then
4196 return New_Copy
(N
);
4198 pragma Assert
(Nkind
(N
) = N_Range
);
4199 return New_Copy
(Low_Bound
(N
));
4203 -- Start of processing for Static_Predicate
4206 -- Convert loop identifier to normal variable and reanalyze it so
4207 -- that this conversion works. We have to use the same defining
4208 -- identifier, since there may be references in the loop body.
4210 Set_Analyzed
(Loop_Id
, False);
4211 Set_Ekind
(Loop_Id
, E_Variable
);
4213 -- In most loops the loop variable is assigned in various
4214 -- alternatives in the body. However, in the rare case when
4215 -- the range specifies a single element, the loop variable
4216 -- may trigger a spurious warning that is could be constant.
4217 -- This warning might as well be suppressed.
4219 Set_Warnings_Off
(Loop_Id
);
4221 -- Loop to create branches of case statement
4225 if Reverse_Present
(LPS
) then
4227 -- Initial value is largest value in predicate.
4230 Make_Object_Declaration
(Loc
,
4231 Defining_Identifier
=> Loop_Id
,
4232 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
4233 Expression
=> Hi_Val
(Last
(Stat
)));
4236 while Present
(P
) loop
4237 if No
(Prev
(P
)) then
4238 S
:= Make_Exit_Statement
(Loc
);
4241 Make_Assignment_Statement
(Loc
,
4242 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4243 Expression
=> Hi_Val
(Prev
(P
)));
4244 Set_Suppress_Assignment_Checks
(S
);
4248 Make_Case_Statement_Alternative
(Loc
,
4249 Statements
=> New_List
(S
),
4250 Discrete_Choices
=> New_List
(Lo_Val
(P
))));
4257 -- Initial value is smallest value in predicate.
4260 Make_Object_Declaration
(Loc
,
4261 Defining_Identifier
=> Loop_Id
,
4262 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
4263 Expression
=> Lo_Val
(First
(Stat
)));
4266 while Present
(P
) loop
4267 if No
(Next
(P
)) then
4268 S
:= Make_Exit_Statement
(Loc
);
4271 Make_Assignment_Statement
(Loc
,
4272 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4273 Expression
=> Lo_Val
(Next
(P
)));
4274 Set_Suppress_Assignment_Checks
(S
);
4278 Make_Case_Statement_Alternative
(Loc
,
4279 Statements
=> New_List
(S
),
4280 Discrete_Choices
=> New_List
(Hi_Val
(P
))));
4286 -- Add others choice
4289 Name_Next
: Name_Id
;
4292 if Reverse_Present
(LPS
) then
4293 Name_Next
:= Name_Pred
;
4295 Name_Next
:= Name_Succ
;
4299 Make_Assignment_Statement
(Loc
,
4300 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4302 Make_Attribute_Reference
(Loc
,
4303 Prefix
=> New_Occurrence_Of
(Ltype
, Loc
),
4304 Attribute_Name
=> Name_Next
,
4305 Expressions
=> New_List
(
4306 New_Occurrence_Of
(Loop_Id
, Loc
))));
4307 Set_Suppress_Assignment_Checks
(S
);
4311 Make_Case_Statement_Alternative
(Loc
,
4312 Discrete_Choices
=> New_List
(Make_Others_Choice
(Loc
)),
4313 Statements
=> New_List
(S
)));
4315 -- Construct case statement and append to body statements
4318 Make_Case_Statement
(Loc
,
4319 Expression
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4320 Alternatives
=> Alts
);
4321 Append_To
(Stmts
, Cstm
);
4325 Set_Suppress_Assignment_Checks
(D
);
4328 Make_Block_Statement
(Loc
,
4329 Declarations
=> New_List
(D
),
4330 Handled_Statement_Sequence
=>
4331 Make_Handled_Sequence_Of_Statements
(Loc
,
4332 Statements
=> New_List
(
4333 Make_Loop_Statement
(Loc
,
4334 Statements
=> Stmts
,
4335 End_Label
=> Empty
)))));
4338 end Static_Predicate
;
4340 end Expand_Predicated_Loop
;
4342 ------------------------------
4343 -- Make_Tag_Ctrl_Assignment --
4344 ------------------------------
4346 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
4347 Asn
: constant Node_Id
:= Relocate_Node
(N
);
4348 L
: constant Node_Id
:= Name
(N
);
4349 Loc
: constant Source_Ptr
:= Sloc
(N
);
4350 Res
: constant List_Id
:= New_List
;
4351 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
4353 Comp_Asn
: constant Boolean := Is_Fully_Repped_Tagged_Type
(T
);
4354 Ctrl_Act
: constant Boolean := Needs_Finalization
(T
)
4355 and then not No_Ctrl_Actions
(N
);
4356 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
4357 and then not Comp_Asn
4358 and then not No_Ctrl_Actions
(N
)
4359 and then Tagged_Type_Expansion
;
4360 -- Tags are not saved and restored when VM_Target because VM tags are
4361 -- represented implicitly in objects.
4363 Next_Id
: Entity_Id
;
4364 Prev_Id
: Entity_Id
;
4368 -- Finalize the target of the assignment when controlled
4370 -- We have two exceptions here:
4372 -- 1. If we are in an init proc since it is an initialization more
4373 -- than an assignment.
4375 -- 2. If the left-hand side is a temporary that was not initialized
4376 -- (or the parent part of a temporary since it is the case in
4377 -- extension aggregates). Such a temporary does not come from
4378 -- source. We must examine the original node for the prefix, because
4379 -- it may be a component of an entry formal, in which case it has
4380 -- been rewritten and does not appear to come from source either.
4382 -- Case of init proc
4384 if not Ctrl_Act
then
4387 -- The left hand side is an uninitialized temporary object
4389 elsif Nkind
(L
) = N_Type_Conversion
4390 and then Is_Entity_Name
(Expression
(L
))
4391 and then Nkind
(Parent
(Entity
(Expression
(L
)))) =
4392 N_Object_Declaration
4393 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
4400 (Obj_Ref
=> Duplicate_Subexpr_No_Checks
(L
),
4404 -- Save the Tag in a local variable Tag_Id
4407 Tag_Id
:= Make_Temporary
(Loc
, 'A');
4410 Make_Object_Declaration
(Loc
,
4411 Defining_Identifier
=> Tag_Id
,
4412 Object_Definition
=> New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
4414 Make_Selected_Component
(Loc
,
4415 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4417 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
))));
4419 -- Otherwise Tag_Id is not used
4425 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
4426 -- VM targets since the fields are not part of the object.
4428 if VM_Target
/= No_VM
4429 and then Is_Controlled
(T
)
4431 Prev_Id
:= Make_Temporary
(Loc
, 'P');
4432 Next_Id
:= Make_Temporary
(Loc
, 'N');
4435 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4438 Make_Object_Declaration
(Loc
,
4439 Defining_Identifier
=> Prev_Id
,
4440 Object_Definition
=>
4441 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4443 Make_Selected_Component
(Loc
,
4445 Unchecked_Convert_To
4446 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4448 Make_Identifier
(Loc
, Name_Prev
))));
4451 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4454 Make_Object_Declaration
(Loc
,
4455 Defining_Identifier
=> Next_Id
,
4456 Object_Definition
=>
4457 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4459 Make_Selected_Component
(Loc
,
4461 Unchecked_Convert_To
4462 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4464 Make_Identifier
(Loc
, Name_Next
))));
4467 -- If the tagged type has a full rep clause, expand the assignment into
4468 -- component-wise assignments. Mark the node as unanalyzed in order to
4469 -- generate the proper code and propagate this scenario by setting a
4470 -- flag to avoid infinite recursion.
4473 Set_Analyzed
(Asn
, False);
4474 Set_Componentwise_Assignment
(Asn
, True);
4477 Append_To
(Res
, Asn
);
4483 Make_Assignment_Statement
(Loc
,
4485 Make_Selected_Component
(Loc
,
4486 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4488 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
)),
4489 Expression
=> New_Occurrence_Of
(Tag_Id
, Loc
)));
4492 -- Restore the Prev and Next fields on .NET/JVM
4494 if VM_Target
/= No_VM
4495 and then Is_Controlled
(T
)
4498 -- Root_Controlled (L).Prev := Prev_Id;
4501 Make_Assignment_Statement
(Loc
,
4503 Make_Selected_Component
(Loc
,
4505 Unchecked_Convert_To
4506 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4508 Make_Identifier
(Loc
, Name_Prev
)),
4509 Expression
=> New_Occurrence_Of
(Prev_Id
, Loc
)));
4512 -- Root_Controlled (L).Next := Next_Id;
4515 Make_Assignment_Statement
(Loc
,
4517 Make_Selected_Component
(Loc
,
4519 Unchecked_Convert_To
4520 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4521 Selector_Name
=> Make_Identifier
(Loc
, Name_Next
)),
4522 Expression
=> New_Occurrence_Of
(Next_Id
, Loc
)));
4525 -- Adjust the target after the assignment when controlled (not in the
4526 -- init proc since it is an initialization more than an assignment).
4531 (Obj_Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
4539 -- Could use comment here ???
4541 when RE_Not_Available
=>
4543 end Make_Tag_Ctrl_Assignment
;