merge with trunk @ 139506
[official-gcc.git] / gcc / ada / exp_intr.adb
blobd3f9334a6079ce8db18095c81966ce8d8845e7ca
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-2008, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Code; use Exp_Code;
36 with Exp_Fixd; use Exp_Fixd;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
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_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Sinfo; use Sinfo;
51 with Sinput; use Sinput;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Stringt; use Stringt;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Urealp; use Urealp;
60 package body Exp_Intr is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Expand_Is_Negative (N : Node_Id);
67 -- Expand a call to the intrinsic Is_Negative function
69 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
70 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
71 -- into a dispatching call to the actual subprogram associated with the
72 -- Constructor formal subprogram, passing it the Parameters actual of
73 -- the call to the instantiation and dispatching based on call's Tag
74 -- parameter.
76 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
77 -- Expand a call to Exception_Information/Message/Name. The first
78 -- parameter, N, is the node for the function call, and Ent is the
79 -- entity for the corresponding routine in the Ada.Exceptions package.
81 procedure Expand_Import_Call (N : Node_Id);
82 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
83 -- N is the node for the function call.
85 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
86 -- Expand an intrinsic shift operation, N and E are from the call to
87 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
88 -- K is the kind for the shift node
90 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
91 -- Expand a call to an instantiation of Unchecked_Conversion into a node
92 -- N_Unchecked_Type_Conversion.
94 procedure Expand_Unc_Deallocation (N : Node_Id);
95 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
96 -- N_Free_Statement and appropriate context.
98 procedure Expand_To_Address (N : Node_Id);
99 procedure Expand_To_Pointer (N : Node_Id);
100 -- Expand a call to corresponding function, declared in an instance of
101 -- System.Address_To_Access_Conversions.
103 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
104 -- Rewrite the node by the appropriate string or positive constant.
105 -- Nam can be one of the following:
106 -- Name_File - expand string that is the name of source file
107 -- Name_Line - expand integer line number
108 -- Name_Source_Location - expand string of form file:line
109 -- Name_Enclosing_Entity - expand string with name of enclosing entity
111 -----------------------------------------
112 -- Expand_Dispatching_Constructor_Call --
113 -----------------------------------------
115 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
116 -- of the form:
118 -- GDC_Instance (The_Tag, Parameters'Access)
120 -- to a class-wide conversion of a dispatching call to the actual
121 -- associated with the formal subprogram Construct, designating The_Tag
122 -- as the controlling tag of the call:
124 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
126 -- which will eventually be expanded to the following:
128 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
130 -- A class-wide membership test is also generated, preceding the call, to
131 -- ensure that the controlling tag denotes a type in T'Class.
133 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
134 Loc : constant Source_Ptr := Sloc (N);
135 Tag_Arg : constant Node_Id := First_Actual (N);
136 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
137 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
138 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
139 Act_Rename : Node_Id;
140 Act_Constr : Entity_Id;
141 Iface_Tag : Node_Id := Empty;
142 Cnstr_Call : Node_Id;
143 Result_Typ : Entity_Id;
145 begin
146 -- The subprogram is the third actual in the instantiation, and is
147 -- retrieved from the corresponding renaming declaration. However,
148 -- freeze nodes may appear before, so we retrieve the declaration
149 -- with an explicit loop.
151 Act_Rename := First (Visible_Declarations (Inst_Pkg));
152 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
153 Next (Act_Rename);
154 end loop;
156 Act_Constr := Entity (Name (Act_Rename));
157 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
159 -- Ada 2005 (AI-251): If the result is an interface type, the function
160 -- returns a class-wide interface type (otherwise the resulting object
161 -- would be abstract!)
163 if Is_Interface (Etype (Act_Constr)) then
164 Set_Etype (Act_Constr, Result_Typ);
166 -- If the result type is not parent of Tag_Arg then we need to
167 -- locate the tag of the secondary dispatch table.
169 if not Is_Ancestor (Etype (Result_Typ), Etype (Tag_Arg)) then
170 pragma Assert (not Is_Interface (Etype (Tag_Arg)));
172 Iface_Tag :=
173 Make_Object_Declaration (Loc,
174 Defining_Identifier =>
175 Make_Defining_Identifier (Loc, New_Internal_Name ('V')),
176 Object_Definition =>
177 New_Reference_To (RTE (RE_Tag), Loc),
178 Expression =>
179 Make_Function_Call (Loc,
180 Name => New_Reference_To (RTE (RE_Secondary_Tag), Loc),
181 Parameter_Associations => New_List (
182 Relocate_Node (Tag_Arg),
183 New_Reference_To
184 (Node (First_Elmt (Access_Disp_Table
185 (Etype (Etype (Act_Constr))))),
186 Loc))));
187 Insert_Action (N, Iface_Tag);
188 end if;
189 end if;
191 -- Create the call to the actual Constructor function
193 Cnstr_Call :=
194 Make_Function_Call (Loc,
195 Name => New_Occurrence_Of (Act_Constr, Loc),
196 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
198 -- Establish its controlling tag from the tag passed to the instance
199 -- The tag may be given by a function call, in which case a temporary
200 -- should be generated now, to prevent out-of-order insertions during
201 -- the expansion of that call when stack-checking is enabled.
203 if Present (Iface_Tag) then
204 Set_Controlling_Argument (Cnstr_Call,
205 New_Occurrence_Of (Defining_Identifier (Iface_Tag), Loc));
206 else
207 Remove_Side_Effects (Tag_Arg);
208 Set_Controlling_Argument (Cnstr_Call,
209 Relocate_Node (Tag_Arg));
210 end if;
212 -- Rewrite and analyze the call to the instance as a class-wide
213 -- conversion of the call to the actual constructor.
215 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
216 Analyze_And_Resolve (N, Etype (Act_Constr));
218 -- Do not generate a run-time check on the built object if tag
219 -- checks are suppressed for the result type or VM_Target /= No_VM
221 if Tag_Checks_Suppressed (Etype (Result_Typ))
222 or else VM_Target /= No_VM
223 then
224 null;
226 -- Generate a class-wide membership test to ensure that the call's tag
227 -- argument denotes a type within the class. We must keep separate the
228 -- case in which the Result_Type of the constructor function is a tagged
229 -- type from the case in which it is an abstract interface because the
230 -- run-time subprogram required to check these cases differ (and have
231 -- one difference in their parameters profile).
233 -- Call CW_Membership if the Result_Type is a tagged type to look for
234 -- the tag in the table of ancestor tags.
236 elsif not Is_Interface (Result_Typ) then
237 Insert_Action (N,
238 Make_Implicit_If_Statement (N,
239 Condition =>
240 Make_Op_Not (Loc,
241 Build_CW_Membership (Loc,
242 Obj_Tag_Node => Duplicate_Subexpr (Tag_Arg),
243 Typ_Tag_Node =>
244 New_Reference_To (
245 Node (First_Elmt (Access_Disp_Table (
246 Root_Type (Result_Typ)))), Loc))),
247 Then_Statements =>
248 New_List (Make_Raise_Statement (Loc,
249 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
251 -- Call IW_Membership test if the Result_Type is an abstract interface
252 -- to look for the tag in the table of interface tags.
254 else
255 Insert_Action (N,
256 Make_Implicit_If_Statement (N,
257 Condition =>
258 Make_Op_Not (Loc,
259 Make_Function_Call (Loc,
260 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
261 Parameter_Associations => New_List (
262 Make_Attribute_Reference (Loc,
263 Prefix => Duplicate_Subexpr (Tag_Arg),
264 Attribute_Name => Name_Address),
266 New_Reference_To (
267 Node (First_Elmt (Access_Disp_Table (
268 Root_Type (Result_Typ)))), Loc)))),
269 Then_Statements =>
270 New_List (
271 Make_Raise_Statement (Loc,
272 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
273 end if;
274 end Expand_Dispatching_Constructor_Call;
276 ---------------------------
277 -- Expand_Exception_Call --
278 ---------------------------
280 -- If the function call is not within an exception handler, then the call
281 -- is replaced by a null string. Otherwise the appropriate routine in
282 -- Ada.Exceptions is called passing the choice parameter specification
283 -- from the enclosing handler. If the enclosing handler lacks a choice
284 -- parameter, then one is supplied.
286 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
287 Loc : constant Source_Ptr := Sloc (N);
288 P : Node_Id;
289 E : Entity_Id;
291 begin
292 -- Climb up parents to see if we are in exception handler
294 P := Parent (N);
295 loop
296 -- Case of not in exception handler, replace by null string
298 if No (P) then
299 Rewrite (N,
300 Make_String_Literal (Loc,
301 Strval => ""));
302 exit;
304 -- Case of in exception handler
306 elsif Nkind (P) = N_Exception_Handler then
308 -- Handler cannot be used for a local raise, and furthermore, this
309 -- is a violation of the No_Exception_Propagation restriction.
311 Set_Local_Raise_Not_OK (P);
312 Check_Restriction (No_Exception_Propagation, N);
314 -- If no choice parameter present, then put one there. Note that
315 -- we do not need to put it on the entity chain, since no one will
316 -- be referencing it by normal visibility methods.
318 if No (Choice_Parameter (P)) then
319 E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
320 Set_Choice_Parameter (P, E);
321 Set_Ekind (E, E_Variable);
322 Set_Etype (E, RTE (RE_Exception_Occurrence));
323 Set_Scope (E, Current_Scope);
324 end if;
326 Rewrite (N,
327 Make_Function_Call (Loc,
328 Name => New_Occurrence_Of (RTE (Ent), Loc),
329 Parameter_Associations => New_List (
330 New_Occurrence_Of (Choice_Parameter (P), Loc))));
331 exit;
333 -- Keep climbing!
335 else
336 P := Parent (P);
337 end if;
338 end loop;
340 Analyze_And_Resolve (N, Standard_String);
341 end Expand_Exception_Call;
343 ------------------------
344 -- Expand_Import_Call --
345 ------------------------
347 -- The function call must have a static string as its argument. We create
348 -- a dummy variable which uses this string as the external name in an
349 -- Import pragma. The result is then obtained as the address of this
350 -- dummy variable, converted to the appropriate target type.
352 procedure Expand_Import_Call (N : Node_Id) is
353 Loc : constant Source_Ptr := Sloc (N);
354 Ent : constant Entity_Id := Entity (Name (N));
355 Str : constant Node_Id := First_Actual (N);
356 Dum : Entity_Id;
358 begin
359 Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
361 Insert_Actions (N, New_List (
362 Make_Object_Declaration (Loc,
363 Defining_Identifier => Dum,
364 Object_Definition =>
365 New_Occurrence_Of (Standard_Character, Loc)),
367 Make_Pragma (Loc,
368 Chars => Name_Import,
369 Pragma_Argument_Associations => New_List (
370 Make_Pragma_Argument_Association (Loc,
371 Expression => Make_Identifier (Loc, Name_Ada)),
373 Make_Pragma_Argument_Association (Loc,
374 Expression => Make_Identifier (Loc, Chars (Dum))),
376 Make_Pragma_Argument_Association (Loc,
377 Chars => Name_Link_Name,
378 Expression => Relocate_Node (Str))))));
380 Rewrite (N,
381 Unchecked_Convert_To (Etype (Ent),
382 Make_Attribute_Reference (Loc,
383 Prefix => Make_Identifier (Loc, Chars (Dum)),
384 Attribute_Name => Name_Address)));
386 Analyze_And_Resolve (N, Etype (Ent));
387 end Expand_Import_Call;
389 ---------------------------
390 -- Expand_Intrinsic_Call --
391 ---------------------------
393 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
394 Nam : Name_Id;
396 begin
397 -- If the intrinsic subprogram is generic, gets its original name
399 if Present (Parent (E))
400 and then Present (Generic_Parent (Parent (E)))
401 then
402 Nam := Chars (Generic_Parent (Parent (E)));
403 else
404 Nam := Chars (E);
405 end if;
407 if Nam = Name_Asm then
408 Expand_Asm_Call (N);
410 elsif Nam = Name_Divide then
411 Expand_Decimal_Divide_Call (N);
413 elsif Nam = Name_Exception_Information then
414 Expand_Exception_Call (N, RE_Exception_Information);
416 elsif Nam = Name_Exception_Message then
417 Expand_Exception_Call (N, RE_Exception_Message);
419 elsif Nam = Name_Exception_Name then
420 Expand_Exception_Call (N, RE_Exception_Name_Simple);
422 elsif Nam = Name_Generic_Dispatching_Constructor then
423 Expand_Dispatching_Constructor_Call (N);
425 elsif Nam = Name_Import_Address
426 or else
427 Nam = Name_Import_Largest_Value
428 or else
429 Nam = Name_Import_Value
430 then
431 Expand_Import_Call (N);
433 elsif Nam = Name_Is_Negative then
434 Expand_Is_Negative (N);
436 elsif Nam = Name_Rotate_Left then
437 Expand_Shift (N, E, N_Op_Rotate_Left);
439 elsif Nam = Name_Rotate_Right then
440 Expand_Shift (N, E, N_Op_Rotate_Right);
442 elsif Nam = Name_Shift_Left then
443 Expand_Shift (N, E, N_Op_Shift_Left);
445 elsif Nam = Name_Shift_Right then
446 Expand_Shift (N, E, N_Op_Shift_Right);
448 elsif Nam = Name_Shift_Right_Arithmetic then
449 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
451 elsif Nam = Name_Unchecked_Conversion then
452 Expand_Unc_Conversion (N, E);
454 elsif Nam = Name_Unchecked_Deallocation then
455 Expand_Unc_Deallocation (N);
457 elsif Nam = Name_To_Address then
458 Expand_To_Address (N);
460 elsif Nam = Name_To_Pointer then
461 Expand_To_Pointer (N);
463 elsif Nam = Name_File
464 or else Nam = Name_Line
465 or else Nam = Name_Source_Location
466 or else Nam = Name_Enclosing_Entity
467 then
468 Expand_Source_Info (N, Nam);
470 -- If we have a renaming, expand the call to the original operation,
471 -- which must itself be intrinsic, since renaming requires matching
472 -- conventions and this has already been checked.
474 elsif Present (Alias (E)) then
475 Expand_Intrinsic_Call (N, Alias (E));
477 -- The only other case is where an external name was specified,
478 -- since this is the only way that an otherwise unrecognized
479 -- name could escape the checking in Sem_Prag. Nothing needs
480 -- to be done in such a case, since we pass such a call to the
481 -- back end unchanged.
483 else
484 null;
485 end if;
486 end Expand_Intrinsic_Call;
488 ------------------------
489 -- Expand_Is_Negative --
490 ------------------------
492 procedure Expand_Is_Negative (N : Node_Id) is
493 Loc : constant Source_Ptr := Sloc (N);
494 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
496 begin
498 -- We replace the function call by the following expression
500 -- if Opnd < 0.0 then
501 -- True
502 -- else
503 -- if Opnd > 0.0 then
504 -- False;
505 -- else
506 -- Float_Unsigned!(Float (Opnd)) /= 0
507 -- end if;
508 -- end if;
510 Rewrite (N,
511 Make_Conditional_Expression (Loc,
512 Expressions => New_List (
513 Make_Op_Lt (Loc,
514 Left_Opnd => Duplicate_Subexpr (Opnd),
515 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
517 New_Occurrence_Of (Standard_True, Loc),
519 Make_Conditional_Expression (Loc,
520 Expressions => New_List (
521 Make_Op_Gt (Loc,
522 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
523 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
525 New_Occurrence_Of (Standard_False, Loc),
527 Make_Op_Ne (Loc,
528 Left_Opnd =>
529 Unchecked_Convert_To
530 (RTE (RE_Float_Unsigned),
531 Convert_To
532 (Standard_Float,
533 Duplicate_Subexpr_No_Checks (Opnd))),
534 Right_Opnd =>
535 Make_Integer_Literal (Loc, 0)))))));
537 Analyze_And_Resolve (N, Standard_Boolean);
538 end Expand_Is_Negative;
540 ------------------
541 -- Expand_Shift --
542 ------------------
544 -- This procedure is used to convert a call to a shift function to the
545 -- corresponding operator node. This conversion is not done by the usual
546 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
547 -- operator nodes, because shifts are not predefined operators.
549 -- As a result, whenever a shift is used in the source program, it will
550 -- remain as a call until converted by this routine to the operator node
551 -- form which Gigi is expecting to see.
553 -- Note: it is possible for the expander to generate shift operator nodes
554 -- directly, which will be analyzed in the normal manner by calling Analyze
555 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
557 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
558 Loc : constant Source_Ptr := Sloc (N);
559 Typ : constant Entity_Id := Etype (N);
560 Left : constant Node_Id := First_Actual (N);
561 Right : constant Node_Id := Next_Actual (Left);
562 Ltyp : constant Node_Id := Etype (Left);
563 Rtyp : constant Node_Id := Etype (Right);
564 Snode : Node_Id;
566 begin
567 Snode := New_Node (K, Loc);
568 Set_Left_Opnd (Snode, Relocate_Node (Left));
569 Set_Right_Opnd (Snode, Relocate_Node (Right));
570 Set_Chars (Snode, Chars (E));
571 Set_Etype (Snode, Base_Type (Typ));
572 Set_Entity (Snode, E);
574 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
575 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
576 then
577 Set_Shift_Count_OK (Snode, True);
578 end if;
580 -- Do the rewrite. Note that we don't call Analyze and Resolve on
581 -- this node, because it already got analyzed and resolved when
582 -- it was a function call!
584 Rewrite (N, Snode);
585 Set_Analyzed (N);
586 end Expand_Shift;
588 ------------------------
589 -- Expand_Source_Info --
590 ------------------------
592 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
593 Loc : constant Source_Ptr := Sloc (N);
594 Ent : Entity_Id;
596 procedure Write_Entity_Name (E : Entity_Id);
597 -- Recursive procedure to construct string for qualified name of
598 -- enclosing program unit. The qualification stops at an enclosing
599 -- scope has no source name (block or loop). If entity is a subprogram
600 -- instance, skip enclosing wrapper package.
602 -----------------------
603 -- Write_Entity_Name --
604 -----------------------
606 procedure Write_Entity_Name (E : Entity_Id) is
607 SDef : Source_Ptr;
608 TDef : constant Source_Buffer_Ptr :=
609 Source_Text (Get_Source_File_Index (Sloc (E)));
611 begin
612 -- Nothing to do if at outer level
614 if Scope (E) = Standard_Standard then
615 null;
617 -- If scope comes from source, write its name
619 elsif Comes_From_Source (Scope (E)) then
620 Write_Entity_Name (Scope (E));
621 Add_Char_To_Name_Buffer ('.');
623 -- If in wrapper package skip past it
625 elsif Is_Wrapper_Package (Scope (E)) then
626 Write_Entity_Name (Scope (Scope (E)));
627 Add_Char_To_Name_Buffer ('.');
629 -- Otherwise nothing to output (happens in unnamed block statements)
631 else
632 null;
633 end if;
635 -- Loop to output the name
637 -- is this right wrt wide char encodings ??? (no!)
639 SDef := Sloc (E);
640 while TDef (SDef) in '0' .. '9'
641 or else TDef (SDef) >= 'A'
642 or else TDef (SDef) = ASCII.ESC
643 loop
644 Add_Char_To_Name_Buffer (TDef (SDef));
645 SDef := SDef + 1;
646 end loop;
647 end Write_Entity_Name;
649 -- Start of processing for Expand_Source_Info
651 begin
652 -- Integer cases
654 if Nam = Name_Line then
655 Rewrite (N,
656 Make_Integer_Literal (Loc,
657 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
658 Analyze_And_Resolve (N, Standard_Positive);
660 -- String cases
662 else
663 Name_Len := 0;
665 case Nam is
666 when Name_File =>
667 Get_Decoded_Name_String
668 (Reference_Name (Get_Source_File_Index (Loc)));
670 when Name_Source_Location =>
671 Build_Location_String (Loc);
673 when Name_Enclosing_Entity =>
675 -- Skip enclosing blocks to reach enclosing unit
677 Ent := Current_Scope;
678 while Present (Ent) loop
679 exit when Ekind (Ent) /= E_Block
680 and then Ekind (Ent) /= E_Loop;
681 Ent := Scope (Ent);
682 end loop;
684 -- Ent now points to the relevant defining entity
686 Write_Entity_Name (Ent);
688 when others =>
689 raise Program_Error;
690 end case;
692 Rewrite (N,
693 Make_String_Literal (Loc,
694 Strval => String_From_Name_Buffer));
695 Analyze_And_Resolve (N, Standard_String);
696 end if;
698 Set_Is_Static_Expression (N);
699 end Expand_Source_Info;
701 ---------------------------
702 -- Expand_Unc_Conversion --
703 ---------------------------
705 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
706 Func : constant Entity_Id := Entity (Name (N));
707 Conv : Node_Id;
708 Ftyp : Entity_Id;
709 Ttyp : Entity_Id;
711 begin
712 -- Rewrite as unchecked conversion node. Note that we must convert
713 -- the operand to the formal type of the input parameter of the
714 -- function, so that the resulting N_Unchecked_Type_Conversion
715 -- call indicates the correct types for Gigi.
717 -- Right now, we only do this if a scalar type is involved. It is
718 -- not clear if it is needed in other cases. If we do attempt to
719 -- do the conversion unconditionally, it crashes 3411-018. To be
720 -- investigated further ???
722 Conv := Relocate_Node (First_Actual (N));
723 Ftyp := Etype (First_Formal (Func));
725 if Is_Scalar_Type (Ftyp) then
726 Conv := Convert_To (Ftyp, Conv);
727 Set_Parent (Conv, N);
728 Analyze_And_Resolve (Conv);
729 end if;
731 -- The instantiation of Unchecked_Conversion creates a wrapper package,
732 -- and the target type is declared as a subtype of the actual. Recover
733 -- the actual, which is the subtype indic. in the subtype declaration
734 -- for the target type. This is semantically correct, and avoids
735 -- anomalies with access subtypes. For entities, leave type as is.
737 -- We do the analysis here, because we do not want the compiler
738 -- to try to optimize or otherwise reorganize the unchecked
739 -- conversion node.
741 Ttyp := Etype (E);
743 if Is_Entity_Name (Conv) then
744 null;
746 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
747 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
749 elsif Is_Itype (Ttyp) then
750 Ttyp :=
751 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
752 else
753 raise Program_Error;
754 end if;
756 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
757 Set_Etype (N, Ttyp);
758 Set_Analyzed (N);
760 if Nkind (N) = N_Unchecked_Type_Conversion then
761 Expand_N_Unchecked_Type_Conversion (N);
762 end if;
763 end Expand_Unc_Conversion;
765 -----------------------------
766 -- Expand_Unc_Deallocation --
767 -----------------------------
769 -- Generate the following Code :
771 -- if Arg /= null then
772 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
773 -- Free (Arg);
774 -- Arg := Null;
775 -- end if;
777 -- For a task, we also generate a call to Free_Task to ensure that the
778 -- task itself is freed if it is terminated, ditto for a simple protected
779 -- object, with a call to Finalize_Protection. For composite types that
780 -- have tasks or simple protected objects as components, we traverse the
781 -- structures to find and terminate those components.
783 procedure Expand_Unc_Deallocation (N : Node_Id) is
784 Loc : constant Source_Ptr := Sloc (N);
785 Arg : constant Node_Id := First_Actual (N);
786 Typ : constant Entity_Id := Etype (Arg);
787 Stmts : constant List_Id := New_List;
788 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
789 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
791 Desig_T : constant Entity_Id := Designated_Type (Typ);
792 Gen_Code : Node_Id;
793 Free_Node : Node_Id;
794 Deref : Node_Id;
795 Free_Arg : Node_Id;
796 Free_Cod : List_Id;
797 Blk : Node_Id;
799 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
800 -- This captures whether we know the argument to be non-null so that
801 -- we can avoid the test. The reason that we need to capture this is
802 -- that we analyze some generated statements before properly attaching
803 -- them to the tree, and that can disturb current value settings.
805 begin
806 if No_Pool_Assigned (Rtyp) then
807 Error_Msg_N ("?deallocation from empty storage pool!", N);
808 end if;
810 -- Nothing to do if we know the argument is null
812 if Known_Null (N) then
813 return;
814 end if;
816 -- Processing for pointer to controlled type
818 if Needs_Finalization (Desig_T) then
819 Deref :=
820 Make_Explicit_Dereference (Loc,
821 Prefix => Duplicate_Subexpr_No_Checks (Arg));
823 -- If the type is tagged, then we must force dispatching on the
824 -- finalization call because the designated type may not be the
825 -- actual type of the object.
827 if Is_Tagged_Type (Desig_T)
828 and then not Is_Class_Wide_Type (Desig_T)
829 then
830 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
832 elsif not Is_Tagged_Type (Desig_T) then
834 -- Set type of result, to force a conversion when needed (see
835 -- exp_ch7, Convert_View), given that Deep_Finalize may be
836 -- inherited from the parent type, and we need the type of the
837 -- expression to see whether the conversion is in fact needed.
839 Set_Etype (Deref, Desig_T);
840 end if;
842 Free_Cod :=
843 Make_Final_Call
844 (Ref => Deref,
845 Typ => Desig_T,
846 With_Detach => New_Reference_To (Standard_True, Loc));
848 if Abort_Allowed then
849 Prepend_To (Free_Cod,
850 Build_Runtime_Call (Loc, RE_Abort_Defer));
852 Blk :=
853 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
854 Make_Handled_Sequence_Of_Statements (Loc,
855 Statements => Free_Cod,
856 At_End_Proc =>
857 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
859 -- We now expand the exception (at end) handler. We set a
860 -- temporary parent pointer since we have not attached Blk
861 -- to the tree yet.
863 Set_Parent (Blk, N);
864 Analyze (Blk);
865 Expand_At_End_Handler
866 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
867 Append (Blk, Stmts);
869 -- We kill saved current values, since analyzing statements not
870 -- properly attached to the tree can set wrong current values.
872 Kill_Current_Values;
874 else
875 Append_List_To (Stmts, Free_Cod);
876 end if;
877 end if;
879 -- For a task type, call Free_Task before freeing the ATCB
881 if Is_Task_Type (Desig_T) then
882 declare
883 Stat : Node_Id := Prev (N);
884 Nam1 : Node_Id;
885 Nam2 : Node_Id;
887 begin
888 -- An Abort followed by a Free will not do what the user
889 -- expects, because the abort is not immediate. This is
890 -- worth a friendly warning.
892 while Present (Stat)
893 and then not Comes_From_Source (Original_Node (Stat))
894 loop
895 Prev (Stat);
896 end loop;
898 if Present (Stat)
899 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
900 then
901 Stat := Original_Node (Stat);
902 Nam1 := First (Names (Stat));
903 Nam2 := Original_Node (First (Parameter_Associations (N)));
905 if Nkind (Nam1) = N_Explicit_Dereference
906 and then Is_Entity_Name (Prefix (Nam1))
907 and then Is_Entity_Name (Nam2)
908 and then Entity (Prefix (Nam1)) = Entity (Nam2)
909 then
910 Error_Msg_N ("abort may take time to complete?", N);
911 Error_Msg_N ("\deallocation might have no effect?", N);
912 Error_Msg_N ("\safer to wait for termination.?", N);
913 end if;
914 end if;
915 end;
917 Append_To
918 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
920 -- For composite types that contain tasks, recurse over the structure
921 -- to build the selectors for the task subcomponents.
923 elsif Has_Task (Desig_T) then
924 if Is_Record_Type (Desig_T) then
925 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
927 elsif Is_Array_Type (Desig_T) then
928 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
929 end if;
930 end if;
932 -- Same for simple protected types. Eventually call Finalize_Protection
933 -- before freeing the PO for each protected component.
935 if Is_Simple_Protected_Type (Desig_T) then
936 Append_To (Stmts,
937 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
939 elsif Has_Simple_Protected_Object (Desig_T) then
940 if Is_Record_Type (Desig_T) then
941 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
942 elsif Is_Array_Type (Desig_T) then
943 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
944 end if;
945 end if;
947 -- Normal processing for non-controlled types
949 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
950 Free_Node := Make_Free_Statement (Loc, Empty);
951 Append_To (Stmts, Free_Node);
952 Set_Storage_Pool (Free_Node, Pool);
954 -- Deal with storage pool
956 if Present (Pool) then
958 -- Freeing the secondary stack is meaningless
960 if Is_RTE (Pool, RE_SS_Pool) then
961 null;
963 elsif Is_Class_Wide_Type (Etype (Pool)) then
965 -- Case of a class-wide pool type: make a dispatching call
966 -- to Deallocate through the class-wide Deallocate_Any.
968 Set_Procedure_To_Call (Free_Node,
969 RTE (RE_Deallocate_Any));
971 else
972 -- Case of a specific pool type: make a statically bound call
974 Set_Procedure_To_Call (Free_Node,
975 Find_Prim_Op (Etype (Pool), Name_Deallocate));
976 end if;
977 end if;
979 if Present (Procedure_To_Call (Free_Node)) then
981 -- For all cases of a Deallocate call, the back-end needs to be
982 -- able to compute the size of the object being freed. This may
983 -- require some adjustments for objects of dynamic size.
985 -- If the type is class wide, we generate an implicit type with the
986 -- right dynamic size, so that the deallocate call gets the right
987 -- size parameter computed by GIGI. Same for an access to
988 -- unconstrained packed array.
990 if Is_Class_Wide_Type (Desig_T)
991 or else
992 (Is_Array_Type (Desig_T)
993 and then not Is_Constrained (Desig_T)
994 and then Is_Packed (Desig_T))
995 then
996 declare
997 Deref : constant Node_Id :=
998 Make_Explicit_Dereference (Loc,
999 Duplicate_Subexpr_No_Checks (Arg));
1000 D_Subtyp : Node_Id;
1001 D_Type : Entity_Id;
1003 begin
1004 Set_Etype (Deref, Typ);
1005 Set_Parent (Deref, Free_Node);
1006 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
1008 if Nkind (D_Subtyp) in N_Has_Entity then
1009 D_Type := Entity (D_Subtyp);
1011 else
1012 D_Type := Make_Defining_Identifier (Loc,
1013 New_Internal_Name ('A'));
1014 Insert_Action (N,
1015 Make_Subtype_Declaration (Loc,
1016 Defining_Identifier => D_Type,
1017 Subtype_Indication => D_Subtyp));
1018 Freeze_Itype (D_Type, N);
1020 end if;
1022 Set_Actual_Designated_Subtype (Free_Node, D_Type);
1023 end;
1025 end if;
1026 end if;
1028 -- Ada 2005 (AI-251): In case of abstract interface type we must
1029 -- displace the pointer to reference the base of the object to
1030 -- deallocate its memory, unless we're targetting a VM, in which case
1031 -- no special processing is required.
1033 -- Generate:
1034 -- free (Base_Address (Obj_Ptr))
1036 if Is_Interface (Directly_Designated_Type (Typ))
1037 and then VM_Target = No_VM
1038 then
1039 Set_Expression (Free_Node,
1040 Unchecked_Convert_To (Typ,
1041 Make_Function_Call (Loc,
1042 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1043 Parameter_Associations => New_List (
1044 Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1046 -- Generate:
1047 -- free (Obj_Ptr)
1049 else
1050 Set_Expression (Free_Node, Free_Arg);
1051 end if;
1053 -- Only remaining step is to set result to null, or generate a
1054 -- raise of constraint error if the target object is "not null".
1056 if Can_Never_Be_Null (Etype (Arg)) then
1057 Append_To (Stmts,
1058 Make_Raise_Constraint_Error (Loc,
1059 Reason => CE_Access_Check_Failed));
1061 else
1062 declare
1063 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1064 begin
1065 Set_Assignment_OK (Lhs);
1066 Append_To (Stmts,
1067 Make_Assignment_Statement (Loc,
1068 Name => Lhs,
1069 Expression => Make_Null (Loc)));
1070 end;
1071 end if;
1073 -- If we know the argument is non-null, then make a block statement
1074 -- that contains the required statements, no need for a test.
1076 if Arg_Known_Non_Null then
1077 Gen_Code :=
1078 Make_Block_Statement (Loc,
1079 Handled_Statement_Sequence =>
1080 Make_Handled_Sequence_Of_Statements (Loc,
1081 Statements => Stmts));
1083 -- If the argument may be null, wrap the statements inside an IF that
1084 -- does an explicit test to exclude the null case.
1086 else
1087 Gen_Code :=
1088 Make_Implicit_If_Statement (N,
1089 Condition =>
1090 Make_Op_Ne (Loc,
1091 Left_Opnd => Duplicate_Subexpr (Arg),
1092 Right_Opnd => Make_Null (Loc)),
1093 Then_Statements => Stmts);
1094 end if;
1096 -- Rewrite the call
1098 Rewrite (N, Gen_Code);
1099 Analyze (N);
1100 end Expand_Unc_Deallocation;
1102 -----------------------
1103 -- Expand_To_Address --
1104 -----------------------
1106 procedure Expand_To_Address (N : Node_Id) is
1107 Loc : constant Source_Ptr := Sloc (N);
1108 Arg : constant Node_Id := First_Actual (N);
1109 Obj : Node_Id;
1111 begin
1112 Remove_Side_Effects (Arg);
1114 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1116 Rewrite (N,
1117 Make_Conditional_Expression (Loc,
1118 Expressions => New_List (
1119 Make_Op_Eq (Loc,
1120 Left_Opnd => New_Copy_Tree (Arg),
1121 Right_Opnd => Make_Null (Loc)),
1122 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1123 Make_Attribute_Reference (Loc,
1124 Prefix => Obj,
1125 Attribute_Name => Name_Address))));
1127 Analyze_And_Resolve (N, RTE (RE_Address));
1128 end Expand_To_Address;
1130 -----------------------
1131 -- Expand_To_Pointer --
1132 -----------------------
1134 procedure Expand_To_Pointer (N : Node_Id) is
1135 Arg : constant Node_Id := First_Actual (N);
1137 begin
1138 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1139 Analyze (N);
1140 end Expand_To_Pointer;
1142 end Exp_Intr;