* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / ada / exp_intr.adb
blob5a402fdeaadb923b9a8dd0cba64d066d0168f2fb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ I N T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch4; use Exp_Ch4;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Code; use Exp_Code;
35 with Exp_Disp; use Exp_Disp;
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;
44 with Sem; use Sem;
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_Dispatching_Constructor_Call (N : Node_Id);
67 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
68 -- into a dispatching call to the actual subprogram associated with the
69 -- Constructor formal subprogram, passing it the Parameters actual of
70 -- the call to the instantiation and dispatching based on call's Tag
71 -- parameter.
73 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
74 -- Expand a call to Exception_Information/Message/Name. The first
75 -- parameter, N, is the node for the function call, and Ent is the
76 -- entity for the corresponding routine in the Ada.Exceptions package.
78 procedure Expand_Import_Call (N : Node_Id);
79 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
80 -- N is the node for the function call.
82 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
83 -- Expand an intrinsic shift operation, N and E are from the call to
84 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
85 -- K is the kind for the shift node
87 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
88 -- Expand a call to an instantiation of Unchecked_Convertion into a node
89 -- N_Unchecked_Type_Conversion.
91 procedure Expand_Unc_Deallocation (N : Node_Id);
92 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
93 -- N_Free_Statement and appropriate context.
95 procedure Expand_To_Address (N : Node_Id);
96 procedure Expand_To_Pointer (N : Node_Id);
97 -- Expand a call to corresponding function, declared in an instance of
98 -- System.Addess_To_Access_Conversions.
100 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
101 -- Rewrite the node by the appropriate string or positive constant.
102 -- Nam can be one of the following:
103 -- Name_File - expand string that is the name of source file
104 -- Name_Line - expand integer line number
105 -- Name_Source_Location - expand string of form file:line
106 -- Name_Enclosing_Entity - expand string with name of enclosing entity
108 -----------------------------------------
109 -- Expand_Dispatching_Constructor_Call --
110 -----------------------------------------
112 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
113 -- of the form:
115 -- GDC_Instance (The_Tag, Parameters'Access)
117 -- to a class-wide conversion of a dispatching call to the actual
118 -- associated with the formal subprogram Construct, designating
119 -- The_Tag as the controlling tag of the call:
121 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
123 -- which will eventually be expanded to the following:
125 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
127 -- A class-wide membership test is also generated, preceding the call,
128 -- to ensure that the controlling tag denotes a type in T'Class.
130 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
131 Loc : constant Source_Ptr := Sloc (N);
132 Tag_Arg : constant Node_Id := First_Actual (N);
133 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
134 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
135 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
136 Act_Rename : constant Node_Id :=
137 Next (Next (First (Visible_Declarations (Inst_Pkg))));
138 Act_Constr : constant Entity_Id := Entity (Name (Act_Rename));
139 Result_Typ : constant Entity_Id := Class_Wide_Type (Etype (Act_Constr));
140 Cnstr_Call : Node_Id;
142 begin
143 -- Create the call to the actual Constructor function
145 Cnstr_Call :=
146 Make_Function_Call (Loc,
147 Name => New_Occurrence_Of (Act_Constr, Loc),
148 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
150 -- Establish its controlling tag from the tag passed to the instance
152 Set_Controlling_Argument (Cnstr_Call, Relocate_Node (Tag_Arg));
154 -- Rewrite and analyze the call to the instance as a class-wide
155 -- conversion of the call to the actual constructor.
157 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
158 Analyze_And_Resolve (N, Etype (Act_Constr));
160 -- Generate a class-wide membership test to ensure that the call's tag
161 -- argument denotes a type within the class.
163 Insert_Action (N,
164 Make_Implicit_If_Statement (N,
165 Condition =>
166 Make_Op_Not (Loc,
167 Make_DT_Access_Action (Result_Typ,
168 Action => CW_Membership,
169 Args => New_List (
170 Duplicate_Subexpr (Tag_Arg),
171 New_Reference_To (
172 Node (First_Elmt (Access_Disp_Table (
173 Root_Type (Result_Typ)))), Loc)))),
174 Then_Statements =>
175 New_List (Make_Raise_Statement (Loc,
176 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
177 end Expand_Dispatching_Constructor_Call;
179 ---------------------------
180 -- Expand_Exception_Call --
181 ---------------------------
183 -- If the function call is not within an exception handler, then the
184 -- call is replaced by a null string. Otherwise the appropriate routine
185 -- in Ada.Exceptions is called passing the choice parameter specification
186 -- from the enclosing handler. If the enclosing handler lacks a choice
187 -- parameter, then one is supplied.
189 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
190 Loc : constant Source_Ptr := Sloc (N);
191 P : Node_Id;
192 E : Entity_Id;
194 begin
195 -- Climb up parents to see if we are in exception handler
197 P := Parent (N);
198 loop
199 -- Case of not in exception handler, replace by null string
201 if No (P) then
202 Rewrite (N,
203 Make_String_Literal (Loc,
204 Strval => ""));
205 exit;
207 -- Case of in exception handler
209 elsif Nkind (P) = N_Exception_Handler then
210 if No (Choice_Parameter (P)) then
212 -- If no choice parameter present, then put one there. Note
213 -- that we do not need to put it on the entity chain, since
214 -- no one will be referencing it by normal visibility methods.
216 E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
217 Set_Choice_Parameter (P, E);
218 Set_Ekind (E, E_Variable);
219 Set_Etype (E, RTE (RE_Exception_Occurrence));
220 Set_Scope (E, Current_Scope);
221 end if;
223 Rewrite (N,
224 Make_Function_Call (Loc,
225 Name => New_Occurrence_Of (RTE (Ent), Loc),
226 Parameter_Associations => New_List (
227 New_Occurrence_Of (Choice_Parameter (P), Loc))));
228 exit;
230 -- Keep climbing!
232 else
233 P := Parent (P);
234 end if;
235 end loop;
237 Analyze_And_Resolve (N, Standard_String);
238 end Expand_Exception_Call;
240 ------------------------
241 -- Expand_Import_Call --
242 ------------------------
244 -- The function call must have a static string as its argument. We create
245 -- a dummy variable which uses this string as the external name in an
246 -- Import pragma. The result is then obtained as the address of this
247 -- dummy variable, converted to the appropriate target type.
249 procedure Expand_Import_Call (N : Node_Id) is
250 Loc : constant Source_Ptr := Sloc (N);
251 Ent : constant Entity_Id := Entity (Name (N));
252 Str : constant Node_Id := First_Actual (N);
253 Dum : Entity_Id;
255 begin
256 Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
258 Insert_Actions (N, New_List (
259 Make_Object_Declaration (Loc,
260 Defining_Identifier => Dum,
261 Object_Definition =>
262 New_Occurrence_Of (Standard_Character, Loc)),
264 Make_Pragma (Loc,
265 Chars => Name_Import,
266 Pragma_Argument_Associations => New_List (
267 Make_Pragma_Argument_Association (Loc,
268 Expression => Make_Identifier (Loc, Name_Ada)),
270 Make_Pragma_Argument_Association (Loc,
271 Expression => Make_Identifier (Loc, Chars (Dum))),
273 Make_Pragma_Argument_Association (Loc,
274 Chars => Name_Link_Name,
275 Expression => Relocate_Node (Str))))));
277 Rewrite (N,
278 Unchecked_Convert_To (Etype (Ent),
279 Make_Attribute_Reference (Loc,
280 Attribute_Name => Name_Address,
281 Prefix => Make_Identifier (Loc, Chars (Dum)))));
283 Analyze_And_Resolve (N, Etype (Ent));
284 end Expand_Import_Call;
286 ---------------------------
287 -- Expand_Intrinsic_Call --
288 ---------------------------
290 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
291 Nam : Name_Id;
293 begin
294 -- If the intrinsic subprogram is generic, gets its original name
296 if Present (Parent (E))
297 and then Present (Generic_Parent (Parent (E)))
298 then
299 Nam := Chars (Generic_Parent (Parent (E)));
300 else
301 Nam := Chars (E);
302 end if;
304 if Nam = Name_Asm then
305 Expand_Asm_Call (N);
307 elsif Nam = Name_Divide then
308 Expand_Decimal_Divide_Call (N);
310 elsif Nam = Name_Exception_Information then
311 Expand_Exception_Call (N, RE_Exception_Information);
313 elsif Nam = Name_Exception_Message then
314 Expand_Exception_Call (N, RE_Exception_Message);
316 elsif Nam = Name_Exception_Name then
317 Expand_Exception_Call (N, RE_Exception_Name_Simple);
319 elsif Nam = Name_Generic_Dispatching_Constructor then
320 Expand_Dispatching_Constructor_Call (N);
322 elsif Nam = Name_Import_Address
323 or else
324 Nam = Name_Import_Largest_Value
325 or else
326 Nam = Name_Import_Value
327 then
328 Expand_Import_Call (N);
330 elsif Nam = Name_Is_Negative then
331 Expand_Is_Negative (N);
333 elsif Nam = Name_Rotate_Left then
334 Expand_Shift (N, E, N_Op_Rotate_Left);
336 elsif Nam = Name_Rotate_Right then
337 Expand_Shift (N, E, N_Op_Rotate_Right);
339 elsif Nam = Name_Shift_Left then
340 Expand_Shift (N, E, N_Op_Shift_Left);
342 elsif Nam = Name_Shift_Right then
343 Expand_Shift (N, E, N_Op_Shift_Right);
345 elsif Nam = Name_Shift_Right_Arithmetic then
346 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
348 elsif Nam = Name_Unchecked_Conversion then
349 Expand_Unc_Conversion (N, E);
351 elsif Nam = Name_Unchecked_Deallocation then
352 Expand_Unc_Deallocation (N);
354 elsif Nam = Name_To_Address then
355 Expand_To_Address (N);
357 elsif Nam = Name_To_Pointer then
358 Expand_To_Pointer (N);
360 elsif Nam = Name_File
361 or else Nam = Name_Line
362 or else Nam = Name_Source_Location
363 or else Nam = Name_Enclosing_Entity
364 then
365 Expand_Source_Info (N, Nam);
367 -- If we have a renaming, expand the call to the original operation,
368 -- which must itself be intrinsic, since renaming requires matching
369 -- conventions and this has already been checked.
371 elsif Present (Alias (E)) then
372 Expand_Intrinsic_Call (N, Alias (E));
374 -- The only other case is where an external name was specified,
375 -- since this is the only way that an otherwise unrecognized
376 -- name could escape the checking in Sem_Prag. Nothing needs
377 -- to be done in such a case, since we pass such a call to the
378 -- back end unchanged.
380 else
381 null;
382 end if;
383 end Expand_Intrinsic_Call;
385 ------------------------
386 -- Expand_Is_Negative --
387 ------------------------
389 procedure Expand_Is_Negative (N : Node_Id) is
390 Loc : constant Source_Ptr := Sloc (N);
391 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
393 begin
395 -- We replace the function call by the following expression
397 -- if Opnd < 0.0 then
398 -- True
399 -- else
400 -- if Opnd > 0.0 then
401 -- False;
402 -- else
403 -- Float_Unsigned!(Float (Opnd)) /= 0
404 -- end if;
405 -- end if;
407 Rewrite (N,
408 Make_Conditional_Expression (Loc,
409 Expressions => New_List (
410 Make_Op_Lt (Loc,
411 Left_Opnd => Duplicate_Subexpr (Opnd),
412 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
414 New_Occurrence_Of (Standard_True, Loc),
416 Make_Conditional_Expression (Loc,
417 Expressions => New_List (
418 Make_Op_Gt (Loc,
419 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
420 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
422 New_Occurrence_Of (Standard_False, Loc),
424 Make_Op_Ne (Loc,
425 Left_Opnd =>
426 Unchecked_Convert_To
427 (RTE (RE_Float_Unsigned),
428 Convert_To
429 (Standard_Float,
430 Duplicate_Subexpr_No_Checks (Opnd))),
431 Right_Opnd =>
432 Make_Integer_Literal (Loc, 0)))))));
434 Analyze_And_Resolve (N, Standard_Boolean);
435 end Expand_Is_Negative;
437 ------------------
438 -- Expand_Shift --
439 ------------------
441 -- This procedure is used to convert a call to a shift function to the
442 -- corresponding operator node. This conversion is not done by the usual
443 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
444 -- operator nodes, because shifts are not predefined operators.
446 -- As a result, whenever a shift is used in the source program, it will
447 -- remain as a call until converted by this routine to the operator node
448 -- form which Gigi is expecting to see.
450 -- Note: it is possible for the expander to generate shift operator nodes
451 -- directly, which will be analyzed in the normal manner by calling Analyze
452 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
454 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
455 Loc : constant Source_Ptr := Sloc (N);
456 Typ : constant Entity_Id := Etype (N);
457 Left : constant Node_Id := First_Actual (N);
458 Right : constant Node_Id := Next_Actual (Left);
459 Ltyp : constant Node_Id := Etype (Left);
460 Rtyp : constant Node_Id := Etype (Right);
461 Snode : Node_Id;
463 begin
464 Snode := New_Node (K, Loc);
465 Set_Left_Opnd (Snode, Relocate_Node (Left));
466 Set_Right_Opnd (Snode, Relocate_Node (Right));
467 Set_Chars (Snode, Chars (E));
468 Set_Etype (Snode, Base_Type (Typ));
469 Set_Entity (Snode, E);
471 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
472 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
473 then
474 Set_Shift_Count_OK (Snode, True);
475 end if;
477 -- Do the rewrite. Note that we don't call Analyze and Resolve on
478 -- this node, because it already got analyzed and resolved when
479 -- it was a function call!
481 Rewrite (N, Snode);
482 Set_Analyzed (N);
483 end Expand_Shift;
485 ------------------------
486 -- Expand_Source_Info --
487 ------------------------
489 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
490 Loc : constant Source_Ptr := Sloc (N);
491 Ent : Entity_Id;
493 procedure Write_Entity_Name (E : Entity_Id);
494 -- Recursive procedure to construct string for qualified name of
495 -- enclosing program unit. The qualification stops at an enclosing
496 -- scope has no source name (block or loop). If entity is a subprogram
497 -- instance, skip enclosing wrapper package.
499 -----------------------
500 -- Write_Entity_Name --
501 -----------------------
503 procedure Write_Entity_Name (E : Entity_Id) is
504 SDef : Source_Ptr;
505 TDef : constant Source_Buffer_Ptr :=
506 Source_Text (Get_Source_File_Index (Sloc (E)));
508 begin
509 -- Nothing to do if at outer level
511 if Scope (E) = Standard_Standard then
512 null;
514 -- If scope comes from source, write its name
516 elsif Comes_From_Source (Scope (E)) then
517 Write_Entity_Name (Scope (E));
518 Add_Char_To_Name_Buffer ('.');
520 -- If in wrapper package skip past it
522 elsif Is_Wrapper_Package (Scope (E)) then
523 Write_Entity_Name (Scope (Scope (E)));
524 Add_Char_To_Name_Buffer ('.');
526 -- Otherwise nothing to output (happens in unnamed block statements)
528 else
529 null;
530 end if;
532 -- Loop to output the name
534 -- is this right wrt wide char encodings ??? (no!)
536 SDef := Sloc (E);
537 while TDef (SDef) in '0' .. '9'
538 or else TDef (SDef) >= 'A'
539 or else TDef (SDef) = ASCII.ESC
540 loop
541 Add_Char_To_Name_Buffer (TDef (SDef));
542 SDef := SDef + 1;
543 end loop;
544 end Write_Entity_Name;
546 -- Start of processing for Expand_Source_Info
548 begin
549 -- Integer cases
551 if Nam = Name_Line then
552 Rewrite (N,
553 Make_Integer_Literal (Loc,
554 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
555 Analyze_And_Resolve (N, Standard_Positive);
557 -- String cases
559 else
560 case Nam is
561 when Name_File =>
562 Get_Decoded_Name_String
563 (Reference_Name (Get_Source_File_Index (Loc)));
565 when Name_Source_Location =>
566 Build_Location_String (Loc);
568 when Name_Enclosing_Entity =>
569 Name_Len := 0;
571 Ent := Current_Scope;
573 -- Skip enclosing blocks to reach enclosing unit
575 while Present (Ent) loop
576 exit when Ekind (Ent) /= E_Block
577 and then Ekind (Ent) /= E_Loop;
578 Ent := Scope (Ent);
579 end loop;
581 -- Ent now points to the relevant defining entity
583 Name_Len := 0;
584 Write_Entity_Name (Ent);
586 when others =>
587 raise Program_Error;
588 end case;
590 Rewrite (N,
591 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
592 Analyze_And_Resolve (N, Standard_String);
593 end if;
595 Set_Is_Static_Expression (N);
596 end Expand_Source_Info;
598 ---------------------------
599 -- Expand_Unc_Conversion --
600 ---------------------------
602 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
603 Func : constant Entity_Id := Entity (Name (N));
604 Conv : Node_Id;
605 Ftyp : Entity_Id;
606 Ttyp : Entity_Id;
608 begin
609 -- Rewrite as unchecked conversion node. Note that we must convert
610 -- the operand to the formal type of the input parameter of the
611 -- function, so that the resulting N_Unchecked_Type_Conversion
612 -- call indicates the correct types for Gigi.
614 -- Right now, we only do this if a scalar type is involved. It is
615 -- not clear if it is needed in other cases. If we do attempt to
616 -- do the conversion unconditionally, it crashes 3411-018. To be
617 -- investigated further ???
619 Conv := Relocate_Node (First_Actual (N));
620 Ftyp := Etype (First_Formal (Func));
622 if Is_Scalar_Type (Ftyp) then
623 Conv := Convert_To (Ftyp, Conv);
624 Set_Parent (Conv, N);
625 Analyze_And_Resolve (Conv);
626 end if;
628 -- The instantiation of Unchecked_Conversion creates a wrapper package,
629 -- and the target type is declared as a subtype of the actual. Recover
630 -- the actual, which is the subtype indic. in the subtype declaration
631 -- for the target type. This is semantically correct, and avoids
632 -- anomalies with access subtypes. For entities, leave type as is.
634 -- We do the analysis here, because we do not want the compiler
635 -- to try to optimize or otherwise reorganize the unchecked
636 -- conversion node.
638 Ttyp := Etype (E);
640 if Is_Entity_Name (Conv) then
641 null;
643 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
644 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
646 elsif Is_Itype (Ttyp) then
647 Ttyp :=
648 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
649 else
650 raise Program_Error;
651 end if;
653 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
654 Set_Etype (N, Ttyp);
655 Set_Analyzed (N);
657 if Nkind (N) = N_Unchecked_Type_Conversion then
658 Expand_N_Unchecked_Type_Conversion (N);
659 end if;
660 end Expand_Unc_Conversion;
662 -----------------------------
663 -- Expand_Unc_Deallocation --
664 -----------------------------
666 -- Generate the following Code :
668 -- if Arg /= null then
669 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
670 -- Free (Arg);
671 -- Arg := Null;
672 -- end if;
674 -- For a task, we also generate a call to Free_Task to ensure that the
675 -- task itself is freed if it is terminated, ditto for a simple protected
676 -- object, with a call to Finalize_Protection. For composite types that
677 -- have tasks or simple protected objects as components, we traverse the
678 -- structures to find and terminate those components.
680 procedure Expand_Unc_Deallocation (N : Node_Id) is
681 Loc : constant Source_Ptr := Sloc (N);
682 Arg : constant Node_Id := First_Actual (N);
683 Typ : constant Entity_Id := Etype (Arg);
684 Stmts : constant List_Id := New_List;
685 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
686 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
688 Desig_T : constant Entity_Id := Designated_Type (Typ);
689 Gen_Code : Node_Id;
690 Free_Node : Node_Id;
691 Deref : Node_Id;
692 Free_Arg : Node_Id;
693 Free_Cod : List_Id;
694 Blk : Node_Id;
696 begin
697 if No_Pool_Assigned (Rtyp) then
698 Error_Msg_N ("?deallocation from empty storage pool", N);
699 end if;
701 if Controlled_Type (Desig_T) then
702 Deref :=
703 Make_Explicit_Dereference (Loc,
704 Prefix => Duplicate_Subexpr_No_Checks (Arg));
706 -- If the type is tagged, then we must force dispatching on the
707 -- finalization call because the designated type may not be the
708 -- actual type of the object.
710 if Is_Tagged_Type (Desig_T)
711 and then not Is_Class_Wide_Type (Desig_T)
712 then
713 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
715 elsif not Is_Tagged_Type (Desig_T) then
717 -- Set type of result, to force a conversion when needed (see
718 -- exp_ch7, Convert_View), given that Deep_Finalize may be
719 -- inherited from the parent type, and we need the type of the
720 -- expression to see whether the conversion is in fact needed.
722 Set_Etype (Deref, Desig_T);
723 end if;
725 Free_Cod :=
726 Make_Final_Call
727 (Ref => Deref,
728 Typ => Desig_T,
729 With_Detach => New_Reference_To (Standard_True, Loc));
731 if Abort_Allowed then
732 Prepend_To (Free_Cod,
733 Build_Runtime_Call (Loc, RE_Abort_Defer));
735 Blk :=
736 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
737 Make_Handled_Sequence_Of_Statements (Loc,
738 Statements => Free_Cod,
739 At_End_Proc =>
740 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
742 -- We now expand the exception (at end) handler. We set a
743 -- temporary parent pointer since we have not attached Blk
744 -- to the tree yet.
746 Set_Parent (Blk, N);
747 Analyze (Blk);
748 Expand_At_End_Handler
749 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
750 Append (Blk, Stmts);
752 else
753 Append_List_To (Stmts, Free_Cod);
754 end if;
755 end if;
757 -- For a task type, call Free_Task before freeing the ATCB
759 if Is_Task_Type (Desig_T) then
760 declare
761 Stat : Node_Id := Prev (N);
762 Nam1 : Node_Id;
763 Nam2 : Node_Id;
765 begin
766 -- An Abort followed by a Free will not do what the user
767 -- expects, because the abort is not immediate. This is
768 -- worth a friendly warning.
770 while Present (Stat)
771 and then not Comes_From_Source (Original_Node (Stat))
772 loop
773 Prev (Stat);
774 end loop;
776 if Present (Stat)
777 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
778 then
779 Stat := Original_Node (Stat);
780 Nam1 := First (Names (Stat));
781 Nam2 := Original_Node (First (Parameter_Associations (N)));
783 if Nkind (Nam1) = N_Explicit_Dereference
784 and then Is_Entity_Name (Prefix (Nam1))
785 and then Is_Entity_Name (Nam2)
786 and then Entity (Prefix (Nam1)) = Entity (Nam2)
787 then
788 Error_Msg_N ("abort may take time to complete?", N);
789 Error_Msg_N ("\deallocation might have no effect?", N);
790 Error_Msg_N ("\safer to wait for termination.?", N);
791 end if;
792 end if;
793 end;
795 Append_To
796 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
798 -- For composite types that contain tasks, recurse over the structure
799 -- to build the selectors for the task subcomponents.
801 elsif Has_Task (Desig_T) then
802 if Is_Record_Type (Desig_T) then
803 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
805 elsif Is_Array_Type (Desig_T) then
806 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
807 end if;
808 end if;
810 -- Same for simple protected types. Eventually call Finalize_Protection
811 -- before freeing the PO for each protected component.
813 if Is_Simple_Protected_Type (Desig_T) then
814 Append_To (Stmts,
815 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
817 elsif Has_Simple_Protected_Object (Desig_T) then
818 if Is_Record_Type (Desig_T) then
819 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
820 elsif Is_Array_Type (Desig_T) then
821 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
822 end if;
823 end if;
825 -- Normal processing for non-controlled types
827 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
828 Free_Node := Make_Free_Statement (Loc, Empty);
829 Append_To (Stmts, Free_Node);
830 Set_Storage_Pool (Free_Node, Pool);
832 -- Make implicit if statement. We omit this if we are the then part
833 -- of a test of the form:
835 -- if not (Arg = null) then
837 -- i.e. if the test is explicit in the source. Arg must be a simple
838 -- identifier for the purposes of this special test. Note that the
839 -- use of /= in the source is always transformed into the above form.
841 declare
842 Test_Needed : Boolean := True;
843 P : constant Node_Id := Parent (N);
844 C : Node_Id;
846 begin
847 if Nkind (Arg) = N_Identifier
848 and then Nkind (P) = N_If_Statement
849 and then First (Then_Statements (P)) = N
850 then
851 if Nkind (Condition (P)) = N_Op_Not then
852 C := Right_Opnd (Condition (P));
854 if Nkind (C) = N_Op_Eq
855 and then Nkind (Left_Opnd (C)) = N_Identifier
856 and then Chars (Arg) = Chars (Left_Opnd (C))
857 and then Nkind (Right_Opnd (C)) = N_Null
858 then
859 Test_Needed := False;
860 end if;
861 end if;
862 end if;
864 -- Generate If_Statement if needed
866 if Test_Needed then
867 Gen_Code :=
868 Make_Implicit_If_Statement (N,
869 Condition =>
870 Make_Op_Ne (Loc,
871 Left_Opnd => Duplicate_Subexpr (Arg),
872 Right_Opnd => Make_Null (Loc)),
873 Then_Statements => Stmts);
875 else
876 Gen_Code :=
877 Make_Block_Statement (Loc,
878 Handled_Statement_Sequence =>
879 Make_Handled_Sequence_Of_Statements (Loc,
880 Statements => Stmts));
881 end if;
882 end;
884 -- Deal with storage pool
886 if Present (Pool) then
888 -- Freeing the secondary stack is meaningless
890 if Is_RTE (Pool, RE_SS_Pool) then
891 null;
893 elsif Is_Class_Wide_Type (Etype (Pool)) then
894 Set_Procedure_To_Call (Free_Node,
895 RTE (RE_Deallocate_Any));
896 else
897 Set_Procedure_To_Call (Free_Node,
898 Find_Prim_Op (Etype (Pool), Name_Deallocate));
900 -- If the type is class wide, we generate an implicit type
901 -- with the right dynamic size, so that the deallocate call
902 -- gets the right size parameter computed by gigi
904 if Is_Class_Wide_Type (Desig_T) then
905 declare
906 Acc_Type : constant Entity_Id :=
907 Create_Itype (E_Access_Type, N);
908 Deref : constant Node_Id :=
909 Make_Explicit_Dereference (Loc,
910 Duplicate_Subexpr_No_Checks (Arg));
912 begin
913 Set_Etype (Deref, Typ);
914 Set_Parent (Deref, Free_Node);
916 Set_Etype (Acc_Type, Acc_Type);
917 Set_Size_Info (Acc_Type, Typ);
918 Set_Directly_Designated_Type
919 (Acc_Type, Entity (Make_Subtype_From_Expr
920 (Deref, Desig_T)));
922 Free_Arg := Unchecked_Convert_To (Acc_Type, Free_Arg);
923 end;
924 end if;
925 end if;
926 end if;
928 Set_Expression (Free_Node, Free_Arg);
930 declare
931 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
933 begin
934 Set_Assignment_OK (Lhs);
935 Append_To (Stmts,
936 Make_Assignment_Statement (Loc,
937 Name => Lhs,
938 Expression => Make_Null (Loc)));
939 end;
941 Rewrite (N, Gen_Code);
942 Analyze (N);
943 end Expand_Unc_Deallocation;
945 -----------------------
946 -- Expand_To_Address --
947 -----------------------
949 procedure Expand_To_Address (N : Node_Id) is
950 Loc : constant Source_Ptr := Sloc (N);
951 Arg : constant Node_Id := First_Actual (N);
952 Obj : Node_Id;
954 begin
955 Remove_Side_Effects (Arg);
957 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
959 Rewrite (N,
960 Make_Conditional_Expression (Loc,
961 Expressions => New_List (
962 Make_Op_Eq (Loc,
963 Left_Opnd => New_Copy_Tree (Arg),
964 Right_Opnd => Make_Null (Loc)),
965 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
966 Make_Attribute_Reference (Loc,
967 Attribute_Name => Name_Address,
968 Prefix => Obj))));
970 Analyze_And_Resolve (N, RTE (RE_Address));
971 end Expand_To_Address;
973 -----------------------
974 -- Expand_To_Pointer --
975 -----------------------
977 procedure Expand_To_Pointer (N : Node_Id) is
978 Arg : constant Node_Id := First_Actual (N);
980 begin
981 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
982 Analyze (N);
983 end Expand_To_Pointer;
985 end Exp_Intr;