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