]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch5.adb
[Ada] Variable-sized node types
[thirdparty/gcc.git] / gcc / ada / exp_ch5.adb
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-2021, 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 ------------------------------------------------------------------------------
25
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 Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch11; use Exp_Ch11;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Inline; use Inline;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sinfo; use Sinfo;
51 with Sinfo.Nodes; use Sinfo.Nodes;
52 with Sinfo.Utils; use Sinfo.Utils;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Stringt; use Stringt;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
66 with Validsw; use Validsw;
67
68 package body Exp_Ch5 is
69
70 procedure Build_Formal_Container_Iteration
71 (N : Node_Id;
72 Container : Entity_Id;
73 Cursor : Entity_Id;
74 Init : out Node_Id;
75 Advance : out Node_Id;
76 New_Loop : out Node_Id);
77 -- Utility to create declarations and loop statement for both forms
78 -- of formal container iterators.
79
80 function Convert_To_Iterable_Type
81 (Container : Entity_Id;
82 Loc : Source_Ptr) return Node_Id;
83 -- Returns New_Occurrence_Of (Container), possibly converted to an ancestor
84 -- type, if the type of Container inherited the Iterable aspect from that
85 -- ancestor.
86
87 function Change_Of_Representation (N : Node_Id) return Boolean;
88 -- Determine if the right-hand side of assignment N is a type conversion
89 -- which requires a change of representation. Called only for the array
90 -- and record cases.
91
92 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
93 -- N is an assignment which assigns an array value. This routine process
94 -- the various special cases and checks required for such assignments,
95 -- including change of representation. Rhs is normally simply the right-
96 -- hand side of the assignment, except that if the right-hand side is a
97 -- type conversion or a qualified expression, then the RHS is the actual
98 -- expression inside any such type conversions or qualifications.
99
100 function Expand_Assign_Array_Loop
101 (N : Node_Id;
102 Larray : Entity_Id;
103 Rarray : Entity_Id;
104 L_Type : Entity_Id;
105 R_Type : Entity_Id;
106 Ndim : Pos;
107 Rev : Boolean) return Node_Id;
108 -- N is an assignment statement which assigns an array value. This routine
109 -- expands the assignment into a loop (or nested loops for the case of a
110 -- multi-dimensional array) to do the assignment component by component.
111 -- Larray and Rarray are the entities of the actual arrays on the left-hand
112 -- and right-hand sides. L_Type and R_Type are the types of these arrays
113 -- (which may not be the same, due to either sliding, or to a change of
114 -- representation case). Ndim is the number of dimensions and the parameter
115 -- Rev indicates if the loops run normally (Rev = False), or reversed
116 -- (Rev = True). The value returned is the constructed loop statement.
117 -- Auxiliary declarations are inserted before node N using the standard
118 -- Insert_Actions mechanism.
119
120 function Expand_Assign_Array_Bitfield
121 (N : Node_Id;
122 Larray : Entity_Id;
123 Rarray : Entity_Id;
124 L_Type : Entity_Id;
125 R_Type : Entity_Id;
126 Rev : Boolean) return Node_Id;
127 -- Alternative to Expand_Assign_Array_Loop for packed bitfields. Generates
128 -- a call to the System.Bitfields.Copy_Bitfield, which is more efficient
129 -- than copying component-by-component.
130
131 function Expand_Assign_Array_Loop_Or_Bitfield
132 (N : Node_Id;
133 Larray : Entity_Id;
134 Rarray : Entity_Id;
135 L_Type : Entity_Id;
136 R_Type : Entity_Id;
137 Ndim : Pos;
138 Rev : Boolean) return Node_Id;
139 -- Calls either Expand_Assign_Array_Loop or Expand_Assign_Array_Bitfield as
140 -- appropriate.
141
142 procedure Expand_Assign_Record (N : Node_Id);
143 -- N is an assignment of an untagged record value. This routine handles
144 -- the case where the assignment must be made component by component,
145 -- either because the target is not byte aligned, or there is a change
146 -- of representation, or when we have a tagged type with a representation
147 -- clause (this last case is required because holes in the tagged type
148 -- might be filled with components from child types).
149
150 procedure Expand_Assign_With_Target_Names (N : Node_Id);
151 -- (AI12-0125): N is an assignment statement whose RHS contains occurrences
152 -- of @ that designate the value of the LHS of the assignment. If the LHS
153 -- is side-effect free the target names can be replaced with a copy of the
154 -- LHS; otherwise the semantics of the assignment is described in terms of
155 -- a procedure with an in-out parameter, and expanded as such.
156
157 procedure Expand_Formal_Container_Loop (N : Node_Id);
158 -- Use the primitives specified in an Iterable aspect to expand a loop
159 -- over a so-called formal container, primarily for SPARK usage.
160
161 procedure Expand_Formal_Container_Element_Loop (N : Node_Id);
162 -- Same, for an iterator of the form " For E of C". In this case the
163 -- iterator provides the name of the element, and the cursor is generated
164 -- internally.
165
166 procedure Expand_Iterator_Loop (N : Node_Id);
167 -- Expand loop over arrays and containers that uses the form "for X of C"
168 -- with an optional subtype mark, or "for Y in C".
169
170 procedure Expand_Iterator_Loop_Over_Container
171 (N : Node_Id;
172 Isc : Node_Id;
173 I_Spec : Node_Id;
174 Container : Node_Id;
175 Container_Typ : Entity_Id);
176 -- Expand loop over containers that uses the form "for X of C" with an
177 -- optional subtype mark, or "for Y in C". Isc is the iteration scheme.
178 -- I_Spec is the iterator specification and Container is either the
179 -- Container (for OF) or the iterator (for IN).
180
181 procedure Expand_Predicated_Loop (N : Node_Id);
182 -- Expand for loop over predicated subtype
183
184 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
185 -- Generate the necessary code for controlled and tagged assignment, that
186 -- is to say, finalization of the target before, adjustment of the target
187 -- after and save and restore of the tag and finalization pointers which
188 -- are not 'part of the value' and must not be changed upon assignment. N
189 -- is the original Assignment node.
190
191 --------------------------------------
192 -- Build_Formal_Container_Iteration --
193 --------------------------------------
194
195 procedure Build_Formal_Container_Iteration
196 (N : Node_Id;
197 Container : Entity_Id;
198 Cursor : Entity_Id;
199 Init : out Node_Id;
200 Advance : out Node_Id;
201 New_Loop : out Node_Id)
202 is
203 Loc : constant Source_Ptr := Sloc (N);
204 Stats : constant List_Id := Statements (N);
205 Typ : constant Entity_Id := Base_Type (Etype (Container));
206
207 Has_Element_Op : constant Entity_Id :=
208 Get_Iterable_Type_Primitive (Typ, Name_Has_Element);
209
210 First_Op : Entity_Id;
211 Next_Op : Entity_Id;
212
213 begin
214 -- Use the proper set of primitives depending on the direction of
215 -- iteration. The legality of a reverse iteration has been checked
216 -- during analysis.
217
218 if Reverse_Present (Iterator_Specification (Iteration_Scheme (N))) then
219 First_Op := Get_Iterable_Type_Primitive (Typ, Name_Last);
220 Next_Op := Get_Iterable_Type_Primitive (Typ, Name_Previous);
221
222 else
223 First_Op := Get_Iterable_Type_Primitive (Typ, Name_First);
224 Next_Op := Get_Iterable_Type_Primitive (Typ, Name_Next);
225 end if;
226
227 -- Declaration for Cursor
228
229 Init :=
230 Make_Object_Declaration (Loc,
231 Defining_Identifier => Cursor,
232 Object_Definition => New_Occurrence_Of (Etype (First_Op), Loc),
233 Expression =>
234 Make_Function_Call (Loc,
235 Name => New_Occurrence_Of (First_Op, Loc),
236 Parameter_Associations => New_List (
237 Convert_To_Iterable_Type (Container, Loc))));
238
239 -- Statement that advances (in the right direction) cursor in loop
240
241 Advance :=
242 Make_Assignment_Statement (Loc,
243 Name => New_Occurrence_Of (Cursor, Loc),
244 Expression =>
245 Make_Function_Call (Loc,
246 Name => New_Occurrence_Of (Next_Op, Loc),
247 Parameter_Associations => New_List (
248 Convert_To_Iterable_Type (Container, Loc),
249 New_Occurrence_Of (Cursor, Loc))));
250
251 -- Iterator is rewritten as a while_loop
252
253 New_Loop :=
254 Make_Loop_Statement (Loc,
255 Iteration_Scheme =>
256 Make_Iteration_Scheme (Loc,
257 Condition =>
258 Make_Function_Call (Loc,
259 Name => New_Occurrence_Of (Has_Element_Op, Loc),
260 Parameter_Associations => New_List (
261 Convert_To_Iterable_Type (Container, Loc),
262 New_Occurrence_Of (Cursor, Loc)))),
263 Statements => Stats,
264 End_Label => Empty);
265
266 -- If the contruct has a specified loop name, preserve it in the new
267 -- loop, for possible use in exit statements.
268
269 if Present (Identifier (N))
270 and then Comes_From_Source (Identifier (N))
271 then
272 Set_Identifier (New_Loop, Identifier (N));
273 end if;
274 end Build_Formal_Container_Iteration;
275
276 ------------------------------
277 -- Change_Of_Representation --
278 ------------------------------
279
280 function Change_Of_Representation (N : Node_Id) return Boolean is
281 Rhs : constant Node_Id := Expression (N);
282 begin
283 return
284 Nkind (Rhs) = N_Type_Conversion
285 and then not Has_Compatible_Representation
286 (Target_Type => Etype (Rhs),
287 Operand_Type => Etype (Expression (Rhs)));
288 end Change_Of_Representation;
289
290 ------------------------------
291 -- Convert_To_Iterable_Type --
292 ------------------------------
293
294 function Convert_To_Iterable_Type
295 (Container : Entity_Id;
296 Loc : Source_Ptr) return Node_Id
297 is
298 Typ : constant Entity_Id := Base_Type (Etype (Container));
299 Aspect : constant Node_Id := Find_Aspect (Typ, Aspect_Iterable);
300 Result : Node_Id;
301
302 begin
303 Result := New_Occurrence_Of (Container, Loc);
304
305 if Entity (Aspect) /= Typ then
306 Result :=
307 Make_Type_Conversion (Loc,
308 Subtype_Mark => New_Occurrence_Of (Entity (Aspect), Loc),
309 Expression => Result);
310 end if;
311
312 return Result;
313 end Convert_To_Iterable_Type;
314
315 -------------------------
316 -- Expand_Assign_Array --
317 -------------------------
318
319 -- There are two issues here. First, do we let Gigi do a block move, or
320 -- do we expand out into a loop? Second, we need to set the two flags
321 -- Forwards_OK and Backwards_OK which show whether the block move (or
322 -- corresponding loops) can be legitimately done in a forwards (low to
323 -- high) or backwards (high to low) manner.
324
325 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
326 Loc : constant Source_Ptr := Sloc (N);
327
328 Lhs : constant Node_Id := Name (N);
329
330 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
331 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
332
333 L_Type : constant Entity_Id :=
334 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
335 R_Type : Entity_Id :=
336 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
337
338 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
339 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
340
341 Crep : constant Boolean := Change_Of_Representation (N);
342
343 pragma Assert
344 (Crep
345 or else Is_Bit_Packed_Array (L_Type) = Is_Bit_Packed_Array (R_Type));
346
347 Larray : Node_Id;
348 Rarray : Node_Id;
349
350 Ndim : constant Pos := Number_Dimensions (L_Type);
351
352 Loop_Required : Boolean := False;
353 -- This switch is set to True if the array move must be done using
354 -- an explicit front end generated loop.
355
356 procedure Apply_Dereference (Arg : Node_Id);
357 -- If the argument is an access to an array, and the assignment is
358 -- converted into a procedure call, apply explicit dereference.
359
360 function Has_Address_Clause (Exp : Node_Id) return Boolean;
361 -- Test if Exp is a reference to an array whose declaration has
362 -- an address clause, or it is a slice of such an array.
363
364 function Is_Formal_Array (Exp : Node_Id) return Boolean;
365 -- Test if Exp is a reference to an array which is either a formal
366 -- parameter or a slice of a formal parameter. These are the cases
367 -- where hidden aliasing can occur.
368
369 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
370 -- Determine if Exp is a reference to an array variable which is other
371 -- than an object defined in the current scope, or a component or a
372 -- slice of such an object. Such objects can be aliased to parameters
373 -- (unlike local array references).
374
375 -----------------------
376 -- Apply_Dereference --
377 -----------------------
378
379 procedure Apply_Dereference (Arg : Node_Id) is
380 Typ : constant Entity_Id := Etype (Arg);
381 begin
382 if Is_Access_Type (Typ) then
383 Rewrite (Arg, Make_Explicit_Dereference (Loc,
384 Prefix => Relocate_Node (Arg)));
385 Analyze_And_Resolve (Arg, Designated_Type (Typ));
386 end if;
387 end Apply_Dereference;
388
389 ------------------------
390 -- Has_Address_Clause --
391 ------------------------
392
393 function Has_Address_Clause (Exp : Node_Id) return Boolean is
394 begin
395 return
396 (Is_Entity_Name (Exp) and then
397 Present (Address_Clause (Entity (Exp))))
398 or else
399 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
400 end Has_Address_Clause;
401
402 ---------------------
403 -- Is_Formal_Array --
404 ---------------------
405
406 function Is_Formal_Array (Exp : Node_Id) return Boolean is
407 begin
408 return
409 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
410 or else
411 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
412 end Is_Formal_Array;
413
414 ------------------------
415 -- Is_Non_Local_Array --
416 ------------------------
417
418 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
419 begin
420 case Nkind (Exp) is
421 when N_Indexed_Component
422 | N_Selected_Component
423 | N_Slice
424 =>
425 return Is_Non_Local_Array (Prefix (Exp));
426
427 when others =>
428 return
429 not (Is_Entity_Name (Exp)
430 and then Scope (Entity (Exp)) = Current_Scope);
431 end case;
432 end Is_Non_Local_Array;
433
434 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
435
436 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
437 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
438
439 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
440 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
441
442 -- Start of processing for Expand_Assign_Array
443
444 begin
445 -- Deal with length check. Note that the length check is done with
446 -- respect to the right-hand side as given, not a possible underlying
447 -- renamed object, since this would generate incorrect extra checks.
448
449 Apply_Length_Check_On_Assignment (Rhs, L_Type, Lhs);
450
451 -- We start by assuming that the move can be done in either direction,
452 -- i.e. that the two sides are completely disjoint.
453
454 Set_Forwards_OK (N, True);
455 Set_Backwards_OK (N, True);
456
457 -- Normally it is only the slice case that can lead to overlap, and
458 -- explicit checks for slices are made below. But there is one case
459 -- where the slice can be implicit and invisible to us: when we have a
460 -- one dimensional array, and either both operands are parameters, or
461 -- one is a parameter (which can be a slice passed by reference) and the
462 -- other is a non-local variable. In this case the parameter could be a
463 -- slice that overlaps with the other operand.
464
465 -- However, if the array subtype is a constrained first subtype in the
466 -- parameter case, then we don't have to worry about overlap, since
467 -- slice assignments aren't possible (other than for a slice denoting
468 -- the whole array).
469
470 -- Note: No overlap is possible if there is a change of representation,
471 -- so we can exclude this case.
472
473 if Ndim = 1
474 and then not Crep
475 and then
476 ((Lhs_Formal and Rhs_Formal)
477 or else
478 (Lhs_Formal and Rhs_Non_Local_Var)
479 or else
480 (Rhs_Formal and Lhs_Non_Local_Var))
481 and then
482 (not Is_Constrained (Etype (Lhs))
483 or else not Is_First_Subtype (Etype (Lhs)))
484 then
485 Set_Forwards_OK (N, False);
486 Set_Backwards_OK (N, False);
487
488 -- Note: the bit-packed case is not worrisome here, since if we have
489 -- a slice passed as a parameter, it is always aligned on a byte
490 -- boundary, and if there are no explicit slices, the assignment
491 -- can be performed directly.
492 end if;
493
494 -- If either operand has an address clause clear Backwards_OK and
495 -- Forwards_OK, since we cannot tell if the operands overlap. We
496 -- exclude this treatment when Rhs is an aggregate, since we know
497 -- that overlap can't occur.
498
499 if (Has_Address_Clause (Lhs) and then Nkind (Rhs) /= N_Aggregate)
500 or else Has_Address_Clause (Rhs)
501 then
502 Set_Forwards_OK (N, False);
503 Set_Backwards_OK (N, False);
504 end if;
505
506 -- We certainly must use a loop for change of representation and also
507 -- we use the operand of the conversion on the right-hand side as the
508 -- effective right-hand side (the component types must match in this
509 -- situation).
510
511 if Crep then
512 Act_Rhs := Get_Referenced_Object (Rhs);
513 R_Type := Get_Actual_Subtype (Act_Rhs);
514 Loop_Required := True;
515
516 -- We require a loop if the left side is possibly bit unaligned
517
518 elsif Possible_Bit_Aligned_Component (Lhs)
519 or else
520 Possible_Bit_Aligned_Component (Rhs)
521 then
522 Loop_Required := True;
523
524 -- Arrays with controlled components are expanded into a loop to force
525 -- calls to Adjust at the component level.
526
527 elsif Has_Controlled_Component (L_Type) then
528 Loop_Required := True;
529
530 -- If object is full access, we cannot tolerate a loop
531
532 elsif Is_Full_Access_Object (Act_Lhs)
533 or else
534 Is_Full_Access_Object (Act_Rhs)
535 then
536 return;
537
538 -- Loop is required if we have atomic components since we have to
539 -- be sure to do any accesses on an element by element basis.
540
541 elsif Has_Atomic_Components (L_Type)
542 or else Has_Atomic_Components (R_Type)
543 or else Is_Full_Access (Component_Type (L_Type))
544 or else Is_Full_Access (Component_Type (R_Type))
545 then
546 Loop_Required := True;
547
548 -- Case where no slice is involved
549
550 elsif not L_Slice and not R_Slice then
551
552 -- The following code deals with the case of unconstrained bit packed
553 -- arrays. The problem is that the template for such arrays contains
554 -- the bounds of the actual source level array, but the copy of an
555 -- entire array requires the bounds of the underlying array. It would
556 -- be nice if the back end could take care of this, but right now it
557 -- does not know how, so if we have such a type, then we expand out
558 -- into a loop, which is inefficient but works correctly. If we don't
559 -- do this, we get the wrong length computed for the array to be
560 -- moved. The two cases we need to worry about are:
561
562 -- Explicit dereference of an unconstrained packed array type as in
563 -- the following example:
564
565 -- procedure C52 is
566 -- type BITS is array(INTEGER range <>) of BOOLEAN;
567 -- pragma PACK(BITS);
568 -- type A is access BITS;
569 -- P1,P2 : A;
570 -- begin
571 -- P1 := new BITS (1 .. 65_535);
572 -- P2 := new BITS (1 .. 65_535);
573 -- P2.ALL := P1.ALL;
574 -- end C52;
575
576 -- A formal parameter reference with an unconstrained bit array type
577 -- is the other case we need to worry about (here we assume the same
578 -- BITS type declared above):
579
580 -- procedure Write_All (File : out BITS; Contents : BITS);
581 -- begin
582 -- File.Storage := Contents;
583 -- end Write_All;
584
585 -- We expand to a loop in either of these two cases
586
587 -- Question for future thought. Another potentially more efficient
588 -- approach would be to create the actual subtype, and then do an
589 -- unchecked conversion to this actual subtype ???
590
591 Check_Unconstrained_Bit_Packed_Array : declare
592
593 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
594 -- Function to perform required test for the first case, above
595 -- (dereference of an unconstrained bit packed array).
596
597 -----------------------
598 -- Is_UBPA_Reference --
599 -----------------------
600
601 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
602 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
603 P_Type : Entity_Id;
604 Des_Type : Entity_Id;
605
606 begin
607 if Present (Packed_Array_Impl_Type (Typ))
608 and then Is_Array_Type (Packed_Array_Impl_Type (Typ))
609 and then not Is_Constrained (Packed_Array_Impl_Type (Typ))
610 then
611 return True;
612
613 elsif Nkind (Opnd) = N_Explicit_Dereference then
614 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
615
616 if not Is_Access_Type (P_Type) then
617 return False;
618
619 else
620 Des_Type := Designated_Type (P_Type);
621 return
622 Is_Bit_Packed_Array (Des_Type)
623 and then not Is_Constrained (Des_Type);
624 end if;
625
626 else
627 return False;
628 end if;
629 end Is_UBPA_Reference;
630
631 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
632
633 begin
634 if Is_UBPA_Reference (Lhs)
635 or else
636 Is_UBPA_Reference (Rhs)
637 then
638 Loop_Required := True;
639
640 -- Here if we do not have the case of a reference to a bit packed
641 -- unconstrained array case. In this case gigi can most certainly
642 -- handle the assignment if a forwards move is allowed.
643
644 -- (could it handle the backwards case also???)
645
646 elsif Forwards_OK (N) then
647 return;
648 end if;
649 end Check_Unconstrained_Bit_Packed_Array;
650
651 -- The back end can always handle the assignment if the right side is a
652 -- string literal (note that overlap is definitely impossible in this
653 -- case). If the type is packed, a string literal is always converted
654 -- into an aggregate, except in the case of a null slice, for which no
655 -- aggregate can be written. In that case, rewrite the assignment as a
656 -- null statement, a length check has already been emitted to verify
657 -- that the range of the left-hand side is empty.
658
659 -- Note that this code is not executed if we have an assignment of a
660 -- string literal to a non-bit aligned component of a record, a case
661 -- which cannot be handled by the backend.
662
663 elsif Nkind (Rhs) = N_String_Literal then
664 if String_Length (Strval (Rhs)) = 0
665 and then Is_Bit_Packed_Array (L_Type)
666 then
667 Rewrite (N, Make_Null_Statement (Loc));
668 Analyze (N);
669 end if;
670
671 return;
672
673 -- If either operand is bit packed, then we need a loop, since we can't
674 -- be sure that the slice is byte aligned. Similarly, if either operand
675 -- is a possibly unaligned slice, then we need a loop (since the back
676 -- end cannot handle unaligned slices).
677
678 elsif Is_Bit_Packed_Array (L_Type)
679 or else Is_Bit_Packed_Array (R_Type)
680 or else Is_Possibly_Unaligned_Slice (Lhs)
681 or else Is_Possibly_Unaligned_Slice (Rhs)
682 then
683 Loop_Required := True;
684
685 -- If we are not bit-packed, and we have only one slice, then no overlap
686 -- is possible except in the parameter case, so we can let the back end
687 -- handle things.
688
689 elsif not (L_Slice and R_Slice) then
690 if Forwards_OK (N) then
691 return;
692 end if;
693 end if;
694
695 -- If the right-hand side is a string literal, introduce a temporary for
696 -- it, for use in the generated loop that will follow.
697
698 if Nkind (Rhs) = N_String_Literal then
699 declare
700 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Rhs);
701 Decl : Node_Id;
702
703 begin
704 Decl :=
705 Make_Object_Declaration (Loc,
706 Defining_Identifier => Temp,
707 Object_Definition => New_Occurrence_Of (L_Type, Loc),
708 Expression => Relocate_Node (Rhs));
709
710 Insert_Action (N, Decl);
711 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
712 R_Type := Etype (Temp);
713 end;
714 end if;
715
716 -- Come here to complete the analysis
717
718 -- Loop_Required: Set to True if we know that a loop is required
719 -- regardless of overlap considerations.
720
721 -- Forwards_OK: Set to False if we already know that a forwards
722 -- move is not safe, else set to True.
723
724 -- Backwards_OK: Set to False if we already know that a backwards
725 -- move is not safe, else set to True
726
727 -- Our task at this stage is to complete the overlap analysis, which can
728 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
729 -- then generating the final code, either by deciding that it is OK
730 -- after all to let Gigi handle it, or by generating appropriate code
731 -- in the front end.
732
733 declare
734 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
735 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
736
737 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
738 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
739 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
740 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
741
742 Act_L_Array : Node_Id;
743 Act_R_Array : Node_Id;
744
745 Cleft_Lo : Node_Id;
746 Cright_Lo : Node_Id;
747 Condition : Node_Id;
748
749 Cresult : Compare_Result;
750
751 begin
752 -- Get the expressions for the arrays. If we are dealing with a
753 -- private type, then convert to the underlying type. We can do
754 -- direct assignments to an array that is a private type, but we
755 -- cannot assign to elements of the array without this extra
756 -- unchecked conversion.
757
758 -- Note: We propagate Parent to the conversion nodes to generate
759 -- a well-formed subtree.
760
761 if Nkind (Act_Lhs) = N_Slice then
762 Larray := Prefix (Act_Lhs);
763 else
764 Larray := Act_Lhs;
765
766 if Is_Private_Type (Etype (Larray)) then
767 declare
768 Par : constant Node_Id := Parent (Larray);
769 begin
770 Larray :=
771 Unchecked_Convert_To
772 (Underlying_Type (Etype (Larray)), Larray);
773 Set_Parent (Larray, Par);
774 end;
775 end if;
776 end if;
777
778 if Nkind (Act_Rhs) = N_Slice then
779 Rarray := Prefix (Act_Rhs);
780 else
781 Rarray := Act_Rhs;
782
783 if Is_Private_Type (Etype (Rarray)) then
784 declare
785 Par : constant Node_Id := Parent (Rarray);
786 begin
787 Rarray :=
788 Unchecked_Convert_To
789 (Underlying_Type (Etype (Rarray)), Rarray);
790 Set_Parent (Rarray, Par);
791 end;
792 end if;
793 end if;
794
795 -- If both sides are slices, we must figure out whether it is safe
796 -- to do the move in one direction or the other. It is always safe
797 -- if there is a change of representation since obviously two arrays
798 -- with different representations cannot possibly overlap.
799
800 if (not Crep) and L_Slice and R_Slice then
801 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
802 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
803
804 -- If both left- and right-hand arrays are entity names, and refer
805 -- to different entities, then we know that the move is safe (the
806 -- two storage areas are completely disjoint).
807
808 if Is_Entity_Name (Act_L_Array)
809 and then Is_Entity_Name (Act_R_Array)
810 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
811 then
812 null;
813
814 -- Otherwise, we assume the worst, which is that the two arrays
815 -- are the same array. There is no need to check if we know that
816 -- is the case, because if we don't know it, we still have to
817 -- assume it.
818
819 -- Generally if the same array is involved, then we have an
820 -- overlapping case. We will have to really assume the worst (i.e.
821 -- set neither of the OK flags) unless we can determine the lower
822 -- or upper bounds at compile time and compare them.
823
824 else
825 Cresult :=
826 Compile_Time_Compare
827 (Left_Lo, Right_Lo, Assume_Valid => True);
828
829 if Cresult = Unknown then
830 Cresult :=
831 Compile_Time_Compare
832 (Left_Hi, Right_Hi, Assume_Valid => True);
833 end if;
834
835 case Cresult is
836 when EQ | LE | LT =>
837 Set_Backwards_OK (N, False);
838
839 when GE | GT =>
840 Set_Forwards_OK (N, False);
841
842 when NE | Unknown =>
843 Set_Backwards_OK (N, False);
844 Set_Forwards_OK (N, False);
845 end case;
846 end if;
847 end if;
848
849 -- If after that analysis Loop_Required is False, meaning that we
850 -- have not discovered some non-overlap reason for requiring a loop,
851 -- then the outcome depends on the capabilities of the back end.
852
853 if not Loop_Required then
854 -- Assume the back end can deal with all cases of overlap by
855 -- falling back to memmove if it cannot use a more efficient
856 -- approach.
857
858 return;
859 end if;
860
861 -- At this stage we have to generate an explicit loop, and we have
862 -- the following cases:
863
864 -- Forwards_OK = True
865
866 -- Rnn : right_index := right_index'First;
867 -- for Lnn in left-index loop
868 -- left (Lnn) := right (Rnn);
869 -- Rnn := right_index'Succ (Rnn);
870 -- end loop;
871
872 -- Note: the above code MUST be analyzed with checks off, because
873 -- otherwise the Succ could overflow. But in any case this is more
874 -- efficient.
875
876 -- Forwards_OK = False, Backwards_OK = True
877
878 -- Rnn : right_index := right_index'Last;
879 -- for Lnn in reverse left-index loop
880 -- left (Lnn) := right (Rnn);
881 -- Rnn := right_index'Pred (Rnn);
882 -- end loop;
883
884 -- Note: the above code MUST be analyzed with checks off, because
885 -- otherwise the Pred could overflow. But in any case this is more
886 -- efficient.
887
888 -- Forwards_OK = Backwards_OK = False
889
890 -- This only happens if we have the same array on each side. It is
891 -- possible to create situations using overlays that violate this,
892 -- but we simply do not promise to get this "right" in this case.
893
894 -- There are two possible subcases. If the No_Implicit_Conditionals
895 -- restriction is set, then we generate the following code:
896
897 -- declare
898 -- T : constant <operand-type> := rhs;
899 -- begin
900 -- lhs := T;
901 -- end;
902
903 -- If implicit conditionals are permitted, then we generate:
904
905 -- if Left_Lo <= Right_Lo then
906 -- <code for Forwards_OK = True above>
907 -- else
908 -- <code for Backwards_OK = True above>
909 -- end if;
910
911 -- In order to detect possible aliasing, we examine the renamed
912 -- expression when the source or target is a renaming. However,
913 -- the renaming may be intended to capture an address that may be
914 -- affected by subsequent code, and therefore we must recover
915 -- the actual entity for the expansion that follows, not the
916 -- object it renames. In particular, if source or target designate
917 -- a portion of a dynamically allocated object, the pointer to it
918 -- may be reassigned but the renaming preserves the proper location.
919
920 if Is_Entity_Name (Rhs)
921 and then
922 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
923 and then Nkind (Act_Rhs) = N_Slice
924 then
925 Rarray := Rhs;
926 end if;
927
928 if Is_Entity_Name (Lhs)
929 and then
930 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
931 and then Nkind (Act_Lhs) = N_Slice
932 then
933 Larray := Lhs;
934 end if;
935
936 -- Cases where either Forwards_OK or Backwards_OK is true
937
938 if Forwards_OK (N) or else Backwards_OK (N) then
939 if Needs_Finalization (Component_Type (L_Type))
940 and then Base_Type (L_Type) = Base_Type (R_Type)
941 and then Ndim = 1
942 and then not No_Ctrl_Actions (N)
943 then
944 declare
945 Proc : constant Entity_Id :=
946 TSS (Base_Type (L_Type), TSS_Slice_Assign);
947 Actuals : List_Id;
948
949 begin
950 Apply_Dereference (Larray);
951 Apply_Dereference (Rarray);
952 Actuals := New_List (
953 Duplicate_Subexpr (Larray, Name_Req => True),
954 Duplicate_Subexpr (Rarray, Name_Req => True),
955 Duplicate_Subexpr (Left_Lo, Name_Req => True),
956 Duplicate_Subexpr (Left_Hi, Name_Req => True),
957 Duplicate_Subexpr (Right_Lo, Name_Req => True),
958 Duplicate_Subexpr (Right_Hi, Name_Req => True));
959
960 Append_To (Actuals,
961 New_Occurrence_Of (
962 Boolean_Literals (not Forwards_OK (N)), Loc));
963
964 Rewrite (N,
965 Make_Procedure_Call_Statement (Loc,
966 Name => New_Occurrence_Of (Proc, Loc),
967 Parameter_Associations => Actuals));
968 end;
969
970 else
971 Rewrite (N,
972 Expand_Assign_Array_Loop_Or_Bitfield
973 (N, Larray, Rarray, L_Type, R_Type, Ndim,
974 Rev => not Forwards_OK (N)));
975 end if;
976
977 -- Case of both are false with No_Implicit_Conditionals
978
979 elsif Restriction_Active (No_Implicit_Conditionals) then
980 declare
981 T : constant Entity_Id :=
982 Make_Defining_Identifier (Loc, Chars => Name_T);
983
984 begin
985 Rewrite (N,
986 Make_Block_Statement (Loc,
987 Declarations => New_List (
988 Make_Object_Declaration (Loc,
989 Defining_Identifier => T,
990 Constant_Present => True,
991 Object_Definition =>
992 New_Occurrence_Of (Etype (Rhs), Loc),
993 Expression => Relocate_Node (Rhs))),
994
995 Handled_Statement_Sequence =>
996 Make_Handled_Sequence_Of_Statements (Loc,
997 Statements => New_List (
998 Make_Assignment_Statement (Loc,
999 Name => Relocate_Node (Lhs),
1000 Expression => New_Occurrence_Of (T, Loc))))));
1001 end;
1002
1003 -- Case of both are false with implicit conditionals allowed
1004
1005 else
1006 -- Before we generate this code, we must ensure that the left and
1007 -- right side array types are defined. They may be itypes, and we
1008 -- cannot let them be defined inside the if, since the first use
1009 -- in the then may not be executed.
1010
1011 Ensure_Defined (L_Type, N);
1012 Ensure_Defined (R_Type, N);
1013
1014 -- We normally compare addresses to find out which way round to
1015 -- do the loop, since this is reliable, and handles the cases of
1016 -- parameters, conversions etc. But we can't do that in the bit
1017 -- packed case, because addresses don't work there.
1018
1019 if not Is_Bit_Packed_Array (L_Type) then
1020 Condition :=
1021 Make_Op_Le (Loc,
1022 Left_Opnd =>
1023 Unchecked_Convert_To (RTE (RE_Integer_Address),
1024 Make_Attribute_Reference (Loc,
1025 Prefix =>
1026 Make_Indexed_Component (Loc,
1027 Prefix =>
1028 Duplicate_Subexpr_Move_Checks (Larray, True),
1029 Expressions => New_List (
1030 Make_Attribute_Reference (Loc,
1031 Prefix =>
1032 New_Occurrence_Of
1033 (L_Index_Typ, Loc),
1034 Attribute_Name => Name_First))),
1035 Attribute_Name => Name_Address)),
1036
1037 Right_Opnd =>
1038 Unchecked_Convert_To (RTE (RE_Integer_Address),
1039 Make_Attribute_Reference (Loc,
1040 Prefix =>
1041 Make_Indexed_Component (Loc,
1042 Prefix =>
1043 Duplicate_Subexpr_Move_Checks (Rarray, True),
1044 Expressions => New_List (
1045 Make_Attribute_Reference (Loc,
1046 Prefix =>
1047 New_Occurrence_Of
1048 (R_Index_Typ, Loc),
1049 Attribute_Name => Name_First))),
1050 Attribute_Name => Name_Address)));
1051
1052 -- For the bit packed and VM cases we use the bounds. That's OK,
1053 -- because we don't have to worry about parameters, since they
1054 -- cannot cause overlap. Perhaps we should worry about weird slice
1055 -- conversions ???
1056
1057 else
1058 -- Copy the bounds
1059
1060 Cleft_Lo := New_Copy_Tree (Left_Lo);
1061 Cright_Lo := New_Copy_Tree (Right_Lo);
1062
1063 -- If the types do not match we add an implicit conversion
1064 -- here to ensure proper match
1065
1066 if Etype (Left_Lo) /= Etype (Right_Lo) then
1067 Cright_Lo :=
1068 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
1069 end if;
1070
1071 -- Reset the Analyzed flag, because the bounds of the index
1072 -- type itself may be universal, and must be reanalyzed to
1073 -- acquire the proper type for the back end.
1074
1075 Set_Analyzed (Cleft_Lo, False);
1076 Set_Analyzed (Cright_Lo, False);
1077
1078 Condition :=
1079 Make_Op_Le (Loc,
1080 Left_Opnd => Cleft_Lo,
1081 Right_Opnd => Cright_Lo);
1082 end if;
1083
1084 if Needs_Finalization (Component_Type (L_Type))
1085 and then Base_Type (L_Type) = Base_Type (R_Type)
1086 and then Ndim = 1
1087 and then not No_Ctrl_Actions (N)
1088 then
1089
1090 -- Call TSS procedure for array assignment, passing the
1091 -- explicit bounds of right- and left-hand sides.
1092
1093 declare
1094 Proc : constant Entity_Id :=
1095 TSS (Base_Type (L_Type), TSS_Slice_Assign);
1096 Actuals : List_Id;
1097
1098 begin
1099 Apply_Dereference (Larray);
1100 Apply_Dereference (Rarray);
1101 Actuals := New_List (
1102 Duplicate_Subexpr (Larray, Name_Req => True),
1103 Duplicate_Subexpr (Rarray, Name_Req => True),
1104 Duplicate_Subexpr (Left_Lo, Name_Req => True),
1105 Duplicate_Subexpr (Left_Hi, Name_Req => True),
1106 Duplicate_Subexpr (Right_Lo, Name_Req => True),
1107 Duplicate_Subexpr (Right_Hi, Name_Req => True));
1108
1109 Append_To (Actuals,
1110 Make_Op_Not (Loc,
1111 Right_Opnd => Condition));
1112
1113 Rewrite (N,
1114 Make_Procedure_Call_Statement (Loc,
1115 Name => New_Occurrence_Of (Proc, Loc),
1116 Parameter_Associations => Actuals));
1117 end;
1118
1119 else
1120 Rewrite (N,
1121 Make_Implicit_If_Statement (N,
1122 Condition => Condition,
1123
1124 Then_Statements => New_List (
1125 Expand_Assign_Array_Loop_Or_Bitfield
1126 (N, Larray, Rarray, L_Type, R_Type, Ndim,
1127 Rev => False)),
1128
1129 Else_Statements => New_List (
1130 Expand_Assign_Array_Loop_Or_Bitfield
1131 (N, Larray, Rarray, L_Type, R_Type, Ndim,
1132 Rev => True))));
1133 end if;
1134 end if;
1135
1136 Analyze (N, Suppress => All_Checks);
1137 end;
1138
1139 exception
1140 when RE_Not_Available =>
1141 return;
1142 end Expand_Assign_Array;
1143
1144 ------------------------------
1145 -- Expand_Assign_Array_Loop --
1146 ------------------------------
1147
1148 -- The following is an example of the loop generated for the case of a
1149 -- two-dimensional array:
1150
1151 -- declare
1152 -- R2b : Tm1X1 := 1;
1153 -- begin
1154 -- for L1b in 1 .. 100 loop
1155 -- declare
1156 -- R4b : Tm1X2 := 1;
1157 -- begin
1158 -- for L3b in 1 .. 100 loop
1159 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
1160 -- R4b := Tm1X2'succ(R4b);
1161 -- end loop;
1162 -- end;
1163 -- R2b := Tm1X1'succ(R2b);
1164 -- end loop;
1165 -- end;
1166
1167 -- Here Rev is False, and Tm1Xn are the subscript types for the right-hand
1168 -- side. The declarations of R2b and R4b are inserted before the original
1169 -- assignment statement.
1170
1171 function Expand_Assign_Array_Loop
1172 (N : Node_Id;
1173 Larray : Entity_Id;
1174 Rarray : Entity_Id;
1175 L_Type : Entity_Id;
1176 R_Type : Entity_Id;
1177 Ndim : Pos;
1178 Rev : Boolean) return Node_Id
1179 is
1180 Loc : constant Source_Ptr := Sloc (N);
1181
1182 Lnn : array (1 .. Ndim) of Entity_Id;
1183 Rnn : array (1 .. Ndim) of Entity_Id;
1184 -- Entities used as subscripts on left and right sides
1185
1186 L_Index_Type : array (1 .. Ndim) of Entity_Id;
1187 R_Index_Type : array (1 .. Ndim) of Entity_Id;
1188 -- Left and right index types
1189
1190 Assign : Node_Id;
1191
1192 F_Or_L : Name_Id;
1193 S_Or_P : Name_Id;
1194
1195 function Build_Step (J : Nat) return Node_Id;
1196 -- The increment step for the index of the right-hand side is written
1197 -- as an attribute reference (Succ or Pred). This function returns
1198 -- the corresponding node, which is placed at the end of the loop body.
1199
1200 ----------------
1201 -- Build_Step --
1202 ----------------
1203
1204 function Build_Step (J : Nat) return Node_Id is
1205 Step : Node_Id;
1206 Lim : Name_Id;
1207
1208 begin
1209 if Rev then
1210 Lim := Name_First;
1211 else
1212 Lim := Name_Last;
1213 end if;
1214
1215 Step :=
1216 Make_Assignment_Statement (Loc,
1217 Name => New_Occurrence_Of (Rnn (J), Loc),
1218 Expression =>
1219 Make_Attribute_Reference (Loc,
1220 Prefix =>
1221 New_Occurrence_Of (R_Index_Type (J), Loc),
1222 Attribute_Name => S_Or_P,
1223 Expressions => New_List (
1224 New_Occurrence_Of (Rnn (J), Loc))));
1225
1226 -- Note that on the last iteration of the loop, the index is increased
1227 -- (or decreased) past the corresponding bound. This is consistent with
1228 -- the C semantics of the back-end, where such an off-by-one value on a
1229 -- dead index variable is OK. However, in CodePeer mode this leads to
1230 -- spurious warnings, and thus we place a guard around the attribute
1231 -- reference. For obvious reasons we only do this for CodePeer.
1232
1233 if CodePeer_Mode then
1234 Step :=
1235 Make_If_Statement (Loc,
1236 Condition =>
1237 Make_Op_Ne (Loc,
1238 Left_Opnd => New_Occurrence_Of (Lnn (J), Loc),
1239 Right_Opnd =>
1240 Make_Attribute_Reference (Loc,
1241 Prefix => New_Occurrence_Of (L_Index_Type (J), Loc),
1242 Attribute_Name => Lim)),
1243 Then_Statements => New_List (Step));
1244 end if;
1245
1246 return Step;
1247 end Build_Step;
1248
1249 -- Start of processing for Expand_Assign_Array_Loop
1250
1251 begin
1252 if Rev then
1253 F_Or_L := Name_Last;
1254 S_Or_P := Name_Pred;
1255 else
1256 F_Or_L := Name_First;
1257 S_Or_P := Name_Succ;
1258 end if;
1259
1260 -- Setup index types and subscript entities
1261
1262 declare
1263 L_Index : Node_Id;
1264 R_Index : Node_Id;
1265
1266 begin
1267 L_Index := First_Index (L_Type);
1268 R_Index := First_Index (R_Type);
1269
1270 for J in 1 .. Ndim loop
1271 Lnn (J) := Make_Temporary (Loc, 'L');
1272 Rnn (J) := Make_Temporary (Loc, 'R');
1273
1274 L_Index_Type (J) := Etype (L_Index);
1275 R_Index_Type (J) := Etype (R_Index);
1276
1277 Next_Index (L_Index);
1278 Next_Index (R_Index);
1279 end loop;
1280 end;
1281
1282 -- Now construct the assignment statement
1283
1284 declare
1285 ExprL : constant List_Id := New_List;
1286 ExprR : constant List_Id := New_List;
1287
1288 begin
1289 for J in 1 .. Ndim loop
1290 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1291 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1292 end loop;
1293
1294 Assign :=
1295 Make_Assignment_Statement (Loc,
1296 Name =>
1297 Make_Indexed_Component (Loc,
1298 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1299 Expressions => ExprL),
1300 Expression =>
1301 Make_Indexed_Component (Loc,
1302 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1303 Expressions => ExprR));
1304
1305 -- We set assignment OK, since there are some cases, e.g. in object
1306 -- declarations, where we are actually assigning into a constant.
1307 -- If there really is an illegality, it was caught long before now,
1308 -- and was flagged when the original assignment was analyzed.
1309
1310 Set_Assignment_OK (Name (Assign));
1311
1312 -- Propagate the No_Ctrl_Actions flag to individual assignments
1313
1314 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1315 end;
1316
1317 -- Now construct the loop from the inside out, with the last subscript
1318 -- varying most rapidly. Note that Assign is first the raw assignment
1319 -- statement, and then subsequently the loop that wraps it up.
1320
1321 for J in reverse 1 .. Ndim loop
1322 Assign :=
1323 Make_Block_Statement (Loc,
1324 Declarations => New_List (
1325 Make_Object_Declaration (Loc,
1326 Defining_Identifier => Rnn (J),
1327 Object_Definition =>
1328 New_Occurrence_Of (R_Index_Type (J), Loc),
1329 Expression =>
1330 Make_Attribute_Reference (Loc,
1331 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1332 Attribute_Name => F_Or_L))),
1333
1334 Handled_Statement_Sequence =>
1335 Make_Handled_Sequence_Of_Statements (Loc,
1336 Statements => New_List (
1337 Make_Implicit_Loop_Statement (N,
1338 Iteration_Scheme =>
1339 Make_Iteration_Scheme (Loc,
1340 Loop_Parameter_Specification =>
1341 Make_Loop_Parameter_Specification (Loc,
1342 Defining_Identifier => Lnn (J),
1343 Reverse_Present => Rev,
1344 Discrete_Subtype_Definition =>
1345 New_Occurrence_Of (L_Index_Type (J), Loc))),
1346
1347 Statements => New_List (Assign, Build_Step (J))))));
1348 end loop;
1349
1350 return Assign;
1351 end Expand_Assign_Array_Loop;
1352
1353 ----------------------------------
1354 -- Expand_Assign_Array_Bitfield --
1355 ----------------------------------
1356
1357 function Expand_Assign_Array_Bitfield
1358 (N : Node_Id;
1359 Larray : Entity_Id;
1360 Rarray : Entity_Id;
1361 L_Type : Entity_Id;
1362 R_Type : Entity_Id;
1363 Rev : Boolean) return Node_Id
1364 is
1365 pragma Assert (not Rev);
1366 -- Reverse copying is not yet supported by Copy_Bitfield.
1367
1368 pragma Assert (not Change_Of_Representation (N));
1369 -- This won't work, for example, to copy a packed array to an unpacked
1370 -- array.
1371
1372 Loc : constant Source_Ptr := Sloc (N);
1373
1374 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
1375 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
1376 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
1377 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
1378
1379 L_Addr : constant Node_Id :=
1380 Make_Attribute_Reference (Loc,
1381 Prefix =>
1382 Make_Indexed_Component (Loc,
1383 Prefix =>
1384 Duplicate_Subexpr (Larray, True),
1385 Expressions => New_List (New_Copy_Tree (Left_Lo))),
1386 Attribute_Name => Name_Address);
1387
1388 L_Bit : constant Node_Id :=
1389 Make_Attribute_Reference (Loc,
1390 Prefix =>
1391 Make_Indexed_Component (Loc,
1392 Prefix =>
1393 Duplicate_Subexpr (Larray, True),
1394 Expressions => New_List (New_Copy_Tree (Left_Lo))),
1395 Attribute_Name => Name_Bit);
1396
1397 R_Addr : constant Node_Id :=
1398 Make_Attribute_Reference (Loc,
1399 Prefix =>
1400 Make_Indexed_Component (Loc,
1401 Prefix =>
1402 Duplicate_Subexpr (Rarray, True),
1403 Expressions => New_List (New_Copy_Tree (Right_Lo))),
1404 Attribute_Name => Name_Address);
1405
1406 R_Bit : constant Node_Id :=
1407 Make_Attribute_Reference (Loc,
1408 Prefix =>
1409 Make_Indexed_Component (Loc,
1410 Prefix =>
1411 Duplicate_Subexpr (Rarray, True),
1412 Expressions => New_List (New_Copy_Tree (Right_Lo))),
1413 Attribute_Name => Name_Bit);
1414
1415 -- Compute the Size of the bitfield
1416
1417 -- Note that the length check has already been done, so we can use the
1418 -- size of either L or R; they are equal. We can't use 'Size here,
1419 -- because sometimes bit fields get copied into a temp, and the 'Size
1420 -- ends up being the size of the temp (e.g. an 8-bit temp containing
1421 -- a 4-bit bit field).
1422
1423 Size : constant Node_Id :=
1424 Make_Op_Multiply (Loc,
1425 Make_Attribute_Reference (Loc,
1426 Prefix =>
1427 Duplicate_Subexpr (Name (N), True),
1428 Attribute_Name => Name_Length),
1429 Make_Attribute_Reference (Loc,
1430 Prefix =>
1431 Duplicate_Subexpr (Name (N), True),
1432 Attribute_Name => Name_Component_Size));
1433
1434 begin
1435 return Make_Procedure_Call_Statement (Loc,
1436 Name => New_Occurrence_Of (RTE (RE_Copy_Bitfield), Loc),
1437 Parameter_Associations => New_List (
1438 R_Addr, R_Bit, L_Addr, L_Bit, Size));
1439 end Expand_Assign_Array_Bitfield;
1440
1441 ------------------------------------------
1442 -- Expand_Assign_Array_Loop_Or_Bitfield --
1443 ------------------------------------------
1444
1445 function Expand_Assign_Array_Loop_Or_Bitfield
1446 (N : Node_Id;
1447 Larray : Entity_Id;
1448 Rarray : Entity_Id;
1449 L_Type : Entity_Id;
1450 R_Type : Entity_Id;
1451 Ndim : Pos;
1452 Rev : Boolean) return Node_Id
1453 is
1454 Slices : constant Boolean :=
1455 Nkind (Name (N)) = N_Slice or else Nkind (Expression (N)) = N_Slice;
1456 L_Prefix_Comp : constant Boolean :=
1457 -- True if the left-hand side is a slice of a component or slice
1458 Nkind (Name (N)) = N_Slice
1459 and then Nkind (Prefix (Name (N))) in
1460 N_Selected_Component | N_Indexed_Component | N_Slice;
1461 R_Prefix_Comp : constant Boolean :=
1462 -- Likewise for the right-hand side
1463 Nkind (Expression (N)) = N_Slice
1464 and then Nkind (Prefix (Expression (N))) in
1465 N_Selected_Component | N_Indexed_Component | N_Slice;
1466
1467 begin
1468 -- Determine whether Copy_Bitfield is appropriate (will work, and will
1469 -- be more efficient than component-by-component copy). Copy_Bitfield
1470 -- doesn't work for reversed storage orders. It is efficient for slices
1471 -- of bit-packed arrays. Copy_Bitfield can read and write bits that are
1472 -- not part of the objects being copied, so we don't want to use it if
1473 -- there are volatile or independent components. If the Prefix of the
1474 -- slice is a component or slice, then it might be a part of an object
1475 -- with some other volatile or independent components, so we disable the
1476 -- optimization in that case as well. We could complicate this code by
1477 -- actually looking for such volatile and independent components.
1478
1479 if Is_Bit_Packed_Array (L_Type)
1480 and then Is_Bit_Packed_Array (R_Type)
1481 and then not Reverse_Storage_Order (L_Type)
1482 and then not Reverse_Storage_Order (R_Type)
1483 and then Ndim = 1
1484 and then not Rev
1485 and then Slices
1486 and then not Has_Volatile_Component (L_Type)
1487 and then not Has_Volatile_Component (R_Type)
1488 and then not Has_Independent_Components (L_Type)
1489 and then not Has_Independent_Components (R_Type)
1490 and then not L_Prefix_Comp
1491 and then not R_Prefix_Comp
1492 and then RTE_Available (RE_Copy_Bitfield)
1493 then
1494 return Expand_Assign_Array_Bitfield
1495 (N, Larray, Rarray, L_Type, R_Type, Rev);
1496 else
1497 return Expand_Assign_Array_Loop
1498 (N, Larray, Rarray, L_Type, R_Type, Ndim, Rev);
1499 end if;
1500 end Expand_Assign_Array_Loop_Or_Bitfield;
1501
1502 --------------------------
1503 -- Expand_Assign_Record --
1504 --------------------------
1505
1506 procedure Expand_Assign_Record (N : Node_Id) is
1507 Lhs : constant Node_Id := Name (N);
1508 Rhs : Node_Id := Expression (N);
1509 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1510
1511 begin
1512 -- If change of representation, then extract the real right-hand side
1513 -- from the type conversion, and proceed with component-wise assignment,
1514 -- since the two types are not the same as far as the back end is
1515 -- concerned.
1516
1517 if Change_Of_Representation (N) then
1518 Rhs := Expression (Rhs);
1519
1520 -- If this may be a case of a large bit aligned component, then proceed
1521 -- with component-wise assignment, to avoid possible clobbering of other
1522 -- components sharing bits in the first or last byte of the component to
1523 -- be assigned.
1524
1525 elsif Possible_Bit_Aligned_Component (Lhs)
1526 or else
1527 Possible_Bit_Aligned_Component (Rhs)
1528 then
1529 null;
1530
1531 -- If we have a tagged type that has a complete record representation
1532 -- clause, we must do we must do component-wise assignments, since child
1533 -- types may have used gaps for their components, and we might be
1534 -- dealing with a view conversion.
1535
1536 elsif Is_Fully_Repped_Tagged_Type (L_Typ) then
1537 null;
1538
1539 -- If neither condition met, then nothing special to do, the back end
1540 -- can handle assignment of the entire component as a single entity.
1541
1542 else
1543 return;
1544 end if;
1545
1546 -- At this stage we know that we must do a component wise assignment
1547
1548 declare
1549 Loc : constant Source_Ptr := Sloc (N);
1550 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1551 Decl : constant Node_Id := Declaration_Node (R_Typ);
1552 RDef : Node_Id;
1553 F : Entity_Id;
1554
1555 function Find_Component
1556 (Typ : Entity_Id;
1557 Comp : Entity_Id) return Entity_Id;
1558 -- Find the component with the given name in the underlying record
1559 -- declaration for Typ. We need to use the actual entity because the
1560 -- type may be private and resolution by identifier alone would fail.
1561
1562 function Make_Component_List_Assign
1563 (CL : Node_Id;
1564 U_U : Boolean := False) return List_Id;
1565 -- Returns a sequence of statements to assign the components that
1566 -- are referenced in the given component list. The flag U_U is
1567 -- used to force the usage of the inferred value of the variant
1568 -- part expression as the switch for the generated case statement.
1569
1570 function Make_Field_Assign
1571 (C : Entity_Id;
1572 U_U : Boolean := False) return Node_Id;
1573 -- Given C, the entity for a discriminant or component, build an
1574 -- assignment for the corresponding field values. The flag U_U
1575 -- signals the presence of an Unchecked_Union and forces the usage
1576 -- of the inferred discriminant value of C as the right-hand side
1577 -- of the assignment.
1578
1579 function Make_Field_Assigns (CI : List_Id) return List_Id;
1580 -- Given CI, a component items list, construct series of statements
1581 -- for fieldwise assignment of the corresponding components.
1582
1583 --------------------
1584 -- Find_Component --
1585 --------------------
1586
1587 function Find_Component
1588 (Typ : Entity_Id;
1589 Comp : Entity_Id) return Entity_Id
1590 is
1591 Utyp : constant Entity_Id := Underlying_Type (Typ);
1592 C : Entity_Id;
1593
1594 begin
1595 C := First_Entity (Utyp);
1596 while Present (C) loop
1597 if Chars (C) = Chars (Comp) then
1598 return C;
1599
1600 -- The component may be a renamed discriminant, in
1601 -- which case check against the name of the original
1602 -- discriminant of the parent type.
1603
1604 elsif Is_Derived_Type (Scope (Comp))
1605 and then Ekind (Comp) = E_Discriminant
1606 and then Present (Corresponding_Discriminant (Comp))
1607 and then
1608 Chars (C) = Chars (Corresponding_Discriminant (Comp))
1609 then
1610 return C;
1611 end if;
1612
1613 Next_Entity (C);
1614 end loop;
1615
1616 raise Program_Error;
1617 end Find_Component;
1618
1619 --------------------------------
1620 -- Make_Component_List_Assign --
1621 --------------------------------
1622
1623 function Make_Component_List_Assign
1624 (CL : Node_Id;
1625 U_U : Boolean := False) return List_Id
1626 is
1627 CI : constant List_Id := Component_Items (CL);
1628 VP : constant Node_Id := Variant_Part (CL);
1629
1630 Constrained_Typ : Entity_Id;
1631 Alts : List_Id;
1632 DC : Node_Id;
1633 DCH : List_Id;
1634 Expr : Node_Id;
1635 Result : List_Id;
1636 V : Node_Id;
1637
1638 begin
1639 -- Try to find a constrained type to extract discriminant values
1640 -- from, so that the case statement built below gets an
1641 -- opportunity to be folded by Expand_N_Case_Statement.
1642
1643 if U_U or else Is_Constrained (Etype (Rhs)) then
1644 Constrained_Typ := Etype (Rhs);
1645 elsif Is_Constrained (Etype (Expression (N))) then
1646 Constrained_Typ := Etype (Expression (N));
1647 else
1648 Constrained_Typ := Empty;
1649 end if;
1650
1651 Result := Make_Field_Assigns (CI);
1652
1653 if Present (VP) then
1654 V := First_Non_Pragma (Variants (VP));
1655 Alts := New_List;
1656 while Present (V) loop
1657 DCH := New_List;
1658 DC := First (Discrete_Choices (V));
1659 while Present (DC) loop
1660 Append_To (DCH, New_Copy_Tree (DC));
1661 Next (DC);
1662 end loop;
1663
1664 Append_To (Alts,
1665 Make_Case_Statement_Alternative (Loc,
1666 Discrete_Choices => DCH,
1667 Statements =>
1668 Make_Component_List_Assign (Component_List (V))));
1669 Next_Non_Pragma (V);
1670 end loop;
1671
1672 if Present (Constrained_Typ) then
1673 Expr :=
1674 New_Copy (Get_Discriminant_Value (
1675 Entity (Name (VP)),
1676 Constrained_Typ,
1677 Discriminant_Constraint (Constrained_Typ)));
1678 else
1679 Expr :=
1680 Make_Selected_Component (Loc,
1681 Prefix => Duplicate_Subexpr (Rhs),
1682 Selector_Name =>
1683 Make_Identifier (Loc, Chars (Name (VP))));
1684 end if;
1685
1686 Append_To (Result,
1687 Make_Case_Statement (Loc,
1688 Expression => Expr,
1689 Alternatives => Alts));
1690 end if;
1691
1692 return Result;
1693 end Make_Component_List_Assign;
1694
1695 -----------------------
1696 -- Make_Field_Assign --
1697 -----------------------
1698
1699 function Make_Field_Assign
1700 (C : Entity_Id;
1701 U_U : Boolean := False) return Node_Id
1702 is
1703 A : Node_Id;
1704 Disc : Entity_Id;
1705 Expr : Node_Id;
1706
1707 begin
1708 -- The discriminant entity to be used in the retrieval below must
1709 -- be one in the corresponding type, given that the assignment may
1710 -- be between derived and parent types.
1711
1712 if Is_Derived_Type (Etype (Rhs)) then
1713 Disc := Find_Component (R_Typ, C);
1714 else
1715 Disc := C;
1716 end if;
1717
1718 -- In the case of an Unchecked_Union, use the discriminant
1719 -- constraint value as on the right-hand side of the assignment.
1720
1721 if U_U then
1722 Expr :=
1723 New_Copy (Get_Discriminant_Value (C,
1724 Etype (Rhs),
1725 Discriminant_Constraint (Etype (Rhs))));
1726 else
1727 Expr :=
1728 Make_Selected_Component (Loc,
1729 Prefix => Duplicate_Subexpr (Rhs),
1730 Selector_Name => New_Occurrence_Of (Disc, Loc));
1731 end if;
1732
1733 -- Generate the assignment statement. When the left-hand side
1734 -- is an object with an address clause present, force generated
1735 -- temporaries to be renamings so as to correctly assign to any
1736 -- overlaid objects.
1737
1738 A :=
1739 Make_Assignment_Statement (Loc,
1740 Name =>
1741 Make_Selected_Component (Loc,
1742 Prefix =>
1743 Duplicate_Subexpr
1744 (Exp => Lhs,
1745 Name_Req => False,
1746 Renaming_Req =>
1747 Is_Entity_Name (Lhs)
1748 and then Present (Address_Clause (Entity (Lhs)))),
1749 Selector_Name =>
1750 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1751 Expression => Expr);
1752
1753 -- Set Assignment_OK, so discriminants can be assigned
1754
1755 Set_Assignment_OK (Name (A), True);
1756
1757 if Componentwise_Assignment (N)
1758 and then Nkind (Name (A)) = N_Selected_Component
1759 and then Chars (Selector_Name (Name (A))) = Name_uParent
1760 then
1761 Set_Componentwise_Assignment (A);
1762 end if;
1763
1764 return A;
1765 end Make_Field_Assign;
1766
1767 ------------------------
1768 -- Make_Field_Assigns --
1769 ------------------------
1770
1771 function Make_Field_Assigns (CI : List_Id) return List_Id is
1772 Item : Node_Id;
1773 Result : List_Id;
1774
1775 begin
1776 Item := First (CI);
1777 Result := New_List;
1778
1779 while Present (Item) loop
1780
1781 -- Look for components, but exclude _tag field assignment if
1782 -- the special Componentwise_Assignment flag is set.
1783
1784 if Nkind (Item) = N_Component_Declaration
1785 and then not (Is_Tag (Defining_Identifier (Item))
1786 and then Componentwise_Assignment (N))
1787 then
1788 Append_To
1789 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1790 end if;
1791
1792 Next (Item);
1793 end loop;
1794
1795 return Result;
1796 end Make_Field_Assigns;
1797
1798 -- Start of processing for Expand_Assign_Record
1799
1800 begin
1801 -- Note that we need to use the base types for this processing in
1802 -- order to retrieve the Type_Definition. In the constrained case,
1803 -- we filter out the non relevant fields in
1804 -- Make_Component_List_Assign.
1805
1806 -- First copy the discriminants. This is done unconditionally. It
1807 -- is required in the unconstrained left side case, and also in the
1808 -- case where this assignment was constructed during the expansion
1809 -- of a type conversion (since initialization of discriminants is
1810 -- suppressed in this case). It is unnecessary but harmless in
1811 -- other cases.
1812
1813 -- Special case: no copy if the target has no discriminants
1814
1815 if Has_Discriminants (L_Typ)
1816 and then Is_Unchecked_Union (Base_Type (L_Typ))
1817 then
1818 null;
1819
1820 elsif Has_Discriminants (L_Typ) then
1821 F := First_Discriminant (R_Typ);
1822 while Present (F) loop
1823
1824 -- If we are expanding the initialization of a derived record
1825 -- that constrains or renames discriminants of the parent, we
1826 -- must use the corresponding discriminant in the parent.
1827
1828 declare
1829 CF : Entity_Id;
1830
1831 begin
1832 if Inside_Init_Proc
1833 and then Present (Corresponding_Discriminant (F))
1834 then
1835 CF := Corresponding_Discriminant (F);
1836 else
1837 CF := F;
1838 end if;
1839
1840 if Is_Unchecked_Union (R_Typ) then
1841
1842 -- Within an initialization procedure this is the
1843 -- assignment to an unchecked union component, in which
1844 -- case there is no discriminant to initialize.
1845
1846 if Inside_Init_Proc then
1847 null;
1848
1849 else
1850 -- The assignment is part of a conversion from a
1851 -- derived unchecked union type with an inferable
1852 -- discriminant, to a parent type.
1853
1854 Insert_Action (N, Make_Field_Assign (CF, True));
1855 end if;
1856
1857 else
1858 Insert_Action (N, Make_Field_Assign (CF));
1859 end if;
1860
1861 Next_Discriminant (F);
1862 end;
1863 end loop;
1864
1865 -- If the derived type has a stored constraint, assign the value
1866 -- of the corresponding discriminants explicitly, skipping those
1867 -- that are renamed discriminants. We cannot just retrieve them
1868 -- from the Rhs by selected component because they are invisible
1869 -- in the type of the right-hand side.
1870
1871 if Stored_Constraint (R_Typ) /= No_Elist then
1872 declare
1873 Assign : Node_Id;
1874 Discr_Val : Elmt_Id;
1875
1876 begin
1877 Discr_Val := First_Elmt (Stored_Constraint (R_Typ));
1878 F := First_Entity (R_Typ);
1879 while Present (F) loop
1880 if Ekind (F) = E_Discriminant
1881 and then Is_Completely_Hidden (F)
1882 and then Present (Corresponding_Record_Component (F))
1883 and then
1884 (not Is_Entity_Name (Node (Discr_Val))
1885 or else Ekind (Entity (Node (Discr_Val))) /=
1886 E_Discriminant)
1887 then
1888 Assign :=
1889 Make_Assignment_Statement (Loc,
1890 Name =>
1891 Make_Selected_Component (Loc,
1892 Prefix => Duplicate_Subexpr (Lhs),
1893 Selector_Name =>
1894 New_Occurrence_Of
1895 (Corresponding_Record_Component (F), Loc)),
1896 Expression => New_Copy (Node (Discr_Val)));
1897
1898 Set_Assignment_OK (Name (Assign));
1899 Insert_Action (N, Assign);
1900 Next_Elmt (Discr_Val);
1901 end if;
1902
1903 Next_Entity (F);
1904 end loop;
1905 end;
1906 end if;
1907 end if;
1908
1909 -- We know the underlying type is a record, but its current view
1910 -- may be private. We must retrieve the usable record declaration.
1911
1912 if Nkind (Decl) in N_Private_Type_Declaration
1913 | N_Private_Extension_Declaration
1914 and then Present (Full_View (R_Typ))
1915 then
1916 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1917 else
1918 RDef := Type_Definition (Decl);
1919 end if;
1920
1921 if Nkind (RDef) = N_Derived_Type_Definition then
1922 RDef := Record_Extension_Part (RDef);
1923 end if;
1924
1925 if Nkind (RDef) = N_Record_Definition
1926 and then Present (Component_List (RDef))
1927 then
1928 if Is_Unchecked_Union (R_Typ) then
1929 Insert_Actions (N,
1930 Make_Component_List_Assign (Component_List (RDef), True));
1931 else
1932 Insert_Actions (N,
1933 Make_Component_List_Assign (Component_List (RDef)));
1934 end if;
1935
1936 Rewrite (N, Make_Null_Statement (Loc));
1937 end if;
1938 end;
1939 end Expand_Assign_Record;
1940
1941 -------------------------------------
1942 -- Expand_Assign_With_Target_Names --
1943 -------------------------------------
1944
1945 procedure Expand_Assign_With_Target_Names (N : Node_Id) is
1946 LHS : constant Node_Id := Name (N);
1947 LHS_Typ : constant Entity_Id := Etype (LHS);
1948 Loc : constant Source_Ptr := Sloc (N);
1949 RHS : constant Node_Id := Expression (N);
1950
1951 Ent : Entity_Id;
1952 -- The entity of the left-hand side
1953
1954 function Replace_Target (N : Node_Id) return Traverse_Result;
1955 -- Replace occurrences of the target name by the proper entity: either
1956 -- the entity of the LHS in simple cases, or the formal of the
1957 -- constructed procedure otherwise.
1958
1959 --------------------
1960 -- Replace_Target --
1961 --------------------
1962
1963 function Replace_Target (N : Node_Id) return Traverse_Result is
1964 begin
1965 if Nkind (N) = N_Target_Name then
1966 Rewrite (N, New_Occurrence_Of (Ent, Sloc (N)));
1967
1968 -- The expression will be reanalyzed when the enclosing assignment
1969 -- is reanalyzed, so reset the entity, which may be a temporary
1970 -- created during analysis, e.g. a loop variable for an iterated
1971 -- component association. However, if entity is callable then
1972 -- resolution has established its proper identity (including in
1973 -- rewritten prefixed calls) so we must preserve it.
1974
1975 elsif Is_Entity_Name (N) then
1976 if Present (Entity (N))
1977 and then not Is_Overloadable (Entity (N))
1978 then
1979 Set_Entity (N, Empty);
1980 end if;
1981 end if;
1982
1983 Set_Analyzed (N, False);
1984 return OK;
1985 end Replace_Target;
1986
1987 procedure Replace_Target_Name is new Traverse_Proc (Replace_Target);
1988
1989 -- Local variables
1990
1991 New_RHS : Node_Id;
1992 Proc_Id : Entity_Id;
1993
1994 -- Start of processing for Expand_Assign_With_Target_Names
1995
1996 begin
1997 New_RHS := New_Copy_Tree (RHS);
1998
1999 -- The left-hand side is a direct name
2000
2001 if Is_Entity_Name (LHS)
2002 and then not Is_Renaming_Of_Object (Entity (LHS))
2003 then
2004 Ent := Entity (LHS);
2005 Replace_Target_Name (New_RHS);
2006
2007 -- Generate:
2008 -- LHS := ... LHS ...;
2009
2010 Rewrite (N,
2011 Make_Assignment_Statement (Loc,
2012 Name => Relocate_Node (LHS),
2013 Expression => New_RHS));
2014
2015 -- The left-hand side is not a direct name, but is side-effect free.
2016 -- Capture its value in a temporary to avoid multiple evaluations.
2017
2018 elsif Side_Effect_Free (LHS) then
2019 Ent := Make_Temporary (Loc, 'T');
2020 Replace_Target_Name (New_RHS);
2021
2022 -- Generate:
2023 -- T : LHS_Typ := LHS;
2024
2025 Insert_Before_And_Analyze (N,
2026 Make_Object_Declaration (Loc,
2027 Defining_Identifier => Ent,
2028 Object_Definition => New_Occurrence_Of (LHS_Typ, Loc),
2029 Expression => New_Copy_Tree (LHS)));
2030
2031 -- Generate:
2032 -- LHS := ... T ...;
2033
2034 Rewrite (N,
2035 Make_Assignment_Statement (Loc,
2036 Name => Relocate_Node (LHS),
2037 Expression => New_RHS));
2038
2039 -- Otherwise wrap the whole assignment statement in a procedure with an
2040 -- IN OUT parameter. The original assignment then becomes a call to the
2041 -- procedure with the left-hand side as an actual.
2042
2043 else
2044 Ent := Make_Temporary (Loc, 'T');
2045 Replace_Target_Name (New_RHS);
2046
2047 -- Generate:
2048 -- procedure P (T : in out LHS_Typ) is
2049 -- begin
2050 -- T := ... T ...;
2051 -- end P;
2052
2053 Proc_Id := Make_Temporary (Loc, 'P');
2054
2055 Insert_Before_And_Analyze (N,
2056 Make_Subprogram_Body (Loc,
2057 Specification =>
2058 Make_Procedure_Specification (Loc,
2059 Defining_Unit_Name => Proc_Id,
2060 Parameter_Specifications => New_List (
2061 Make_Parameter_Specification (Loc,
2062 Defining_Identifier => Ent,
2063 In_Present => True,
2064 Out_Present => True,
2065 Parameter_Type =>
2066 New_Occurrence_Of (LHS_Typ, Loc)))),
2067
2068 Declarations => Empty_List,
2069
2070 Handled_Statement_Sequence =>
2071 Make_Handled_Sequence_Of_Statements (Loc,
2072 Statements => New_List (
2073 Make_Assignment_Statement (Loc,
2074 Name => New_Occurrence_Of (Ent, Loc),
2075 Expression => New_RHS)))));
2076
2077 -- Generate:
2078 -- P (LHS);
2079
2080 Rewrite (N,
2081 Make_Procedure_Call_Statement (Loc,
2082 Name => New_Occurrence_Of (Proc_Id, Loc),
2083 Parameter_Associations => New_List (Relocate_Node (LHS))));
2084 end if;
2085
2086 -- Analyze rewritten node, either as assignment or procedure call
2087
2088 Analyze (N);
2089 end Expand_Assign_With_Target_Names;
2090
2091 -----------------------------------
2092 -- Expand_N_Assignment_Statement --
2093 -----------------------------------
2094
2095 -- This procedure implements various cases where an assignment statement
2096 -- cannot just be passed on to the back end in untransformed state.
2097
2098 procedure Expand_N_Assignment_Statement (N : Node_Id) is
2099 Crep : constant Boolean := Change_Of_Representation (N);
2100 Lhs : constant Node_Id := Name (N);
2101 Loc : constant Source_Ptr := Sloc (N);
2102 Rhs : constant Node_Id := Expression (N);
2103 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
2104 Exp : Node_Id;
2105
2106 begin
2107 -- Special case to check right away, if the Componentwise_Assignment
2108 -- flag is set, this is a reanalysis from the expansion of the primitive
2109 -- assignment procedure for a tagged type, and all we need to do is to
2110 -- expand to assignment of components, because otherwise, we would get
2111 -- infinite recursion (since this looks like a tagged assignment which
2112 -- would normally try to *call* the primitive assignment procedure).
2113
2114 if Componentwise_Assignment (N) then
2115 Expand_Assign_Record (N);
2116 return;
2117 end if;
2118
2119 -- Defend against invalid subscripts on left side if we are in standard
2120 -- validity checking mode. No need to do this if we are checking all
2121 -- subscripts.
2122
2123 -- Note that we do this right away, because there are some early return
2124 -- paths in this procedure, and this is required on all paths.
2125
2126 if Validity_Checks_On
2127 and then Validity_Check_Default
2128 and then not Validity_Check_Subscripts
2129 then
2130 Check_Valid_Lvalue_Subscripts (Lhs);
2131 end if;
2132
2133 -- Separate expansion if RHS contain target names. Note that assignment
2134 -- may already have been expanded if RHS is aggregate.
2135
2136 if Nkind (N) = N_Assignment_Statement and then Has_Target_Names (N) then
2137 Expand_Assign_With_Target_Names (N);
2138 return;
2139 end if;
2140
2141 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
2142
2143 -- Rewrite an assignment to X'Priority into a run-time call
2144
2145 -- For example: X'Priority := New_Prio_Expr;
2146 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
2147
2148 -- Note that although X'Priority is notionally an object, it is quite
2149 -- deliberately not defined as an aliased object in the RM. This means
2150 -- that it works fine to rewrite it as a call, without having to worry
2151 -- about complications that would other arise from X'Priority'Access,
2152 -- which is illegal, because of the lack of aliasing.
2153
2154 if Ada_Version >= Ada_2005 then
2155 declare
2156 Call : Node_Id;
2157 Conctyp : Entity_Id;
2158 Ent : Entity_Id;
2159 Subprg : Entity_Id;
2160 RT_Subprg_Name : Node_Id;
2161
2162 begin
2163 -- Handle chains of renamings
2164
2165 Ent := Name (N);
2166 while Nkind (Ent) in N_Has_Entity
2167 and then Present (Entity (Ent))
2168 and then Present (Renamed_Object (Entity (Ent)))
2169 loop
2170 Ent := Renamed_Object (Entity (Ent));
2171 end loop;
2172
2173 -- The attribute Priority applied to protected objects has been
2174 -- previously expanded into a call to the Get_Ceiling run-time
2175 -- subprogram. In restricted profiles this is not available.
2176
2177 if Is_Expanded_Priority_Attribute (Ent) then
2178
2179 -- Look for the enclosing concurrent type
2180
2181 Conctyp := Current_Scope;
2182 while not Is_Concurrent_Type (Conctyp) loop
2183 Conctyp := Scope (Conctyp);
2184 end loop;
2185
2186 pragma Assert (Is_Protected_Type (Conctyp));
2187
2188 -- Generate the first actual of the call
2189
2190 Subprg := Current_Scope;
2191 while not Present (Protected_Body_Subprogram (Subprg)) loop
2192 Subprg := Scope (Subprg);
2193 end loop;
2194
2195 -- Select the appropriate run-time call
2196
2197 if Number_Entries (Conctyp) = 0 then
2198 RT_Subprg_Name :=
2199 New_Occurrence_Of (RTE (RE_Set_Ceiling), Loc);
2200 else
2201 RT_Subprg_Name :=
2202 New_Occurrence_Of (RTE (RO_PE_Set_Ceiling), Loc);
2203 end if;
2204
2205 Call :=
2206 Make_Procedure_Call_Statement (Loc,
2207 Name => RT_Subprg_Name,
2208 Parameter_Associations => New_List (
2209 New_Copy_Tree (First (Parameter_Associations (Ent))),
2210 Relocate_Node (Expression (N))));
2211
2212 Rewrite (N, Call);
2213 Analyze (N);
2214
2215 return;
2216 end if;
2217 end;
2218 end if;
2219
2220 -- Deal with assignment checks unless suppressed
2221
2222 if not Suppress_Assignment_Checks (N) then
2223
2224 -- First deal with generation of range check if required,
2225 -- and then predicate checks if the type carries a predicate.
2226 -- If the Rhs is an expression these tests may have been applied
2227 -- already. This is the case if the RHS is a type conversion.
2228 -- Other such redundant checks could be removed ???
2229
2230 if Nkind (Rhs) /= N_Type_Conversion
2231 or else Entity (Subtype_Mark (Rhs)) /= Typ
2232 then
2233 if Do_Range_Check (Rhs) then
2234 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
2235 end if;
2236
2237 Apply_Predicate_Check (Rhs, Typ);
2238 end if;
2239 end if;
2240
2241 -- Check for a special case where a high level transformation is
2242 -- required. If we have either of:
2243
2244 -- P.field := rhs;
2245 -- P (sub) := rhs;
2246
2247 -- where P is a reference to a bit packed array, then we have to unwind
2248 -- the assignment. The exact meaning of being a reference to a bit
2249 -- packed array is as follows:
2250
2251 -- An indexed component whose prefix is a bit packed array is a
2252 -- reference to a bit packed array.
2253
2254 -- An indexed component or selected component whose prefix is a
2255 -- reference to a bit packed array is itself a reference ot a
2256 -- bit packed array.
2257
2258 -- The required transformation is
2259
2260 -- Tnn : prefix_type := P;
2261 -- Tnn.field := rhs;
2262 -- P := Tnn;
2263
2264 -- or
2265
2266 -- Tnn : prefix_type := P;
2267 -- Tnn (subscr) := rhs;
2268 -- P := Tnn;
2269
2270 -- Since P is going to be evaluated more than once, any subscripts
2271 -- in P must have their evaluation forced.
2272
2273 if Nkind (Lhs) in N_Indexed_Component | N_Selected_Component
2274 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
2275 then
2276 declare
2277 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
2278 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
2279 Tnn : constant Entity_Id :=
2280 Make_Temporary (Loc, 'T', BPAR_Expr);
2281
2282 begin
2283 -- Insert the post assignment first, because we want to copy the
2284 -- BPAR_Expr tree before it gets analyzed in the context of the
2285 -- pre assignment. Note that we do not analyze the post assignment
2286 -- yet (we cannot till we have completed the analysis of the pre
2287 -- assignment). As usual, the analysis of this post assignment
2288 -- will happen on its own when we "run into" it after finishing
2289 -- the current assignment.
2290
2291 Insert_After (N,
2292 Make_Assignment_Statement (Loc,
2293 Name => New_Copy_Tree (BPAR_Expr),
2294 Expression => New_Occurrence_Of (Tnn, Loc)));
2295
2296 -- At this stage BPAR_Expr is a reference to a bit packed array
2297 -- where the reference was not expanded in the original tree,
2298 -- since it was on the left side of an assignment. But in the
2299 -- pre-assignment statement (the object definition), BPAR_Expr
2300 -- will end up on the right-hand side, and must be reexpanded. To
2301 -- achieve this, we reset the analyzed flag of all selected and
2302 -- indexed components down to the actual indexed component for
2303 -- the packed array.
2304
2305 Exp := BPAR_Expr;
2306 loop
2307 Set_Analyzed (Exp, False);
2308
2309 if Nkind (Exp) in N_Indexed_Component | N_Selected_Component
2310 then
2311 Exp := Prefix (Exp);
2312 else
2313 exit;
2314 end if;
2315 end loop;
2316
2317 -- Now we can insert and analyze the pre-assignment
2318
2319 -- If the right-hand side requires a transient scope, it has
2320 -- already been placed on the stack. However, the declaration is
2321 -- inserted in the tree outside of this scope, and must reflect
2322 -- the proper scope for its variable. This awkward bit is forced
2323 -- by the stricter scope discipline imposed by GCC 2.97.
2324
2325 declare
2326 Uses_Transient_Scope : constant Boolean :=
2327 Scope_Is_Transient
2328 and then N = Node_To_Be_Wrapped;
2329
2330 begin
2331 if Uses_Transient_Scope then
2332 Push_Scope (Scope (Current_Scope));
2333 end if;
2334
2335 Insert_Before_And_Analyze (N,
2336 Make_Object_Declaration (Loc,
2337 Defining_Identifier => Tnn,
2338 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
2339 Expression => BPAR_Expr));
2340
2341 if Uses_Transient_Scope then
2342 Pop_Scope;
2343 end if;
2344 end;
2345
2346 -- Now fix up the original assignment and continue processing
2347
2348 Rewrite (Prefix (Lhs),
2349 New_Occurrence_Of (Tnn, Loc));
2350
2351 -- We do not need to reanalyze that assignment, and we do not need
2352 -- to worry about references to the temporary, but we do need to
2353 -- make sure that the temporary is not marked as a true constant
2354 -- since we now have a generated assignment to it.
2355
2356 Set_Is_True_Constant (Tnn, False);
2357 end;
2358 end if;
2359
2360 -- When we have the appropriate type of aggregate in the expression (it
2361 -- has been determined during analysis of the aggregate by setting the
2362 -- delay flag), let's perform in place assignment and thus avoid
2363 -- creating a temporary.
2364
2365 if Is_Delayed_Aggregate (Rhs) then
2366 Convert_Aggr_In_Assignment (N);
2367 Rewrite (N, Make_Null_Statement (Loc));
2368 Analyze (N);
2369
2370 return;
2371 end if;
2372
2373 -- Apply discriminant check if required. If Lhs is an access type to a
2374 -- designated type with discriminants, we must always check. If the
2375 -- type has unknown discriminants, more elaborate processing below.
2376
2377 if Has_Discriminants (Etype (Lhs))
2378 and then not Has_Unknown_Discriminants (Etype (Lhs))
2379 then
2380 -- Skip discriminant check if change of representation. Will be
2381 -- done when the change of representation is expanded out.
2382
2383 if not Crep then
2384 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
2385 end if;
2386
2387 -- If the type is private without discriminants, and the full type
2388 -- has discriminants (necessarily with defaults) a check may still be
2389 -- necessary if the Lhs is aliased. The private discriminants must be
2390 -- visible to build the discriminant constraints.
2391
2392 -- Only an explicit dereference that comes from source indicates
2393 -- aliasing. Access to formals of protected operations and entries
2394 -- create dereferences but are not semantic aliasings.
2395
2396 elsif Is_Private_Type (Etype (Lhs))
2397 and then Has_Discriminants (Typ)
2398 and then Nkind (Lhs) = N_Explicit_Dereference
2399 and then Comes_From_Source (Lhs)
2400 then
2401 declare
2402 Lt : constant Entity_Id := Etype (Lhs);
2403 Ubt : Entity_Id := Base_Type (Typ);
2404
2405 begin
2406 -- In the case of an expander-generated record subtype whose base
2407 -- type still appears private, Typ will have been set to that
2408 -- private type rather than the underlying record type (because
2409 -- Underlying type will have returned the record subtype), so it's
2410 -- necessary to apply Underlying_Type again to the base type to
2411 -- get the record type we need for the discriminant check. Such
2412 -- subtypes can be created for assignments in certain cases, such
2413 -- as within an instantiation passed this kind of private type.
2414 -- It would be good to avoid this special test, but making changes
2415 -- to prevent this odd form of record subtype seems difficult. ???
2416
2417 if Is_Private_Type (Ubt) then
2418 Ubt := Underlying_Type (Ubt);
2419 end if;
2420
2421 Set_Etype (Lhs, Ubt);
2422 Rewrite (Rhs, OK_Convert_To (Base_Type (Ubt), Rhs));
2423 Apply_Discriminant_Check (Rhs, Ubt, Lhs);
2424 Set_Etype (Lhs, Lt);
2425 end;
2426
2427 -- If the Lhs has a private type with unknown discriminants, it may
2428 -- have a full view with discriminants, but those are nameable only
2429 -- in the underlying type, so convert the Rhs to it before potential
2430 -- checking. Convert Lhs as well, otherwise the actual subtype might
2431 -- not be constructible. If the discriminants have defaults the type
2432 -- is unconstrained and there is nothing to check.
2433 -- Ditto if a private type with unknown discriminants has a full view
2434 -- that is an unconstrained array, in which case a length check is
2435 -- needed.
2436
2437 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs))) then
2438 if Has_Discriminants (Typ)
2439 and then not Has_Defaulted_Discriminants (Typ)
2440 then
2441 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
2442 Rewrite (Lhs, OK_Convert_To (Base_Type (Typ), Lhs));
2443 Apply_Discriminant_Check (Rhs, Typ, Lhs);
2444
2445 elsif Is_Array_Type (Typ) and then Is_Constrained (Typ) then
2446 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
2447 Rewrite (Lhs, OK_Convert_To (Base_Type (Typ), Lhs));
2448 Apply_Length_Check (Rhs, Typ);
2449 end if;
2450
2451 -- In the access type case, we need the same discriminant check, and
2452 -- also range checks if we have an access to constrained array.
2453
2454 elsif Is_Access_Type (Etype (Lhs))
2455 and then Is_Constrained (Designated_Type (Etype (Lhs)))
2456 then
2457 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
2458
2459 -- Skip discriminant check if change of representation. Will be
2460 -- done when the change of representation is expanded out.
2461
2462 if not Crep then
2463 Apply_Discriminant_Check (Rhs, Etype (Lhs));
2464 end if;
2465
2466 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
2467 Apply_Range_Check (Rhs, Etype (Lhs));
2468
2469 if Is_Constrained (Etype (Lhs)) then
2470 Apply_Length_Check (Rhs, Etype (Lhs));
2471 end if;
2472 end if;
2473 end if;
2474
2475 -- Ada 2005 (AI-231): Generate the run-time check
2476
2477 if Is_Access_Type (Typ)
2478 and then Can_Never_Be_Null (Etype (Lhs))
2479 and then not Can_Never_Be_Null (Etype (Rhs))
2480
2481 -- If an actual is an out parameter of a null-excluding access
2482 -- type, there is access check on entry, so we set the flag
2483 -- Suppress_Assignment_Checks on the generated statement to
2484 -- assign the actual to the parameter block, and we do not want
2485 -- to generate an additional check at this point.
2486
2487 and then not Suppress_Assignment_Checks (N)
2488 then
2489 Apply_Constraint_Check (Rhs, Etype (Lhs));
2490 end if;
2491
2492 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
2493 -- stand-alone obj of an anonymous access type. Do not install the check
2494 -- when the Lhs denotes a container cursor and the Next function employs
2495 -- an access type, because this can never result in a dangling pointer.
2496
2497 if Is_Access_Type (Typ)
2498 and then Is_Entity_Name (Lhs)
2499 and then Ekind (Entity (Lhs)) /= E_Loop_Parameter
2500 and then Present (Effective_Extra_Accessibility (Entity (Lhs)))
2501 then
2502 declare
2503 function Lhs_Entity return Entity_Id;
2504 -- Look through renames to find the underlying entity.
2505 -- For assignment to a rename, we don't care about the
2506 -- Enclosing_Dynamic_Scope of the rename declaration.
2507
2508 ----------------
2509 -- Lhs_Entity --
2510 ----------------
2511
2512 function Lhs_Entity return Entity_Id is
2513 Result : Entity_Id := Entity (Lhs);
2514
2515 begin
2516 while Present (Renamed_Object (Result)) loop
2517
2518 -- Renamed_Object must return an Entity_Name here
2519 -- because of preceding "Present (E_E_A (...))" test.
2520
2521 Result := Entity (Renamed_Object (Result));
2522 end loop;
2523
2524 return Result;
2525 end Lhs_Entity;
2526
2527 -- Local Declarations
2528
2529 Access_Check : constant Node_Id :=
2530 Make_Raise_Program_Error (Loc,
2531 Condition =>
2532 Make_Op_Gt (Loc,
2533 Left_Opnd =>
2534 Accessibility_Level (Rhs, Dynamic_Level),
2535 Right_Opnd =>
2536 Make_Integer_Literal (Loc,
2537 Intval =>
2538 Scope_Depth
2539 (Enclosing_Dynamic_Scope
2540 (Lhs_Entity)))),
2541 Reason => PE_Accessibility_Check_Failed);
2542
2543 Access_Level_Update : constant Node_Id :=
2544 Make_Assignment_Statement (Loc,
2545 Name =>
2546 New_Occurrence_Of
2547 (Effective_Extra_Accessibility
2548 (Entity (Lhs)), Loc),
2549 Expression =>
2550 Accessibility_Level
2551 (Rhs, Dynamic_Level));
2552
2553 begin
2554 if not Accessibility_Checks_Suppressed (Entity (Lhs)) then
2555 Insert_Action (N, Access_Check);
2556 end if;
2557
2558 Insert_Action (N, Access_Level_Update);
2559 end;
2560 end if;
2561
2562 -- Case of assignment to a bit packed array element. If there is a
2563 -- change of representation this must be expanded into components,
2564 -- otherwise this is a bit-field assignment.
2565
2566 if Nkind (Lhs) = N_Indexed_Component
2567 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
2568 then
2569 -- Normal case, no change of representation
2570
2571 if not Crep then
2572 Expand_Bit_Packed_Element_Set (N);
2573 return;
2574
2575 -- Change of representation case
2576
2577 else
2578 -- Generate the following, to force component-by-component
2579 -- assignments in an efficient way. Otherwise each component
2580 -- will require a temporary and two bit-field manipulations.
2581
2582 -- T1 : Elmt_Type;
2583 -- T1 := RhS;
2584 -- Lhs := T1;
2585
2586 declare
2587 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2588 Stats : List_Id;
2589
2590 begin
2591 Stats :=
2592 New_List (
2593 Make_Object_Declaration (Loc,
2594 Defining_Identifier => Tnn,
2595 Object_Definition =>
2596 New_Occurrence_Of (Etype (Lhs), Loc)),
2597 Make_Assignment_Statement (Loc,
2598 Name => New_Occurrence_Of (Tnn, Loc),
2599 Expression => Relocate_Node (Rhs)),
2600 Make_Assignment_Statement (Loc,
2601 Name => Relocate_Node (Lhs),
2602 Expression => New_Occurrence_Of (Tnn, Loc)));
2603
2604 Insert_Actions (N, Stats);
2605 Rewrite (N, Make_Null_Statement (Loc));
2606 Analyze (N);
2607 end;
2608 end if;
2609
2610 -- Build-in-place function call case. This is for assignment statements
2611 -- that come from aggregate component associations or from init procs.
2612 -- User-written assignment statements with b-i-p calls are handled
2613 -- elsewhere.
2614
2615 elsif Is_Build_In_Place_Function_Call (Rhs) then
2616 pragma Assert (not Comes_From_Source (N));
2617 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
2618
2619 elsif Is_Tagged_Type (Typ)
2620 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
2621 then
2622 Tagged_Case : declare
2623 L : List_Id := No_List;
2624 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
2625
2626 begin
2627 -- In the controlled case, we ensure that function calls are
2628 -- evaluated before finalizing the target. In all cases, it makes
2629 -- the expansion easier if the side effects are removed first.
2630
2631 Remove_Side_Effects (Lhs);
2632 Remove_Side_Effects (Rhs);
2633
2634 -- Avoid recursion in the mechanism
2635
2636 Set_Analyzed (N);
2637
2638 -- If dispatching assignment, we need to dispatch to _assign
2639
2640 if Is_Class_Wide_Type (Typ)
2641
2642 -- If the type is tagged, we may as well use the predefined
2643 -- primitive assignment. This avoids inlining a lot of code
2644 -- and in the class-wide case, the assignment is replaced
2645 -- by a dispatching call to _assign. It is suppressed in the
2646 -- case of assignments created by the expander that correspond
2647 -- to initializations, where we do want to copy the tag
2648 -- (Expand_Ctrl_Actions flag is set False in this case). It is
2649 -- also suppressed if restriction No_Dispatching_Calls is in
2650 -- force because in that case predefined primitives are not
2651 -- generated.
2652
2653 or else (Is_Tagged_Type (Typ)
2654 and then Chars (Current_Scope) /= Name_uAssign
2655 and then Expand_Ctrl_Actions
2656 and then
2657 not Restriction_Active (No_Dispatching_Calls))
2658 then
2659 -- We should normally not encounter any limited type here,
2660 -- except in the corner case where an assignment was not
2661 -- intended like the pathological case of a raise expression
2662 -- within a return statement.
2663
2664 if Is_Limited_Type (Typ) then
2665 pragma Assert (not Comes_From_Source (N));
2666 return;
2667 end if;
2668
2669 -- Fetch the primitive op _assign and proper type to call it.
2670 -- Because of possible conflicts between private and full view,
2671 -- fetch the proper type directly from the operation profile.
2672
2673 declare
2674 Op : constant Entity_Id :=
2675 Find_Prim_Op (Typ, Name_uAssign);
2676 F_Typ : Entity_Id := Etype (First_Formal (Op));
2677
2678 begin
2679 -- If the assignment is dispatching, make sure to use the
2680 -- proper type.
2681
2682 if Is_Class_Wide_Type (Typ) then
2683 F_Typ := Class_Wide_Type (F_Typ);
2684 end if;
2685
2686 L := New_List;
2687
2688 -- In case of assignment to a class-wide tagged type, before
2689 -- the assignment we generate run-time check to ensure that
2690 -- the tags of source and target match.
2691
2692 if not Tag_Checks_Suppressed (Typ)
2693 and then Is_Class_Wide_Type (Typ)
2694 and then Is_Tagged_Type (Typ)
2695 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
2696 then
2697 declare
2698 Lhs_Tag : Node_Id;
2699 Rhs_Tag : Node_Id;
2700
2701 begin
2702 if not Is_Interface (Typ) then
2703 Lhs_Tag :=
2704 Make_Selected_Component (Loc,
2705 Prefix => Duplicate_Subexpr (Lhs),
2706 Selector_Name =>
2707 Make_Identifier (Loc, Name_uTag));
2708 Rhs_Tag :=
2709 Make_Selected_Component (Loc,
2710 Prefix => Duplicate_Subexpr (Rhs),
2711 Selector_Name =>
2712 Make_Identifier (Loc, Name_uTag));
2713 else
2714 -- Displace the pointer to the base of the objects
2715 -- applying 'Address, which is later expanded into
2716 -- a call to RE_Base_Address.
2717
2718 Lhs_Tag :=
2719 Make_Explicit_Dereference (Loc,
2720 Prefix =>
2721 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
2722 Make_Attribute_Reference (Loc,
2723 Prefix => Duplicate_Subexpr (Lhs),
2724 Attribute_Name => Name_Address)));
2725 Rhs_Tag :=
2726 Make_Explicit_Dereference (Loc,
2727 Prefix =>
2728 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
2729 Make_Attribute_Reference (Loc,
2730 Prefix => Duplicate_Subexpr (Rhs),
2731 Attribute_Name => Name_Address)));
2732 end if;
2733
2734 Append_To (L,
2735 Make_Raise_Constraint_Error (Loc,
2736 Condition =>
2737 Make_Op_Ne (Loc,
2738 Left_Opnd => Lhs_Tag,
2739 Right_Opnd => Rhs_Tag),
2740 Reason => CE_Tag_Check_Failed));
2741 end;
2742 end if;
2743
2744 declare
2745 Left_N : Node_Id := Duplicate_Subexpr (Lhs);
2746 Right_N : Node_Id := Duplicate_Subexpr (Rhs);
2747
2748 begin
2749 -- In order to dispatch the call to _assign the type of
2750 -- the actuals must match. Add conversion (if required).
2751
2752 if Etype (Lhs) /= F_Typ then
2753 Left_N := Unchecked_Convert_To (F_Typ, Left_N);
2754 end if;
2755
2756 if Etype (Rhs) /= F_Typ then
2757 Right_N := Unchecked_Convert_To (F_Typ, Right_N);
2758 end if;
2759
2760 Append_To (L,
2761 Make_Procedure_Call_Statement (Loc,
2762 Name => New_Occurrence_Of (Op, Loc),
2763 Parameter_Associations => New_List (
2764 Node1 => Left_N,
2765 Node2 => Right_N)));
2766 end;
2767 end;
2768
2769 else
2770 L := Make_Tag_Ctrl_Assignment (N);
2771
2772 -- We can't afford to have destructive Finalization Actions in
2773 -- the Self assignment case, so if the target and the source
2774 -- are not obviously different, code is generated to avoid the
2775 -- self assignment case:
2776
2777 -- if lhs'address /= rhs'address then
2778 -- <code for controlled and/or tagged assignment>
2779 -- end if;
2780
2781 -- Skip this if Restriction (No_Finalization) is active
2782
2783 if not Statically_Different (Lhs, Rhs)
2784 and then Expand_Ctrl_Actions
2785 and then not Restriction_Active (No_Finalization)
2786 then
2787 L := New_List (
2788 Make_Implicit_If_Statement (N,
2789 Condition =>
2790 Make_Op_Ne (Loc,
2791 Left_Opnd =>
2792 Make_Attribute_Reference (Loc,
2793 Prefix => Duplicate_Subexpr (Lhs),
2794 Attribute_Name => Name_Address),
2795
2796 Right_Opnd =>
2797 Make_Attribute_Reference (Loc,
2798 Prefix => Duplicate_Subexpr (Rhs),
2799 Attribute_Name => Name_Address)),
2800
2801 Then_Statements => L));
2802 end if;
2803
2804 -- We need to set up an exception handler for implementing
2805 -- 7.6.1(18). The remaining adjustments are tackled by the
2806 -- implementation of adjust for record_controllers (see
2807 -- s-finimp.adb).
2808
2809 -- This is skipped if we have no finalization
2810
2811 if Expand_Ctrl_Actions
2812 and then not Restriction_Active (No_Finalization)
2813 then
2814 L := New_List (
2815 Make_Block_Statement (Loc,
2816 Handled_Statement_Sequence =>
2817 Make_Handled_Sequence_Of_Statements (Loc,
2818 Statements => L,
2819 Exception_Handlers => New_List (
2820 Make_Handler_For_Ctrl_Operation (Loc)))));
2821 end if;
2822 end if;
2823
2824 Rewrite (N,
2825 Make_Block_Statement (Loc,
2826 Handled_Statement_Sequence =>
2827 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
2828
2829 -- If no restrictions on aborts, protect the whole assignment
2830 -- for controlled objects as per 9.8(11).
2831
2832 if Needs_Finalization (Typ)
2833 and then Expand_Ctrl_Actions
2834 and then Abort_Allowed
2835 then
2836 declare
2837 Blk : constant Entity_Id :=
2838 New_Internal_Entity
2839 (E_Block, Current_Scope, Sloc (N), 'B');
2840 AUD : constant Entity_Id := RTE (RE_Abort_Undefer_Direct);
2841
2842 begin
2843 Set_Is_Abort_Block (N);
2844
2845 Set_Scope (Blk, Current_Scope);
2846 Set_Etype (Blk, Standard_Void_Type);
2847 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
2848
2849 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
2850 Set_At_End_Proc (Handled_Statement_Sequence (N),
2851 New_Occurrence_Of (AUD, Loc));
2852
2853 -- Present the Abort_Undefer_Direct function to the backend
2854 -- so that it can inline the call to the function.
2855
2856 Add_Inlined_Body (AUD, N);
2857
2858 Expand_At_End_Handler
2859 (Handled_Statement_Sequence (N), Blk);
2860 end;
2861 end if;
2862
2863 -- N has been rewritten to a block statement for which it is
2864 -- known by construction that no checks are necessary: analyze
2865 -- it with all checks suppressed.
2866
2867 Analyze (N, Suppress => All_Checks);
2868 return;
2869 end Tagged_Case;
2870
2871 -- Array types
2872
2873 elsif Is_Array_Type (Typ) then
2874 declare
2875 Actual_Rhs : Node_Id := Rhs;
2876
2877 begin
2878 while Nkind (Actual_Rhs) in
2879 N_Type_Conversion | N_Qualified_Expression
2880 loop
2881 Actual_Rhs := Expression (Actual_Rhs);
2882 end loop;
2883
2884 Expand_Assign_Array (N, Actual_Rhs);
2885 return;
2886 end;
2887
2888 -- Record types
2889
2890 elsif Is_Record_Type (Typ) then
2891 Expand_Assign_Record (N);
2892 return;
2893
2894 -- Scalar types. This is where we perform the processing related to the
2895 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2896 -- scalar values.
2897
2898 elsif Is_Scalar_Type (Typ) then
2899
2900 -- Case where right side is known valid
2901
2902 if Expr_Known_Valid (Rhs) then
2903
2904 -- Here the right side is valid, so it is fine. The case to deal
2905 -- with is when the left side is a local variable reference whose
2906 -- value is not currently known to be valid. If this is the case,
2907 -- and the assignment appears in an unconditional context, then
2908 -- we can mark the left side as now being valid if one of these
2909 -- conditions holds:
2910
2911 -- The expression of the right side has Do_Range_Check set so
2912 -- that we know a range check will be performed. Note that it
2913 -- can be the case that a range check is omitted because we
2914 -- make the assumption that we can assume validity for operands
2915 -- appearing in the right side in determining whether a range
2916 -- check is required
2917
2918 -- The subtype of the right side matches the subtype of the
2919 -- left side. In this case, even though we have not checked
2920 -- the range of the right side, we know it is in range of its
2921 -- subtype if the expression is valid.
2922
2923 if Is_Local_Variable_Reference (Lhs)
2924 and then not Is_Known_Valid (Entity (Lhs))
2925 and then In_Unconditional_Context (N)
2926 then
2927 if Do_Range_Check (Rhs)
2928 or else Etype (Lhs) = Etype (Rhs)
2929 then
2930 Set_Is_Known_Valid (Entity (Lhs), True);
2931 end if;
2932 end if;
2933
2934 -- Case where right side may be invalid in the sense of the RM
2935 -- reference above. The RM does not require that we check for the
2936 -- validity on an assignment, but it does require that the assignment
2937 -- of an invalid value not cause erroneous behavior.
2938
2939 -- The general approach in GNAT is to use the Is_Known_Valid flag
2940 -- to avoid the need for validity checking on assignments. However
2941 -- in some cases, we have to do validity checking in order to make
2942 -- sure that the setting of this flag is correct.
2943
2944 else
2945 -- Validate right side if we are validating copies
2946
2947 if Validity_Checks_On
2948 and then Validity_Check_Copies
2949 then
2950 -- Skip this if left-hand side is an array or record component
2951 -- and elementary component validity checks are suppressed.
2952
2953 if Nkind (Lhs) in N_Selected_Component | N_Indexed_Component
2954 and then not Validity_Check_Components
2955 then
2956 null;
2957 else
2958 Ensure_Valid (Rhs);
2959 end if;
2960
2961 -- We can propagate this to the left side where appropriate
2962
2963 if Is_Local_Variable_Reference (Lhs)
2964 and then not Is_Known_Valid (Entity (Lhs))
2965 and then In_Unconditional_Context (N)
2966 then
2967 Set_Is_Known_Valid (Entity (Lhs), True);
2968 end if;
2969
2970 -- Otherwise check to see what should be done
2971
2972 -- If left side is a local variable, then we just set its flag to
2973 -- indicate that its value may no longer be valid, since we are
2974 -- copying a potentially invalid value.
2975
2976 elsif Is_Local_Variable_Reference (Lhs) then
2977 Set_Is_Known_Valid (Entity (Lhs), False);
2978
2979 -- Check for case of a nonlocal variable on the left side which
2980 -- is currently known to be valid. In this case, we simply ensure
2981 -- that the right side is valid. We only play the game of copying
2982 -- validity status for local variables, since we are doing this
2983 -- statically, not by tracing the full flow graph.
2984
2985 elsif Is_Entity_Name (Lhs)
2986 and then Is_Known_Valid (Entity (Lhs))
2987 then
2988 -- Note: If Validity_Checking mode is set to none, we ignore
2989 -- the Ensure_Valid call so don't worry about that case here.
2990
2991 Ensure_Valid (Rhs);
2992
2993 -- In all other cases, we can safely copy an invalid value without
2994 -- worrying about the status of the left side. Since it is not a
2995 -- variable reference it will not be considered
2996 -- as being known to be valid in any case.
2997
2998 else
2999 null;
3000 end if;
3001 end if;
3002 end if;
3003
3004 exception
3005 when RE_Not_Available =>
3006 return;
3007 end Expand_N_Assignment_Statement;
3008
3009 ------------------------------
3010 -- Expand_N_Block_Statement --
3011 ------------------------------
3012
3013 -- Encode entity names defined in block statement
3014
3015 procedure Expand_N_Block_Statement (N : Node_Id) is
3016 begin
3017 Qualify_Entity_Names (N);
3018 end Expand_N_Block_Statement;
3019
3020 -----------------------------
3021 -- Expand_N_Case_Statement --
3022 -----------------------------
3023
3024 procedure Expand_N_Case_Statement (N : Node_Id) is
3025 Loc : constant Source_Ptr := Sloc (N);
3026 Expr : constant Node_Id := Expression (N);
3027 From_Cond_Expr : constant Boolean := From_Conditional_Expression (N);
3028 Alt : Node_Id;
3029 Len : Nat;
3030 Cond : Node_Id;
3031 Choice : Node_Id;
3032 Chlist : List_Id;
3033
3034 begin
3035 -- Check for the situation where we know at compile time which branch
3036 -- will be taken.
3037
3038 -- If the value is static but its subtype is predicated and the value
3039 -- does not obey the predicate, the value is marked non-static, and
3040 -- there can be no corresponding static alternative. In that case we
3041 -- replace the case statement with an exception, regardless of whether
3042 -- assertions are enabled or not, unless predicates are ignored.
3043
3044 if Compile_Time_Known_Value (Expr)
3045 and then Has_Predicates (Etype (Expr))
3046 and then not Predicates_Ignored (Etype (Expr))
3047 and then not Is_OK_Static_Expression (Expr)
3048 then
3049 Rewrite (N,
3050 Make_Raise_Constraint_Error (Loc, Reason => CE_Invalid_Data));
3051 Analyze (N);
3052 return;
3053
3054 elsif Compile_Time_Known_Value (Expr)
3055 and then (not Has_Predicates (Etype (Expr))
3056 or else Is_Static_Expression (Expr))
3057 then
3058 Alt := Find_Static_Alternative (N);
3059
3060 -- Do not consider controlled objects found in a case statement which
3061 -- actually models a case expression because their early finalization
3062 -- will affect the result of the expression.
3063
3064 if not From_Conditional_Expression (N) then
3065 Process_Statements_For_Controlled_Objects (Alt);
3066 end if;
3067
3068 -- Move statements from this alternative after the case statement.
3069 -- They are already analyzed, so will be skipped by the analyzer.
3070
3071 Insert_List_After (N, Statements (Alt));
3072
3073 -- That leaves the case statement as a shell. So now we can kill all
3074 -- other alternatives in the case statement.
3075
3076 Kill_Dead_Code (Expression (N));
3077
3078 declare
3079 Dead_Alt : Node_Id;
3080
3081 begin
3082 -- Loop through case alternatives, skipping pragmas, and skipping
3083 -- the one alternative that we select (and therefore retain).
3084
3085 Dead_Alt := First (Alternatives (N));
3086 while Present (Dead_Alt) loop
3087 if Dead_Alt /= Alt
3088 and then Nkind (Dead_Alt) = N_Case_Statement_Alternative
3089 then
3090 Kill_Dead_Code (Statements (Dead_Alt), Warn_On_Deleted_Code);
3091 end if;
3092
3093 Next (Dead_Alt);
3094 end loop;
3095 end;
3096
3097 Rewrite (N, Make_Null_Statement (Loc));
3098 return;
3099 end if;
3100
3101 -- Here if the choice is not determined at compile time
3102
3103 declare
3104 Last_Alt : constant Node_Id := Last (Alternatives (N));
3105
3106 Others_Present : Boolean;
3107 Others_Node : Node_Id;
3108
3109 Then_Stms : List_Id;
3110 Else_Stms : List_Id;
3111
3112 begin
3113 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
3114 Others_Present := True;
3115 Others_Node := Last_Alt;
3116 else
3117 Others_Present := False;
3118 end if;
3119
3120 -- First step is to worry about possible invalid argument. The RM
3121 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
3122 -- outside the base range), then Constraint_Error must be raised.
3123
3124 -- Case of validity check required (validity checks are on, the
3125 -- expression is not known to be valid, and the case statement
3126 -- comes from source -- no need to validity check internally
3127 -- generated case statements).
3128
3129 if Validity_Check_Default
3130 and then not Predicates_Ignored (Etype (Expr))
3131 then
3132 -- Recognize the simple case where Expr is an object reference
3133 -- and the case statement is directly preceded by an
3134 -- "if Obj'Valid then": in this case, do not emit another validity
3135 -- check.
3136
3137 declare
3138 Check_Validity : Boolean := True;
3139 Attr : Node_Id;
3140 begin
3141 if Nkind (Expr) = N_Identifier
3142 and then Nkind (Parent (N)) = N_If_Statement
3143 and then Nkind (Original_Node (Condition (Parent (N))))
3144 = N_Attribute_Reference
3145 and then No (Prev (N))
3146 then
3147 Attr := Original_Node (Condition (Parent (N)));
3148
3149 if Attribute_Name (Attr) = Name_Valid
3150 and then Nkind (Prefix (Attr)) = N_Identifier
3151 and then Entity (Prefix (Attr)) = Entity (Expr)
3152 then
3153 Check_Validity := False;
3154 end if;
3155 end if;
3156
3157 if Check_Validity then
3158 Ensure_Valid (Expr);
3159 end if;
3160 end;
3161 end if;
3162
3163 -- If there is only a single alternative, just replace it with the
3164 -- sequence of statements since obviously that is what is going to
3165 -- be executed in all cases.
3166
3167 Len := List_Length (Alternatives (N));
3168
3169 if Len = 1 then
3170
3171 -- We still need to evaluate the expression if it has any side
3172 -- effects.
3173
3174 Remove_Side_Effects (Expression (N));
3175 Alt := First (Alternatives (N));
3176
3177 -- Do not consider controlled objects found in a case statement
3178 -- which actually models a case expression because their early
3179 -- finalization will affect the result of the expression.
3180
3181 if not From_Conditional_Expression (N) then
3182 Process_Statements_For_Controlled_Objects (Alt);
3183 end if;
3184
3185 Insert_List_After (N, Statements (Alt));
3186
3187 -- That leaves the case statement as a shell. The alternative that
3188 -- will be executed is reset to a null list. So now we can kill
3189 -- the entire case statement.
3190
3191 Kill_Dead_Code (Expression (N));
3192 Rewrite (N, Make_Null_Statement (Loc));
3193 return;
3194
3195 -- An optimization. If there are only two alternatives, and only
3196 -- a single choice, then rewrite the whole case statement as an
3197 -- if statement, since this can result in subsequent optimizations.
3198 -- This helps not only with case statements in the source of a
3199 -- simple form, but also with generated code (discriminant check
3200 -- functions in particular).
3201
3202 -- Note: it is OK to do this before expanding out choices for any
3203 -- static predicates, since the if statement processing will handle
3204 -- the static predicate case fine.
3205
3206 elsif Len = 2 then
3207 Chlist := Discrete_Choices (First (Alternatives (N)));
3208
3209 if List_Length (Chlist) = 1 then
3210 Choice := First (Chlist);
3211
3212 Then_Stms := Statements (First (Alternatives (N)));
3213 Else_Stms := Statements (Last (Alternatives (N)));
3214
3215 -- For TRUE, generate "expression", not expression = true
3216
3217 if Nkind (Choice) = N_Identifier
3218 and then Entity (Choice) = Standard_True
3219 then
3220 Cond := Expression (N);
3221
3222 -- For FALSE, generate "expression" and switch then/else
3223
3224 elsif Nkind (Choice) = N_Identifier
3225 and then Entity (Choice) = Standard_False
3226 then
3227 Cond := Expression (N);
3228 Else_Stms := Statements (First (Alternatives (N)));
3229 Then_Stms := Statements (Last (Alternatives (N)));
3230
3231 -- For a range, generate "expression in range"
3232
3233 elsif Nkind (Choice) = N_Range
3234 or else (Nkind (Choice) = N_Attribute_Reference
3235 and then Attribute_Name (Choice) = Name_Range)
3236 or else (Is_Entity_Name (Choice)
3237 and then Is_Type (Entity (Choice)))
3238 then
3239 Cond :=
3240 Make_In (Loc,
3241 Left_Opnd => Expression (N),
3242 Right_Opnd => Relocate_Node (Choice));
3243
3244 -- A subtype indication is not a legal operator in a membership
3245 -- test, so retrieve its range.
3246
3247 elsif Nkind (Choice) = N_Subtype_Indication then
3248 Cond :=
3249 Make_In (Loc,
3250 Left_Opnd => Expression (N),
3251 Right_Opnd =>
3252 Relocate_Node
3253 (Range_Expression (Constraint (Choice))));
3254
3255 -- For any other subexpression "expression = value"
3256
3257 else
3258 Cond :=
3259 Make_Op_Eq (Loc,
3260 Left_Opnd => Expression (N),
3261 Right_Opnd => Relocate_Node (Choice));
3262 end if;
3263
3264 -- Now rewrite the case as an IF
3265
3266 Rewrite (N,
3267 Make_If_Statement (Loc,
3268 Condition => Cond,
3269 Then_Statements => Then_Stms,
3270 Else_Statements => Else_Stms));
3271
3272 -- The rewritten if statement needs to inherit whether the
3273 -- case statement was expanded from a conditional expression,
3274 -- for proper handling of nested controlled objects.
3275
3276 Set_From_Conditional_Expression (N, From_Cond_Expr);
3277
3278 Analyze (N);
3279
3280 return;
3281 end if;
3282 end if;
3283
3284 -- If the last alternative is not an Others choice, replace it with
3285 -- an N_Others_Choice. Note that we do not bother to call Analyze on
3286 -- the modified case statement, since it's only effect would be to
3287 -- compute the contents of the Others_Discrete_Choices which is not
3288 -- needed by the back end anyway.
3289
3290 -- The reason for this is that the back end always needs some default
3291 -- for a switch, so if we have not supplied one in the processing
3292 -- above for validity checking, then we need to supply one here.
3293
3294 if not Others_Present then
3295 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
3296
3297 -- If Predicates_Ignored is true the value does not satisfy the
3298 -- predicate, and there is no Others choice, Constraint_Error
3299 -- must be raised (4.5.7 (21/3)).
3300
3301 if Predicates_Ignored (Etype (Expr)) then
3302 declare
3303 Except : constant Node_Id :=
3304 Make_Raise_Constraint_Error (Loc,
3305 Reason => CE_Invalid_Data);
3306 New_Alt : constant Node_Id :=
3307 Make_Case_Statement_Alternative (Loc,
3308 Discrete_Choices => New_List (
3309 Make_Others_Choice (Loc)),
3310 Statements => New_List (Except));
3311
3312 begin
3313 Append (New_Alt, Alternatives (N));
3314 Analyze_And_Resolve (Except);
3315 end;
3316
3317 else
3318 Set_Others_Discrete_Choices
3319 (Others_Node, Discrete_Choices (Last_Alt));
3320 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
3321 end if;
3322
3323 end if;
3324
3325 -- Deal with possible declarations of controlled objects, and also
3326 -- with rewriting choice sequences for static predicate references.
3327
3328 Alt := First_Non_Pragma (Alternatives (N));
3329 while Present (Alt) loop
3330
3331 -- Do not consider controlled objects found in a case statement
3332 -- which actually models a case expression because their early
3333 -- finalization will affect the result of the expression.
3334
3335 if not From_Conditional_Expression (N) then
3336 Process_Statements_For_Controlled_Objects (Alt);
3337 end if;
3338
3339 if Has_SP_Choice (Alt) then
3340 Expand_Static_Predicates_In_Choices (Alt);
3341 end if;
3342
3343 Next_Non_Pragma (Alt);
3344 end loop;
3345 end;
3346 end Expand_N_Case_Statement;
3347
3348 -----------------------------
3349 -- Expand_N_Exit_Statement --
3350 -----------------------------
3351
3352 -- The only processing required is to deal with a possible C/Fortran
3353 -- boolean value used as the condition for the exit statement.
3354
3355 procedure Expand_N_Exit_Statement (N : Node_Id) is
3356 begin
3357 Adjust_Condition (Condition (N));
3358 end Expand_N_Exit_Statement;
3359
3360 ----------------------------------
3361 -- Expand_Formal_Container_Loop --
3362 ----------------------------------
3363
3364 procedure Expand_Formal_Container_Loop (N : Node_Id) is
3365 Loc : constant Source_Ptr := Sloc (N);
3366 Isc : constant Node_Id := Iteration_Scheme (N);
3367 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3368 Cursor : constant Entity_Id := Defining_Identifier (I_Spec);
3369 Container : constant Node_Id := Entity (Name (I_Spec));
3370 Stats : constant List_Id := Statements (N);
3371
3372 Advance : Node_Id;
3373 Init_Decl : Node_Id;
3374 Init_Name : Entity_Id;
3375 New_Loop : Node_Id;
3376
3377 begin
3378 -- The expansion of a formal container loop resembles the one for Ada
3379 -- containers. The only difference is that the primitives mention the
3380 -- domain of iteration explicitly, and function First applied to the
3381 -- container yields a cursor directly.
3382
3383 -- Cursor : Cursor_type := First (Container);
3384 -- while Has_Element (Cursor, Container) loop
3385 -- <original loop statements>
3386 -- Cursor := Next (Container, Cursor);
3387 -- end loop;
3388
3389 Build_Formal_Container_Iteration
3390 (N, Container, Cursor, Init_Decl, Advance, New_Loop);
3391
3392 Append_To (Stats, Advance);
3393
3394 -- Build a block to capture declaration of the cursor
3395
3396 Rewrite (N,
3397 Make_Block_Statement (Loc,
3398 Declarations => New_List (Init_Decl),
3399 Handled_Statement_Sequence =>
3400 Make_Handled_Sequence_Of_Statements (Loc,
3401 Statements => New_List (New_Loop))));
3402
3403 -- The loop parameter is declared by an object declaration, but within
3404 -- the loop we must prevent user assignments to it, so we analyze the
3405 -- declaration and reset the entity kind, before analyzing the rest of
3406 -- the loop.
3407
3408 Analyze (Init_Decl);
3409 Init_Name := Defining_Identifier (Init_Decl);
3410 Set_Ekind (Init_Name, E_Loop_Parameter);
3411
3412 -- The cursor was marked as a loop parameter to prevent user assignments
3413 -- to it, however this renders the advancement step illegal as it is not
3414 -- possible to change the value of a constant. Flag the advancement step
3415 -- as a legal form of assignment to remedy this side effect.
3416
3417 Set_Assignment_OK (Name (Advance));
3418 Analyze (N);
3419
3420 -- Because we have to analyze the initial declaration of the loop
3421 -- parameter multiple times its scope is incorrectly set at this point
3422 -- to the one surrounding the block statement - so set the scope
3423 -- manually to be the actual block statement, and indicate that it is
3424 -- not visible after the block has been analyzed.
3425
3426 Set_Scope (Init_Name, Entity (Identifier (N)));
3427 Set_Is_Immediately_Visible (Init_Name, False);
3428 end Expand_Formal_Container_Loop;
3429
3430 ------------------------------------------
3431 -- Expand_Formal_Container_Element_Loop --
3432 ------------------------------------------
3433
3434 procedure Expand_Formal_Container_Element_Loop (N : Node_Id) is
3435 Loc : constant Source_Ptr := Sloc (N);
3436 Isc : constant Node_Id := Iteration_Scheme (N);
3437 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3438 Element : constant Entity_Id := Defining_Identifier (I_Spec);
3439 Container : constant Node_Id := Entity (Name (I_Spec));
3440 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
3441 Stats : constant List_Id := Statements (N);
3442
3443 Cursor : constant Entity_Id :=
3444 Make_Defining_Identifier (Loc,
3445 Chars => New_External_Name (Chars (Element), 'C'));
3446 Elmt_Decl : Node_Id;
3447 Elmt_Ref : Node_Id;
3448
3449 Element_Op : constant Entity_Id :=
3450 Get_Iterable_Type_Primitive (Container_Typ, Name_Element);
3451
3452 Advance : Node_Id;
3453 Init : Node_Id;
3454 New_Loop : Node_Id;
3455
3456 begin
3457 -- For an element iterator, the Element aspect must be present,
3458 -- (this is checked during analysis) and the expansion takes the form:
3459
3460 -- Cursor : Cursor_Type := First (Container);
3461 -- Elmt : Element_Type;
3462 -- while Has_Element (Cursor, Container) loop
3463 -- Elmt := Element (Container, Cursor);
3464 -- <original loop statements>
3465 -- Cursor := Next (Container, Cursor);
3466 -- end loop;
3467
3468 -- However this expansion is not legal if the element is indefinite.
3469 -- In that case we create a block to hold a variable declaration
3470 -- initialized with a call to Element, and generate:
3471
3472 -- Cursor : Cursor_Type := First (Container);
3473 -- while Has_Element (Cursor, Container) loop
3474 -- declare
3475 -- Elmt : Element_Type := Element (Container, Cursor);
3476 -- begin
3477 -- <original loop statements>
3478 -- Cursor := Next (Container, Cursor);
3479 -- end;
3480 -- end loop;
3481
3482 Build_Formal_Container_Iteration
3483 (N, Container, Cursor, Init, Advance, New_Loop);
3484 Append_To (Stats, Advance);
3485
3486 Set_Ekind (Cursor, E_Variable);
3487 Insert_Action (N, Init);
3488
3489 -- The loop parameter is declared by an object declaration, but within
3490 -- the loop we must prevent user assignments to it; the following flag
3491 -- accomplishes that.
3492
3493 Set_Is_Loop_Parameter (Element);
3494
3495 -- Declaration for Element
3496
3497 Elmt_Decl :=
3498 Make_Object_Declaration (Loc,
3499 Defining_Identifier => Element,
3500 Object_Definition => New_Occurrence_Of (Etype (Element_Op), Loc));
3501
3502 if not Is_Constrained (Etype (Element_Op)) then
3503 Set_Expression (Elmt_Decl,
3504 Make_Function_Call (Loc,
3505 Name => New_Occurrence_Of (Element_Op, Loc),
3506 Parameter_Associations => New_List (
3507 Convert_To_Iterable_Type (Container, Loc),
3508 New_Occurrence_Of (Cursor, Loc))));
3509
3510 Set_Statements (New_Loop,
3511 New_List
3512 (Make_Block_Statement (Loc,
3513 Declarations => New_List (Elmt_Decl),
3514 Handled_Statement_Sequence =>
3515 Make_Handled_Sequence_Of_Statements (Loc,
3516 Statements => Stats))));
3517
3518 else
3519 Elmt_Ref :=
3520 Make_Assignment_Statement (Loc,
3521 Name => New_Occurrence_Of (Element, Loc),
3522 Expression =>
3523 Make_Function_Call (Loc,
3524 Name => New_Occurrence_Of (Element_Op, Loc),
3525 Parameter_Associations => New_List (
3526 Convert_To_Iterable_Type (Container, Loc),
3527 New_Occurrence_Of (Cursor, Loc))));
3528
3529 Prepend (Elmt_Ref, Stats);
3530
3531 -- The element is assignable in the expanded code
3532
3533 Set_Assignment_OK (Name (Elmt_Ref));
3534
3535 -- The loop is rewritten as a block, to hold the element declaration
3536
3537 New_Loop :=
3538 Make_Block_Statement (Loc,
3539 Declarations => New_List (Elmt_Decl),
3540 Handled_Statement_Sequence =>
3541 Make_Handled_Sequence_Of_Statements (Loc,
3542 Statements => New_List (New_Loop)));
3543 end if;
3544
3545 -- The element is only modified in expanded code, so it appears as
3546 -- unassigned to the warning machinery. We must suppress this spurious
3547 -- warning explicitly.
3548
3549 Set_Warnings_Off (Element);
3550
3551 Rewrite (N, New_Loop);
3552 Analyze (N);
3553 end Expand_Formal_Container_Element_Loop;
3554
3555 ---------------------------
3556 -- Expand_N_If_Statement --
3557 ---------------------------
3558
3559 -- First we deal with the case of C and Fortran convention boolean values,
3560 -- with zero/non-zero semantics.
3561
3562 -- Second, we deal with the obvious rewriting for the cases where the
3563 -- condition of the IF is known at compile time to be True or False.
3564
3565 -- Third, we remove elsif parts which have non-empty Condition_Actions and
3566 -- rewrite as independent if statements. For example:
3567
3568 -- if x then xs
3569 -- elsif y then ys
3570 -- ...
3571 -- end if;
3572
3573 -- becomes
3574 --
3575 -- if x then xs
3576 -- else
3577 -- <<condition actions of y>>
3578 -- if y then ys
3579 -- ...
3580 -- end if;
3581 -- end if;
3582
3583 -- This rewriting is needed if at least one elsif part has a non-empty
3584 -- Condition_Actions list. We also do the same processing if there is a
3585 -- constant condition in an elsif part (in conjunction with the first
3586 -- processing step mentioned above, for the recursive call made to deal
3587 -- with the created inner if, this deals with properly optimizing the
3588 -- cases of constant elsif conditions).
3589
3590 procedure Expand_N_If_Statement (N : Node_Id) is
3591 Loc : constant Source_Ptr := Sloc (N);
3592 Hed : Node_Id;
3593 E : Node_Id;
3594 New_If : Node_Id;
3595
3596 Warn_If_Deleted : constant Boolean :=
3597 Warn_On_Deleted_Code and then Comes_From_Source (N);
3598 -- Indicates whether we want warnings when we delete branches of the
3599 -- if statement based on constant condition analysis. We never want
3600 -- these warnings for expander generated code.
3601
3602 begin
3603 -- Do not consider controlled objects found in an if statement which
3604 -- actually models an if expression because their early finalization
3605 -- will affect the result of the expression.
3606
3607 if not From_Conditional_Expression (N) then
3608 Process_Statements_For_Controlled_Objects (N);
3609 end if;
3610
3611 Adjust_Condition (Condition (N));
3612
3613 -- The following loop deals with constant conditions for the IF. We
3614 -- need a loop because as we eliminate False conditions, we grab the
3615 -- first elsif condition and use it as the primary condition.
3616
3617 while Compile_Time_Known_Value (Condition (N)) loop
3618
3619 -- If condition is True, we can simply rewrite the if statement now
3620 -- by replacing it by the series of then statements.
3621
3622 if Is_True (Expr_Value (Condition (N))) then
3623
3624 -- All the else parts can be killed
3625
3626 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3627 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3628
3629 Hed := Remove_Head (Then_Statements (N));
3630 Insert_List_After (N, Then_Statements (N));
3631 Rewrite (N, Hed);
3632 return;
3633
3634 -- If condition is False, then we can delete the condition and
3635 -- the Then statements
3636
3637 else
3638 -- We do not delete the condition if constant condition warnings
3639 -- are enabled, since otherwise we end up deleting the desired
3640 -- warning. Of course the backend will get rid of this True/False
3641 -- test anyway, so nothing is lost here.
3642
3643 if not Constant_Condition_Warnings then
3644 Kill_Dead_Code (Condition (N));
3645 end if;
3646
3647 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3648
3649 -- If there are no elsif statements, then we simply replace the
3650 -- entire if statement by the sequence of else statements.
3651
3652 if No (Elsif_Parts (N)) then
3653 if No (Else_Statements (N))
3654 or else Is_Empty_List (Else_Statements (N))
3655 then
3656 Rewrite (N,
3657 Make_Null_Statement (Sloc (N)));
3658 else
3659 Hed := Remove_Head (Else_Statements (N));
3660 Insert_List_After (N, Else_Statements (N));
3661 Rewrite (N, Hed);
3662 end if;
3663
3664 return;
3665
3666 -- If there are elsif statements, the first of them becomes the
3667 -- if/then section of the rebuilt if statement This is the case
3668 -- where we loop to reprocess this copied condition.
3669
3670 else
3671 Hed := Remove_Head (Elsif_Parts (N));
3672 Insert_Actions (N, Condition_Actions (Hed));
3673 Set_Condition (N, Condition (Hed));
3674 Set_Then_Statements (N, Then_Statements (Hed));
3675
3676 -- Hed might have been captured as the condition determining
3677 -- the current value for an entity. Now it is detached from
3678 -- the tree, so a Current_Value pointer in the condition might
3679 -- need to be updated.
3680
3681 Set_Current_Value_Condition (N);
3682
3683 if Is_Empty_List (Elsif_Parts (N)) then
3684 Set_Elsif_Parts (N, No_List);
3685 end if;
3686 end if;
3687 end if;
3688 end loop;
3689
3690 -- Loop through elsif parts, dealing with constant conditions and
3691 -- possible condition actions that are present.
3692
3693 if Present (Elsif_Parts (N)) then
3694 E := First (Elsif_Parts (N));
3695 while Present (E) loop
3696
3697 -- Do not consider controlled objects found in an if statement
3698 -- which actually models an if expression because their early
3699 -- finalization will affect the result of the expression.
3700
3701 if not From_Conditional_Expression (N) then
3702 Process_Statements_For_Controlled_Objects (E);
3703 end if;
3704
3705 Adjust_Condition (Condition (E));
3706
3707 -- If there are condition actions, then rewrite the if statement
3708 -- as indicated above. We also do the same rewrite for a True or
3709 -- False condition. The further processing of this constant
3710 -- condition is then done by the recursive call to expand the
3711 -- newly created if statement
3712
3713 if Present (Condition_Actions (E))
3714 or else Compile_Time_Known_Value (Condition (E))
3715 then
3716 New_If :=
3717 Make_If_Statement (Sloc (E),
3718 Condition => Condition (E),
3719 Then_Statements => Then_Statements (E),
3720 Elsif_Parts => No_List,
3721 Else_Statements => Else_Statements (N));
3722
3723 -- Elsif parts for new if come from remaining elsif's of parent
3724
3725 while Present (Next (E)) loop
3726 if No (Elsif_Parts (New_If)) then
3727 Set_Elsif_Parts (New_If, New_List);
3728 end if;
3729
3730 Append (Remove_Next (E), Elsif_Parts (New_If));
3731 end loop;
3732
3733 Set_Else_Statements (N, New_List (New_If));
3734
3735 if Present (Condition_Actions (E)) then
3736 Insert_List_Before (New_If, Condition_Actions (E));
3737 end if;
3738
3739 Remove (E);
3740
3741 if Is_Empty_List (Elsif_Parts (N)) then
3742 Set_Elsif_Parts (N, No_List);
3743 end if;
3744
3745 Analyze (New_If);
3746
3747 -- Note this is not an implicit if statement, since it is part
3748 -- of an explicit if statement in the source (or of an implicit
3749 -- if statement that has already been tested). We set the flag
3750 -- after calling Analyze to avoid generating extra warnings
3751 -- specific to pure if statements, however (see
3752 -- Sem_Ch5.Analyze_If_Statement).
3753
3754 Preserve_Comes_From_Source (New_If, N);
3755 return;
3756
3757 -- No special processing for that elsif part, move to next
3758
3759 else
3760 Next (E);
3761 end if;
3762 end loop;
3763 end if;
3764
3765 -- Some more optimizations applicable if we still have an IF statement
3766
3767 if Nkind (N) /= N_If_Statement then
3768 return;
3769 end if;
3770
3771 -- Another optimization, special cases that can be simplified
3772
3773 -- if expression then
3774 -- return [standard.]true;
3775 -- else
3776 -- return [standard.]false;
3777 -- end if;
3778
3779 -- can be changed to:
3780
3781 -- return expression;
3782
3783 -- and
3784
3785 -- if expression then
3786 -- return [standard.]false;
3787 -- else
3788 -- return [standard.]true;
3789 -- end if;
3790
3791 -- can be changed to:
3792
3793 -- return not (expression);
3794
3795 -- Do these optimizations only for internally generated code and only
3796 -- when -fpreserve-control-flow isn't set, to preserve the original
3797 -- source control flow.
3798
3799 if not Comes_From_Source (N)
3800 and then not Opt.Suppress_Control_Flow_Optimizations
3801 and then Nkind (N) = N_If_Statement
3802 and then No (Elsif_Parts (N))
3803 and then Present (Else_Statements (N))
3804 and then List_Length (Then_Statements (N)) = 1
3805 and then List_Length (Else_Statements (N)) = 1
3806 then
3807 declare
3808 Then_Stm : constant Node_Id := First (Then_Statements (N));
3809 Else_Stm : constant Node_Id := First (Else_Statements (N));
3810
3811 Then_Expr : Node_Id;
3812 Else_Expr : Node_Id;
3813
3814 begin
3815 if Nkind (Then_Stm) = N_Simple_Return_Statement
3816 and then
3817 Nkind (Else_Stm) = N_Simple_Return_Statement
3818 then
3819 Then_Expr := Expression (Then_Stm);
3820 Else_Expr := Expression (Else_Stm);
3821
3822 if Nkind (Then_Expr) in N_Expanded_Name | N_Identifier
3823 and then
3824 Nkind (Else_Expr) in N_Expanded_Name | N_Identifier
3825 then
3826 if Entity (Then_Expr) = Standard_True
3827 and then Entity (Else_Expr) = Standard_False
3828 then
3829 Rewrite (N,
3830 Make_Simple_Return_Statement (Loc,
3831 Expression => Relocate_Node (Condition (N))));
3832 Analyze (N);
3833
3834 elsif Entity (Then_Expr) = Standard_False
3835 and then Entity (Else_Expr) = Standard_True
3836 then
3837 Rewrite (N,
3838 Make_Simple_Return_Statement (Loc,
3839 Expression =>
3840 Make_Op_Not (Loc,
3841 Right_Opnd => Relocate_Node (Condition (N)))));
3842 Analyze (N);
3843 end if;
3844 end if;
3845 end if;
3846 end;
3847 end if;
3848 end Expand_N_If_Statement;
3849
3850 --------------------------
3851 -- Expand_Iterator_Loop --
3852 --------------------------
3853
3854 procedure Expand_Iterator_Loop (N : Node_Id) is
3855 Isc : constant Node_Id := Iteration_Scheme (N);
3856 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3857
3858 Container : constant Node_Id := Name (I_Spec);
3859 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
3860
3861 begin
3862 -- Processing for arrays
3863
3864 if Is_Array_Type (Container_Typ) then
3865 pragma Assert (Of_Present (I_Spec));
3866 Expand_Iterator_Loop_Over_Array (N);
3867
3868 elsif Has_Aspect (Container_Typ, Aspect_Iterable) then
3869 if Of_Present (I_Spec) then
3870 Expand_Formal_Container_Element_Loop (N);
3871 else
3872 Expand_Formal_Container_Loop (N);
3873 end if;
3874
3875 -- Processing for containers
3876
3877 else
3878 Expand_Iterator_Loop_Over_Container
3879 (N, Isc, I_Spec, Container, Container_Typ);
3880 end if;
3881 end Expand_Iterator_Loop;
3882
3883 -------------------------------------
3884 -- Expand_Iterator_Loop_Over_Array --
3885 -------------------------------------
3886
3887 procedure Expand_Iterator_Loop_Over_Array (N : Node_Id) is
3888 Isc : constant Node_Id := Iteration_Scheme (N);
3889 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3890 Array_Node : constant Node_Id := Name (I_Spec);
3891 Array_Typ : constant Entity_Id := Base_Type (Etype (Array_Node));
3892 Array_Dim : constant Pos := Number_Dimensions (Array_Typ);
3893 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3894 Loc : constant Source_Ptr := Sloc (Isc);
3895 Stats : List_Id := Statements (N);
3896 Core_Loop : Node_Id;
3897 Dim1 : Int;
3898 Ind_Comp : Node_Id;
3899 Iterator : Entity_Id;
3900
3901 begin
3902 if Present (Iterator_Filter (I_Spec)) then
3903 pragma Assert (Ada_Version >= Ada_2020);
3904 Stats := New_List (Make_If_Statement (Loc,
3905 Condition => Iterator_Filter (I_Spec),
3906 Then_Statements => Stats));
3907 end if;
3908
3909 -- for Element of Array loop
3910
3911 -- It requires an internally generated cursor to iterate over the array
3912
3913 pragma Assert (Of_Present (I_Spec));
3914
3915 Iterator := Make_Temporary (Loc, 'C');
3916
3917 -- Generate:
3918 -- Element : Component_Type renames Array (Iterator);
3919 -- Iterator is the index value, or a list of index values
3920 -- in the case of a multidimensional array.
3921
3922 Ind_Comp :=
3923 Make_Indexed_Component (Loc,
3924 Prefix => New_Copy_Tree (Array_Node),
3925 Expressions => New_List (New_Occurrence_Of (Iterator, Loc)));
3926
3927 -- Propagate the original node to the copy since the analysis of the
3928 -- following object renaming declaration relies on the original node.
3929
3930 Set_Original_Node (Prefix (Ind_Comp), Original_Node (Array_Node));
3931
3932 Prepend_To (Stats,
3933 Make_Object_Renaming_Declaration (Loc,
3934 Defining_Identifier => Id,
3935 Subtype_Mark =>
3936 New_Occurrence_Of (Component_Type (Array_Typ), Loc),
3937 Name => Ind_Comp));
3938
3939 -- Mark the loop variable as needing debug info, so that expansion
3940 -- of the renaming will result in Materialize_Entity getting set via
3941 -- Debug_Renaming_Declaration. (This setting is needed here because
3942 -- the setting in Freeze_Entity comes after the expansion, which is
3943 -- too late. ???)
3944
3945 Set_Debug_Info_Needed (Id);
3946
3947 -- Generate:
3948
3949 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3950 -- Element : Component_Type renames Array (Iterator);
3951 -- <original loop statements>
3952 -- end loop;
3953
3954 -- If this is an iteration over a multidimensional array, the
3955 -- innermost loop is over the last dimension in Ada, and over
3956 -- the first dimension in Fortran.
3957
3958 if Convention (Array_Typ) = Convention_Fortran then
3959 Dim1 := 1;
3960 else
3961 Dim1 := Array_Dim;
3962 end if;
3963
3964 Core_Loop :=
3965 Make_Loop_Statement (Sloc (N),
3966 Iteration_Scheme =>
3967 Make_Iteration_Scheme (Loc,
3968 Loop_Parameter_Specification =>
3969 Make_Loop_Parameter_Specification (Loc,
3970 Defining_Identifier => Iterator,
3971 Discrete_Subtype_Definition =>
3972 Make_Attribute_Reference (Loc,
3973 Prefix => New_Copy_Tree (Array_Node),
3974 Attribute_Name => Name_Range,
3975 Expressions => New_List (
3976 Make_Integer_Literal (Loc, Dim1))),
3977 Reverse_Present => Reverse_Present (I_Spec))),
3978 Statements => Stats,
3979 End_Label => Empty);
3980
3981 -- Processing for multidimensional array. The body of each loop is
3982 -- a loop over a previous dimension, going in decreasing order in Ada
3983 -- and in increasing order in Fortran.
3984
3985 if Array_Dim > 1 then
3986 for Dim in 1 .. Array_Dim - 1 loop
3987 if Convention (Array_Typ) = Convention_Fortran then
3988 Dim1 := Dim + 1;
3989 else
3990 Dim1 := Array_Dim - Dim;
3991 end if;
3992
3993 Iterator := Make_Temporary (Loc, 'C');
3994
3995 -- Generate the dimension loops starting from the innermost one
3996
3997 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3998 -- <core loop>
3999 -- end loop;
4000
4001 Core_Loop :=
4002 Make_Loop_Statement (Sloc (N),
4003 Iteration_Scheme =>
4004 Make_Iteration_Scheme (Loc,
4005 Loop_Parameter_Specification =>
4006 Make_Loop_Parameter_Specification (Loc,
4007 Defining_Identifier => Iterator,
4008 Discrete_Subtype_Definition =>
4009 Make_Attribute_Reference (Loc,
4010 Prefix => New_Copy_Tree (Array_Node),
4011 Attribute_Name => Name_Range,
4012 Expressions => New_List (
4013 Make_Integer_Literal (Loc, Dim1))),
4014 Reverse_Present => Reverse_Present (I_Spec))),
4015 Statements => New_List (Core_Loop),
4016 End_Label => Empty);
4017
4018 -- Update the previously created object renaming declaration with
4019 -- the new iterator, by adding the index of the next loop to the
4020 -- indexed component, in the order that corresponds to the
4021 -- convention.
4022
4023 if Convention (Array_Typ) = Convention_Fortran then
4024 Append_To (Expressions (Ind_Comp),
4025 New_Occurrence_Of (Iterator, Loc));
4026 else
4027 Prepend_To (Expressions (Ind_Comp),
4028 New_Occurrence_Of (Iterator, Loc));
4029 end if;
4030 end loop;
4031 end if;
4032
4033 -- Inherit the loop identifier from the original loop. This ensures that
4034 -- the scope stack is consistent after the rewriting.
4035
4036 if Present (Identifier (N)) then
4037 Set_Identifier (Core_Loop, Relocate_Node (Identifier (N)));
4038 end if;
4039
4040 Rewrite (N, Core_Loop);
4041 Analyze (N);
4042 end Expand_Iterator_Loop_Over_Array;
4043
4044 -----------------------------------------
4045 -- Expand_Iterator_Loop_Over_Container --
4046 -----------------------------------------
4047
4048 -- For a 'for ... in' loop, such as:
4049
4050 -- for Cursor in Iterator_Function (...) loop
4051 -- ...
4052 -- end loop;
4053
4054 -- we generate:
4055
4056 -- Iter : Iterator_Type := Iterator_Function (...);
4057 -- Cursor : Cursor_type := First (Iter); -- or Last for "reverse"
4058 -- while Has_Element (Cursor) loop
4059 -- ...
4060 --
4061 -- Cursor := Iter.Next (Cursor); -- or Prev for "reverse"
4062 -- end loop;
4063
4064 -- For a 'for ... of' loop, such as:
4065
4066 -- for X of Container loop
4067 -- ...
4068 -- end loop;
4069
4070 -- the RM implies the generation of:
4071
4072 -- Iter : Iterator_Type := Container.Iterate; -- the Default_Iterator
4073 -- Cursor : Cursor_Type := First (Iter); -- or Last for "reverse"
4074 -- while Has_Element (Cursor) loop
4075 -- declare
4076 -- X : Element_Type renames Element (Cursor).Element.all;
4077 -- -- or Constant_Element
4078 -- begin
4079 -- ...
4080 -- end;
4081 -- Cursor := Iter.Next (Cursor); -- or Prev for "reverse"
4082 -- end loop;
4083
4084 -- In the general case, we do what the RM says. However, the operations
4085 -- Element and Iter.Next are slow, which is bad inside a loop, because they
4086 -- involve dispatching via interfaces, secondary stack manipulation,
4087 -- Busy/Lock incr/decr, and adjust/finalization/at-end handling. So for the
4088 -- predefined containers, we use an equivalent but optimized expansion.
4089
4090 -- In the optimized case, we make use of these:
4091
4092 -- procedure Next (Position : in out Cursor); -- instead of Iter.Next
4093
4094 -- function Pseudo_Reference
4095 -- (Container : aliased Vector'Class) return Reference_Control_Type;
4096
4097 -- type Element_Access is access all Element_Type;
4098
4099 -- function Get_Element_Access
4100 -- (Position : Cursor) return not null Element_Access;
4101
4102 -- Next is declared in the visible part of the container packages.
4103 -- The other three are added in the private part. (We're not supposed to
4104 -- pollute the namespace for clients. The compiler has no trouble breaking
4105 -- privacy to call things in the private part of an instance.)
4106
4107 -- Source:
4108
4109 -- for X of My_Vector loop
4110 -- X.Count := X.Count + 1;
4111 -- ...
4112 -- end loop;
4113
4114 -- The compiler will generate:
4115
4116 -- Iter : Reversible_Iterator'Class := Iterate (My_Vector);
4117 -- -- Reversible_Iterator is an interface. Iterate is the
4118 -- -- Default_Iterator aspect of Vector. This increments Lock,
4119 -- -- disallowing tampering with cursors. Unfortunately, it does not
4120 -- -- increment Busy. The result of Iterate is Limited_Controlled;
4121 -- -- finalization will decrement Lock. This is a build-in-place
4122 -- -- dispatching call to Iterate.
4123
4124 -- Cur : Cursor := First (Iter); -- or Last
4125 -- -- Dispatching call via interface.
4126
4127 -- Control : Reference_Control_Type := Pseudo_Reference (My_Vector);
4128 -- -- Pseudo_Reference increments Busy, to detect tampering with
4129 -- -- elements, as required by RM. Also redundantly increment
4130 -- -- Lock. Finalization of Control will decrement both Busy and
4131 -- -- Lock. Pseudo_Reference returns a record containing a pointer to
4132 -- -- My_Vector, used by Finalize.
4133 -- --
4134 -- -- Control is not used below, except to finalize it -- it's purely
4135 -- -- an RAII thing. This is needed because we are eliminating the
4136 -- -- call to Reference within the loop.
4137
4138 -- while Has_Element (Cur) loop
4139 -- declare
4140 -- X : My_Element renames Get_Element_Access (Cur).all;
4141 -- -- Get_Element_Access returns a pointer to the element
4142 -- -- designated by Cur. No dispatching here, and no horsing
4143 -- -- around with access discriminants. This is instead of the
4144 -- -- existing
4145 -- --
4146 -- -- X : My_Element renames Reference (Cur).Element.all;
4147 -- --
4148 -- -- which creates a controlled object.
4149 -- begin
4150 -- -- Any attempt to tamper with My_Vector here in the loop
4151 -- -- will correctly raise Program_Error, because of the
4152 -- -- Control.
4153 --
4154 -- X.Count := X.Count + 1;
4155 -- ...
4156 --
4157 -- Next (Cur); -- or Prev
4158 -- -- This is instead of "Cur := Next (Iter, Cur);"
4159 -- end;
4160 -- -- No finalization here
4161 -- end loop;
4162 -- Finalize Iter and Control here, decrementing Lock twice and Busy
4163 -- once.
4164
4165 -- This optimization makes "for ... of" loops over 30 times faster in cases
4166 -- measured.
4167
4168 procedure Expand_Iterator_Loop_Over_Container
4169 (N : Node_Id;
4170 Isc : Node_Id;
4171 I_Spec : Node_Id;
4172 Container : Node_Id;
4173 Container_Typ : Entity_Id)
4174 is
4175 Id : constant Entity_Id := Defining_Identifier (I_Spec);
4176 Elem_Typ : constant Entity_Id := Etype (Id);
4177 Id_Kind : constant Entity_Kind := Ekind (Id);
4178 Loc : constant Source_Ptr := Sloc (N);
4179
4180 Stats : List_Id := Statements (N);
4181 -- Maybe wrapped in a conditional if a filter is present
4182
4183 Cursor : Entity_Id;
4184 Decl : Node_Id;
4185 Iter_Type : Entity_Id;
4186 Iterator : Entity_Id;
4187 Name_Init : Name_Id;
4188 Name_Step : Name_Id;
4189 New_Loop : Node_Id;
4190
4191 Fast_Element_Access_Op : Entity_Id := Empty;
4192 Fast_Step_Op : Entity_Id := Empty;
4193 -- Only for optimized version of "for ... of"
4194
4195 Iter_Pack : Entity_Id;
4196 -- The package in which the iterator interface is instantiated. This is
4197 -- typically an instance within the container package.
4198
4199 Pack : Entity_Id;
4200 -- The package in which the container type is declared
4201
4202 begin
4203 if Present (Iterator_Filter (I_Spec)) then
4204 pragma Assert (Ada_Version >= Ada_2020);
4205 Stats := New_List (Make_If_Statement (Loc,
4206 Condition => Iterator_Filter (I_Spec),
4207 Then_Statements => Stats));
4208 end if;
4209
4210 -- Determine the advancement and initialization steps for the cursor.
4211 -- Analysis of the expanded loop will verify that the container has a
4212 -- reverse iterator.
4213
4214 if Reverse_Present (I_Spec) then
4215 Name_Init := Name_Last;
4216 Name_Step := Name_Previous;
4217 else
4218 Name_Init := Name_First;
4219 Name_Step := Name_Next;
4220 end if;
4221
4222 -- The type of the iterator is the return type of the Iterate function
4223 -- used. For the "of" form this is the default iterator for the type,
4224 -- otherwise it is the type of the explicit function used in the
4225 -- iterator specification. The most common case will be an Iterate
4226 -- function in the container package.
4227
4228 -- The Iterator type is declared in an instance within the container
4229 -- package itself, for example:
4230
4231 -- package Vector_Iterator_Interfaces is new
4232 -- Ada.Iterator_Interfaces (Cursor, Has_Element);
4233
4234 -- If the container type is a derived type, the cursor type is found in
4235 -- the package of the ultimate ancestor type.
4236
4237 if Is_Derived_Type (Container_Typ) then
4238 Pack := Scope (Root_Type (Container_Typ));
4239 else
4240 Pack := Scope (Container_Typ);
4241 end if;
4242
4243 if Of_Present (I_Spec) then
4244 Handle_Of : declare
4245 Container_Arg : Node_Id;
4246
4247 function Get_Default_Iterator
4248 (T : Entity_Id) return Entity_Id;
4249 -- Return the default iterator for a specific type. If the type is
4250 -- derived, we return the inherited or overridden one if
4251 -- appropriate.
4252
4253 --------------------------
4254 -- Get_Default_Iterator --
4255 --------------------------
4256
4257 function Get_Default_Iterator
4258 (T : Entity_Id) return Entity_Id
4259 is
4260 Iter : constant Entity_Id :=
4261 Entity (Find_Value_Of_Aspect (T, Aspect_Default_Iterator));
4262 Prim : Elmt_Id;
4263 Op : Entity_Id;
4264
4265 begin
4266 Container_Arg := New_Copy_Tree (Container);
4267
4268 -- A previous version of GNAT allowed indexing aspects to be
4269 -- redefined on derived container types, while the default
4270 -- iterator was inherited from the parent type. This
4271 -- nonstandard extension is preserved for use by the
4272 -- modeling project under debug flag -gnatd.X.
4273
4274 if Debug_Flag_Dot_XX then
4275 if Base_Type (Etype (Container)) /=
4276 Base_Type (Etype (First_Formal (Iter)))
4277 then
4278 Container_Arg :=
4279 Make_Type_Conversion (Loc,
4280 Subtype_Mark =>
4281 New_Occurrence_Of
4282 (Etype (First_Formal (Iter)), Loc),
4283 Expression => Container_Arg);
4284 end if;
4285
4286 return Iter;
4287
4288 elsif Is_Derived_Type (T) then
4289
4290 -- The default iterator must be a primitive operation of the
4291 -- type, at the same dispatch slot position. The DT position
4292 -- may not be established if type is not frozen yet.
4293
4294 Prim := First_Elmt (Primitive_Operations (T));
4295 while Present (Prim) loop
4296 Op := Node (Prim);
4297
4298 if Alias (Op) = Iter
4299 or else
4300 (Chars (Op) = Chars (Iter)
4301 and then Present (DTC_Entity (Op))
4302 and then DT_Position (Op) = DT_Position (Iter))
4303 then
4304 return Op;
4305 end if;
4306
4307 Next_Elmt (Prim);
4308 end loop;
4309
4310 -- If we didn't find it, then our parent type is not
4311 -- iterable, so we return the Default_Iterator aspect of
4312 -- this type.
4313
4314 return Iter;
4315
4316 -- Otherwise not a derived type
4317
4318 else
4319 return Iter;
4320 end if;
4321 end Get_Default_Iterator;
4322
4323 -- Local variables
4324
4325 Default_Iter : Entity_Id;
4326 Ent : Entity_Id;
4327
4328 Reference_Control_Type : Entity_Id := Empty;
4329 Pseudo_Reference : Entity_Id := Empty;
4330
4331 -- Start of processing for Handle_Of
4332
4333 begin
4334 if Is_Class_Wide_Type (Container_Typ) then
4335 Default_Iter :=
4336 Get_Default_Iterator (Etype (Base_Type (Container_Typ)));
4337 else
4338 Default_Iter := Get_Default_Iterator (Etype (Container));
4339 end if;
4340
4341 Cursor := Make_Temporary (Loc, 'C');
4342
4343 -- For a container element iterator, the iterator type is obtained
4344 -- from the corresponding aspect, whose return type is descended
4345 -- from the corresponding interface type in some instance of
4346 -- Ada.Iterator_Interfaces. The actuals of that instantiation
4347 -- are Cursor and Has_Element.
4348
4349 Iter_Type := Etype (Default_Iter);
4350
4351 -- The iterator type, which is a class-wide type, may itself be
4352 -- derived locally, so the desired instantiation is the scope of
4353 -- the root type of the iterator type.
4354
4355 Iter_Pack := Scope (Root_Type (Etype (Iter_Type)));
4356
4357 -- Find declarations needed for "for ... of" optimization
4358 -- These declarations come from GNAT sources or sources
4359 -- derived from them. User code may include additional
4360 -- overloadings with similar names, and we need to perforn
4361 -- some reasonable resolution to find the needed primitives.
4362 -- It is unclear whether this mechanism is fragile if a user
4363 -- makes arbitrary changes to the private part of a package
4364 -- that supports iterators.
4365
4366 Ent := First_Entity (Pack);
4367 while Present (Ent) loop
4368 if Chars (Ent) = Name_Get_Element_Access
4369 and then Present (First_Formal (Ent))
4370 and then Chars (First_Formal (Ent)) = Name_Position
4371 and then No (Next_Formal (First_Formal (Ent)))
4372 then
4373 Fast_Element_Access_Op := Ent;
4374
4375 elsif Chars (Ent) = Name_Step
4376 and then Ekind (Ent) = E_Procedure
4377 then
4378 Fast_Step_Op := Ent;
4379
4380 elsif Chars (Ent) = Name_Reference_Control_Type then
4381 Reference_Control_Type := Ent;
4382
4383 elsif Chars (Ent) = Name_Pseudo_Reference then
4384 Pseudo_Reference := Ent;
4385 end if;
4386
4387 Next_Entity (Ent);
4388 end loop;
4389
4390 if Present (Reference_Control_Type)
4391 and then Present (Pseudo_Reference)
4392 then
4393 Insert_Action (N,
4394 Make_Object_Declaration (Loc,
4395 Defining_Identifier => Make_Temporary (Loc, 'D'),
4396 Object_Definition =>
4397 New_Occurrence_Of (Reference_Control_Type, Loc),
4398 Expression =>
4399 Make_Function_Call (Loc,
4400 Name =>
4401 New_Occurrence_Of (Pseudo_Reference, Loc),
4402 Parameter_Associations =>
4403 New_List (New_Copy_Tree (Container_Arg)))));
4404 end if;
4405
4406 -- Rewrite domain of iteration as a call to the default iterator
4407 -- for the container type. The formal may be an access parameter
4408 -- in which case we must build a reference to the container.
4409
4410 declare
4411 Arg : Node_Id;
4412 begin
4413 if Is_Access_Type (Etype (First_Entity (Default_Iter))) then
4414 Arg :=
4415 Make_Attribute_Reference (Loc,
4416 Prefix => Container_Arg,
4417 Attribute_Name => Name_Unrestricted_Access);
4418 else
4419 Arg := Container_Arg;
4420 end if;
4421
4422 Rewrite (Name (I_Spec),
4423 Make_Function_Call (Loc,
4424 Name =>
4425 New_Occurrence_Of (Default_Iter, Loc),
4426 Parameter_Associations => New_List (Arg)));
4427 end;
4428
4429 Analyze_And_Resolve (Name (I_Spec));
4430
4431 -- Find cursor type in proper iterator package, which is an
4432 -- instantiation of Iterator_Interfaces.
4433
4434 Ent := First_Entity (Iter_Pack);
4435 while Present (Ent) loop
4436 if Chars (Ent) = Name_Cursor then
4437 Set_Etype (Cursor, Etype (Ent));
4438 exit;
4439 end if;
4440
4441 Next_Entity (Ent);
4442 end loop;
4443
4444 if Present (Fast_Element_Access_Op) then
4445 Decl :=
4446 Make_Object_Renaming_Declaration (Loc,
4447 Defining_Identifier => Id,
4448 Subtype_Mark =>
4449 New_Occurrence_Of (Elem_Typ, Loc),
4450 Name =>
4451 Make_Explicit_Dereference (Loc,
4452 Prefix =>
4453 Make_Function_Call (Loc,
4454 Name =>
4455 New_Occurrence_Of (Fast_Element_Access_Op, Loc),
4456 Parameter_Associations =>
4457 New_List (New_Occurrence_Of (Cursor, Loc)))));
4458
4459 else
4460 Decl :=
4461 Make_Object_Renaming_Declaration (Loc,
4462 Defining_Identifier => Id,
4463 Subtype_Mark =>
4464 New_Occurrence_Of (Elem_Typ, Loc),
4465 Name =>
4466 Make_Indexed_Component (Loc,
4467 Prefix => Relocate_Node (Container_Arg),
4468 Expressions =>
4469 New_List (New_Occurrence_Of (Cursor, Loc))));
4470 end if;
4471
4472 -- The defining identifier in the iterator is user-visible and
4473 -- must be visible in the debugger.
4474
4475 Set_Debug_Info_Needed (Id);
4476
4477 -- If the container does not have a variable indexing aspect,
4478 -- the element is a constant in the loop. The container itself
4479 -- may be constant, in which case the element is a constant as
4480 -- well. The container has been rewritten as a call to Iterate,
4481 -- so examine original node.
4482
4483 if No (Find_Value_Of_Aspect
4484 (Container_Typ, Aspect_Variable_Indexing))
4485 or else not Is_Variable (Original_Node (Container))
4486 then
4487 Set_Ekind (Id, E_Constant);
4488 end if;
4489
4490 Prepend_To (Stats, Decl);
4491 end Handle_Of;
4492
4493 -- X in Iterate (S) : type of iterator is type of explicitly given
4494 -- Iterate function, and the loop variable is the cursor. It will be
4495 -- assigned in the loop and must be a variable.
4496
4497 else
4498 Iter_Type := Etype (Name (I_Spec));
4499
4500 -- The iterator type, which is a class-wide type, may itself be
4501 -- derived locally, so the desired instantiation is the scope of
4502 -- the root type of the iterator type, as in the "of" case.
4503
4504 Iter_Pack := Scope (Root_Type (Etype (Iter_Type)));
4505 Cursor := Id;
4506 end if;
4507
4508 Iterator := Make_Temporary (Loc, 'I');
4509
4510 -- For both iterator forms, add a call to the step operation to advance
4511 -- the cursor. Generate:
4512
4513 -- Cursor := Iterator.Next (Cursor);
4514
4515 -- or else
4516
4517 -- Cursor := Next (Cursor);
4518
4519 if Present (Fast_Element_Access_Op) and then Present (Fast_Step_Op) then
4520 declare
4521 Curs_Name : constant Node_Id := New_Occurrence_Of (Cursor, Loc);
4522 Step_Call : Node_Id;
4523
4524 begin
4525 Step_Call :=
4526 Make_Procedure_Call_Statement (Loc,
4527 Name =>
4528 New_Occurrence_Of (Fast_Step_Op, Loc),
4529 Parameter_Associations => New_List (Curs_Name));
4530
4531 Append_To (Stats, Step_Call);
4532 Set_Assignment_OK (Curs_Name);
4533 end;
4534
4535 else
4536 declare
4537 Rhs : Node_Id;
4538
4539 begin
4540 Rhs :=
4541 Make_Function_Call (Loc,
4542 Name =>
4543 Make_Selected_Component (Loc,
4544 Prefix => New_Occurrence_Of (Iterator, Loc),
4545 Selector_Name => Make_Identifier (Loc, Name_Step)),
4546 Parameter_Associations => New_List (
4547 New_Occurrence_Of (Cursor, Loc)));
4548
4549 Append_To (Stats,
4550 Make_Assignment_Statement (Loc,
4551 Name => New_Occurrence_Of (Cursor, Loc),
4552 Expression => Rhs));
4553 Set_Assignment_OK (Name (Last (Stats)));
4554 end;
4555 end if;
4556
4557 -- Generate:
4558 -- while Has_Element (Cursor) loop
4559 -- <Stats>
4560 -- end loop;
4561
4562 -- Has_Element is the second actual in the iterator package
4563
4564 New_Loop :=
4565 Make_Loop_Statement (Loc,
4566 Iteration_Scheme =>
4567 Make_Iteration_Scheme (Loc,
4568 Condition =>
4569 Make_Function_Call (Loc,
4570 Name =>
4571 New_Occurrence_Of
4572 (Next_Entity (First_Entity (Iter_Pack)), Loc),
4573 Parameter_Associations => New_List (
4574 New_Occurrence_Of (Cursor, Loc)))),
4575
4576 Statements => Stats,
4577 End_Label => Empty);
4578
4579 -- If present, preserve identifier of loop, which can be used in an exit
4580 -- statement in the body.
4581
4582 if Present (Identifier (N)) then
4583 Set_Identifier (New_Loop, Relocate_Node (Identifier (N)));
4584 end if;
4585
4586 -- Create the declarations for Iterator and cursor and insert them
4587 -- before the source loop. Given that the domain of iteration is already
4588 -- an entity, the iterator is just a renaming of that entity. Possible
4589 -- optimization ???
4590
4591 Insert_Action (N,
4592 Make_Object_Renaming_Declaration (Loc,
4593 Defining_Identifier => Iterator,
4594 Subtype_Mark => New_Occurrence_Of (Iter_Type, Loc),
4595 Name => Relocate_Node (Name (I_Spec))));
4596
4597 -- Create declaration for cursor
4598
4599 declare
4600 Cursor_Decl : constant Node_Id :=
4601 Make_Object_Declaration (Loc,
4602 Defining_Identifier => Cursor,
4603 Object_Definition =>
4604 New_Occurrence_Of (Etype (Cursor), Loc),
4605 Expression =>
4606 Make_Selected_Component (Loc,
4607 Prefix =>
4608 New_Occurrence_Of (Iterator, Loc),
4609 Selector_Name =>
4610 Make_Identifier (Loc, Name_Init)));
4611
4612 begin
4613 -- The cursor is only modified in expanded code, so it appears
4614 -- as unassigned to the warning machinery. We must suppress this
4615 -- spurious warning explicitly. The cursor's kind is that of the
4616 -- original loop parameter (it is a constant if the domain of
4617 -- iteration is constant).
4618
4619 Set_Warnings_Off (Cursor);
4620 Set_Assignment_OK (Cursor_Decl);
4621
4622 Insert_Action (N, Cursor_Decl);
4623 Set_Ekind (Cursor, Id_Kind);
4624 end;
4625
4626 -- If the range of iteration is given by a function call that returns
4627 -- a container, the finalization actions have been saved in the
4628 -- Condition_Actions of the iterator. Insert them now at the head of
4629 -- the loop.
4630
4631 if Present (Condition_Actions (Isc)) then
4632 Insert_List_Before (N, Condition_Actions (Isc));
4633 end if;
4634
4635 Rewrite (N, New_Loop);
4636 Analyze (N);
4637 end Expand_Iterator_Loop_Over_Container;
4638
4639 -----------------------------
4640 -- Expand_N_Loop_Statement --
4641 -----------------------------
4642
4643 -- 1. Remove null loop entirely
4644 -- 2. Deal with while condition for C/Fortran boolean
4645 -- 3. Deal with loops with a non-standard enumeration type range
4646 -- 4. Deal with while loops where Condition_Actions is set
4647 -- 5. Deal with loops over predicated subtypes
4648 -- 6. Deal with loops with iterators over arrays and containers
4649
4650 procedure Expand_N_Loop_Statement (N : Node_Id) is
4651 Loc : constant Source_Ptr := Sloc (N);
4652 Scheme : constant Node_Id := Iteration_Scheme (N);
4653 Stmt : Node_Id;
4654
4655 begin
4656 -- Delete null loop
4657
4658 if Is_Null_Loop (N) then
4659 Rewrite (N, Make_Null_Statement (Loc));
4660 return;
4661 end if;
4662
4663 -- Deal with condition for C/Fortran Boolean
4664
4665 if Present (Scheme) then
4666 Adjust_Condition (Condition (Scheme));
4667 end if;
4668
4669 -- Nothing more to do for plain loop with no iteration scheme
4670
4671 if No (Scheme) then
4672 null;
4673
4674 -- Case of for loop (Loop_Parameter_Specification present)
4675
4676 -- Note: we do not have to worry about validity checking of the for loop
4677 -- range bounds here, since they were frozen with constant declarations
4678 -- and it is during that process that the validity checking is done.
4679
4680 elsif Present (Loop_Parameter_Specification (Scheme)) then
4681 declare
4682 LPS : constant Node_Id :=
4683 Loop_Parameter_Specification (Scheme);
4684 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
4685 Ltype : constant Entity_Id := Etype (Loop_Id);
4686 Btype : constant Entity_Id := Base_Type (Ltype);
4687 Stats : constant List_Id := Statements (N);
4688 Expr : Node_Id;
4689 Decls : List_Id;
4690 New_Id : Entity_Id;
4691
4692 begin
4693 -- If Discrete_Subtype_Definition has been rewritten as an
4694 -- N_Raise_xxx_Error, rewrite the whole loop as a raise node to
4695 -- avoid confusing the code generator down the line.
4696
4697 if Nkind (Discrete_Subtype_Definition (LPS)) in N_Raise_xxx_Error
4698 then
4699 Rewrite (N, Discrete_Subtype_Definition (LPS));
4700 return;
4701 end if;
4702
4703 if Present (Iterator_Filter (LPS)) then
4704 pragma Assert (Ada_Version >= Ada_2020);
4705 Set_Statements (N,
4706 New_List (Make_If_Statement (Loc,
4707 Condition => Iterator_Filter (LPS),
4708 Then_Statements => Stats)));
4709 end if;
4710
4711 -- Deal with loop over predicates
4712
4713 if Is_Discrete_Type (Ltype)
4714 and then Present (Predicate_Function (Ltype))
4715 then
4716 Expand_Predicated_Loop (N);
4717
4718 -- Handle the case where we have a for loop with the range type
4719 -- being an enumeration type with non-standard representation.
4720 -- In this case we expand:
4721
4722 -- for x in [reverse] a .. b loop
4723 -- ...
4724 -- end loop;
4725
4726 -- to
4727
4728 -- for xP in [reverse] integer
4729 -- range etype'Pos (a) .. etype'Pos (b)
4730 -- loop
4731 -- declare
4732 -- x : constant etype := Pos_To_Rep (xP);
4733 -- begin
4734 -- ...
4735 -- end;
4736 -- end loop;
4737
4738 elsif Is_Enumeration_Type (Btype)
4739 and then Present (Enum_Pos_To_Rep (Btype))
4740 then
4741 New_Id :=
4742 Make_Defining_Identifier (Loc,
4743 Chars => New_External_Name (Chars (Loop_Id), 'P'));
4744
4745 -- If the type has a contiguous representation, successive
4746 -- values can be generated as offsets from the first literal.
4747
4748 if Has_Contiguous_Rep (Btype) then
4749 Expr :=
4750 Unchecked_Convert_To (Btype,
4751 Make_Op_Add (Loc,
4752 Left_Opnd =>
4753 Make_Integer_Literal (Loc,
4754 Enumeration_Rep (First_Literal (Btype))),
4755 Right_Opnd => New_Occurrence_Of (New_Id, Loc)));
4756 else
4757 -- Use the constructed array Enum_Pos_To_Rep
4758
4759 Expr :=
4760 Make_Indexed_Component (Loc,
4761 Prefix =>
4762 New_Occurrence_Of (Enum_Pos_To_Rep (Btype), Loc),
4763 Expressions =>
4764 New_List (New_Occurrence_Of (New_Id, Loc)));
4765 end if;
4766
4767 -- Build declaration for loop identifier
4768
4769 Decls :=
4770 New_List (
4771 Make_Object_Declaration (Loc,
4772 Defining_Identifier => Loop_Id,
4773 Constant_Present => True,
4774 Object_Definition => New_Occurrence_Of (Ltype, Loc),
4775 Expression => Expr));
4776
4777 Rewrite (N,
4778 Make_Loop_Statement (Loc,
4779 Identifier => Identifier (N),
4780
4781 Iteration_Scheme =>
4782 Make_Iteration_Scheme (Loc,
4783 Loop_Parameter_Specification =>
4784 Make_Loop_Parameter_Specification (Loc,
4785 Defining_Identifier => New_Id,
4786 Reverse_Present => Reverse_Present (LPS),
4787
4788 Discrete_Subtype_Definition =>
4789 Make_Subtype_Indication (Loc,
4790
4791 Subtype_Mark =>
4792 New_Occurrence_Of (Standard_Natural, Loc),
4793
4794 Constraint =>
4795 Make_Range_Constraint (Loc,
4796 Range_Expression =>
4797 Make_Range (Loc,
4798
4799 Low_Bound =>
4800 Make_Attribute_Reference (Loc,
4801 Prefix =>
4802 New_Occurrence_Of (Btype, Loc),
4803
4804 Attribute_Name => Name_Pos,
4805
4806 Expressions => New_List (
4807 Relocate_Node
4808 (Type_Low_Bound (Ltype)))),
4809
4810 High_Bound =>
4811 Make_Attribute_Reference (Loc,
4812 Prefix =>
4813 New_Occurrence_Of (Btype, Loc),
4814
4815 Attribute_Name => Name_Pos,
4816
4817 Expressions => New_List (
4818 Relocate_Node
4819 (Type_High_Bound
4820 (Ltype))))))))),
4821
4822 Statements => New_List (
4823 Make_Block_Statement (Loc,
4824 Declarations => Decls,
4825 Handled_Statement_Sequence =>
4826 Make_Handled_Sequence_Of_Statements (Loc,
4827 Statements => Stats))),
4828
4829 End_Label => End_Label (N)));
4830
4831 -- The loop parameter's entity must be removed from the loop
4832 -- scope's entity list and rendered invisible, since it will
4833 -- now be located in the new block scope. Any other entities
4834 -- already associated with the loop scope, such as the loop
4835 -- parameter's subtype, will remain there.
4836
4837 -- In an element loop, the loop will contain a declaration for
4838 -- a cursor variable; otherwise the loop id is the first entity
4839 -- in the scope constructed for the loop.
4840
4841 if Comes_From_Source (Loop_Id) then
4842 pragma Assert (First_Entity (Scope (Loop_Id)) = Loop_Id);
4843 null;
4844 end if;
4845
4846 Set_First_Entity (Scope (Loop_Id), Next_Entity (Loop_Id));
4847 Remove_Homonym (Loop_Id);
4848
4849 if Last_Entity (Scope (Loop_Id)) = Loop_Id then
4850 Set_Last_Entity (Scope (Loop_Id), Empty);
4851 end if;
4852
4853 Analyze (N);
4854
4855 -- Nothing to do with other cases of for loops
4856
4857 else
4858 null;
4859 end if;
4860 end;
4861
4862 -- Second case, if we have a while loop with Condition_Actions set, then
4863 -- we change it into a plain loop:
4864
4865 -- while C loop
4866 -- ...
4867 -- end loop;
4868
4869 -- changed to:
4870
4871 -- loop
4872 -- <<condition actions>>
4873 -- exit when not C;
4874 -- ...
4875 -- end loop
4876
4877 elsif Present (Scheme)
4878 and then Present (Condition_Actions (Scheme))
4879 and then Present (Condition (Scheme))
4880 then
4881 declare
4882 ES : Node_Id;
4883
4884 begin
4885 ES :=
4886 Make_Exit_Statement (Sloc (Condition (Scheme)),
4887 Condition =>
4888 Make_Op_Not (Sloc (Condition (Scheme)),
4889 Right_Opnd => Condition (Scheme)));
4890
4891 Prepend (ES, Statements (N));
4892 Insert_List_Before (ES, Condition_Actions (Scheme));
4893
4894 -- This is not an implicit loop, since it is generated in response
4895 -- to the loop statement being processed. If this is itself
4896 -- implicit, the restriction has already been checked. If not,
4897 -- it is an explicit loop.
4898
4899 Rewrite (N,
4900 Make_Loop_Statement (Sloc (N),
4901 Identifier => Identifier (N),
4902 Statements => Statements (N),
4903 End_Label => End_Label (N)));
4904
4905 Analyze (N);
4906 end;
4907
4908 -- Here to deal with iterator case
4909
4910 elsif Present (Scheme)
4911 and then Present (Iterator_Specification (Scheme))
4912 then
4913 Expand_Iterator_Loop (N);
4914
4915 -- An iterator loop may generate renaming declarations for elements
4916 -- that require debug information. This is the case in particular
4917 -- with element iterators, where debug information must be generated
4918 -- for the temporary that holds the element value. These temporaries
4919 -- are created within a transient block whose local declarations are
4920 -- transferred to the loop, which now has nontrivial local objects.
4921
4922 if Nkind (N) = N_Loop_Statement
4923 and then Present (Identifier (N))
4924 then
4925 Qualify_Entity_Names (N);
4926 end if;
4927 end if;
4928
4929 -- When the iteration scheme mentions attribute 'Loop_Entry, the loop
4930 -- is transformed into a conditional block where the original loop is
4931 -- the sole statement. Inspect the statements of the nested loop for
4932 -- controlled objects.
4933
4934 Stmt := N;
4935
4936 if Subject_To_Loop_Entry_Attributes (Stmt) then
4937 Stmt := Find_Loop_In_Conditional_Block (Stmt);
4938 end if;
4939
4940 Process_Statements_For_Controlled_Objects (Stmt);
4941 end Expand_N_Loop_Statement;
4942
4943 ----------------------------
4944 -- Expand_Predicated_Loop --
4945 ----------------------------
4946
4947 -- Note: the expander can handle generation of loops over predicated
4948 -- subtypes for both the dynamic and static cases. Depending on what
4949 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
4950 -- mode, the semantic analyzer may disallow one or both forms.
4951
4952 procedure Expand_Predicated_Loop (N : Node_Id) is
4953 Orig_Loop_Id : Node_Id := Empty;
4954 Loc : constant Source_Ptr := Sloc (N);
4955 Isc : constant Node_Id := Iteration_Scheme (N);
4956 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
4957 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
4958 Ltype : constant Entity_Id := Etype (Loop_Id);
4959 Stat : constant List_Id := Static_Discrete_Predicate (Ltype);
4960 Stmts : constant List_Id := Statements (N);
4961
4962 begin
4963 -- Case of iteration over non-static predicate, should not be possible
4964 -- since this is not allowed by the semantics and should have been
4965 -- caught during analysis of the loop statement.
4966
4967 if No (Stat) then
4968 raise Program_Error;
4969
4970 -- If the predicate list is empty, that corresponds to a predicate of
4971 -- False, in which case the loop won't run at all, and we rewrite the
4972 -- entire loop as a null statement.
4973
4974 elsif Is_Empty_List (Stat) then
4975 Rewrite (N, Make_Null_Statement (Loc));
4976 Analyze (N);
4977
4978 -- For expansion over a static predicate we generate the following
4979
4980 -- declare
4981 -- J : Ltype := min-val;
4982 -- begin
4983 -- loop
4984 -- body
4985 -- case J is
4986 -- when endpoint => J := startpoint;
4987 -- when endpoint => J := startpoint;
4988 -- ...
4989 -- when max-val => exit;
4990 -- when others => J := Lval'Succ (J);
4991 -- end case;
4992 -- end loop;
4993 -- end;
4994
4995 -- with min-val replaced by max-val and Succ replaced by Pred if the
4996 -- loop parameter specification carries a Reverse indicator.
4997
4998 -- To make this a little clearer, let's take a specific example:
4999
5000 -- type Int is range 1 .. 10;
5001 -- subtype StaticP is Int with
5002 -- predicate => StaticP in 3 | 10 | 5 .. 7;
5003 -- ...
5004 -- for L in StaticP loop
5005 -- Put_Line ("static:" & J'Img);
5006 -- end loop;
5007
5008 -- In this case, the loop is transformed into
5009
5010 -- begin
5011 -- J : L := 3;
5012 -- loop
5013 -- body
5014 -- case J is
5015 -- when 3 => J := 5;
5016 -- when 7 => J := 10;
5017 -- when 10 => exit;
5018 -- when others => J := L'Succ (J);
5019 -- end case;
5020 -- end loop;
5021 -- end;
5022
5023 -- In addition, if the loop specification is given by a subtype
5024 -- indication that constrains a predicated type, the bounds of
5025 -- iteration are given by those of the subtype indication.
5026
5027 else
5028 Static_Predicate : declare
5029 S : Node_Id;
5030 D : Node_Id;
5031 P : Node_Id;
5032 Alts : List_Id;
5033 Cstm : Node_Id;
5034
5035 -- If the domain is an itype, note the bounds of its range.
5036
5037 L_Hi : Node_Id := Empty;
5038 L_Lo : Node_Id := Empty;
5039
5040 function Lo_Val (N : Node_Id) return Node_Id;
5041 -- Given static expression or static range, returns an identifier
5042 -- whose value is the low bound of the expression value or range.
5043
5044 function Hi_Val (N : Node_Id) return Node_Id;
5045 -- Given static expression or static range, returns an identifier
5046 -- whose value is the high bound of the expression value or range.
5047
5048 ------------
5049 -- Hi_Val --
5050 ------------
5051
5052 function Hi_Val (N : Node_Id) return Node_Id is
5053 begin
5054 if Is_OK_Static_Expression (N) then
5055 return New_Copy (N);
5056 else
5057 pragma Assert (Nkind (N) = N_Range);
5058 return New_Copy (High_Bound (N));
5059 end if;
5060 end Hi_Val;
5061
5062 ------------
5063 -- Lo_Val --
5064 ------------
5065
5066 function Lo_Val (N : Node_Id) return Node_Id is
5067 begin
5068 if Is_OK_Static_Expression (N) then
5069 return New_Copy (N);
5070 else
5071 pragma Assert (Nkind (N) = N_Range);
5072 return New_Copy (Low_Bound (N));
5073 end if;
5074 end Lo_Val;
5075
5076 -- Start of processing for Static_Predicate
5077
5078 begin
5079 -- Convert loop identifier to normal variable and reanalyze it so
5080 -- that this conversion works. We have to use the same defining
5081 -- identifier, since there may be references in the loop body.
5082
5083 Set_Analyzed (Loop_Id, False);
5084 Set_Ekind (Loop_Id, E_Variable);
5085
5086 -- In most loops the loop variable is assigned in various
5087 -- alternatives in the body. However, in the rare case when
5088 -- the range specifies a single element, the loop variable
5089 -- may trigger a spurious warning that is could be constant.
5090 -- This warning might as well be suppressed.
5091
5092 Set_Warnings_Off (Loop_Id);
5093
5094 if Is_Itype (Ltype) then
5095 L_Hi := High_Bound (Scalar_Range (Ltype));
5096 L_Lo := Low_Bound (Scalar_Range (Ltype));
5097 end if;
5098
5099 -- Loop to create branches of case statement
5100
5101 Alts := New_List;
5102
5103 if Reverse_Present (LPS) then
5104
5105 -- Initial value is largest value in predicate.
5106
5107 if Is_Itype (Ltype) then
5108 D :=
5109 Make_Object_Declaration (Loc,
5110 Defining_Identifier => Loop_Id,
5111 Object_Definition => New_Occurrence_Of (Ltype, Loc),
5112 Expression => L_Hi);
5113
5114 else
5115 D :=
5116 Make_Object_Declaration (Loc,
5117 Defining_Identifier => Loop_Id,
5118 Object_Definition => New_Occurrence_Of (Ltype, Loc),
5119 Expression => Hi_Val (Last (Stat)));
5120 end if;
5121
5122 P := Last (Stat);
5123 while Present (P) loop
5124 if No (Prev (P)) then
5125 S := Make_Exit_Statement (Loc);
5126 else
5127 S :=
5128 Make_Assignment_Statement (Loc,
5129 Name => New_Occurrence_Of (Loop_Id, Loc),
5130 Expression => Hi_Val (Prev (P)));
5131 Set_Suppress_Assignment_Checks (S);
5132 end if;
5133
5134 Append_To (Alts,
5135 Make_Case_Statement_Alternative (Loc,
5136 Statements => New_List (S),
5137 Discrete_Choices => New_List (Lo_Val (P))));
5138
5139 Prev (P);
5140 end loop;
5141
5142 if Is_Itype (Ltype)
5143 and then Is_OK_Static_Expression (L_Lo)
5144 and then
5145 Expr_Value (L_Lo) /= Expr_Value (Lo_Val (First (Stat)))
5146 then
5147 Append_To (Alts,
5148 Make_Case_Statement_Alternative (Loc,
5149 Statements => New_List (Make_Exit_Statement (Loc)),
5150 Discrete_Choices => New_List (L_Lo)));
5151 end if;
5152
5153 else
5154 -- Initial value is smallest value in predicate
5155
5156 if Is_Itype (Ltype) then
5157 D :=
5158 Make_Object_Declaration (Loc,
5159 Defining_Identifier => Loop_Id,
5160 Object_Definition => New_Occurrence_Of (Ltype, Loc),
5161 Expression => L_Lo);
5162 else
5163 D :=
5164 Make_Object_Declaration (Loc,
5165 Defining_Identifier => Loop_Id,
5166 Object_Definition => New_Occurrence_Of (Ltype, Loc),
5167 Expression => Lo_Val (First (Stat)));
5168 end if;
5169
5170 P := First (Stat);
5171 while Present (P) loop
5172 if No (Next (P)) then
5173 S := Make_Exit_Statement (Loc);
5174 else
5175 S :=
5176 Make_Assignment_Statement (Loc,
5177 Name => New_Occurrence_Of (Loop_Id, Loc),
5178 Expression => Lo_Val (Next (P)));
5179 Set_Suppress_Assignment_Checks (S);
5180 end if;
5181
5182 Append_To (Alts,
5183 Make_Case_Statement_Alternative (Loc,
5184 Statements => New_List (S),
5185 Discrete_Choices => New_List (Hi_Val (P))));
5186
5187 Next (P);
5188 end loop;
5189
5190 if Is_Itype (Ltype)
5191 and then Is_OK_Static_Expression (L_Hi)
5192 and then
5193 Expr_Value (L_Hi) /= Expr_Value (Lo_Val (Last (Stat)))
5194 then
5195 Append_To (Alts,
5196 Make_Case_Statement_Alternative (Loc,
5197 Statements => New_List (Make_Exit_Statement (Loc)),
5198 Discrete_Choices => New_List (L_Hi)));
5199 end if;
5200 end if;
5201
5202 -- Add others choice
5203
5204 declare
5205 Name_Next : Name_Id;
5206
5207 begin
5208 if Reverse_Present (LPS) then
5209 Name_Next := Name_Pred;
5210 else
5211 Name_Next := Name_Succ;
5212 end if;
5213
5214 S :=
5215 Make_Assignment_Statement (Loc,
5216 Name => New_Occurrence_Of (Loop_Id, Loc),
5217 Expression =>
5218 Make_Attribute_Reference (Loc,
5219 Prefix => New_Occurrence_Of (Ltype, Loc),
5220 Attribute_Name => Name_Next,
5221 Expressions => New_List (
5222 New_Occurrence_Of (Loop_Id, Loc))));
5223 Set_Suppress_Assignment_Checks (S);
5224 end;
5225
5226 Append_To (Alts,
5227 Make_Case_Statement_Alternative (Loc,
5228 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5229 Statements => New_List (S)));
5230
5231 -- Construct case statement and append to body statements
5232
5233 Cstm :=
5234 Make_Case_Statement (Loc,
5235 Expression => New_Occurrence_Of (Loop_Id, Loc),
5236 Alternatives => Alts);
5237 Append_To (Stmts, Cstm);
5238
5239 -- Rewrite the loop preserving the loop identifier in case there
5240 -- are exit statements referencing it.
5241
5242 if Present (Identifier (N)) then
5243 Orig_Loop_Id := New_Occurrence_Of
5244 (Entity (Identifier (N)), Loc);
5245 end if;
5246
5247 Set_Suppress_Assignment_Checks (D);
5248
5249 Rewrite (N,
5250 Make_Block_Statement (Loc,
5251 Declarations => New_List (D),
5252 Handled_Statement_Sequence =>
5253 Make_Handled_Sequence_Of_Statements (Loc,
5254 Statements => New_List (
5255 Make_Loop_Statement (Loc,
5256 Statements => Stmts,
5257 Identifier => Orig_Loop_Id,
5258 End_Label => Empty)))));
5259
5260 Analyze (N);
5261 end Static_Predicate;
5262 end if;
5263 end Expand_Predicated_Loop;
5264
5265 ------------------------------
5266 -- Make_Tag_Ctrl_Assignment --
5267 ------------------------------
5268
5269 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
5270 Asn : constant Node_Id := Relocate_Node (N);
5271 L : constant Node_Id := Name (N);
5272 Loc : constant Source_Ptr := Sloc (N);
5273 Res : constant List_Id := New_List;
5274 T : constant Entity_Id := Underlying_Type (Etype (L));
5275
5276 Comp_Asn : constant Boolean := Is_Fully_Repped_Tagged_Type (T);
5277 Ctrl_Act : constant Boolean := Needs_Finalization (T)
5278 and then not No_Ctrl_Actions (N);
5279 Save_Tag : constant Boolean := Is_Tagged_Type (T)
5280 and then not Comp_Asn
5281 and then not No_Ctrl_Actions (N)
5282 and then Tagged_Type_Expansion;
5283 Adj_Call : Node_Id;
5284 Fin_Call : Node_Id;
5285 Tag_Id : Entity_Id;
5286
5287 begin
5288 -- Finalize the target of the assignment when controlled
5289
5290 -- We have two exceptions here:
5291
5292 -- 1. If we are in an init proc since it is an initialization more
5293 -- than an assignment.
5294
5295 -- 2. If the left-hand side is a temporary that was not initialized
5296 -- (or the parent part of a temporary since it is the case in
5297 -- extension aggregates). Such a temporary does not come from
5298 -- source. We must examine the original node for the prefix, because
5299 -- it may be a component of an entry formal, in which case it has
5300 -- been rewritten and does not appear to come from source either.
5301
5302 -- Case of init proc
5303
5304 if not Ctrl_Act then
5305 null;
5306
5307 -- The left-hand side is an uninitialized temporary object
5308
5309 elsif Nkind (L) = N_Type_Conversion
5310 and then Is_Entity_Name (Expression (L))
5311 and then Nkind (Parent (Entity (Expression (L)))) =
5312 N_Object_Declaration
5313 and then No_Initialization (Parent (Entity (Expression (L))))
5314 then
5315 null;
5316
5317 else
5318 Fin_Call :=
5319 Make_Final_Call
5320 (Obj_Ref => Duplicate_Subexpr_No_Checks (L),
5321 Typ => Etype (L));
5322
5323 if Present (Fin_Call) then
5324 Append_To (Res, Fin_Call);
5325 end if;
5326 end if;
5327
5328 -- Save the Tag in a local variable Tag_Id
5329
5330 if Save_Tag then
5331 Tag_Id := Make_Temporary (Loc, 'A');
5332
5333 Append_To (Res,
5334 Make_Object_Declaration (Loc,
5335 Defining_Identifier => Tag_Id,
5336 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
5337 Expression =>
5338 Make_Selected_Component (Loc,
5339 Prefix => Duplicate_Subexpr_No_Checks (L),
5340 Selector_Name =>
5341 New_Occurrence_Of (First_Tag_Component (T), Loc))));
5342
5343 -- Otherwise Tag_Id is not used
5344
5345 else
5346 Tag_Id := Empty;
5347 end if;
5348
5349 -- If the tagged type has a full rep clause, expand the assignment into
5350 -- component-wise assignments. Mark the node as unanalyzed in order to
5351 -- generate the proper code and propagate this scenario by setting a
5352 -- flag to avoid infinite recursion.
5353
5354 if Comp_Asn then
5355 Set_Analyzed (Asn, False);
5356 Set_Componentwise_Assignment (Asn, True);
5357 end if;
5358
5359 Append_To (Res, Asn);
5360
5361 -- Restore the tag
5362
5363 if Save_Tag then
5364 Append_To (Res,
5365 Make_Assignment_Statement (Loc,
5366 Name =>
5367 Make_Selected_Component (Loc,
5368 Prefix => Duplicate_Subexpr_No_Checks (L),
5369 Selector_Name =>
5370 New_Occurrence_Of (First_Tag_Component (T), Loc)),
5371 Expression => New_Occurrence_Of (Tag_Id, Loc)));
5372 end if;
5373
5374 -- Adjust the target after the assignment when controlled (not in the
5375 -- init proc since it is an initialization more than an assignment).
5376
5377 if Ctrl_Act then
5378 Adj_Call :=
5379 Make_Adjust_Call
5380 (Obj_Ref => Duplicate_Subexpr_Move_Checks (L),
5381 Typ => Etype (L));
5382
5383 if Present (Adj_Call) then
5384 Append_To (Res, Adj_Call);
5385 end if;
5386 end if;
5387
5388 return Res;
5389
5390 exception
5391
5392 -- Could use comment here ???
5393
5394 when RE_Not_Available =>
5395 return Empty_List;
5396 end Make_Tag_Ctrl_Assignment;
5397
5398 end Exp_Ch5;