OSDN Git Service

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