OSDN Git Service

2005-03-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_intr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ I N T R                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 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 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Einfo;    use Einfo;
29 with Errout;   use Errout;
30 with Exp_Ch4;  use Exp_Ch4;
31 with Exp_Ch7;  use Exp_Ch7;
32 with Exp_Ch11; use Exp_Ch11;
33 with Exp_Code; use Exp_Code;
34 with Exp_Fixd; use Exp_Fixd;
35 with Exp_Util; use Exp_Util;
36 with Itypes;   use Itypes;
37 with Namet;    use Namet;
38 with Nmake;    use Nmake;
39 with Nlists;   use Nlists;
40 with Restrict; use Restrict;
41 with Rtsfind;  use Rtsfind;
42 with Sem;      use Sem;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res;  use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo;    use Sinfo;
47 with Sinput;   use Sinput;
48 with Snames;   use Snames;
49 with Stand;    use Stand;
50 with Stringt;  use Stringt;
51 with Tbuild;   use Tbuild;
52 with Uintp;    use Uintp;
53 with Urealp;   use Urealp;
54
55 package body Exp_Intr is
56
57    -----------------------
58    -- Local Subprograms --
59    -----------------------
60
61    procedure Expand_Is_Negative (N : Node_Id);
62    --  Expand a call to the intrinsic Is_Negative function
63
64    procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
65    --  Expand a call to Exception_Information/Message/Name. The first
66    --  parameter, N, is the node for the function call, and Ent is the
67    --  entity for the corresponding routine in the Ada.Exceptions package.
68
69    procedure Expand_Import_Call (N : Node_Id);
70    --  Expand a call to Import_Address/Longest_Integer/Value. The parameter
71    --  N is the node for the function call.
72
73    procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
74    --  Expand an intrinsic shift operation, N and E are from the call to
75    --  Expand_Intrinsic_Call (call node and subprogram spec entity) and
76    --  K is the kind for the shift node
77
78    procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
79    --  Expand a call to an instantiation of Unchecked_Convertion into a node
80    --  N_Unchecked_Type_Conversion.
81
82    procedure Expand_Unc_Deallocation (N : Node_Id);
83    --  Expand a call to an instantiation of Unchecked_Deallocation into a node
84    --  N_Free_Statement and appropriate context.
85
86    procedure Expand_To_Address (N : Node_Id);
87    procedure Expand_To_Pointer (N : Node_Id);
88    --  Expand a call to corresponding function, declared in an instance of
89    --  System.Addess_To_Access_Conversions.
90
91    procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
92    --  Rewrite the node by the appropriate string or positive constant.
93    --  Nam can be one of the following:
94    --    Name_File             - expand string that is the name of source file
95    --    Name_Line             - expand integer line number
96    --    Name_Source_Location  - expand string of form file:line
97    --    Name_Enclosing_Entity - expand string  with name of enclosing entity
98
99    ---------------------------
100    -- Expand_Exception_Call --
101    ---------------------------
102
103    --  If the function call is not within an exception handler, then the
104    --  call is replaced by a null string. Otherwise the appropriate routine
105    --  in Ada.Exceptions is called passing the choice parameter specification
106    --  from the enclosing handler. If the enclosing handler lacks a choice
107    --  parameter, then one is supplied.
108
109    procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
110       Loc : constant Source_Ptr := Sloc (N);
111       P   : Node_Id;
112       E   : Entity_Id;
113
114    begin
115       --  Climb up parents to see if we are in exception handler
116
117       P := Parent (N);
118       loop
119          --  Case of not in exception handler, replace by null string
120
121          if No (P) then
122             Rewrite (N,
123               Make_String_Literal (Loc,
124                 Strval => ""));
125             exit;
126
127          --  Case of in exception handler
128
129          elsif Nkind (P) = N_Exception_Handler then
130             if No (Choice_Parameter (P)) then
131
132                --  If no choice parameter present, then put one there. Note
133                --  that we do not need to put it on the entity chain, since
134                --  no one will be referencing it by normal visibility methods.
135
136                E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
137                Set_Choice_Parameter (P, E);
138                Set_Ekind (E, E_Variable);
139                Set_Etype (E, RTE (RE_Exception_Occurrence));
140                Set_Scope (E, Current_Scope);
141             end if;
142
143             Rewrite (N,
144               Make_Function_Call (Loc,
145                 Name => New_Occurrence_Of (RTE (Ent), Loc),
146                 Parameter_Associations => New_List (
147                   New_Occurrence_Of (Choice_Parameter (P), Loc))));
148             exit;
149
150          --  Keep climbing!
151
152          else
153             P := Parent (P);
154          end if;
155       end loop;
156
157       Analyze_And_Resolve (N, Standard_String);
158    end Expand_Exception_Call;
159
160    ------------------------
161    -- Expand_Import_Call --
162    ------------------------
163
164    --  The function call must have a static string as its argument. We create
165    --  a dummy variable which uses this string as the external name in an
166    --  Import pragma. The result is then obtained as the address of this
167    --  dummy variable, converted to the appropriate target type.
168
169    procedure Expand_Import_Call (N : Node_Id) is
170       Loc : constant Source_Ptr := Sloc (N);
171       Ent : constant Entity_Id  := Entity (Name (N));
172       Str : constant Node_Id    := First_Actual (N);
173       Dum : Entity_Id;
174
175    begin
176       Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
177
178       Insert_Actions (N, New_List (
179         Make_Object_Declaration (Loc,
180           Defining_Identifier => Dum,
181           Object_Definition   =>
182             New_Occurrence_Of (Standard_Character, Loc)),
183
184         Make_Pragma (Loc,
185           Chars => Name_Import,
186           Pragma_Argument_Associations => New_List (
187             Make_Pragma_Argument_Association (Loc,
188               Expression => Make_Identifier (Loc, Name_Ada)),
189
190             Make_Pragma_Argument_Association (Loc,
191               Expression => Make_Identifier (Loc, Chars (Dum))),
192
193             Make_Pragma_Argument_Association (Loc,
194               Chars => Name_Link_Name,
195               Expression => Relocate_Node (Str))))));
196
197       Rewrite (N,
198         Unchecked_Convert_To (Etype (Ent),
199           Make_Attribute_Reference (Loc,
200             Attribute_Name => Name_Address,
201             Prefix => Make_Identifier (Loc, Chars (Dum)))));
202
203       Analyze_And_Resolve (N, Etype (Ent));
204    end Expand_Import_Call;
205
206    ---------------------------
207    -- Expand_Intrinsic_Call --
208    ---------------------------
209
210    procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
211       Nam : Name_Id;
212
213    begin
214       --  If the intrinsic subprogram is generic, gets its original name
215
216       if Present (Parent (E))
217         and then Present (Generic_Parent (Parent (E)))
218       then
219          Nam := Chars (Generic_Parent (Parent (E)));
220       else
221          Nam := Chars (E);
222       end if;
223
224       if Nam = Name_Asm then
225          Expand_Asm_Call (N);
226
227       elsif Nam = Name_Divide then
228          Expand_Decimal_Divide_Call (N);
229
230       elsif Nam = Name_Exception_Information then
231          Expand_Exception_Call (N, RE_Exception_Information);
232
233       elsif Nam = Name_Exception_Message then
234          Expand_Exception_Call (N, RE_Exception_Message);
235
236       elsif Nam = Name_Exception_Name then
237          Expand_Exception_Call (N, RE_Exception_Name_Simple);
238
239       elsif Nam = Name_Import_Address
240               or else
241             Nam = Name_Import_Largest_Value
242               or else
243             Nam = Name_Import_Value
244       then
245          Expand_Import_Call (N);
246
247       elsif Nam = Name_Is_Negative then
248          Expand_Is_Negative (N);
249
250       elsif Nam = Name_Rotate_Left then
251          Expand_Shift (N, E, N_Op_Rotate_Left);
252
253       elsif Nam = Name_Rotate_Right then
254          Expand_Shift (N, E, N_Op_Rotate_Right);
255
256       elsif Nam = Name_Shift_Left then
257          Expand_Shift (N, E, N_Op_Shift_Left);
258
259       elsif Nam = Name_Shift_Right then
260          Expand_Shift (N, E, N_Op_Shift_Right);
261
262       elsif Nam = Name_Shift_Right_Arithmetic then
263          Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
264
265       elsif Nam = Name_Unchecked_Conversion then
266          Expand_Unc_Conversion (N, E);
267
268       elsif Nam = Name_Unchecked_Deallocation then
269          Expand_Unc_Deallocation (N);
270
271       elsif Nam = Name_To_Address then
272          Expand_To_Address (N);
273
274       elsif Nam = Name_To_Pointer then
275          Expand_To_Pointer (N);
276
277       elsif Nam = Name_File
278         or else Nam = Name_Line
279         or else Nam = Name_Source_Location
280         or else Nam = Name_Enclosing_Entity
281       then
282          Expand_Source_Info (N, Nam);
283
284          --  If we have a renaming, expand the call to the original operation,
285          --  which must itself be intrinsic, since renaming requires matching
286          --  conventions and this has already been checked.
287
288       elsif Present (Alias (E)) then
289          Expand_Intrinsic_Call (N,  Alias (E));
290
291          --  The only other case is where an external name was specified,
292          --  since this is the only way that an otherwise unrecognized
293          --  name could escape the checking in Sem_Prag. Nothing needs
294          --  to be done in such a case, since we pass such a call to the
295          --  back end unchanged.
296
297       else
298          null;
299       end if;
300    end Expand_Intrinsic_Call;
301
302    ------------------------
303    -- Expand_Is_Negative --
304    ------------------------
305
306    procedure Expand_Is_Negative (N : Node_Id) is
307       Loc   : constant Source_Ptr := Sloc (N);
308       Opnd  : constant Node_Id    := Relocate_Node (First_Actual (N));
309
310    begin
311
312       --  We replace the function call by the following expression
313
314       --    if Opnd < 0.0 then
315       --       True
316       --    else
317       --       if Opnd > 0.0 then
318       --          False;
319       --       else
320       --          Float_Unsigned!(Float (Opnd)) /= 0
321       --       end if;
322       --    end if;
323
324       Rewrite (N,
325         Make_Conditional_Expression (Loc,
326           Expressions => New_List (
327             Make_Op_Lt (Loc,
328               Left_Opnd  => Duplicate_Subexpr (Opnd),
329               Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
330
331             New_Occurrence_Of (Standard_True, Loc),
332
333             Make_Conditional_Expression (Loc,
334              Expressions => New_List (
335                Make_Op_Gt (Loc,
336                  Left_Opnd  => Duplicate_Subexpr_No_Checks (Opnd),
337                  Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
338
339                New_Occurrence_Of (Standard_False, Loc),
340
341                 Make_Op_Ne (Loc,
342                   Left_Opnd =>
343                     Unchecked_Convert_To
344                       (RTE (RE_Float_Unsigned),
345                        Convert_To
346                          (Standard_Float,
347                           Duplicate_Subexpr_No_Checks (Opnd))),
348                   Right_Opnd =>
349                     Make_Integer_Literal (Loc, 0)))))));
350
351       Analyze_And_Resolve (N, Standard_Boolean);
352    end Expand_Is_Negative;
353
354    ------------------
355    -- Expand_Shift --
356    ------------------
357
358    --  This procedure is used to convert a call to a shift function to the
359    --  corresponding operator node. This conversion is not done by the usual
360    --  circuit for converting calls to operator functions (e.g. "+"(1,2)) to
361    --  operator nodes, because shifts are not predefined operators.
362
363    --  As a result, whenever a shift is used in the source program, it will
364    --  remain as a call until converted by this routine to the operator node
365    --  form which Gigi is expecting to see.
366
367    --  Note: it is possible for the expander to generate shift operator nodes
368    --  directly, which will be analyzed in the normal manner by calling Analyze
369    --  and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
370
371    procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
372       Loc   : constant Source_Ptr := Sloc (N);
373       Typ   : constant Entity_Id  := Etype (N);
374       Left  : constant Node_Id    := First_Actual (N);
375       Right : constant Node_Id    := Next_Actual (Left);
376       Ltyp  : constant Node_Id    := Etype (Left);
377       Rtyp  : constant Node_Id    := Etype (Right);
378       Snode : Node_Id;
379
380    begin
381       Snode := New_Node (K, Loc);
382       Set_Left_Opnd  (Snode, Relocate_Node (Left));
383       Set_Right_Opnd (Snode, Relocate_Node (Right));
384       Set_Chars      (Snode, Chars (E));
385       Set_Etype      (Snode, Base_Type (Typ));
386       Set_Entity     (Snode, E);
387
388       if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
389         and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
390       then
391          Set_Shift_Count_OK (Snode, True);
392       end if;
393
394       --  Do the rewrite. Note that we don't call Analyze and Resolve on
395       --  this node, because it already got analyzed and resolved when
396       --  it was a function call!
397
398       Rewrite (N, Snode);
399       Set_Analyzed (N);
400    end Expand_Shift;
401
402    ------------------------
403    -- Expand_Source_Info --
404    ------------------------
405
406    procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
407       Loc : constant Source_Ptr := Sloc (N);
408       Ent : Entity_Id;
409
410    begin
411       --  Integer cases
412
413       if Nam = Name_Line then
414          Rewrite (N,
415            Make_Integer_Literal (Loc,
416              Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
417          Analyze_And_Resolve (N, Standard_Positive);
418
419       --  String cases
420
421       else
422          case Nam is
423             when Name_File =>
424                Get_Decoded_Name_String
425                  (Reference_Name (Get_Source_File_Index (Loc)));
426
427             when Name_Source_Location =>
428                Build_Location_String (Loc);
429
430             when Name_Enclosing_Entity =>
431                Name_Len := 0;
432
433                Ent := Current_Scope;
434
435                --  Skip enclosing blocks to reach enclosing unit.
436
437                while Present (Ent) loop
438                   exit when Ekind (Ent) /= E_Block
439                     and then Ekind (Ent) /= E_Loop;
440                   Ent := Scope (Ent);
441                end loop;
442
443                --  Ent now points to the relevant defining entity
444
445                declare
446                   SDef : Source_Ptr := Sloc (Ent);
447                   TDef : Source_Buffer_Ptr;
448
449                begin
450                   TDef := Source_Text (Get_Source_File_Index (SDef));
451                   Name_Len := 0;
452
453                   while TDef (SDef) in '0' .. '9'
454                     or else TDef (SDef) >= 'A'
455                     or else TDef (SDef) = ASCII.ESC
456                   loop
457                      Add_Char_To_Name_Buffer (TDef (SDef));
458                      SDef := SDef + 1;
459                   end loop;
460                end;
461
462             when others =>
463                raise Program_Error;
464          end case;
465
466          Rewrite (N,
467            Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
468          Analyze_And_Resolve (N, Standard_String);
469       end if;
470
471       Set_Is_Static_Expression (N);
472    end Expand_Source_Info;
473
474    ---------------------------
475    -- Expand_Unc_Conversion --
476    ---------------------------
477
478    procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
479       Func : constant Entity_Id  := Entity (Name (N));
480       Conv : Node_Id;
481       Ftyp : Entity_Id;
482       Ttyp : Entity_Id;
483
484    begin
485       --  Rewrite as unchecked conversion node. Note that we must convert
486       --  the operand to the formal type of the input parameter of the
487       --  function, so that the resulting N_Unchecked_Type_Conversion
488       --  call indicates the correct types for Gigi.
489
490       --  Right now, we only do this if a scalar type is involved. It is
491       --  not clear if it is needed in other cases. If we do attempt to
492       --  do the conversion unconditionally, it crashes 3411-018. To be
493       --  investigated further ???
494
495       Conv := Relocate_Node (First_Actual (N));
496       Ftyp := Etype (First_Formal (Func));
497
498       if Is_Scalar_Type (Ftyp) then
499          Conv := Convert_To (Ftyp, Conv);
500          Set_Parent (Conv, N);
501          Analyze_And_Resolve (Conv);
502       end if;
503
504       --  The instantiation of Unchecked_Conversion creates a wrapper package,
505       --  and the target type is declared as a subtype of the actual. Recover
506       --  the actual, which is the subtype indic. in the subtype declaration
507       --  for the target type. This is semantically correct, and avoids
508       --  anomalies with access subtypes. For entities, leave type as is.
509
510       --  We do the analysis here, because we do not want the compiler
511       --  to try to optimize or otherwise reorganize the unchecked
512       --  conversion node.
513
514       Ttyp := Etype (E);
515
516       if Is_Entity_Name (Conv) then
517          null;
518
519       elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
520          Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
521
522       elsif Is_Itype (Ttyp) then
523          Ttyp :=
524            Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
525       else
526          raise Program_Error;
527       end if;
528
529       Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
530       Set_Etype (N, Ttyp);
531       Set_Analyzed (N);
532
533       if Nkind (N) = N_Unchecked_Type_Conversion then
534          Expand_N_Unchecked_Type_Conversion (N);
535       end if;
536    end Expand_Unc_Conversion;
537
538    -----------------------------
539    -- Expand_Unc_Deallocation --
540    -----------------------------
541
542    --  Generate the following Code :
543
544    --    if Arg /= null then
545    --     <Finalize_Call> (.., T'Class(Arg.all), ..);  -- for controlled types
546    --       Free (Arg);
547    --       Arg := Null;
548    --    end if;
549
550    --  For a task, we also generate a call to Free_Task to ensure that the
551    --  task itself is freed if it is terminated, ditto for a simple protected
552    --  object, with a call to Finalize_Protection. For composite types that
553    --  have tasks or simple protected objects as components, we traverse the
554    --  structures to find and terminate those components.
555
556    procedure Expand_Unc_Deallocation (N : Node_Id) is
557       Loc   : constant Source_Ptr := Sloc (N);
558       Arg   : constant Node_Id    := First_Actual (N);
559       Typ   : constant Entity_Id  := Etype (Arg);
560       Stmts : constant List_Id    := New_List;
561       Rtyp  : constant Entity_Id  := Underlying_Type (Root_Type (Typ));
562       Pool  : constant Entity_Id  := Associated_Storage_Pool (Rtyp);
563
564       Desig_T   : constant Entity_Id  := Designated_Type (Typ);
565       Gen_Code  : Node_Id;
566       Free_Node : Node_Id;
567       Deref     : Node_Id;
568       Free_Arg  : Node_Id;
569       Free_Cod  : List_Id;
570       Blk       : Node_Id;
571
572    begin
573       if No_Pool_Assigned (Rtyp) then
574          Error_Msg_N ("?deallocation from empty storage pool", N);
575       end if;
576
577       if Controlled_Type (Desig_T) then
578          Deref :=
579            Make_Explicit_Dereference (Loc,
580              Prefix => Duplicate_Subexpr_No_Checks (Arg));
581
582          --  If the type is tagged, then we must force dispatching on the
583          --  finalization call because the designated type may not be the
584          --  actual type of the object
585
586          if Is_Tagged_Type (Desig_T)
587            and then not Is_Class_Wide_Type (Desig_T)
588          then
589             Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
590          end if;
591
592          Free_Cod :=
593            Make_Final_Call
594             (Ref         => Deref,
595              Typ         => Desig_T,
596              With_Detach => New_Reference_To (Standard_True, Loc));
597
598          if Abort_Allowed then
599             Prepend_To (Free_Cod,
600               Build_Runtime_Call (Loc, RE_Abort_Defer));
601
602             Blk :=
603               Make_Block_Statement (Loc, Handled_Statement_Sequence =>
604                 Make_Handled_Sequence_Of_Statements (Loc,
605                   Statements  => Free_Cod,
606                   At_End_Proc =>
607                     New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
608
609             --  We now expand the exception (at end) handler. We set a
610             --  temporary parent pointer since we have not attached Blk
611             --  to the tree yet.
612
613             Set_Parent (Blk, N);
614             Analyze (Blk);
615             Expand_At_End_Handler
616               (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
617             Append (Blk, Stmts);
618
619          else
620             Append_List_To (Stmts, Free_Cod);
621          end if;
622       end if;
623
624       --  For a task type, call Free_Task before freeing the ATCB
625
626       if Is_Task_Type (Desig_T) then
627          declare
628             Stat : Node_Id := Prev (N);
629             Nam1 : Node_Id;
630             Nam2 : Node_Id;
631
632          begin
633             --  An Abort followed by a Free will not do what the user
634             --  expects, because the abort is not immediate. This is
635             --  worth a friendly warning.
636
637             while Present (Stat)
638               and then not Comes_From_Source (Original_Node (Stat))
639             loop
640                Prev (Stat);
641             end loop;
642
643             if Present (Stat)
644               and then Nkind (Original_Node (Stat)) = N_Abort_Statement
645             then
646                Stat := Original_Node (Stat);
647                Nam1 := First (Names (Stat));
648                Nam2 := Original_Node (First (Parameter_Associations (N)));
649
650                if Nkind (Nam1) = N_Explicit_Dereference
651                  and then Is_Entity_Name (Prefix (Nam1))
652                  and then Is_Entity_Name (Nam2)
653                  and then Entity (Prefix (Nam1)) = Entity (Nam2)
654                then
655                   Error_Msg_N ("Abort may take time to complete?", N);
656                   Error_Msg_N ("\deallocation might have no effect?", N);
657                   Error_Msg_N ("\safer to wait for termination.?", N);
658                end if;
659             end if;
660          end;
661
662          Append_To
663            (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
664
665       --  For composite types that contain tasks, recurse over the structure
666       --  to build the selectors for the task subcomponents.
667
668       elsif Has_Task (Desig_T) then
669          if Is_Record_Type (Desig_T) then
670             Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
671
672          elsif Is_Array_Type (Desig_T) then
673             Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
674          end if;
675       end if;
676
677       --  Same for simple protected types. Eventually call Finalize_Protection
678       --  before freeing the PO for each protected component.
679
680       if Is_Simple_Protected_Type (Desig_T) then
681          Append_To (Stmts,
682            Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
683
684       elsif Has_Simple_Protected_Object (Desig_T) then
685          if Is_Record_Type (Desig_T) then
686             Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
687          elsif Is_Array_Type (Desig_T) then
688             Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
689          end if;
690       end if;
691
692       --  Normal processing for non-controlled types
693
694       Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
695       Free_Node := Make_Free_Statement (Loc, Empty);
696       Append_To (Stmts, Free_Node);
697       Set_Storage_Pool (Free_Node, Pool);
698
699       --  Make implicit if statement. We omit this if we are the then part
700       --  of a test of the form:
701
702       --    if not (Arg = null) then
703
704       --  i.e. if the test is explicit in the source. Arg must be a simple
705       --  identifier for the purposes of this special test. Note that the
706       --  use of /= in the source is always transformed into the above form.
707
708       declare
709          Test_Needed : Boolean := True;
710          P           : constant Node_Id := Parent (N);
711          C           : Node_Id;
712
713       begin
714          if Nkind (Arg) = N_Identifier
715            and then Nkind (P) =  N_If_Statement
716            and then First (Then_Statements (P)) = N
717          then
718             if Nkind (Condition (P)) = N_Op_Not then
719                C := Right_Opnd (Condition (P));
720
721                if Nkind (C) = N_Op_Eq
722                  and then Nkind (Left_Opnd (C)) = N_Identifier
723                  and then Chars (Arg) = Chars (Left_Opnd (C))
724                  and then Nkind (Right_Opnd (C)) = N_Null
725                then
726                   Test_Needed := False;
727                end if;
728             end if;
729          end if;
730
731          --  Generate If_Statement if needed
732
733          if Test_Needed then
734             Gen_Code :=
735               Make_Implicit_If_Statement (N,
736                 Condition =>
737                   Make_Op_Ne (Loc,
738                     Left_Opnd  => Duplicate_Subexpr (Arg),
739                     Right_Opnd => Make_Null (Loc)),
740                 Then_Statements => Stmts);
741
742          else
743             Gen_Code :=
744               Make_Block_Statement (Loc,
745                 Handled_Statement_Sequence =>
746                   Make_Handled_Sequence_Of_Statements (Loc,
747                     Statements => Stmts));
748          end if;
749       end;
750
751       --  Deal with storage pool
752
753       if Present (Pool) then
754
755          --  Freeing the secondary stack is meaningless
756
757          if Is_RTE (Pool, RE_SS_Pool) then
758             null;
759
760          elsif Is_Class_Wide_Type (Etype (Pool)) then
761             Set_Procedure_To_Call (Free_Node,
762               RTE (RE_Deallocate_Any));
763          else
764             Set_Procedure_To_Call (Free_Node,
765               Find_Prim_Op (Etype (Pool), Name_Deallocate));
766
767             --  If the type is class wide, we generate an implicit type
768             --  with the right dynamic size, so that the deallocate call
769             --  gets the right size parameter computed by gigi
770
771             if Is_Class_Wide_Type (Desig_T) then
772                declare
773                   Acc_Type : constant Entity_Id :=
774                                Create_Itype (E_Access_Type, N);
775                   Deref    : constant Node_Id :=
776                                Make_Explicit_Dereference (Loc,
777                                  Duplicate_Subexpr_No_Checks (Arg));
778
779                begin
780                   Set_Etype  (Deref, Typ);
781                   Set_Parent (Deref, Free_Node);
782
783                   Set_Etype     (Acc_Type, Acc_Type);
784                   Set_Size_Info (Acc_Type, Typ);
785                   Set_Directly_Designated_Type
786                                 (Acc_Type, Entity (Make_Subtype_From_Expr
787                                                     (Deref, Desig_T)));
788
789                   Free_Arg := Unchecked_Convert_To (Acc_Type, Free_Arg);
790                end;
791             end if;
792          end if;
793       end if;
794
795       Set_Expression (Free_Node, Free_Arg);
796
797       declare
798          Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
799
800       begin
801          Set_Assignment_OK (Lhs);
802          Append_To (Stmts,
803            Make_Assignment_Statement (Loc,
804              Name       => Lhs,
805              Expression => Make_Null (Loc)));
806       end;
807
808       Rewrite (N, Gen_Code);
809       Analyze (N);
810    end Expand_Unc_Deallocation;
811
812    -----------------------
813    -- Expand_To_Address --
814    -----------------------
815
816    procedure Expand_To_Address (N : Node_Id) is
817       Loc : constant Source_Ptr := Sloc (N);
818       Arg : constant Node_Id := First_Actual (N);
819       Obj : Node_Id;
820
821    begin
822       Remove_Side_Effects (Arg);
823
824       Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
825
826       Rewrite (N,
827         Make_Conditional_Expression (Loc,
828           Expressions => New_List (
829             Make_Op_Eq (Loc,
830               Left_Opnd => New_Copy_Tree (Arg),
831               Right_Opnd => Make_Null (Loc)),
832             New_Occurrence_Of (RTE (RE_Null_Address), Loc),
833             Make_Attribute_Reference (Loc,
834               Attribute_Name => Name_Address,
835               Prefix => Obj))));
836
837       Analyze_And_Resolve (N, RTE (RE_Address));
838    end Expand_To_Address;
839
840    -----------------------
841    -- Expand_To_Pointer --
842    -----------------------
843
844    procedure Expand_To_Pointer (N : Node_Id) is
845       Arg : constant Node_Id := First_Actual (N);
846
847    begin
848       Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
849       Analyze (N);
850    end Expand_To_Pointer;
851
852 end Exp_Intr;