2015-01-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch5.adb
blobfc6141a53ad6283bcb0569e226ed4c869bcec97a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 5 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sinfo; use Sinfo;
49 with Sem; use Sem;
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
68 (N : Node_Id;
69 Container : Entity_Id;
70 Cursor : Entity_Id;
71 Init : out Node_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
80 -- and record cases.
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
91 (N : Node_Id;
92 Larray : Entity_Id;
93 Rarray : Entity_Id;
94 L_Type : Entity_Id;
95 R_Type : Entity_Id;
96 Ndim : Pos;
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
125 -- internally.
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
149 (N : Node_Id;
150 Container : Entity_Id;
151 Cursor : Entity_Id;
152 Init : out Node_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);
166 begin
167 -- Declaration for Cursor
169 Init :=
170 Make_Object_Declaration (Loc,
171 Defining_Identifier => Cursor,
172 Object_Definition => New_Occurrence_Of (Etype (First_Op), Loc),
173 Expression =>
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
181 Advance :=
182 Make_Assignment_Statement (Loc,
183 Name => New_Occurrence_Of (Cursor, Loc),
184 Expression =>
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
193 New_Loop :=
194 Make_Loop_Statement (Loc,
195 Iteration_Scheme =>
196 Make_Iteration_Scheme (Loc,
197 Condition =>
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)))),
203 Statements => Stats,
204 End_Label => Empty);
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);
213 begin
214 return
215 Nkind (Rhs) = N_Type_Conversion
216 and then
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);
248 Larray : Node_Id;
249 Rarray : Node_Id;
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);
282 begin
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));
287 end if;
288 end Apply_Dereference;
290 ------------------------
291 -- Has_Address_Clause --
292 ------------------------
294 function Has_Address_Clause (Exp : Node_Id) return Boolean is
295 begin
296 return
297 (Is_Entity_Name (Exp) and then
298 Present (Address_Clause (Entity (Exp))))
299 or else
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
308 begin
309 return
310 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
311 or else
312 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
313 end Is_Formal_Array;
315 ------------------------
316 -- Is_Non_Local_Array --
317 ------------------------
319 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
320 begin
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
337 begin
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
361 -- the whole array).
363 -- Note: No overlap is possible if there is a change of representation,
364 -- so we can exclude this case.
366 if Ndim = 1
367 and then not Crep
368 and then
369 ((Lhs_Formal and Rhs_Formal)
370 or else
371 (Lhs_Formal and Rhs_Non_Local_Var)
372 or else
373 (Rhs_Formal and Lhs_Non_Local_Var))
374 and then
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
385 then
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.
393 end if;
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)
402 then
403 Set_Forwards_OK (N, False);
404 Set_Backwards_OK (N, False);
405 end if;
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
410 -- situation).
412 if Crep then
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)
420 or else
421 Possible_Bit_Aligned_Component (Rhs)
422 then
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)
434 or else
435 Is_Atomic_Object (Act_Rhs)
436 then
437 return;
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))
446 then
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:
466 -- procedure C52 is
467 -- type BITS is array(INTEGER range <>) of BOOLEAN;
468 -- pragma PACK(BITS);
469 -- type A is access BITS;
470 -- P1,P2 : A;
471 -- begin
472 -- P1 := new BITS (1 .. 65_535);
473 -- P2 := new BITS (1 .. 65_535);
474 -- P2.ALL := P1.ALL;
475 -- end C52;
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);
482 -- begin
483 -- File.Storage := Contents;
484 -- end Write_All;
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));
504 P_Type : Entity_Id;
505 Des_Type : Entity_Id;
507 begin
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))
511 then
512 return True;
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
518 return False;
520 else
521 Des_Type := Designated_Type (P_Type);
522 return
523 Is_Bit_Packed_Array (Des_Type)
524 and then not Is_Constrained (Des_Type);
525 end if;
527 else
528 return False;
529 end if;
530 end Is_UBPA_Reference;
532 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
534 begin
535 if Is_UBPA_Reference (Lhs)
536 or else
537 Is_UBPA_Reference (Rhs)
538 then
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
548 return;
549 end if;
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)
567 then
568 Rewrite (N, Make_Null_Statement (Loc));
569 Analyze (N);
570 end if;
572 return;
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)
583 then
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
588 -- handle things.
590 elsif not (L_Slice and R_Slice) then
591 if Forwards_OK (N) then
592 return;
593 end if;
594 end if;
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
600 declare
601 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Rhs);
602 Decl : Node_Id;
604 begin
605 Decl :=
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);
614 end;
615 end if;
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
632 -- in the front end.
634 declare
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;
646 Cleft_Lo : Node_Id;
647 Cright_Lo : Node_Id;
648 Condition : Node_Id;
650 Cresult : Compare_Result;
652 begin
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);
664 else
665 Larray := Act_Lhs;
667 if Is_Private_Type (Etype (Larray)) then
668 declare
669 Par : constant Node_Id := Parent (Larray);
670 begin
671 Larray :=
672 Unchecked_Convert_To
673 (Underlying_Type (Etype (Larray)), Larray);
674 Set_Parent (Larray, Par);
675 end;
676 end if;
677 end if;
679 if Nkind (Act_Rhs) = N_Slice then
680 Rarray := Prefix (Act_Rhs);
681 else
682 Rarray := Act_Rhs;
684 if Is_Private_Type (Etype (Rarray)) then
685 declare
686 Par : constant Node_Id := Parent (Rarray);
687 begin
688 Rarray :=
689 Unchecked_Convert_To
690 (Underlying_Type (Etype (Rarray)), Rarray);
691 Set_Parent (Rarray, Par);
692 end;
693 end if;
694 end if;
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)
712 then
713 null;
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
718 -- assume it.
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.
725 else
726 Cresult :=
727 Compile_Time_Compare
728 (Left_Lo, Right_Lo, Assume_Valid => True);
730 if Cresult = Unknown then
731 Cresult :=
732 Compile_Time_Compare
733 (Left_Hi, Right_Hi, Assume_Valid => True);
734 end if;
736 case Cresult is
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);
741 end case;
742 end if;
743 end if;
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
755 return;
757 -- Assume other back ends can handle it if Forwards_OK is set
759 elsif Forwards_OK (N) then
760 return;
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
767 return;
768 end if;
769 end if;
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);
780 -- end loop;
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
784 -- efficient.
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);
792 -- end loop;
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
796 -- efficient.
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:
807 -- declare
808 -- T : constant <operand-type> := rhs;
809 -- begin
810 -- lhs := T;
811 -- end;
813 -- If implicit conditionals are permitted, then we generate:
815 -- if Left_Lo <= Right_Lo then
816 -- <code for Forwards_OK = True above>
817 -- else
818 -- <code for Backwards_OK = True above>
819 -- end if;
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)
831 and then
832 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
833 and then Nkind (Act_Rhs) = N_Slice
834 then
835 Rarray := Rhs;
836 end if;
838 if Is_Entity_Name (Lhs)
839 and then
840 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
841 and then Nkind (Act_Lhs) = N_Slice
842 then
843 Larray := Lhs;
844 end if;
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)
851 and then Ndim = 1
852 and then not No_Ctrl_Actions (N)
853 then
854 declare
855 Proc : constant Entity_Id :=
856 TSS (Base_Type (L_Type), TSS_Slice_Assign);
857 Actuals : List_Id;
859 begin
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));
870 Append_To (Actuals,
871 New_Occurrence_Of (
872 Boolean_Literals (not Forwards_OK (N)), Loc));
874 Rewrite (N,
875 Make_Procedure_Call_Statement (Loc,
876 Name => New_Occurrence_Of (Proc, Loc),
877 Parameter_Associations => Actuals));
878 end;
880 else
881 Rewrite (N,
882 Expand_Assign_Array_Loop
883 (N, Larray, Rarray, L_Type, R_Type, Ndim,
884 Rev => not Forwards_OK (N)));
885 end if;
887 -- Case of both are false with No_Implicit_Conditionals
889 elsif Restriction_Active (No_Implicit_Conditionals) then
890 declare
891 T : constant Entity_Id :=
892 Make_Defining_Identifier (Loc, Chars => Name_T);
894 begin
895 Rewrite (N,
896 Make_Block_Statement (Loc,
897 Declarations => New_List (
898 Make_Object_Declaration (Loc,
899 Defining_Identifier => T,
900 Constant_Present => True,
901 Object_Definition =>
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))))));
911 end;
913 -- Case of both are false with implicit conditionals allowed
915 else
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
930 Condition :=
931 Make_Op_Le (Loc,
932 Left_Opnd =>
933 Unchecked_Convert_To (RTE (RE_Integer_Address),
934 Make_Attribute_Reference (Loc,
935 Prefix =>
936 Make_Indexed_Component (Loc,
937 Prefix =>
938 Duplicate_Subexpr_Move_Checks (Larray, True),
939 Expressions => New_List (
940 Make_Attribute_Reference (Loc,
941 Prefix =>
942 New_Occurrence_Of
943 (L_Index_Typ, Loc),
944 Attribute_Name => Name_First))),
945 Attribute_Name => Name_Address)),
947 Right_Opnd =>
948 Unchecked_Convert_To (RTE (RE_Integer_Address),
949 Make_Attribute_Reference (Loc,
950 Prefix =>
951 Make_Indexed_Component (Loc,
952 Prefix =>
953 Duplicate_Subexpr_Move_Checks (Rarray, True),
954 Expressions => New_List (
955 Make_Attribute_Reference (Loc,
956 Prefix =>
957 New_Occurrence_Of
958 (R_Index_Typ, 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
965 -- conversions ???
967 else
968 -- Copy the bounds
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
977 Cright_Lo :=
978 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
979 end if;
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);
988 Condition :=
989 Make_Op_Le (Loc,
990 Left_Opnd => Cleft_Lo,
991 Right_Opnd => Cright_Lo);
992 end if;
994 if Needs_Finalization (Component_Type (L_Type))
995 and then Base_Type (L_Type) = Base_Type (R_Type)
996 and then Ndim = 1
997 and then not No_Ctrl_Actions (N)
998 then
1000 -- Call TSS procedure for array assignment, passing the
1001 -- explicit bounds of right and left hand sides.
1003 declare
1004 Proc : constant Entity_Id :=
1005 TSS (Base_Type (L_Type), TSS_Slice_Assign);
1006 Actuals : List_Id;
1008 begin
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));
1019 Append_To (Actuals,
1020 Make_Op_Not (Loc,
1021 Right_Opnd => Condition));
1023 Rewrite (N,
1024 Make_Procedure_Call_Statement (Loc,
1025 Name => New_Occurrence_Of (Proc, Loc),
1026 Parameter_Associations => Actuals));
1027 end;
1029 else
1030 Rewrite (N,
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,
1037 Rev => False)),
1039 Else_Statements => New_List (
1040 Expand_Assign_Array_Loop
1041 (N, Larray, Rarray, L_Type, R_Type, Ndim,
1042 Rev => True))));
1043 end if;
1044 end if;
1046 Analyze (N, Suppress => All_Checks);
1047 end;
1049 exception
1050 when RE_Not_Available =>
1051 return;
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:
1061 -- declare
1062 -- R2b : Tm1X1 := 1;
1063 -- begin
1064 -- for L1b in 1 .. 100 loop
1065 -- declare
1066 -- R4b : Tm1X2 := 1;
1067 -- begin
1068 -- for L3b in 1 .. 100 loop
1069 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
1070 -- R4b := Tm1X2'succ(R4b);
1071 -- end loop;
1072 -- end;
1073 -- R2b := Tm1X1'succ(R2b);
1074 -- end loop;
1075 -- end;
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
1082 (N : Node_Id;
1083 Larray : Entity_Id;
1084 Rarray : Entity_Id;
1085 L_Type : Entity_Id;
1086 R_Type : Entity_Id;
1087 Ndim : Pos;
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
1100 Assign : Node_Id;
1102 F_Or_L : Name_Id;
1103 S_Or_P : Name_Id;
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.
1110 ----------------
1111 -- Build_Step --
1112 ----------------
1114 function Build_Step (J : Nat) return Node_Id is
1115 Step : Node_Id;
1116 Lim : Name_Id;
1118 begin
1119 if Rev then
1120 Lim := Name_First;
1121 else
1122 Lim := Name_Last;
1123 end if;
1125 Step :=
1126 Make_Assignment_Statement (Loc,
1127 Name => New_Occurrence_Of (Rnn (J), Loc),
1128 Expression =>
1129 Make_Attribute_Reference (Loc,
1130 Prefix =>
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
1144 Step :=
1145 Make_If_Statement (Loc,
1146 Condition =>
1147 Make_Op_Ne (Loc,
1148 Left_Opnd => New_Occurrence_Of (Lnn (J), Loc),
1149 Right_Opnd =>
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));
1154 end if;
1156 return Step;
1157 end Build_Step;
1159 -- Start of processing for Expand_Assign_Array_Loop
1161 begin
1162 if Rev then
1163 F_Or_L := Name_Last;
1164 S_Or_P := Name_Pred;
1165 else
1166 F_Or_L := Name_First;
1167 S_Or_P := Name_Succ;
1168 end if;
1170 -- Setup index types and subscript entities
1172 declare
1173 L_Index : Node_Id;
1174 R_Index : Node_Id;
1176 begin
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);
1189 end loop;
1190 end;
1192 -- Now construct the assignment statement
1194 declare
1195 ExprL : constant List_Id := New_List;
1196 ExprR : constant List_Id := New_List;
1198 begin
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));
1202 end loop;
1204 Assign :=
1205 Make_Assignment_Statement (Loc,
1206 Name =>
1207 Make_Indexed_Component (Loc,
1208 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1209 Expressions => ExprL),
1210 Expression =>
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));
1225 end;
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
1232 Assign :=
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),
1239 Expression =>
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,
1248 Iteration_Scheme =>
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))))));
1258 end loop;
1260 return Assign;
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));
1272 begin
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
1276 -- concerned.
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
1284 -- be assigned.
1286 elsif Possible_Bit_Aligned_Component (Lhs)
1288 Possible_Bit_Aligned_Component (Rhs)
1289 then
1290 null;
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
1298 null;
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.
1303 else
1304 return;
1305 end if;
1307 -- At this stage we know that we must do a component wise assignment
1309 declare
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);
1313 RDef : Node_Id;
1314 F : Entity_Id;
1316 function Find_Component
1317 (Typ : Entity_Id;
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
1324 (CL : Node_Id;
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
1332 (C : Entity_Id;
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
1349 (Typ : Entity_Id;
1350 Comp : Entity_Id) return Entity_Id
1352 Utyp : constant Entity_Id := Underlying_Type (Typ);
1353 C : Entity_Id;
1355 begin
1356 C := First_Entity (Utyp);
1357 while Present (C) loop
1358 if Chars (C) = Chars (Comp) then
1359 return C;
1360 end if;
1362 Next_Entity (C);
1363 end loop;
1365 raise Program_Error;
1366 end Find_Component;
1368 --------------------------------
1369 -- Make_Component_List_Assign --
1370 --------------------------------
1372 function Make_Component_List_Assign
1373 (CL : Node_Id;
1374 U_U : Boolean := False) return List_Id
1376 CI : constant List_Id := Component_Items (CL);
1377 VP : constant Node_Id := Variant_Part (CL);
1379 Alts : List_Id;
1380 DC : Node_Id;
1381 DCH : List_Id;
1382 Expr : Node_Id;
1383 Result : List_Id;
1384 V : Node_Id;
1386 begin
1387 Result := Make_Field_Assigns (CI);
1389 if Present (VP) then
1390 V := First_Non_Pragma (Variants (VP));
1391 Alts := New_List;
1392 while Present (V) loop
1393 DCH := New_List;
1394 DC := First (Discrete_Choices (V));
1395 while Present (DC) loop
1396 Append_To (DCH, New_Copy_Tree (DC));
1397 Next (DC);
1398 end loop;
1400 Append_To (Alts,
1401 Make_Case_Statement_Alternative (Loc,
1402 Discrete_Choices => DCH,
1403 Statements =>
1404 Make_Component_List_Assign (Component_List (V))));
1405 Next_Non_Pragma (V);
1406 end loop;
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
1411 -- folded.
1413 if U_U then
1414 Expr :=
1415 New_Copy (Get_Discriminant_Value (
1416 Entity (Name (VP)),
1417 Etype (Rhs),
1418 Discriminant_Constraint (Etype (Rhs))));
1419 else
1420 Expr :=
1421 Make_Selected_Component (Loc,
1422 Prefix => Duplicate_Subexpr (Rhs),
1423 Selector_Name =>
1424 Make_Identifier (Loc, Chars (Name (VP))));
1425 end if;
1427 Append_To (Result,
1428 Make_Case_Statement (Loc,
1429 Expression => Expr,
1430 Alternatives => Alts));
1431 end if;
1433 return Result;
1434 end Make_Component_List_Assign;
1436 -----------------------
1437 -- Make_Field_Assign --
1438 -----------------------
1440 function Make_Field_Assign
1441 (C : Entity_Id;
1442 U_U : Boolean := False) return Node_Id
1444 A : Node_Id;
1445 Expr : Node_Id;
1447 begin
1448 -- In the case of an Unchecked_Union, use the discriminant
1449 -- constraint value as on the right hand side of the assignment.
1451 if U_U then
1452 Expr :=
1453 New_Copy (Get_Discriminant_Value (C,
1454 Etype (Rhs),
1455 Discriminant_Constraint (Etype (Rhs))));
1456 else
1457 Expr :=
1458 Make_Selected_Component (Loc,
1459 Prefix => Duplicate_Subexpr (Rhs),
1460 Selector_Name => New_Occurrence_Of (C, Loc));
1461 end if;
1463 A :=
1464 Make_Assignment_Statement (Loc,
1465 Name =>
1466 Make_Selected_Component (Loc,
1467 Prefix => Duplicate_Subexpr (Lhs),
1468 Selector_Name =>
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
1479 then
1480 Set_Componentwise_Assignment (A);
1481 end if;
1483 return A;
1484 end Make_Field_Assign;
1486 ------------------------
1487 -- Make_Field_Assigns --
1488 ------------------------
1490 function Make_Field_Assigns (CI : List_Id) return List_Id is
1491 Item : Node_Id;
1492 Result : List_Id;
1494 begin
1495 Item := First (CI);
1496 Result := New_List;
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))
1506 then
1507 Append_To
1508 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1509 end if;
1511 Next (Item);
1512 end loop;
1514 return Result;
1515 end Make_Field_Assigns;
1517 -- Start of processing for Expand_Assign_Record
1519 begin
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
1529 -- other cases.
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.
1539 declare
1540 CF : Entity_Id;
1542 begin
1543 if Inside_Init_Proc
1544 and then Present (Corresponding_Discriminant (F))
1545 then
1546 CF := Corresponding_Discriminant (F);
1547 else
1548 CF := F;
1549 end if;
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
1558 null;
1560 else
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));
1566 end if;
1568 else
1569 Insert_Action (N, Make_Field_Assign (CF));
1570 end if;
1572 Next_Discriminant (F);
1573 end;
1574 end loop;
1575 end if;
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))
1583 then
1584 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1585 else
1586 RDef := Type_Definition (Decl);
1587 end if;
1589 if Nkind (RDef) = N_Derived_Type_Definition then
1590 RDef := Record_Extension_Part (RDef);
1591 end if;
1593 if Nkind (RDef) = N_Record_Definition
1594 and then Present (Component_List (RDef))
1595 then
1596 if Is_Unchecked_Union (R_Typ) then
1597 Insert_Actions (N,
1598 Make_Component_List_Assign (Component_List (RDef), True));
1599 else
1600 Insert_Actions
1601 (N, Make_Component_List_Assign (Component_List (RDef)));
1602 end if;
1604 Rewrite (N, Make_Null_Statement (Loc));
1605 end if;
1606 end;
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));
1622 Exp : Node_Id;
1624 begin
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);
1634 return;
1635 end if;
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
1639 -- subscripts.
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
1647 then
1648 Check_Valid_Lvalue_Subscripts (Lhs);
1649 end if;
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
1665 declare
1666 Call : Node_Id;
1667 Conctyp : Entity_Id;
1668 Ent : Entity_Id;
1669 Subprg : Entity_Id;
1670 RT_Subprg_Name : Node_Id;
1672 begin
1673 -- Handle chains of renamings
1675 Ent := Name (N);
1676 while Nkind (Ent) in N_Has_Entity
1677 and then Present (Entity (Ent))
1678 and then Present (Renamed_Object (Entity (Ent)))
1679 loop
1680 Ent := Renamed_Object (Entity (Ent));
1681 end loop;
1683 -- The attribute Priority applied to protected objects has been
1684 -- previously expanded into a call to the Get_Ceiling run-time
1685 -- subprogram.
1687 if Nkind (Ent) = N_Function_Call
1688 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1689 or else
1690 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1691 then
1692 -- Look for the enclosing concurrent type
1694 Conctyp := Current_Scope;
1695 while not Is_Concurrent_Type (Conctyp) loop
1696 Conctyp := Scope (Conctyp);
1697 end loop;
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);
1706 end loop;
1708 -- Select the appropriate run-time call
1710 if Number_Entries (Conctyp) = 0 then
1711 RT_Subprg_Name :=
1712 New_Occurrence_Of (RTE (RE_Set_Ceiling), Loc);
1713 else
1714 RT_Subprg_Name :=
1715 New_Occurrence_Of (RTE (RO_PE_Set_Ceiling), Loc);
1716 end if;
1718 Call :=
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))));
1725 Rewrite (N, Call);
1726 Analyze (N);
1727 return;
1728 end if;
1729 end;
1730 end if;
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);
1740 end if;
1742 -- Then generate predicate check if required
1744 Apply_Predicate_Check (Rhs, Typ);
1745 end if;
1747 -- Check for a special case where a high level transformation is
1748 -- required. If we have either of:
1750 -- P.field := rhs;
1751 -- P (sub) := rhs;
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;
1768 -- P := Tnn;
1770 -- or
1772 -- Tnn : prefix_type := P;
1773 -- Tnn (subscr) := rhs;
1774 -- P := Tnn;
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))
1781 then
1782 declare
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);
1788 begin
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.
1797 Insert_After (N,
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.
1811 Exp := BPAR_Expr;
1812 loop
1813 Set_Analyzed (Exp, False);
1815 if Nkind_In
1816 (Exp, N_Selected_Component, N_Indexed_Component)
1817 then
1818 Exp := Prefix (Exp);
1819 else
1820 exit;
1821 end if;
1822 end loop;
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.
1832 declare
1833 Uses_Transient_Scope : constant Boolean :=
1834 Scope_Is_Transient
1835 and then N = Node_To_Be_Wrapped;
1837 begin
1838 if Uses_Transient_Scope then
1839 Push_Scope (Scope (Current_Scope));
1840 end if;
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
1849 Pop_Scope;
1850 end if;
1851 end;
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);
1864 end;
1865 end if;
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));
1875 Analyze (N);
1876 return;
1877 end if;
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))
1885 then
1886 -- Skip discriminant check if change of representation. Will be
1887 -- done when the change of representation is expanded out.
1889 if not Crep then
1890 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1891 end if;
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)
1906 then
1907 declare
1908 Lt : constant Entity_Id := Etype (Lhs);
1909 Ubt : Entity_Id := Base_Type (Typ);
1911 begin
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);
1925 end if;
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);
1931 end;
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)
1941 then
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)))
1951 then
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.
1957 if not Crep then
1958 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1959 end if;
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));
1966 end if;
1968 if Nkind (Rhs) = N_Allocator then
1969 declare
1970 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1971 C_Es : Check_Result;
1973 begin
1974 C_Es :=
1975 Get_Range_Checks
1976 (Lhs,
1977 Target_Typ,
1978 Etype (Designated_Type (Etype (Lhs))));
1980 Insert_Range_Checks
1981 (C_Es,
1983 Target_Typ,
1984 Sloc (Lhs),
1985 Lhs);
1986 end;
1987 end if;
1988 end if;
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
1995 then
1996 Analyze_And_Resolve (Expression (Rhs));
1997 Apply_Range_Check
1998 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1999 end if;
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)
2014 then
2015 Apply_Constraint_Check (Rhs, Etype (Lhs));
2016 end if;
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)))
2024 then
2025 declare
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.
2031 ----------------
2032 -- Lhs_Entity --
2033 ----------------
2035 function Lhs_Entity return Entity_Id is
2036 Result : Entity_Id := Entity (Lhs);
2038 begin
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));
2045 end loop;
2047 return Result;
2048 end Lhs_Entity;
2050 -- Local Declarations
2052 Access_Check : constant Node_Id :=
2053 Make_Raise_Program_Error (Loc,
2054 Condition =>
2055 Make_Op_Gt (Loc,
2056 Left_Opnd =>
2057 Dynamic_Accessibility_Level (Rhs),
2058 Right_Opnd =>
2059 Make_Integer_Literal (Loc,
2060 Intval =>
2061 Scope_Depth
2062 (Enclosing_Dynamic_Scope
2063 (Lhs_Entity)))),
2064 Reason => PE_Accessibility_Check_Failed);
2066 Access_Level_Update : constant Node_Id :=
2067 Make_Assignment_Statement (Loc,
2068 Name =>
2069 New_Occurrence_Of
2070 (Effective_Extra_Accessibility
2071 (Entity (Lhs)), Loc),
2072 Expression =>
2073 Dynamic_Accessibility_Level (Rhs));
2075 begin
2076 if not Accessibility_Checks_Suppressed (Entity (Lhs)) then
2077 Insert_Action (N, Access_Check);
2078 end if;
2080 Insert_Action (N, Access_Level_Update);
2081 end;
2082 end if;
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)))
2090 then
2091 -- Normal case, no change of representation
2093 if not Crep then
2094 Expand_Bit_Packed_Element_Set (N);
2095 return;
2097 -- Change of representation case
2099 else
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.
2104 -- T1 : Elmt_Type;
2105 -- T1 := RhS;
2106 -- Lhs := T1;
2108 declare
2109 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2110 Stats : List_Id;
2112 begin
2113 Stats :=
2114 New_List (
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));
2128 Analyze (N);
2129 end;
2130 end if;
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)
2138 then
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);
2146 return;
2148 elsif Is_Tagged_Type (Typ)
2149 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
2150 then
2151 Tagged_Case : declare
2152 L : List_Id := No_List;
2153 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
2155 begin
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
2165 Set_Analyzed (N);
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
2180 -- generated.
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
2186 and then
2187 not Restriction_Active (No_Dispatching_Calls))
2188 then
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
2195 -- versions.
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);
2201 return;
2202 end if;
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.
2208 declare
2209 Op : constant Entity_Id :=
2210 Find_Prim_Op (Typ, Name_uAssign);
2211 F_Typ : Entity_Id := Etype (First_Formal (Op));
2213 begin
2214 -- If the assignment is dispatching, make sure to use the
2215 -- proper type.
2217 if Is_Class_Wide_Type (Typ) then
2218 F_Typ := Class_Wide_Type (F_Typ);
2219 end if;
2221 L := New_List;
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)))
2231 then
2232 Append_To (L,
2233 Make_Raise_Constraint_Error (Loc,
2234 Condition =>
2235 Make_Op_Ne (Loc,
2236 Left_Opnd =>
2237 Make_Selected_Component (Loc,
2238 Prefix => Duplicate_Subexpr (Lhs),
2239 Selector_Name =>
2240 Make_Identifier (Loc, Name_uTag)),
2241 Right_Opnd =>
2242 Make_Selected_Component (Loc,
2243 Prefix => Duplicate_Subexpr (Rhs),
2244 Selector_Name =>
2245 Make_Identifier (Loc, Name_uTag))),
2246 Reason => CE_Tag_Check_Failed));
2247 end if;
2249 declare
2250 Left_N : Node_Id := Duplicate_Subexpr (Lhs);
2251 Right_N : Node_Id := Duplicate_Subexpr (Rhs);
2253 begin
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);
2259 end if;
2261 if Etype (Rhs) /= F_Typ then
2262 Right_N := Unchecked_Convert_To (F_Typ, Right_N);
2263 end if;
2265 Append_To (L,
2266 Make_Procedure_Call_Statement (Loc,
2267 Name => New_Occurrence_Of (Op, Loc),
2268 Parameter_Associations => New_List (
2269 Node1 => Left_N,
2270 Node2 => Right_N)));
2271 end;
2272 end;
2274 else
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>
2284 -- end if;
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)
2291 then
2292 L := New_List (
2293 Make_Implicit_If_Statement (N,
2294 Condition =>
2295 Make_Op_Ne (Loc,
2296 Left_Opnd =>
2297 Make_Attribute_Reference (Loc,
2298 Prefix => Duplicate_Subexpr (Lhs),
2299 Attribute_Name => Name_Address),
2301 Right_Opnd =>
2302 Make_Attribute_Reference (Loc,
2303 Prefix => Duplicate_Subexpr (Rhs),
2304 Attribute_Name => Name_Address)),
2306 Then_Statements => L));
2307 end if;
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
2312 -- s-finimp.adb).
2314 -- This is skipped if we have no finalization
2316 if Expand_Ctrl_Actions
2317 and then not Restriction_Active (No_Finalization)
2318 then
2319 L := New_List (
2320 Make_Block_Statement (Loc,
2321 Handled_Statement_Sequence =>
2322 Make_Handled_Sequence_Of_Statements (Loc,
2323 Statements => L,
2324 Exception_Handlers => New_List (
2325 Make_Handler_For_Ctrl_Operation (Loc)))));
2326 end if;
2327 end if;
2329 Rewrite (N,
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
2340 then
2341 declare
2342 Blk : constant Entity_Id :=
2343 New_Internal_Entity
2344 (E_Block, Current_Scope, Sloc (N), 'B');
2346 begin
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);
2356 end;
2357 end if;
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);
2364 return;
2365 end Tagged_Case;
2367 -- Array types
2369 elsif Is_Array_Type (Typ) then
2370 declare
2371 Actual_Rhs : Node_Id := Rhs;
2373 begin
2374 while Nkind_In (Actual_Rhs, N_Type_Conversion,
2375 N_Qualified_Expression)
2376 loop
2377 Actual_Rhs := Expression (Actual_Rhs);
2378 end loop;
2380 Expand_Assign_Array (N, Actual_Rhs);
2381 return;
2382 end;
2384 -- Record types
2386 elsif Is_Record_Type (Typ) then
2387 Expand_Assign_Record (N);
2388 return;
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
2392 -- scalar values.
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)
2422 then
2423 if Do_Range_Check (Rhs)
2424 or else Etype (Lhs) = Etype (Rhs)
2425 then
2426 Set_Is_Known_Valid (Entity (Lhs), True);
2427 end if;
2428 end if;
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.
2440 else
2441 -- Validate right side if we are validating copies
2443 if Validity_Checks_On
2444 and then Validity_Check_Copies
2445 then
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
2451 then
2452 null;
2453 else
2454 Ensure_Valid (Rhs);
2455 end if;
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)
2462 then
2463 Set_Is_Known_Valid (Entity (Lhs), True);
2464 end if;
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))
2483 then
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.
2487 Ensure_Valid (Rhs);
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.
2494 else
2495 null;
2496 end if;
2497 end if;
2498 end if;
2500 exception
2501 when RE_Not_Available =>
2502 return;
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
2512 begin
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);
2523 Alt : Node_Id;
2524 Len : Nat;
2525 Cond : Node_Id;
2526 Choice : Node_Id;
2527 Chlist : List_Id;
2529 begin
2530 -- Check for the situation where we know at compile time which branch
2531 -- will be taken
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);
2542 end if;
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));
2554 declare
2555 Dead_Alt : Node_Id;
2557 begin
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
2563 if Dead_Alt /= Alt
2564 and then Nkind (Dead_Alt) = N_Case_Statement_Alternative
2565 then
2566 Kill_Dead_Code (Statements (Dead_Alt), Warn_On_Deleted_Code);
2567 end if;
2569 Next (Dead_Alt);
2570 end loop;
2571 end;
2573 Rewrite (N, Make_Null_Statement (Loc));
2574 return;
2575 end if;
2577 -- Here if the choice is not determined at compile time
2579 declare
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;
2588 begin
2589 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2590 Others_Present := True;
2591 Others_Node := Last_Alt;
2592 else
2593 Others_Present := False;
2594 end if;
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);
2607 end if;
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));
2615 if Len = 1 then
2617 -- We still need to evaluate the expression if it has any side
2618 -- effects.
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);
2629 end if;
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));
2639 return;
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.
2652 elsif Len = 2 then
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
2665 then
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
2672 then
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)))
2684 then
2685 Cond :=
2686 Make_In (Loc,
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
2694 Cond :=
2695 Make_In (Loc,
2696 Left_Opnd => Expression (N),
2697 Right_Opnd =>
2698 Relocate_Node
2699 (Range_Expression (Constraint (Choice))));
2701 -- For any other subexpression "expression = value"
2703 else
2704 Cond :=
2705 Make_Op_Eq (Loc,
2706 Left_Opnd => Expression (N),
2707 Right_Opnd => Relocate_Node (Choice));
2708 end if;
2710 -- Now rewrite the case as an IF
2712 Rewrite (N,
2713 Make_If_Statement (Loc,
2714 Condition => Cond,
2715 Then_Statements => Then_Stms,
2716 Else_Statements => Else_Stms));
2717 Analyze (N);
2718 return;
2719 end if;
2720 end if;
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));
2737 end if;
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);
2751 end if;
2753 if Has_SP_Choice (Alt) then
2754 Expand_Static_Predicates_In_Choices (Alt);
2755 end if;
2757 Next_Non_Pragma (Alt);
2758 end loop;
2759 end;
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
2770 begin
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);
2786 Advance : Node_Id;
2787 Blk_Nod : Node_Id;
2788 Init : Node_Id;
2789 New_Loop : Node_Id;
2791 begin
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);
2800 -- end loop;
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.
2810 Blk_Nod :=
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);
2818 Analyze (N);
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;
2838 Elmt_Ref : Node_Id;
2840 Element_Op : constant Entity_Id :=
2841 Get_Iterable_Type_Primitive (Container_Typ, Name_Element);
2843 Advance : Node_Id;
2844 Init : Node_Id;
2845 New_Loop : Node_Id;
2847 begin
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);
2857 -- end loop;
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.
2867 Elmt_Decl :=
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);
2878 Elmt_Ref :=
2879 Make_Assignment_Statement (Loc,
2880 Name => New_Occurrence_Of (Element, Loc),
2881 Expression =>
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
2893 New_Loop :=
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
2905 -- the loop;
2907 Analyze (Elmt_Decl);
2908 Set_Ekind (Defining_Identifier (Elmt_Decl), E_Loop_Parameter);
2909 Set_Assignment_OK (Name (Elmt_Ref));
2911 Analyze (N);
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
2921 begin
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:
2938 -- if x then xs
2939 -- elsif y then ys
2940 -- ...
2941 -- end if;
2943 -- becomes
2945 -- if x then xs
2946 -- else
2947 -- <<condition actions of y>>
2948 -- if y then ys
2949 -- ...
2950 -- end if;
2951 -- end if;
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);
2962 Hed : Node_Id;
2963 E : Node_Id;
2964 New_If : Node_Id;
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.
2972 begin
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);
2979 end if;
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));
3001 Rewrite (N, Hed);
3002 return;
3004 -- If condition is False, then we can delete the condition and
3005 -- the Then statements
3007 else
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));
3015 end if;
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))
3025 then
3026 Rewrite (N,
3027 Make_Null_Statement (Sloc (N)));
3028 else
3029 Hed := Remove_Head (Else_Statements (N));
3030 Insert_List_After (N, Else_Statements (N));
3031 Rewrite (N, Hed);
3032 end if;
3034 return;
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.
3040 else
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);
3055 end if;
3056 end if;
3057 end if;
3058 end loop;
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);
3073 end if;
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))
3085 then
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).
3090 New_If :=
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);
3102 end if;
3104 Append (Remove_Next (E), Elsif_Parts (New_If));
3105 end loop;
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));
3111 end if;
3113 Remove (E);
3115 if Is_Empty_List (Elsif_Parts (N)) then
3116 Set_Elsif_Parts (N, No_List);
3117 end if;
3119 Analyze (New_If);
3120 return;
3122 -- No special processing for that elsif part, move to next
3124 else
3125 Next (E);
3126 end if;
3127 end loop;
3128 end if;
3130 -- Some more optimizations applicable if we still have an IF statement
3132 if Nkind (N) /= N_If_Statement then
3133 return;
3134 end if;
3136 -- Another optimization, special cases that can be simplified
3138 -- if expression then
3139 -- return true;
3140 -- else
3141 -- return false;
3142 -- end if;
3144 -- can be changed to:
3146 -- return expression;
3148 -- and
3150 -- if expression then
3151 -- return false;
3152 -- else
3153 -- return true;
3154 -- end if;
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
3165 then
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
3171 then
3172 declare
3173 Then_Stm : constant Node_Id := First (Then_Statements (N));
3174 Else_Stm : constant Node_Id := First (Else_Statements (N));
3176 begin
3177 if Nkind (Then_Stm) = N_Simple_Return_Statement
3178 and then
3179 Nkind (Else_Stm) = N_Simple_Return_Statement
3180 then
3181 declare
3182 Then_Expr : constant Node_Id := Expression (Then_Stm);
3183 Else_Expr : constant Node_Id := Expression (Else_Stm);
3185 begin
3186 if Nkind (Then_Expr) = N_Identifier
3187 and then
3188 Nkind (Else_Expr) = N_Identifier
3189 then
3190 if Entity (Then_Expr) = Standard_True
3191 and then Entity (Else_Expr) = Standard_False
3192 then
3193 Rewrite (N,
3194 Make_Simple_Return_Statement (Loc,
3195 Expression => Relocate_Node (Condition (N))));
3196 Analyze (N);
3197 return;
3199 elsif Entity (Then_Expr) = Standard_False
3200 and then Entity (Else_Expr) = Standard_True
3201 then
3202 Rewrite (N,
3203 Make_Simple_Return_Statement (Loc,
3204 Expression =>
3205 Make_Op_Not (Loc,
3206 Right_Opnd =>
3207 Relocate_Node (Condition (N)))));
3208 Analyze (N);
3209 return;
3210 end if;
3211 end if;
3212 end;
3213 end if;
3214 end;
3215 end if;
3216 end if;
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);
3232 Cursor : Entity_Id;
3233 Iterator : Entity_Id;
3234 New_Loop : Node_Id;
3235 Stats : List_Id := Statements (N);
3237 begin
3238 -- Processing for arrays
3240 if Is_Array_Type (Container_Typ) then
3241 Expand_Iterator_Loop_Over_Array (N);
3242 return;
3244 elsif Has_Aspect (Container_Typ, Aspect_Iterable) then
3245 if Of_Present (I_Spec) then
3246 Expand_Formal_Container_Element_Loop (N);
3247 else
3248 Expand_Formal_Container_Loop (N);
3249 end if;
3251 return;
3252 end if;
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
3271 -- declare
3272 -- -- The block is added when Element_Type is controlled
3274 -- Obj : Pack.Element_Type := Element (Cursor);
3275 -- -- for the "of" loop form
3276 -- begin
3277 -- <original loop statements>
3278 -- end;
3280 -- Cursor := Iter.Next (Cursor);
3281 -- end loop;
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.
3287 declare
3288 Element_Type : constant Entity_Id := Etype (Id);
3289 Iter_Type : Entity_Id;
3290 Pack : Entity_Id;
3291 Decl : Node_Id;
3292 Name_Init : Name_Id;
3293 Name_Step : Name_Id;
3295 begin
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));
3312 else
3313 Pack := Scope (Container_Typ);
3314 end if;
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
3326 Handle_Of : declare
3327 Default_Iter : Entity_Id;
3328 Container_Arg : Node_Id;
3329 Ent : Entity_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));
3346 Prim : Elmt_Id;
3347 Op : Entity_Id;
3349 begin
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)))
3361 then
3362 Container_Arg :=
3363 Make_Type_Conversion (Loc,
3364 Subtype_Mark =>
3365 New_Occurrence_Of
3366 (Etype (First_Formal (Iter)), Loc),
3367 Expression => Container_Arg);
3368 end if;
3370 return Iter;
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
3379 Op := Node (Prim);
3381 if Chars (Op) = Chars (Iter)
3382 and then DT_Position (Op) = DT_Position (Iter)
3383 then
3384 return Op;
3385 end if;
3387 Next_Elmt (Prim);
3388 end loop;
3390 -- default iterator must exist.
3392 pragma Assert (False);
3394 else -- not a derived type
3395 return Iter;
3396 end if;
3397 end Get_Default_Iterator;
3399 -- Start of processing for Handle_Of
3401 begin
3402 if Is_Class_Wide_Type (Container_Typ) then
3403 Default_Iter :=
3404 Get_Default_Iterator (Etype (Base_Type (Container_Typ)));
3406 else
3407 Default_Iter := Get_Default_Iterator (Etype (Container));
3408 end if;
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));
3443 exit;
3444 end if;
3445 Next_Entity (Ent);
3446 end loop;
3448 -- Generate:
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.
3454 Decl :=
3455 Make_Object_Renaming_Declaration (Loc,
3456 Defining_Identifier => Id,
3457 Subtype_Mark =>
3458 New_Occurrence_Of (Element_Type, Loc),
3459 Name =>
3460 Make_Indexed_Component (Loc,
3461 Prefix => Relocate_Node (Container_Arg),
3462 Expressions =>
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))
3475 then
3476 Set_Ekind (Id, E_Constant);
3477 end if;
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
3486 -- Generate:
3487 -- declare
3488 -- Id : Element_Type := Element (curosr);
3489 -- begin
3490 -- <original loop statements>
3491 -- end;
3493 Stats := New_List (
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
3502 else
3503 Prepend_To (Stats, Decl);
3504 end if;
3505 end Handle_Of;
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.
3511 else
3512 Cursor := Id;
3513 end if;
3515 Iterator := Make_Temporary (Loc, 'I');
3517 -- Determine the advancement and initialization steps for the
3518 -- cursor.
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;
3527 else
3528 Name_Init := Name_First;
3529 Name_Step := Name_Next;
3530 end if;
3532 -- For both iterator forms, add a call to the step operation to
3533 -- advance the cursor. Generate:
3535 -- Cursor := Iterator.Next (Cursor);
3537 -- or else
3539 -- Cursor := Next (Cursor);
3541 declare
3542 Rhs : Node_Id;
3544 begin
3545 Rhs :=
3546 Make_Function_Call (Loc,
3547 Name =>
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)));
3554 Append_To (Stats,
3555 Make_Assignment_Statement (Loc,
3556 Name => New_Occurrence_Of (Cursor, Loc),
3557 Expression => Rhs));
3558 Set_Assignment_OK (Name (Last (Stats)));
3559 end;
3561 -- Generate:
3562 -- while Iterator.Has_Element loop
3563 -- <Stats>
3564 -- end loop;
3566 -- Has_Element is the second actual in the iterator package
3568 New_Loop :=
3569 Make_Loop_Statement (Loc,
3570 Iteration_Scheme =>
3571 Make_Iteration_Scheme (Loc,
3572 Condition =>
3573 Make_Function_Call (Loc,
3574 Name =>
3575 New_Occurrence_Of (
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)));
3588 end if;
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 ???
3594 -- Generate:
3596 -- I : Iterator_Type renames Container;
3597 -- C : Cursor_Type := Container.[First | Last];
3599 Insert_Action (N,
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
3607 declare
3608 Decl : Node_Id;
3610 begin
3611 Decl :=
3612 Make_Object_Declaration (Loc,
3613 Defining_Identifier => Cursor,
3614 Object_Definition =>
3615 New_Occurrence_Of (Etype (Cursor), Loc),
3616 Expression =>
3617 Make_Selected_Component (Loc,
3618 Prefix => New_Occurrence_Of (Iterator, Loc),
3619 Selector_Name =>
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);
3633 end;
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));
3642 end if;
3643 end;
3645 Rewrite (N, New_Loop);
3646 Analyze (N);
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;
3663 Ind_Comp : Node_Id;
3664 Iterator : Entity_Id;
3666 -- Start of processing for Expand_Iterator_Loop_Over_Array
3668 begin
3669 -- for Element of Array loop
3671 -- This case requires an internally generated cursor to iterate over
3672 -- the array.
3674 if Of_Present (I_Spec) then
3675 Iterator := Make_Temporary (Loc, 'C');
3677 -- Generate:
3678 -- Element : Component_Type renames Array (Iterator);
3680 Ind_Comp :=
3681 Make_Indexed_Component (Loc,
3682 Prefix => Relocate_Node (Array_Node),
3683 Expressions => New_List (New_Occurrence_Of (Iterator, Loc)));
3685 Prepend_To (Stats,
3686 Make_Object_Renaming_Declaration (Loc,
3687 Defining_Identifier => Id,
3688 Subtype_Mark =>
3689 New_Occurrence_Of (Component_Type (Array_Typ), Loc),
3690 Name => Ind_Comp));
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
3696 -- too late. ???)
3698 Set_Debug_Info_Needed (Id);
3700 -- for Index in Array loop
3702 -- This case utilizes the already given iterator name
3704 else
3705 Iterator := Id;
3706 end if;
3708 -- Generate:
3710 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3711 -- Element : Component_Type renames Array (Iterator);
3712 -- <original loop statements>
3713 -- end loop;
3715 Core_Loop :=
3716 Make_Loop_Statement (Loc,
3717 Iteration_Scheme =>
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
3741 -- <core loop>
3742 -- end loop;
3744 Core_Loop :=
3745 Make_Loop_Statement (Loc,
3746 Iteration_Scheme =>
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));
3766 end loop;
3767 end if;
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)));
3774 end if;
3776 Rewrite (N, Core_Loop);
3777 Analyze (N);
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);
3795 Stmt : Node_Id;
3797 begin
3798 -- Delete null loop
3800 if Is_Null_Loop (N) then
3801 Rewrite (N, Make_Null_Statement (Loc));
3802 return;
3803 end if;
3805 -- Deal with condition for C/Fortran Boolean
3807 if Present (Scheme) then
3808 Adjust_Condition (Condition (Scheme));
3809 end if;
3811 -- Generate polling call
3813 if Is_Non_Empty_List (Statements (N)) then
3814 Generate_Poll_Call (First (Statements (N)));
3815 end if;
3817 -- Nothing more to do for plain loop with no iteration scheme
3819 if No (Scheme) then
3820 null;
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
3829 declare
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);
3835 Expr : Node_Id;
3836 Decls : List_Id;
3837 New_Id : Entity_Id;
3839 begin
3840 -- Deal with loop over predicates
3842 if Is_Discrete_Type (Ltype)
3843 and then Present (Predicate_Function (Ltype))
3844 then
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
3852 -- ...
3853 -- end loop;
3855 -- to
3857 -- for xP in [reverse] integer
3858 -- range etype'Pos (a) .. etype'Pos (b)
3859 -- loop
3860 -- declare
3861 -- x : constant etype := Pos_To_Rep (xP);
3862 -- begin
3863 -- ...
3864 -- end;
3865 -- end loop;
3867 elsif Is_Enumeration_Type (Btype)
3868 and then Present (Enum_Pos_To_Rep (Btype))
3869 then
3870 New_Id :=
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
3878 Expr :=
3879 Unchecked_Convert_To (Btype,
3880 Make_Op_Add (Loc,
3881 Left_Opnd =>
3882 Make_Integer_Literal (Loc,
3883 Enumeration_Rep (First_Literal (Btype))),
3884 Right_Opnd => New_Occurrence_Of (New_Id, Loc)));
3885 else
3886 -- Use the constructed array Enum_Pos_To_Rep
3888 Expr :=
3889 Make_Indexed_Component (Loc,
3890 Prefix =>
3891 New_Occurrence_Of (Enum_Pos_To_Rep (Btype), Loc),
3892 Expressions =>
3893 New_List (New_Occurrence_Of (New_Id, Loc)));
3894 end if;
3896 -- Build declaration for loop identifier
3898 Decls :=
3899 New_List (
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));
3906 Rewrite (N,
3907 Make_Loop_Statement (Loc,
3908 Identifier => Identifier (N),
3910 Iteration_Scheme =>
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,
3920 Subtype_Mark =>
3921 New_Occurrence_Of (Standard_Natural, Loc),
3923 Constraint =>
3924 Make_Range_Constraint (Loc,
3925 Range_Expression =>
3926 Make_Range (Loc,
3928 Low_Bound =>
3929 Make_Attribute_Reference (Loc,
3930 Prefix =>
3931 New_Occurrence_Of (Btype, Loc),
3933 Attribute_Name => Name_Pos,
3935 Expressions => New_List (
3936 Relocate_Node
3937 (Type_Low_Bound (Ltype)))),
3939 High_Bound =>
3940 Make_Attribute_Reference (Loc,
3941 Prefix =>
3942 New_Occurrence_Of (Btype, Loc),
3944 Attribute_Name => Name_Pos,
3946 Expressions => New_List (
3947 Relocate_Node
3948 (Type_High_Bound
3949 (Ltype))))))))),
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);
3972 null;
3973 end if;
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);
3980 end if;
3982 Analyze (N);
3984 -- Nothing to do with other cases of for loops
3986 else
3987 null;
3988 end if;
3989 end;
3991 -- Second case, if we have a while loop with Condition_Actions set, then
3992 -- we change it into a plain loop:
3994 -- while C loop
3995 -- ...
3996 -- end loop;
3998 -- changed to:
4000 -- loop
4001 -- <<condition actions>>
4002 -- exit when not C;
4003 -- ...
4004 -- end loop
4006 elsif Present (Scheme)
4007 and then Present (Condition_Actions (Scheme))
4008 and then Present (Condition (Scheme))
4009 then
4010 declare
4011 ES : Node_Id;
4013 begin
4014 ES :=
4015 Make_Exit_Statement (Sloc (Condition (Scheme)),
4016 Condition =>
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.
4028 Rewrite (N,
4029 Make_Loop_Statement (Sloc (N),
4030 Identifier => Identifier (N),
4031 Statements => Statements (N),
4032 End_Label => End_Label (N)));
4034 Analyze (N);
4035 end;
4037 -- Here to deal with iterator case
4039 elsif Present (Scheme)
4040 and then Present (Iterator_Specification (Scheme))
4041 then
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))
4053 then
4054 Qualify_Entity_Names (N);
4055 end if;
4056 end if;
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.
4063 Stmt := N;
4065 if Subject_To_Loop_Entry_Attributes (Stmt) then
4066 Stmt := Find_Loop_In_Conditional_Block (Stmt);
4067 end if;
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);
4090 begin
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.
4095 if No (Stat) then
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));
4104 Analyze (N);
4106 -- For expansion over a static predicate we generate the following
4108 -- declare
4109 -- J : Ltype := min-val;
4110 -- begin
4111 -- loop
4112 -- body
4113 -- case J is
4114 -- when endpoint => J := startpoint;
4115 -- when endpoint => J := startpoint;
4116 -- ...
4117 -- when max-val => exit;
4118 -- when others => J := Lval'Succ (J);
4119 -- end case;
4120 -- end loop;
4121 -- end;
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;
4128 -- ...
4129 -- for L in StaticP loop
4130 -- Put_Line ("static:" & J'Img);
4131 -- end loop;
4133 -- In this case, the loop is transformed into
4135 -- begin
4136 -- J : L := 3;
4137 -- loop
4138 -- body
4139 -- case J is
4140 -- when 3 => J := 5;
4141 -- when 7 => J := 10;
4142 -- when 10 => exit;
4143 -- when others => J := L'Succ (J);
4144 -- end case;
4145 -- end loop;
4146 -- end;
4148 else
4149 Static_Predicate : declare
4150 S : Node_Id;
4151 D : Node_Id;
4152 P : Node_Id;
4153 Alts : List_Id;
4154 Cstm : Node_Id;
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.
4164 ------------
4165 -- Hi_Val --
4166 ------------
4168 function Hi_Val (N : Node_Id) return Node_Id is
4169 begin
4170 if Is_OK_Static_Expression (N) then
4171 return New_Copy (N);
4172 else
4173 pragma Assert (Nkind (N) = N_Range);
4174 return New_Copy (High_Bound (N));
4175 end if;
4176 end Hi_Val;
4178 ------------
4179 -- Lo_Val --
4180 ------------
4182 function Lo_Val (N : Node_Id) return Node_Id is
4183 begin
4184 if Is_OK_Static_Expression (N) then
4185 return New_Copy (N);
4186 else
4187 pragma Assert (Nkind (N) = N_Range);
4188 return New_Copy (Low_Bound (N));
4189 end if;
4190 end Lo_Val;
4192 -- Start of processing for Static_Predicate
4194 begin
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
4212 Alts := New_List;
4213 P := First (Stat);
4214 while Present (P) loop
4215 if No (Next (P)) then
4216 S := Make_Exit_Statement (Loc);
4217 else
4218 S :=
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);
4223 end if;
4225 Append_To (Alts,
4226 Make_Case_Statement_Alternative (Loc,
4227 Statements => New_List (S),
4228 Discrete_Choices => New_List (Hi_Val (P))));
4230 Next (P);
4231 end loop;
4233 -- Add others choice
4235 S :=
4236 Make_Assignment_Statement (Loc,
4237 Name => New_Occurrence_Of (Loop_Id, Loc),
4238 Expression =>
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);
4246 Append_To (Alts,
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
4253 Cstm :=
4254 Make_Case_Statement (Loc,
4255 Expression => New_Occurrence_Of (Loop_Id, Loc),
4256 Alternatives => Alts);
4257 Append_To (Stmts, Cstm);
4259 -- Rewrite the loop
4261 D :=
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);
4268 Rewrite (N,
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)))));
4278 Analyze (N);
4279 end Static_Predicate;
4280 end if;
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;
4306 Tag_Id : Entity_Id;
4308 begin
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
4326 null;
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))))
4335 then
4336 null;
4338 else
4339 Append_To (Res,
4340 Make_Final_Call
4341 (Obj_Ref => Duplicate_Subexpr_No_Checks (L),
4342 Typ => Etype (L)));
4343 end if;
4345 -- Save the Tag in a local variable Tag_Id
4347 if Save_Tag then
4348 Tag_Id := Make_Temporary (Loc, 'A');
4350 Append_To (Res,
4351 Make_Object_Declaration (Loc,
4352 Defining_Identifier => Tag_Id,
4353 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4354 Expression =>
4355 Make_Selected_Component (Loc,
4356 Prefix => Duplicate_Subexpr_No_Checks (L),
4357 Selector_Name =>
4358 New_Occurrence_Of (First_Tag_Component (T), Loc))));
4360 -- Otherwise Tag_Id is not used
4362 else
4363 Tag_Id := Empty;
4364 end if;
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)
4371 then
4372 Prev_Id := Make_Temporary (Loc, 'P');
4373 Next_Id := Make_Temporary (Loc, 'N');
4375 -- Generate:
4376 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4378 Append_To (Res,
4379 Make_Object_Declaration (Loc,
4380 Defining_Identifier => Prev_Id,
4381 Object_Definition =>
4382 New_Occurrence_Of (RTE (RE_Root_Controlled_Ptr), Loc),
4383 Expression =>
4384 Make_Selected_Component (Loc,
4385 Prefix =>
4386 Unchecked_Convert_To
4387 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4388 Selector_Name =>
4389 Make_Identifier (Loc, Name_Prev))));
4391 -- Generate:
4392 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4394 Append_To (Res,
4395 Make_Object_Declaration (Loc,
4396 Defining_Identifier => Next_Id,
4397 Object_Definition =>
4398 New_Occurrence_Of (RTE (RE_Root_Controlled_Ptr), Loc),
4399 Expression =>
4400 Make_Selected_Component (Loc,
4401 Prefix =>
4402 Unchecked_Convert_To
4403 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4404 Selector_Name =>
4405 Make_Identifier (Loc, Name_Next))));
4406 end if;
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.
4413 if Comp_Asn then
4414 Set_Analyzed (Asn, False);
4415 Set_Componentwise_Assignment (Asn, True);
4416 end if;
4418 Append_To (Res, Asn);
4420 -- Restore the tag
4422 if Save_Tag then
4423 Append_To (Res,
4424 Make_Assignment_Statement (Loc,
4425 Name =>
4426 Make_Selected_Component (Loc,
4427 Prefix => Duplicate_Subexpr_No_Checks (L),
4428 Selector_Name =>
4429 New_Occurrence_Of (First_Tag_Component (T), Loc)),
4430 Expression => New_Occurrence_Of (Tag_Id, Loc)));
4431 end if;
4433 -- Restore the Prev and Next fields on .NET/JVM
4435 if VM_Target /= No_VM
4436 and then Is_Controlled (T)
4437 then
4438 -- Generate:
4439 -- Root_Controlled (L).Prev := Prev_Id;
4441 Append_To (Res,
4442 Make_Assignment_Statement (Loc,
4443 Name =>
4444 Make_Selected_Component (Loc,
4445 Prefix =>
4446 Unchecked_Convert_To
4447 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4448 Selector_Name =>
4449 Make_Identifier (Loc, Name_Prev)),
4450 Expression => New_Occurrence_Of (Prev_Id, Loc)));
4452 -- Generate:
4453 -- Root_Controlled (L).Next := Next_Id;
4455 Append_To (Res,
4456 Make_Assignment_Statement (Loc,
4457 Name =>
4458 Make_Selected_Component (Loc,
4459 Prefix =>
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)));
4464 end if;
4466 -- Adjust the target after the assignment when controlled (not in the
4467 -- init proc since it is an initialization more than an assignment).
4469 if Ctrl_Act then
4470 Append_To (Res,
4471 Make_Adjust_Call
4472 (Obj_Ref => Duplicate_Subexpr_Move_Checks (L),
4473 Typ => Etype (L)));
4474 end if;
4476 return Res;
4478 exception
4480 -- Could use comment here ???
4482 when RE_Not_Available =>
4483 return Empty_List;
4484 end Make_Tag_Ctrl_Assignment;
4486 end Exp_Ch5;