OSDN Git Service

PR 33870
[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-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Einfo;    use Einfo;
29 with Elists;   use Elists;
30 with Errout;   use Errout;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4;  use Exp_Ch4;
33 with Exp_Ch7;  use Exp_Ch7;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Code; use Exp_Code;
36 with Exp_Fixd; use Exp_Fixd;
37 with Exp_Util; use Exp_Util;
38 with Freeze;   use Freeze;
39 with Namet;    use Namet;
40 with Nmake;    use Nmake;
41 with Nlists;   use Nlists;
42 with Restrict; use Restrict;
43 with Rident;   use Rident;
44 with Rtsfind;  use Rtsfind;
45 with Sem;      use Sem;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res;  use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Sinfo;    use Sinfo;
50 with Sinput;   use Sinput;
51 with Snames;   use Snames;
52 with Stand;    use Stand;
53 with Stringt;  use Stringt;
54 with Tbuild;   use Tbuild;
55 with Uintp;    use Uintp;
56 with Urealp;   use Urealp;
57
58 package body Exp_Intr is
59
60    -----------------------
61    -- Local Subprograms --
62    -----------------------
63
64    procedure Expand_Is_Negative (N : Node_Id);
65    --  Expand a call to the intrinsic Is_Negative function
66
67    procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
68    --  Expand a call to an instantiation of Generic_Dispatching_Constructor
69    --  into a dispatching call to the actual subprogram associated with the
70    --  Constructor formal subprogram, passing it the Parameters actual of
71    --  the call to the instantiation and dispatching based on call's Tag
72    --  parameter.
73
74    procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
75    --  Expand a call to Exception_Information/Message/Name. The first
76    --  parameter, N, is the node for the function call, and Ent is the
77    --  entity for the corresponding routine in the Ada.Exceptions package.
78
79    procedure Expand_Import_Call (N : Node_Id);
80    --  Expand a call to Import_Address/Longest_Integer/Value. The parameter
81    --  N is the node for the function call.
82
83    procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
84    --  Expand an intrinsic shift operation, N and E are from the call to
85    --  Expand_Intrinsic_Call (call node and subprogram spec entity) and
86    --  K is the kind for the shift node
87
88    procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
89    --  Expand a call to an instantiation of Unchecked_Convertion into a node
90    --  N_Unchecked_Type_Conversion.
91
92    procedure Expand_Unc_Deallocation (N : Node_Id);
93    --  Expand a call to an instantiation of Unchecked_Deallocation into a node
94    --  N_Free_Statement and appropriate context.
95
96    procedure Expand_To_Address (N : Node_Id);
97    procedure Expand_To_Pointer (N : Node_Id);
98    --  Expand a call to corresponding function, declared in an instance of
99    --  System.Addess_To_Access_Conversions.
100
101    procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
102    --  Rewrite the node by the appropriate string or positive constant.
103    --  Nam can be one of the following:
104    --    Name_File             - expand string that is the name of source file
105    --    Name_Line             - expand integer line number
106    --    Name_Source_Location  - expand string of form file:line
107    --    Name_Enclosing_Entity - expand string  with name of enclosing entity
108
109    -----------------------------------------
110    -- Expand_Dispatching_Constructor_Call --
111    -----------------------------------------
112
113    --  Transform a call to an instantiation of Generic_Dispatching_Constructor
114    --  of the form:
115
116    --     GDC_Instance (The_Tag, Parameters'Access)
117
118    --  to a class-wide conversion of a dispatching call to the actual
119    --  associated with the formal subprogram Construct, designating The_Tag
120    --  as the controlling tag of the call:
121
122    --     T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
123
124    --  which will eventually be expanded to the following:
125
126    --     T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
127
128    --  A class-wide membership test is also generated, preceding the call, to
129    --  ensure that the controlling tag denotes a type in T'Class.
130
131    procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
132       Loc        : constant Source_Ptr := Sloc (N);
133       Tag_Arg    : constant Node_Id    := First_Actual (N);
134       Param_Arg  : constant Node_Id    := Next_Actual (Tag_Arg);
135       Subp_Decl  : constant Node_Id    := Parent (Parent (Entity (Name (N))));
136       Inst_Pkg   : constant Node_Id    := Parent (Subp_Decl);
137       Act_Rename : Node_Id;
138       Act_Constr : Entity_Id;
139       Result_Typ : Entity_Id;
140       Cnstr_Call : Node_Id;
141
142    begin
143       --  The subprogram is the third actual in the instantiation, and is
144       --  retrieved from the corresponding renaming declaration. However,
145       --  freeze nodes may appear before, so we retrieve the declaration
146       --  with an explicit loop.
147
148       Act_Rename := First (Visible_Declarations (Inst_Pkg));
149       while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
150          Next (Act_Rename);
151       end loop;
152
153       Act_Constr := Entity (Name (Act_Rename));
154       Result_Typ := Class_Wide_Type (Etype (Act_Constr));
155
156       --  Ada 2005 (AI-251): If the result is an interface type, the function
157       --  returns a class-wide interface type (otherwise the resulting object
158       --  would be abstract!)
159
160       if Is_Interface (Etype (Act_Constr)) then
161          Set_Etype (Act_Constr, Result_Typ);
162       end if;
163
164       --  Create the call to the actual Constructor function
165
166       Cnstr_Call :=
167         Make_Function_Call (Loc,
168           Name                   => New_Occurrence_Of (Act_Constr, Loc),
169           Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
170
171       --  Establish its controlling tag from the tag passed to the instance
172       --  The tag may be given by a function call, in which case a temporary
173       --  should be generated now, to prevent out-of-order insertions during
174       --  the expansion of that call when stack-checking is enabled.
175
176       Remove_Side_Effects (Tag_Arg);
177       Set_Controlling_Argument (Cnstr_Call, Relocate_Node (Tag_Arg));
178
179       --  Rewrite and analyze the call to the instance as a class-wide
180       --  conversion of the call to the actual constructor.
181
182       Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
183       Analyze_And_Resolve (N, Etype (Act_Constr));
184
185       --  Do not generate a run-time check on the built object if tag
186       --  checks are suppressed for the result type.
187
188       if Tag_Checks_Suppressed (Etype (Result_Typ)) then
189          null;
190
191       --  Generate a class-wide membership test to ensure that the call's tag
192       --  argument denotes a type within the class. We must keep separate the
193       --  case in which the Result_Type of the constructor function is a tagged
194       --  type from the case in which it is an abstract interface because the
195       --  run-time subprogram required to check these cases differ (and have
196       --  one difference in their parameters profile).
197
198       --  Call CW_Membership if the Result_Type is a tagged type to look for
199       --  the tag in the table of ancestor tags.
200
201       elsif not Is_Interface (Result_Typ) then
202          Insert_Action (N,
203            Make_Implicit_If_Statement (N,
204              Condition =>
205                Make_Op_Not (Loc,
206                  Build_CW_Membership (Loc,
207                    Obj_Tag_Node => Duplicate_Subexpr (Tag_Arg),
208                    Typ_Tag_Node =>
209                      New_Reference_To (
210                         Node (First_Elmt (Access_Disp_Table (
211                                             Root_Type (Result_Typ)))), Loc))),
212              Then_Statements =>
213                New_List (Make_Raise_Statement (Loc,
214                            New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
215
216       --  Call IW_Membership test if the Result_Type is an abstract interface
217       --  to look for the tag in the table of interface tags.
218
219       else
220          Insert_Action (N,
221            Make_Implicit_If_Statement (N,
222              Condition =>
223                Make_Op_Not (Loc,
224                  Make_Function_Call (Loc,
225                     Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
226                     Parameter_Associations => New_List (
227                       Make_Attribute_Reference (Loc,
228                         Prefix => Duplicate_Subexpr (Tag_Arg),
229                         Attribute_Name => Name_Address),
230
231                       New_Reference_To (
232                         Node (First_Elmt (Access_Disp_Table (
233                                             Root_Type (Result_Typ)))), Loc)))),
234              Then_Statements =>
235                New_List (
236                  Make_Raise_Statement (Loc,
237                    Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
238       end if;
239    end Expand_Dispatching_Constructor_Call;
240
241    ---------------------------
242    -- Expand_Exception_Call --
243    ---------------------------
244
245    --  If the function call is not within an exception handler, then the call
246    --  is replaced by a null string. Otherwise the appropriate routine in
247    --  Ada.Exceptions is called passing the choice parameter specification
248    --  from the enclosing handler. If the enclosing handler lacks a choice
249    --  parameter, then one is supplied.
250
251    procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
252       Loc : constant Source_Ptr := Sloc (N);
253       P   : Node_Id;
254       E   : Entity_Id;
255
256    begin
257       --  Climb up parents to see if we are in exception handler
258
259       P := Parent (N);
260       loop
261          --  Case of not in exception handler, replace by null string
262
263          if No (P) then
264             Rewrite (N,
265               Make_String_Literal (Loc,
266                 Strval => ""));
267             exit;
268
269          --  Case of in exception handler
270
271          elsif Nkind (P) = N_Exception_Handler then
272
273             --  Handler cannot be used for a local raise, and furthermore, this
274             --  is a violation of the No_Exception_Propagation restriction.
275
276             Set_Local_Raise_Not_OK (P);
277             Check_Restriction (No_Exception_Propagation, N);
278
279             --  If no choice parameter present, then put one there. Note that
280             --  we do not need to put it on the entity chain, since no one will
281             --  be referencing it by normal visibility methods.
282
283             if No (Choice_Parameter (P)) then
284                E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
285                Set_Choice_Parameter (P, E);
286                Set_Ekind (E, E_Variable);
287                Set_Etype (E, RTE (RE_Exception_Occurrence));
288                Set_Scope (E, Current_Scope);
289             end if;
290
291             Rewrite (N,
292               Make_Function_Call (Loc,
293                 Name => New_Occurrence_Of (RTE (Ent), Loc),
294                 Parameter_Associations => New_List (
295                   New_Occurrence_Of (Choice_Parameter (P), Loc))));
296             exit;
297
298          --  Keep climbing!
299
300          else
301             P := Parent (P);
302          end if;
303       end loop;
304
305       Analyze_And_Resolve (N, Standard_String);
306    end Expand_Exception_Call;
307
308    ------------------------
309    -- Expand_Import_Call --
310    ------------------------
311
312    --  The function call must have a static string as its argument. We create
313    --  a dummy variable which uses this string as the external name in an
314    --  Import pragma. The result is then obtained as the address of this
315    --  dummy variable, converted to the appropriate target type.
316
317    procedure Expand_Import_Call (N : Node_Id) is
318       Loc : constant Source_Ptr := Sloc (N);
319       Ent : constant Entity_Id  := Entity (Name (N));
320       Str : constant Node_Id    := First_Actual (N);
321       Dum : Entity_Id;
322
323    begin
324       Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
325
326       Insert_Actions (N, New_List (
327         Make_Object_Declaration (Loc,
328           Defining_Identifier => Dum,
329           Object_Definition   =>
330             New_Occurrence_Of (Standard_Character, Loc)),
331
332         Make_Pragma (Loc,
333           Chars => Name_Import,
334           Pragma_Argument_Associations => New_List (
335             Make_Pragma_Argument_Association (Loc,
336               Expression => Make_Identifier (Loc, Name_Ada)),
337
338             Make_Pragma_Argument_Association (Loc,
339               Expression => Make_Identifier (Loc, Chars (Dum))),
340
341             Make_Pragma_Argument_Association (Loc,
342               Chars => Name_Link_Name,
343               Expression => Relocate_Node (Str))))));
344
345       Rewrite (N,
346         Unchecked_Convert_To (Etype (Ent),
347           Make_Attribute_Reference (Loc,
348             Attribute_Name => Name_Address,
349             Prefix => Make_Identifier (Loc, Chars (Dum)))));
350
351       Analyze_And_Resolve (N, Etype (Ent));
352    end Expand_Import_Call;
353
354    ---------------------------
355    -- Expand_Intrinsic_Call --
356    ---------------------------
357
358    procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
359       Nam : Name_Id;
360
361    begin
362       --  If the intrinsic subprogram is generic, gets its original name
363
364       if Present (Parent (E))
365         and then Present (Generic_Parent (Parent (E)))
366       then
367          Nam := Chars (Generic_Parent (Parent (E)));
368       else
369          Nam := Chars (E);
370       end if;
371
372       if Nam = Name_Asm then
373          Expand_Asm_Call (N);
374
375       elsif Nam = Name_Divide then
376          Expand_Decimal_Divide_Call (N);
377
378       elsif Nam = Name_Exception_Information then
379          Expand_Exception_Call (N, RE_Exception_Information);
380
381       elsif Nam = Name_Exception_Message then
382          Expand_Exception_Call (N, RE_Exception_Message);
383
384       elsif Nam = Name_Exception_Name then
385          Expand_Exception_Call (N, RE_Exception_Name_Simple);
386
387       elsif Nam = Name_Generic_Dispatching_Constructor then
388          Expand_Dispatching_Constructor_Call (N);
389
390       elsif Nam = Name_Import_Address
391               or else
392             Nam = Name_Import_Largest_Value
393               or else
394             Nam = Name_Import_Value
395       then
396          Expand_Import_Call (N);
397
398       elsif Nam = Name_Is_Negative then
399          Expand_Is_Negative (N);
400
401       elsif Nam = Name_Rotate_Left then
402          Expand_Shift (N, E, N_Op_Rotate_Left);
403
404       elsif Nam = Name_Rotate_Right then
405          Expand_Shift (N, E, N_Op_Rotate_Right);
406
407       elsif Nam = Name_Shift_Left then
408          Expand_Shift (N, E, N_Op_Shift_Left);
409
410       elsif Nam = Name_Shift_Right then
411          Expand_Shift (N, E, N_Op_Shift_Right);
412
413       elsif Nam = Name_Shift_Right_Arithmetic then
414          Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
415
416       elsif Nam = Name_Unchecked_Conversion then
417          Expand_Unc_Conversion (N, E);
418
419       elsif Nam = Name_Unchecked_Deallocation then
420          Expand_Unc_Deallocation (N);
421
422       elsif Nam = Name_To_Address then
423          Expand_To_Address (N);
424
425       elsif Nam = Name_To_Pointer then
426          Expand_To_Pointer (N);
427
428       elsif Nam = Name_File
429         or else Nam = Name_Line
430         or else Nam = Name_Source_Location
431         or else Nam = Name_Enclosing_Entity
432       then
433          Expand_Source_Info (N, Nam);
434
435          --  If we have a renaming, expand the call to the original operation,
436          --  which must itself be intrinsic, since renaming requires matching
437          --  conventions and this has already been checked.
438
439       elsif Present (Alias (E)) then
440          Expand_Intrinsic_Call (N,  Alias (E));
441
442          --  The only other case is where an external name was specified,
443          --  since this is the only way that an otherwise unrecognized
444          --  name could escape the checking in Sem_Prag. Nothing needs
445          --  to be done in such a case, since we pass such a call to the
446          --  back end unchanged.
447
448       else
449          null;
450       end if;
451    end Expand_Intrinsic_Call;
452
453    ------------------------
454    -- Expand_Is_Negative --
455    ------------------------
456
457    procedure Expand_Is_Negative (N : Node_Id) is
458       Loc   : constant Source_Ptr := Sloc (N);
459       Opnd  : constant Node_Id    := Relocate_Node (First_Actual (N));
460
461    begin
462
463       --  We replace the function call by the following expression
464
465       --    if Opnd < 0.0 then
466       --       True
467       --    else
468       --       if Opnd > 0.0 then
469       --          False;
470       --       else
471       --          Float_Unsigned!(Float (Opnd)) /= 0
472       --       end if;
473       --    end if;
474
475       Rewrite (N,
476         Make_Conditional_Expression (Loc,
477           Expressions => New_List (
478             Make_Op_Lt (Loc,
479               Left_Opnd  => Duplicate_Subexpr (Opnd),
480               Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
481
482             New_Occurrence_Of (Standard_True, Loc),
483
484             Make_Conditional_Expression (Loc,
485              Expressions => New_List (
486                Make_Op_Gt (Loc,
487                  Left_Opnd  => Duplicate_Subexpr_No_Checks (Opnd),
488                  Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
489
490                New_Occurrence_Of (Standard_False, Loc),
491
492                 Make_Op_Ne (Loc,
493                   Left_Opnd =>
494                     Unchecked_Convert_To
495                       (RTE (RE_Float_Unsigned),
496                        Convert_To
497                          (Standard_Float,
498                           Duplicate_Subexpr_No_Checks (Opnd))),
499                   Right_Opnd =>
500                     Make_Integer_Literal (Loc, 0)))))));
501
502       Analyze_And_Resolve (N, Standard_Boolean);
503    end Expand_Is_Negative;
504
505    ------------------
506    -- Expand_Shift --
507    ------------------
508
509    --  This procedure is used to convert a call to a shift function to the
510    --  corresponding operator node. This conversion is not done by the usual
511    --  circuit for converting calls to operator functions (e.g. "+"(1,2)) to
512    --  operator nodes, because shifts are not predefined operators.
513
514    --  As a result, whenever a shift is used in the source program, it will
515    --  remain as a call until converted by this routine to the operator node
516    --  form which Gigi is expecting to see.
517
518    --  Note: it is possible for the expander to generate shift operator nodes
519    --  directly, which will be analyzed in the normal manner by calling Analyze
520    --  and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
521
522    procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
523       Loc   : constant Source_Ptr := Sloc (N);
524       Typ   : constant Entity_Id  := Etype (N);
525       Left  : constant Node_Id    := First_Actual (N);
526       Right : constant Node_Id    := Next_Actual (Left);
527       Ltyp  : constant Node_Id    := Etype (Left);
528       Rtyp  : constant Node_Id    := Etype (Right);
529       Snode : Node_Id;
530
531    begin
532       Snode := New_Node (K, Loc);
533       Set_Left_Opnd  (Snode, Relocate_Node (Left));
534       Set_Right_Opnd (Snode, Relocate_Node (Right));
535       Set_Chars      (Snode, Chars (E));
536       Set_Etype      (Snode, Base_Type (Typ));
537       Set_Entity     (Snode, E);
538
539       if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
540         and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
541       then
542          Set_Shift_Count_OK (Snode, True);
543       end if;
544
545       --  Do the rewrite. Note that we don't call Analyze and Resolve on
546       --  this node, because it already got analyzed and resolved when
547       --  it was a function call!
548
549       Rewrite (N, Snode);
550       Set_Analyzed (N);
551    end Expand_Shift;
552
553    ------------------------
554    -- Expand_Source_Info --
555    ------------------------
556
557    procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
558       Loc : constant Source_Ptr := Sloc (N);
559       Ent : Entity_Id;
560
561       procedure Write_Entity_Name (E : Entity_Id);
562       --  Recursive procedure to construct string for qualified name of
563       --  enclosing program unit. The qualification stops at an enclosing
564       --  scope has no source name (block or loop). If entity is a subprogram
565       --  instance, skip enclosing wrapper package.
566
567       -----------------------
568       -- Write_Entity_Name --
569       -----------------------
570
571       procedure Write_Entity_Name (E : Entity_Id) is
572          SDef : Source_Ptr;
573          TDef : constant Source_Buffer_Ptr :=
574                   Source_Text (Get_Source_File_Index (Sloc (E)));
575
576       begin
577          --  Nothing to do if at outer level
578
579          if Scope (E) = Standard_Standard then
580             null;
581
582          --  If scope comes from source, write its name
583
584          elsif Comes_From_Source (Scope (E)) then
585             Write_Entity_Name (Scope (E));
586             Add_Char_To_Name_Buffer ('.');
587
588          --  If in wrapper package skip past it
589
590          elsif Is_Wrapper_Package (Scope (E)) then
591             Write_Entity_Name (Scope (Scope (E)));
592             Add_Char_To_Name_Buffer ('.');
593
594          --  Otherwise nothing to output (happens in unnamed block statements)
595
596          else
597             null;
598          end if;
599
600          --  Loop to output the name
601
602          --  is this right wrt wide char encodings ??? (no!)
603
604          SDef := Sloc (E);
605          while TDef (SDef) in '0' .. '9'
606            or else TDef (SDef) >= 'A'
607            or else TDef (SDef) = ASCII.ESC
608          loop
609             Add_Char_To_Name_Buffer (TDef (SDef));
610             SDef := SDef + 1;
611          end loop;
612       end Write_Entity_Name;
613
614    --  Start of processing for Expand_Source_Info
615
616    begin
617       --  Integer cases
618
619       if Nam = Name_Line then
620          Rewrite (N,
621            Make_Integer_Literal (Loc,
622              Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
623          Analyze_And_Resolve (N, Standard_Positive);
624
625       --  String cases
626
627       else
628          case Nam is
629             when Name_File =>
630                Get_Decoded_Name_String
631                  (Reference_Name (Get_Source_File_Index (Loc)));
632
633             when Name_Source_Location =>
634                Build_Location_String (Loc);
635
636             when Name_Enclosing_Entity =>
637                Name_Len := 0;
638
639                Ent := Current_Scope;
640
641                --  Skip enclosing blocks to reach enclosing unit
642
643                while Present (Ent) loop
644                   exit when Ekind (Ent) /= E_Block
645                     and then Ekind (Ent) /= E_Loop;
646                   Ent := Scope (Ent);
647                end loop;
648
649                --  Ent now points to the relevant defining entity
650
651                Name_Len := 0;
652                Write_Entity_Name (Ent);
653
654             when others =>
655                raise Program_Error;
656          end case;
657
658          Rewrite (N,
659            Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
660          Analyze_And_Resolve (N, Standard_String);
661       end if;
662
663       Set_Is_Static_Expression (N);
664    end Expand_Source_Info;
665
666    ---------------------------
667    -- Expand_Unc_Conversion --
668    ---------------------------
669
670    procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
671       Func : constant Entity_Id  := Entity (Name (N));
672       Conv : Node_Id;
673       Ftyp : Entity_Id;
674       Ttyp : Entity_Id;
675
676    begin
677       --  Rewrite as unchecked conversion node. Note that we must convert
678       --  the operand to the formal type of the input parameter of the
679       --  function, so that the resulting N_Unchecked_Type_Conversion
680       --  call indicates the correct types for Gigi.
681
682       --  Right now, we only do this if a scalar type is involved. It is
683       --  not clear if it is needed in other cases. If we do attempt to
684       --  do the conversion unconditionally, it crashes 3411-018. To be
685       --  investigated further ???
686
687       Conv := Relocate_Node (First_Actual (N));
688       Ftyp := Etype (First_Formal (Func));
689
690       if Is_Scalar_Type (Ftyp) then
691          Conv := Convert_To (Ftyp, Conv);
692          Set_Parent (Conv, N);
693          Analyze_And_Resolve (Conv);
694       end if;
695
696       --  The instantiation of Unchecked_Conversion creates a wrapper package,
697       --  and the target type is declared as a subtype of the actual. Recover
698       --  the actual, which is the subtype indic. in the subtype declaration
699       --  for the target type. This is semantically correct, and avoids
700       --  anomalies with access subtypes. For entities, leave type as is.
701
702       --  We do the analysis here, because we do not want the compiler
703       --  to try to optimize or otherwise reorganize the unchecked
704       --  conversion node.
705
706       Ttyp := Etype (E);
707
708       if Is_Entity_Name (Conv) then
709          null;
710
711       elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
712          Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
713
714       elsif Is_Itype (Ttyp) then
715          Ttyp :=
716            Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
717       else
718          raise Program_Error;
719       end if;
720
721       Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
722       Set_Etype (N, Ttyp);
723       Set_Analyzed (N);
724
725       if Nkind (N) = N_Unchecked_Type_Conversion then
726          Expand_N_Unchecked_Type_Conversion (N);
727       end if;
728    end Expand_Unc_Conversion;
729
730    -----------------------------
731    -- Expand_Unc_Deallocation --
732    -----------------------------
733
734    --  Generate the following Code :
735
736    --    if Arg /= null then
737    --     <Finalize_Call> (.., T'Class(Arg.all), ..);  -- for controlled types
738    --       Free (Arg);
739    --       Arg := Null;
740    --    end if;
741
742    --  For a task, we also generate a call to Free_Task to ensure that the
743    --  task itself is freed if it is terminated, ditto for a simple protected
744    --  object, with a call to Finalize_Protection. For composite types that
745    --  have tasks or simple protected objects as components, we traverse the
746    --  structures to find and terminate those components.
747
748    procedure Expand_Unc_Deallocation (N : Node_Id) is
749       Loc   : constant Source_Ptr := Sloc (N);
750       Arg   : constant Node_Id    := First_Actual (N);
751       Typ   : constant Entity_Id  := Etype (Arg);
752       Stmts : constant List_Id    := New_List;
753       Rtyp  : constant Entity_Id  := Underlying_Type (Root_Type (Typ));
754       Pool  : constant Entity_Id  := Associated_Storage_Pool (Rtyp);
755
756       Desig_T   : constant Entity_Id  := Designated_Type (Typ);
757       Gen_Code  : Node_Id;
758       Free_Node : Node_Id;
759       Deref     : Node_Id;
760       Free_Arg  : Node_Id;
761       Free_Cod  : List_Id;
762       Blk       : Node_Id;
763
764       Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
765       --  This captures whether we know the argument to be non-null so that
766       --  we can avoid the test. The reason that we need to capture this is
767       --  that we analyze some generated statements before properly attaching
768       --  them to the tree, and that can disturb current value settings.
769
770    begin
771       if No_Pool_Assigned (Rtyp) then
772          Error_Msg_N ("?deallocation from empty storage pool!", N);
773       end if;
774
775       --  Nothing to do if we know the argument is null
776
777       if Known_Null (N) then
778          return;
779       end if;
780
781       --  Processing for pointer to controlled type
782
783       if Controlled_Type (Desig_T) then
784          Deref :=
785            Make_Explicit_Dereference (Loc,
786              Prefix => Duplicate_Subexpr_No_Checks (Arg));
787
788          --  If the type is tagged, then we must force dispatching on the
789          --  finalization call because the designated type may not be the
790          --  actual type of the object.
791
792          if Is_Tagged_Type (Desig_T)
793            and then not Is_Class_Wide_Type (Desig_T)
794          then
795             Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
796
797          elsif not Is_Tagged_Type (Desig_T) then
798
799             --  Set type of result, to force a conversion when needed (see
800             --  exp_ch7, Convert_View), given that Deep_Finalize may be
801             --  inherited from the parent type, and we need the type of the
802             --  expression to see whether the conversion is in fact needed.
803
804             Set_Etype (Deref, Desig_T);
805          end if;
806
807          Free_Cod :=
808            Make_Final_Call
809             (Ref         => Deref,
810              Typ         => Desig_T,
811              With_Detach => New_Reference_To (Standard_True, Loc));
812
813          if Abort_Allowed then
814             Prepend_To (Free_Cod,
815               Build_Runtime_Call (Loc, RE_Abort_Defer));
816
817             Blk :=
818               Make_Block_Statement (Loc, Handled_Statement_Sequence =>
819                 Make_Handled_Sequence_Of_Statements (Loc,
820                   Statements  => Free_Cod,
821                   At_End_Proc =>
822                     New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
823
824             --  We now expand the exception (at end) handler. We set a
825             --  temporary parent pointer since we have not attached Blk
826             --  to the tree yet.
827
828             Set_Parent (Blk, N);
829             Analyze (Blk);
830             Expand_At_End_Handler
831               (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
832             Append (Blk, Stmts);
833
834             --  We kill saved current values, since analyzing statements not
835             --  properly attached to the tree can set wrong current values.
836
837             Kill_Current_Values;
838
839          else
840             Append_List_To (Stmts, Free_Cod);
841          end if;
842       end if;
843
844       --  For a task type, call Free_Task before freeing the ATCB
845
846       if Is_Task_Type (Desig_T) then
847          declare
848             Stat : Node_Id := Prev (N);
849             Nam1 : Node_Id;
850             Nam2 : Node_Id;
851
852          begin
853             --  An Abort followed by a Free will not do what the user
854             --  expects, because the abort is not immediate. This is
855             --  worth a friendly warning.
856
857             while Present (Stat)
858               and then not Comes_From_Source (Original_Node (Stat))
859             loop
860                Prev (Stat);
861             end loop;
862
863             if Present (Stat)
864               and then Nkind (Original_Node (Stat)) = N_Abort_Statement
865             then
866                Stat := Original_Node (Stat);
867                Nam1 := First (Names (Stat));
868                Nam2 := Original_Node (First (Parameter_Associations (N)));
869
870                if Nkind (Nam1) = N_Explicit_Dereference
871                  and then Is_Entity_Name (Prefix (Nam1))
872                  and then Is_Entity_Name (Nam2)
873                  and then Entity (Prefix (Nam1)) = Entity (Nam2)
874                then
875                   Error_Msg_N ("abort may take time to complete?", N);
876                   Error_Msg_N ("\deallocation might have no effect?", N);
877                   Error_Msg_N ("\safer to wait for termination.?", N);
878                end if;
879             end if;
880          end;
881
882          Append_To
883            (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
884
885       --  For composite types that contain tasks, recurse over the structure
886       --  to build the selectors for the task subcomponents.
887
888       elsif Has_Task (Desig_T) then
889          if Is_Record_Type (Desig_T) then
890             Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
891
892          elsif Is_Array_Type (Desig_T) then
893             Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
894          end if;
895       end if;
896
897       --  Same for simple protected types. Eventually call Finalize_Protection
898       --  before freeing the PO for each protected component.
899
900       if Is_Simple_Protected_Type (Desig_T) then
901          Append_To (Stmts,
902            Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
903
904       elsif Has_Simple_Protected_Object (Desig_T) then
905          if Is_Record_Type (Desig_T) then
906             Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
907          elsif Is_Array_Type (Desig_T) then
908             Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
909          end if;
910       end if;
911
912       --  Normal processing for non-controlled types
913
914       Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
915       Free_Node := Make_Free_Statement (Loc, Empty);
916       Append_To (Stmts, Free_Node);
917       Set_Storage_Pool (Free_Node, Pool);
918
919       --  Deal with storage pool
920
921       if Present (Pool) then
922
923          --  Freeing the secondary stack is meaningless
924
925          if Is_RTE (Pool, RE_SS_Pool) then
926             null;
927
928          elsif Is_Class_Wide_Type (Etype (Pool)) then
929
930             --  Case of a class-wide pool type: make a dispatching call
931             --  to Deallocate through the class-wide Deallocate_Any.
932
933             Set_Procedure_To_Call (Free_Node,
934               RTE (RE_Deallocate_Any));
935
936          else
937             --  Case of a specific pool type: make a statically bound call
938
939             Set_Procedure_To_Call (Free_Node,
940               Find_Prim_Op (Etype (Pool), Name_Deallocate));
941          end if;
942       end if;
943
944       if Present (Procedure_To_Call (Free_Node)) then
945
946          --  For all cases of a Deallocate call, the back-end needs to be
947          --  able to compute the size of the object being freed. This may
948          --  require some adjustments for objects of dynamic size.
949          --
950          --  If the type is class wide, we generate an implicit type with the
951          --  right dynamic size, so that the deallocate call gets the right
952          --  size parameter computed by GIGI. Same for an access to
953          --  unconstrained packed array.
954
955          if Is_Class_Wide_Type (Desig_T)
956            or else
957             (Is_Array_Type (Desig_T)
958                and then not Is_Constrained (Desig_T)
959                and then Is_Packed (Desig_T))
960          then
961             declare
962                Deref    : constant Node_Id :=
963                             Make_Explicit_Dereference (Loc,
964                               Duplicate_Subexpr_No_Checks (Arg));
965                D_Subtyp : Node_Id;
966                D_Type   : Entity_Id;
967
968             begin
969                Set_Etype  (Deref, Typ);
970                Set_Parent (Deref, Free_Node);
971                D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
972
973                if Nkind (D_Subtyp) in N_Has_Entity then
974                   D_Type := Entity (D_Subtyp);
975
976                else
977                   D_Type := Make_Defining_Identifier (Loc,
978                               New_Internal_Name ('A'));
979                   Insert_Action (N,
980                     Make_Subtype_Declaration (Loc,
981                       Defining_Identifier => D_Type,
982                       Subtype_Indication  => D_Subtyp));
983                   Freeze_Itype (D_Type, N);
984
985                end if;
986
987                Set_Actual_Designated_Subtype (Free_Node, D_Type);
988             end;
989
990          end if;
991       end if;
992
993       --  Ada 2005 (AI-251): In case of abstract interface type we must
994       --  displace the pointer to reference the base of the object to
995       --  deallocate its memory.
996
997       --  Generate:
998       --    free (Base_Address (Obj_Ptr))
999
1000       if Is_Interface (Directly_Designated_Type (Typ)) then
1001          Set_Expression (Free_Node,
1002            Unchecked_Convert_To (Typ,
1003              Make_Function_Call (Loc,
1004                Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1005                Parameter_Associations => New_List (
1006                  Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1007
1008       --  Generate:
1009       --    free (Obj_Ptr)
1010
1011       else
1012          Set_Expression (Free_Node, Free_Arg);
1013       end if;
1014
1015       --  Only remaining step is to set result to null, or generate a
1016       --  raise of constraint error if the target object is "not null".
1017
1018       if Can_Never_Be_Null (Etype (Arg)) then
1019          Append_To (Stmts,
1020            Make_Raise_Constraint_Error (Loc,
1021              Reason => CE_Access_Check_Failed));
1022
1023       else
1024          declare
1025             Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1026          begin
1027             Set_Assignment_OK (Lhs);
1028             Append_To (Stmts,
1029               Make_Assignment_Statement (Loc,
1030                 Name       => Lhs,
1031                 Expression => Make_Null (Loc)));
1032          end;
1033       end if;
1034
1035       --  If we know the argument is non-null, then make a block statement
1036       --  that contains the required statements, no need for a test.
1037
1038       if Arg_Known_Non_Null then
1039          Gen_Code :=
1040            Make_Block_Statement (Loc,
1041              Handled_Statement_Sequence =>
1042                Make_Handled_Sequence_Of_Statements (Loc,
1043              Statements => Stmts));
1044
1045       --  If the argument may be null, wrap the statements inside an IF that
1046       --  does an explicit test to exclude the null case.
1047
1048       else
1049          Gen_Code :=
1050            Make_Implicit_If_Statement (N,
1051              Condition =>
1052                Make_Op_Ne (Loc,
1053                  Left_Opnd  => Duplicate_Subexpr (Arg),
1054                  Right_Opnd => Make_Null (Loc)),
1055              Then_Statements => Stmts);
1056       end if;
1057
1058       --  Rewrite the call
1059
1060       Rewrite (N, Gen_Code);
1061       Analyze (N);
1062    end Expand_Unc_Deallocation;
1063
1064    -----------------------
1065    -- Expand_To_Address --
1066    -----------------------
1067
1068    procedure Expand_To_Address (N : Node_Id) is
1069       Loc : constant Source_Ptr := Sloc (N);
1070       Arg : constant Node_Id := First_Actual (N);
1071       Obj : Node_Id;
1072
1073    begin
1074       Remove_Side_Effects (Arg);
1075
1076       Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1077
1078       Rewrite (N,
1079         Make_Conditional_Expression (Loc,
1080           Expressions => New_List (
1081             Make_Op_Eq (Loc,
1082               Left_Opnd => New_Copy_Tree (Arg),
1083               Right_Opnd => Make_Null (Loc)),
1084             New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1085             Make_Attribute_Reference (Loc,
1086               Attribute_Name => Name_Address,
1087               Prefix => Obj))));
1088
1089       Analyze_And_Resolve (N, RTE (RE_Address));
1090    end Expand_To_Address;
1091
1092    -----------------------
1093    -- Expand_To_Pointer --
1094    -----------------------
1095
1096    procedure Expand_To_Pointer (N : Node_Id) is
1097       Arg : constant Node_Id := First_Actual (N);
1098
1099    begin
1100       Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1101       Analyze (N);
1102    end Expand_To_Pointer;
1103
1104 end Exp_Intr;