re PR ada/20300 (ICE on undefined value of type derived from Character)
[official-gcc.git] / gcc / ada / exp_intr.adb
blobb46b8f09c7d218b32374ba3360f54dd37b96e485
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 begin
494 -- Integer cases
496 if Nam = Name_Line then
497 Rewrite (N,
498 Make_Integer_Literal (Loc,
499 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
500 Analyze_And_Resolve (N, Standard_Positive);
502 -- String cases
504 else
505 case Nam is
506 when Name_File =>
507 Get_Decoded_Name_String
508 (Reference_Name (Get_Source_File_Index (Loc)));
510 when Name_Source_Location =>
511 Build_Location_String (Loc);
513 when Name_Enclosing_Entity =>
514 Name_Len := 0;
516 Ent := Current_Scope;
518 -- Skip enclosing blocks to reach enclosing unit.
520 while Present (Ent) loop
521 exit when Ekind (Ent) /= E_Block
522 and then Ekind (Ent) /= E_Loop;
523 Ent := Scope (Ent);
524 end loop;
526 -- Ent now points to the relevant defining entity
528 declare
529 SDef : Source_Ptr := Sloc (Ent);
530 TDef : Source_Buffer_Ptr;
532 begin
533 TDef := Source_Text (Get_Source_File_Index (SDef));
534 Name_Len := 0;
536 while TDef (SDef) in '0' .. '9'
537 or else TDef (SDef) >= 'A'
538 or else TDef (SDef) = ASCII.ESC
539 loop
540 Add_Char_To_Name_Buffer (TDef (SDef));
541 SDef := SDef + 1;
542 end loop;
543 end;
545 when others =>
546 raise Program_Error;
547 end case;
549 Rewrite (N,
550 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
551 Analyze_And_Resolve (N, Standard_String);
552 end if;
554 Set_Is_Static_Expression (N);
555 end Expand_Source_Info;
557 ---------------------------
558 -- Expand_Unc_Conversion --
559 ---------------------------
561 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
562 Func : constant Entity_Id := Entity (Name (N));
563 Conv : Node_Id;
564 Ftyp : Entity_Id;
565 Ttyp : Entity_Id;
567 begin
568 -- Rewrite as unchecked conversion node. Note that we must convert
569 -- the operand to the formal type of the input parameter of the
570 -- function, so that the resulting N_Unchecked_Type_Conversion
571 -- call indicates the correct types for Gigi.
573 -- Right now, we only do this if a scalar type is involved. It is
574 -- not clear if it is needed in other cases. If we do attempt to
575 -- do the conversion unconditionally, it crashes 3411-018. To be
576 -- investigated further ???
578 Conv := Relocate_Node (First_Actual (N));
579 Ftyp := Etype (First_Formal (Func));
581 if Is_Scalar_Type (Ftyp) then
582 Conv := Convert_To (Ftyp, Conv);
583 Set_Parent (Conv, N);
584 Analyze_And_Resolve (Conv);
585 end if;
587 -- The instantiation of Unchecked_Conversion creates a wrapper package,
588 -- and the target type is declared as a subtype of the actual. Recover
589 -- the actual, which is the subtype indic. in the subtype declaration
590 -- for the target type. This is semantically correct, and avoids
591 -- anomalies with access subtypes. For entities, leave type as is.
593 -- We do the analysis here, because we do not want the compiler
594 -- to try to optimize or otherwise reorganize the unchecked
595 -- conversion node.
597 Ttyp := Etype (E);
599 if Is_Entity_Name (Conv) then
600 null;
602 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
603 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
605 elsif Is_Itype (Ttyp) then
606 Ttyp :=
607 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
608 else
609 raise Program_Error;
610 end if;
612 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
613 Set_Etype (N, Ttyp);
614 Set_Analyzed (N);
616 if Nkind (N) = N_Unchecked_Type_Conversion then
617 Expand_N_Unchecked_Type_Conversion (N);
618 end if;
619 end Expand_Unc_Conversion;
621 -----------------------------
622 -- Expand_Unc_Deallocation --
623 -----------------------------
625 -- Generate the following Code :
627 -- if Arg /= null then
628 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
629 -- Free (Arg);
630 -- Arg := Null;
631 -- end if;
633 -- For a task, we also generate a call to Free_Task to ensure that the
634 -- task itself is freed if it is terminated, ditto for a simple protected
635 -- object, with a call to Finalize_Protection. For composite types that
636 -- have tasks or simple protected objects as components, we traverse the
637 -- structures to find and terminate those components.
639 procedure Expand_Unc_Deallocation (N : Node_Id) is
640 Loc : constant Source_Ptr := Sloc (N);
641 Arg : constant Node_Id := First_Actual (N);
642 Typ : constant Entity_Id := Etype (Arg);
643 Stmts : constant List_Id := New_List;
644 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
645 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
647 Desig_T : constant Entity_Id := Designated_Type (Typ);
648 Gen_Code : Node_Id;
649 Free_Node : Node_Id;
650 Deref : Node_Id;
651 Free_Arg : Node_Id;
652 Free_Cod : List_Id;
653 Blk : Node_Id;
655 begin
656 if No_Pool_Assigned (Rtyp) then
657 Error_Msg_N ("?deallocation from empty storage pool", N);
658 end if;
660 if Controlled_Type (Desig_T) then
661 Deref :=
662 Make_Explicit_Dereference (Loc,
663 Prefix => Duplicate_Subexpr_No_Checks (Arg));
665 -- If the type is tagged, then we must force dispatching on the
666 -- finalization call because the designated type may not be the
667 -- actual type of the object
669 if Is_Tagged_Type (Desig_T)
670 and then not Is_Class_Wide_Type (Desig_T)
671 then
672 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
673 end if;
675 Free_Cod :=
676 Make_Final_Call
677 (Ref => Deref,
678 Typ => Desig_T,
679 With_Detach => New_Reference_To (Standard_True, Loc));
681 if Abort_Allowed then
682 Prepend_To (Free_Cod,
683 Build_Runtime_Call (Loc, RE_Abort_Defer));
685 Blk :=
686 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
687 Make_Handled_Sequence_Of_Statements (Loc,
688 Statements => Free_Cod,
689 At_End_Proc =>
690 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
692 -- We now expand the exception (at end) handler. We set a
693 -- temporary parent pointer since we have not attached Blk
694 -- to the tree yet.
696 Set_Parent (Blk, N);
697 Analyze (Blk);
698 Expand_At_End_Handler
699 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
700 Append (Blk, Stmts);
702 else
703 Append_List_To (Stmts, Free_Cod);
704 end if;
705 end if;
707 -- For a task type, call Free_Task before freeing the ATCB
709 if Is_Task_Type (Desig_T) then
710 declare
711 Stat : Node_Id := Prev (N);
712 Nam1 : Node_Id;
713 Nam2 : Node_Id;
715 begin
716 -- An Abort followed by a Free will not do what the user
717 -- expects, because the abort is not immediate. This is
718 -- worth a friendly warning.
720 while Present (Stat)
721 and then not Comes_From_Source (Original_Node (Stat))
722 loop
723 Prev (Stat);
724 end loop;
726 if Present (Stat)
727 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
728 then
729 Stat := Original_Node (Stat);
730 Nam1 := First (Names (Stat));
731 Nam2 := Original_Node (First (Parameter_Associations (N)));
733 if Nkind (Nam1) = N_Explicit_Dereference
734 and then Is_Entity_Name (Prefix (Nam1))
735 and then Is_Entity_Name (Nam2)
736 and then Entity (Prefix (Nam1)) = Entity (Nam2)
737 then
738 Error_Msg_N ("Abort may take time to complete?", N);
739 Error_Msg_N ("\deallocation might have no effect?", N);
740 Error_Msg_N ("\safer to wait for termination.?", N);
741 end if;
742 end if;
743 end;
745 Append_To
746 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
748 -- For composite types that contain tasks, recurse over the structure
749 -- to build the selectors for the task subcomponents.
751 elsif Has_Task (Desig_T) then
752 if Is_Record_Type (Desig_T) then
753 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
755 elsif Is_Array_Type (Desig_T) then
756 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
757 end if;
758 end if;
760 -- Same for simple protected types. Eventually call Finalize_Protection
761 -- before freeing the PO for each protected component.
763 if Is_Simple_Protected_Type (Desig_T) then
764 Append_To (Stmts,
765 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
767 elsif Has_Simple_Protected_Object (Desig_T) then
768 if Is_Record_Type (Desig_T) then
769 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
770 elsif Is_Array_Type (Desig_T) then
771 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
772 end if;
773 end if;
775 -- Normal processing for non-controlled types
777 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
778 Free_Node := Make_Free_Statement (Loc, Empty);
779 Append_To (Stmts, Free_Node);
780 Set_Storage_Pool (Free_Node, Pool);
782 -- Make implicit if statement. We omit this if we are the then part
783 -- of a test of the form:
785 -- if not (Arg = null) then
787 -- i.e. if the test is explicit in the source. Arg must be a simple
788 -- identifier for the purposes of this special test. Note that the
789 -- use of /= in the source is always transformed into the above form.
791 declare
792 Test_Needed : Boolean := True;
793 P : constant Node_Id := Parent (N);
794 C : Node_Id;
796 begin
797 if Nkind (Arg) = N_Identifier
798 and then Nkind (P) = N_If_Statement
799 and then First (Then_Statements (P)) = N
800 then
801 if Nkind (Condition (P)) = N_Op_Not then
802 C := Right_Opnd (Condition (P));
804 if Nkind (C) = N_Op_Eq
805 and then Nkind (Left_Opnd (C)) = N_Identifier
806 and then Chars (Arg) = Chars (Left_Opnd (C))
807 and then Nkind (Right_Opnd (C)) = N_Null
808 then
809 Test_Needed := False;
810 end if;
811 end if;
812 end if;
814 -- Generate If_Statement if needed
816 if Test_Needed then
817 Gen_Code :=
818 Make_Implicit_If_Statement (N,
819 Condition =>
820 Make_Op_Ne (Loc,
821 Left_Opnd => Duplicate_Subexpr (Arg),
822 Right_Opnd => Make_Null (Loc)),
823 Then_Statements => Stmts);
825 else
826 Gen_Code :=
827 Make_Block_Statement (Loc,
828 Handled_Statement_Sequence =>
829 Make_Handled_Sequence_Of_Statements (Loc,
830 Statements => Stmts));
831 end if;
832 end;
834 -- Deal with storage pool
836 if Present (Pool) then
838 -- Freeing the secondary stack is meaningless
840 if Is_RTE (Pool, RE_SS_Pool) then
841 null;
843 elsif Is_Class_Wide_Type (Etype (Pool)) then
844 Set_Procedure_To_Call (Free_Node,
845 RTE (RE_Deallocate_Any));
846 else
847 Set_Procedure_To_Call (Free_Node,
848 Find_Prim_Op (Etype (Pool), Name_Deallocate));
850 -- If the type is class wide, we generate an implicit type
851 -- with the right dynamic size, so that the deallocate call
852 -- gets the right size parameter computed by gigi
854 if Is_Class_Wide_Type (Desig_T) then
855 declare
856 Acc_Type : constant Entity_Id :=
857 Create_Itype (E_Access_Type, N);
858 Deref : constant Node_Id :=
859 Make_Explicit_Dereference (Loc,
860 Duplicate_Subexpr_No_Checks (Arg));
862 begin
863 Set_Etype (Deref, Typ);
864 Set_Parent (Deref, Free_Node);
866 Set_Etype (Acc_Type, Acc_Type);
867 Set_Size_Info (Acc_Type, Typ);
868 Set_Directly_Designated_Type
869 (Acc_Type, Entity (Make_Subtype_From_Expr
870 (Deref, Desig_T)));
872 Free_Arg := Unchecked_Convert_To (Acc_Type, Free_Arg);
873 end;
874 end if;
875 end if;
876 end if;
878 Set_Expression (Free_Node, Free_Arg);
880 declare
881 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
883 begin
884 Set_Assignment_OK (Lhs);
885 Append_To (Stmts,
886 Make_Assignment_Statement (Loc,
887 Name => Lhs,
888 Expression => Make_Null (Loc)));
889 end;
891 Rewrite (N, Gen_Code);
892 Analyze (N);
893 end Expand_Unc_Deallocation;
895 -----------------------
896 -- Expand_To_Address --
897 -----------------------
899 procedure Expand_To_Address (N : Node_Id) is
900 Loc : constant Source_Ptr := Sloc (N);
901 Arg : constant Node_Id := First_Actual (N);
902 Obj : Node_Id;
904 begin
905 Remove_Side_Effects (Arg);
907 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
909 Rewrite (N,
910 Make_Conditional_Expression (Loc,
911 Expressions => New_List (
912 Make_Op_Eq (Loc,
913 Left_Opnd => New_Copy_Tree (Arg),
914 Right_Opnd => Make_Null (Loc)),
915 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
916 Make_Attribute_Reference (Loc,
917 Attribute_Name => Name_Address,
918 Prefix => Obj))));
920 Analyze_And_Resolve (N, RTE (RE_Address));
921 end Expand_To_Address;
923 -----------------------
924 -- Expand_To_Pointer --
925 -----------------------
927 procedure Expand_To_Pointer (N : Node_Id) is
928 Arg : constant Node_Id := First_Actual (N);
930 begin
931 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
932 Analyze (N);
933 end Expand_To_Pointer;
935 end Exp_Intr;