1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree; use Atree;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Ch4; use Exp_Ch4;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Ch9; use Exp_Ch9;
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 Itypes; use Itypes;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Restrict; use Restrict;
43 with Rtsfind; use Rtsfind;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sinfo; use Sinfo;
49 with Sinput; use Sinput;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Stringt; use Stringt;
53 with Tbuild; use Tbuild;
54 with Uintp; use Uintp;
55 with Urealp; use Urealp;
57 package body Exp_Intr is
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Expand_Is_Negative (N : Node_Id);
64 -- Expand a call to the intrinsic Is_Negative function
66 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
67 -- Expand a call to Exception_Information/Message/Name. The first
68 -- parameter, N, is the node for the function call, and Ent is the
69 -- entity for the corresponding routine in the Ada.Exceptions package.
71 procedure Expand_Import_Call (N : Node_Id);
72 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
73 -- N is the node for the function call.
75 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
76 -- Expand an intrinsic shift operation, N and E are from the call to
77 -- Expand_Instrinsic_Call (call node and subprogram spec entity) and
78 -- K is the kind for the shift node
80 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
81 -- Expand a call to an instantiation of Unchecked_Convertion into a node
82 -- N_Unchecked_Type_Conversion.
84 procedure Expand_Unc_Deallocation (N : Node_Id);
85 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
86 -- N_Free_Statement and appropriate context.
88 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
89 -- Rewrite the node by the appropriate string or positive constant.
90 -- Nam can be one of the following:
91 -- Name_File - expand string that is the name of source file
92 -- Name_Line - expand integer line number
93 -- Name_Source_Location - expand string of form file:line
94 -- Name_Enclosing_Entity - expand string with name of enclosing entity
96 ---------------------------
97 -- Expand_Exception_Call --
98 ---------------------------
100 -- If the function call is not within an exception handler, then the
101 -- call is replaced by a null string. Otherwise the appropriate routine
102 -- in Ada.Exceptions is called passing the choice parameter specification
103 -- from the enclosing handler. If the enclosing handler lacks a choice
104 -- parameter, then one is supplied.
106 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
107 Loc : constant Source_Ptr := Sloc (N);
113 -- Climb up parents to see if we are in exception handler
117 -- Case of not in exception handler
123 Make_String_Literal (Loc,
127 -- Case of in exception handler
129 elsif Nkind (P) = N_Exception_Handler then
130 if No (Choice_Parameter (P)) then
132 -- If no choice parameter present, then put one there. Note
133 -- that we do not need to put it on the entity chain, since
134 -- no one will be referencing it by normal visibility methods.
136 E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
137 Set_Choice_Parameter (P, E);
138 Set_Ekind (E, E_Variable);
139 Set_Etype (E, RTE (RE_Exception_Occurrence));
140 Set_Scope (E, Current_Scope);
144 Make_Function_Call (Loc,
145 Name => New_Occurrence_Of (RTE (Ent), Loc),
146 Parameter_Associations => New_List (
147 New_Occurrence_Of (Choice_Parameter (P), Loc))));
157 Analyze_And_Resolve (N, Standard_String);
158 end Expand_Exception_Call;
160 ------------------------
161 -- Expand_Import_Call --
162 ------------------------
164 -- The function call must have a static string as its argument. We create
165 -- a dummy variable which uses this string as the external name in an
166 -- Import pragma. The result is then obtained as the address of this
167 -- dummy variable, converted to the appropriate target type.
169 procedure Expand_Import_Call (N : Node_Id) is
170 Loc : constant Source_Ptr := Sloc (N);
171 Ent : constant Entity_Id := Entity (Name (N));
172 Str : constant Node_Id := First_Actual (N);
176 Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
178 Insert_Actions (N, New_List (
179 Make_Object_Declaration (Loc,
180 Defining_Identifier => Dum,
182 New_Occurrence_Of (Standard_Character, Loc)),
185 Chars => Name_Import,
186 Pragma_Argument_Associations => New_List (
187 Make_Pragma_Argument_Association (Loc,
188 Expression => Make_Identifier (Loc, Name_Ada)),
190 Make_Pragma_Argument_Association (Loc,
191 Expression => Make_Identifier (Loc, Chars (Dum))),
193 Make_Pragma_Argument_Association (Loc,
194 Chars => Name_Link_Name,
195 Expression => Relocate_Node (Str))))));
198 Unchecked_Convert_To (Etype (Ent),
199 Make_Attribute_Reference (Loc,
200 Attribute_Name => Name_Address,
201 Prefix => Make_Identifier (Loc, Chars (Dum)))));
203 Analyze_And_Resolve (N, Etype (Ent));
204 end Expand_Import_Call;
206 ---------------------------
207 -- Expand_Intrinsic_Call --
208 ---------------------------
210 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
214 -- If the intrinsic subprogram is generic, gets its original name.
216 if Present (Parent (E))
217 and then Present (Generic_Parent (Parent (E)))
219 Nam := Chars (Generic_Parent (Parent (E)));
224 if Nam = Name_Asm then
227 elsif Nam = Name_Divide then
228 Expand_Decimal_Divide_Call (N);
230 elsif Nam = Name_Exception_Information then
231 Expand_Exception_Call (N, RE_Exception_Information);
233 elsif Nam = Name_Exception_Message then
234 Expand_Exception_Call (N, RE_Exception_Message);
236 elsif Nam = Name_Exception_Name then
237 Expand_Exception_Call (N, RE_Exception_Name_Simple);
239 elsif Nam = Name_Import_Address
241 Nam = Name_Import_Largest_Value
243 Nam = Name_Import_Value
245 Expand_Import_Call (N);
247 elsif Nam = Name_Is_Negative then
248 Expand_Is_Negative (N);
250 elsif Nam = Name_Rotate_Left then
251 Expand_Shift (N, E, N_Op_Rotate_Left);
253 elsif Nam = Name_Rotate_Right then
254 Expand_Shift (N, E, N_Op_Rotate_Right);
256 elsif Nam = Name_Shift_Left then
257 Expand_Shift (N, E, N_Op_Shift_Left);
259 elsif Nam = Name_Shift_Right then
260 Expand_Shift (N, E, N_Op_Shift_Right);
262 elsif Nam = Name_Shift_Right_Arithmetic then
263 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
265 elsif Nam = Name_Unchecked_Conversion then
266 Expand_Unc_Conversion (N, E);
268 elsif Nam = Name_Unchecked_Deallocation then
269 Expand_Unc_Deallocation (N);
271 elsif Nam = Name_File
272 or else Nam = Name_Line
273 or else Nam = Name_Source_Location
274 or else Nam = Name_Enclosing_Entity
276 Expand_Source_Info (N, Nam);
279 -- Only other possibility is a renaming, in which case we expand
280 -- the call to the original operation (which must be intrinsic).
282 pragma Assert (Present (Alias (E)));
283 Expand_Intrinsic_Call (N, Alias (E));
286 end Expand_Intrinsic_Call;
288 ------------------------
289 -- Expand_Is_Negative --
290 ------------------------
292 procedure Expand_Is_Negative (N : Node_Id) is
293 Loc : constant Source_Ptr := Sloc (N);
294 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
298 -- We replace the function call by the following expression
300 -- if Opnd < 0.0 then
303 -- if Opnd > 0.0 then
306 -- Float_Unsigned!(Float (Opnd)) /= 0
311 Make_Conditional_Expression (Loc,
312 Expressions => New_List (
314 Left_Opnd => Duplicate_Subexpr (Opnd),
315 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
317 New_Occurrence_Of (Standard_True, Loc),
319 Make_Conditional_Expression (Loc,
320 Expressions => New_List (
322 Left_Opnd => Duplicate_Subexpr (Opnd),
323 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
325 New_Occurrence_Of (Standard_False, Loc),
329 Unchecked_Convert_To (RTE (RE_Float_Unsigned),
330 Convert_To (Standard_Float,
331 Duplicate_Subexpr (Opnd))),
333 Make_Integer_Literal (Loc, 0)))))));
335 Analyze_And_Resolve (N, Standard_Boolean);
336 end Expand_Is_Negative;
342 -- This procedure is used to convert a call to a shift function to the
343 -- corresponding operator node. This conversion is not done by the usual
344 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
345 -- operator nodes, because shifts are not predefined operators.
347 -- As a result, whenever a shift is used in the source program, it will
348 -- remain as a call until converted by this routine to the operator node
349 -- form which Gigi is expecting to see.
351 -- Note: it is possible for the expander to generate shift operator nodes
352 -- directly, which will be analyzed in the normal manner by calling Analyze
353 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
355 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
356 Loc : constant Source_Ptr := Sloc (N);
357 Typ : constant Entity_Id := Etype (N);
358 Left : constant Node_Id := First_Actual (N);
359 Right : constant Node_Id := Next_Actual (Left);
360 Ltyp : constant Node_Id := Etype (Left);
361 Rtyp : constant Node_Id := Etype (Right);
365 Snode := New_Node (K, Loc);
366 Set_Left_Opnd (Snode, Relocate_Node (Left));
367 Set_Right_Opnd (Snode, Relocate_Node (Right));
368 Set_Chars (Snode, Chars (E));
369 Set_Etype (Snode, Base_Type (Typ));
370 Set_Entity (Snode, E);
372 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
373 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
375 Set_Shift_Count_OK (Snode, True);
378 -- Do the rewrite. Note that we don't call Analyze and Resolve on
379 -- this node, because it already got analyzed and resolved when
380 -- it was a function call!
387 ------------------------
388 -- Expand_Source_Info --
389 ------------------------
391 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
392 Loc : constant Source_Ptr := Sloc (N);
398 if Nam = Name_Line then
400 Make_Integer_Literal (Loc,
401 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
402 Analyze_And_Resolve (N, Standard_Positive);
409 Get_Decoded_Name_String
410 (Reference_Name (Get_Source_File_Index (Loc)));
412 when Name_Source_Location =>
413 Build_Location_String (Loc);
415 when Name_Enclosing_Entity =>
418 Ent := Current_Scope;
420 -- Skip enclosing blocks to reach enclosing unit.
422 while Present (Ent) loop
423 exit when Ekind (Ent) /= E_Block
424 and then Ekind (Ent) /= E_Loop;
428 -- Ent now points to the relevant defining entity
431 SDef : Source_Ptr := Sloc (Ent);
432 TDef : Source_Buffer_Ptr;
435 TDef := Source_Text (Get_Source_File_Index (SDef));
438 while TDef (SDef) in '0' .. '9'
439 or else TDef (SDef) >= 'A'
440 or else TDef (SDef) = ASCII.ESC
442 Add_Char_To_Name_Buffer (TDef (SDef));
452 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
453 Analyze_And_Resolve (N, Standard_String);
456 Set_Is_Static_Expression (N);
457 end Expand_Source_Info;
459 ---------------------------
460 -- Expand_Unc_Conversion --
461 ---------------------------
463 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
464 Func : constant Entity_Id := Entity (Name (N));
469 -- Rewrite as unchecked conversion node. Note that we must convert
470 -- the operand to the formal type of the input parameter of the
471 -- function, so that the resulting N_Unchecked_Type_Conversion
472 -- call indicates the correct types for Gigi.
474 -- Right now, we only do this if a scalar type is involved. It is
475 -- not clear if it is needed in other cases. If we do attempt to
476 -- do the conversion unconditionally, it crashes 3411-018. To be
477 -- investigated further ???
479 Conv := Relocate_Node (First_Actual (N));
480 Ftyp := Etype (First_Formal (Func));
482 if Is_Scalar_Type (Ftyp) then
483 Conv := Convert_To (Ftyp, Conv);
484 Set_Parent (Conv, N);
485 Analyze_And_Resolve (Conv);
488 -- We do the analysis here, because we do not want the compiler
489 -- to try to optimize or otherwise reorganize the unchecked
492 Rewrite (N, Unchecked_Convert_To (Etype (E), Conv));
493 Set_Etype (N, Etype (E));
496 if Nkind (N) = N_Unchecked_Type_Conversion then
497 Expand_N_Unchecked_Type_Conversion (N);
499 end Expand_Unc_Conversion;
501 -----------------------------
502 -- Expand_Unc_Deallocation --
503 -----------------------------
505 -- Generate the following Code :
507 -- if Arg /= null then
508 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
513 -- For a task, we also generate a call to Free_Task to ensure that the
514 -- task itself is freed if it is terminated, ditto for a simple protected
515 -- object, with a call to Finalize_Protection
517 procedure Expand_Unc_Deallocation (N : Node_Id) is
518 Loc : constant Source_Ptr := Sloc (N);
519 Arg : constant Node_Id := First_Actual (N);
520 Typ : constant Entity_Id := Etype (Arg);
521 Stmts : constant List_Id := New_List;
522 Pool : constant Entity_Id :=
523 Associated_Storage_Pool (Underlying_Type (Root_Type (Typ)));
525 Desig_T : Entity_Id := Designated_Type (Typ);
534 if Controlled_Type (Desig_T) then
536 Deref := Make_Explicit_Dereference (Loc, Duplicate_Subexpr (Arg));
538 -- If the type is tagged, then we must force dispatching on the
539 -- finalization call because the designated type may not be the
540 -- actual type of the object
542 if Is_Tagged_Type (Desig_T)
543 and then not Is_Class_Wide_Type (Desig_T)
545 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
552 With_Detach => New_Reference_To (Standard_True, Loc));
554 if Abort_Allowed then
555 Prepend_To (Free_Cod,
556 Build_Runtime_Call (Loc, RE_Abort_Defer));
559 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
560 Make_Handled_Sequence_Of_Statements (Loc,
561 Statements => Free_Cod,
563 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
565 -- We now expand the exception (at end) handler. We set a
566 -- temporary parent pointer since we have not attached Blk
571 Expand_At_End_Handler
572 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
576 Append_List_To (Stmts, Free_Cod);
580 -- For a task type, call Free_Task before freeing the ATCB.
582 if Is_Task_Type (Desig_T) then
585 Stat : Node_Id := Prev (N);
590 -- An Abort followed by a Free will not do what the user
591 -- expects, because the abort is not immediate. This is worth
592 -- a friendly warning.
595 and then not Comes_From_Source (Original_Node (Stat))
601 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
603 Stat := Original_Node (Stat);
604 Nam1 := First (Names (Stat));
605 Nam2 := Original_Node (First (Parameter_Associations (N)));
607 if Nkind (Nam1) = N_Explicit_Dereference
608 and then Is_Entity_Name (Prefix (Nam1))
609 and then Is_Entity_Name (Nam2)
610 and then Entity (Prefix (Nam1)) = Entity (Nam2)
612 Error_Msg_N ("Abort may take time to complete?", N);
613 Error_Msg_N ("\deallocation might have no effect?", N);
614 Error_Msg_N ("\safer to wait for termination.?", N);
620 Make_Procedure_Call_Statement (Loc,
621 Name => New_Reference_To (RTE (RE_Free_Task), Loc),
622 Parameter_Associations => New_List (
623 Concurrent_Ref (Duplicate_Subexpr (Arg)))));
626 -- For a protected type with no entries, call Finalize_Protection
627 -- before freeing the PO.
629 if Is_Protected_Type (Desig_T) and then not Has_Entries (Desig_T) then
631 Make_Procedure_Call_Statement (Loc,
632 Name => New_Reference_To (RTE (RE_Finalize_Protection), Loc),
633 Parameter_Associations => New_List (
634 Concurrent_Ref (Duplicate_Subexpr (Arg)))));
637 -- Normal processing for non-controlled types
639 Free_Arg := Duplicate_Subexpr (Arg);
640 Free_Node := Make_Free_Statement (Loc, Empty);
641 Append_To (Stmts, Free_Node);
642 Set_Storage_Pool (Free_Node, Pool);
644 -- Make implicit if statement. We omit this if we are the then part
645 -- of a test of the form:
647 -- if not (Arg = null) then
649 -- i.e. if the test is explicit in the source. Arg must be a simple
650 -- identifier for the purposes of this special test. Note that the
651 -- use of /= in the source is always transformed into the above form.
654 Test_Needed : Boolean := True;
655 P : constant Node_Id := Parent (N);
659 if Nkind (Arg) = N_Identifier
660 and then Nkind (P) = N_If_Statement
661 and then First (Then_Statements (P)) = N
663 if Nkind (Condition (P)) = N_Op_Not then
664 C := Right_Opnd (Condition (P));
666 if Nkind (C) = N_Op_Eq
667 and then Nkind (Left_Opnd (C)) = N_Identifier
668 and then Chars (Arg) = Chars (Left_Opnd (C))
669 and then Nkind (Right_Opnd (C)) = N_Null
671 Test_Needed := False;
676 -- Generate If_Statement if needed
680 Make_Implicit_If_Statement (N,
683 Left_Opnd => Duplicate_Subexpr (Arg),
684 Right_Opnd => Make_Null (Loc)),
685 Then_Statements => Stmts);
689 Make_Block_Statement (Loc,
690 Handled_Statement_Sequence =>
691 Make_Handled_Sequence_Of_Statements (Loc,
692 Statements => Stmts));
696 -- Deal with storage pool
698 if Present (Pool) then
700 -- Freeing the secondary stack is meaningless
702 if Is_RTE (Pool, RE_SS_Pool) then
706 Set_Procedure_To_Call (Free_Node,
707 Find_Prim_Op (Etype (Pool), Name_Deallocate));
709 -- If the type is class wide, we generate an implicit type
710 -- with the right dynamic size, so that the deallocate call
711 -- gets the right size parameter computed by gigi
713 if Is_Class_Wide_Type (Desig_T) then
715 Acc_Type : constant Entity_Id :=
716 Create_Itype (E_Access_Type, N);
717 Deref : constant Node_Id :=
718 Make_Explicit_Dereference (Loc,
719 Duplicate_Subexpr (Arg));
722 Set_Etype (Deref, Typ);
723 Set_Parent (Deref, Free_Node);
725 Set_Etype (Acc_Type, Acc_Type);
726 Set_Size_Info (Acc_Type, Typ);
727 Set_Directly_Designated_Type
728 (Acc_Type, Entity (Make_Subtype_From_Expr
731 Free_Arg := Unchecked_Convert_To (Acc_Type, Free_Arg);
737 Set_Expression (Free_Node, Free_Arg);
740 Lhs : Node_Id := Duplicate_Subexpr (Arg);
743 Set_Assignment_OK (Lhs);
745 Make_Assignment_Statement (Loc,
747 Expression => Make_Null (Loc)));
750 Rewrite (N, Gen_Code);
752 end Expand_Unc_Deallocation;