* jump.c: Remove prototypes for delete_computation and
[official-gcc.git] / gcc / ada / exp_intr.adb
blob9bb4d729de2062dfad996a3883ac6ae789a79665
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-2006, 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 Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
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_Disp; use Exp_Disp;
37 with Exp_Fixd; use Exp_Fixd;
38 with Exp_Util; use Exp_Util;
39 with Freeze; use Freeze;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
43 with Restrict; use Restrict;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res; use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Sinfo; use Sinfo;
50 with Sinput; use Sinput;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Stringt; use Stringt;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
56 with Urealp; use Urealp;
58 package body Exp_Intr is
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 procedure Expand_Is_Negative (N : Node_Id);
65 -- Expand a call to the intrinsic Is_Negative function
67 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
68 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
69 -- into a dispatching call to the actual subprogram associated with the
70 -- Constructor formal subprogram, passing it the Parameters actual of
71 -- the call to the instantiation and dispatching based on call's Tag
72 -- parameter.
74 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
75 -- Expand a call to Exception_Information/Message/Name. The first
76 -- parameter, N, is the node for the function call, and Ent is the
77 -- entity for the corresponding routine in the Ada.Exceptions package.
79 procedure Expand_Import_Call (N : Node_Id);
80 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
81 -- N is the node for the function call.
83 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
84 -- Expand an intrinsic shift operation, N and E are from the call to
85 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
86 -- K is the kind for the shift node
88 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
89 -- Expand a call to an instantiation of Unchecked_Convertion into a node
90 -- N_Unchecked_Type_Conversion.
92 procedure Expand_Unc_Deallocation (N : Node_Id);
93 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
94 -- N_Free_Statement and appropriate context.
96 procedure Expand_To_Address (N : Node_Id);
97 procedure Expand_To_Pointer (N : Node_Id);
98 -- Expand a call to corresponding function, declared in an instance of
99 -- System.Addess_To_Access_Conversions.
101 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
102 -- Rewrite the node by the appropriate string or positive constant.
103 -- Nam can be one of the following:
104 -- Name_File - expand string that is the name of source file
105 -- Name_Line - expand integer line number
106 -- Name_Source_Location - expand string of form file:line
107 -- Name_Enclosing_Entity - expand string with name of enclosing entity
109 -----------------------------------------
110 -- Expand_Dispatching_Constructor_Call --
111 -----------------------------------------
113 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
114 -- of the form:
116 -- GDC_Instance (The_Tag, Parameters'Access)
118 -- to a class-wide conversion of a dispatching call to the actual
119 -- associated with the formal subprogram Construct, designating The_Tag
120 -- as the controlling tag of the call:
122 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
124 -- which will eventually be expanded to the following:
126 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
128 -- A class-wide membership test is also generated, preceding the call, to
129 -- ensure that the controlling tag denotes a type in T'Class.
131 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
132 Loc : constant Source_Ptr := Sloc (N);
133 Tag_Arg : constant Node_Id := First_Actual (N);
134 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
135 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
136 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
137 Act_Rename : Node_Id;
138 Act_Constr : Entity_Id;
139 Result_Typ : Entity_Id;
140 Cnstr_Call : Node_Id;
142 begin
143 -- The subprogram is the third actual in the instantiation, and is
144 -- retrieved from the corresponding renaming declaration. However,
145 -- freeze nodes may appear before, so we retrieve the declaration
146 -- with an explicit loop.
148 Act_Rename := First (Visible_Declarations (Inst_Pkg));
149 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
150 Next (Act_Rename);
151 end loop;
153 Act_Constr := Entity (Name (Act_Rename));
154 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
156 -- Create the call to the actual Constructor function
158 Cnstr_Call :=
159 Make_Function_Call (Loc,
160 Name => New_Occurrence_Of (Act_Constr, Loc),
161 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
163 -- Establish its controlling tag from the tag passed to the instance
165 Set_Controlling_Argument (Cnstr_Call, Relocate_Node (Tag_Arg));
167 -- Rewrite and analyze the call to the instance as a class-wide
168 -- conversion of the call to the actual constructor.
170 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
171 Analyze_And_Resolve (N, Etype (Act_Constr));
173 -- Do not generate a run-time check on the built object if tag
174 -- checks is suppressed for the result type.
176 if Tag_Checks_Suppressed (Etype (Result_Typ)) then
177 null;
179 -- Generate a class-wide membership test to ensure that the call's tag
180 -- argument denotes a type within the class. We must keep separate the
181 -- case in which the Result_Type of the constructor function is a tagged
182 -- type from the case in which it is an abstract interface because the
183 -- run-time subprogram required to check these cases differ (and have
184 -- one difference in their parameters profile).
186 -- Call CW_Membership if the Result_Type is a tagged type to look for
187 -- the tag in the table of ancestor tags.
189 elsif not Is_Interface (Result_Typ) then
190 Insert_Action (N,
191 Make_Implicit_If_Statement (N,
192 Condition =>
193 Make_Op_Not (Loc,
194 Make_DT_Access_Action (Result_Typ,
195 Action => CW_Membership,
196 Args => New_List (
197 Duplicate_Subexpr (Tag_Arg),
198 New_Reference_To (
199 Node (First_Elmt (Access_Disp_Table (
200 Root_Type (Result_Typ)))), Loc)))),
201 Then_Statements =>
202 New_List (Make_Raise_Statement (Loc,
203 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
205 -- Call IW_Membership test if the Result_Type is an abstract interface
206 -- to look for the tag in the table of interface tags.
208 else
209 Insert_Action (N,
210 Make_Implicit_If_Statement (N,
211 Condition =>
212 Make_Op_Not (Loc,
213 Make_DT_Access_Action (Result_Typ,
214 Action => IW_Membership,
215 Args => New_List (
216 Make_Attribute_Reference (Loc,
217 Prefix => Duplicate_Subexpr (Tag_Arg),
218 Attribute_Name => Name_Address),
220 New_Reference_To (
221 Node (First_Elmt (Access_Disp_Table (
222 Root_Type (Result_Typ)))), Loc)))),
223 Then_Statements =>
224 New_List (
225 Make_Raise_Statement (Loc,
226 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
227 end if;
228 end Expand_Dispatching_Constructor_Call;
230 ---------------------------
231 -- Expand_Exception_Call --
232 ---------------------------
234 -- If the function call is not within an exception handler, then the
235 -- call is replaced by a null string. Otherwise the appropriate routine
236 -- in Ada.Exceptions is called passing the choice parameter specification
237 -- from the enclosing handler. If the enclosing handler lacks a choice
238 -- parameter, then one is supplied.
240 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
241 Loc : constant Source_Ptr := Sloc (N);
242 P : Node_Id;
243 E : Entity_Id;
245 begin
246 -- Climb up parents to see if we are in exception handler
248 P := Parent (N);
249 loop
250 -- Case of not in exception handler, replace by null string
252 if No (P) then
253 Rewrite (N,
254 Make_String_Literal (Loc,
255 Strval => ""));
256 exit;
258 -- Case of in exception handler
260 elsif Nkind (P) = N_Exception_Handler then
261 if No (Choice_Parameter (P)) then
263 -- If no choice parameter present, then put one there. Note
264 -- that we do not need to put it on the entity chain, since
265 -- no one will be referencing it by normal visibility methods.
267 E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
268 Set_Choice_Parameter (P, E);
269 Set_Ekind (E, E_Variable);
270 Set_Etype (E, RTE (RE_Exception_Occurrence));
271 Set_Scope (E, Current_Scope);
272 end if;
274 Rewrite (N,
275 Make_Function_Call (Loc,
276 Name => New_Occurrence_Of (RTE (Ent), Loc),
277 Parameter_Associations => New_List (
278 New_Occurrence_Of (Choice_Parameter (P), Loc))));
279 exit;
281 -- Keep climbing!
283 else
284 P := Parent (P);
285 end if;
286 end loop;
288 Analyze_And_Resolve (N, Standard_String);
289 end Expand_Exception_Call;
291 ------------------------
292 -- Expand_Import_Call --
293 ------------------------
295 -- The function call must have a static string as its argument. We create
296 -- a dummy variable which uses this string as the external name in an
297 -- Import pragma. The result is then obtained as the address of this
298 -- dummy variable, converted to the appropriate target type.
300 procedure Expand_Import_Call (N : Node_Id) is
301 Loc : constant Source_Ptr := Sloc (N);
302 Ent : constant Entity_Id := Entity (Name (N));
303 Str : constant Node_Id := First_Actual (N);
304 Dum : Entity_Id;
306 begin
307 Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
309 Insert_Actions (N, New_List (
310 Make_Object_Declaration (Loc,
311 Defining_Identifier => Dum,
312 Object_Definition =>
313 New_Occurrence_Of (Standard_Character, Loc)),
315 Make_Pragma (Loc,
316 Chars => Name_Import,
317 Pragma_Argument_Associations => New_List (
318 Make_Pragma_Argument_Association (Loc,
319 Expression => Make_Identifier (Loc, Name_Ada)),
321 Make_Pragma_Argument_Association (Loc,
322 Expression => Make_Identifier (Loc, Chars (Dum))),
324 Make_Pragma_Argument_Association (Loc,
325 Chars => Name_Link_Name,
326 Expression => Relocate_Node (Str))))));
328 Rewrite (N,
329 Unchecked_Convert_To (Etype (Ent),
330 Make_Attribute_Reference (Loc,
331 Attribute_Name => Name_Address,
332 Prefix => Make_Identifier (Loc, Chars (Dum)))));
334 Analyze_And_Resolve (N, Etype (Ent));
335 end Expand_Import_Call;
337 ---------------------------
338 -- Expand_Intrinsic_Call --
339 ---------------------------
341 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
342 Nam : Name_Id;
344 begin
345 -- If the intrinsic subprogram is generic, gets its original name
347 if Present (Parent (E))
348 and then Present (Generic_Parent (Parent (E)))
349 then
350 Nam := Chars (Generic_Parent (Parent (E)));
351 else
352 Nam := Chars (E);
353 end if;
355 if Nam = Name_Asm then
356 Expand_Asm_Call (N);
358 elsif Nam = Name_Divide then
359 Expand_Decimal_Divide_Call (N);
361 elsif Nam = Name_Exception_Information then
362 Expand_Exception_Call (N, RE_Exception_Information);
364 elsif Nam = Name_Exception_Message then
365 Expand_Exception_Call (N, RE_Exception_Message);
367 elsif Nam = Name_Exception_Name then
368 Expand_Exception_Call (N, RE_Exception_Name_Simple);
370 elsif Nam = Name_Generic_Dispatching_Constructor then
371 Expand_Dispatching_Constructor_Call (N);
373 elsif Nam = Name_Import_Address
374 or else
375 Nam = Name_Import_Largest_Value
376 or else
377 Nam = Name_Import_Value
378 then
379 Expand_Import_Call (N);
381 elsif Nam = Name_Is_Negative then
382 Expand_Is_Negative (N);
384 elsif Nam = Name_Rotate_Left then
385 Expand_Shift (N, E, N_Op_Rotate_Left);
387 elsif Nam = Name_Rotate_Right then
388 Expand_Shift (N, E, N_Op_Rotate_Right);
390 elsif Nam = Name_Shift_Left then
391 Expand_Shift (N, E, N_Op_Shift_Left);
393 elsif Nam = Name_Shift_Right then
394 Expand_Shift (N, E, N_Op_Shift_Right);
396 elsif Nam = Name_Shift_Right_Arithmetic then
397 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
399 elsif Nam = Name_Unchecked_Conversion then
400 Expand_Unc_Conversion (N, E);
402 elsif Nam = Name_Unchecked_Deallocation then
403 Expand_Unc_Deallocation (N);
405 elsif Nam = Name_To_Address then
406 Expand_To_Address (N);
408 elsif Nam = Name_To_Pointer then
409 Expand_To_Pointer (N);
411 elsif Nam = Name_File
412 or else Nam = Name_Line
413 or else Nam = Name_Source_Location
414 or else Nam = Name_Enclosing_Entity
415 then
416 Expand_Source_Info (N, Nam);
418 -- If we have a renaming, expand the call to the original operation,
419 -- which must itself be intrinsic, since renaming requires matching
420 -- conventions and this has already been checked.
422 elsif Present (Alias (E)) then
423 Expand_Intrinsic_Call (N, Alias (E));
425 -- The only other case is where an external name was specified,
426 -- since this is the only way that an otherwise unrecognized
427 -- name could escape the checking in Sem_Prag. Nothing needs
428 -- to be done in such a case, since we pass such a call to the
429 -- back end unchanged.
431 else
432 null;
433 end if;
434 end Expand_Intrinsic_Call;
436 ------------------------
437 -- Expand_Is_Negative --
438 ------------------------
440 procedure Expand_Is_Negative (N : Node_Id) is
441 Loc : constant Source_Ptr := Sloc (N);
442 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
444 begin
446 -- We replace the function call by the following expression
448 -- if Opnd < 0.0 then
449 -- True
450 -- else
451 -- if Opnd > 0.0 then
452 -- False;
453 -- else
454 -- Float_Unsigned!(Float (Opnd)) /= 0
455 -- end if;
456 -- end if;
458 Rewrite (N,
459 Make_Conditional_Expression (Loc,
460 Expressions => New_List (
461 Make_Op_Lt (Loc,
462 Left_Opnd => Duplicate_Subexpr (Opnd),
463 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
465 New_Occurrence_Of (Standard_True, Loc),
467 Make_Conditional_Expression (Loc,
468 Expressions => New_List (
469 Make_Op_Gt (Loc,
470 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
471 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
473 New_Occurrence_Of (Standard_False, Loc),
475 Make_Op_Ne (Loc,
476 Left_Opnd =>
477 Unchecked_Convert_To
478 (RTE (RE_Float_Unsigned),
479 Convert_To
480 (Standard_Float,
481 Duplicate_Subexpr_No_Checks (Opnd))),
482 Right_Opnd =>
483 Make_Integer_Literal (Loc, 0)))))));
485 Analyze_And_Resolve (N, Standard_Boolean);
486 end Expand_Is_Negative;
488 ------------------
489 -- Expand_Shift --
490 ------------------
492 -- This procedure is used to convert a call to a shift function to the
493 -- corresponding operator node. This conversion is not done by the usual
494 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
495 -- operator nodes, because shifts are not predefined operators.
497 -- As a result, whenever a shift is used in the source program, it will
498 -- remain as a call until converted by this routine to the operator node
499 -- form which Gigi is expecting to see.
501 -- Note: it is possible for the expander to generate shift operator nodes
502 -- directly, which will be analyzed in the normal manner by calling Analyze
503 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
505 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
506 Loc : constant Source_Ptr := Sloc (N);
507 Typ : constant Entity_Id := Etype (N);
508 Left : constant Node_Id := First_Actual (N);
509 Right : constant Node_Id := Next_Actual (Left);
510 Ltyp : constant Node_Id := Etype (Left);
511 Rtyp : constant Node_Id := Etype (Right);
512 Snode : Node_Id;
514 begin
515 Snode := New_Node (K, Loc);
516 Set_Left_Opnd (Snode, Relocate_Node (Left));
517 Set_Right_Opnd (Snode, Relocate_Node (Right));
518 Set_Chars (Snode, Chars (E));
519 Set_Etype (Snode, Base_Type (Typ));
520 Set_Entity (Snode, E);
522 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
523 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
524 then
525 Set_Shift_Count_OK (Snode, True);
526 end if;
528 -- Do the rewrite. Note that we don't call Analyze and Resolve on
529 -- this node, because it already got analyzed and resolved when
530 -- it was a function call!
532 Rewrite (N, Snode);
533 Set_Analyzed (N);
534 end Expand_Shift;
536 ------------------------
537 -- Expand_Source_Info --
538 ------------------------
540 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
541 Loc : constant Source_Ptr := Sloc (N);
542 Ent : Entity_Id;
544 procedure Write_Entity_Name (E : Entity_Id);
545 -- Recursive procedure to construct string for qualified name of
546 -- enclosing program unit. The qualification stops at an enclosing
547 -- scope has no source name (block or loop). If entity is a subprogram
548 -- instance, skip enclosing wrapper package.
550 -----------------------
551 -- Write_Entity_Name --
552 -----------------------
554 procedure Write_Entity_Name (E : Entity_Id) is
555 SDef : Source_Ptr;
556 TDef : constant Source_Buffer_Ptr :=
557 Source_Text (Get_Source_File_Index (Sloc (E)));
559 begin
560 -- Nothing to do if at outer level
562 if Scope (E) = Standard_Standard then
563 null;
565 -- If scope comes from source, write its name
567 elsif Comes_From_Source (Scope (E)) then
568 Write_Entity_Name (Scope (E));
569 Add_Char_To_Name_Buffer ('.');
571 -- If in wrapper package skip past it
573 elsif Is_Wrapper_Package (Scope (E)) then
574 Write_Entity_Name (Scope (Scope (E)));
575 Add_Char_To_Name_Buffer ('.');
577 -- Otherwise nothing to output (happens in unnamed block statements)
579 else
580 null;
581 end if;
583 -- Loop to output the name
585 -- is this right wrt wide char encodings ??? (no!)
587 SDef := Sloc (E);
588 while TDef (SDef) in '0' .. '9'
589 or else TDef (SDef) >= 'A'
590 or else TDef (SDef) = ASCII.ESC
591 loop
592 Add_Char_To_Name_Buffer (TDef (SDef));
593 SDef := SDef + 1;
594 end loop;
595 end Write_Entity_Name;
597 -- Start of processing for Expand_Source_Info
599 begin
600 -- Integer cases
602 if Nam = Name_Line then
603 Rewrite (N,
604 Make_Integer_Literal (Loc,
605 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
606 Analyze_And_Resolve (N, Standard_Positive);
608 -- String cases
610 else
611 case Nam is
612 when Name_File =>
613 Get_Decoded_Name_String
614 (Reference_Name (Get_Source_File_Index (Loc)));
616 when Name_Source_Location =>
617 Build_Location_String (Loc);
619 when Name_Enclosing_Entity =>
620 Name_Len := 0;
622 Ent := Current_Scope;
624 -- Skip enclosing blocks to reach enclosing unit
626 while Present (Ent) loop
627 exit when Ekind (Ent) /= E_Block
628 and then Ekind (Ent) /= E_Loop;
629 Ent := Scope (Ent);
630 end loop;
632 -- Ent now points to the relevant defining entity
634 Name_Len := 0;
635 Write_Entity_Name (Ent);
637 when others =>
638 raise Program_Error;
639 end case;
641 Rewrite (N,
642 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
643 Analyze_And_Resolve (N, Standard_String);
644 end if;
646 Set_Is_Static_Expression (N);
647 end Expand_Source_Info;
649 ---------------------------
650 -- Expand_Unc_Conversion --
651 ---------------------------
653 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
654 Func : constant Entity_Id := Entity (Name (N));
655 Conv : Node_Id;
656 Ftyp : Entity_Id;
657 Ttyp : Entity_Id;
659 begin
660 -- Rewrite as unchecked conversion node. Note that we must convert
661 -- the operand to the formal type of the input parameter of the
662 -- function, so that the resulting N_Unchecked_Type_Conversion
663 -- call indicates the correct types for Gigi.
665 -- Right now, we only do this if a scalar type is involved. It is
666 -- not clear if it is needed in other cases. If we do attempt to
667 -- do the conversion unconditionally, it crashes 3411-018. To be
668 -- investigated further ???
670 Conv := Relocate_Node (First_Actual (N));
671 Ftyp := Etype (First_Formal (Func));
673 if Is_Scalar_Type (Ftyp) then
674 Conv := Convert_To (Ftyp, Conv);
675 Set_Parent (Conv, N);
676 Analyze_And_Resolve (Conv);
677 end if;
679 -- The instantiation of Unchecked_Conversion creates a wrapper package,
680 -- and the target type is declared as a subtype of the actual. Recover
681 -- the actual, which is the subtype indic. in the subtype declaration
682 -- for the target type. This is semantically correct, and avoids
683 -- anomalies with access subtypes. For entities, leave type as is.
685 -- We do the analysis here, because we do not want the compiler
686 -- to try to optimize or otherwise reorganize the unchecked
687 -- conversion node.
689 Ttyp := Etype (E);
691 if Is_Entity_Name (Conv) then
692 null;
694 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
695 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
697 elsif Is_Itype (Ttyp) then
698 Ttyp :=
699 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
700 else
701 raise Program_Error;
702 end if;
704 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
705 Set_Etype (N, Ttyp);
706 Set_Analyzed (N);
708 if Nkind (N) = N_Unchecked_Type_Conversion then
709 Expand_N_Unchecked_Type_Conversion (N);
710 end if;
711 end Expand_Unc_Conversion;
713 -----------------------------
714 -- Expand_Unc_Deallocation --
715 -----------------------------
717 -- Generate the following Code :
719 -- if Arg /= null then
720 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
721 -- Free (Arg);
722 -- Arg := Null;
723 -- end if;
725 -- For a task, we also generate a call to Free_Task to ensure that the
726 -- task itself is freed if it is terminated, ditto for a simple protected
727 -- object, with a call to Finalize_Protection. For composite types that
728 -- have tasks or simple protected objects as components, we traverse the
729 -- structures to find and terminate those components.
731 procedure Expand_Unc_Deallocation (N : Node_Id) is
732 Loc : constant Source_Ptr := Sloc (N);
733 Arg : constant Node_Id := First_Actual (N);
734 Typ : constant Entity_Id := Etype (Arg);
735 Stmts : constant List_Id := New_List;
736 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
737 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
739 Desig_T : constant Entity_Id := Designated_Type (Typ);
740 Gen_Code : Node_Id;
741 Free_Node : Node_Id;
742 Deref : Node_Id;
743 Free_Arg : Node_Id;
744 Free_Cod : List_Id;
745 Blk : Node_Id;
747 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
748 -- This captures whether we know the argument to be non-null so that
749 -- we can avoid the test. The reason that we need to capture this is
750 -- that we analyze some generated statements before properly attaching
751 -- them to the tree, and that can disturb current value settings.
753 begin
754 if No_Pool_Assigned (Rtyp) then
755 Error_Msg_N ("?deallocation from empty storage pool", N);
756 end if;
758 -- Nothing to do if we know the argument is null
760 if Known_Null (N) then
761 return;
762 end if;
764 -- Processing for pointer to controlled type
766 if Controlled_Type (Desig_T) then
767 Deref :=
768 Make_Explicit_Dereference (Loc,
769 Prefix => Duplicate_Subexpr_No_Checks (Arg));
771 -- If the type is tagged, then we must force dispatching on the
772 -- finalization call because the designated type may not be the
773 -- actual type of the object.
775 if Is_Tagged_Type (Desig_T)
776 and then not Is_Class_Wide_Type (Desig_T)
777 then
778 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
780 elsif not Is_Tagged_Type (Desig_T) then
782 -- Set type of result, to force a conversion when needed (see
783 -- exp_ch7, Convert_View), given that Deep_Finalize may be
784 -- inherited from the parent type, and we need the type of the
785 -- expression to see whether the conversion is in fact needed.
787 Set_Etype (Deref, Desig_T);
788 end if;
790 Free_Cod :=
791 Make_Final_Call
792 (Ref => Deref,
793 Typ => Desig_T,
794 With_Detach => New_Reference_To (Standard_True, Loc));
796 if Abort_Allowed then
797 Prepend_To (Free_Cod,
798 Build_Runtime_Call (Loc, RE_Abort_Defer));
800 Blk :=
801 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
802 Make_Handled_Sequence_Of_Statements (Loc,
803 Statements => Free_Cod,
804 At_End_Proc =>
805 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
807 -- We now expand the exception (at end) handler. We set a
808 -- temporary parent pointer since we have not attached Blk
809 -- to the tree yet.
811 Set_Parent (Blk, N);
812 Analyze (Blk);
813 Expand_At_End_Handler
814 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
815 Append (Blk, Stmts);
817 -- We kill saved current values, since analyzing statements not
818 -- properly attached to the tree can set wrong current values.
820 Kill_Current_Values;
822 else
823 Append_List_To (Stmts, Free_Cod);
824 end if;
825 end if;
827 -- For a task type, call Free_Task before freeing the ATCB
829 if Is_Task_Type (Desig_T) then
830 declare
831 Stat : Node_Id := Prev (N);
832 Nam1 : Node_Id;
833 Nam2 : Node_Id;
835 begin
836 -- An Abort followed by a Free will not do what the user
837 -- expects, because the abort is not immediate. This is
838 -- worth a friendly warning.
840 while Present (Stat)
841 and then not Comes_From_Source (Original_Node (Stat))
842 loop
843 Prev (Stat);
844 end loop;
846 if Present (Stat)
847 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
848 then
849 Stat := Original_Node (Stat);
850 Nam1 := First (Names (Stat));
851 Nam2 := Original_Node (First (Parameter_Associations (N)));
853 if Nkind (Nam1) = N_Explicit_Dereference
854 and then Is_Entity_Name (Prefix (Nam1))
855 and then Is_Entity_Name (Nam2)
856 and then Entity (Prefix (Nam1)) = Entity (Nam2)
857 then
858 Error_Msg_N ("abort may take time to complete?", N);
859 Error_Msg_N ("\deallocation might have no effect?", N);
860 Error_Msg_N ("\safer to wait for termination.?", N);
861 end if;
862 end if;
863 end;
865 Append_To
866 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
868 -- For composite types that contain tasks, recurse over the structure
869 -- to build the selectors for the task subcomponents.
871 elsif Has_Task (Desig_T) then
872 if Is_Record_Type (Desig_T) then
873 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
875 elsif Is_Array_Type (Desig_T) then
876 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
877 end if;
878 end if;
880 -- Same for simple protected types. Eventually call Finalize_Protection
881 -- before freeing the PO for each protected component.
883 if Is_Simple_Protected_Type (Desig_T) then
884 Append_To (Stmts,
885 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
887 elsif Has_Simple_Protected_Object (Desig_T) then
888 if Is_Record_Type (Desig_T) then
889 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
890 elsif Is_Array_Type (Desig_T) then
891 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
892 end if;
893 end if;
895 -- Normal processing for non-controlled types
897 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
898 Free_Node := Make_Free_Statement (Loc, Empty);
899 Append_To (Stmts, Free_Node);
900 Set_Storage_Pool (Free_Node, Pool);
902 -- Deal with storage pool
904 if Present (Pool) then
906 -- Freeing the secondary stack is meaningless
908 if Is_RTE (Pool, RE_SS_Pool) then
909 null;
911 elsif Is_Class_Wide_Type (Etype (Pool)) then
913 -- Case of a class-wide pool type: make a dispatching call
914 -- to Deallocate through the class-wide Deallocate_Any.
916 Set_Procedure_To_Call (Free_Node,
917 RTE (RE_Deallocate_Any));
919 else
920 -- Case of a specific pool type: make a statically bound call
922 Set_Procedure_To_Call (Free_Node,
923 Find_Prim_Op (Etype (Pool), Name_Deallocate));
924 end if;
925 end if;
927 if Present (Procedure_To_Call (Free_Node)) then
929 -- For all cases of a Deallocate call, the back-end needs to be
930 -- able to compute the size of the object being freed. This may
931 -- require some adjustments for objects of dynamic size.
933 -- If the type is class wide, we generate an implicit type with the
934 -- right dynamic size, so that the deallocate call gets the right
935 -- size parameter computed by GIGI. Same for an access to
936 -- unconstrained packed array.
938 if Is_Class_Wide_Type (Desig_T)
939 or else
940 (Is_Array_Type (Desig_T)
941 and then not Is_Constrained (Desig_T)
942 and then Is_Packed (Desig_T))
943 then
944 declare
945 Deref : constant Node_Id :=
946 Make_Explicit_Dereference (Loc,
947 Duplicate_Subexpr_No_Checks (Arg));
948 D_Subtyp : Node_Id;
949 D_Type : Entity_Id;
951 begin
952 Set_Etype (Deref, Typ);
953 Set_Parent (Deref, Free_Node);
954 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
956 if Nkind (D_Subtyp) in N_Has_Entity then
957 D_Type := Entity (D_Subtyp);
959 else
960 D_Type := Make_Defining_Identifier (Loc,
961 New_Internal_Name ('A'));
962 Insert_Action (N,
963 Make_Subtype_Declaration (Loc,
964 Defining_Identifier => D_Type,
965 Subtype_Indication => D_Subtyp));
966 Freeze_Itype (D_Type, N);
968 end if;
970 Set_Actual_Designated_Subtype (Free_Node, D_Type);
971 end;
973 end if;
974 end if;
976 Set_Expression (Free_Node, Free_Arg);
978 -- Only remaining step is to set result to null, or generate a
979 -- raise of constraint error if the target object is "not null".
981 if Can_Never_Be_Null (Etype (Arg)) then
982 Append_To (Stmts,
983 Make_Raise_Constraint_Error (Loc,
984 Reason => CE_Access_Check_Failed));
986 else
987 declare
988 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
989 begin
990 Set_Assignment_OK (Lhs);
991 Append_To (Stmts,
992 Make_Assignment_Statement (Loc,
993 Name => Lhs,
994 Expression => Make_Null (Loc)));
995 end;
996 end if;
998 -- If we know the argument is non-null, then make a block statement
999 -- that contains the required statements, no need for a test.
1001 if Arg_Known_Non_Null then
1002 Gen_Code :=
1003 Make_Block_Statement (Loc,
1004 Handled_Statement_Sequence =>
1005 Make_Handled_Sequence_Of_Statements (Loc,
1006 Statements => Stmts));
1008 -- If the argument may be null, wrap the statements inside an IF that
1009 -- does an explicit test to exclude the null case.
1011 else
1012 Gen_Code :=
1013 Make_Implicit_If_Statement (N,
1014 Condition =>
1015 Make_Op_Ne (Loc,
1016 Left_Opnd => Duplicate_Subexpr (Arg),
1017 Right_Opnd => Make_Null (Loc)),
1018 Then_Statements => Stmts);
1019 end if;
1021 -- Rewrite the call
1023 Rewrite (N, Gen_Code);
1024 Analyze (N);
1025 end Expand_Unc_Deallocation;
1027 -----------------------
1028 -- Expand_To_Address --
1029 -----------------------
1031 procedure Expand_To_Address (N : Node_Id) is
1032 Loc : constant Source_Ptr := Sloc (N);
1033 Arg : constant Node_Id := First_Actual (N);
1034 Obj : Node_Id;
1036 begin
1037 Remove_Side_Effects (Arg);
1039 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1041 Rewrite (N,
1042 Make_Conditional_Expression (Loc,
1043 Expressions => New_List (
1044 Make_Op_Eq (Loc,
1045 Left_Opnd => New_Copy_Tree (Arg),
1046 Right_Opnd => Make_Null (Loc)),
1047 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1048 Make_Attribute_Reference (Loc,
1049 Attribute_Name => Name_Address,
1050 Prefix => Obj))));
1052 Analyze_And_Resolve (N, RTE (RE_Address));
1053 end Expand_To_Address;
1055 -----------------------
1056 -- Expand_To_Pointer --
1057 -----------------------
1059 procedure Expand_To_Pointer (N : Node_Id) is
1060 Arg : constant Node_Id := First_Actual (N);
1062 begin
1063 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1064 Analyze (N);
1065 end Expand_To_Pointer;
1067 end Exp_Intr;