1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with 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 Namet
; use Namet
;
42 with Nlists
; use Nlists
;
43 with Nmake
; use Nmake
;
45 with Restrict
; use Restrict
;
46 with Rident
; use Rident
;
47 with Rtsfind
; use Rtsfind
;
48 with Sinfo
; use Sinfo
;
50 with Sem_Aux
; use Sem_Aux
;
51 with Sem_Ch3
; use Sem_Ch3
;
52 with Sem_Ch8
; use Sem_Ch8
;
53 with Sem_Ch13
; use Sem_Ch13
;
54 with Sem_Eval
; use Sem_Eval
;
55 with Sem_Res
; use Sem_Res
;
56 with Sem_Util
; use Sem_Util
;
57 with Snames
; use Snames
;
58 with Stand
; use Stand
;
59 with Stringt
; use Stringt
;
60 with Targparm
; use Targparm
;
61 with Tbuild
; use Tbuild
;
62 with Uintp
; use Uintp
;
63 with Validsw
; use Validsw
;
65 package body Exp_Ch5
is
67 procedure Build_Formal_Container_Iteration
69 Container
: Entity_Id
;
72 Advance
: out Node_Id
;
73 New_Loop
: out Node_Id
);
74 -- Utility to create declarations and loop statement for both forms
75 -- of formal container iterators.
77 function Change_Of_Representation
(N
: Node_Id
) return Boolean;
78 -- Determine if the right hand side of assignment N is a type conversion
79 -- which requires a change of representation. Called only for the array
82 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
);
83 -- N is an assignment which assigns an array value. This routine process
84 -- the various special cases and checks required for such assignments,
85 -- including change of representation. Rhs is normally simply the right
86 -- hand side of the assignment, except that if the right hand side is a
87 -- type conversion or a qualified expression, then the RHS is the actual
88 -- expression inside any such type conversions or qualifications.
90 function Expand_Assign_Array_Loop
97 Rev
: Boolean) return Node_Id
;
98 -- N is an assignment statement which assigns an array value. This routine
99 -- expands the assignment into a loop (or nested loops for the case of a
100 -- multi-dimensional array) to do the assignment component by component.
101 -- Larray and Rarray are the entities of the actual arrays on the left
102 -- hand and right hand sides. L_Type and R_Type are the types of these
103 -- arrays (which may not be the same, due to either sliding, or to a
104 -- change of representation case). Ndim is the number of dimensions and
105 -- the parameter Rev indicates if the loops run normally (Rev = False),
106 -- or reversed (Rev = True). The value returned is the constructed
107 -- loop statement. Auxiliary declarations are inserted before node N
108 -- using the standard Insert_Actions mechanism.
110 procedure Expand_Assign_Record
(N
: Node_Id
);
111 -- N is an assignment of an untagged record value. This routine handles
112 -- the case where the assignment must be made component by component,
113 -- either because the target is not byte aligned, or there is a change
114 -- of representation, or when we have a tagged type with a representation
115 -- clause (this last case is required because holes in the tagged type
116 -- might be filled with components from child types).
118 procedure Expand_Formal_Container_Loop
(N
: Node_Id
);
119 -- Use the primitives specified in an Iterable aspect to expand a loop
120 -- over a so-called formal container, primarily for SPARK usage.
122 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
);
123 -- Same, for an iterator of the form " For E of C". In this case the
124 -- iterator provides the name of the element, and the cursor is generated
127 procedure Expand_Iterator_Loop
(N
: Node_Id
);
128 -- Expand loop over arrays and containers that uses the form "for X of C"
129 -- with an optional subtype mark, or "for Y in C".
131 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
);
132 -- Expand loop over arrays that uses the form "for X of C"
134 procedure Expand_Predicated_Loop
(N
: Node_Id
);
135 -- Expand for loop over predicated subtype
137 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
;
138 -- Generate the necessary code for controlled and tagged assignment, that
139 -- is to say, finalization of the target before, adjustment of the target
140 -- after and save and restore of the tag and finalization pointers which
141 -- are not 'part of the value' and must not be changed upon assignment. N
142 -- is the original Assignment node.
144 --------------------------------------
145 -- Build_Formal_Container_iteration --
146 --------------------------------------
148 procedure Build_Formal_Container_Iteration
150 Container
: Entity_Id
;
153 Advance
: out Node_Id
;
154 New_Loop
: out Node_Id
)
156 Loc
: constant Source_Ptr
:= Sloc
(N
);
157 Stats
: constant List_Id
:= Statements
(N
);
158 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
159 First_Op
: constant Entity_Id
:=
160 Get_Iterable_Type_Primitive
(Typ
, Name_First
);
161 Next_Op
: constant Entity_Id
:=
162 Get_Iterable_Type_Primitive
(Typ
, Name_Next
);
164 Has_Element_Op
: constant Entity_Id
:=
165 Get_Iterable_Type_Primitive
(Typ
, Name_Has_Element
);
167 -- Declaration for Cursor
170 Make_Object_Declaration
(Loc
,
171 Defining_Identifier
=> Cursor
,
172 Object_Definition
=> New_Occurrence_Of
(Etype
(First_Op
), Loc
),
174 Make_Function_Call
(Loc
,
175 Name
=> New_Occurrence_Of
(First_Op
, Loc
),
176 Parameter_Associations
=> New_List
(
177 New_Occurrence_Of
(Container
, Loc
))));
179 -- Statement that advances cursor in loop
182 Make_Assignment_Statement
(Loc
,
183 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
185 Make_Function_Call
(Loc
,
186 Name
=> New_Occurrence_Of
(Next_Op
, Loc
),
187 Parameter_Associations
=> New_List
(
188 New_Occurrence_Of
(Container
, Loc
),
189 New_Occurrence_Of
(Cursor
, Loc
))));
191 -- Iterator is rewritten as a while_loop
194 Make_Loop_Statement
(Loc
,
196 Make_Iteration_Scheme
(Loc
,
198 Make_Function_Call
(Loc
,
199 Name
=> New_Occurrence_Of
(Has_Element_Op
, Loc
),
200 Parameter_Associations
=> New_List
(
201 New_Occurrence_Of
(Container
, Loc
),
202 New_Occurrence_Of
(Cursor
, Loc
)))),
205 end Build_Formal_Container_Iteration
;
207 ------------------------------
208 -- Change_Of_Representation --
209 ------------------------------
211 function Change_Of_Representation
(N
: Node_Id
) return Boolean is
212 Rhs
: constant Node_Id
:= Expression
(N
);
215 Nkind
(Rhs
) = N_Type_Conversion
217 not Same_Representation
(Etype
(Rhs
), Etype
(Expression
(Rhs
)));
218 end Change_Of_Representation
;
220 -------------------------
221 -- Expand_Assign_Array --
222 -------------------------
224 -- There are two issues here. First, do we let Gigi do a block move, or
225 -- do we expand out into a loop? Second, we need to set the two flags
226 -- Forwards_OK and Backwards_OK which show whether the block move (or
227 -- corresponding loops) can be legitimately done in a forwards (low to
228 -- high) or backwards (high to low) manner.
230 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
) is
231 Loc
: constant Source_Ptr
:= Sloc
(N
);
233 Lhs
: constant Node_Id
:= Name
(N
);
235 Act_Lhs
: constant Node_Id
:= Get_Referenced_Object
(Lhs
);
236 Act_Rhs
: Node_Id
:= Get_Referenced_Object
(Rhs
);
238 L_Type
: constant Entity_Id
:=
239 Underlying_Type
(Get_Actual_Subtype
(Act_Lhs
));
240 R_Type
: Entity_Id
:=
241 Underlying_Type
(Get_Actual_Subtype
(Act_Rhs
));
243 L_Slice
: constant Boolean := Nkind
(Act_Lhs
) = N_Slice
;
244 R_Slice
: constant Boolean := Nkind
(Act_Rhs
) = N_Slice
;
246 Crep
: constant Boolean := Change_Of_Representation
(N
);
251 Ndim
: constant Pos
:= Number_Dimensions
(L_Type
);
253 Loop_Required
: Boolean := False;
254 -- This switch is set to True if the array move must be done using
255 -- an explicit front end generated loop.
257 procedure Apply_Dereference
(Arg
: Node_Id
);
258 -- If the argument is an access to an array, and the assignment is
259 -- converted into a procedure call, apply explicit dereference.
261 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean;
262 -- Test if Exp is a reference to an array whose declaration has
263 -- an address clause, or it is a slice of such an array.
265 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean;
266 -- Test if Exp is a reference to an array which is either a formal
267 -- parameter or a slice of a formal parameter. These are the cases
268 -- where hidden aliasing can occur.
270 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean;
271 -- Determine if Exp is a reference to an array variable which is other
272 -- than an object defined in the current scope, or a slice of such
273 -- an object. Such objects can be aliased to parameters (unlike local
274 -- array references).
276 -----------------------
277 -- Apply_Dereference --
278 -----------------------
280 procedure Apply_Dereference
(Arg
: Node_Id
) is
281 Typ
: constant Entity_Id
:= Etype
(Arg
);
283 if Is_Access_Type
(Typ
) then
284 Rewrite
(Arg
, Make_Explicit_Dereference
(Loc
,
285 Prefix
=> Relocate_Node
(Arg
)));
286 Analyze_And_Resolve
(Arg
, Designated_Type
(Typ
));
288 end Apply_Dereference
;
290 ------------------------
291 -- Has_Address_Clause --
292 ------------------------
294 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean is
297 (Is_Entity_Name
(Exp
) and then
298 Present
(Address_Clause
(Entity
(Exp
))))
300 (Nkind
(Exp
) = N_Slice
and then Has_Address_Clause
(Prefix
(Exp
)));
301 end Has_Address_Clause
;
303 ---------------------
304 -- Is_Formal_Array --
305 ---------------------
307 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean is
310 (Is_Entity_Name
(Exp
) and then Is_Formal
(Entity
(Exp
)))
312 (Nkind
(Exp
) = N_Slice
and then Is_Formal_Array
(Prefix
(Exp
)));
315 ------------------------
316 -- Is_Non_Local_Array --
317 ------------------------
319 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean is
321 return (Is_Entity_Name
(Exp
)
322 and then Scope
(Entity
(Exp
)) /= Current_Scope
)
323 or else (Nkind
(Exp
) = N_Slice
324 and then Is_Non_Local_Array
(Prefix
(Exp
)));
325 end Is_Non_Local_Array
;
327 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
329 Lhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Lhs
);
330 Rhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Rhs
);
332 Lhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Lhs
);
333 Rhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Rhs
);
335 -- Start of processing for Expand_Assign_Array
338 -- Deal with length check. Note that the length check is done with
339 -- respect to the right hand side as given, not a possible underlying
340 -- renamed object, since this would generate incorrect extra checks.
342 Apply_Length_Check
(Rhs
, L_Type
);
344 -- We start by assuming that the move can be done in either direction,
345 -- i.e. that the two sides are completely disjoint.
347 Set_Forwards_OK
(N
, True);
348 Set_Backwards_OK
(N
, True);
350 -- Normally it is only the slice case that can lead to overlap, and
351 -- explicit checks for slices are made below. But there is one case
352 -- where the slice can be implicit and invisible to us: when we have a
353 -- one dimensional array, and either both operands are parameters, or
354 -- one is a parameter (which can be a slice passed by reference) and the
355 -- other is a non-local variable. In this case the parameter could be a
356 -- slice that overlaps with the other operand.
358 -- However, if the array subtype is a constrained first subtype in the
359 -- parameter case, then we don't have to worry about overlap, since
360 -- slice assignments aren't possible (other than for a slice denoting
363 -- Note: No overlap is possible if there is a change of representation,
364 -- so we can exclude this case.
369 ((Lhs_Formal
and Rhs_Formal
)
371 (Lhs_Formal
and Rhs_Non_Local_Var
)
373 (Rhs_Formal
and Lhs_Non_Local_Var
))
375 (not Is_Constrained
(Etype
(Lhs
))
376 or else not Is_First_Subtype
(Etype
(Lhs
)))
378 -- In the case of compiling for the Java or .NET Virtual Machine,
379 -- slices are always passed by making a copy, so we don't have to
380 -- worry about overlap. We also want to prevent generation of "<"
381 -- comparisons for array addresses, since that's a meaningless
382 -- operation on the VM.
384 and then VM_Target
= No_VM
386 Set_Forwards_OK
(N
, False);
387 Set_Backwards_OK
(N
, False);
389 -- Note: the bit-packed case is not worrisome here, since if we have
390 -- a slice passed as a parameter, it is always aligned on a byte
391 -- boundary, and if there are no explicit slices, the assignment
392 -- can be performed directly.
395 -- If either operand has an address clause clear Backwards_OK and
396 -- Forwards_OK, since we cannot tell if the operands overlap. We
397 -- exclude this treatment when Rhs is an aggregate, since we know
398 -- that overlap can't occur.
400 if (Has_Address_Clause
(Lhs
) and then Nkind
(Rhs
) /= N_Aggregate
)
401 or else Has_Address_Clause
(Rhs
)
403 Set_Forwards_OK
(N
, False);
404 Set_Backwards_OK
(N
, False);
407 -- We certainly must use a loop for change of representation and also
408 -- we use the operand of the conversion on the right hand side as the
409 -- effective right hand side (the component types must match in this
413 Act_Rhs
:= Get_Referenced_Object
(Rhs
);
414 R_Type
:= Get_Actual_Subtype
(Act_Rhs
);
415 Loop_Required
:= True;
417 -- We require a loop if the left side is possibly bit unaligned
419 elsif Possible_Bit_Aligned_Component
(Lhs
)
421 Possible_Bit_Aligned_Component
(Rhs
)
423 Loop_Required
:= True;
425 -- Arrays with controlled components are expanded into a loop to force
426 -- calls to Adjust at the component level.
428 elsif Has_Controlled_Component
(L_Type
) then
429 Loop_Required
:= True;
431 -- If object is atomic, we cannot tolerate a loop
433 elsif Is_Atomic_Object
(Act_Lhs
)
435 Is_Atomic_Object
(Act_Rhs
)
439 -- Loop is required if we have atomic components since we have to
440 -- be sure to do any accesses on an element by element basis.
442 elsif Has_Atomic_Components
(L_Type
)
443 or else Has_Atomic_Components
(R_Type
)
444 or else Is_Atomic
(Component_Type
(L_Type
))
445 or else Is_Atomic
(Component_Type
(R_Type
))
447 Loop_Required
:= True;
449 -- Case where no slice is involved
451 elsif not L_Slice
and not R_Slice
then
453 -- The following code deals with the case of unconstrained bit packed
454 -- arrays. The problem is that the template for such arrays contains
455 -- the bounds of the actual source level array, but the copy of an
456 -- entire array requires the bounds of the underlying array. It would
457 -- be nice if the back end could take care of this, but right now it
458 -- does not know how, so if we have such a type, then we expand out
459 -- into a loop, which is inefficient but works correctly. If we don't
460 -- do this, we get the wrong length computed for the array to be
461 -- moved. The two cases we need to worry about are:
463 -- Explicit dereference of an unconstrained packed array type as in
464 -- the following example:
467 -- type BITS is array(INTEGER range <>) of BOOLEAN;
468 -- pragma PACK(BITS);
469 -- type A is access BITS;
472 -- P1 := new BITS (1 .. 65_535);
473 -- P2 := new BITS (1 .. 65_535);
477 -- A formal parameter reference with an unconstrained bit array type
478 -- is the other case we need to worry about (here we assume the same
479 -- BITS type declared above):
481 -- procedure Write_All (File : out BITS; Contents : BITS);
483 -- File.Storage := Contents;
486 -- We expand to a loop in either of these two cases
488 -- Question for future thought. Another potentially more efficient
489 -- approach would be to create the actual subtype, and then do an
490 -- unchecked conversion to this actual subtype ???
492 Check_Unconstrained_Bit_Packed_Array
: declare
494 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean;
495 -- Function to perform required test for the first case, above
496 -- (dereference of an unconstrained bit packed array).
498 -----------------------
499 -- Is_UBPA_Reference --
500 -----------------------
502 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean is
503 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Opnd
));
505 Des_Type
: Entity_Id
;
508 if Present
(Packed_Array_Impl_Type
(Typ
))
509 and then Is_Array_Type
(Packed_Array_Impl_Type
(Typ
))
510 and then not Is_Constrained
(Packed_Array_Impl_Type
(Typ
))
514 elsif Nkind
(Opnd
) = N_Explicit_Dereference
then
515 P_Type
:= Underlying_Type
(Etype
(Prefix
(Opnd
)));
517 if not Is_Access_Type
(P_Type
) then
521 Des_Type
:= Designated_Type
(P_Type
);
523 Is_Bit_Packed_Array
(Des_Type
)
524 and then not Is_Constrained
(Des_Type
);
530 end Is_UBPA_Reference
;
532 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
535 if Is_UBPA_Reference
(Lhs
)
537 Is_UBPA_Reference
(Rhs
)
539 Loop_Required
:= True;
541 -- Here if we do not have the case of a reference to a bit packed
542 -- unconstrained array case. In this case gigi can most certainly
543 -- handle the assignment if a forwards move is allowed.
545 -- (could it handle the backwards case also???)
547 elsif Forwards_OK
(N
) then
550 end Check_Unconstrained_Bit_Packed_Array
;
552 -- The back end can always handle the assignment if the right side is a
553 -- string literal (note that overlap is definitely impossible in this
554 -- case). If the type is packed, a string literal is always converted
555 -- into an aggregate, except in the case of a null slice, for which no
556 -- aggregate can be written. In that case, rewrite the assignment as a
557 -- null statement, a length check has already been emitted to verify
558 -- that the range of the left-hand side is empty.
560 -- Note that this code is not executed if we have an assignment of a
561 -- string literal to a non-bit aligned component of a record, a case
562 -- which cannot be handled by the backend.
564 elsif Nkind
(Rhs
) = N_String_Literal
then
565 if String_Length
(Strval
(Rhs
)) = 0
566 and then Is_Bit_Packed_Array
(L_Type
)
568 Rewrite
(N
, Make_Null_Statement
(Loc
));
574 -- If either operand is bit packed, then we need a loop, since we can't
575 -- be sure that the slice is byte aligned. Similarly, if either operand
576 -- is a possibly unaligned slice, then we need a loop (since the back
577 -- end cannot handle unaligned slices).
579 elsif Is_Bit_Packed_Array
(L_Type
)
580 or else Is_Bit_Packed_Array
(R_Type
)
581 or else Is_Possibly_Unaligned_Slice
(Lhs
)
582 or else Is_Possibly_Unaligned_Slice
(Rhs
)
584 Loop_Required
:= True;
586 -- If we are not bit-packed, and we have only one slice, then no overlap
587 -- is possible except in the parameter case, so we can let the back end
590 elsif not (L_Slice
and R_Slice
) then
591 if Forwards_OK
(N
) then
596 -- If the right-hand side is a string literal, introduce a temporary for
597 -- it, for use in the generated loop that will follow.
599 if Nkind
(Rhs
) = N_String_Literal
then
601 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Rhs
);
606 Make_Object_Declaration
(Loc
,
607 Defining_Identifier
=> Temp
,
608 Object_Definition
=> New_Occurrence_Of
(L_Type
, Loc
),
609 Expression
=> Relocate_Node
(Rhs
));
611 Insert_Action
(N
, Decl
);
612 Rewrite
(Rhs
, New_Occurrence_Of
(Temp
, Loc
));
613 R_Type
:= Etype
(Temp
);
617 -- Come here to complete the analysis
619 -- Loop_Required: Set to True if we know that a loop is required
620 -- regardless of overlap considerations.
622 -- Forwards_OK: Set to False if we already know that a forwards
623 -- move is not safe, else set to True.
625 -- Backwards_OK: Set to False if we already know that a backwards
626 -- move is not safe, else set to True
628 -- Our task at this stage is to complete the overlap analysis, which can
629 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
630 -- then generating the final code, either by deciding that it is OK
631 -- after all to let Gigi handle it, or by generating appropriate code
635 L_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(L_Type
));
636 R_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(R_Type
));
638 Left_Lo
: constant Node_Id
:= Type_Low_Bound
(L_Index_Typ
);
639 Left_Hi
: constant Node_Id
:= Type_High_Bound
(L_Index_Typ
);
640 Right_Lo
: constant Node_Id
:= Type_Low_Bound
(R_Index_Typ
);
641 Right_Hi
: constant Node_Id
:= Type_High_Bound
(R_Index_Typ
);
643 Act_L_Array
: Node_Id
;
644 Act_R_Array
: Node_Id
;
650 Cresult
: Compare_Result
;
653 -- Get the expressions for the arrays. If we are dealing with a
654 -- private type, then convert to the underlying type. We can do
655 -- direct assignments to an array that is a private type, but we
656 -- cannot assign to elements of the array without this extra
657 -- unchecked conversion.
659 -- Note: We propagate Parent to the conversion nodes to generate
660 -- a well-formed subtree.
662 if Nkind
(Act_Lhs
) = N_Slice
then
663 Larray
:= Prefix
(Act_Lhs
);
667 if Is_Private_Type
(Etype
(Larray
)) then
669 Par
: constant Node_Id
:= Parent
(Larray
);
673 (Underlying_Type
(Etype
(Larray
)), Larray
);
674 Set_Parent
(Larray
, Par
);
679 if Nkind
(Act_Rhs
) = N_Slice
then
680 Rarray
:= Prefix
(Act_Rhs
);
684 if Is_Private_Type
(Etype
(Rarray
)) then
686 Par
: constant Node_Id
:= Parent
(Rarray
);
690 (Underlying_Type
(Etype
(Rarray
)), Rarray
);
691 Set_Parent
(Rarray
, Par
);
696 -- If both sides are slices, we must figure out whether it is safe
697 -- to do the move in one direction or the other. It is always safe
698 -- if there is a change of representation since obviously two arrays
699 -- with different representations cannot possibly overlap.
701 if (not Crep
) and L_Slice
and R_Slice
then
702 Act_L_Array
:= Get_Referenced_Object
(Prefix
(Act_Lhs
));
703 Act_R_Array
:= Get_Referenced_Object
(Prefix
(Act_Rhs
));
705 -- If both left and right hand arrays are entity names, and refer
706 -- to different entities, then we know that the move is safe (the
707 -- two storage areas are completely disjoint).
709 if Is_Entity_Name
(Act_L_Array
)
710 and then Is_Entity_Name
(Act_R_Array
)
711 and then Entity
(Act_L_Array
) /= Entity
(Act_R_Array
)
715 -- Otherwise, we assume the worst, which is that the two arrays
716 -- are the same array. There is no need to check if we know that
717 -- is the case, because if we don't know it, we still have to
720 -- Generally if the same array is involved, then we have an
721 -- overlapping case. We will have to really assume the worst (i.e.
722 -- set neither of the OK flags) unless we can determine the lower
723 -- or upper bounds at compile time and compare them.
728 (Left_Lo
, Right_Lo
, Assume_Valid
=> True);
730 if Cresult
= Unknown
then
733 (Left_Hi
, Right_Hi
, Assume_Valid
=> True);
737 when LT | LE | EQ
=> Set_Backwards_OK
(N
, False);
738 when GT | GE
=> Set_Forwards_OK
(N
, False);
739 when NE | Unknown
=> Set_Backwards_OK
(N
, False);
740 Set_Forwards_OK
(N
, False);
745 -- If after that analysis Loop_Required is False, meaning that we
746 -- have not discovered some non-overlap reason for requiring a loop,
747 -- then the outcome depends on the capabilities of the back end.
749 if not Loop_Required
then
751 -- The GCC back end can deal with all cases of overlap by falling
752 -- back to memmove if it cannot use a more efficient approach.
754 if VM_Target
= No_VM
and not AAMP_On_Target
then
757 -- Assume other back ends can handle it if Forwards_OK is set
759 elsif Forwards_OK
(N
) then
762 -- If Forwards_OK is not set, the back end will need something
763 -- like memmove to handle the move. For now, this processing is
764 -- activated using the .s debug flag (-gnatd.s).
766 elsif Debug_Flag_Dot_S
then
771 -- At this stage we have to generate an explicit loop, and we have
772 -- the following cases:
774 -- Forwards_OK = True
776 -- Rnn : right_index := right_index'First;
777 -- for Lnn in left-index loop
778 -- left (Lnn) := right (Rnn);
779 -- Rnn := right_index'Succ (Rnn);
782 -- Note: the above code MUST be analyzed with checks off, because
783 -- otherwise the Succ could overflow. But in any case this is more
786 -- Forwards_OK = False, Backwards_OK = True
788 -- Rnn : right_index := right_index'Last;
789 -- for Lnn in reverse left-index loop
790 -- left (Lnn) := right (Rnn);
791 -- Rnn := right_index'Pred (Rnn);
794 -- Note: the above code MUST be analyzed with checks off, because
795 -- otherwise the Pred could overflow. But in any case this is more
798 -- Forwards_OK = Backwards_OK = False
800 -- This only happens if we have the same array on each side. It is
801 -- possible to create situations using overlays that violate this,
802 -- but we simply do not promise to get this "right" in this case.
804 -- There are two possible subcases. If the No_Implicit_Conditionals
805 -- restriction is set, then we generate the following code:
808 -- T : constant <operand-type> := rhs;
813 -- If implicit conditionals are permitted, then we generate:
815 -- if Left_Lo <= Right_Lo then
816 -- <code for Forwards_OK = True above>
818 -- <code for Backwards_OK = True above>
821 -- In order to detect possible aliasing, we examine the renamed
822 -- expression when the source or target is a renaming. However,
823 -- the renaming may be intended to capture an address that may be
824 -- affected by subsequent code, and therefore we must recover
825 -- the actual entity for the expansion that follows, not the
826 -- object it renames. In particular, if source or target designate
827 -- a portion of a dynamically allocated object, the pointer to it
828 -- may be reassigned but the renaming preserves the proper location.
830 if Is_Entity_Name
(Rhs
)
832 Nkind
(Parent
(Entity
(Rhs
))) = N_Object_Renaming_Declaration
833 and then Nkind
(Act_Rhs
) = N_Slice
838 if Is_Entity_Name
(Lhs
)
840 Nkind
(Parent
(Entity
(Lhs
))) = N_Object_Renaming_Declaration
841 and then Nkind
(Act_Lhs
) = N_Slice
846 -- Cases where either Forwards_OK or Backwards_OK is true
848 if Forwards_OK
(N
) or else Backwards_OK
(N
) then
849 if Needs_Finalization
(Component_Type
(L_Type
))
850 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
852 and then not No_Ctrl_Actions
(N
)
855 Proc
: constant Entity_Id
:=
856 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
860 Apply_Dereference
(Larray
);
861 Apply_Dereference
(Rarray
);
862 Actuals
:= New_List
(
863 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
864 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
865 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
866 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
867 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
868 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
872 Boolean_Literals
(not Forwards_OK
(N
)), Loc
));
875 Make_Procedure_Call_Statement
(Loc
,
876 Name
=> New_Occurrence_Of
(Proc
, Loc
),
877 Parameter_Associations
=> Actuals
));
882 Expand_Assign_Array_Loop
883 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
884 Rev
=> not Forwards_OK
(N
)));
887 -- Case of both are false with No_Implicit_Conditionals
889 elsif Restriction_Active
(No_Implicit_Conditionals
) then
891 T
: constant Entity_Id
:=
892 Make_Defining_Identifier
(Loc
, Chars
=> Name_T
);
896 Make_Block_Statement
(Loc
,
897 Declarations
=> New_List
(
898 Make_Object_Declaration
(Loc
,
899 Defining_Identifier
=> T
,
900 Constant_Present
=> True,
902 New_Occurrence_Of
(Etype
(Rhs
), Loc
),
903 Expression
=> Relocate_Node
(Rhs
))),
905 Handled_Statement_Sequence
=>
906 Make_Handled_Sequence_Of_Statements
(Loc
,
907 Statements
=> New_List
(
908 Make_Assignment_Statement
(Loc
,
909 Name
=> Relocate_Node
(Lhs
),
910 Expression
=> New_Occurrence_Of
(T
, Loc
))))));
913 -- Case of both are false with implicit conditionals allowed
916 -- Before we generate this code, we must ensure that the left and
917 -- right side array types are defined. They may be itypes, and we
918 -- cannot let them be defined inside the if, since the first use
919 -- in the then may not be executed.
921 Ensure_Defined
(L_Type
, N
);
922 Ensure_Defined
(R_Type
, N
);
924 -- We normally compare addresses to find out which way round to
925 -- do the loop, since this is reliable, and handles the cases of
926 -- parameters, conversions etc. But we can't do that in the bit
927 -- packed case or the VM case, because addresses don't work there.
929 if not Is_Bit_Packed_Array
(L_Type
) and then VM_Target
= No_VM
then
933 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
934 Make_Attribute_Reference
(Loc
,
936 Make_Indexed_Component
(Loc
,
938 Duplicate_Subexpr_Move_Checks
(Larray
, True),
939 Expressions
=> New_List
(
940 Make_Attribute_Reference
(Loc
,
944 Attribute_Name
=> Name_First
))),
945 Attribute_Name
=> Name_Address
)),
948 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
949 Make_Attribute_Reference
(Loc
,
951 Make_Indexed_Component
(Loc
,
953 Duplicate_Subexpr_Move_Checks
(Rarray
, True),
954 Expressions
=> New_List
(
955 Make_Attribute_Reference
(Loc
,
959 Attribute_Name
=> Name_First
))),
960 Attribute_Name
=> Name_Address
)));
962 -- For the bit packed and VM cases we use the bounds. That's OK,
963 -- because we don't have to worry about parameters, since they
964 -- cannot cause overlap. Perhaps we should worry about weird slice
970 Cleft_Lo
:= New_Copy_Tree
(Left_Lo
);
971 Cright_Lo
:= New_Copy_Tree
(Right_Lo
);
973 -- If the types do not match we add an implicit conversion
974 -- here to ensure proper match
976 if Etype
(Left_Lo
) /= Etype
(Right_Lo
) then
978 Unchecked_Convert_To
(Etype
(Left_Lo
), Cright_Lo
);
981 -- Reset the Analyzed flag, because the bounds of the index
982 -- type itself may be universal, and must must be reanalyzed
983 -- to acquire the proper type for the back end.
985 Set_Analyzed
(Cleft_Lo
, False);
986 Set_Analyzed
(Cright_Lo
, False);
990 Left_Opnd
=> Cleft_Lo
,
991 Right_Opnd
=> Cright_Lo
);
994 if Needs_Finalization
(Component_Type
(L_Type
))
995 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
997 and then not No_Ctrl_Actions
(N
)
1000 -- Call TSS procedure for array assignment, passing the
1001 -- explicit bounds of right and left hand sides.
1004 Proc
: constant Entity_Id
:=
1005 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
1009 Apply_Dereference
(Larray
);
1010 Apply_Dereference
(Rarray
);
1011 Actuals
:= New_List
(
1012 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1013 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1014 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
1015 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
1016 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
1017 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
1021 Right_Opnd
=> Condition
));
1024 Make_Procedure_Call_Statement
(Loc
,
1025 Name
=> New_Occurrence_Of
(Proc
, Loc
),
1026 Parameter_Associations
=> Actuals
));
1031 Make_Implicit_If_Statement
(N
,
1032 Condition
=> Condition
,
1034 Then_Statements
=> New_List
(
1035 Expand_Assign_Array_Loop
1036 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1039 Else_Statements
=> New_List
(
1040 Expand_Assign_Array_Loop
1041 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1046 Analyze
(N
, Suppress
=> All_Checks
);
1050 when RE_Not_Available
=>
1052 end Expand_Assign_Array
;
1054 ------------------------------
1055 -- Expand_Assign_Array_Loop --
1056 ------------------------------
1058 -- The following is an example of the loop generated for the case of a
1059 -- two-dimensional array:
1062 -- R2b : Tm1X1 := 1;
1064 -- for L1b in 1 .. 100 loop
1066 -- R4b : Tm1X2 := 1;
1068 -- for L3b in 1 .. 100 loop
1069 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
1070 -- R4b := Tm1X2'succ(R4b);
1073 -- R2b := Tm1X1'succ(R2b);
1077 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
1078 -- side. The declarations of R2b and R4b are inserted before the original
1079 -- assignment statement.
1081 function Expand_Assign_Array_Loop
1088 Rev
: Boolean) return Node_Id
1090 Loc
: constant Source_Ptr
:= Sloc
(N
);
1092 Lnn
: array (1 .. Ndim
) of Entity_Id
;
1093 Rnn
: array (1 .. Ndim
) of Entity_Id
;
1094 -- Entities used as subscripts on left and right sides
1096 L_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1097 R_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1098 -- Left and right index types
1105 function Build_Step
(J
: Nat
) return Node_Id
;
1106 -- The increment step for the index of the right-hand side is written
1107 -- as an attribute reference (Succ or Pred). This function returns
1108 -- the corresponding node, which is placed at the end of the loop body.
1114 function Build_Step
(J
: Nat
) return Node_Id
is
1126 Make_Assignment_Statement
(Loc
,
1127 Name
=> New_Occurrence_Of
(Rnn
(J
), Loc
),
1129 Make_Attribute_Reference
(Loc
,
1131 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1132 Attribute_Name
=> S_Or_P
,
1133 Expressions
=> New_List
(
1134 New_Occurrence_Of
(Rnn
(J
), Loc
))));
1136 -- Note that on the last iteration of the loop, the index is increased
1137 -- (or decreased) past the corresponding bound. This is consistent with
1138 -- the C semantics of the back-end, where such an off-by-one value on a
1139 -- dead index variable is OK. However, in CodePeer mode this leads to
1140 -- spurious warnings, and thus we place a guard around the attribute
1141 -- reference. For obvious reasons we only do this for CodePeer.
1143 if CodePeer_Mode
then
1145 Make_If_Statement
(Loc
,
1148 Left_Opnd
=> New_Occurrence_Of
(Lnn
(J
), Loc
),
1150 Make_Attribute_Reference
(Loc
,
1151 Prefix
=> New_Occurrence_Of
(L_Index_Type
(J
), Loc
),
1152 Attribute_Name
=> Lim
)),
1153 Then_Statements
=> New_List
(Step
));
1159 -- Start of processing for Expand_Assign_Array_Loop
1163 F_Or_L
:= Name_Last
;
1164 S_Or_P
:= Name_Pred
;
1166 F_Or_L
:= Name_First
;
1167 S_Or_P
:= Name_Succ
;
1170 -- Setup index types and subscript entities
1177 L_Index
:= First_Index
(L_Type
);
1178 R_Index
:= First_Index
(R_Type
);
1180 for J
in 1 .. Ndim
loop
1181 Lnn
(J
) := Make_Temporary
(Loc
, 'L');
1182 Rnn
(J
) := Make_Temporary
(Loc
, 'R');
1184 L_Index_Type
(J
) := Etype
(L_Index
);
1185 R_Index_Type
(J
) := Etype
(R_Index
);
1187 Next_Index
(L_Index
);
1188 Next_Index
(R_Index
);
1192 -- Now construct the assignment statement
1195 ExprL
: constant List_Id
:= New_List
;
1196 ExprR
: constant List_Id
:= New_List
;
1199 for J
in 1 .. Ndim
loop
1200 Append_To
(ExprL
, New_Occurrence_Of
(Lnn
(J
), Loc
));
1201 Append_To
(ExprR
, New_Occurrence_Of
(Rnn
(J
), Loc
));
1205 Make_Assignment_Statement
(Loc
,
1207 Make_Indexed_Component
(Loc
,
1208 Prefix
=> Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1209 Expressions
=> ExprL
),
1211 Make_Indexed_Component
(Loc
,
1212 Prefix
=> Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1213 Expressions
=> ExprR
));
1215 -- We set assignment OK, since there are some cases, e.g. in object
1216 -- declarations, where we are actually assigning into a constant.
1217 -- If there really is an illegality, it was caught long before now,
1218 -- and was flagged when the original assignment was analyzed.
1220 Set_Assignment_OK
(Name
(Assign
));
1222 -- Propagate the No_Ctrl_Actions flag to individual assignments
1224 Set_No_Ctrl_Actions
(Assign
, No_Ctrl_Actions
(N
));
1227 -- Now construct the loop from the inside out, with the last subscript
1228 -- varying most rapidly. Note that Assign is first the raw assignment
1229 -- statement, and then subsequently the loop that wraps it up.
1231 for J
in reverse 1 .. Ndim
loop
1233 Make_Block_Statement
(Loc
,
1234 Declarations
=> New_List
(
1235 Make_Object_Declaration
(Loc
,
1236 Defining_Identifier
=> Rnn
(J
),
1237 Object_Definition
=>
1238 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1240 Make_Attribute_Reference
(Loc
,
1241 Prefix
=> New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1242 Attribute_Name
=> F_Or_L
))),
1244 Handled_Statement_Sequence
=>
1245 Make_Handled_Sequence_Of_Statements
(Loc
,
1246 Statements
=> New_List
(
1247 Make_Implicit_Loop_Statement
(N
,
1249 Make_Iteration_Scheme
(Loc
,
1250 Loop_Parameter_Specification
=>
1251 Make_Loop_Parameter_Specification
(Loc
,
1252 Defining_Identifier
=> Lnn
(J
),
1253 Reverse_Present
=> Rev
,
1254 Discrete_Subtype_Definition
=>
1255 New_Occurrence_Of
(L_Index_Type
(J
), Loc
))),
1257 Statements
=> New_List
(Assign
, Build_Step
(J
))))));
1261 end Expand_Assign_Array_Loop
;
1263 --------------------------
1264 -- Expand_Assign_Record --
1265 --------------------------
1267 procedure Expand_Assign_Record
(N
: Node_Id
) is
1268 Lhs
: constant Node_Id
:= Name
(N
);
1269 Rhs
: Node_Id
:= Expression
(N
);
1270 L_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Lhs
));
1273 -- If change of representation, then extract the real right hand side
1274 -- from the type conversion, and proceed with component-wise assignment,
1275 -- since the two types are not the same as far as the back end is
1278 if Change_Of_Representation
(N
) then
1279 Rhs
:= Expression
(Rhs
);
1281 -- If this may be a case of a large bit aligned component, then proceed
1282 -- with component-wise assignment, to avoid possible clobbering of other
1283 -- components sharing bits in the first or last byte of the component to
1286 elsif Possible_Bit_Aligned_Component
(Lhs
)
1288 Possible_Bit_Aligned_Component
(Rhs
)
1292 -- If we have a tagged type that has a complete record representation
1293 -- clause, we must do we must do component-wise assignments, since child
1294 -- types may have used gaps for their components, and we might be
1295 -- dealing with a view conversion.
1297 elsif Is_Fully_Repped_Tagged_Type
(L_Typ
) then
1300 -- If neither condition met, then nothing special to do, the back end
1301 -- can handle assignment of the entire component as a single entity.
1307 -- At this stage we know that we must do a component wise assignment
1310 Loc
: constant Source_Ptr
:= Sloc
(N
);
1311 R_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Rhs
));
1312 Decl
: constant Node_Id
:= Declaration_Node
(R_Typ
);
1316 function Find_Component
1318 Comp
: Entity_Id
) return Entity_Id
;
1319 -- Find the component with the given name in the underlying record
1320 -- declaration for Typ. We need to use the actual entity because the
1321 -- type may be private and resolution by identifier alone would fail.
1323 function Make_Component_List_Assign
1325 U_U
: Boolean := False) return List_Id
;
1326 -- Returns a sequence of statements to assign the components that
1327 -- are referenced in the given component list. The flag U_U is
1328 -- used to force the usage of the inferred value of the variant
1329 -- part expression as the switch for the generated case statement.
1331 function Make_Field_Assign
1333 U_U
: Boolean := False) return Node_Id
;
1334 -- Given C, the entity for a discriminant or component, build an
1335 -- assignment for the corresponding field values. The flag U_U
1336 -- signals the presence of an Unchecked_Union and forces the usage
1337 -- of the inferred discriminant value of C as the right hand side
1338 -- of the assignment.
1340 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
;
1341 -- Given CI, a component items list, construct series of statements
1342 -- for fieldwise assignment of the corresponding components.
1344 --------------------
1345 -- Find_Component --
1346 --------------------
1348 function Find_Component
1350 Comp
: Entity_Id
) return Entity_Id
1352 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
1356 C
:= First_Entity
(Utyp
);
1357 while Present
(C
) loop
1358 if Chars
(C
) = Chars
(Comp
) then
1365 raise Program_Error
;
1368 --------------------------------
1369 -- Make_Component_List_Assign --
1370 --------------------------------
1372 function Make_Component_List_Assign
1374 U_U
: Boolean := False) return List_Id
1376 CI
: constant List_Id
:= Component_Items
(CL
);
1377 VP
: constant Node_Id
:= Variant_Part
(CL
);
1387 Result
:= Make_Field_Assigns
(CI
);
1389 if Present
(VP
) then
1390 V
:= First_Non_Pragma
(Variants
(VP
));
1392 while Present
(V
) loop
1394 DC
:= First
(Discrete_Choices
(V
));
1395 while Present
(DC
) loop
1396 Append_To
(DCH
, New_Copy_Tree
(DC
));
1401 Make_Case_Statement_Alternative
(Loc
,
1402 Discrete_Choices
=> DCH
,
1404 Make_Component_List_Assign
(Component_List
(V
))));
1405 Next_Non_Pragma
(V
);
1408 -- If we have an Unchecked_Union, use the value of the inferred
1409 -- discriminant of the variant part expression as the switch
1410 -- for the case statement. The case statement may later be
1415 New_Copy
(Get_Discriminant_Value
(
1418 Discriminant_Constraint
(Etype
(Rhs
))));
1421 Make_Selected_Component
(Loc
,
1422 Prefix
=> Duplicate_Subexpr
(Rhs
),
1424 Make_Identifier
(Loc
, Chars
(Name
(VP
))));
1428 Make_Case_Statement
(Loc
,
1430 Alternatives
=> Alts
));
1434 end Make_Component_List_Assign
;
1436 -----------------------
1437 -- Make_Field_Assign --
1438 -----------------------
1440 function Make_Field_Assign
1442 U_U
: Boolean := False) return Node_Id
1448 -- In the case of an Unchecked_Union, use the discriminant
1449 -- constraint value as on the right hand side of the assignment.
1453 New_Copy
(Get_Discriminant_Value
(C
,
1455 Discriminant_Constraint
(Etype
(Rhs
))));
1458 Make_Selected_Component
(Loc
,
1459 Prefix
=> Duplicate_Subexpr
(Rhs
),
1460 Selector_Name
=> New_Occurrence_Of
(C
, Loc
));
1464 Make_Assignment_Statement
(Loc
,
1466 Make_Selected_Component
(Loc
,
1467 Prefix
=> Duplicate_Subexpr
(Lhs
),
1469 New_Occurrence_Of
(Find_Component
(L_Typ
, C
), Loc
)),
1470 Expression
=> Expr
);
1472 -- Set Assignment_OK, so discriminants can be assigned
1474 Set_Assignment_OK
(Name
(A
), True);
1476 if Componentwise_Assignment
(N
)
1477 and then Nkind
(Name
(A
)) = N_Selected_Component
1478 and then Chars
(Selector_Name
(Name
(A
))) = Name_uParent
1480 Set_Componentwise_Assignment
(A
);
1484 end Make_Field_Assign
;
1486 ------------------------
1487 -- Make_Field_Assigns --
1488 ------------------------
1490 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
is
1498 while Present
(Item
) loop
1500 -- Look for components, but exclude _tag field assignment if
1501 -- the special Componentwise_Assignment flag is set.
1503 if Nkind
(Item
) = N_Component_Declaration
1504 and then not (Is_Tag
(Defining_Identifier
(Item
))
1505 and then Componentwise_Assignment
(N
))
1508 (Result
, Make_Field_Assign
(Defining_Identifier
(Item
)));
1515 end Make_Field_Assigns
;
1517 -- Start of processing for Expand_Assign_Record
1520 -- Note that we use the base types for this processing. This results
1521 -- in some extra work in the constrained case, but the change of
1522 -- representation case is so unusual that it is not worth the effort.
1524 -- First copy the discriminants. This is done unconditionally. It
1525 -- is required in the unconstrained left side case, and also in the
1526 -- case where this assignment was constructed during the expansion
1527 -- of a type conversion (since initialization of discriminants is
1528 -- suppressed in this case). It is unnecessary but harmless in
1531 if Has_Discriminants
(L_Typ
) then
1532 F
:= First_Discriminant
(R_Typ
);
1533 while Present
(F
) loop
1535 -- If we are expanding the initialization of a derived record
1536 -- that constrains or renames discriminants of the parent, we
1537 -- must use the corresponding discriminant in the parent.
1544 and then Present
(Corresponding_Discriminant
(F
))
1546 CF
:= Corresponding_Discriminant
(F
);
1551 if Is_Unchecked_Union
(Base_Type
(R_Typ
)) then
1553 -- Within an initialization procedure this is the
1554 -- assignment to an unchecked union component, in which
1555 -- case there is no discriminant to initialize.
1557 if Inside_Init_Proc
then
1561 -- The assignment is part of a conversion from a
1562 -- derived unchecked union type with an inferable
1563 -- discriminant, to a parent type.
1565 Insert_Action
(N
, Make_Field_Assign
(CF
, True));
1569 Insert_Action
(N
, Make_Field_Assign
(CF
));
1572 Next_Discriminant
(F
);
1577 -- We know the underlying type is a record, but its current view
1578 -- may be private. We must retrieve the usable record declaration.
1580 if Nkind_In
(Decl
, N_Private_Type_Declaration
,
1581 N_Private_Extension_Declaration
)
1582 and then Present
(Full_View
(R_Typ
))
1584 RDef
:= Type_Definition
(Declaration_Node
(Full_View
(R_Typ
)));
1586 RDef
:= Type_Definition
(Decl
);
1589 if Nkind
(RDef
) = N_Derived_Type_Definition
then
1590 RDef
:= Record_Extension_Part
(RDef
);
1593 if Nkind
(RDef
) = N_Record_Definition
1594 and then Present
(Component_List
(RDef
))
1596 if Is_Unchecked_Union
(R_Typ
) then
1598 Make_Component_List_Assign
(Component_List
(RDef
), True));
1601 (N
, Make_Component_List_Assign
(Component_List
(RDef
)));
1604 Rewrite
(N
, Make_Null_Statement
(Loc
));
1607 end Expand_Assign_Record
;
1609 -----------------------------------
1610 -- Expand_N_Assignment_Statement --
1611 -----------------------------------
1613 -- This procedure implements various cases where an assignment statement
1614 -- cannot just be passed on to the back end in untransformed state.
1616 procedure Expand_N_Assignment_Statement
(N
: Node_Id
) is
1617 Loc
: constant Source_Ptr
:= Sloc
(N
);
1618 Crep
: constant Boolean := Change_Of_Representation
(N
);
1619 Lhs
: constant Node_Id
:= Name
(N
);
1620 Rhs
: constant Node_Id
:= Expression
(N
);
1621 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Lhs
));
1625 -- Special case to check right away, if the Componentwise_Assignment
1626 -- flag is set, this is a reanalysis from the expansion of the primitive
1627 -- assignment procedure for a tagged type, and all we need to do is to
1628 -- expand to assignment of components, because otherwise, we would get
1629 -- infinite recursion (since this looks like a tagged assignment which
1630 -- would normally try to *call* the primitive assignment procedure).
1632 if Componentwise_Assignment
(N
) then
1633 Expand_Assign_Record
(N
);
1637 -- Defend against invalid subscripts on left side if we are in standard
1638 -- validity checking mode. No need to do this if we are checking all
1641 -- Note that we do this right away, because there are some early return
1642 -- paths in this procedure, and this is required on all paths.
1644 if Validity_Checks_On
1645 and then Validity_Check_Default
1646 and then not Validity_Check_Subscripts
1648 Check_Valid_Lvalue_Subscripts
(Lhs
);
1651 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1653 -- Rewrite an assignment to X'Priority into a run-time call
1655 -- For example: X'Priority := New_Prio_Expr;
1656 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1658 -- Note that although X'Priority is notionally an object, it is quite
1659 -- deliberately not defined as an aliased object in the RM. This means
1660 -- that it works fine to rewrite it as a call, without having to worry
1661 -- about complications that would other arise from X'Priority'Access,
1662 -- which is illegal, because of the lack of aliasing.
1664 if Ada_Version
>= Ada_2005
then
1667 Conctyp
: Entity_Id
;
1670 RT_Subprg_Name
: Node_Id
;
1673 -- Handle chains of renamings
1676 while Nkind
(Ent
) in N_Has_Entity
1677 and then Present
(Entity
(Ent
))
1678 and then Present
(Renamed_Object
(Entity
(Ent
)))
1680 Ent
:= Renamed_Object
(Entity
(Ent
));
1683 -- The attribute Priority applied to protected objects has been
1684 -- previously expanded into a call to the Get_Ceiling run-time
1687 if Nkind
(Ent
) = N_Function_Call
1688 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
1690 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
))
1692 -- Look for the enclosing concurrent type
1694 Conctyp
:= Current_Scope
;
1695 while not Is_Concurrent_Type
(Conctyp
) loop
1696 Conctyp
:= Scope
(Conctyp
);
1699 pragma Assert
(Is_Protected_Type
(Conctyp
));
1701 -- Generate the first actual of the call
1703 Subprg
:= Current_Scope
;
1704 while not Present
(Protected_Body_Subprogram
(Subprg
)) loop
1705 Subprg
:= Scope
(Subprg
);
1708 -- Select the appropriate run-time call
1710 if Number_Entries
(Conctyp
) = 0 then
1712 New_Occurrence_Of
(RTE
(RE_Set_Ceiling
), Loc
);
1715 New_Occurrence_Of
(RTE
(RO_PE_Set_Ceiling
), Loc
);
1719 Make_Procedure_Call_Statement
(Loc
,
1720 Name
=> RT_Subprg_Name
,
1721 Parameter_Associations
=> New_List
(
1722 New_Copy_Tree
(First
(Parameter_Associations
(Ent
))),
1723 Relocate_Node
(Expression
(N
))));
1732 -- Deal with assignment checks unless suppressed
1734 if not Suppress_Assignment_Checks
(N
) then
1736 -- First deal with generation of range check if required
1738 if Do_Range_Check
(Rhs
) then
1739 Generate_Range_Check
(Rhs
, Typ
, CE_Range_Check_Failed
);
1742 -- Then generate predicate check if required
1744 Apply_Predicate_Check
(Rhs
, Typ
);
1747 -- Check for a special case where a high level transformation is
1748 -- required. If we have either of:
1753 -- where P is a reference to a bit packed array, then we have to unwind
1754 -- the assignment. The exact meaning of being a reference to a bit
1755 -- packed array is as follows:
1757 -- An indexed component whose prefix is a bit packed array is a
1758 -- reference to a bit packed array.
1760 -- An indexed component or selected component whose prefix is a
1761 -- reference to a bit packed array is itself a reference ot a
1762 -- bit packed array.
1764 -- The required transformation is
1766 -- Tnn : prefix_type := P;
1767 -- Tnn.field := rhs;
1772 -- Tnn : prefix_type := P;
1773 -- Tnn (subscr) := rhs;
1776 -- Since P is going to be evaluated more than once, any subscripts
1777 -- in P must have their evaluation forced.
1779 if Nkind_In
(Lhs
, N_Indexed_Component
, N_Selected_Component
)
1780 and then Is_Ref_To_Bit_Packed_Array
(Prefix
(Lhs
))
1783 BPAR_Expr
: constant Node_Id
:= Relocate_Node
(Prefix
(Lhs
));
1784 BPAR_Typ
: constant Entity_Id
:= Etype
(BPAR_Expr
);
1785 Tnn
: constant Entity_Id
:=
1786 Make_Temporary
(Loc
, 'T', BPAR_Expr
);
1789 -- Insert the post assignment first, because we want to copy the
1790 -- BPAR_Expr tree before it gets analyzed in the context of the
1791 -- pre assignment. Note that we do not analyze the post assignment
1792 -- yet (we cannot till we have completed the analysis of the pre
1793 -- assignment). As usual, the analysis of this post assignment
1794 -- will happen on its own when we "run into" it after finishing
1795 -- the current assignment.
1798 Make_Assignment_Statement
(Loc
,
1799 Name
=> New_Copy_Tree
(BPAR_Expr
),
1800 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
1802 -- At this stage BPAR_Expr is a reference to a bit packed array
1803 -- where the reference was not expanded in the original tree,
1804 -- since it was on the left side of an assignment. But in the
1805 -- pre-assignment statement (the object definition), BPAR_Expr
1806 -- will end up on the right hand side, and must be reexpanded. To
1807 -- achieve this, we reset the analyzed flag of all selected and
1808 -- indexed components down to the actual indexed component for
1809 -- the packed array.
1813 Set_Analyzed
(Exp
, False);
1816 (Exp
, N_Selected_Component
, N_Indexed_Component
)
1818 Exp
:= Prefix
(Exp
);
1824 -- Now we can insert and analyze the pre-assignment
1826 -- If the right-hand side requires a transient scope, it has
1827 -- already been placed on the stack. However, the declaration is
1828 -- inserted in the tree outside of this scope, and must reflect
1829 -- the proper scope for its variable. This awkward bit is forced
1830 -- by the stricter scope discipline imposed by GCC 2.97.
1833 Uses_Transient_Scope
: constant Boolean :=
1835 and then N
= Node_To_Be_Wrapped
;
1838 if Uses_Transient_Scope
then
1839 Push_Scope
(Scope
(Current_Scope
));
1842 Insert_Before_And_Analyze
(N
,
1843 Make_Object_Declaration
(Loc
,
1844 Defining_Identifier
=> Tnn
,
1845 Object_Definition
=> New_Occurrence_Of
(BPAR_Typ
, Loc
),
1846 Expression
=> BPAR_Expr
));
1848 if Uses_Transient_Scope
then
1853 -- Now fix up the original assignment and continue processing
1855 Rewrite
(Prefix
(Lhs
),
1856 New_Occurrence_Of
(Tnn
, Loc
));
1858 -- We do not need to reanalyze that assignment, and we do not need
1859 -- to worry about references to the temporary, but we do need to
1860 -- make sure that the temporary is not marked as a true constant
1861 -- since we now have a generated assignment to it.
1863 Set_Is_True_Constant
(Tnn
, False);
1867 -- When we have the appropriate type of aggregate in the expression (it
1868 -- has been determined during analysis of the aggregate by setting the
1869 -- delay flag), let's perform in place assignment and thus avoid
1870 -- creating a temporary.
1872 if Is_Delayed_Aggregate
(Rhs
) then
1873 Convert_Aggr_In_Assignment
(N
);
1874 Rewrite
(N
, Make_Null_Statement
(Loc
));
1879 -- Apply discriminant check if required. If Lhs is an access type to a
1880 -- designated type with discriminants, we must always check. If the
1881 -- type has unknown discriminants, more elaborate processing below.
1883 if Has_Discriminants
(Etype
(Lhs
))
1884 and then not Has_Unknown_Discriminants
(Etype
(Lhs
))
1886 -- Skip discriminant check if change of representation. Will be
1887 -- done when the change of representation is expanded out.
1890 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1893 -- If the type is private without discriminants, and the full type
1894 -- has discriminants (necessarily with defaults) a check may still be
1895 -- necessary if the Lhs is aliased. The private discriminants must be
1896 -- visible to build the discriminant constraints.
1898 -- Only an explicit dereference that comes from source indicates
1899 -- aliasing. Access to formals of protected operations and entries
1900 -- create dereferences but are not semantic aliasings.
1902 elsif Is_Private_Type
(Etype
(Lhs
))
1903 and then Has_Discriminants
(Typ
)
1904 and then Nkind
(Lhs
) = N_Explicit_Dereference
1905 and then Comes_From_Source
(Lhs
)
1908 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1909 Ubt
: Entity_Id
:= Base_Type
(Typ
);
1912 -- In the case of an expander-generated record subtype whose base
1913 -- type still appears private, Typ will have been set to that
1914 -- private type rather than the underlying record type (because
1915 -- Underlying type will have returned the record subtype), so it's
1916 -- necessary to apply Underlying_Type again to the base type to
1917 -- get the record type we need for the discriminant check. Such
1918 -- subtypes can be created for assignments in certain cases, such
1919 -- as within an instantiation passed this kind of private type.
1920 -- It would be good to avoid this special test, but making changes
1921 -- to prevent this odd form of record subtype seems difficult. ???
1923 if Is_Private_Type
(Ubt
) then
1924 Ubt
:= Underlying_Type
(Ubt
);
1927 Set_Etype
(Lhs
, Ubt
);
1928 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Ubt
), Rhs
));
1929 Apply_Discriminant_Check
(Rhs
, Ubt
, Lhs
);
1930 Set_Etype
(Lhs
, Lt
);
1933 -- If the Lhs has a private type with unknown discriminants, it may
1934 -- have a full view with discriminants, but those are nameable only
1935 -- in the underlying type, so convert the Rhs to it before potential
1936 -- checking. Convert Lhs as well, otherwise the actual subtype might
1937 -- not be constructible.
1939 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1940 and then Has_Discriminants
(Typ
)
1942 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1943 Rewrite
(Lhs
, OK_Convert_To
(Base_Type
(Typ
), Lhs
));
1944 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1946 -- In the access type case, we need the same discriminant check, and
1947 -- also range checks if we have an access to constrained array.
1949 elsif Is_Access_Type
(Etype
(Lhs
))
1950 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1952 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1954 -- Skip discriminant check if change of representation. Will be
1955 -- done when the change of representation is expanded out.
1958 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1961 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1962 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1964 if Is_Constrained
(Etype
(Lhs
)) then
1965 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1968 if Nkind
(Rhs
) = N_Allocator
then
1970 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1971 C_Es
: Check_Result
;
1978 Etype
(Designated_Type
(Etype
(Lhs
))));
1990 -- Apply range check for access type case
1992 elsif Is_Access_Type
(Etype
(Lhs
))
1993 and then Nkind
(Rhs
) = N_Allocator
1994 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1996 Analyze_And_Resolve
(Expression
(Rhs
));
1998 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
2001 -- Ada 2005 (AI-231): Generate the run-time check
2003 if Is_Access_Type
(Typ
)
2004 and then Can_Never_Be_Null
(Etype
(Lhs
))
2005 and then not Can_Never_Be_Null
(Etype
(Rhs
))
2007 -- If an actual is an out parameter of a null-excluding access
2008 -- type, there is access check on entry, so we set the flag
2009 -- Suppress_Assignment_Checks on the generated statement to
2010 -- assign the actual to the parameter block, and we do not want
2011 -- to generate an additional check at this point.
2013 and then not Suppress_Assignment_Checks
(N
)
2015 Apply_Constraint_Check
(Rhs
, Etype
(Lhs
));
2018 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
2019 -- stand-alone obj of an anonymous access type.
2021 if Is_Access_Type
(Typ
)
2022 and then Is_Entity_Name
(Lhs
)
2023 and then Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
2026 function Lhs_Entity
return Entity_Id
;
2027 -- Look through renames to find the underlying entity.
2028 -- For assignment to a rename, we don't care about the
2029 -- Enclosing_Dynamic_Scope of the rename declaration.
2035 function Lhs_Entity
return Entity_Id
is
2036 Result
: Entity_Id
:= Entity
(Lhs
);
2039 while Present
(Renamed_Object
(Result
)) loop
2041 -- Renamed_Object must return an Entity_Name here
2042 -- because of preceding "Present (E_E_A (...))" test.
2044 Result
:= Entity
(Renamed_Object
(Result
));
2050 -- Local Declarations
2052 Access_Check
: constant Node_Id
:=
2053 Make_Raise_Program_Error
(Loc
,
2057 Dynamic_Accessibility_Level
(Rhs
),
2059 Make_Integer_Literal
(Loc
,
2062 (Enclosing_Dynamic_Scope
2064 Reason
=> PE_Accessibility_Check_Failed
);
2066 Access_Level_Update
: constant Node_Id
:=
2067 Make_Assignment_Statement
(Loc
,
2070 (Effective_Extra_Accessibility
2071 (Entity
(Lhs
)), Loc
),
2073 Dynamic_Accessibility_Level
(Rhs
));
2076 if not Accessibility_Checks_Suppressed
(Entity
(Lhs
)) then
2077 Insert_Action
(N
, Access_Check
);
2080 Insert_Action
(N
, Access_Level_Update
);
2084 -- Case of assignment to a bit packed array element. If there is a
2085 -- change of representation this must be expanded into components,
2086 -- otherwise this is a bit-field assignment.
2088 if Nkind
(Lhs
) = N_Indexed_Component
2089 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
2091 -- Normal case, no change of representation
2094 Expand_Bit_Packed_Element_Set
(N
);
2097 -- Change of representation case
2100 -- Generate the following, to force component-by-component
2101 -- assignments in an efficient way. Otherwise each component
2102 -- will require a temporary and two bit-field manipulations.
2109 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2115 Make_Object_Declaration
(Loc
,
2116 Defining_Identifier
=> Tnn
,
2117 Object_Definition
=>
2118 New_Occurrence_Of
(Etype
(Lhs
), Loc
)),
2119 Make_Assignment_Statement
(Loc
,
2120 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
2121 Expression
=> Relocate_Node
(Rhs
)),
2122 Make_Assignment_Statement
(Loc
,
2123 Name
=> Relocate_Node
(Lhs
),
2124 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
2126 Insert_Actions
(N
, Stats
);
2127 Rewrite
(N
, Make_Null_Statement
(Loc
));
2132 -- Build-in-place function call case. Note that we're not yet doing
2133 -- build-in-place for user-written assignment statements (the assignment
2134 -- here came from an aggregate.)
2136 elsif Ada_Version
>= Ada_2005
2137 and then Is_Build_In_Place_Function_Call
(Rhs
)
2139 Make_Build_In_Place_Call_In_Assignment
(N
, Rhs
);
2141 elsif Is_Tagged_Type
(Typ
) and then Is_Value_Type
(Etype
(Lhs
)) then
2143 -- Nothing to do for valuetypes
2144 -- ??? Set_Scope_Is_Transient (False);
2148 elsif Is_Tagged_Type
(Typ
)
2149 or else (Needs_Finalization
(Typ
) and then not Is_Array_Type
(Typ
))
2151 Tagged_Case
: declare
2152 L
: List_Id
:= No_List
;
2153 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
2156 -- In the controlled case, we ensure that function calls are
2157 -- evaluated before finalizing the target. In all cases, it makes
2158 -- the expansion easier if the side-effects are removed first.
2160 Remove_Side_Effects
(Lhs
);
2161 Remove_Side_Effects
(Rhs
);
2163 -- Avoid recursion in the mechanism
2167 -- If dispatching assignment, we need to dispatch to _assign
2169 if Is_Class_Wide_Type
(Typ
)
2171 -- If the type is tagged, we may as well use the predefined
2172 -- primitive assignment. This avoids inlining a lot of code
2173 -- and in the class-wide case, the assignment is replaced
2174 -- by a dispatching call to _assign. It is suppressed in the
2175 -- case of assignments created by the expander that correspond
2176 -- to initializations, where we do want to copy the tag
2177 -- (Expand_Ctrl_Actions flag is set False in this case). It is
2178 -- also suppressed if restriction No_Dispatching_Calls is in
2179 -- force because in that case predefined primitives are not
2182 or else (Is_Tagged_Type
(Typ
)
2183 and then not Is_Value_Type
(Etype
(Lhs
))
2184 and then Chars
(Current_Scope
) /= Name_uAssign
2185 and then Expand_Ctrl_Actions
2187 not Restriction_Active
(No_Dispatching_Calls
))
2189 if Is_Limited_Type
(Typ
) then
2191 -- This can happen in an instance when the formal is an
2192 -- extension of a limited interface, and the actual is
2193 -- limited. This is an error according to AI05-0087, but
2194 -- is not caught at the point of instantiation in earlier
2197 -- This is wrong, error messages cannot be issued during
2198 -- expansion, since they would be missed in -gnatc mode ???
2200 Error_Msg_N
("assignment not available on limited type", N
);
2204 -- Fetch the primitive op _assign and proper type to call it.
2205 -- Because of possible conflicts between private and full view,
2206 -- fetch the proper type directly from the operation profile.
2209 Op
: constant Entity_Id
:=
2210 Find_Prim_Op
(Typ
, Name_uAssign
);
2211 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
2214 -- If the assignment is dispatching, make sure to use the
2217 if Is_Class_Wide_Type
(Typ
) then
2218 F_Typ
:= Class_Wide_Type
(F_Typ
);
2223 -- In case of assignment to a class-wide tagged type, before
2224 -- the assignment we generate run-time check to ensure that
2225 -- the tags of source and target match.
2227 if not Tag_Checks_Suppressed
(Typ
)
2228 and then Is_Class_Wide_Type
(Typ
)
2229 and then Is_Tagged_Type
(Typ
)
2230 and then Is_Tagged_Type
(Underlying_Type
(Etype
(Rhs
)))
2233 Make_Raise_Constraint_Error
(Loc
,
2237 Make_Selected_Component
(Loc
,
2238 Prefix
=> Duplicate_Subexpr
(Lhs
),
2240 Make_Identifier
(Loc
, Name_uTag
)),
2242 Make_Selected_Component
(Loc
,
2243 Prefix
=> Duplicate_Subexpr
(Rhs
),
2245 Make_Identifier
(Loc
, Name_uTag
))),
2246 Reason
=> CE_Tag_Check_Failed
));
2250 Left_N
: Node_Id
:= Duplicate_Subexpr
(Lhs
);
2251 Right_N
: Node_Id
:= Duplicate_Subexpr
(Rhs
);
2254 -- In order to dispatch the call to _assign the type of
2255 -- the actuals must match. Add conversion (if required).
2257 if Etype
(Lhs
) /= F_Typ
then
2258 Left_N
:= Unchecked_Convert_To
(F_Typ
, Left_N
);
2261 if Etype
(Rhs
) /= F_Typ
then
2262 Right_N
:= Unchecked_Convert_To
(F_Typ
, Right_N
);
2266 Make_Procedure_Call_Statement
(Loc
,
2267 Name
=> New_Occurrence_Of
(Op
, Loc
),
2268 Parameter_Associations
=> New_List
(
2270 Node2
=> Right_N
)));
2275 L
:= Make_Tag_Ctrl_Assignment
(N
);
2277 -- We can't afford to have destructive Finalization Actions in
2278 -- the Self assignment case, so if the target and the source
2279 -- are not obviously different, code is generated to avoid the
2280 -- self assignment case:
2282 -- if lhs'address /= rhs'address then
2283 -- <code for controlled and/or tagged assignment>
2286 -- Skip this if Restriction (No_Finalization) is active
2288 if not Statically_Different
(Lhs
, Rhs
)
2289 and then Expand_Ctrl_Actions
2290 and then not Restriction_Active
(No_Finalization
)
2293 Make_Implicit_If_Statement
(N
,
2297 Make_Attribute_Reference
(Loc
,
2298 Prefix
=> Duplicate_Subexpr
(Lhs
),
2299 Attribute_Name
=> Name_Address
),
2302 Make_Attribute_Reference
(Loc
,
2303 Prefix
=> Duplicate_Subexpr
(Rhs
),
2304 Attribute_Name
=> Name_Address
)),
2306 Then_Statements
=> L
));
2309 -- We need to set up an exception handler for implementing
2310 -- 7.6.1(18). The remaining adjustments are tackled by the
2311 -- implementation of adjust for record_controllers (see
2314 -- This is skipped if we have no finalization
2316 if Expand_Ctrl_Actions
2317 and then not Restriction_Active
(No_Finalization
)
2320 Make_Block_Statement
(Loc
,
2321 Handled_Statement_Sequence
=>
2322 Make_Handled_Sequence_Of_Statements
(Loc
,
2324 Exception_Handlers
=> New_List
(
2325 Make_Handler_For_Ctrl_Operation
(Loc
)))));
2330 Make_Block_Statement
(Loc
,
2331 Handled_Statement_Sequence
=>
2332 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
2334 -- If no restrictions on aborts, protect the whole assignment
2335 -- for controlled objects as per 9.8(11).
2337 if Needs_Finalization
(Typ
)
2338 and then Expand_Ctrl_Actions
2339 and then Abort_Allowed
2342 Blk
: constant Entity_Id
:=
2344 (E_Block
, Current_Scope
, Sloc
(N
), 'B');
2347 Set_Scope
(Blk
, Current_Scope
);
2348 Set_Etype
(Blk
, Standard_Void_Type
);
2349 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
2351 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2352 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
2353 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
2354 Expand_At_End_Handler
2355 (Handled_Statement_Sequence
(N
), Blk
);
2359 -- N has been rewritten to a block statement for which it is
2360 -- known by construction that no checks are necessary: analyze
2361 -- it with all checks suppressed.
2363 Analyze
(N
, Suppress
=> All_Checks
);
2369 elsif Is_Array_Type
(Typ
) then
2371 Actual_Rhs
: Node_Id
:= Rhs
;
2374 while Nkind_In
(Actual_Rhs
, N_Type_Conversion
,
2375 N_Qualified_Expression
)
2377 Actual_Rhs
:= Expression
(Actual_Rhs
);
2380 Expand_Assign_Array
(N
, Actual_Rhs
);
2386 elsif Is_Record_Type
(Typ
) then
2387 Expand_Assign_Record
(N
);
2390 -- Scalar types. This is where we perform the processing related to the
2391 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2394 elsif Is_Scalar_Type
(Typ
) then
2396 -- Case where right side is known valid
2398 if Expr_Known_Valid
(Rhs
) then
2400 -- Here the right side is valid, so it is fine. The case to deal
2401 -- with is when the left side is a local variable reference whose
2402 -- value is not currently known to be valid. If this is the case,
2403 -- and the assignment appears in an unconditional context, then
2404 -- we can mark the left side as now being valid if one of these
2405 -- conditions holds:
2407 -- The expression of the right side has Do_Range_Check set so
2408 -- that we know a range check will be performed. Note that it
2409 -- can be the case that a range check is omitted because we
2410 -- make the assumption that we can assume validity for operands
2411 -- appearing in the right side in determining whether a range
2412 -- check is required
2414 -- The subtype of the right side matches the subtype of the
2415 -- left side. In this case, even though we have not checked
2416 -- the range of the right side, we know it is in range of its
2417 -- subtype if the expression is valid.
2419 if Is_Local_Variable_Reference
(Lhs
)
2420 and then not Is_Known_Valid
(Entity
(Lhs
))
2421 and then In_Unconditional_Context
(N
)
2423 if Do_Range_Check
(Rhs
)
2424 or else Etype
(Lhs
) = Etype
(Rhs
)
2426 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2430 -- Case where right side may be invalid in the sense of the RM
2431 -- reference above. The RM does not require that we check for the
2432 -- validity on an assignment, but it does require that the assignment
2433 -- of an invalid value not cause erroneous behavior.
2435 -- The general approach in GNAT is to use the Is_Known_Valid flag
2436 -- to avoid the need for validity checking on assignments. However
2437 -- in some cases, we have to do validity checking in order to make
2438 -- sure that the setting of this flag is correct.
2441 -- Validate right side if we are validating copies
2443 if Validity_Checks_On
2444 and then Validity_Check_Copies
2446 -- Skip this if left hand side is an array or record component
2447 -- and elementary component validity checks are suppressed.
2449 if Nkind_In
(Lhs
, N_Selected_Component
, N_Indexed_Component
)
2450 and then not Validity_Check_Components
2457 -- We can propagate this to the left side where appropriate
2459 if Is_Local_Variable_Reference
(Lhs
)
2460 and then not Is_Known_Valid
(Entity
(Lhs
))
2461 and then In_Unconditional_Context
(N
)
2463 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2466 -- Otherwise check to see what should be done
2468 -- If left side is a local variable, then we just set its flag to
2469 -- indicate that its value may no longer be valid, since we are
2470 -- copying a potentially invalid value.
2472 elsif Is_Local_Variable_Reference
(Lhs
) then
2473 Set_Is_Known_Valid
(Entity
(Lhs
), False);
2475 -- Check for case of a nonlocal variable on the left side which
2476 -- is currently known to be valid. In this case, we simply ensure
2477 -- that the right side is valid. We only play the game of copying
2478 -- validity status for local variables, since we are doing this
2479 -- statically, not by tracing the full flow graph.
2481 elsif Is_Entity_Name
(Lhs
)
2482 and then Is_Known_Valid
(Entity
(Lhs
))
2484 -- Note: If Validity_Checking mode is set to none, we ignore
2485 -- the Ensure_Valid call so don't worry about that case here.
2489 -- In all other cases, we can safely copy an invalid value without
2490 -- worrying about the status of the left side. Since it is not a
2491 -- variable reference it will not be considered
2492 -- as being known to be valid in any case.
2501 when RE_Not_Available
=>
2503 end Expand_N_Assignment_Statement
;
2505 ------------------------------
2506 -- Expand_N_Block_Statement --
2507 ------------------------------
2509 -- Encode entity names defined in block statement
2511 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
2513 Qualify_Entity_Names
(N
);
2514 end Expand_N_Block_Statement
;
2516 -----------------------------
2517 -- Expand_N_Case_Statement --
2518 -----------------------------
2520 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
2521 Loc
: constant Source_Ptr
:= Sloc
(N
);
2522 Expr
: constant Node_Id
:= Expression
(N
);
2530 -- Check for the situation where we know at compile time which branch
2533 if Compile_Time_Known_Value
(Expr
) then
2534 Alt
:= Find_Static_Alternative
(N
);
2536 -- Do not consider controlled objects found in a case statement which
2537 -- actually models a case expression because their early finalization
2538 -- will affect the result of the expression.
2540 if not From_Conditional_Expression
(N
) then
2541 Process_Statements_For_Controlled_Objects
(Alt
);
2544 -- Move statements from this alternative after the case statement.
2545 -- They are already analyzed, so will be skipped by the analyzer.
2547 Insert_List_After
(N
, Statements
(Alt
));
2549 -- That leaves the case statement as a shell. So now we can kill all
2550 -- other alternatives in the case statement.
2552 Kill_Dead_Code
(Expression
(N
));
2558 -- Loop through case alternatives, skipping pragmas, and skipping
2559 -- the one alternative that we select (and therefore retain).
2561 Dead_Alt
:= First
(Alternatives
(N
));
2562 while Present
(Dead_Alt
) loop
2564 and then Nkind
(Dead_Alt
) = N_Case_Statement_Alternative
2566 Kill_Dead_Code
(Statements
(Dead_Alt
), Warn_On_Deleted_Code
);
2573 Rewrite
(N
, Make_Null_Statement
(Loc
));
2577 -- Here if the choice is not determined at compile time
2580 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
2582 Others_Present
: Boolean;
2583 Others_Node
: Node_Id
;
2585 Then_Stms
: List_Id
;
2586 Else_Stms
: List_Id
;
2589 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
2590 Others_Present
:= True;
2591 Others_Node
:= Last_Alt
;
2593 Others_Present
:= False;
2596 -- First step is to worry about possible invalid argument. The RM
2597 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2598 -- outside the base range), then Constraint_Error must be raised.
2600 -- Case of validity check required (validity checks are on, the
2601 -- expression is not known to be valid, and the case statement
2602 -- comes from source -- no need to validity check internally
2603 -- generated case statements).
2605 if Validity_Check_Default
then
2606 Ensure_Valid
(Expr
);
2609 -- If there is only a single alternative, just replace it with the
2610 -- sequence of statements since obviously that is what is going to
2611 -- be executed in all cases.
2613 Len
:= List_Length
(Alternatives
(N
));
2617 -- We still need to evaluate the expression if it has any side
2620 Remove_Side_Effects
(Expression
(N
));
2621 Alt
:= First
(Alternatives
(N
));
2623 -- Do not consider controlled objects found in a case statement
2624 -- which actually models a case expression because their early
2625 -- finalization will affect the result of the expression.
2627 if not From_Conditional_Expression
(N
) then
2628 Process_Statements_For_Controlled_Objects
(Alt
);
2631 Insert_List_After
(N
, Statements
(Alt
));
2633 -- That leaves the case statement as a shell. The alternative that
2634 -- will be executed is reset to a null list. So now we can kill
2635 -- the entire case statement.
2637 Kill_Dead_Code
(Expression
(N
));
2638 Rewrite
(N
, Make_Null_Statement
(Loc
));
2641 -- An optimization. If there are only two alternatives, and only
2642 -- a single choice, then rewrite the whole case statement as an
2643 -- if statement, since this can result in subsequent optimizations.
2644 -- This helps not only with case statements in the source of a
2645 -- simple form, but also with generated code (discriminant check
2646 -- functions in particular).
2648 -- Note: it is OK to do this before expanding out choices for any
2649 -- static predicates, since the if statement processing will handle
2650 -- the static predicate case fine.
2653 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2655 if List_Length
(Chlist
) = 1 then
2656 Choice
:= First
(Chlist
);
2658 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2659 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2661 -- For TRUE, generate "expression", not expression = true
2663 if Nkind
(Choice
) = N_Identifier
2664 and then Entity
(Choice
) = Standard_True
2666 Cond
:= Expression
(N
);
2668 -- For FALSE, generate "expression" and switch then/else
2670 elsif Nkind
(Choice
) = N_Identifier
2671 and then Entity
(Choice
) = Standard_False
2673 Cond
:= Expression
(N
);
2674 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2675 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2677 -- For a range, generate "expression in range"
2679 elsif Nkind
(Choice
) = N_Range
2680 or else (Nkind
(Choice
) = N_Attribute_Reference
2681 and then Attribute_Name
(Choice
) = Name_Range
)
2682 or else (Is_Entity_Name
(Choice
)
2683 and then Is_Type
(Entity
(Choice
)))
2687 Left_Opnd
=> Expression
(N
),
2688 Right_Opnd
=> Relocate_Node
(Choice
));
2690 -- A subtype indication is not a legal operator in a membership
2691 -- test, so retrieve its range.
2693 elsif Nkind
(Choice
) = N_Subtype_Indication
then
2696 Left_Opnd
=> Expression
(N
),
2699 (Range_Expression
(Constraint
(Choice
))));
2701 -- For any other subexpression "expression = value"
2706 Left_Opnd
=> Expression
(N
),
2707 Right_Opnd
=> Relocate_Node
(Choice
));
2710 -- Now rewrite the case as an IF
2713 Make_If_Statement
(Loc
,
2715 Then_Statements
=> Then_Stms
,
2716 Else_Statements
=> Else_Stms
));
2722 -- If the last alternative is not an Others choice, replace it with
2723 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2724 -- the modified case statement, since it's only effect would be to
2725 -- compute the contents of the Others_Discrete_Choices which is not
2726 -- needed by the back end anyway.
2728 -- The reason for this is that the back end always needs some default
2729 -- for a switch, so if we have not supplied one in the processing
2730 -- above for validity checking, then we need to supply one here.
2732 if not Others_Present
then
2733 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2734 Set_Others_Discrete_Choices
2735 (Others_Node
, Discrete_Choices
(Last_Alt
));
2736 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2739 -- Deal with possible declarations of controlled objects, and also
2740 -- with rewriting choice sequences for static predicate references.
2742 Alt
:= First_Non_Pragma
(Alternatives
(N
));
2743 while Present
(Alt
) loop
2745 -- Do not consider controlled objects found in a case statement
2746 -- which actually models a case expression because their early
2747 -- finalization will affect the result of the expression.
2749 if not From_Conditional_Expression
(N
) then
2750 Process_Statements_For_Controlled_Objects
(Alt
);
2753 if Has_SP_Choice
(Alt
) then
2754 Expand_Static_Predicates_In_Choices
(Alt
);
2757 Next_Non_Pragma
(Alt
);
2760 end Expand_N_Case_Statement
;
2762 -----------------------------
2763 -- Expand_N_Exit_Statement --
2764 -----------------------------
2766 -- The only processing required is to deal with a possible C/Fortran
2767 -- boolean value used as the condition for the exit statement.
2769 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2771 Adjust_Condition
(Condition
(N
));
2772 end Expand_N_Exit_Statement
;
2774 ----------------------------------
2775 -- Expand_Formal_Container_Loop --
2776 ----------------------------------
2778 procedure Expand_Formal_Container_Loop
(N
: Node_Id
) is
2779 Loc
: constant Source_Ptr
:= Sloc
(N
);
2780 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2781 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2782 Cursor
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2783 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2784 Stats
: constant List_Id
:= Statements
(N
);
2792 -- The expansion resembles the one for Ada containers, but the
2793 -- primitives mention the domain of iteration explicitly, and
2794 -- function First applied to the container yields a cursor directly.
2796 -- Cursor : Cursor_type := First (Container);
2797 -- while Has_Element (Cursor, Container) loop
2798 -- <original loop statements>
2799 -- Cursor := Next (Container, Cursor);
2802 Build_Formal_Container_Iteration
2803 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2805 Set_Ekind
(Cursor
, E_Variable
);
2806 Append_To
(Stats
, Advance
);
2808 -- Build block to capture declaration of cursor entity.
2811 Make_Block_Statement
(Loc
,
2812 Declarations
=> New_List
(Init
),
2813 Handled_Statement_Sequence
=>
2814 Make_Handled_Sequence_Of_Statements
(Loc
,
2815 Statements
=> New_List
(New_Loop
)));
2817 Rewrite
(N
, Blk_Nod
);
2819 end Expand_Formal_Container_Loop
;
2821 ------------------------------------------
2822 -- Expand_Formal_Container_Element_Loop --
2823 ------------------------------------------
2825 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
) is
2826 Loc
: constant Source_Ptr
:= Sloc
(N
);
2827 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2828 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2829 Element
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2830 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2831 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
2832 Stats
: constant List_Id
:= Statements
(N
);
2834 Cursor
: constant Entity_Id
:=
2835 Make_Defining_Identifier
(Loc
,
2836 Chars
=> New_External_Name
(Chars
(Element
), 'C'));
2837 Elmt_Decl
: Node_Id
;
2840 Element_Op
: constant Entity_Id
:=
2841 Get_Iterable_Type_Primitive
(Container_Typ
, Name_Element
);
2848 -- For an element iterator, the Element aspect must be present,
2849 -- (this is checked during analysis) and the expansion takes the form:
2851 -- Cursor : Cursor_type := First (Container);
2852 -- Elmt : Element_Type;
2853 -- while Has_Element (Cursor, Container) loop
2854 -- Elmt := Element (Container, Cursor);
2855 -- <original loop statements>
2856 -- Cursor := Next (Container, Cursor);
2859 Build_Formal_Container_Iteration
2860 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2862 Set_Ekind
(Cursor
, E_Variable
);
2863 Insert_Action
(N
, Init
);
2865 -- Declaration for Element.
2868 Make_Object_Declaration
(Loc
,
2869 Defining_Identifier
=> Element
,
2870 Object_Definition
=> New_Occurrence_Of
(Etype
(Element_Op
), Loc
));
2872 -- The element is only modified in expanded code, so it appears as
2873 -- unassigned to the warning machinery. We must suppress this spurious
2874 -- warning explicitly.
2876 Set_Warnings_Off
(Element
);
2879 Make_Assignment_Statement
(Loc
,
2880 Name
=> New_Occurrence_Of
(Element
, Loc
),
2882 Make_Function_Call
(Loc
,
2883 Name
=> New_Occurrence_Of
(Element_Op
, Loc
),
2884 Parameter_Associations
=> New_List
(
2885 New_Occurrence_Of
(Container
, Loc
),
2886 New_Occurrence_Of
(Cursor
, Loc
))));
2888 Prepend
(Elmt_Ref
, Stats
);
2889 Append_To
(Stats
, Advance
);
2891 -- The loop is rewritten as a block, to hold the element declaration
2894 Make_Block_Statement
(Loc
,
2895 Declarations
=> New_List
(Elmt_Decl
),
2896 Handled_Statement_Sequence
=>
2897 Make_Handled_Sequence_Of_Statements
(Loc
,
2898 Statements
=> New_List
(New_Loop
)));
2900 Rewrite
(N
, New_Loop
);
2902 -- The loop parameter is declared by an object declaration, but within
2903 -- the loop we must prevent user assignments to it, so we analyze the
2904 -- declaration and reset the entity kind, before analyzing the rest of
2907 Analyze
(Elmt_Decl
);
2908 Set_Ekind
(Defining_Identifier
(Elmt_Decl
), E_Loop_Parameter
);
2909 Set_Assignment_OK
(Name
(Elmt_Ref
));
2912 end Expand_Formal_Container_Element_Loop
;
2914 -----------------------------
2915 -- Expand_N_Goto_Statement --
2916 -----------------------------
2918 -- Add poll before goto if polling active
2920 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2922 Generate_Poll_Call
(N
);
2923 end Expand_N_Goto_Statement
;
2925 ---------------------------
2926 -- Expand_N_If_Statement --
2927 ---------------------------
2929 -- First we deal with the case of C and Fortran convention boolean values,
2930 -- with zero/non-zero semantics.
2932 -- Second, we deal with the obvious rewriting for the cases where the
2933 -- condition of the IF is known at compile time to be True or False.
2935 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2936 -- rewrite as independent if statements. For example:
2947 -- <<condition actions of y>>
2953 -- This rewriting is needed if at least one elsif part has a non-empty
2954 -- Condition_Actions list. We also do the same processing if there is a
2955 -- constant condition in an elsif part (in conjunction with the first
2956 -- processing step mentioned above, for the recursive call made to deal
2957 -- with the created inner if, this deals with properly optimizing the
2958 -- cases of constant elsif conditions).
2960 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2961 Loc
: constant Source_Ptr
:= Sloc
(N
);
2966 Warn_If_Deleted
: constant Boolean :=
2967 Warn_On_Deleted_Code
and then Comes_From_Source
(N
);
2968 -- Indicates whether we want warnings when we delete branches of the
2969 -- if statement based on constant condition analysis. We never want
2970 -- these warnings for expander generated code.
2973 -- Do not consider controlled objects found in an if statement which
2974 -- actually models an if expression because their early finalization
2975 -- will affect the result of the expression.
2977 if not From_Conditional_Expression
(N
) then
2978 Process_Statements_For_Controlled_Objects
(N
);
2981 Adjust_Condition
(Condition
(N
));
2983 -- The following loop deals with constant conditions for the IF. We
2984 -- need a loop because as we eliminate False conditions, we grab the
2985 -- first elsif condition and use it as the primary condition.
2987 while Compile_Time_Known_Value
(Condition
(N
)) loop
2989 -- If condition is True, we can simply rewrite the if statement now
2990 -- by replacing it by the series of then statements.
2992 if Is_True
(Expr_Value
(Condition
(N
))) then
2994 -- All the else parts can be killed
2996 Kill_Dead_Code
(Elsif_Parts
(N
), Warn_If_Deleted
);
2997 Kill_Dead_Code
(Else_Statements
(N
), Warn_If_Deleted
);
2999 Hed
:= Remove_Head
(Then_Statements
(N
));
3000 Insert_List_After
(N
, Then_Statements
(N
));
3004 -- If condition is False, then we can delete the condition and
3005 -- the Then statements
3008 -- We do not delete the condition if constant condition warnings
3009 -- are enabled, since otherwise we end up deleting the desired
3010 -- warning. Of course the backend will get rid of this True/False
3011 -- test anyway, so nothing is lost here.
3013 if not Constant_Condition_Warnings
then
3014 Kill_Dead_Code
(Condition
(N
));
3017 Kill_Dead_Code
(Then_Statements
(N
), Warn_If_Deleted
);
3019 -- If there are no elsif statements, then we simply replace the
3020 -- entire if statement by the sequence of else statements.
3022 if No
(Elsif_Parts
(N
)) then
3023 if No
(Else_Statements
(N
))
3024 or else Is_Empty_List
(Else_Statements
(N
))
3027 Make_Null_Statement
(Sloc
(N
)));
3029 Hed
:= Remove_Head
(Else_Statements
(N
));
3030 Insert_List_After
(N
, Else_Statements
(N
));
3036 -- If there are elsif statements, the first of them becomes the
3037 -- if/then section of the rebuilt if statement This is the case
3038 -- where we loop to reprocess this copied condition.
3041 Hed
:= Remove_Head
(Elsif_Parts
(N
));
3042 Insert_Actions
(N
, Condition_Actions
(Hed
));
3043 Set_Condition
(N
, Condition
(Hed
));
3044 Set_Then_Statements
(N
, Then_Statements
(Hed
));
3046 -- Hed might have been captured as the condition determining
3047 -- the current value for an entity. Now it is detached from
3048 -- the tree, so a Current_Value pointer in the condition might
3049 -- need to be updated.
3051 Set_Current_Value_Condition
(N
);
3053 if Is_Empty_List
(Elsif_Parts
(N
)) then
3054 Set_Elsif_Parts
(N
, No_List
);
3060 -- Loop through elsif parts, dealing with constant conditions and
3061 -- possible condition actions that are present.
3063 if Present
(Elsif_Parts
(N
)) then
3064 E
:= First
(Elsif_Parts
(N
));
3065 while Present
(E
) loop
3067 -- Do not consider controlled objects found in an if statement
3068 -- which actually models an if expression because their early
3069 -- finalization will affect the result of the expression.
3071 if not From_Conditional_Expression
(N
) then
3072 Process_Statements_For_Controlled_Objects
(E
);
3075 Adjust_Condition
(Condition
(E
));
3077 -- If there are condition actions, then rewrite the if statement
3078 -- as indicated above. We also do the same rewrite for a True or
3079 -- False condition. The further processing of this constant
3080 -- condition is then done by the recursive call to expand the
3081 -- newly created if statement
3083 if Present
(Condition_Actions
(E
))
3084 or else Compile_Time_Known_Value
(Condition
(E
))
3086 -- Note this is not an implicit if statement, since it is part
3087 -- of an explicit if statement in the source (or of an implicit
3088 -- if statement that has already been tested).
3091 Make_If_Statement
(Sloc
(E
),
3092 Condition
=> Condition
(E
),
3093 Then_Statements
=> Then_Statements
(E
),
3094 Elsif_Parts
=> No_List
,
3095 Else_Statements
=> Else_Statements
(N
));
3097 -- Elsif parts for new if come from remaining elsif's of parent
3099 while Present
(Next
(E
)) loop
3100 if No
(Elsif_Parts
(New_If
)) then
3101 Set_Elsif_Parts
(New_If
, New_List
);
3104 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
3107 Set_Else_Statements
(N
, New_List
(New_If
));
3109 if Present
(Condition_Actions
(E
)) then
3110 Insert_List_Before
(New_If
, Condition_Actions
(E
));
3115 if Is_Empty_List
(Elsif_Parts
(N
)) then
3116 Set_Elsif_Parts
(N
, No_List
);
3122 -- No special processing for that elsif part, move to next
3130 -- Some more optimizations applicable if we still have an IF statement
3132 if Nkind
(N
) /= N_If_Statement
then
3136 -- Another optimization, special cases that can be simplified
3138 -- if expression then
3144 -- can be changed to:
3146 -- return expression;
3150 -- if expression then
3156 -- can be changed to:
3158 -- return not (expression);
3160 -- Only do these optimizations if we are at least at -O1 level and
3161 -- do not do them if control flow optimizations are suppressed.
3163 if Optimization_Level
> 0
3164 and then not Opt
.Suppress_Control_Flow_Optimizations
3166 if Nkind
(N
) = N_If_Statement
3167 and then No
(Elsif_Parts
(N
))
3168 and then Present
(Else_Statements
(N
))
3169 and then List_Length
(Then_Statements
(N
)) = 1
3170 and then List_Length
(Else_Statements
(N
)) = 1
3173 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
3174 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
3177 if Nkind
(Then_Stm
) = N_Simple_Return_Statement
3179 Nkind
(Else_Stm
) = N_Simple_Return_Statement
3182 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
3183 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
3186 if Nkind
(Then_Expr
) = N_Identifier
3188 Nkind
(Else_Expr
) = N_Identifier
3190 if Entity
(Then_Expr
) = Standard_True
3191 and then Entity
(Else_Expr
) = Standard_False
3194 Make_Simple_Return_Statement
(Loc
,
3195 Expression
=> Relocate_Node
(Condition
(N
))));
3199 elsif Entity
(Then_Expr
) = Standard_False
3200 and then Entity
(Else_Expr
) = Standard_True
3203 Make_Simple_Return_Statement
(Loc
,
3207 Relocate_Node
(Condition
(N
)))));
3217 end Expand_N_If_Statement
;
3219 --------------------------
3220 -- Expand_Iterator_Loop --
3221 --------------------------
3223 procedure Expand_Iterator_Loop
(N
: Node_Id
) is
3224 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3225 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3226 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3227 Loc
: constant Source_Ptr
:= Sloc
(N
);
3229 Container
: constant Node_Id
:= Name
(I_Spec
);
3230 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
3231 I_Kind
: constant Entity_Kind
:= Ekind
(Id
);
3233 Iterator
: Entity_Id
;
3235 Stats
: List_Id
:= Statements
(N
);
3238 -- Processing for arrays
3240 if Is_Array_Type
(Container_Typ
) then
3241 Expand_Iterator_Loop_Over_Array
(N
);
3244 elsif Has_Aspect
(Container_Typ
, Aspect_Iterable
) then
3245 if Of_Present
(I_Spec
) then
3246 Expand_Formal_Container_Element_Loop
(N
);
3248 Expand_Formal_Container_Loop
(N
);
3254 -- Processing for containers
3256 -- For an "of" iterator the name is a container expression, which
3257 -- is transformed into a call to the default iterator.
3259 -- For an iterator of the form "in" the name is a function call
3260 -- that delivers an iterator type.
3262 -- In both cases, analysis of the iterator has introduced an object
3263 -- declaration to capture the domain, so that Container is an entity.
3265 -- The for loop is expanded into a while loop which uses a container
3266 -- specific cursor to desgnate each element.
3268 -- Iter : Iterator_Type := Container.Iterate;
3269 -- Cursor : Cursor_type := First (Iter);
3270 -- while Has_Element (Iter) loop
3272 -- -- The block is added when Element_Type is controlled
3274 -- Obj : Pack.Element_Type := Element (Cursor);
3275 -- -- for the "of" loop form
3277 -- <original loop statements>
3280 -- Cursor := Iter.Next (Cursor);
3283 -- If "reverse" is present, then the initialization of the cursor
3284 -- uses Last and the step becomes Prev. Pack is the name of the
3285 -- scope where the container package is instantiated.
3288 Element_Type
: constant Entity_Id
:= Etype
(Id
);
3289 Iter_Type
: Entity_Id
;
3292 Name_Init
: Name_Id
;
3293 Name_Step
: Name_Id
;
3296 -- The type of the iterator is the return type of the Iterate
3297 -- function used. For the "of" form this is the default iterator
3298 -- for the type, otherwise it is the type of the explicit
3299 -- function used in the iterator specification. The most common
3300 -- case will be an Iterate function in the container package.
3302 -- The primitive operations of the container type may not be
3303 -- use-visible, so we introduce the name of the enclosing package
3304 -- in the declarations below. The Iterator type is declared in a
3305 -- an instance within the container package itself.
3307 -- If the container type is a derived type, the cursor type is
3308 -- found in the package of the parent type.
3310 if Is_Derived_Type
(Container_Typ
) then
3311 Pack
:= Scope
(Root_Type
(Container_Typ
));
3313 Pack
:= Scope
(Container_Typ
);
3316 Iter_Type
:= Etype
(Name
(I_Spec
));
3318 -- The "of" case uses an internally generated cursor whose type
3319 -- is found in the container package. The domain of iteration
3320 -- is expanded into a call to the default Iterator function, but
3321 -- this expansion does not take place in quantified expressions
3322 -- that are analyzed with expansion disabled, and in that case the
3323 -- type of the iterator must be obtained from the aspect.
3325 if Of_Present
(I_Spec
) then
3327 Default_Iter
: Entity_Id
;
3328 Container_Arg
: Node_Id
;
3331 function Get_Default_Iterator
3332 (T
: Entity_Id
) return Entity_Id
;
3333 -- If the container is a derived type, the aspect holds the
3334 -- parent operation. The required one is a primitive of the
3335 -- derived type and is either inherited or overridden.
3337 --------------------------
3338 -- Get_Default_Iterator --
3339 --------------------------
3341 function Get_Default_Iterator
3342 (T
: Entity_Id
) return Entity_Id
3344 Iter
: constant Entity_Id
:=
3345 Entity
(Find_Value_Of_Aspect
(T
, Aspect_Default_Iterator
));
3350 Container_Arg
:= New_Copy_Tree
(Container
);
3352 -- A previous version of GNAT allowed indexing aspects to
3353 -- be redefined on derived container types, while the
3354 -- default iterator was inherited from the aprent type.
3355 -- This non-standard extension is preserved temporarily for
3356 -- use by the modelling project under debug flag d.X.
3358 if Debug_Flag_Dot_XX
then
3359 if Base_Type
(Etype
(Container
)) /=
3360 Base_Type
(Etype
(First_Formal
(Iter
)))
3363 Make_Type_Conversion
(Loc
,
3366 (Etype
(First_Formal
(Iter
)), Loc
),
3367 Expression
=> Container_Arg
);
3372 elsif Is_Derived_Type
(T
) then
3374 -- The default iterator must be a primitive operation
3375 -- of the type, at the same dispatch slot position.
3377 Prim
:= First_Elmt
(Primitive_Operations
(T
));
3378 while Present
(Prim
) loop
3381 if Chars
(Op
) = Chars
(Iter
)
3382 and then DT_Position
(Op
) = DT_Position
(Iter
)
3390 -- default iterator must exist.
3392 pragma Assert
(False);
3394 else -- not a derived type
3397 end Get_Default_Iterator
;
3399 -- Start of processing for Handle_Of
3402 if Is_Class_Wide_Type
(Container_Typ
) then
3404 Get_Default_Iterator
(Etype
(Base_Type
(Container_Typ
)));
3407 Default_Iter
:= Get_Default_Iterator
(Etype
(Container
));
3410 Cursor
:= Make_Temporary
(Loc
, 'C');
3412 -- For an container element iterator, the iterator type
3413 -- is obtained from the corresponding aspect, whose return
3414 -- type is descended from the corresponding interface type
3415 -- in some instance of Ada.Iterator_Interfaces. The actuals
3416 -- of that instantiation are Cursor and Has_Element.
3418 Iter_Type
:= Etype
(Default_Iter
);
3420 -- The iterator type, which is a class_wide type, may itself
3421 -- be derived locally, so the desired instantiation is the
3422 -- scope of the root type of the iterator type.
3424 Pack
:= Scope
(Root_Type
(Etype
(Iter_Type
)));
3426 -- Rewrite domain of iteration as a call to the default
3427 -- iterator for the container type.
3429 Rewrite
(Name
(I_Spec
),
3430 Make_Function_Call
(Loc
,
3431 Name
=> New_Occurrence_Of
(Default_Iter
, Loc
),
3432 Parameter_Associations
=>
3433 New_List
(Container_Arg
)));
3434 Analyze_And_Resolve
(Name
(I_Spec
));
3436 -- Find cursor type in proper iterator package, which is an
3437 -- instantiation of Iterator_Interfaces.
3439 Ent
:= First_Entity
(Pack
);
3440 while Present
(Ent
) loop
3441 if Chars
(Ent
) = Name_Cursor
then
3442 Set_Etype
(Cursor
, Etype
(Ent
));
3449 -- Id : Element_Type renames Container (Cursor);
3450 -- This assumes that the container type has an indexing
3451 -- operation with Cursor. The check that this operation
3452 -- exists is performed in Check_Container_Indexing.
3455 Make_Object_Renaming_Declaration
(Loc
,
3456 Defining_Identifier
=> Id
,
3458 New_Occurrence_Of
(Element_Type
, Loc
),
3460 Make_Indexed_Component
(Loc
,
3461 Prefix
=> Relocate_Node
(Container_Arg
),
3463 New_List
(New_Occurrence_Of
(Cursor
, Loc
))));
3465 -- The defining identifier in the iterator is user-visible
3466 -- and must be visible in the debugger.
3468 Set_Debug_Info_Needed
(Id
);
3470 -- If the container does not have a variable indexing aspect,
3471 -- the element is a constant in the loop.
3473 if No
(Find_Value_Of_Aspect
3474 (Container_Typ
, Aspect_Variable_Indexing
))
3476 Set_Ekind
(Id
, E_Constant
);
3479 -- If the container holds controlled objects, wrap the loop
3480 -- statements and element renaming declaration with a block.
3481 -- This ensures that the result of Element (Cusor) is
3482 -- cleaned up after each iteration of the loop.
3484 if Needs_Finalization
(Element_Type
) then
3488 -- Id : Element_Type := Element (curosr);
3490 -- <original loop statements>
3494 Make_Block_Statement
(Loc
,
3495 Declarations
=> New_List
(Decl
),
3496 Handled_Statement_Sequence
=>
3497 Make_Handled_Sequence_Of_Statements
(Loc
,
3498 Statements
=> Stats
)));
3500 -- Elements do not need finalization
3503 Prepend_To
(Stats
, Decl
);
3507 -- X in Iterate (S) : type of iterator is type of explicitly
3508 -- given Iterate function, and the loop variable is the cursor.
3509 -- It will be assigned in the loop and must be a variable.
3515 Iterator
:= Make_Temporary
(Loc
, 'I');
3517 -- Determine the advancement and initialization steps for the
3520 -- Analysis of the expanded loop will verify that the container
3521 -- has a reverse iterator.
3523 if Reverse_Present
(I_Spec
) then
3524 Name_Init
:= Name_Last
;
3525 Name_Step
:= Name_Previous
;
3528 Name_Init
:= Name_First
;
3529 Name_Step
:= Name_Next
;
3532 -- For both iterator forms, add a call to the step operation to
3533 -- advance the cursor. Generate:
3535 -- Cursor := Iterator.Next (Cursor);
3539 -- Cursor := Next (Cursor);
3546 Make_Function_Call
(Loc
,
3548 Make_Selected_Component
(Loc
,
3549 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3550 Selector_Name
=> Make_Identifier
(Loc
, Name_Step
)),
3551 Parameter_Associations
=> New_List
(
3552 New_Occurrence_Of
(Cursor
, Loc
)));
3555 Make_Assignment_Statement
(Loc
,
3556 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
3557 Expression
=> Rhs
));
3558 Set_Assignment_OK
(Name
(Last
(Stats
)));
3562 -- while Iterator.Has_Element loop
3566 -- Has_Element is the second actual in the iterator package
3569 Make_Loop_Statement
(Loc
,
3571 Make_Iteration_Scheme
(Loc
,
3573 Make_Function_Call
(Loc
,
3576 Next_Entity
(First_Entity
(Pack
)), Loc
),
3577 Parameter_Associations
=>
3578 New_List
(New_Occurrence_Of
(Cursor
, Loc
)))),
3580 Statements
=> Stats
,
3581 End_Label
=> Empty
);
3583 -- If present, preserve identifier of loop, which can be used in
3584 -- an exit statement in the body.
3586 if Present
(Identifier
(N
)) then
3587 Set_Identifier
(New_Loop
, Relocate_Node
(Identifier
(N
)));
3590 -- Create the declarations for Iterator and cursor and insert them
3591 -- before the source loop. Given that the domain of iteration is
3592 -- already an entity, the iterator is just a renaming of that
3593 -- entity. Possible optimization ???
3596 -- I : Iterator_Type renames Container;
3597 -- C : Cursor_Type := Container.[First | Last];
3600 Make_Object_Renaming_Declaration
(Loc
,
3601 Defining_Identifier
=> Iterator
,
3602 Subtype_Mark
=> New_Occurrence_Of
(Iter_Type
, Loc
),
3603 Name
=> Relocate_Node
(Name
(I_Spec
))));
3605 -- Create declaration for cursor
3612 Make_Object_Declaration
(Loc
,
3613 Defining_Identifier
=> Cursor
,
3614 Object_Definition
=>
3615 New_Occurrence_Of
(Etype
(Cursor
), Loc
),
3617 Make_Selected_Component
(Loc
,
3618 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3620 Make_Identifier
(Loc
, Name_Init
)));
3622 -- The cursor is only modified in expanded code, so it appears
3623 -- as unassigned to the warning machinery. We must suppress
3624 -- this spurious warning explicitly. The cursor's kind is that of
3625 -- the original loop parameter (it is a constant if the domain of
3626 -- iteration is constant).
3628 Set_Warnings_Off
(Cursor
);
3629 Set_Assignment_OK
(Decl
);
3631 Insert_Action
(N
, Decl
);
3632 Set_Ekind
(Cursor
, I_Kind
);
3635 -- If the range of iteration is given by a function call that
3636 -- returns a container, the finalization actions have been saved
3637 -- in the Condition_Actions of the iterator. Insert them now at
3638 -- the head of the loop.
3640 if Present
(Condition_Actions
(Isc
)) then
3641 Insert_List_Before
(N
, Condition_Actions
(Isc
));
3645 Rewrite
(N
, New_Loop
);
3647 end Expand_Iterator_Loop
;
3649 -------------------------------------
3650 -- Expand_Iterator_Loop_Over_Array --
3651 -------------------------------------
3653 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
) is
3654 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3655 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3656 Array_Node
: constant Node_Id
:= Name
(I_Spec
);
3657 Array_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Array_Node
));
3658 Array_Dim
: constant Pos
:= Number_Dimensions
(Array_Typ
);
3659 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3660 Loc
: constant Source_Ptr
:= Sloc
(N
);
3661 Stats
: constant List_Id
:= Statements
(N
);
3662 Core_Loop
: Node_Id
;
3664 Iterator
: Entity_Id
;
3666 -- Start of processing for Expand_Iterator_Loop_Over_Array
3669 -- for Element of Array loop
3671 -- This case requires an internally generated cursor to iterate over
3674 if Of_Present
(I_Spec
) then
3675 Iterator
:= Make_Temporary
(Loc
, 'C');
3678 -- Element : Component_Type renames Array (Iterator);
3681 Make_Indexed_Component
(Loc
,
3682 Prefix
=> Relocate_Node
(Array_Node
),
3683 Expressions
=> New_List
(New_Occurrence_Of
(Iterator
, Loc
)));
3686 Make_Object_Renaming_Declaration
(Loc
,
3687 Defining_Identifier
=> Id
,
3689 New_Occurrence_Of
(Component_Type
(Array_Typ
), Loc
),
3692 -- Mark the loop variable as needing debug info, so that expansion
3693 -- of the renaming will result in Materialize_Entity getting set via
3694 -- Debug_Renaming_Declaration. (This setting is needed here because
3695 -- the setting in Freeze_Entity comes after the expansion, which is
3698 Set_Debug_Info_Needed
(Id
);
3700 -- for Index in Array loop
3702 -- This case utilizes the already given iterator name
3710 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3711 -- Element : Component_Type renames Array (Iterator);
3712 -- <original loop statements>
3716 Make_Loop_Statement
(Loc
,
3718 Make_Iteration_Scheme
(Loc
,
3719 Loop_Parameter_Specification
=>
3720 Make_Loop_Parameter_Specification
(Loc
,
3721 Defining_Identifier
=> Iterator
,
3722 Discrete_Subtype_Definition
=>
3723 Make_Attribute_Reference
(Loc
,
3724 Prefix
=> Relocate_Node
(Array_Node
),
3725 Attribute_Name
=> Name_Range
,
3726 Expressions
=> New_List
(
3727 Make_Integer_Literal
(Loc
, Array_Dim
))),
3728 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3729 Statements
=> Stats
,
3730 End_Label
=> Empty
);
3732 -- Processing for multidimensional array
3734 if Array_Dim
> 1 then
3735 for Dim
in 1 .. Array_Dim
- 1 loop
3736 Iterator
:= Make_Temporary
(Loc
, 'C');
3738 -- Generate the dimension loops starting from the innermost one
3740 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3745 Make_Loop_Statement
(Loc
,
3747 Make_Iteration_Scheme
(Loc
,
3748 Loop_Parameter_Specification
=>
3749 Make_Loop_Parameter_Specification
(Loc
,
3750 Defining_Identifier
=> Iterator
,
3751 Discrete_Subtype_Definition
=>
3752 Make_Attribute_Reference
(Loc
,
3753 Prefix
=> Relocate_Node
(Array_Node
),
3754 Attribute_Name
=> Name_Range
,
3755 Expressions
=> New_List
(
3756 Make_Integer_Literal
(Loc
, Array_Dim
- Dim
))),
3757 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3758 Statements
=> New_List
(Core_Loop
),
3759 End_Label
=> Empty
);
3761 -- Update the previously created object renaming declaration with
3762 -- the new iterator.
3764 Prepend_To
(Expressions
(Ind_Comp
),
3765 New_Occurrence_Of
(Iterator
, Loc
));
3769 -- Inherit the loop identifier from the original loop. This ensures that
3770 -- the scope stack is consistent after the rewriting.
3772 if Present
(Identifier
(N
)) then
3773 Set_Identifier
(Core_Loop
, Relocate_Node
(Identifier
(N
)));
3776 Rewrite
(N
, Core_Loop
);
3778 end Expand_Iterator_Loop_Over_Array
;
3780 -----------------------------
3781 -- Expand_N_Loop_Statement --
3782 -----------------------------
3784 -- 1. Remove null loop entirely
3785 -- 2. Deal with while condition for C/Fortran boolean
3786 -- 3. Deal with loops with a non-standard enumeration type range
3787 -- 4. Deal with while loops where Condition_Actions is set
3788 -- 5. Deal with loops over predicated subtypes
3789 -- 6. Deal with loops with iterators over arrays and containers
3790 -- 7. Insert polling call if required
3792 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
3793 Loc
: constant Source_Ptr
:= Sloc
(N
);
3794 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
3800 if Is_Null_Loop
(N
) then
3801 Rewrite
(N
, Make_Null_Statement
(Loc
));
3805 -- Deal with condition for C/Fortran Boolean
3807 if Present
(Scheme
) then
3808 Adjust_Condition
(Condition
(Scheme
));
3811 -- Generate polling call
3813 if Is_Non_Empty_List
(Statements
(N
)) then
3814 Generate_Poll_Call
(First
(Statements
(N
)));
3817 -- Nothing more to do for plain loop with no iteration scheme
3822 -- Case of for loop (Loop_Parameter_Specification present)
3824 -- Note: we do not have to worry about validity checking of the for loop
3825 -- range bounds here, since they were frozen with constant declarations
3826 -- and it is during that process that the validity checking is done.
3828 elsif Present
(Loop_Parameter_Specification
(Scheme
)) then
3830 LPS
: constant Node_Id
:=
3831 Loop_Parameter_Specification
(Scheme
);
3832 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3833 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3834 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
3840 -- Deal with loop over predicates
3842 if Is_Discrete_Type
(Ltype
)
3843 and then Present
(Predicate_Function
(Ltype
))
3845 Expand_Predicated_Loop
(N
);
3847 -- Handle the case where we have a for loop with the range type
3848 -- being an enumeration type with non-standard representation.
3849 -- In this case we expand:
3851 -- for x in [reverse] a .. b loop
3857 -- for xP in [reverse] integer
3858 -- range etype'Pos (a) .. etype'Pos (b)
3861 -- x : constant etype := Pos_To_Rep (xP);
3867 elsif Is_Enumeration_Type
(Btype
)
3868 and then Present
(Enum_Pos_To_Rep
(Btype
))
3871 Make_Defining_Identifier
(Loc
,
3872 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
3874 -- If the type has a contiguous representation, successive
3875 -- values can be generated as offsets from the first literal.
3877 if Has_Contiguous_Rep
(Btype
) then
3879 Unchecked_Convert_To
(Btype
,
3882 Make_Integer_Literal
(Loc
,
3883 Enumeration_Rep
(First_Literal
(Btype
))),
3884 Right_Opnd
=> New_Occurrence_Of
(New_Id
, Loc
)));
3886 -- Use the constructed array Enum_Pos_To_Rep
3889 Make_Indexed_Component
(Loc
,
3891 New_Occurrence_Of
(Enum_Pos_To_Rep
(Btype
), Loc
),
3893 New_List
(New_Occurrence_Of
(New_Id
, Loc
)));
3896 -- Build declaration for loop identifier
3900 Make_Object_Declaration
(Loc
,
3901 Defining_Identifier
=> Loop_Id
,
3902 Constant_Present
=> True,
3903 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
3904 Expression
=> Expr
));
3907 Make_Loop_Statement
(Loc
,
3908 Identifier
=> Identifier
(N
),
3911 Make_Iteration_Scheme
(Loc
,
3912 Loop_Parameter_Specification
=>
3913 Make_Loop_Parameter_Specification
(Loc
,
3914 Defining_Identifier
=> New_Id
,
3915 Reverse_Present
=> Reverse_Present
(LPS
),
3917 Discrete_Subtype_Definition
=>
3918 Make_Subtype_Indication
(Loc
,
3921 New_Occurrence_Of
(Standard_Natural
, Loc
),
3924 Make_Range_Constraint
(Loc
,
3929 Make_Attribute_Reference
(Loc
,
3931 New_Occurrence_Of
(Btype
, Loc
),
3933 Attribute_Name
=> Name_Pos
,
3935 Expressions
=> New_List
(
3937 (Type_Low_Bound
(Ltype
)))),
3940 Make_Attribute_Reference
(Loc
,
3942 New_Occurrence_Of
(Btype
, Loc
),
3944 Attribute_Name
=> Name_Pos
,
3946 Expressions
=> New_List
(
3951 Statements
=> New_List
(
3952 Make_Block_Statement
(Loc
,
3953 Declarations
=> Decls
,
3954 Handled_Statement_Sequence
=>
3955 Make_Handled_Sequence_Of_Statements
(Loc
,
3956 Statements
=> Statements
(N
)))),
3958 End_Label
=> End_Label
(N
)));
3960 -- The loop parameter's entity must be removed from the loop
3961 -- scope's entity list and rendered invisible, since it will
3962 -- now be located in the new block scope. Any other entities
3963 -- already associated with the loop scope, such as the loop
3964 -- parameter's subtype, will remain there.
3966 -- In an element loop, the loop will contain a declaration for
3967 -- a cursor variable; otherwise the loop id is the first entity
3968 -- in the scope constructed for the loop.
3970 if Comes_From_Source
(Loop_Id
) then
3971 pragma Assert
(First_Entity
(Scope
(Loop_Id
)) = Loop_Id
);
3975 Set_First_Entity
(Scope
(Loop_Id
), Next_Entity
(Loop_Id
));
3976 Remove_Homonym
(Loop_Id
);
3978 if Last_Entity
(Scope
(Loop_Id
)) = Loop_Id
then
3979 Set_Last_Entity
(Scope
(Loop_Id
), Empty
);
3984 -- Nothing to do with other cases of for loops
3991 -- Second case, if we have a while loop with Condition_Actions set, then
3992 -- we change it into a plain loop:
4001 -- <<condition actions>>
4006 elsif Present
(Scheme
)
4007 and then Present
(Condition_Actions
(Scheme
))
4008 and then Present
(Condition
(Scheme
))
4015 Make_Exit_Statement
(Sloc
(Condition
(Scheme
)),
4017 Make_Op_Not
(Sloc
(Condition
(Scheme
)),
4018 Right_Opnd
=> Condition
(Scheme
)));
4020 Prepend
(ES
, Statements
(N
));
4021 Insert_List_Before
(ES
, Condition_Actions
(Scheme
));
4023 -- This is not an implicit loop, since it is generated in response
4024 -- to the loop statement being processed. If this is itself
4025 -- implicit, the restriction has already been checked. If not,
4026 -- it is an explicit loop.
4029 Make_Loop_Statement
(Sloc
(N
),
4030 Identifier
=> Identifier
(N
),
4031 Statements
=> Statements
(N
),
4032 End_Label
=> End_Label
(N
)));
4037 -- Here to deal with iterator case
4039 elsif Present
(Scheme
)
4040 and then Present
(Iterator_Specification
(Scheme
))
4042 Expand_Iterator_Loop
(N
);
4044 -- An iterator loop may generate renaming declarations for elements
4045 -- that require debug information. This is the case in particular
4046 -- with element iterators, where debug information must be generated
4047 -- for the temporary that holds the element value. These temporaries
4048 -- are created within a transient block whose local declarations are
4049 -- transferred to the loop, which now has non-trivial local objects.
4051 if Nkind
(N
) = N_Loop_Statement
4052 and then Present
(Identifier
(N
))
4054 Qualify_Entity_Names
(N
);
4058 -- When the iteration scheme mentiones attribute 'Loop_Entry, the loop
4059 -- is transformed into a conditional block where the original loop is
4060 -- the sole statement. Inspect the statements of the nested loop for
4061 -- controlled objects.
4065 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
4066 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
4069 Process_Statements_For_Controlled_Objects
(Stmt
);
4070 end Expand_N_Loop_Statement
;
4072 ----------------------------
4073 -- Expand_Predicated_Loop --
4074 ----------------------------
4076 -- Note: the expander can handle generation of loops over predicated
4077 -- subtypes for both the dynamic and static cases. Depending on what
4078 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
4079 -- mode, the semantic analyzer may disallow one or both forms.
4081 procedure Expand_Predicated_Loop
(N
: Node_Id
) is
4082 Loc
: constant Source_Ptr
:= Sloc
(N
);
4083 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
4084 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
4085 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
4086 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
4087 Stat
: constant List_Id
:= Static_Discrete_Predicate
(Ltype
);
4088 Stmts
: constant List_Id
:= Statements
(N
);
4091 -- Case of iteration over non-static predicate, should not be possible
4092 -- since this is not allowed by the semantics and should have been
4093 -- caught during analysis of the loop statement.
4096 raise Program_Error
;
4098 -- If the predicate list is empty, that corresponds to a predicate of
4099 -- False, in which case the loop won't run at all, and we rewrite the
4100 -- entire loop as a null statement.
4102 elsif Is_Empty_List
(Stat
) then
4103 Rewrite
(N
, Make_Null_Statement
(Loc
));
4106 -- For expansion over a static predicate we generate the following
4109 -- J : Ltype := min-val;
4114 -- when endpoint => J := startpoint;
4115 -- when endpoint => J := startpoint;
4117 -- when max-val => exit;
4118 -- when others => J := Lval'Succ (J);
4123 -- To make this a little clearer, let's take a specific example:
4125 -- type Int is range 1 .. 10;
4126 -- subtype L is Int with
4127 -- predicate => L in 3 | 10 | 5 .. 7;
4129 -- for L in StaticP loop
4130 -- Put_Line ("static:" & J'Img);
4133 -- In this case, the loop is transformed into
4140 -- when 3 => J := 5;
4141 -- when 7 => J := 10;
4143 -- when others => J := L'Succ (J);
4149 Static_Predicate
: declare
4156 function Lo_Val
(N
: Node_Id
) return Node_Id
;
4157 -- Given static expression or static range, returns an identifier
4158 -- whose value is the low bound of the expression value or range.
4160 function Hi_Val
(N
: Node_Id
) return Node_Id
;
4161 -- Given static expression or static range, returns an identifier
4162 -- whose value is the high bound of the expression value or range.
4168 function Hi_Val
(N
: Node_Id
) return Node_Id
is
4170 if Is_OK_Static_Expression
(N
) then
4171 return New_Copy
(N
);
4173 pragma Assert
(Nkind
(N
) = N_Range
);
4174 return New_Copy
(High_Bound
(N
));
4182 function Lo_Val
(N
: Node_Id
) return Node_Id
is
4184 if Is_OK_Static_Expression
(N
) then
4185 return New_Copy
(N
);
4187 pragma Assert
(Nkind
(N
) = N_Range
);
4188 return New_Copy
(Low_Bound
(N
));
4192 -- Start of processing for Static_Predicate
4195 -- Convert loop identifier to normal variable and reanalyze it so
4196 -- that this conversion works. We have to use the same defining
4197 -- identifier, since there may be references in the loop body.
4199 Set_Analyzed
(Loop_Id
, False);
4200 Set_Ekind
(Loop_Id
, E_Variable
);
4202 -- In most loops the loop variable is assigned in various
4203 -- alternatives in the body. However, in the rare case when
4204 -- the range specifies a single element, the loop variable
4205 -- may trigger a spurious warning that is could be constant.
4206 -- This warning might as well be suppressed.
4208 Set_Warnings_Off
(Loop_Id
);
4210 -- Loop to create branches of case statement
4214 while Present
(P
) loop
4215 if No
(Next
(P
)) then
4216 S
:= Make_Exit_Statement
(Loc
);
4219 Make_Assignment_Statement
(Loc
,
4220 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4221 Expression
=> Lo_Val
(Next
(P
)));
4222 Set_Suppress_Assignment_Checks
(S
);
4226 Make_Case_Statement_Alternative
(Loc
,
4227 Statements
=> New_List
(S
),
4228 Discrete_Choices
=> New_List
(Hi_Val
(P
))));
4233 -- Add others choice
4236 Make_Assignment_Statement
(Loc
,
4237 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4239 Make_Attribute_Reference
(Loc
,
4240 Prefix
=> New_Occurrence_Of
(Ltype
, Loc
),
4241 Attribute_Name
=> Name_Succ
,
4242 Expressions
=> New_List
(
4243 New_Occurrence_Of
(Loop_Id
, Loc
))));
4244 Set_Suppress_Assignment_Checks
(S
);
4247 Make_Case_Statement_Alternative
(Loc
,
4248 Discrete_Choices
=> New_List
(Make_Others_Choice
(Loc
)),
4249 Statements
=> New_List
(S
)));
4251 -- Construct case statement and append to body statements
4254 Make_Case_Statement
(Loc
,
4255 Expression
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4256 Alternatives
=> Alts
);
4257 Append_To
(Stmts
, Cstm
);
4262 Make_Object_Declaration
(Loc
,
4263 Defining_Identifier
=> Loop_Id
,
4264 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
4265 Expression
=> Lo_Val
(First
(Stat
)));
4266 Set_Suppress_Assignment_Checks
(D
);
4269 Make_Block_Statement
(Loc
,
4270 Declarations
=> New_List
(D
),
4271 Handled_Statement_Sequence
=>
4272 Make_Handled_Sequence_Of_Statements
(Loc
,
4273 Statements
=> New_List
(
4274 Make_Loop_Statement
(Loc
,
4275 Statements
=> Stmts
,
4276 End_Label
=> Empty
)))));
4279 end Static_Predicate
;
4281 end Expand_Predicated_Loop
;
4283 ------------------------------
4284 -- Make_Tag_Ctrl_Assignment --
4285 ------------------------------
4287 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
4288 Asn
: constant Node_Id
:= Relocate_Node
(N
);
4289 L
: constant Node_Id
:= Name
(N
);
4290 Loc
: constant Source_Ptr
:= Sloc
(N
);
4291 Res
: constant List_Id
:= New_List
;
4292 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
4294 Comp_Asn
: constant Boolean := Is_Fully_Repped_Tagged_Type
(T
);
4295 Ctrl_Act
: constant Boolean := Needs_Finalization
(T
)
4296 and then not No_Ctrl_Actions
(N
);
4297 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
4298 and then not Comp_Asn
4299 and then not No_Ctrl_Actions
(N
)
4300 and then Tagged_Type_Expansion
;
4301 -- Tags are not saved and restored when VM_Target because VM tags are
4302 -- represented implicitly in objects.
4304 Next_Id
: Entity_Id
;
4305 Prev_Id
: Entity_Id
;
4309 -- Finalize the target of the assignment when controlled
4311 -- We have two exceptions here:
4313 -- 1. If we are in an init proc since it is an initialization more
4314 -- than an assignment.
4316 -- 2. If the left-hand side is a temporary that was not initialized
4317 -- (or the parent part of a temporary since it is the case in
4318 -- extension aggregates). Such a temporary does not come from
4319 -- source. We must examine the original node for the prefix, because
4320 -- it may be a component of an entry formal, in which case it has
4321 -- been rewritten and does not appear to come from source either.
4323 -- Case of init proc
4325 if not Ctrl_Act
then
4328 -- The left hand side is an uninitialized temporary object
4330 elsif Nkind
(L
) = N_Type_Conversion
4331 and then Is_Entity_Name
(Expression
(L
))
4332 and then Nkind
(Parent
(Entity
(Expression
(L
)))) =
4333 N_Object_Declaration
4334 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
4341 (Obj_Ref
=> Duplicate_Subexpr_No_Checks
(L
),
4345 -- Save the Tag in a local variable Tag_Id
4348 Tag_Id
:= Make_Temporary
(Loc
, 'A');
4351 Make_Object_Declaration
(Loc
,
4352 Defining_Identifier
=> Tag_Id
,
4353 Object_Definition
=> New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
4355 Make_Selected_Component
(Loc
,
4356 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4358 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
))));
4360 -- Otherwise Tag_Id is not used
4366 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
4367 -- VM targets since the fields are not part of the object.
4369 if VM_Target
/= No_VM
4370 and then Is_Controlled
(T
)
4372 Prev_Id
:= Make_Temporary
(Loc
, 'P');
4373 Next_Id
:= Make_Temporary
(Loc
, 'N');
4376 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4379 Make_Object_Declaration
(Loc
,
4380 Defining_Identifier
=> Prev_Id
,
4381 Object_Definition
=>
4382 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4384 Make_Selected_Component
(Loc
,
4386 Unchecked_Convert_To
4387 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4389 Make_Identifier
(Loc
, Name_Prev
))));
4392 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4395 Make_Object_Declaration
(Loc
,
4396 Defining_Identifier
=> Next_Id
,
4397 Object_Definition
=>
4398 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4400 Make_Selected_Component
(Loc
,
4402 Unchecked_Convert_To
4403 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4405 Make_Identifier
(Loc
, Name_Next
))));
4408 -- If the tagged type has a full rep clause, expand the assignment into
4409 -- component-wise assignments. Mark the node as unanalyzed in order to
4410 -- generate the proper code and propagate this scenario by setting a
4411 -- flag to avoid infinite recursion.
4414 Set_Analyzed
(Asn
, False);
4415 Set_Componentwise_Assignment
(Asn
, True);
4418 Append_To
(Res
, Asn
);
4424 Make_Assignment_Statement
(Loc
,
4426 Make_Selected_Component
(Loc
,
4427 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4429 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
)),
4430 Expression
=> New_Occurrence_Of
(Tag_Id
, Loc
)));
4433 -- Restore the Prev and Next fields on .NET/JVM
4435 if VM_Target
/= No_VM
4436 and then Is_Controlled
(T
)
4439 -- Root_Controlled (L).Prev := Prev_Id;
4442 Make_Assignment_Statement
(Loc
,
4444 Make_Selected_Component
(Loc
,
4446 Unchecked_Convert_To
4447 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4449 Make_Identifier
(Loc
, Name_Prev
)),
4450 Expression
=> New_Occurrence_Of
(Prev_Id
, Loc
)));
4453 -- Root_Controlled (L).Next := Next_Id;
4456 Make_Assignment_Statement
(Loc
,
4458 Make_Selected_Component
(Loc
,
4460 Unchecked_Convert_To
4461 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4462 Selector_Name
=> Make_Identifier
(Loc
, Name_Next
)),
4463 Expression
=> New_Occurrence_Of
(Next_Id
, Loc
)));
4466 -- Adjust the target after the assignment when controlled (not in the
4467 -- init proc since it is an initialization more than an assignment).
4472 (Obj_Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
4480 -- Could use comment here ???
4482 when RE_Not_Available
=>
4484 end Make_Tag_Ctrl_Assignment
;