2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / exp_ch5.adb
bloba257b274ce058e03fe189aa2bcdda9baace186b0
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-2003, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Exp_Aggr; use Exp_Aggr;
31 with Exp_Ch7; use Exp_Ch7;
32 with Exp_Ch11; use Exp_Ch11;
33 with Exp_Dbug; use Exp_Dbug;
34 with Exp_Pakd; use Exp_Pakd;
35 with Exp_Util; use Exp_Util;
36 with Hostparm; use Hostparm;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Restrict; use Restrict;
41 with Rtsfind; use Rtsfind;
42 with Sinfo; use Sinfo;
43 with Sem; use Sem;
44 with Sem_Ch8; use Sem_Ch8;
45 with Sem_Ch13; use Sem_Ch13;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res; use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Snames; use Snames;
50 with Stand; use Stand;
51 with Tbuild; use Tbuild;
52 with Ttypes; use Ttypes;
53 with Uintp; use Uintp;
54 with Validsw; use Validsw;
56 package body Exp_Ch5 is
58 function Change_Of_Representation (N : Node_Id) return Boolean;
59 -- Determine if the right hand side of the assignment N is a type
60 -- conversion which requires a change of representation. Called
61 -- only for the array and record cases.
63 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
64 -- N is an assignment which assigns an array value. This routine process
65 -- the various special cases and checks required for such assignments,
66 -- including change of representation. Rhs is normally simply the right
67 -- hand side of the assignment, except that if the right hand side is
68 -- a type conversion or a qualified expression, then the Rhs is the
69 -- actual expression inside any such type conversions or qualifications.
71 function Expand_Assign_Array_Loop
72 (N : Node_Id;
73 Larray : Entity_Id;
74 Rarray : Entity_Id;
75 L_Type : Entity_Id;
76 R_Type : Entity_Id;
77 Ndim : Pos;
78 Rev : Boolean)
79 return Node_Id;
80 -- N is an assignment statement which assigns an array value. This routine
81 -- expands the assignment into a loop (or nested loops for the case of a
82 -- multi-dimensional array) to do the assignment component by component.
83 -- Larray and Rarray are the entities of the actual arrays on the left
84 -- hand and right hand sides. L_Type and R_Type are the types of these
85 -- arrays (which may not be the same, due to either sliding, or to a
86 -- change of representation case). Ndim is the number of dimensions and
87 -- the parameter Rev indicates if the loops run normally (Rev = False),
88 -- or reversed (Rev = True). The value returned is the constructed
89 -- loop statement. Auxiliary declarations are inserted before node N
90 -- using the standard Insert_Actions mechanism.
92 procedure Expand_Assign_Record (N : Node_Id);
93 -- N is an assignment of a non-tagged record value. This routine handles
94 -- the case where the assignment must be made component by component,
95 -- either because the target is not byte aligned, or there is a change
96 -- of representation.
98 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
99 -- Generate the necessary code for controlled and Tagged assignment,
100 -- that is to say, finalization of the target before, adjustement of
101 -- the target after and save and restore of the tag and finalization
102 -- pointers which are not 'part of the value' and must not be changed
103 -- upon assignment. N is the original Assignment node.
105 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean;
106 -- This function is used in processing the assignment of a record or
107 -- indexed component. The back end can handle such assignments fine
108 -- if the objects involved are small (64-bits or less) records or
109 -- scalar items (including bit-packed arrays represented with modular
110 -- types) or are both aligned on a byte boundary (starting on a byte
111 -- boundary, and occupying an integral number of bytes).
113 -- However, problems arise for records larger than 64 bits, or for
114 -- arrays (other than bit-packed arrays represented with a modular
115 -- type) if the component starts on a non-byte boundary, or does
116 -- not occupy an integral number of bytes (i.e. there are some bits
117 -- possibly shared with fields at the start or beginning of the
118 -- component). The back end cannot handle loading and storing such
119 -- components in a single operation.
121 -- This function is used to detect the troublesome situation. it is
122 -- conservative in the sense that it produces True unless it knows
123 -- for sure that the component is safe (as outlined in the first
124 -- paragraph above). The code generation for record and array
125 -- assignment checks for trouble using this function, and if so
126 -- the assignment is generated component-wise, which the back end
127 -- is required to handle correctly.
129 -- Note that in GNAT 3, the back end will reject such components
130 -- anyway, so the hard work in checking for this case is wasted
131 -- in GNAT 3, but it's harmless, so it is easier to do it in
132 -- all cases, rather than conditionalize it in GNAT 5 or beyond.
134 ------------------------------
135 -- Change_Of_Representation --
136 ------------------------------
138 function Change_Of_Representation (N : Node_Id) return Boolean is
139 Rhs : constant Node_Id := Expression (N);
140 begin
141 return
142 Nkind (Rhs) = N_Type_Conversion
143 and then
144 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
145 end Change_Of_Representation;
147 -------------------------
148 -- Expand_Assign_Array --
149 -------------------------
151 -- There are two issues here. First, do we let Gigi do a block move, or
152 -- do we expand out into a loop? Second, we need to set the two flags
153 -- Forwards_OK and Backwards_OK which show whether the block move (or
154 -- corresponding loops) can be legitimately done in a forwards (low to
155 -- high) or backwards (high to low) manner.
157 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
158 Loc : constant Source_Ptr := Sloc (N);
160 Lhs : constant Node_Id := Name (N);
162 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
163 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
165 L_Type : constant Entity_Id :=
166 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
167 R_Type : Entity_Id :=
168 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
170 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
171 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
173 Crep : constant Boolean := Change_Of_Representation (N);
175 Larray : Node_Id;
176 Rarray : Node_Id;
178 Ndim : constant Pos := Number_Dimensions (L_Type);
180 Loop_Required : Boolean := False;
181 -- This switch is set to True if the array move must be done using
182 -- an explicit front end generated loop.
184 function Has_Address_Clause (Exp : Node_Id) return Boolean;
185 -- Test if Exp is a reference to an array whose declaration has
186 -- an address clause, or it is a slice of such an array.
188 function Is_Formal_Array (Exp : Node_Id) return Boolean;
189 -- Test if Exp is a reference to an array which is either a formal
190 -- parameter or a slice of a formal parameter. These are the cases
191 -- where hidden aliasing can occur.
193 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
194 -- Determine if Exp is a reference to an array variable which is other
195 -- than an object defined in the current scope, or a slice of such
196 -- an object. Such objects can be aliased to parameters (unlike local
197 -- array references).
199 function Possible_Unaligned_Slice (Arg : Node_Id) return Boolean;
200 -- Returns True if Arg (either the left or right hand side of the
201 -- assignment) is a slice that could be unaligned wrt the array type.
202 -- This is true if Arg is a component of a packed record, or is
203 -- a record component to which a component clause applies. This
204 -- is a little pessimistic, but the result of an unnecessary
205 -- decision that something is possibly unaligned is only to
206 -- generate a front end loop, which is not so terrible.
207 -- It would really be better if backend handled this ???
209 ------------------------
210 -- Has_Address_Clause --
211 ------------------------
213 function Has_Address_Clause (Exp : Node_Id) return Boolean is
214 begin
215 return
216 (Is_Entity_Name (Exp) and then
217 Present (Address_Clause (Entity (Exp))))
218 or else
219 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
220 end Has_Address_Clause;
222 ---------------------
223 -- Is_Formal_Array --
224 ---------------------
226 function Is_Formal_Array (Exp : Node_Id) return Boolean is
227 begin
228 return
229 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
230 or else
231 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
232 end Is_Formal_Array;
234 ------------------------
235 -- Is_Non_Local_Array --
236 ------------------------
238 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
239 begin
240 return (Is_Entity_Name (Exp)
241 and then Scope (Entity (Exp)) /= Current_Scope)
242 or else (Nkind (Exp) = N_Slice
243 and then Is_Non_Local_Array (Prefix (Exp)));
244 end Is_Non_Local_Array;
246 ------------------------------
247 -- Possible_Unaligned_Slice --
248 ------------------------------
250 function Possible_Unaligned_Slice (Arg : Node_Id) return Boolean is
251 begin
252 -- No issue if this is not a slice, or else strict alignment
253 -- is not required in any case.
255 if Nkind (Arg) /= N_Slice
256 or else not Target_Strict_Alignment
257 then
258 return False;
259 end if;
261 -- No issue if the component type is a byte or byte aligned
263 declare
264 Array_Typ : constant Entity_Id := Etype (Arg);
265 Comp_Typ : constant Entity_Id := Component_Type (Array_Typ);
266 Pref : constant Node_Id := Prefix (Arg);
268 begin
269 if Known_Alignment (Array_Typ) then
270 if Alignment (Array_Typ) = 1 then
271 return False;
272 end if;
274 elsif Known_Component_Size (Array_Typ) then
275 if Component_Size (Array_Typ) = 1 then
276 return False;
277 end if;
279 elsif Known_Esize (Comp_Typ) then
280 if Esize (Comp_Typ) <= System_Storage_Unit then
281 return False;
282 end if;
283 end if;
285 -- No issue if this is not a selected component
287 if Nkind (Pref) /= N_Selected_Component then
288 return False;
289 end if;
291 -- Else we test for a possibly unaligned component
293 return
294 Is_Packed (Etype (Pref))
295 or else
296 Present (Component_Clause (Entity (Selector_Name (Pref))));
297 end;
298 end Possible_Unaligned_Slice;
300 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
302 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
303 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
305 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
306 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
308 -- Start of processing for Expand_Assign_Array
310 begin
311 -- Deal with length check, note that the length check is done with
312 -- respect to the right hand side as given, not a possible underlying
313 -- renamed object, since this would generate incorrect extra checks.
315 Apply_Length_Check (Rhs, L_Type);
317 -- We start by assuming that the move can be done in either
318 -- direction, i.e. that the two sides are completely disjoint.
320 Set_Forwards_OK (N, True);
321 Set_Backwards_OK (N, True);
323 -- Normally it is only the slice case that can lead to overlap,
324 -- and explicit checks for slices are made below. But there is
325 -- one case where the slice can be implicit and invisible to us
326 -- and that is the case where we have a one dimensional array,
327 -- and either both operands are parameters, or one is a parameter
328 -- and the other is a global variable. In this case the parameter
329 -- could be a slice that overlaps with the other parameter.
331 -- Check for the case of slices requiring an explicit loop. Normally
332 -- it is only the explicit slice cases that bother us, but in the
333 -- case of one dimensional arrays, parameters can be slices that
334 -- are passed by reference, so we can have aliasing for assignments
335 -- from one parameter to another, or assignments between parameters
336 -- and nonlocal variables. However, if the array subtype is a
337 -- constrained first subtype in the parameter case, then we don't
338 -- have to worry about overlap, since slice assignments aren't
339 -- possible (other than for a slice denoting the whole array).
341 -- Note: overlap is never possible if there is a change of
342 -- representation, so we can exclude this case.
344 if Ndim = 1
345 and then not Crep
346 and then
347 ((Lhs_Formal and Rhs_Formal)
348 or else
349 (Lhs_Formal and Rhs_Non_Local_Var)
350 or else
351 (Rhs_Formal and Lhs_Non_Local_Var))
352 and then
353 (not Is_Constrained (Etype (Lhs))
354 or else not Is_First_Subtype (Etype (Lhs)))
356 -- In the case of compiling for the Java Virtual Machine,
357 -- slices are always passed by making a copy, so we don't
358 -- have to worry about overlap. We also want to prevent
359 -- generation of "<" comparisons for array addresses,
360 -- since that's a meaningless operation on the JVM.
362 and then not Java_VM
363 then
364 Set_Forwards_OK (N, False);
365 Set_Backwards_OK (N, False);
367 -- Note: the bit-packed case is not worrisome here, since if
368 -- we have a slice passed as a parameter, it is always aligned
369 -- on a byte boundary, and if there are no explicit slices, the
370 -- assignment can be performed directly.
371 end if;
373 -- We certainly must use a loop for change of representation
374 -- and also we use the operand of the conversion on the right
375 -- hand side as the effective right hand side (the component
376 -- types must match in this situation).
378 if Crep then
379 Act_Rhs := Get_Referenced_Object (Rhs);
380 R_Type := Get_Actual_Subtype (Act_Rhs);
381 Loop_Required := True;
383 -- We require a loop if the left side is possibly bit unaligned
385 elsif Possible_Bit_Aligned_Component (Lhs)
386 or else
387 Possible_Bit_Aligned_Component (Rhs)
388 then
389 Loop_Required := True;
391 -- Arrays with controlled components are expanded into a loop
392 -- to force calls to adjust at the component level.
394 elsif Has_Controlled_Component (L_Type) then
395 Loop_Required := True;
397 -- Case where no slice is involved
399 elsif not L_Slice and not R_Slice then
401 -- The following code deals with the case of unconstrained bit
402 -- packed arrays. The problem is that the template for such
403 -- arrays contains the bounds of the actual source level array,
405 -- But the copy of an entire array requires the bounds of the
406 -- underlying array. It would be nice if the back end could take
407 -- care of this, but right now it does not know how, so if we
408 -- have such a type, then we expand out into a loop, which is
409 -- inefficient but works correctly. If we don't do this, we
410 -- get the wrong length computed for the array to be moved.
411 -- The two cases we need to worry about are:
413 -- Explicit deference of an unconstrained packed array type as
414 -- in the following example:
416 -- procedure C52 is
417 -- type BITS is array(INTEGER range <>) of BOOLEAN;
418 -- pragma PACK(BITS);
419 -- type A is access BITS;
420 -- P1,P2 : A;
421 -- begin
422 -- P1 := new BITS (1 .. 65_535);
423 -- P2 := new BITS (1 .. 65_535);
424 -- P2.ALL := P1.ALL;
425 -- end C52;
427 -- A formal parameter reference with an unconstrained bit
428 -- array type is the other case we need to worry about (here
429 -- we assume the same BITS type declared above:
431 -- procedure Write_All (File : out BITS; Contents : in BITS);
432 -- begin
433 -- File.Storage := Contents;
434 -- end Write_All;
436 -- We expand to a loop in either of these two cases.
438 -- Question for future thought. Another potentially more efficient
439 -- approach would be to create the actual subtype, and then do an
440 -- unchecked conversion to this actual subtype ???
442 Check_Unconstrained_Bit_Packed_Array : declare
444 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
445 -- Function to perform required test for the first case,
446 -- above (dereference of an unconstrained bit packed array)
448 -----------------------
449 -- Is_UBPA_Reference --
450 -----------------------
452 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
453 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
454 P_Type : Entity_Id;
455 Des_Type : Entity_Id;
457 begin
458 if Present (Packed_Array_Type (Typ))
459 and then Is_Array_Type (Packed_Array_Type (Typ))
460 and then not Is_Constrained (Packed_Array_Type (Typ))
461 then
462 return True;
464 elsif Nkind (Opnd) = N_Explicit_Dereference then
465 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
467 if not Is_Access_Type (P_Type) then
468 return False;
470 else
471 Des_Type := Designated_Type (P_Type);
472 return
473 Is_Bit_Packed_Array (Des_Type)
474 and then not Is_Constrained (Des_Type);
475 end if;
477 else
478 return False;
479 end if;
480 end Is_UBPA_Reference;
482 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
484 begin
485 if Is_UBPA_Reference (Lhs)
486 or else
487 Is_UBPA_Reference (Rhs)
488 then
489 Loop_Required := True;
491 -- Here if we do not have the case of a reference to a bit
492 -- packed unconstrained array case. In this case gigi can
493 -- most certainly handle the assignment if a forwards move
494 -- is allowed.
496 -- (could it handle the backwards case also???)
498 elsif Forwards_OK (N) then
499 return;
500 end if;
501 end Check_Unconstrained_Bit_Packed_Array;
503 -- Gigi can always handle the assignment if the right side is a string
504 -- literal (note that overlap is definitely impossible in this case).
505 -- If the type is packed, a string literal is always converted into a
506 -- aggregate, except in the case of a null slice, for which no aggregate
507 -- can be written. In that case, rewrite the assignment as a null
508 -- statement, a length check has already been emitted to verify that
509 -- the range of the left-hand side is empty.
511 elsif Nkind (Rhs) = N_String_Literal then
512 if Ekind (R_Type) = E_String_Literal_Subtype
513 and then String_Literal_Length (R_Type) = 0
514 and then Is_Bit_Packed_Array (L_Type)
515 then
516 Rewrite (N, Make_Null_Statement (Loc));
517 Analyze (N);
518 end if;
520 return;
522 -- If either operand is bit packed, then we need a loop, since we
523 -- can't be sure that the slice is byte aligned. Similarly, if either
524 -- operand is a possibly unaligned slice, then we need a loop (since
525 -- gigi cannot handle unaligned slices).
527 elsif Is_Bit_Packed_Array (L_Type)
528 or else Is_Bit_Packed_Array (R_Type)
529 or else Possible_Unaligned_Slice (Lhs)
530 or else Possible_Unaligned_Slice (Rhs)
531 then
532 Loop_Required := True;
534 -- If we are not bit-packed, and we have only one slice, then no
535 -- overlap is possible except in the parameter case, so we can let
536 -- gigi handle things.
538 elsif not (L_Slice and R_Slice) then
539 if Forwards_OK (N) then
540 return;
541 end if;
542 end if;
544 -- Come here to compelete the analysis
546 -- Loop_Required: Set to True if we know that a loop is required
547 -- regardless of overlap considerations.
549 -- Forwards_OK: Set to False if we already know that a forwards
550 -- move is not safe, else set to True.
552 -- Backwards_OK: Set to False if we already know that a backwards
553 -- move is not safe, else set to True
555 -- Our task at this stage is to complete the overlap analysis, which
556 -- can result in possibly setting Forwards_OK or Backwards_OK to
557 -- False, and then generating the final code, either by deciding
558 -- that it is OK after all to let Gigi handle it, or by generating
559 -- appropriate code in the front end.
561 declare
562 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
563 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
565 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
566 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
567 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
568 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
570 Act_L_Array : Node_Id;
571 Act_R_Array : Node_Id;
573 Cleft_Lo : Node_Id;
574 Cright_Lo : Node_Id;
575 Condition : Node_Id;
577 Cresult : Compare_Result;
579 begin
580 -- Get the expressions for the arrays. If we are dealing with a
581 -- private type, then convert to the underlying type. We can do
582 -- direct assignments to an array that is a private type, but
583 -- we cannot assign to elements of the array without this extra
584 -- unchecked conversion.
586 if Nkind (Act_Lhs) = N_Slice then
587 Larray := Prefix (Act_Lhs);
588 else
589 Larray := Act_Lhs;
591 if Is_Private_Type (Etype (Larray)) then
592 Larray :=
593 Unchecked_Convert_To
594 (Underlying_Type (Etype (Larray)), Larray);
595 end if;
596 end if;
598 if Nkind (Act_Rhs) = N_Slice then
599 Rarray := Prefix (Act_Rhs);
600 else
601 Rarray := Act_Rhs;
603 if Is_Private_Type (Etype (Rarray)) then
604 Rarray :=
605 Unchecked_Convert_To
606 (Underlying_Type (Etype (Rarray)), Rarray);
607 end if;
608 end if;
610 -- If both sides are slices, we must figure out whether
611 -- it is safe to do the move in one direction or the other
612 -- It is always safe if there is a change of representation
613 -- since obviously two arrays with different representations
614 -- cannot possibly overlap.
616 if (not Crep) and L_Slice and R_Slice then
617 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
618 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
620 -- If both left and right hand arrays are entity names, and
621 -- refer to different entities, then we know that the move
622 -- is safe (the two storage areas are completely disjoint).
624 if Is_Entity_Name (Act_L_Array)
625 and then Is_Entity_Name (Act_R_Array)
626 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
627 then
628 null;
630 -- Otherwise, we assume the worst, which is that the two
631 -- arrays are the same array. There is no need to check if
632 -- we know that is the case, because if we don't know it,
633 -- we still have to assume it!
635 -- Generally if the same array is involved, then we have
636 -- an overlapping case. We will have to really assume the
637 -- worst (i.e. set neither of the OK flags) unless we can
638 -- determine the lower or upper bounds at compile time and
639 -- compare them.
641 else
642 Cresult := Compile_Time_Compare (Left_Lo, Right_Lo);
644 if Cresult = Unknown then
645 Cresult := Compile_Time_Compare (Left_Hi, Right_Hi);
646 end if;
648 case Cresult is
649 when LT | LE | EQ => Set_Backwards_OK (N, False);
650 when GT | GE => Set_Forwards_OK (N, False);
651 when NE | Unknown => Set_Backwards_OK (N, False);
652 Set_Forwards_OK (N, False);
653 end case;
654 end if;
655 end if;
657 -- If after that analysis, Forwards_OK is still True, and
658 -- Loop_Required is False, meaning that we have not discovered
659 -- some non-overlap reason for requiring a loop, then we can
660 -- still let gigi handle it.
662 if not Loop_Required then
663 if Forwards_OK (N) then
664 return;
666 else
667 null;
668 -- Here is where a memmove would be appropriate ???
669 end if;
670 end if;
672 -- At this stage we have to generate an explicit loop, and
673 -- we have the following cases:
675 -- Forwards_OK = True
677 -- Rnn : right_index := right_index'First;
678 -- for Lnn in left-index loop
679 -- left (Lnn) := right (Rnn);
680 -- Rnn := right_index'Succ (Rnn);
681 -- end loop;
683 -- Note: the above code MUST be analyzed with checks off,
684 -- because otherwise the Succ could overflow. But in any
685 -- case this is more efficient!
687 -- Forwards_OK = False, Backwards_OK = True
689 -- Rnn : right_index := right_index'Last;
690 -- for Lnn in reverse left-index loop
691 -- left (Lnn) := right (Rnn);
692 -- Rnn := right_index'Pred (Rnn);
693 -- end loop;
695 -- Note: the above code MUST be analyzed with checks off,
696 -- because otherwise the Pred could overflow. But in any
697 -- case this is more efficient!
699 -- Forwards_OK = Backwards_OK = False
701 -- This only happens if we have the same array on each side. It is
702 -- possible to create situations using overlays that violate this,
703 -- but we simply do not promise to get this "right" in this case.
705 -- There are two possible subcases. If the No_Implicit_Conditionals
706 -- restriction is set, then we generate the following code:
708 -- declare
709 -- T : constant <operand-type> := rhs;
710 -- begin
711 -- lhs := T;
712 -- end;
714 -- If implicit conditionals are permitted, then we generate:
716 -- if Left_Lo <= Right_Lo then
717 -- <code for Forwards_OK = True above>
718 -- else
719 -- <code for Backwards_OK = True above>
720 -- end if;
722 -- Cases where either Forwards_OK or Backwards_OK is true
724 if Forwards_OK (N) or else Backwards_OK (N) then
725 Rewrite (N,
726 Expand_Assign_Array_Loop
727 (N, Larray, Rarray, L_Type, R_Type, Ndim,
728 Rev => not Forwards_OK (N)));
730 -- Case of both are false with No_Implicit_Conditionals
732 elsif Restrictions (No_Implicit_Conditionals) then
733 declare
734 T : constant Entity_Id := Make_Defining_Identifier (Loc,
735 Chars => Name_T);
737 begin
738 Rewrite (N,
739 Make_Block_Statement (Loc,
740 Declarations => New_List (
741 Make_Object_Declaration (Loc,
742 Defining_Identifier => T,
743 Constant_Present => True,
744 Object_Definition =>
745 New_Occurrence_Of (Etype (Rhs), Loc),
746 Expression => Relocate_Node (Rhs))),
748 Handled_Statement_Sequence =>
749 Make_Handled_Sequence_Of_Statements (Loc,
750 Statements => New_List (
751 Make_Assignment_Statement (Loc,
752 Name => Relocate_Node (Lhs),
753 Expression => New_Occurrence_Of (T, Loc))))));
754 end;
756 -- Case of both are false with implicit conditionals allowed
758 else
759 -- Before we generate this code, we must ensure that the
760 -- left and right side array types are defined. They may
761 -- be itypes, and we cannot let them be defined inside the
762 -- if, since the first use in the then may not be executed.
764 Ensure_Defined (L_Type, N);
765 Ensure_Defined (R_Type, N);
767 -- We normally compare addresses to find out which way round
768 -- to do the loop, since this is realiable, and handles the
769 -- cases of parameters, conversions etc. But we can't do that
770 -- in the bit packed case or the Java VM case, because addresses
771 -- don't work there.
773 if not Is_Bit_Packed_Array (L_Type) and then not Java_VM then
774 Condition :=
775 Make_Op_Le (Loc,
776 Left_Opnd =>
777 Unchecked_Convert_To (RTE (RE_Integer_Address),
778 Make_Attribute_Reference (Loc,
779 Prefix =>
780 Make_Indexed_Component (Loc,
781 Prefix =>
782 Duplicate_Subexpr_Move_Checks (Larray, True),
783 Expressions => New_List (
784 Make_Attribute_Reference (Loc,
785 Prefix =>
786 New_Reference_To
787 (L_Index_Typ, Loc),
788 Attribute_Name => Name_First))),
789 Attribute_Name => Name_Address)),
791 Right_Opnd =>
792 Unchecked_Convert_To (RTE (RE_Integer_Address),
793 Make_Attribute_Reference (Loc,
794 Prefix =>
795 Make_Indexed_Component (Loc,
796 Prefix =>
797 Duplicate_Subexpr_Move_Checks (Rarray, True),
798 Expressions => New_List (
799 Make_Attribute_Reference (Loc,
800 Prefix =>
801 New_Reference_To
802 (R_Index_Typ, Loc),
803 Attribute_Name => Name_First))),
804 Attribute_Name => Name_Address)));
806 -- For the bit packed and Java VM cases we use the bounds.
807 -- That's OK, because we don't have to worry about parameters,
808 -- since they cannot cause overlap. Perhaps we should worry
809 -- about weird slice conversions ???
811 else
812 -- Copy the bounds and reset the Analyzed flag, because the
813 -- bounds of the index type itself may be universal, and must
814 -- must be reaanalyzed to acquire the proper type for Gigi.
816 Cleft_Lo := New_Copy_Tree (Left_Lo);
817 Cright_Lo := New_Copy_Tree (Right_Lo);
818 Set_Analyzed (Cleft_Lo, False);
819 Set_Analyzed (Cright_Lo, False);
821 Condition :=
822 Make_Op_Le (Loc,
823 Left_Opnd => Cleft_Lo,
824 Right_Opnd => Cright_Lo);
825 end if;
827 Rewrite (N,
828 Make_Implicit_If_Statement (N,
829 Condition => Condition,
831 Then_Statements => New_List (
832 Expand_Assign_Array_Loop
833 (N, Larray, Rarray, L_Type, R_Type, Ndim,
834 Rev => False)),
836 Else_Statements => New_List (
837 Expand_Assign_Array_Loop
838 (N, Larray, Rarray, L_Type, R_Type, Ndim,
839 Rev => True))));
840 end if;
842 Analyze (N, Suppress => All_Checks);
843 end;
845 exception
846 when RE_Not_Available =>
847 return;
848 end Expand_Assign_Array;
850 ------------------------------
851 -- Expand_Assign_Array_Loop --
852 ------------------------------
854 -- The following is an example of the loop generated for the case of
855 -- a two-dimensional array:
857 -- declare
858 -- R2b : Tm1X1 := 1;
859 -- begin
860 -- for L1b in 1 .. 100 loop
861 -- declare
862 -- R4b : Tm1X2 := 1;
863 -- begin
864 -- for L3b in 1 .. 100 loop
865 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
866 -- R4b := Tm1X2'succ(R4b);
867 -- end loop;
868 -- end;
869 -- R2b := Tm1X1'succ(R2b);
870 -- end loop;
871 -- end;
873 -- Here Rev is False, and Tm1Xn are the subscript types for the right
874 -- hand side. The declarations of R2b and R4b are inserted before the
875 -- original assignment statement.
877 function Expand_Assign_Array_Loop
878 (N : Node_Id;
879 Larray : Entity_Id;
880 Rarray : Entity_Id;
881 L_Type : Entity_Id;
882 R_Type : Entity_Id;
883 Ndim : Pos;
884 Rev : Boolean)
885 return Node_Id
887 Loc : constant Source_Ptr := Sloc (N);
889 Lnn : array (1 .. Ndim) of Entity_Id;
890 Rnn : array (1 .. Ndim) of Entity_Id;
891 -- Entities used as subscripts on left and right sides
893 L_Index_Type : array (1 .. Ndim) of Entity_Id;
894 R_Index_Type : array (1 .. Ndim) of Entity_Id;
895 -- Left and right index types
897 Assign : Node_Id;
899 F_Or_L : Name_Id;
900 S_Or_P : Name_Id;
902 begin
903 if Rev then
904 F_Or_L := Name_Last;
905 S_Or_P := Name_Pred;
906 else
907 F_Or_L := Name_First;
908 S_Or_P := Name_Succ;
909 end if;
911 -- Setup index types and subscript entities
913 declare
914 L_Index : Node_Id;
915 R_Index : Node_Id;
917 begin
918 L_Index := First_Index (L_Type);
919 R_Index := First_Index (R_Type);
921 for J in 1 .. Ndim loop
922 Lnn (J) :=
923 Make_Defining_Identifier (Loc,
924 Chars => New_Internal_Name ('L'));
926 Rnn (J) :=
927 Make_Defining_Identifier (Loc,
928 Chars => New_Internal_Name ('R'));
930 L_Index_Type (J) := Etype (L_Index);
931 R_Index_Type (J) := Etype (R_Index);
933 Next_Index (L_Index);
934 Next_Index (R_Index);
935 end loop;
936 end;
938 -- Now construct the assignment statement
940 declare
941 ExprL : constant List_Id := New_List;
942 ExprR : constant List_Id := New_List;
944 begin
945 for J in 1 .. Ndim loop
946 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
947 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
948 end loop;
950 Assign :=
951 Make_Assignment_Statement (Loc,
952 Name =>
953 Make_Indexed_Component (Loc,
954 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
955 Expressions => ExprL),
956 Expression =>
957 Make_Indexed_Component (Loc,
958 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
959 Expressions => ExprR));
961 -- Propagate the No_Ctrl_Actions flag to individual assignments
963 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
964 end;
966 -- Now construct the loop from the inside out, with the last subscript
967 -- varying most rapidly. Note that Assign is first the raw assignment
968 -- statement, and then subsequently the loop that wraps it up.
970 for J in reverse 1 .. Ndim loop
971 Assign :=
972 Make_Block_Statement (Loc,
973 Declarations => New_List (
974 Make_Object_Declaration (Loc,
975 Defining_Identifier => Rnn (J),
976 Object_Definition =>
977 New_Occurrence_Of (R_Index_Type (J), Loc),
978 Expression =>
979 Make_Attribute_Reference (Loc,
980 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
981 Attribute_Name => F_Or_L))),
983 Handled_Statement_Sequence =>
984 Make_Handled_Sequence_Of_Statements (Loc,
985 Statements => New_List (
986 Make_Implicit_Loop_Statement (N,
987 Iteration_Scheme =>
988 Make_Iteration_Scheme (Loc,
989 Loop_Parameter_Specification =>
990 Make_Loop_Parameter_Specification (Loc,
991 Defining_Identifier => Lnn (J),
992 Reverse_Present => Rev,
993 Discrete_Subtype_Definition =>
994 New_Reference_To (L_Index_Type (J), Loc))),
996 Statements => New_List (
997 Assign,
999 Make_Assignment_Statement (Loc,
1000 Name => New_Occurrence_Of (Rnn (J), Loc),
1001 Expression =>
1002 Make_Attribute_Reference (Loc,
1003 Prefix =>
1004 New_Occurrence_Of (R_Index_Type (J), Loc),
1005 Attribute_Name => S_Or_P,
1006 Expressions => New_List (
1007 New_Occurrence_Of (Rnn (J), Loc)))))))));
1008 end loop;
1010 return Assign;
1011 end Expand_Assign_Array_Loop;
1013 --------------------------
1014 -- Expand_Assign_Record --
1015 --------------------------
1017 -- The only processing required is in the change of representation
1018 -- case, where we must expand the assignment to a series of field
1019 -- by field assignments.
1021 procedure Expand_Assign_Record (N : Node_Id) is
1022 Lhs : constant Node_Id := Name (N);
1023 Rhs : Node_Id := Expression (N);
1025 begin
1026 -- If change of representation, then extract the real right hand
1027 -- side from the type conversion, and proceed with component-wise
1028 -- assignment, since the two types are not the same as far as the
1029 -- back end is concerned.
1031 if Change_Of_Representation (N) then
1032 Rhs := Expression (Rhs);
1034 -- If this may be a case of a large bit aligned component, then
1035 -- proceed with component-wise assignment, to avoid possible
1036 -- clobbering of other components sharing bits in the first or
1037 -- last byte of the component to be assigned.
1039 elsif Possible_Bit_Aligned_Component (Lhs)
1041 Possible_Bit_Aligned_Component (Rhs)
1042 then
1043 null;
1045 -- If neither condition met, then nothing special to do, the back end
1046 -- can handle assignment of the entire component as a single entity.
1048 else
1049 return;
1050 end if;
1052 -- At this stage we know that we must do a component wise assignment
1054 declare
1055 Loc : constant Source_Ptr := Sloc (N);
1056 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1057 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1058 Decl : constant Node_Id := Declaration_Node (R_Typ);
1059 RDef : Node_Id;
1060 F : Entity_Id;
1062 function Find_Component
1063 (Typ : Entity_Id;
1064 Comp : Entity_Id) return Entity_Id;
1065 -- Find the component with the given name in the underlying record
1066 -- declaration for Typ. We need to use the actual entity because
1067 -- the type may be private and resolution by identifier alone would
1068 -- fail.
1070 function Make_Component_List_Assign (CL : Node_Id) return List_Id;
1071 -- Returns a sequence of statements to assign the components that
1072 -- are referenced in the given component list.
1074 function Make_Field_Assign (C : Entity_Id) return Node_Id;
1075 -- Given C, the entity for a discriminant or component, build
1076 -- an assignment for the corresponding field values.
1078 function Make_Field_Assigns (CI : List_Id) return List_Id;
1079 -- Given CI, a component items list, construct series of statements
1080 -- for fieldwise assignment of the corresponding components.
1082 --------------------
1083 -- Find_Component --
1084 --------------------
1086 function Find_Component
1087 (Typ : Entity_Id;
1088 Comp : Entity_Id) return Entity_Id
1090 Utyp : constant Entity_Id := Underlying_Type (Typ);
1091 C : Entity_Id;
1093 begin
1094 C := First_Entity (Utyp);
1096 while Present (C) loop
1097 if Chars (C) = Chars (Comp) then
1098 return C;
1099 end if;
1100 Next_Entity (C);
1101 end loop;
1103 raise Program_Error;
1104 end Find_Component;
1106 --------------------------------
1107 -- Make_Component_List_Assign --
1108 --------------------------------
1110 function Make_Component_List_Assign (CL : Node_Id) return List_Id is
1111 CI : constant List_Id := Component_Items (CL);
1112 VP : constant Node_Id := Variant_Part (CL);
1114 Result : List_Id;
1115 Alts : List_Id;
1116 V : Node_Id;
1117 DC : Node_Id;
1118 DCH : List_Id;
1120 begin
1121 Result := Make_Field_Assigns (CI);
1123 if Present (VP) then
1125 V := First_Non_Pragma (Variants (VP));
1126 Alts := New_List;
1127 while Present (V) loop
1129 DCH := New_List;
1130 DC := First (Discrete_Choices (V));
1131 while Present (DC) loop
1132 Append_To (DCH, New_Copy_Tree (DC));
1133 Next (DC);
1134 end loop;
1136 Append_To (Alts,
1137 Make_Case_Statement_Alternative (Loc,
1138 Discrete_Choices => DCH,
1139 Statements =>
1140 Make_Component_List_Assign (Component_List (V))));
1141 Next_Non_Pragma (V);
1142 end loop;
1144 Append_To (Result,
1145 Make_Case_Statement (Loc,
1146 Expression =>
1147 Make_Selected_Component (Loc,
1148 Prefix => Duplicate_Subexpr (Rhs),
1149 Selector_Name =>
1150 Make_Identifier (Loc, Chars (Name (VP)))),
1151 Alternatives => Alts));
1153 end if;
1155 return Result;
1156 end Make_Component_List_Assign;
1158 -----------------------
1159 -- Make_Field_Assign --
1160 -----------------------
1162 function Make_Field_Assign (C : Entity_Id) return Node_Id is
1163 A : Node_Id;
1165 begin
1166 A :=
1167 Make_Assignment_Statement (Loc,
1168 Name =>
1169 Make_Selected_Component (Loc,
1170 Prefix => Duplicate_Subexpr (Lhs),
1171 Selector_Name =>
1172 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1173 Expression =>
1174 Make_Selected_Component (Loc,
1175 Prefix => Duplicate_Subexpr (Rhs),
1176 Selector_Name => New_Occurrence_Of (C, Loc)));
1178 -- Set Assignment_OK, so discriminants can be assigned
1180 Set_Assignment_OK (Name (A), True);
1181 return A;
1182 end Make_Field_Assign;
1184 ------------------------
1185 -- Make_Field_Assigns --
1186 ------------------------
1188 function Make_Field_Assigns (CI : List_Id) return List_Id is
1189 Item : Node_Id;
1190 Result : List_Id;
1192 begin
1193 Item := First (CI);
1194 Result := New_List;
1196 while Present (Item) loop
1197 if Nkind (Item) = N_Component_Declaration then
1198 Append_To
1199 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1200 end if;
1202 Next (Item);
1203 end loop;
1205 return Result;
1206 end Make_Field_Assigns;
1208 -- Start of processing for Expand_Assign_Record
1210 begin
1211 -- Note that we use the base types for this processing. This results
1212 -- in some extra work in the constrained case, but the change of
1213 -- representation case is so unusual that it is not worth the effort.
1215 -- First copy the discriminants. This is done unconditionally. It
1216 -- is required in the unconstrained left side case, and also in the
1217 -- case where this assignment was constructed during the expansion
1218 -- of a type conversion (since initialization of discriminants is
1219 -- suppressed in this case). It is unnecessary but harmless in
1220 -- other cases.
1222 if Has_Discriminants (L_Typ) then
1223 F := First_Discriminant (R_Typ);
1224 while Present (F) loop
1225 Insert_Action (N, Make_Field_Assign (F));
1226 Next_Discriminant (F);
1227 end loop;
1228 end if;
1230 -- We know the underlying type is a record, but its current view
1231 -- may be private. We must retrieve the usable record declaration.
1233 if Nkind (Decl) = N_Private_Type_Declaration
1234 and then Present (Full_View (R_Typ))
1235 then
1236 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1237 else
1238 RDef := Type_Definition (Decl);
1239 end if;
1241 if Nkind (RDef) = N_Record_Definition
1242 and then Present (Component_List (RDef))
1243 then
1244 Insert_Actions
1245 (N, Make_Component_List_Assign (Component_List (RDef)));
1247 Rewrite (N, Make_Null_Statement (Loc));
1248 end if;
1250 end;
1251 end Expand_Assign_Record;
1253 -----------------------------------
1254 -- Expand_N_Assignment_Statement --
1255 -----------------------------------
1257 -- For array types, deal with slice assignments and setting the flags
1258 -- to indicate if it can be statically determined which direction the
1259 -- move should go in. Also deal with generating range/length checks.
1261 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1262 Loc : constant Source_Ptr := Sloc (N);
1263 Lhs : constant Node_Id := Name (N);
1264 Rhs : constant Node_Id := Expression (N);
1265 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1266 Exp : Node_Id;
1268 begin
1269 -- First deal with generation of range check if required. For now
1270 -- we do this only for discrete types.
1272 if Do_Range_Check (Rhs)
1273 and then Is_Discrete_Type (Typ)
1274 then
1275 Set_Do_Range_Check (Rhs, False);
1276 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1277 end if;
1279 -- Check for a special case where a high level transformation is
1280 -- required. If we have either of:
1282 -- P.field := rhs;
1283 -- P (sub) := rhs;
1285 -- where P is a reference to a bit packed array, then we have to unwind
1286 -- the assignment. The exact meaning of being a reference to a bit
1287 -- packed array is as follows:
1289 -- An indexed component whose prefix is a bit packed array is a
1290 -- reference to a bit packed array.
1292 -- An indexed component or selected component whose prefix is a
1293 -- reference to a bit packed array is itself a reference ot a
1294 -- bit packed array.
1296 -- The required transformation is
1298 -- Tnn : prefix_type := P;
1299 -- Tnn.field := rhs;
1300 -- P := Tnn;
1302 -- or
1304 -- Tnn : prefix_type := P;
1305 -- Tnn (subscr) := rhs;
1306 -- P := Tnn;
1308 -- Since P is going to be evaluated more than once, any subscripts
1309 -- in P must have their evaluation forced.
1311 if (Nkind (Lhs) = N_Indexed_Component
1312 or else
1313 Nkind (Lhs) = N_Selected_Component)
1314 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1315 then
1316 declare
1317 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1318 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1319 Tnn : constant Entity_Id :=
1320 Make_Defining_Identifier (Loc,
1321 Chars => New_Internal_Name ('T'));
1323 begin
1324 -- Insert the post assignment first, because we want to copy
1325 -- the BPAR_Expr tree before it gets analyzed in the context
1326 -- of the pre assignment. Note that we do not analyze the
1327 -- post assignment yet (we cannot till we have completed the
1328 -- analysis of the pre assignment). As usual, the analysis
1329 -- of this post assignment will happen on its own when we
1330 -- "run into" it after finishing the current assignment.
1332 Insert_After (N,
1333 Make_Assignment_Statement (Loc,
1334 Name => New_Copy_Tree (BPAR_Expr),
1335 Expression => New_Occurrence_Of (Tnn, Loc)));
1337 -- At this stage BPAR_Expr is a reference to a bit packed
1338 -- array where the reference was not expanded in the original
1339 -- tree, since it was on the left side of an assignment. But
1340 -- in the pre-assignment statement (the object definition),
1341 -- BPAR_Expr will end up on the right hand side, and must be
1342 -- reexpanded. To achieve this, we reset the analyzed flag
1343 -- of all selected and indexed components down to the actual
1344 -- indexed component for the packed array.
1346 Exp := BPAR_Expr;
1347 loop
1348 Set_Analyzed (Exp, False);
1350 if Nkind (Exp) = N_Selected_Component
1351 or else
1352 Nkind (Exp) = N_Indexed_Component
1353 then
1354 Exp := Prefix (Exp);
1355 else
1356 exit;
1357 end if;
1358 end loop;
1360 -- Now we can insert and analyze the pre-assignment.
1362 -- If the right-hand side requires a transient scope, it has
1363 -- already been placed on the stack. However, the declaration is
1364 -- inserted in the tree outside of this scope, and must reflect
1365 -- the proper scope for its variable. This awkward bit is forced
1366 -- by the stricter scope discipline imposed by GCC 2.97.
1368 declare
1369 Uses_Transient_Scope : constant Boolean :=
1370 Scope_Is_Transient and then N = Node_To_Be_Wrapped;
1372 begin
1373 if Uses_Transient_Scope then
1374 New_Scope (Scope (Current_Scope));
1375 end if;
1377 Insert_Before_And_Analyze (N,
1378 Make_Object_Declaration (Loc,
1379 Defining_Identifier => Tnn,
1380 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1381 Expression => BPAR_Expr));
1383 if Uses_Transient_Scope then
1384 Pop_Scope;
1385 end if;
1386 end;
1388 -- Now fix up the original assignment and continue processing
1390 Rewrite (Prefix (Lhs),
1391 New_Occurrence_Of (Tnn, Loc));
1393 -- We do not need to reanalyze that assignment, and we do not need
1394 -- to worry about references to the temporary, but we do need to
1395 -- make sure that the temporary is not marked as a true constant
1396 -- since we now have a generate assignment to it!
1398 Set_Is_True_Constant (Tnn, False);
1399 end;
1400 end if;
1402 -- When we have the appropriate type of aggregate in the
1403 -- expression (it has been determined during analysis of the
1404 -- aggregate by setting the delay flag), let's perform in place
1405 -- assignment and thus avoid creating a temporay.
1407 if Is_Delayed_Aggregate (Rhs) then
1408 Convert_Aggr_In_Assignment (N);
1409 Rewrite (N, Make_Null_Statement (Loc));
1410 Analyze (N);
1411 return;
1412 end if;
1414 -- Apply discriminant check if required. If Lhs is an access type
1415 -- to a designated type with discriminants, we must always check.
1417 if Has_Discriminants (Etype (Lhs)) then
1419 -- Skip discriminant check if change of representation. Will be
1420 -- done when the change of representation is expanded out.
1422 if not Change_Of_Representation (N) then
1423 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1424 end if;
1426 -- If the type is private without discriminants, and the full type
1427 -- has discriminants (necessarily with defaults) a check may still be
1428 -- necessary if the Lhs is aliased. The private determinants must be
1429 -- visible to build the discriminant constraints.
1431 -- Only an explicit dereference that comes from source indicates
1432 -- aliasing. Access to formals of protected operations and entries
1433 -- create dereferences but are not semantic aliasings.
1435 elsif Is_Private_Type (Etype (Lhs))
1436 and then Has_Discriminants (Typ)
1437 and then Nkind (Lhs) = N_Explicit_Dereference
1438 and then Comes_From_Source (Lhs)
1439 then
1440 declare
1441 Lt : constant Entity_Id := Etype (Lhs);
1442 begin
1443 Set_Etype (Lhs, Typ);
1444 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1445 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1446 Set_Etype (Lhs, Lt);
1447 end;
1449 -- If the Lhs has a private type with unknown discriminants, it
1450 -- may have a full view with discriminants, but those are nameable
1451 -- only in the underlying type, so convert the Rhs to it before
1452 -- potential checking.
1454 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1455 and then Has_Discriminants (Typ)
1456 then
1457 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1458 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1460 -- In the access type case, we need the same discriminant check,
1461 -- and also range checks if we have an access to constrained array.
1463 elsif Is_Access_Type (Etype (Lhs))
1464 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1465 then
1466 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1468 -- Skip discriminant check if change of representation. Will be
1469 -- done when the change of representation is expanded out.
1471 if not Change_Of_Representation (N) then
1472 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1473 end if;
1475 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1476 Apply_Range_Check (Rhs, Etype (Lhs));
1478 if Is_Constrained (Etype (Lhs)) then
1479 Apply_Length_Check (Rhs, Etype (Lhs));
1480 end if;
1482 if Nkind (Rhs) = N_Allocator then
1483 declare
1484 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1485 C_Es : Check_Result;
1487 begin
1488 C_Es :=
1489 Range_Check
1490 (Lhs,
1491 Target_Typ,
1492 Etype (Designated_Type (Etype (Lhs))));
1494 Insert_Range_Checks
1495 (C_Es,
1497 Target_Typ,
1498 Sloc (Lhs),
1499 Lhs);
1500 end;
1501 end if;
1502 end if;
1504 -- Apply range check for access type case
1506 elsif Is_Access_Type (Etype (Lhs))
1507 and then Nkind (Rhs) = N_Allocator
1508 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1509 then
1510 Analyze_And_Resolve (Expression (Rhs));
1511 Apply_Range_Check
1512 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1513 end if;
1515 -- If we are assigning an access type and the left side is an
1516 -- entity, then make sure that Is_Known_Non_Null properly
1517 -- reflects the state of the entity after the assignment
1519 if Is_Access_Type (Typ)
1520 and then Is_Entity_Name (Lhs)
1521 and then Known_Non_Null (Rhs)
1522 and then Safe_To_Capture_Value (N, Entity (Lhs))
1523 then
1524 Set_Is_Known_Non_Null (Entity (Lhs), Known_Non_Null (Rhs));
1525 end if;
1527 -- Case of assignment to a bit packed array element
1529 if Nkind (Lhs) = N_Indexed_Component
1530 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1531 then
1532 Expand_Bit_Packed_Element_Set (N);
1533 return;
1535 -- Case of tagged type assignment
1537 elsif Is_Tagged_Type (Typ)
1538 or else (Controlled_Type (Typ) and then not Is_Array_Type (Typ))
1539 then
1540 Tagged_Case : declare
1541 L : List_Id := No_List;
1542 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1544 begin
1545 -- In the controlled case, we need to make sure that function
1546 -- calls are evaluated before finalizing the target. In all
1547 -- cases, it makes the expansion easier if the side-effects
1548 -- are removed first.
1550 Remove_Side_Effects (Lhs);
1551 Remove_Side_Effects (Rhs);
1553 -- Avoid recursion in the mechanism
1555 Set_Analyzed (N);
1557 -- If dispatching assignment, we need to dispatch to _assign
1559 if Is_Class_Wide_Type (Typ)
1561 -- If the type is tagged, we may as well use the predefined
1562 -- primitive assignment. This avoids inlining a lot of code
1563 -- and in the class-wide case, the assignment is replaced by
1564 -- a dispatch call to _assign. Note that this cannot be done
1565 -- when discriminant checks are locally suppressed (as in
1566 -- extension aggregate expansions) because otherwise the
1567 -- discriminant check will be performed within the _assign
1568 -- call.
1570 or else (Is_Tagged_Type (Typ)
1571 and then Chars (Current_Scope) /= Name_uAssign
1572 and then Expand_Ctrl_Actions
1573 and then not Discriminant_Checks_Suppressed (Empty))
1574 then
1575 -- Fetch the primitive op _assign and proper type to call
1576 -- it. Because of possible conflits between private and
1577 -- full view the proper type is fetched directly from the
1578 -- operation profile.
1580 declare
1581 Op : constant Entity_Id :=
1582 Find_Prim_Op (Typ, Name_uAssign);
1583 F_Typ : Entity_Id := Etype (First_Formal (Op));
1585 begin
1586 -- If the assignment is dispatching, make sure to use the
1587 -- ??? where is rest of this comment ???
1589 if Is_Class_Wide_Type (Typ) then
1590 F_Typ := Class_Wide_Type (F_Typ);
1591 end if;
1593 L := New_List (
1594 Make_Procedure_Call_Statement (Loc,
1595 Name => New_Reference_To (Op, Loc),
1596 Parameter_Associations => New_List (
1597 Unchecked_Convert_To (F_Typ, Duplicate_Subexpr (Lhs)),
1598 Unchecked_Convert_To (F_Typ,
1599 Duplicate_Subexpr (Rhs)))));
1600 end;
1602 else
1603 L := Make_Tag_Ctrl_Assignment (N);
1605 -- We can't afford to have destructive Finalization Actions
1606 -- in the Self assignment case, so if the target and the
1607 -- source are not obviously different, code is generated to
1608 -- avoid the self assignment case
1610 -- if lhs'address /= rhs'address then
1611 -- <code for controlled and/or tagged assignment>
1612 -- end if;
1614 if not Statically_Different (Lhs, Rhs)
1615 and then Expand_Ctrl_Actions
1616 then
1617 L := New_List (
1618 Make_Implicit_If_Statement (N,
1619 Condition =>
1620 Make_Op_Ne (Loc,
1621 Left_Opnd =>
1622 Make_Attribute_Reference (Loc,
1623 Prefix => Duplicate_Subexpr (Lhs),
1624 Attribute_Name => Name_Address),
1626 Right_Opnd =>
1627 Make_Attribute_Reference (Loc,
1628 Prefix => Duplicate_Subexpr (Rhs),
1629 Attribute_Name => Name_Address)),
1631 Then_Statements => L));
1632 end if;
1634 -- We need to set up an exception handler for implementing
1635 -- 7.6.1 (18). The remaining adjustments are tackled by the
1636 -- implementation of adjust for record_controllers (see
1637 -- s-finimp.adb)
1639 -- This is skipped if we have no finalization
1641 if Expand_Ctrl_Actions
1642 and then not Restrictions (No_Finalization)
1643 then
1644 L := New_List (
1645 Make_Block_Statement (Loc,
1646 Handled_Statement_Sequence =>
1647 Make_Handled_Sequence_Of_Statements (Loc,
1648 Statements => L,
1649 Exception_Handlers => New_List (
1650 Make_Exception_Handler (Loc,
1651 Exception_Choices =>
1652 New_List (Make_Others_Choice (Loc)),
1653 Statements => New_List (
1654 Make_Raise_Program_Error (Loc,
1655 Reason =>
1656 PE_Finalize_Raised_Exception)
1657 ))))));
1658 end if;
1659 end if;
1661 Rewrite (N,
1662 Make_Block_Statement (Loc,
1663 Handled_Statement_Sequence =>
1664 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1666 -- If no restrictions on aborts, protect the whole assignement
1667 -- for controlled objects as per 9.8(11)
1669 if Controlled_Type (Typ)
1670 and then Expand_Ctrl_Actions
1671 and then Abort_Allowed
1672 then
1673 declare
1674 Blk : constant Entity_Id :=
1675 New_Internal_Entity (
1676 E_Block, Current_Scope, Sloc (N), 'B');
1678 begin
1679 Set_Scope (Blk, Current_Scope);
1680 Set_Etype (Blk, Standard_Void_Type);
1681 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
1683 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
1684 Set_At_End_Proc (Handled_Statement_Sequence (N),
1685 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
1686 Expand_At_End_Handler
1687 (Handled_Statement_Sequence (N), Blk);
1688 end;
1689 end if;
1691 Analyze (N);
1692 return;
1693 end Tagged_Case;
1695 -- Array types
1697 elsif Is_Array_Type (Typ) then
1698 declare
1699 Actual_Rhs : Node_Id := Rhs;
1701 begin
1702 while Nkind (Actual_Rhs) = N_Type_Conversion
1703 or else
1704 Nkind (Actual_Rhs) = N_Qualified_Expression
1705 loop
1706 Actual_Rhs := Expression (Actual_Rhs);
1707 end loop;
1709 Expand_Assign_Array (N, Actual_Rhs);
1710 return;
1711 end;
1713 -- Record types
1715 elsif Is_Record_Type (Typ) then
1716 Expand_Assign_Record (N);
1717 return;
1719 -- Scalar types. This is where we perform the processing related
1720 -- to the requirements of (RM 13.9.1(9-11)) concerning the handling
1721 -- of invalid scalar values.
1723 elsif Is_Scalar_Type (Typ) then
1725 -- Case where right side is known valid
1727 if Expr_Known_Valid (Rhs) then
1729 -- Here the right side is valid, so it is fine. The case to
1730 -- deal with is when the left side is a local variable reference
1731 -- whose value is not currently known to be valid. If this is
1732 -- the case, and the assignment appears in an unconditional
1733 -- context, then we can mark the left side as now being valid.
1735 if Is_Local_Variable_Reference (Lhs)
1736 and then not Is_Known_Valid (Entity (Lhs))
1737 and then In_Unconditional_Context (N)
1738 then
1739 Set_Is_Known_Valid (Entity (Lhs), True);
1740 end if;
1742 -- Case where right side may be invalid in the sense of the RM
1743 -- reference above. The RM does not require that we check for
1744 -- the validity on an assignment, but it does require that the
1745 -- assignment of an invalid value not cause erroneous behavior.
1747 -- The general approach in GNAT is to use the Is_Known_Valid flag
1748 -- to avoid the need for validity checking on assignments. However
1749 -- in some cases, we have to do validity checking in order to make
1750 -- sure that the setting of this flag is correct.
1752 else
1753 -- Validate right side if we are validating copies
1755 if Validity_Checks_On
1756 and then Validity_Check_Copies
1757 then
1758 Ensure_Valid (Rhs);
1760 -- We can propagate this to the left side where appropriate
1762 if Is_Local_Variable_Reference (Lhs)
1763 and then not Is_Known_Valid (Entity (Lhs))
1764 and then In_Unconditional_Context (N)
1765 then
1766 Set_Is_Known_Valid (Entity (Lhs), True);
1767 end if;
1769 -- Otherwise check to see what should be done
1771 -- If left side is a local variable, then we just set its
1772 -- flag to indicate that its value may no longer be valid,
1773 -- since we are copying a potentially invalid value.
1775 elsif Is_Local_Variable_Reference (Lhs) then
1776 Set_Is_Known_Valid (Entity (Lhs), False);
1778 -- Check for case of a nonlocal variable on the left side
1779 -- which is currently known to be valid. In this case, we
1780 -- simply ensure that the right side is valid. We only play
1781 -- the game of copying validity status for local variables,
1782 -- since we are doing this statically, not by tracing the
1783 -- full flow graph.
1785 elsif Is_Entity_Name (Lhs)
1786 and then Is_Known_Valid (Entity (Lhs))
1787 then
1788 -- Note that the Ensure_Valid call is ignored if the
1789 -- Validity_Checking mode is set to none so we do not
1790 -- need to worry about that case here.
1792 Ensure_Valid (Rhs);
1794 -- In all other cases, we can safely copy an invalid value
1795 -- without worrying about the status of the left side. Since
1796 -- it is not a variable reference it will not be considered
1797 -- as being known to be valid in any case.
1799 else
1800 null;
1801 end if;
1802 end if;
1803 end if;
1805 -- Defend against invalid subscripts on left side if we are in
1806 -- standard validity checking mode. No need to do this if we
1807 -- are checking all subscripts.
1809 if Validity_Checks_On
1810 and then Validity_Check_Default
1811 and then not Validity_Check_Subscripts
1812 then
1813 Check_Valid_Lvalue_Subscripts (Lhs);
1814 end if;
1816 exception
1817 when RE_Not_Available =>
1818 return;
1819 end Expand_N_Assignment_Statement;
1821 ------------------------------
1822 -- Expand_N_Block_Statement --
1823 ------------------------------
1825 -- Encode entity names defined in block statement
1827 procedure Expand_N_Block_Statement (N : Node_Id) is
1828 begin
1829 Qualify_Entity_Names (N);
1830 end Expand_N_Block_Statement;
1832 -----------------------------
1833 -- Expand_N_Case_Statement --
1834 -----------------------------
1836 procedure Expand_N_Case_Statement (N : Node_Id) is
1837 Loc : constant Source_Ptr := Sloc (N);
1838 Expr : constant Node_Id := Expression (N);
1839 Alt : Node_Id;
1840 Len : Nat;
1841 Cond : Node_Id;
1842 Choice : Node_Id;
1843 Chlist : List_Id;
1845 begin
1846 -- Check for the situation where we know at compile time which
1847 -- branch will be taken
1849 if Compile_Time_Known_Value (Expr) then
1850 Alt := Find_Static_Alternative (N);
1852 -- Move the statements from this alternative after the case
1853 -- statement. They are already analyzed, so will be skipped
1854 -- by the analyzer.
1856 Insert_List_After (N, Statements (Alt));
1858 -- That leaves the case statement as a shell. The alternative
1859 -- that will be executed is reset to a null list. So now we can
1860 -- kill the entire case statement.
1862 Kill_Dead_Code (Expression (N));
1863 Kill_Dead_Code (Alternatives (N));
1864 Rewrite (N, Make_Null_Statement (Loc));
1865 return;
1866 end if;
1868 -- Here if the choice is not determined at compile time
1870 declare
1871 Last_Alt : constant Node_Id := Last (Alternatives (N));
1873 Others_Present : Boolean;
1874 Others_Node : Node_Id;
1876 Then_Stms : List_Id;
1877 Else_Stms : List_Id;
1879 begin
1880 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
1881 Others_Present := True;
1882 Others_Node := Last_Alt;
1883 else
1884 Others_Present := False;
1885 end if;
1887 -- First step is to worry about possible invalid argument. The RM
1888 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
1889 -- outside the base range), then Constraint_Error must be raised.
1891 -- Case of validity check required (validity checks are on, the
1892 -- expression is not known to be valid, and the case statement
1893 -- comes from source -- no need to validity check internally
1894 -- generated case statements).
1896 if Validity_Check_Default then
1897 Ensure_Valid (Expr);
1898 end if;
1900 -- If there is only a single alternative, just replace it with
1901 -- the sequence of statements since obviously that is what is
1902 -- going to be executed in all cases.
1904 Len := List_Length (Alternatives (N));
1906 if Len = 1 then
1907 -- We still need to evaluate the expression if it has any
1908 -- side effects.
1910 Remove_Side_Effects (Expression (N));
1912 Insert_List_After (N, Statements (First (Alternatives (N))));
1914 -- That leaves the case statement as a shell. The alternative
1915 -- that will be executed is reset to a null list. So now we can
1916 -- kill the entire case statement.
1918 Kill_Dead_Code (Expression (N));
1919 Rewrite (N, Make_Null_Statement (Loc));
1920 return;
1921 end if;
1923 -- An optimization. If there are only two alternatives, and only
1924 -- a single choice, then rewrite the whole case statement as an
1925 -- if statement, since this can result in susbequent optimizations.
1926 -- This helps not only with case statements in the source of a
1927 -- simple form, but also with generated code (discriminant check
1928 -- functions in particular)
1930 if Len = 2 then
1931 Chlist := Discrete_Choices (First (Alternatives (N)));
1933 if List_Length (Chlist) = 1 then
1934 Choice := First (Chlist);
1936 Then_Stms := Statements (First (Alternatives (N)));
1937 Else_Stms := Statements (Last (Alternatives (N)));
1939 -- For TRUE, generate "expression", not expression = true
1941 if Nkind (Choice) = N_Identifier
1942 and then Entity (Choice) = Standard_True
1943 then
1944 Cond := Expression (N);
1946 -- For FALSE, generate "expression" and switch then/else
1948 elsif Nkind (Choice) = N_Identifier
1949 and then Entity (Choice) = Standard_False
1950 then
1951 Cond := Expression (N);
1952 Else_Stms := Statements (First (Alternatives (N)));
1953 Then_Stms := Statements (Last (Alternatives (N)));
1955 -- For a range, generate "expression in range"
1957 elsif Nkind (Choice) = N_Range
1958 or else (Nkind (Choice) = N_Attribute_Reference
1959 and then Attribute_Name (Choice) = Name_Range)
1960 or else (Is_Entity_Name (Choice)
1961 and then Is_Type (Entity (Choice)))
1962 or else Nkind (Choice) = N_Subtype_Indication
1963 then
1964 Cond :=
1965 Make_In (Loc,
1966 Left_Opnd => Expression (N),
1967 Right_Opnd => Relocate_Node (Choice));
1969 -- For any other subexpression "expression = value"
1971 else
1972 Cond :=
1973 Make_Op_Eq (Loc,
1974 Left_Opnd => Expression (N),
1975 Right_Opnd => Relocate_Node (Choice));
1976 end if;
1978 -- Now rewrite the case as an IF
1980 Rewrite (N,
1981 Make_If_Statement (Loc,
1982 Condition => Cond,
1983 Then_Statements => Then_Stms,
1984 Else_Statements => Else_Stms));
1985 Analyze (N);
1986 return;
1987 end if;
1988 end if;
1990 -- If the last alternative is not an Others choice, replace it
1991 -- with an N_Others_Choice. Note that we do not bother to call
1992 -- Analyze on the modified case statement, since it's only effect
1993 -- would be to compute the contents of the Others_Discrete_Choices
1994 -- which is not needed by the back end anyway.
1996 -- The reason we do this is that the back end always needs some
1997 -- default for a switch, so if we have not supplied one in the
1998 -- processing above for validity checking, then we need to
1999 -- supply one here.
2001 if not Others_Present then
2002 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2003 Set_Others_Discrete_Choices
2004 (Others_Node, Discrete_Choices (Last_Alt));
2005 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2006 end if;
2007 end;
2008 end Expand_N_Case_Statement;
2010 -----------------------------
2011 -- Expand_N_Exit_Statement --
2012 -----------------------------
2014 -- The only processing required is to deal with a possible C/Fortran
2015 -- boolean value used as the condition for the exit statement.
2017 procedure Expand_N_Exit_Statement (N : Node_Id) is
2018 begin
2019 Adjust_Condition (Condition (N));
2020 end Expand_N_Exit_Statement;
2022 -----------------------------
2023 -- Expand_N_Goto_Statement --
2024 -----------------------------
2026 -- Add poll before goto if polling active
2028 procedure Expand_N_Goto_Statement (N : Node_Id) is
2029 begin
2030 Generate_Poll_Call (N);
2031 end Expand_N_Goto_Statement;
2033 ---------------------------
2034 -- Expand_N_If_Statement --
2035 ---------------------------
2037 -- First we deal with the case of C and Fortran convention boolean
2038 -- values, with zero/non-zero semantics.
2040 -- Second, we deal with the obvious rewriting for the cases where the
2041 -- condition of the IF is known at compile time to be True or False.
2043 -- Third, we remove elsif parts which have non-empty Condition_Actions
2044 -- and rewrite as independent if statements. For example:
2046 -- if x then xs
2047 -- elsif y then ys
2048 -- ...
2049 -- end if;
2051 -- becomes
2053 -- if x then xs
2054 -- else
2055 -- <<condition actions of y>>
2056 -- if y then ys
2057 -- ...
2058 -- end if;
2059 -- end if;
2061 -- This rewriting is needed if at least one elsif part has a non-empty
2062 -- Condition_Actions list. We also do the same processing if there is
2063 -- a constant condition in an elsif part (in conjunction with the first
2064 -- processing step mentioned above, for the recursive call made to deal
2065 -- with the created inner if, this deals with properly optimizing the
2066 -- cases of constant elsif conditions).
2068 procedure Expand_N_If_Statement (N : Node_Id) is
2069 Loc : constant Source_Ptr := Sloc (N);
2070 Hed : Node_Id;
2071 E : Node_Id;
2072 New_If : Node_Id;
2074 begin
2075 Adjust_Condition (Condition (N));
2077 -- The following loop deals with constant conditions for the IF. We
2078 -- need a loop because as we eliminate False conditions, we grab the
2079 -- first elsif condition and use it as the primary condition.
2081 while Compile_Time_Known_Value (Condition (N)) loop
2083 -- If condition is True, we can simply rewrite the if statement
2084 -- now by replacing it by the series of then statements.
2086 if Is_True (Expr_Value (Condition (N))) then
2088 -- All the else parts can be killed
2090 Kill_Dead_Code (Elsif_Parts (N));
2091 Kill_Dead_Code (Else_Statements (N));
2093 Hed := Remove_Head (Then_Statements (N));
2094 Insert_List_After (N, Then_Statements (N));
2095 Rewrite (N, Hed);
2096 return;
2098 -- If condition is False, then we can delete the condition and
2099 -- the Then statements
2101 else
2102 -- We do not delete the condition if constant condition
2103 -- warnings are enabled, since otherwise we end up deleting
2104 -- the desired warning. Of course the backend will get rid
2105 -- of this True/False test anyway, so nothing is lost here.
2107 if not Constant_Condition_Warnings then
2108 Kill_Dead_Code (Condition (N));
2109 end if;
2111 Kill_Dead_Code (Then_Statements (N));
2113 -- If there are no elsif statements, then we simply replace
2114 -- the entire if statement by the sequence of else statements.
2116 if No (Elsif_Parts (N)) then
2118 if No (Else_Statements (N))
2119 or else Is_Empty_List (Else_Statements (N))
2120 then
2121 Rewrite (N,
2122 Make_Null_Statement (Sloc (N)));
2124 else
2125 Hed := Remove_Head (Else_Statements (N));
2126 Insert_List_After (N, Else_Statements (N));
2127 Rewrite (N, Hed);
2128 end if;
2130 return;
2132 -- If there are elsif statements, the first of them becomes
2133 -- the if/then section of the rebuilt if statement This is
2134 -- the case where we loop to reprocess this copied condition.
2136 else
2137 Hed := Remove_Head (Elsif_Parts (N));
2138 Insert_Actions (N, Condition_Actions (Hed));
2139 Set_Condition (N, Condition (Hed));
2140 Set_Then_Statements (N, Then_Statements (Hed));
2142 if Is_Empty_List (Elsif_Parts (N)) then
2143 Set_Elsif_Parts (N, No_List);
2144 end if;
2145 end if;
2146 end if;
2147 end loop;
2149 -- Loop through elsif parts, dealing with constant conditions and
2150 -- possible expression actions that are present.
2152 if Present (Elsif_Parts (N)) then
2153 E := First (Elsif_Parts (N));
2154 while Present (E) loop
2155 Adjust_Condition (Condition (E));
2157 -- If there are condition actions, then we rewrite the if
2158 -- statement as indicated above. We also do the same rewrite
2159 -- if the condition is True or False. The further processing
2160 -- of this constant condition is then done by the recursive
2161 -- call to expand the newly created if statement
2163 if Present (Condition_Actions (E))
2164 or else Compile_Time_Known_Value (Condition (E))
2165 then
2166 -- Note this is not an implicit if statement, since it is
2167 -- part of an explicit if statement in the source (or of an
2168 -- implicit if statement that has already been tested).
2170 New_If :=
2171 Make_If_Statement (Sloc (E),
2172 Condition => Condition (E),
2173 Then_Statements => Then_Statements (E),
2174 Elsif_Parts => No_List,
2175 Else_Statements => Else_Statements (N));
2177 -- Elsif parts for new if come from remaining elsif's of parent
2179 while Present (Next (E)) loop
2180 if No (Elsif_Parts (New_If)) then
2181 Set_Elsif_Parts (New_If, New_List);
2182 end if;
2184 Append (Remove_Next (E), Elsif_Parts (New_If));
2185 end loop;
2187 Set_Else_Statements (N, New_List (New_If));
2189 if Present (Condition_Actions (E)) then
2190 Insert_List_Before (New_If, Condition_Actions (E));
2191 end if;
2193 Remove (E);
2195 if Is_Empty_List (Elsif_Parts (N)) then
2196 Set_Elsif_Parts (N, No_List);
2197 end if;
2199 Analyze (New_If);
2200 return;
2202 -- No special processing for that elsif part, move to next
2204 else
2205 Next (E);
2206 end if;
2207 end loop;
2208 end if;
2210 -- Some more optimizations applicable if we still have an IF statement
2212 if Nkind (N) /= N_If_Statement then
2213 return;
2214 end if;
2216 -- Another optimization, special cases that can be simplified
2218 -- if expression then
2219 -- return true;
2220 -- else
2221 -- return false;
2222 -- end if;
2224 -- can be changed to:
2226 -- return expression;
2228 -- and
2230 -- if expression then
2231 -- return false;
2232 -- else
2233 -- return true;
2234 -- end if;
2236 -- can be changed to:
2238 -- return not (expression);
2240 if Nkind (N) = N_If_Statement
2241 and then No (Elsif_Parts (N))
2242 and then Present (Else_Statements (N))
2243 and then List_Length (Then_Statements (N)) = 1
2244 and then List_Length (Else_Statements (N)) = 1
2245 then
2246 declare
2247 Then_Stm : Node_Id := First (Then_Statements (N));
2248 Else_Stm : Node_Id := First (Else_Statements (N));
2250 begin
2251 if Nkind (Then_Stm) = N_Return_Statement
2252 and then
2253 Nkind (Else_Stm) = N_Return_Statement
2254 then
2255 declare
2256 Then_Expr : constant Node_Id := Expression (Then_Stm);
2257 Else_Expr : constant Node_Id := Expression (Else_Stm);
2259 begin
2260 if Nkind (Then_Expr) = N_Identifier
2261 and then
2262 Nkind (Else_Expr) = N_Identifier
2263 then
2264 if Entity (Then_Expr) = Standard_True
2265 and then Entity (Else_Expr) = Standard_False
2266 then
2267 Rewrite (N,
2268 Make_Return_Statement (Loc,
2269 Expression => Relocate_Node (Condition (N))));
2270 Analyze (N);
2271 return;
2273 elsif Entity (Then_Expr) = Standard_False
2274 and then Entity (Else_Expr) = Standard_True
2275 then
2276 Rewrite (N,
2277 Make_Return_Statement (Loc,
2278 Expression =>
2279 Make_Op_Not (Loc,
2280 Right_Opnd => Relocate_Node (Condition (N)))));
2281 Analyze (N);
2282 return;
2283 end if;
2284 end if;
2285 end;
2286 end if;
2287 end;
2288 end if;
2289 end Expand_N_If_Statement;
2291 -----------------------------
2292 -- Expand_N_Loop_Statement --
2293 -----------------------------
2295 -- 1. Deal with while condition for C/Fortran boolean
2296 -- 2. Deal with loops with a non-standard enumeration type range
2297 -- 3. Deal with while loops where Condition_Actions is set
2298 -- 4. Insert polling call if required
2300 procedure Expand_N_Loop_Statement (N : Node_Id) is
2301 Loc : constant Source_Ptr := Sloc (N);
2302 Isc : constant Node_Id := Iteration_Scheme (N);
2304 begin
2305 if Present (Isc) then
2306 Adjust_Condition (Condition (Isc));
2307 end if;
2309 if Is_Non_Empty_List (Statements (N)) then
2310 Generate_Poll_Call (First (Statements (N)));
2311 end if;
2313 if No (Isc) then
2314 return;
2315 end if;
2317 -- Handle the case where we have a for loop with the range type being
2318 -- an enumeration type with non-standard representation. In this case
2319 -- we expand:
2321 -- for x in [reverse] a .. b loop
2322 -- ...
2323 -- end loop;
2325 -- to
2327 -- for xP in [reverse] integer
2328 -- range etype'Pos (a) .. etype'Pos (b) loop
2329 -- declare
2330 -- x : constant etype := Pos_To_Rep (xP);
2331 -- begin
2332 -- ...
2333 -- end;
2334 -- end loop;
2336 if Present (Loop_Parameter_Specification (Isc)) then
2337 declare
2338 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
2339 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
2340 Ltype : constant Entity_Id := Etype (Loop_Id);
2341 Btype : constant Entity_Id := Base_Type (Ltype);
2342 Expr : Node_Id;
2343 New_Id : Entity_Id;
2345 begin
2346 if not Is_Enumeration_Type (Btype)
2347 or else No (Enum_Pos_To_Rep (Btype))
2348 then
2349 return;
2350 end if;
2352 New_Id :=
2353 Make_Defining_Identifier (Loc,
2354 Chars => New_External_Name (Chars (Loop_Id), 'P'));
2356 -- If the type has a contiguous representation, successive
2357 -- values can be generated as offsets from the first literal.
2359 if Has_Contiguous_Rep (Btype) then
2360 Expr :=
2361 Unchecked_Convert_To (Btype,
2362 Make_Op_Add (Loc,
2363 Left_Opnd =>
2364 Make_Integer_Literal (Loc,
2365 Enumeration_Rep (First_Literal (Btype))),
2366 Right_Opnd => New_Reference_To (New_Id, Loc)));
2367 else
2368 -- Use the constructed array Enum_Pos_To_Rep.
2370 Expr :=
2371 Make_Indexed_Component (Loc,
2372 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
2373 Expressions => New_List (New_Reference_To (New_Id, Loc)));
2374 end if;
2376 Rewrite (N,
2377 Make_Loop_Statement (Loc,
2378 Identifier => Identifier (N),
2380 Iteration_Scheme =>
2381 Make_Iteration_Scheme (Loc,
2382 Loop_Parameter_Specification =>
2383 Make_Loop_Parameter_Specification (Loc,
2384 Defining_Identifier => New_Id,
2385 Reverse_Present => Reverse_Present (LPS),
2387 Discrete_Subtype_Definition =>
2388 Make_Subtype_Indication (Loc,
2390 Subtype_Mark =>
2391 New_Reference_To (Standard_Natural, Loc),
2393 Constraint =>
2394 Make_Range_Constraint (Loc,
2395 Range_Expression =>
2396 Make_Range (Loc,
2398 Low_Bound =>
2399 Make_Attribute_Reference (Loc,
2400 Prefix =>
2401 New_Reference_To (Btype, Loc),
2403 Attribute_Name => Name_Pos,
2405 Expressions => New_List (
2406 Relocate_Node
2407 (Type_Low_Bound (Ltype)))),
2409 High_Bound =>
2410 Make_Attribute_Reference (Loc,
2411 Prefix =>
2412 New_Reference_To (Btype, Loc),
2414 Attribute_Name => Name_Pos,
2416 Expressions => New_List (
2417 Relocate_Node
2418 (Type_High_Bound (Ltype))))))))),
2420 Statements => New_List (
2421 Make_Block_Statement (Loc,
2422 Declarations => New_List (
2423 Make_Object_Declaration (Loc,
2424 Defining_Identifier => Loop_Id,
2425 Constant_Present => True,
2426 Object_Definition => New_Reference_To (Ltype, Loc),
2427 Expression => Expr)),
2429 Handled_Statement_Sequence =>
2430 Make_Handled_Sequence_Of_Statements (Loc,
2431 Statements => Statements (N)))),
2433 End_Label => End_Label (N)));
2434 Analyze (N);
2435 end;
2437 -- Second case, if we have a while loop with Condition_Actions set,
2438 -- then we change it into a plain loop:
2440 -- while C loop
2441 -- ...
2442 -- end loop;
2444 -- changed to:
2446 -- loop
2447 -- <<condition actions>>
2448 -- exit when not C;
2449 -- ...
2450 -- end loop
2452 elsif Present (Isc)
2453 and then Present (Condition_Actions (Isc))
2454 then
2455 declare
2456 ES : Node_Id;
2458 begin
2459 ES :=
2460 Make_Exit_Statement (Sloc (Condition (Isc)),
2461 Condition =>
2462 Make_Op_Not (Sloc (Condition (Isc)),
2463 Right_Opnd => Condition (Isc)));
2465 Prepend (ES, Statements (N));
2466 Insert_List_Before (ES, Condition_Actions (Isc));
2468 -- This is not an implicit loop, since it is generated in
2469 -- response to the loop statement being processed. If this
2470 -- is itself implicit, the restriction has already been
2471 -- checked. If not, it is an explicit loop.
2473 Rewrite (N,
2474 Make_Loop_Statement (Sloc (N),
2475 Identifier => Identifier (N),
2476 Statements => Statements (N),
2477 End_Label => End_Label (N)));
2479 Analyze (N);
2480 end;
2481 end if;
2482 end Expand_N_Loop_Statement;
2484 -------------------------------
2485 -- Expand_N_Return_Statement --
2486 -------------------------------
2488 procedure Expand_N_Return_Statement (N : Node_Id) is
2489 Loc : constant Source_Ptr := Sloc (N);
2490 Exp : constant Node_Id := Expression (N);
2491 Exptyp : Entity_Id;
2492 T : Entity_Id;
2493 Utyp : Entity_Id;
2494 Scope_Id : Entity_Id;
2495 Kind : Entity_Kind;
2496 Call : Node_Id;
2497 Acc_Stat : Node_Id;
2498 Goto_Stat : Node_Id;
2499 Lab_Node : Node_Id;
2500 Cur_Idx : Nat;
2501 Return_Type : Entity_Id;
2502 Result_Exp : Node_Id;
2503 Result_Id : Entity_Id;
2504 Result_Obj : Node_Id;
2506 begin
2507 -- Case where returned expression is present
2509 if Present (Exp) then
2511 -- Always normalize C/Fortran boolean result. This is not always
2512 -- necessary, but it seems a good idea to minimize the passing
2513 -- around of non-normalized values, and in any case this handles
2514 -- the processing of barrier functions for protected types, which
2515 -- turn the condition into a return statement.
2517 Exptyp := Etype (Exp);
2519 if Is_Boolean_Type (Exptyp)
2520 and then Nonzero_Is_True (Exptyp)
2521 then
2522 Adjust_Condition (Exp);
2523 Adjust_Result_Type (Exp, Exptyp);
2524 end if;
2526 -- Do validity check if enabled for returns
2528 if Validity_Checks_On
2529 and then Validity_Check_Returns
2530 then
2531 Ensure_Valid (Exp);
2532 end if;
2533 end if;
2535 -- Find relevant enclosing scope from which return is returning
2537 Cur_Idx := Scope_Stack.Last;
2538 loop
2539 Scope_Id := Scope_Stack.Table (Cur_Idx).Entity;
2541 if Ekind (Scope_Id) /= E_Block
2542 and then Ekind (Scope_Id) /= E_Loop
2543 then
2544 exit;
2546 else
2547 Cur_Idx := Cur_Idx - 1;
2548 pragma Assert (Cur_Idx >= 0);
2549 end if;
2550 end loop;
2552 if No (Exp) then
2553 Kind := Ekind (Scope_Id);
2555 -- If it is a return from procedures do no extra steps.
2557 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
2558 return;
2559 end if;
2561 pragma Assert (Is_Entry (Scope_Id));
2563 -- Look at the enclosing block to see whether the return is from
2564 -- an accept statement or an entry body.
2566 for J in reverse 0 .. Cur_Idx loop
2567 Scope_Id := Scope_Stack.Table (J).Entity;
2568 exit when Is_Concurrent_Type (Scope_Id);
2569 end loop;
2571 -- If it is a return from accept statement it should be expanded
2572 -- as a call to RTS Complete_Rendezvous and a goto to the end of
2573 -- the accept body.
2575 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
2576 -- Expand_N_Accept_Alternative in exp_ch9.adb)
2578 if Is_Task_Type (Scope_Id) then
2580 Call := (Make_Procedure_Call_Statement (Loc,
2581 Name => New_Reference_To
2582 (RTE (RE_Complete_Rendezvous), Loc)));
2583 Insert_Before (N, Call);
2584 -- why not insert actions here???
2585 Analyze (Call);
2587 Acc_Stat := Parent (N);
2588 while Nkind (Acc_Stat) /= N_Accept_Statement loop
2589 Acc_Stat := Parent (Acc_Stat);
2590 end loop;
2592 Lab_Node := Last (Statements
2593 (Handled_Statement_Sequence (Acc_Stat)));
2595 Goto_Stat := Make_Goto_Statement (Loc,
2596 Name => New_Occurrence_Of
2597 (Entity (Identifier (Lab_Node)), Loc));
2599 Set_Analyzed (Goto_Stat);
2601 Rewrite (N, Goto_Stat);
2602 Analyze (N);
2604 -- If it is a return from an entry body, put a Complete_Entry_Body
2605 -- call in front of the return.
2607 elsif Is_Protected_Type (Scope_Id) then
2609 Call :=
2610 Make_Procedure_Call_Statement (Loc,
2611 Name => New_Reference_To
2612 (RTE (RE_Complete_Entry_Body), Loc),
2613 Parameter_Associations => New_List
2614 (Make_Attribute_Reference (Loc,
2615 Prefix =>
2616 New_Reference_To
2617 (Object_Ref
2618 (Corresponding_Body (Parent (Scope_Id))),
2619 Loc),
2620 Attribute_Name => Name_Unchecked_Access)));
2622 Insert_Before (N, Call);
2623 Analyze (Call);
2625 end if;
2627 return;
2628 end if;
2630 T := Etype (Exp);
2631 Return_Type := Etype (Scope_Id);
2632 Utyp := Underlying_Type (Return_Type);
2634 -- Check the result expression of a scalar function against
2635 -- the subtype of the function by inserting a conversion.
2636 -- This conversion must eventually be performed for other
2637 -- classes of types, but for now it's only done for scalars.
2638 -- ???
2640 if Is_Scalar_Type (T) then
2641 Rewrite (Exp, Convert_To (Return_Type, Exp));
2642 Analyze (Exp);
2643 end if;
2645 -- Implement the rules of 6.5(8-10), which require a tag check in
2646 -- the case of a limited tagged return type, and tag reassignment
2647 -- for nonlimited tagged results. These actions are needed when
2648 -- the return type is a specific tagged type and the result
2649 -- expression is a conversion or a formal parameter, because in
2650 -- that case the tag of the expression might differ from the tag
2651 -- of the specific result type.
2653 if Is_Tagged_Type (Utyp)
2654 and then not Is_Class_Wide_Type (Utyp)
2655 and then (Nkind (Exp) = N_Type_Conversion
2656 or else Nkind (Exp) = N_Unchecked_Type_Conversion
2657 or else (Is_Entity_Name (Exp)
2658 and then Ekind (Entity (Exp)) in Formal_Kind))
2659 then
2660 -- When the return type is limited, perform a check that the
2661 -- tag of the result is the same as the tag of the return type.
2663 if Is_Limited_Type (Return_Type) then
2664 Insert_Action (Exp,
2665 Make_Raise_Constraint_Error (Loc,
2666 Condition =>
2667 Make_Op_Ne (Loc,
2668 Left_Opnd =>
2669 Make_Selected_Component (Loc,
2670 Prefix => Duplicate_Subexpr (Exp),
2671 Selector_Name =>
2672 New_Reference_To (Tag_Component (Utyp), Loc)),
2673 Right_Opnd =>
2674 Unchecked_Convert_To (RTE (RE_Tag),
2675 New_Reference_To
2676 (Access_Disp_Table (Base_Type (Utyp)), Loc))),
2677 Reason => CE_Tag_Check_Failed));
2679 -- If the result type is a specific nonlimited tagged type,
2680 -- then we have to ensure that the tag of the result is that
2681 -- of the result type. This is handled by making a copy of the
2682 -- expression in the case where it might have a different tag,
2683 -- namely when the expression is a conversion or a formal
2684 -- parameter. We create a new object of the result type and
2685 -- initialize it from the expression, which will implicitly
2686 -- force the tag to be set appropriately.
2688 else
2689 Result_Id :=
2690 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2691 Result_Exp := New_Reference_To (Result_Id, Loc);
2693 Result_Obj :=
2694 Make_Object_Declaration (Loc,
2695 Defining_Identifier => Result_Id,
2696 Object_Definition => New_Reference_To (Return_Type, Loc),
2697 Constant_Present => True,
2698 Expression => Relocate_Node (Exp));
2700 Set_Assignment_OK (Result_Obj);
2701 Insert_Action (Exp, Result_Obj);
2703 Rewrite (Exp, Result_Exp);
2704 Analyze_And_Resolve (Exp, Return_Type);
2705 end if;
2706 end if;
2708 -- Deal with returning variable length objects and controlled types
2710 -- Nothing to do if we are returning by reference, or this is not
2711 -- a type that requires special processing (indicated by the fact
2712 -- that it requires a cleanup scope for the secondary stack case)
2714 if Is_Return_By_Reference_Type (T)
2715 or else not Requires_Transient_Scope (Return_Type)
2716 then
2717 null;
2719 -- Case of secondary stack not used
2721 elsif Function_Returns_With_DSP (Scope_Id) then
2723 -- Here what we need to do is to always return by reference, since
2724 -- we will return with the stack pointer depressed. We may need to
2725 -- do a copy to a local temporary before doing this return.
2727 No_Secondary_Stack_Case : declare
2728 Local_Copy_Required : Boolean := False;
2729 -- Set to True if a local copy is required
2731 Copy_Ent : Entity_Id;
2732 -- Used for the target entity if a copy is required
2734 Decl : Node_Id;
2735 -- Declaration used to create copy if needed
2737 procedure Test_Copy_Required (Expr : Node_Id);
2738 -- Determines if Expr represents a return value for which a
2739 -- copy is required. More specifically, a copy is not required
2740 -- if Expr represents an object or component of an object that
2741 -- is either in the local subprogram frame, or is constant.
2742 -- If a copy is required, then Local_Copy_Required is set True.
2744 ------------------------
2745 -- Test_Copy_Required --
2746 ------------------------
2748 procedure Test_Copy_Required (Expr : Node_Id) is
2749 Ent : Entity_Id;
2751 begin
2752 -- If component, test prefix (object containing component)
2754 if Nkind (Expr) = N_Indexed_Component
2755 or else
2756 Nkind (Expr) = N_Selected_Component
2757 then
2758 Test_Copy_Required (Prefix (Expr));
2759 return;
2761 -- See if we have an entity name
2763 elsif Is_Entity_Name (Expr) then
2764 Ent := Entity (Expr);
2766 -- Constant entity is always OK, no copy required
2768 if Ekind (Ent) = E_Constant then
2769 return;
2771 -- No copy required for local variable
2773 elsif Ekind (Ent) = E_Variable
2774 and then Scope (Ent) = Current_Subprogram
2775 then
2776 return;
2777 end if;
2778 end if;
2780 -- All other cases require a copy
2782 Local_Copy_Required := True;
2783 end Test_Copy_Required;
2785 -- Start of processing for No_Secondary_Stack_Case
2787 begin
2788 -- No copy needed if result is from a function call.
2789 -- In this case the result is already being returned by
2790 -- reference with the stack pointer depressed.
2792 -- To make up for a gcc 2.8.1 deficiency (???), we perform
2793 -- the copy for array types if the constrained status of the
2794 -- target type is different from that of the expression.
2796 if Requires_Transient_Scope (T)
2797 and then
2798 (not Is_Array_Type (T)
2799 or else Is_Constrained (T) = Is_Constrained (Return_Type)
2800 or else Controlled_Type (T))
2801 and then Nkind (Exp) = N_Function_Call
2802 then
2803 Set_By_Ref (N);
2805 -- We always need a local copy for a controlled type, since
2806 -- we are required to finalize the local value before return.
2807 -- The copy will automatically include the required finalize.
2808 -- Moreover, gigi cannot make this copy, since we need special
2809 -- processing to ensure proper behavior for finalization.
2811 -- Note: the reason we are returning with a depressed stack
2812 -- pointer in the controlled case (even if the type involved
2813 -- is constrained) is that we must make a local copy to deal
2814 -- properly with the requirement that the local result be
2815 -- finalized.
2817 elsif Controlled_Type (Utyp) then
2818 Copy_Ent :=
2819 Make_Defining_Identifier (Loc,
2820 Chars => New_Internal_Name ('R'));
2822 -- Build declaration to do the copy, and insert it, setting
2823 -- Assignment_OK, because we may be copying a limited type.
2824 -- In addition we set the special flag to inhibit finalize
2825 -- attachment if this is a controlled type (since this attach
2826 -- must be done by the caller, otherwise if we attach it here
2827 -- we will finalize the returned result prematurely).
2829 Decl :=
2830 Make_Object_Declaration (Loc,
2831 Defining_Identifier => Copy_Ent,
2832 Object_Definition => New_Occurrence_Of (Return_Type, Loc),
2833 Expression => Relocate_Node (Exp));
2835 Set_Assignment_OK (Decl);
2836 Set_Delay_Finalize_Attach (Decl);
2837 Insert_Action (N, Decl);
2839 -- Now the actual return uses the copied value
2841 Rewrite (Exp, New_Occurrence_Of (Copy_Ent, Loc));
2842 Analyze_And_Resolve (Exp, Return_Type);
2844 -- Since we have made the copy, gigi does not have to, so
2845 -- we set the By_Ref flag to prevent another copy being made.
2847 Set_By_Ref (N);
2849 -- Non-controlled cases
2851 else
2852 Test_Copy_Required (Exp);
2854 -- If a local copy is required, then gigi will make the
2855 -- copy, otherwise, we can return the result directly,
2856 -- so set By_Ref to suppress the gigi copy.
2858 if not Local_Copy_Required then
2859 Set_By_Ref (N);
2860 end if;
2861 end if;
2862 end No_Secondary_Stack_Case;
2864 -- Here if secondary stack is used
2866 else
2867 -- Make sure that no surrounding block will reclaim the
2868 -- secondary-stack on which we are going to put the result.
2869 -- Not only may this introduce secondary stack leaks but worse,
2870 -- if the reclamation is done too early, then the result we are
2871 -- returning may get clobbered. See example in 7417-003.
2873 declare
2874 S : Entity_Id := Current_Scope;
2876 begin
2877 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
2878 Set_Sec_Stack_Needed_For_Return (S, True);
2879 S := Enclosing_Dynamic_Scope (S);
2880 end loop;
2881 end;
2883 -- Optimize the case where the result is a function call. In this
2884 -- case either the result is already on the secondary stack, or is
2885 -- already being returned with the stack pointer depressed and no
2886 -- further processing is required except to set the By_Ref flag to
2887 -- ensure that gigi does not attempt an extra unnecessary copy.
2888 -- (actually not just unnecessary but harmfully wrong in the case
2889 -- of a controlled type, where gigi does not know how to do a copy).
2890 -- To make up for a gcc 2.8.1 deficiency (???), we perform
2891 -- the copy for array types if the constrained status of the
2892 -- target type is different from that of the expression.
2894 if Requires_Transient_Scope (T)
2895 and then
2896 (not Is_Array_Type (T)
2897 or else Is_Constrained (T) = Is_Constrained (Return_Type)
2898 or else Controlled_Type (T))
2899 and then Nkind (Exp) = N_Function_Call
2900 then
2901 Set_By_Ref (N);
2903 -- For controlled types, do the allocation on the sec-stack
2904 -- manually in order to call adjust at the right time
2905 -- type Anon1 is access Return_Type;
2906 -- for Anon1'Storage_pool use ss_pool;
2907 -- Anon2 : anon1 := new Return_Type'(expr);
2908 -- return Anon2.all;
2910 elsif Controlled_Type (Utyp) then
2911 declare
2912 Loc : constant Source_Ptr := Sloc (N);
2913 Temp : constant Entity_Id :=
2914 Make_Defining_Identifier (Loc,
2915 Chars => New_Internal_Name ('R'));
2916 Acc_Typ : constant Entity_Id :=
2917 Make_Defining_Identifier (Loc,
2918 Chars => New_Internal_Name ('A'));
2919 Alloc_Node : Node_Id;
2921 begin
2922 Set_Ekind (Acc_Typ, E_Access_Type);
2924 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
2926 Alloc_Node :=
2927 Make_Allocator (Loc,
2928 Expression =>
2929 Make_Qualified_Expression (Loc,
2930 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
2931 Expression => Relocate_Node (Exp)));
2933 Insert_List_Before_And_Analyze (N, New_List (
2934 Make_Full_Type_Declaration (Loc,
2935 Defining_Identifier => Acc_Typ,
2936 Type_Definition =>
2937 Make_Access_To_Object_Definition (Loc,
2938 Subtype_Indication =>
2939 New_Reference_To (Return_Type, Loc))),
2941 Make_Object_Declaration (Loc,
2942 Defining_Identifier => Temp,
2943 Object_Definition => New_Reference_To (Acc_Typ, Loc),
2944 Expression => Alloc_Node)));
2946 Rewrite (Exp,
2947 Make_Explicit_Dereference (Loc,
2948 Prefix => New_Reference_To (Temp, Loc)));
2950 Analyze_And_Resolve (Exp, Return_Type);
2951 end;
2953 -- Otherwise use the gigi mechanism to allocate result on the
2954 -- secondary stack.
2956 else
2957 Set_Storage_Pool (N, RTE (RE_SS_Pool));
2959 -- If we are generating code for the Java VM do not use
2960 -- SS_Allocate since everything is heap-allocated anyway.
2962 if not Java_VM then
2963 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
2964 end if;
2965 end if;
2966 end if;
2968 exception
2969 when RE_Not_Available =>
2970 return;
2971 end Expand_N_Return_Statement;
2973 ------------------------------
2974 -- Make_Tag_Ctrl_Assignment --
2975 ------------------------------
2977 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
2978 Loc : constant Source_Ptr := Sloc (N);
2979 L : constant Node_Id := Name (N);
2980 T : constant Entity_Id := Underlying_Type (Etype (L));
2982 Ctrl_Act : constant Boolean := Controlled_Type (T)
2983 and then not No_Ctrl_Actions (N);
2985 Save_Tag : constant Boolean := Is_Tagged_Type (T)
2986 and then not No_Ctrl_Actions (N)
2987 and then not Java_VM;
2988 -- Tags are not saved and restored when Java_VM because JVM tags
2989 -- are represented implicitly in objects.
2991 Res : List_Id;
2992 Tag_Tmp : Entity_Id;
2993 Prev_Tmp : Entity_Id;
2994 Next_Tmp : Entity_Id;
2995 Ctrl_Ref : Node_Id;
2996 Ctrl_Ref2 : Node_Id := Empty;
2997 Prev_Tmp2 : Entity_Id := Empty; -- prevent warning
2998 Next_Tmp2 : Entity_Id := Empty; -- prevent warning
3000 begin
3001 Res := New_List;
3003 -- Finalize the target of the assignment when controlled.
3004 -- We have two exceptions here:
3006 -- 1. If we are in an init proc since it is an initialization
3007 -- more than an assignment
3009 -- 2. If the left-hand side is a temporary that was not initialized
3010 -- (or the parent part of a temporary since it is the case in
3011 -- extension aggregates). Such a temporary does not come from
3012 -- source. We must examine the original node for the prefix, because
3013 -- it may be a component of an entry formal, in which case it has
3014 -- been rewritten and does not appear to come from source either.
3016 -- Case of init proc
3018 if not Ctrl_Act then
3019 null;
3021 -- The left hand side is an uninitialized temporary
3023 elsif Nkind (L) = N_Type_Conversion
3024 and then Is_Entity_Name (Expression (L))
3025 and then No_Initialization (Parent (Entity (Expression (L))))
3026 then
3027 null;
3028 else
3029 Append_List_To (Res,
3030 Make_Final_Call (
3031 Ref => Duplicate_Subexpr_No_Checks (L),
3032 Typ => Etype (L),
3033 With_Detach => New_Reference_To (Standard_False, Loc)));
3034 end if;
3036 Next_Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3038 -- Save the Tag in a local variable Tag_Tmp
3040 if Save_Tag then
3041 Tag_Tmp :=
3042 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3044 Append_To (Res,
3045 Make_Object_Declaration (Loc,
3046 Defining_Identifier => Tag_Tmp,
3047 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
3048 Expression =>
3049 Make_Selected_Component (Loc,
3050 Prefix => Duplicate_Subexpr_No_Checks (L),
3051 Selector_Name => New_Reference_To (Tag_Component (T), Loc))));
3053 -- Otherwise Tag_Tmp not used
3055 else
3056 Tag_Tmp := Empty;
3057 end if;
3059 -- Save the Finalization Pointers in local variables Prev_Tmp and
3060 -- Next_Tmp. For objects with Has_Controlled_Component set, these
3061 -- pointers are in the Record_Controller and if it is also
3062 -- Is_Controlled, we need to save the object pointers as well.
3064 if Ctrl_Act then
3065 Ctrl_Ref := Duplicate_Subexpr_No_Checks (L);
3067 if Has_Controlled_Component (T) then
3068 Ctrl_Ref :=
3069 Make_Selected_Component (Loc,
3070 Prefix => Ctrl_Ref,
3071 Selector_Name =>
3072 New_Reference_To (Controller_Component (T), Loc));
3074 if Is_Controlled (T) then
3075 Ctrl_Ref2 := Duplicate_Subexpr_No_Checks (L);
3076 end if;
3077 end if;
3079 Prev_Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
3081 Append_To (Res,
3082 Make_Object_Declaration (Loc,
3083 Defining_Identifier => Prev_Tmp,
3085 Object_Definition =>
3086 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
3088 Expression =>
3089 Make_Selected_Component (Loc,
3090 Prefix =>
3091 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref),
3092 Selector_Name => Make_Identifier (Loc, Name_Prev))));
3094 Next_Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3096 Append_To (Res,
3097 Make_Object_Declaration (Loc,
3098 Defining_Identifier => Next_Tmp,
3100 Object_Definition =>
3101 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
3103 Expression =>
3104 Make_Selected_Component (Loc,
3105 Prefix =>
3106 Unchecked_Convert_To (RTE (RE_Finalizable),
3107 New_Copy_Tree (Ctrl_Ref)),
3108 Selector_Name => Make_Identifier (Loc, Name_Next))));
3110 if Present (Ctrl_Ref2) then
3111 Prev_Tmp2 :=
3112 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
3114 Append_To (Res,
3115 Make_Object_Declaration (Loc,
3116 Defining_Identifier => Prev_Tmp2,
3118 Object_Definition =>
3119 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
3121 Expression =>
3122 Make_Selected_Component (Loc,
3123 Prefix =>
3124 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref2),
3125 Selector_Name => Make_Identifier (Loc, Name_Prev))));
3127 Next_Tmp2 :=
3128 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3130 Append_To (Res,
3131 Make_Object_Declaration (Loc,
3132 Defining_Identifier => Next_Tmp2,
3134 Object_Definition =>
3135 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
3137 Expression =>
3138 Make_Selected_Component (Loc,
3139 Prefix =>
3140 Unchecked_Convert_To (RTE (RE_Finalizable),
3141 New_Copy_Tree (Ctrl_Ref2)),
3142 Selector_Name => Make_Identifier (Loc, Name_Next))));
3143 end if;
3145 -- If not controlled type, then Prev_Tmp and Ctrl_Ref unused
3147 else
3148 Prev_Tmp := Empty;
3149 Ctrl_Ref := Empty;
3150 end if;
3152 -- Do the Assignment
3154 Append_To (Res, Relocate_Node (N));
3156 -- Restore the Tag
3158 if Save_Tag then
3159 Append_To (Res,
3160 Make_Assignment_Statement (Loc,
3161 Name =>
3162 Make_Selected_Component (Loc,
3163 Prefix => Duplicate_Subexpr_No_Checks (L),
3164 Selector_Name => New_Reference_To (Tag_Component (T), Loc)),
3165 Expression => New_Reference_To (Tag_Tmp, Loc)));
3166 end if;
3168 -- Restore the finalization pointers
3170 if Ctrl_Act then
3171 Append_To (Res,
3172 Make_Assignment_Statement (Loc,
3173 Name =>
3174 Make_Selected_Component (Loc,
3175 Prefix =>
3176 Unchecked_Convert_To (RTE (RE_Finalizable),
3177 New_Copy_Tree (Ctrl_Ref)),
3178 Selector_Name => Make_Identifier (Loc, Name_Prev)),
3179 Expression => New_Reference_To (Prev_Tmp, Loc)));
3181 Append_To (Res,
3182 Make_Assignment_Statement (Loc,
3183 Name =>
3184 Make_Selected_Component (Loc,
3185 Prefix =>
3186 Unchecked_Convert_To (RTE (RE_Finalizable),
3187 New_Copy_Tree (Ctrl_Ref)),
3188 Selector_Name => Make_Identifier (Loc, Name_Next)),
3189 Expression => New_Reference_To (Next_Tmp, Loc)));
3191 if Present (Ctrl_Ref2) then
3192 Append_To (Res,
3193 Make_Assignment_Statement (Loc,
3194 Name =>
3195 Make_Selected_Component (Loc,
3196 Prefix =>
3197 Unchecked_Convert_To (RTE (RE_Finalizable),
3198 New_Copy_Tree (Ctrl_Ref2)),
3199 Selector_Name => Make_Identifier (Loc, Name_Prev)),
3200 Expression => New_Reference_To (Prev_Tmp2, Loc)));
3202 Append_To (Res,
3203 Make_Assignment_Statement (Loc,
3204 Name =>
3205 Make_Selected_Component (Loc,
3206 Prefix =>
3207 Unchecked_Convert_To (RTE (RE_Finalizable),
3208 New_Copy_Tree (Ctrl_Ref2)),
3209 Selector_Name => Make_Identifier (Loc, Name_Next)),
3210 Expression => New_Reference_To (Next_Tmp2, Loc)));
3211 end if;
3212 end if;
3214 -- Adjust the target after the assignment when controlled. (not in
3215 -- the init proc since it is an initialization more than an
3216 -- assignment)
3218 if Ctrl_Act then
3219 Append_List_To (Res,
3220 Make_Adjust_Call (
3221 Ref => Duplicate_Subexpr_Move_Checks (L),
3222 Typ => Etype (L),
3223 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
3224 With_Attach => Make_Integer_Literal (Loc, 0)));
3225 end if;
3227 return Res;
3229 exception
3230 when RE_Not_Available =>
3231 return Empty_List;
3232 end Make_Tag_Ctrl_Assignment;
3234 ------------------------------------
3235 -- Possible_Bit_Aligned_Component --
3236 ------------------------------------
3238 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
3239 begin
3240 case Nkind (N) is
3242 -- Case of indexed component
3244 when N_Indexed_Component =>
3245 declare
3246 P : constant Node_Id := Prefix (N);
3247 Ptyp : constant Entity_Id := Etype (P);
3249 begin
3250 -- If we know the component size and it is less than 64, then
3251 -- we are definitely OK. The back end always does assignment
3252 -- of misaligned small objects correctly.
3254 if Known_Static_Component_Size (Ptyp)
3255 and then Component_Size (Ptyp) <= 64
3256 then
3257 return False;
3259 -- Otherwise, we need to test the prefix, to see if we are
3260 -- indexing from a possibly unaligned component.
3262 else
3263 return Possible_Bit_Aligned_Component (P);
3264 end if;
3265 end;
3267 -- Case of selected component
3269 when N_Selected_Component =>
3270 declare
3271 P : constant Node_Id := Prefix (N);
3272 Comp : constant Entity_Id := Entity (Selector_Name (N));
3274 begin
3275 -- If there is no component clause, then we are in the clear
3276 -- since the back end will never misalign a large component
3277 -- unless it is forced to do so. In the clear means we need
3278 -- only the recursive test on the prefix.
3280 if No (Component_Clause (Comp)) then
3281 return Possible_Bit_Aligned_Component (P);
3283 -- Otherwise we have a component clause, which means that
3284 -- the Esize and Normalized_First_Bit fields are set and
3285 -- contain static values known at compile time.
3287 else
3288 -- If we know that we have a small (64 bits or less) record
3289 -- or bit-packed array, then everything is fine, since the
3290 -- back end can handle these cases correctly.
3292 if Esize (Comp) <= 64
3293 and then (Is_Record_Type (Etype (Comp))
3294 or else
3295 Is_Bit_Packed_Array (Etype (Comp)))
3296 then
3297 return False;
3299 -- Otherwise if the component is not byte aligned, we
3300 -- know we have the nasty unaligned case.
3302 elsif Normalized_First_Bit (Comp) /= Uint_0
3303 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
3304 then
3305 return True;
3307 -- If we are large and byte aligned, then OK at this level
3308 -- but we still need to test our prefix recursively.
3310 else
3311 return Possible_Bit_Aligned_Component (P);
3312 end if;
3313 end if;
3314 end;
3316 -- If we have neither a record nor array component, it means that
3317 -- we have fallen off the top testing prefixes recursively, and
3318 -- we now have a stand alone object, where we don't have a problem
3320 when others =>
3321 return False;
3323 end case;
3324 end Possible_Bit_Aligned_Component;
3326 end Exp_Ch5;