2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / exp_intr.adb
blob8002fef8bc97cc583d283745ff2b2ab1682a9275
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-2015, 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 Expander; use Expander;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Code; use Exp_Code;
37 with Exp_Fixd; use Exp_Fixd;
38 with Exp_Util; use Exp_Util;
39 with Freeze; use Freeze;
40 with Inline; use Inline;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Sinput; use Sinput;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Stringt; use Stringt;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Uintp; use Uintp;
61 with Urealp; use Urealp;
63 package body Exp_Intr is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Expand_Binary_Operator_Call (N : Node_Id);
70 -- Expand a call to an intrinsic arithmetic operator when the operand
71 -- types or sizes are not identical.
73 procedure Expand_Is_Negative (N : Node_Id);
74 -- Expand a call to the intrinsic Is_Negative function
76 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
77 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
78 -- into a dispatching call to the actual subprogram associated with the
79 -- Constructor formal subprogram, passing it the Parameters actual of
80 -- the call to the instantiation and dispatching based on call's Tag
81 -- parameter.
83 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
84 -- Expand a call to Exception_Information/Message/Name. The first
85 -- parameter, N, is the node for the function call, and Ent is the
86 -- entity for the corresponding routine in the Ada.Exceptions package.
88 procedure Expand_Import_Call (N : Node_Id);
89 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
90 -- N is the node for the function call.
92 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
93 -- Expand an intrinsic shift operation, N and E are from the call to
94 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
95 -- K is the kind for the shift node
97 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
98 -- Expand a call to an instantiation of Unchecked_Conversion into a node
99 -- N_Unchecked_Type_Conversion.
101 procedure Expand_Unc_Deallocation (N : Node_Id);
102 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
103 -- N_Free_Statement and appropriate context.
105 procedure Expand_To_Address (N : Node_Id);
106 procedure Expand_To_Pointer (N : Node_Id);
107 -- Expand a call to corresponding function, declared in an instance of
108 -- System.Address_To_Access_Conversions.
110 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
111 -- Rewrite the node by the appropriate string or positive constant.
112 -- Nam can be one of the following:
113 -- Name_File - expand string name of source file
114 -- Name_Line - expand integer line number
115 -- Name_Source_Location - expand string of form file:line
116 -- Name_Enclosing_Entity - expand string name of enclosing entity
117 -- Name_Compilation_Date - expand string with compilation date
118 -- Name_Compilation_Time - expand string with compilation time
120 procedure Write_Entity_Name (E : Entity_Id);
121 -- Recursive procedure to construct string for qualified name of enclosing
122 -- program unit. The qualification stops at an enclosing scope has no
123 -- source name (block or loop). If entity is a subprogram instance, skip
124 -- enclosing wrapper package. The name is appended to the current contents
125 -- of Name_Buffer, incrementing Name_Len.
127 ---------------------
128 -- Add_Source_Info --
129 ---------------------
131 procedure Add_Source_Info (Loc : Source_Ptr; Nam : Name_Id) is
132 Ent : Entity_Id;
134 Save_NB : constant String := Name_Buffer (1 .. Name_Len);
135 Save_NL : constant Natural := Name_Len;
136 -- Save current Name_Buffer contents
138 begin
139 Name_Len := 0;
141 -- Line
143 case Nam is
145 when Name_Line =>
146 Add_Nat_To_Name_Buffer (Nat (Get_Logical_Line_Number (Loc)));
148 when Name_File =>
149 Get_Decoded_Name_String
150 (Reference_Name (Get_Source_File_Index (Loc)));
152 when Name_Source_Location =>
153 Build_Location_String (Loc);
155 when Name_Enclosing_Entity =>
157 -- Skip enclosing blocks to reach enclosing unit
159 Ent := Current_Scope;
160 while Present (Ent) loop
161 exit when not Ekind_In (Ent, E_Block, E_Loop);
162 Ent := Scope (Ent);
163 end loop;
165 -- Ent now points to the relevant defining entity
167 Write_Entity_Name (Ent);
169 when Name_Compilation_Date =>
170 declare
171 subtype S13 is String (1 .. 3);
172 Months : constant array (1 .. 12) of S13 :=
173 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
174 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
176 M1 : constant Character := Opt.Compilation_Time (6);
177 M2 : constant Character := Opt.Compilation_Time (7);
179 MM : constant Natural range 1 .. 12 :=
180 (Character'Pos (M1) - Character'Pos ('0')) * 10 +
181 (Character'Pos (M2) - Character'Pos ('0'));
183 begin
184 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
186 Name_Buffer (1 .. 3) := Months (MM);
187 Name_Buffer (4) := ' ';
188 Name_Buffer (5 .. 6) := Opt.Compilation_Time (9 .. 10);
189 Name_Buffer (7) := ' ';
190 Name_Buffer (8 .. 11) := Opt.Compilation_Time (1 .. 4);
191 Name_Len := 11;
192 end;
194 when Name_Compilation_Time =>
195 Name_Buffer (1 .. 8) := Opt.Compilation_Time (12 .. 19);
196 Name_Len := 8;
198 when others =>
199 raise Program_Error;
200 end case;
202 -- Prepend original Name_Buffer contents
204 Name_Buffer (Save_NL + 1 .. Save_NL + Name_Len) :=
205 Name_Buffer (1 .. Name_Len);
206 Name_Buffer (1 .. Save_NL) := Save_NB;
207 Name_Len := Name_Len + Save_NL;
208 end Add_Source_Info;
210 ---------------------------------
211 -- Expand_Binary_Operator_Call --
212 ---------------------------------
214 procedure Expand_Binary_Operator_Call (N : Node_Id) is
215 T1 : constant Entity_Id := Underlying_Type (Etype (Left_Opnd (N)));
216 T2 : constant Entity_Id := Underlying_Type (Etype (Right_Opnd (N)));
217 TR : constant Entity_Id := Etype (N);
218 T3 : Entity_Id;
219 Res : Node_Id;
221 Siz : constant Uint := UI_Max (RM_Size (T1), RM_Size (T2));
222 -- Maximum of operand sizes
224 begin
225 -- Nothing to do if the operands have the same modular type
227 if Base_Type (T1) = Base_Type (T2)
228 and then Is_Modular_Integer_Type (T1)
229 then
230 return;
231 end if;
233 -- Use Unsigned_32 for sizes of 32 or below, else Unsigned_64
235 if Siz > 32 then
236 T3 := RTE (RE_Unsigned_64);
237 else
238 T3 := RTE (RE_Unsigned_32);
239 end if;
241 -- Copy operator node, and reset type and entity fields, for
242 -- subsequent reanalysis.
244 Res := New_Copy (N);
245 Set_Etype (Res, T3);
247 case Nkind (N) is
248 when N_Op_And =>
249 Set_Entity (Res, Standard_Op_And);
250 when N_Op_Or =>
251 Set_Entity (Res, Standard_Op_Or);
252 when N_Op_Xor =>
253 Set_Entity (Res, Standard_Op_Xor);
254 when others =>
255 raise Program_Error;
256 end case;
258 -- Convert operands to large enough intermediate type
260 Set_Left_Opnd (Res,
261 Unchecked_Convert_To (T3, Relocate_Node (Left_Opnd (N))));
262 Set_Right_Opnd (Res,
263 Unchecked_Convert_To (T3, Relocate_Node (Right_Opnd (N))));
265 -- Analyze and resolve result formed by conversion to target type
267 Rewrite (N, Unchecked_Convert_To (TR, Res));
268 Analyze_And_Resolve (N, TR);
269 end Expand_Binary_Operator_Call;
271 -----------------------------------------
272 -- Expand_Dispatching_Constructor_Call --
273 -----------------------------------------
275 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
276 -- of the form:
278 -- GDC_Instance (The_Tag, Parameters'Access)
280 -- to a class-wide conversion of a dispatching call to the actual
281 -- associated with the formal subprogram Construct, designating The_Tag
282 -- as the controlling tag of the call:
284 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
286 -- which will eventually be expanded to the following:
288 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
290 -- A class-wide membership test is also generated, preceding the call, to
291 -- ensure that the controlling tag denotes a type in T'Class.
293 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
294 Loc : constant Source_Ptr := Sloc (N);
295 Tag_Arg : constant Node_Id := First_Actual (N);
296 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
297 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
298 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
299 Act_Rename : Node_Id;
300 Act_Constr : Entity_Id;
301 Iface_Tag : Node_Id := Empty;
302 Cnstr_Call : Node_Id;
303 Result_Typ : Entity_Id;
305 begin
306 -- Remove side effects from tag argument early, before rewriting
307 -- the dispatching constructor call, as Remove_Side_Effects relies
308 -- on Tag_Arg's Parent link properly attached to the tree (once the
309 -- call is rewritten, the Parent is inconsistent as it points to the
310 -- rewritten node, which is not the syntactic parent of the Tag_Arg
311 -- anymore).
313 Remove_Side_Effects (Tag_Arg);
315 -- The subprogram is the third actual in the instantiation, and is
316 -- retrieved from the corresponding renaming declaration. However,
317 -- freeze nodes may appear before, so we retrieve the declaration
318 -- with an explicit loop.
320 Act_Rename := First (Visible_Declarations (Inst_Pkg));
321 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
322 Next (Act_Rename);
323 end loop;
325 Act_Constr := Entity (Name (Act_Rename));
326 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
328 if Is_Interface (Etype (Act_Constr)) then
330 -- If the result type is not known to be a parent of Tag_Arg then we
331 -- need to locate the tag of the secondary dispatch table.
333 if not Is_Ancestor (Etype (Result_Typ), Etype (Tag_Arg),
334 Use_Full_View => True)
335 and then Tagged_Type_Expansion
336 then
337 -- Obtain the reference to the Ada.Tags service before generating
338 -- the Object_Declaration node to ensure that if this service is
339 -- not available in the runtime then we generate a clear error.
341 declare
342 Fname : constant Node_Id :=
343 New_Occurrence_Of (RTE (RE_Secondary_Tag), Loc);
345 begin
346 pragma Assert (not Is_Interface (Etype (Tag_Arg)));
348 -- The tag is the first entry in the dispatch table of the
349 -- return type of the constructor.
351 Iface_Tag :=
352 Make_Object_Declaration (Loc,
353 Defining_Identifier => Make_Temporary (Loc, 'V'),
354 Object_Definition =>
355 New_Occurrence_Of (RTE (RE_Tag), Loc),
356 Expression =>
357 Make_Function_Call (Loc,
358 Name => Fname,
359 Parameter_Associations => New_List (
360 Relocate_Node (Tag_Arg),
361 New_Occurrence_Of
362 (Node (First_Elmt
363 (Access_Disp_Table (Etype (Act_Constr)))),
364 Loc))));
365 Insert_Action (N, Iface_Tag);
366 end;
367 end if;
368 end if;
370 -- Create the call to the actual Constructor function
372 Cnstr_Call :=
373 Make_Function_Call (Loc,
374 Name => New_Occurrence_Of (Act_Constr, Loc),
375 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
377 -- Establish its controlling tag from the tag passed to the instance
378 -- The tag may be given by a function call, in which case a temporary
379 -- should be generated now, to prevent out-of-order insertions during
380 -- the expansion of that call when stack-checking is enabled.
382 if Present (Iface_Tag) then
383 Set_Controlling_Argument (Cnstr_Call,
384 New_Occurrence_Of (Defining_Identifier (Iface_Tag), Loc));
385 else
386 Set_Controlling_Argument (Cnstr_Call,
387 Relocate_Node (Tag_Arg));
388 end if;
390 -- Rewrite and analyze the call to the instance as a class-wide
391 -- conversion of the call to the actual constructor.
393 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
394 Analyze_And_Resolve (N, Etype (Act_Constr));
396 -- Do not generate a run-time check on the built object if tag
397 -- checks are suppressed for the result type or VM_Target /= No_VM
399 if Tag_Checks_Suppressed (Etype (Result_Typ))
400 or else not Tagged_Type_Expansion
401 then
402 null;
404 -- Generate a class-wide membership test to ensure that the call's tag
405 -- argument denotes a type within the class. We must keep separate the
406 -- case in which the Result_Type of the constructor function is a tagged
407 -- type from the case in which it is an abstract interface because the
408 -- run-time subprogram required to check these cases differ (and have
409 -- one difference in their parameters profile).
411 -- Call CW_Membership if the Result_Type is a tagged type to look for
412 -- the tag in the table of ancestor tags.
414 elsif not Is_Interface (Result_Typ) then
415 declare
416 Obj_Tag_Node : Node_Id := New_Copy_Tree (Tag_Arg);
417 CW_Test_Node : Node_Id;
419 begin
420 Build_CW_Membership (Loc,
421 Obj_Tag_Node => Obj_Tag_Node,
422 Typ_Tag_Node =>
423 New_Occurrence_Of (
424 Node (First_Elmt (Access_Disp_Table (
425 Root_Type (Result_Typ)))), Loc),
426 Related_Nod => N,
427 New_Node => CW_Test_Node);
429 Insert_Action (N,
430 Make_Implicit_If_Statement (N,
431 Condition =>
432 Make_Op_Not (Loc, CW_Test_Node),
433 Then_Statements =>
434 New_List (Make_Raise_Statement (Loc,
435 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
436 end;
438 -- Call IW_Membership test if the Result_Type is an abstract interface
439 -- to look for the tag in the table of interface tags.
441 else
442 Insert_Action (N,
443 Make_Implicit_If_Statement (N,
444 Condition =>
445 Make_Op_Not (Loc,
446 Make_Function_Call (Loc,
447 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
448 Parameter_Associations => New_List (
449 Make_Attribute_Reference (Loc,
450 Prefix => New_Copy_Tree (Tag_Arg),
451 Attribute_Name => Name_Address),
453 New_Occurrence_Of (
454 Node (First_Elmt (Access_Disp_Table (
455 Root_Type (Result_Typ)))), Loc)))),
456 Then_Statements =>
457 New_List (
458 Make_Raise_Statement (Loc,
459 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
460 end if;
461 end Expand_Dispatching_Constructor_Call;
463 ---------------------------
464 -- Expand_Exception_Call --
465 ---------------------------
467 -- If the function call is not within an exception handler, then the call
468 -- is replaced by a null string. Otherwise the appropriate routine in
469 -- Ada.Exceptions is called passing the choice parameter specification
470 -- from the enclosing handler. If the enclosing handler lacks a choice
471 -- parameter, then one is supplied.
473 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
474 Loc : constant Source_Ptr := Sloc (N);
475 P : Node_Id;
476 E : Entity_Id;
478 begin
479 -- Climb up parents to see if we are in exception handler
481 P := Parent (N);
482 loop
483 -- Case of not in exception handler, replace by null string
485 if No (P) then
486 Rewrite (N,
487 Make_String_Literal (Loc,
488 Strval => ""));
489 exit;
491 -- Case of in exception handler
493 elsif Nkind (P) = N_Exception_Handler then
495 -- Handler cannot be used for a local raise, and furthermore, this
496 -- is a violation of the No_Exception_Propagation restriction.
498 Set_Local_Raise_Not_OK (P);
499 Check_Restriction (No_Exception_Propagation, N);
501 -- If no choice parameter present, then put one there. Note that
502 -- we do not need to put it on the entity chain, since no one will
503 -- be referencing it by normal visibility methods.
505 if No (Choice_Parameter (P)) then
506 E := Make_Temporary (Loc, 'E');
507 Set_Choice_Parameter (P, E);
508 Set_Ekind (E, E_Variable);
509 Set_Etype (E, RTE (RE_Exception_Occurrence));
510 Set_Scope (E, Current_Scope);
511 end if;
513 Rewrite (N,
514 Make_Function_Call (Loc,
515 Name => New_Occurrence_Of (RTE (Ent), Loc),
516 Parameter_Associations => New_List (
517 New_Occurrence_Of (Choice_Parameter (P), Loc))));
518 exit;
520 -- Keep climbing
522 else
523 P := Parent (P);
524 end if;
525 end loop;
527 Analyze_And_Resolve (N, Standard_String);
528 end Expand_Exception_Call;
530 ------------------------
531 -- Expand_Import_Call --
532 ------------------------
534 -- The function call must have a static string as its argument. We create
535 -- a dummy variable which uses this string as the external name in an
536 -- Import pragma. The result is then obtained as the address of this
537 -- dummy variable, converted to the appropriate target type.
539 procedure Expand_Import_Call (N : Node_Id) is
540 Loc : constant Source_Ptr := Sloc (N);
541 Ent : constant Entity_Id := Entity (Name (N));
542 Str : constant Node_Id := First_Actual (N);
543 Dum : constant Entity_Id := Make_Temporary (Loc, 'D');
545 begin
546 Insert_Actions (N, New_List (
547 Make_Object_Declaration (Loc,
548 Defining_Identifier => Dum,
549 Object_Definition =>
550 New_Occurrence_Of (Standard_Character, Loc)),
552 Make_Pragma (Loc,
553 Chars => Name_Import,
554 Pragma_Argument_Associations => New_List (
555 Make_Pragma_Argument_Association (Loc,
556 Expression => Make_Identifier (Loc, Name_Ada)),
558 Make_Pragma_Argument_Association (Loc,
559 Expression => Make_Identifier (Loc, Chars (Dum))),
561 Make_Pragma_Argument_Association (Loc,
562 Chars => Name_Link_Name,
563 Expression => Relocate_Node (Str))))));
565 Rewrite (N,
566 Unchecked_Convert_To (Etype (Ent),
567 Make_Attribute_Reference (Loc,
568 Prefix => Make_Identifier (Loc, Chars (Dum)),
569 Attribute_Name => Name_Address)));
571 Analyze_And_Resolve (N, Etype (Ent));
572 end Expand_Import_Call;
574 ---------------------------
575 -- Expand_Intrinsic_Call --
576 ---------------------------
578 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
579 Nam : Name_Id;
581 begin
582 -- If an external name is specified for the intrinsic, it is handled
583 -- by the back-end: leave the call node unchanged for now.
585 if Present (Interface_Name (E)) then
586 return;
587 end if;
589 -- If the intrinsic subprogram is generic, gets its original name
591 if Present (Parent (E))
592 and then Present (Generic_Parent (Parent (E)))
593 then
594 Nam := Chars (Generic_Parent (Parent (E)));
595 else
596 Nam := Chars (E);
597 end if;
599 if Nam = Name_Asm then
600 Expand_Asm_Call (N);
602 elsif Nam = Name_Divide then
603 Expand_Decimal_Divide_Call (N);
605 elsif Nam = Name_Exception_Information then
606 Expand_Exception_Call (N, RE_Exception_Information);
608 elsif Nam = Name_Exception_Message then
609 Expand_Exception_Call (N, RE_Exception_Message);
611 elsif Nam = Name_Exception_Name then
612 Expand_Exception_Call (N, RE_Exception_Name_Simple);
614 elsif Nam = Name_Generic_Dispatching_Constructor then
615 Expand_Dispatching_Constructor_Call (N);
617 elsif Nam_In (Nam, Name_Import_Address,
618 Name_Import_Largest_Value,
619 Name_Import_Value)
620 then
621 Expand_Import_Call (N);
623 elsif Nam = Name_Is_Negative then
624 Expand_Is_Negative (N);
626 elsif Nam = Name_Rotate_Left then
627 Expand_Shift (N, E, N_Op_Rotate_Left);
629 elsif Nam = Name_Rotate_Right then
630 Expand_Shift (N, E, N_Op_Rotate_Right);
632 elsif Nam = Name_Shift_Left then
633 Expand_Shift (N, E, N_Op_Shift_Left);
635 elsif Nam = Name_Shift_Right then
636 Expand_Shift (N, E, N_Op_Shift_Right);
638 elsif Nam = Name_Shift_Right_Arithmetic then
639 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
641 elsif Nam = Name_Unchecked_Conversion then
642 Expand_Unc_Conversion (N, E);
644 elsif Nam = Name_Unchecked_Deallocation then
645 Expand_Unc_Deallocation (N);
647 elsif Nam = Name_To_Address then
648 Expand_To_Address (N);
650 elsif Nam = Name_To_Pointer then
651 Expand_To_Pointer (N);
653 elsif Nam_In (Nam, Name_File,
654 Name_Line,
655 Name_Source_Location,
656 Name_Enclosing_Entity,
657 Name_Compilation_Date,
658 Name_Compilation_Time)
659 then
660 Expand_Source_Info (N, Nam);
662 -- If we have a renaming, expand the call to the original operation,
663 -- which must itself be intrinsic, since renaming requires matching
664 -- conventions and this has already been checked.
666 elsif Present (Alias (E)) then
667 Expand_Intrinsic_Call (N, Alias (E));
669 elsif Nkind (N) in N_Binary_Op then
670 Expand_Binary_Operator_Call (N);
672 -- The only other case is where an external name was specified, since
673 -- this is the only way that an otherwise unrecognized name could
674 -- escape the checking in Sem_Prag. Nothing needs to be done in such
675 -- a case, since we pass such a call to the back end unchanged.
677 else
678 null;
679 end if;
680 end Expand_Intrinsic_Call;
682 ------------------------
683 -- Expand_Is_Negative --
684 ------------------------
686 procedure Expand_Is_Negative (N : Node_Id) is
687 Loc : constant Source_Ptr := Sloc (N);
688 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
690 begin
692 -- We replace the function call by the following expression
694 -- if Opnd < 0.0 then
695 -- True
696 -- else
697 -- if Opnd > 0.0 then
698 -- False;
699 -- else
700 -- Float_Unsigned!(Float (Opnd)) /= 0
701 -- end if;
702 -- end if;
704 Rewrite (N,
705 Make_If_Expression (Loc,
706 Expressions => New_List (
707 Make_Op_Lt (Loc,
708 Left_Opnd => Duplicate_Subexpr (Opnd),
709 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
711 New_Occurrence_Of (Standard_True, Loc),
713 Make_If_Expression (Loc,
714 Expressions => New_List (
715 Make_Op_Gt (Loc,
716 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
717 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
719 New_Occurrence_Of (Standard_False, Loc),
721 Make_Op_Ne (Loc,
722 Left_Opnd =>
723 Unchecked_Convert_To
724 (RTE (RE_Float_Unsigned),
725 Convert_To
726 (Standard_Float,
727 Duplicate_Subexpr_No_Checks (Opnd))),
728 Right_Opnd =>
729 Make_Integer_Literal (Loc, 0)))))));
731 Analyze_And_Resolve (N, Standard_Boolean);
732 end Expand_Is_Negative;
734 ------------------
735 -- Expand_Shift --
736 ------------------
738 -- This procedure is used to convert a call to a shift function to the
739 -- corresponding operator node. This conversion is not done by the usual
740 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
741 -- operator nodes, because shifts are not predefined operators.
743 -- As a result, whenever a shift is used in the source program, it will
744 -- remain as a call until converted by this routine to the operator node
745 -- form which the back end is expecting to see.
747 -- Note: it is possible for the expander to generate shift operator nodes
748 -- directly, which will be analyzed in the normal manner by calling Analyze
749 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
751 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
752 Entyp : constant Entity_Id := Etype (E);
753 Left : constant Node_Id := First_Actual (N);
754 Loc : constant Source_Ptr := Sloc (N);
755 Right : constant Node_Id := Next_Actual (Left);
756 Ltyp : constant Node_Id := Etype (Left);
757 Rtyp : constant Node_Id := Etype (Right);
758 Typ : constant Entity_Id := Etype (N);
759 Snode : Node_Id;
761 begin
762 Snode := New_Node (K, Loc);
763 Set_Right_Opnd (Snode, Relocate_Node (Right));
764 Set_Chars (Snode, Chars (E));
765 Set_Etype (Snode, Base_Type (Entyp));
766 Set_Entity (Snode, E);
768 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
769 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
770 then
771 Set_Shift_Count_OK (Snode, True);
772 end if;
774 if Typ = Entyp then
776 -- Note that we don't call Analyze and Resolve on this node, because
777 -- it already got analyzed and resolved when it was a function call.
779 Set_Left_Opnd (Snode, Relocate_Node (Left));
780 Rewrite (N, Snode);
781 Set_Analyzed (N);
783 -- However, we do call the expander, so that the expansion for
784 -- rotates and shift_right_arithmetic happens if Modify_Tree_For_C
785 -- is set.
787 if Expander_Active then
788 Expand (N);
789 end if;
791 else
792 -- If the context type is not the type of the operator, it is an
793 -- inherited operator for a derived type. Wrap the node in a
794 -- conversion so that it is type-consistent for possible further
795 -- expansion (e.g. within a lock-free protected type).
797 Set_Left_Opnd (Snode,
798 Unchecked_Convert_To (Base_Type (Entyp), Relocate_Node (Left)));
799 Rewrite (N, Unchecked_Convert_To (Typ, Snode));
801 -- Analyze and resolve result formed by conversion to target type
803 Analyze_And_Resolve (N, Typ);
804 end if;
805 end Expand_Shift;
807 ------------------------
808 -- Expand_Source_Info --
809 ------------------------
811 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
812 Loc : constant Source_Ptr := Sloc (N);
813 Ent : Entity_Id;
815 begin
816 -- Integer cases
818 if Nam = Name_Line then
819 Rewrite (N,
820 Make_Integer_Literal (Loc,
821 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
822 Analyze_And_Resolve (N, Standard_Positive);
824 -- String cases
826 else
827 Name_Len := 0;
829 case Nam is
830 when Name_File =>
831 Get_Decoded_Name_String
832 (Reference_Name (Get_Source_File_Index (Loc)));
834 when Name_Source_Location =>
835 Build_Location_String (Loc);
837 when Name_Enclosing_Entity =>
839 -- Skip enclosing blocks to reach enclosing unit
841 Ent := Current_Scope;
842 while Present (Ent) loop
843 exit when Ekind (Ent) /= E_Block
844 and then Ekind (Ent) /= E_Loop;
845 Ent := Scope (Ent);
846 end loop;
848 -- Ent now points to the relevant defining entity
850 Write_Entity_Name (Ent);
852 when Name_Compilation_Date =>
853 declare
854 subtype S13 is String (1 .. 3);
855 Months : constant array (1 .. 12) of S13 :=
856 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
857 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
859 M1 : constant Character := Opt.Compilation_Time (6);
860 M2 : constant Character := Opt.Compilation_Time (7);
862 MM : constant Natural range 1 .. 12 :=
863 (Character'Pos (M1) - Character'Pos ('0')) * 10 +
864 (Character'Pos (M2) - Character'Pos ('0'));
866 begin
867 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
869 Name_Buffer (1 .. 3) := Months (MM);
870 Name_Buffer (4) := ' ';
871 Name_Buffer (5 .. 6) := Opt.Compilation_Time (9 .. 10);
872 Name_Buffer (7) := ' ';
873 Name_Buffer (8 .. 11) := Opt.Compilation_Time (1 .. 4);
874 Name_Len := 11;
875 end;
877 when Name_Compilation_Time =>
878 Name_Buffer (1 .. 8) := Opt.Compilation_Time (12 .. 19);
879 Name_Len := 8;
881 when others =>
882 raise Program_Error;
883 end case;
885 Rewrite (N,
886 Make_String_Literal (Loc,
887 Strval => String_From_Name_Buffer));
888 Analyze_And_Resolve (N, Standard_String);
889 end if;
891 Set_Is_Static_Expression (N);
892 end Expand_Source_Info;
894 ---------------------------
895 -- Expand_Unc_Conversion --
896 ---------------------------
898 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
899 Func : constant Entity_Id := Entity (Name (N));
900 Conv : Node_Id;
901 Ftyp : Entity_Id;
902 Ttyp : Entity_Id;
904 begin
905 -- Rewrite as unchecked conversion node. Note that we must convert
906 -- the operand to the formal type of the input parameter of the
907 -- function, so that the resulting N_Unchecked_Type_Conversion
908 -- call indicates the correct types for Gigi.
910 -- Right now, we only do this if a scalar type is involved. It is
911 -- not clear if it is needed in other cases. If we do attempt to
912 -- do the conversion unconditionally, it crashes 3411-018. To be
913 -- investigated further ???
915 Conv := Relocate_Node (First_Actual (N));
916 Ftyp := Etype (First_Formal (Func));
918 if Is_Scalar_Type (Ftyp) then
919 Conv := Convert_To (Ftyp, Conv);
920 Set_Parent (Conv, N);
921 Analyze_And_Resolve (Conv);
922 end if;
924 -- The instantiation of Unchecked_Conversion creates a wrapper package,
925 -- and the target type is declared as a subtype of the actual. Recover
926 -- the actual, which is the subtype indic. in the subtype declaration
927 -- for the target type. This is semantically correct, and avoids
928 -- anomalies with access subtypes. For entities, leave type as is.
930 -- We do the analysis here, because we do not want the compiler
931 -- to try to optimize or otherwise reorganize the unchecked
932 -- conversion node.
934 Ttyp := Etype (E);
936 if Is_Entity_Name (Conv) then
937 null;
939 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
940 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
942 elsif Is_Itype (Ttyp) then
943 Ttyp :=
944 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
945 else
946 raise Program_Error;
947 end if;
949 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
950 Set_Etype (N, Ttyp);
951 Set_Analyzed (N);
953 if Nkind (N) = N_Unchecked_Type_Conversion then
954 Expand_N_Unchecked_Type_Conversion (N);
955 end if;
956 end Expand_Unc_Conversion;
958 -----------------------------
959 -- Expand_Unc_Deallocation --
960 -----------------------------
962 -- Generate the following Code :
964 -- if Arg /= null then
965 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
966 -- Free (Arg);
967 -- Arg := Null;
968 -- end if;
970 -- For a task, we also generate a call to Free_Task to ensure that the
971 -- task itself is freed if it is terminated, ditto for a simple protected
972 -- object, with a call to Finalize_Protection. For composite types that
973 -- have tasks or simple protected objects as components, we traverse the
974 -- structures to find and terminate those components.
976 procedure Expand_Unc_Deallocation (N : Node_Id) is
977 Arg : constant Node_Id := First_Actual (N);
978 Loc : constant Source_Ptr := Sloc (N);
979 Typ : constant Entity_Id := Etype (Arg);
980 Desig_T : constant Entity_Id := Designated_Type (Typ);
981 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
982 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
983 Stmts : constant List_Id := New_List;
984 Needs_Fin : constant Boolean := Needs_Finalization (Desig_T);
986 Finalizer_Data : Finalization_Exception_Data;
988 Blk : Node_Id := Empty;
989 Blk_Id : Entity_Id;
990 Deref : Node_Id;
991 Final_Code : List_Id;
992 Free_Arg : Node_Id;
993 Free_Node : Node_Id;
994 Gen_Code : Node_Id;
996 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
997 -- This captures whether we know the argument to be non-null so that
998 -- we can avoid the test. The reason that we need to capture this is
999 -- that we analyze some generated statements before properly attaching
1000 -- them to the tree, and that can disturb current value settings.
1002 Dummy : Entity_Id;
1003 -- This variable captures an unused dummy internal entity, see the
1004 -- comment associated with its use.
1006 begin
1007 -- Nothing to do if we know the argument is null
1009 if Known_Null (N) then
1010 return;
1011 end if;
1013 -- Processing for pointer to controlled type
1015 if Needs_Fin then
1016 Deref :=
1017 Make_Explicit_Dereference (Loc,
1018 Prefix => Duplicate_Subexpr_No_Checks (Arg));
1020 -- If the type is tagged, then we must force dispatching on the
1021 -- finalization call because the designated type may not be the
1022 -- actual type of the object.
1024 if Is_Tagged_Type (Desig_T)
1025 and then not Is_Class_Wide_Type (Desig_T)
1026 then
1027 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
1029 elsif not Is_Tagged_Type (Desig_T) then
1031 -- Set type of result, to force a conversion when needed (see
1032 -- exp_ch7, Convert_View), given that Deep_Finalize may be
1033 -- inherited from the parent type, and we need the type of the
1034 -- expression to see whether the conversion is in fact needed.
1036 Set_Etype (Deref, Desig_T);
1037 end if;
1039 -- The finalization call is expanded wrapped in a block to catch any
1040 -- possible exception. If an exception does occur, then Program_Error
1041 -- must be raised following the freeing of the object and its removal
1042 -- from the finalization collection's list. We set a flag to record
1043 -- that an exception was raised, and save its occurrence for use in
1044 -- the later raise.
1046 -- Generate:
1047 -- Abort : constant Boolean :=
1048 -- Exception_Occurrence (Get_Current_Excep.all.all) =
1049 -- Standard'Abort_Signal'Identity;
1050 -- <or>
1051 -- Abort : constant Boolean := False; -- no abort
1053 -- E : Exception_Occurrence;
1054 -- Raised : Boolean := False;
1056 -- begin
1057 -- [Deep_]Finalize (Obj);
1058 -- exception
1059 -- when others =>
1060 -- Raised := True;
1061 -- Save_Occurrence (E, Get_Current_Excep.all.all);
1062 -- end;
1064 Build_Object_Declarations (Finalizer_Data, Stmts, Loc);
1066 Final_Code := New_List (
1067 Make_Block_Statement (Loc,
1068 Handled_Statement_Sequence =>
1069 Make_Handled_Sequence_Of_Statements (Loc,
1070 Statements => New_List (
1071 Make_Final_Call (Obj_Ref => Deref, Typ => Desig_T)),
1072 Exception_Handlers => New_List (
1073 Build_Exception_Handler (Finalizer_Data)))));
1075 -- For .NET/JVM, detach the object from the containing finalization
1076 -- collection before finalizing it.
1078 if VM_Target /= No_VM and then Is_Controlled (Desig_T) then
1079 Prepend_To (Final_Code,
1080 Make_Detach_Call (New_Copy_Tree (Arg)));
1081 end if;
1083 -- If aborts are allowed, then the finalization code must be
1084 -- protected by an abort defer/undefer pair.
1086 if Abort_Allowed then
1087 Prepend_To (Final_Code, Build_Runtime_Call (Loc, RE_Abort_Defer));
1089 declare
1090 AUD : constant Entity_Id := RTE (RE_Abort_Undefer_Direct);
1092 begin
1093 Blk :=
1094 Make_Block_Statement (Loc,
1095 Handled_Statement_Sequence =>
1096 Make_Handled_Sequence_Of_Statements (Loc,
1097 Statements => Final_Code,
1098 At_End_Proc => New_Occurrence_Of (AUD, Loc)));
1100 -- Present the Abort_Undefer_Direct function to the backend so
1101 -- that it can inline the call to the function.
1103 Add_Inlined_Body (AUD, N);
1104 end;
1106 Add_Block_Identifier (Blk, Blk_Id);
1108 Append (Blk, Stmts);
1110 else
1111 -- Generate a dummy entity to ensure that the internal symbols are
1112 -- in sync when a unit is compiled with and without aborts.
1114 Dummy := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
1115 Append_List_To (Stmts, Final_Code);
1116 end if;
1117 end if;
1119 -- For a task type, call Free_Task before freeing the ATCB
1121 if Is_Task_Type (Desig_T) then
1123 -- We used to detect the case of Abort followed by a Free here,
1124 -- because the Free wouldn't actually free if it happens before
1125 -- the aborted task actually terminates. The warning was removed,
1126 -- because Free now works properly (the task will be freed once
1127 -- it terminates).
1129 Append_To
1130 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
1132 -- For composite types that contain tasks, recurse over the structure
1133 -- to build the selectors for the task subcomponents.
1135 elsif Has_Task (Desig_T) then
1136 if Is_Record_Type (Desig_T) then
1137 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1139 elsif Is_Array_Type (Desig_T) then
1140 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1141 end if;
1142 end if;
1144 -- Same for simple protected types. Eventually call Finalize_Protection
1145 -- before freeing the PO for each protected component.
1147 if Is_Simple_Protected_Type (Desig_T) then
1148 Append_To (Stmts,
1149 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
1151 elsif Has_Simple_Protected_Object (Desig_T) then
1152 if Is_Record_Type (Desig_T) then
1153 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1154 elsif Is_Array_Type (Desig_T) then
1155 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1156 end if;
1157 end if;
1159 -- Normal processing for non-controlled types. The argument to free is
1160 -- a renaming rather than a constant to ensure that the original context
1161 -- is always set to null after the deallocation takes place.
1163 Free_Arg := Duplicate_Subexpr_No_Checks (Arg, Renaming_Req => True);
1164 Free_Node := Make_Free_Statement (Loc, Empty);
1165 Append_To (Stmts, Free_Node);
1166 Set_Storage_Pool (Free_Node, Pool);
1168 -- Attach to tree before analysis of generated subtypes below
1170 Set_Parent (Stmts, Parent (N));
1172 -- Deal with storage pool
1174 if Present (Pool) then
1176 -- Freeing the secondary stack is meaningless
1178 if Is_RTE (Pool, RE_SS_Pool) then
1179 null;
1181 -- If the pool object is of a simple storage pool type, then attempt
1182 -- to locate the type's Deallocate procedure, if any, and set the
1183 -- free operation's procedure to call. If the type doesn't have a
1184 -- Deallocate (which is allowed), then the actual will simply be set
1185 -- to null.
1187 elsif Present (Get_Rep_Pragma
1188 (Etype (Pool), Name_Simple_Storage_Pool_Type))
1189 then
1190 declare
1191 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
1192 Dealloc_Op : Entity_Id;
1193 begin
1194 Dealloc_Op := Get_Name_Entity_Id (Name_Deallocate);
1195 while Present (Dealloc_Op) loop
1196 if Scope (Dealloc_Op) = Scope (Pool_Type)
1197 and then Present (First_Formal (Dealloc_Op))
1198 and then Etype (First_Formal (Dealloc_Op)) = Pool_Type
1199 then
1200 Set_Procedure_To_Call (Free_Node, Dealloc_Op);
1201 exit;
1202 else
1203 Dealloc_Op := Homonym (Dealloc_Op);
1204 end if;
1205 end loop;
1206 end;
1208 -- Case of a class-wide pool type: make a dispatching call to
1209 -- Deallocate through the class-wide Deallocate_Any.
1211 elsif Is_Class_Wide_Type (Etype (Pool)) then
1212 Set_Procedure_To_Call (Free_Node, RTE (RE_Deallocate_Any));
1214 -- Case of a specific pool type: make a statically bound call
1216 else
1217 Set_Procedure_To_Call (Free_Node,
1218 Find_Prim_Op (Etype (Pool), Name_Deallocate));
1219 end if;
1220 end if;
1222 if Present (Procedure_To_Call (Free_Node)) then
1224 -- For all cases of a Deallocate call, the back-end needs to be able
1225 -- to compute the size of the object being freed. This may require
1226 -- some adjustments for objects of dynamic size.
1228 -- If the type is class wide, we generate an implicit type with the
1229 -- right dynamic size, so that the deallocate call gets the right
1230 -- size parameter computed by GIGI. Same for an access to
1231 -- unconstrained packed array.
1233 if Is_Class_Wide_Type (Desig_T)
1234 or else
1235 (Is_Array_Type (Desig_T)
1236 and then not Is_Constrained (Desig_T)
1237 and then Is_Packed (Desig_T))
1238 then
1239 declare
1240 Deref : constant Node_Id :=
1241 Make_Explicit_Dereference (Loc,
1242 Duplicate_Subexpr_No_Checks (Arg));
1243 D_Subtyp : Node_Id;
1244 D_Type : Entity_Id;
1246 begin
1247 -- Perform minor decoration as it is needed by the side effect
1248 -- removal mechanism.
1250 Set_Etype (Deref, Desig_T);
1251 Set_Parent (Deref, Free_Node);
1252 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
1254 if Nkind (D_Subtyp) in N_Has_Entity then
1255 D_Type := Entity (D_Subtyp);
1257 else
1258 D_Type := Make_Temporary (Loc, 'A');
1259 Insert_Action (Deref,
1260 Make_Subtype_Declaration (Loc,
1261 Defining_Identifier => D_Type,
1262 Subtype_Indication => D_Subtyp));
1263 end if;
1265 -- Force freezing at the point of the dereference. For the
1266 -- class wide case, this avoids having the subtype frozen
1267 -- before the equivalent type.
1269 Freeze_Itype (D_Type, Deref);
1271 Set_Actual_Designated_Subtype (Free_Node, D_Type);
1272 end;
1274 end if;
1275 end if;
1277 -- Ada 2005 (AI-251): In case of abstract interface type we must
1278 -- displace the pointer to reference the base of the object to
1279 -- deallocate its memory, unless we're targetting a VM, in which case
1280 -- no special processing is required.
1282 -- Generate:
1283 -- free (Base_Address (Obj_Ptr))
1285 if Is_Interface (Directly_Designated_Type (Typ))
1286 and then Tagged_Type_Expansion
1287 then
1288 Set_Expression (Free_Node,
1289 Unchecked_Convert_To (Typ,
1290 Make_Function_Call (Loc,
1291 Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc),
1292 Parameter_Associations => New_List (
1293 Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1295 -- Generate:
1296 -- free (Obj_Ptr)
1298 else
1299 Set_Expression (Free_Node, Free_Arg);
1300 end if;
1302 -- Only remaining step is to set result to null, or generate a raise of
1303 -- Constraint_Error if the target object is "not null".
1305 if Can_Never_Be_Null (Etype (Arg)) then
1306 Append_To (Stmts,
1307 Make_Raise_Constraint_Error (Loc,
1308 Reason => CE_Access_Check_Failed));
1310 else
1311 declare
1312 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1313 begin
1314 Set_Assignment_OK (Lhs);
1315 Append_To (Stmts,
1316 Make_Assignment_Statement (Loc,
1317 Name => Lhs,
1318 Expression => Make_Null (Loc)));
1319 end;
1320 end if;
1322 -- Generate a test of whether any earlier finalization raised an
1323 -- exception, and in that case raise Program_Error with the previous
1324 -- exception occurrence.
1326 -- Generate:
1327 -- if Raised and then not Abort then
1328 -- raise Program_Error; -- for .NET and
1329 -- -- restricted RTS
1330 -- <or>
1331 -- Raise_From_Controlled_Operation (E); -- all other cases
1332 -- end if;
1334 if Needs_Fin then
1335 Append_To (Stmts, Build_Raise_Statement (Finalizer_Data));
1336 end if;
1338 -- If we know the argument is non-null, then make a block statement
1339 -- that contains the required statements, no need for a test.
1341 if Arg_Known_Non_Null then
1342 Gen_Code :=
1343 Make_Block_Statement (Loc,
1344 Handled_Statement_Sequence =>
1345 Make_Handled_Sequence_Of_Statements (Loc,
1346 Statements => Stmts));
1348 -- If the argument may be null, wrap the statements inside an IF that
1349 -- does an explicit test to exclude the null case.
1351 else
1352 Gen_Code :=
1353 Make_Implicit_If_Statement (N,
1354 Condition =>
1355 Make_Op_Ne (Loc,
1356 Left_Opnd => Duplicate_Subexpr (Arg),
1357 Right_Opnd => Make_Null (Loc)),
1358 Then_Statements => Stmts);
1359 end if;
1361 -- Rewrite the call
1363 Rewrite (N, Gen_Code);
1364 Analyze (N);
1366 -- If we generated a block with an At_End_Proc, expand the exception
1367 -- handler. We need to wait until after everything else is analyzed.
1369 if Present (Blk) then
1370 Expand_At_End_Handler
1371 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
1372 end if;
1373 end Expand_Unc_Deallocation;
1375 -----------------------
1376 -- Expand_To_Address --
1377 -----------------------
1379 procedure Expand_To_Address (N : Node_Id) is
1380 Loc : constant Source_Ptr := Sloc (N);
1381 Arg : constant Node_Id := First_Actual (N);
1382 Obj : Node_Id;
1384 begin
1385 Remove_Side_Effects (Arg);
1387 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1389 Rewrite (N,
1390 Make_If_Expression (Loc,
1391 Expressions => New_List (
1392 Make_Op_Eq (Loc,
1393 Left_Opnd => New_Copy_Tree (Arg),
1394 Right_Opnd => Make_Null (Loc)),
1395 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1396 Make_Attribute_Reference (Loc,
1397 Prefix => Obj,
1398 Attribute_Name => Name_Address))));
1400 Analyze_And_Resolve (N, RTE (RE_Address));
1401 end Expand_To_Address;
1403 -----------------------
1404 -- Expand_To_Pointer --
1405 -----------------------
1407 procedure Expand_To_Pointer (N : Node_Id) is
1408 Arg : constant Node_Id := First_Actual (N);
1410 begin
1411 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1412 Analyze (N);
1413 end Expand_To_Pointer;
1415 -----------------------
1416 -- Write_Entity_Name --
1417 -----------------------
1419 procedure Write_Entity_Name (E : Entity_Id) is
1421 procedure Write_Entity_Name_Inner (E : Entity_Id);
1422 -- Inner recursive routine, keep outer routine non-recursive to ease
1423 -- debugging when we get strange results from this routine.
1425 -----------------------------
1426 -- Write_Entity_Name_Inner --
1427 -----------------------------
1429 procedure Write_Entity_Name_Inner (E : Entity_Id) is
1430 begin
1431 -- If entity has an internal name, skip by it, and print its scope.
1432 -- Note that Is_Internal_Name destroys Name_Buffer, hence the save
1433 -- and restore since we depend on its current contents. Note that
1434 -- we strip a final R from the name before the test, this is needed
1435 -- for some cases of instantiations.
1437 declare
1438 Save_NB : constant String := Name_Buffer (1 .. Name_Len);
1439 Save_NL : constant Natural := Name_Len;
1440 Iname : Boolean;
1442 begin
1443 Get_Name_String (Chars (E));
1445 if Name_Buffer (Name_Len) = 'R' then
1446 Name_Len := Name_Len - 1;
1447 end if;
1449 Iname := Is_Internal_Name;
1451 Name_Buffer (1 .. Save_NL) := Save_NB;
1452 Name_Len := Save_NL;
1454 if Iname then
1455 Write_Entity_Name_Inner (Scope (E));
1456 return;
1457 end if;
1458 end;
1460 -- Just print entity name if its scope is at the outer level
1462 if Scope (E) = Standard_Standard then
1463 null;
1465 -- If scope comes from source, write scope and entity
1467 elsif Comes_From_Source (Scope (E)) then
1468 Write_Entity_Name (Scope (E));
1469 Add_Char_To_Name_Buffer ('.');
1471 -- If in wrapper package skip past it
1473 elsif Is_Wrapper_Package (Scope (E)) then
1474 Write_Entity_Name (Scope (Scope (E)));
1475 Add_Char_To_Name_Buffer ('.');
1477 -- Otherwise nothing to output (happens in unnamed block statements)
1479 else
1480 null;
1481 end if;
1483 -- Output the name
1485 declare
1486 Save_NB : constant String := Name_Buffer (1 .. Name_Len);
1487 Save_NL : constant Natural := Name_Len;
1489 begin
1490 Get_Unqualified_Decoded_Name_String (Chars (E));
1492 -- Remove trailing upper case letters from the name (useful for
1493 -- dealing with some cases of internal names generated in the case
1494 -- of references from within a generic.
1496 while Name_Len > 1
1497 and then Name_Buffer (Name_Len) in 'A' .. 'Z'
1498 loop
1499 Name_Len := Name_Len - 1;
1500 end loop;
1502 -- Adjust casing appropriately (gets name from source if possible)
1504 Adjust_Name_Case (Sloc (E));
1506 -- Append to original entry value of Name_Buffer
1508 Name_Buffer (Save_NL + 1 .. Save_NL + Name_Len) :=
1509 Name_Buffer (1 .. Name_Len);
1510 Name_Buffer (1 .. Save_NL) := Save_NB;
1511 Name_Len := Save_NL + Name_Len;
1512 end;
1513 end Write_Entity_Name_Inner;
1515 -- Start of processing for Write_Entity_Name
1517 begin
1518 Write_Entity_Name_Inner (E);
1519 end Write_Entity_Name;
1520 end Exp_Intr;