[gcc]
[official-gcc.git] / gcc / ada / exp_intr.adb
blobfde0617aa834cbe8ba6b1f8afaf5d61d7fbbdd8a
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-2016, 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 Tbuild; use Tbuild;
58 with Uintp; use Uintp;
59 with Urealp; use Urealp;
61 package body Exp_Intr is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 procedure Expand_Binary_Operator_Call (N : Node_Id);
68 -- Expand a call to an intrinsic arithmetic operator when the operand
69 -- types or sizes are not identical.
71 procedure Expand_Is_Negative (N : Node_Id);
72 -- Expand a call to the intrinsic Is_Negative function
74 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
75 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
76 -- into a dispatching call to the actual subprogram associated with the
77 -- Constructor formal subprogram, passing it the Parameters actual of
78 -- the call to the instantiation and dispatching based on call's Tag
79 -- parameter.
81 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
82 -- Expand a call to Exception_Information/Message/Name. The first
83 -- parameter, N, is the node for the function call, and Ent is the
84 -- entity for the corresponding routine in the Ada.Exceptions package.
86 procedure Expand_Import_Call (N : Node_Id);
87 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
88 -- N is the node for the function call.
90 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
91 -- Expand an intrinsic shift operation, N and E are from the call to
92 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
93 -- K is the kind for the shift node
95 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
96 -- Expand a call to an instantiation of Unchecked_Conversion into a node
97 -- N_Unchecked_Type_Conversion.
99 procedure Expand_Unc_Deallocation (N : Node_Id);
100 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
101 -- N_Free_Statement and appropriate context.
103 procedure Expand_To_Address (N : Node_Id);
104 procedure Expand_To_Pointer (N : Node_Id);
105 -- Expand a call to corresponding function, declared in an instance of
106 -- System.Address_To_Access_Conversions.
108 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
109 -- Rewrite the node as the appropriate string literal or positive
110 -- constant. Nam is the name of one of the intrinsics declared in
111 -- GNAT.Source_Info; see g-souinf.ads for documentation of these
112 -- intrinsics.
114 procedure Append_Entity_Name (Buf : in out Bounded_String; E : Entity_Id);
115 -- Recursive procedure to construct string for qualified name of enclosing
116 -- program unit. The qualification stops at an enclosing scope has no
117 -- source name (block or loop). If entity is a subprogram instance, skip
118 -- enclosing wrapper package. The name is appended to Buf.
120 ---------------------
121 -- Add_Source_Info --
122 ---------------------
124 procedure Add_Source_Info
125 (Buf : in out Bounded_String;
126 Loc : Source_Ptr;
127 Nam : Name_Id)
129 begin
130 case Nam is
131 when Name_Line =>
132 Append (Buf, Nat (Get_Logical_Line_Number (Loc)));
134 when Name_File =>
135 Append_Decoded (Buf, Reference_Name (Get_Source_File_Index (Loc)));
137 when Name_Source_Location =>
138 Build_Location_String (Buf, Loc);
140 when Name_Enclosing_Entity =>
142 -- Skip enclosing blocks to reach enclosing unit
144 declare
145 Ent : Entity_Id := Current_Scope;
146 begin
147 while Present (Ent) loop
148 exit when not Ekind_In (Ent, E_Block, E_Loop);
149 Ent := Scope (Ent);
150 end loop;
152 -- Ent now points to the relevant defining entity
154 Append_Entity_Name (Buf, Ent);
155 end;
157 when Name_Compilation_ISO_Date =>
158 Append (Buf, Opt.Compilation_Time (1 .. 10));
160 when Name_Compilation_Date =>
161 declare
162 subtype S13 is String (1 .. 3);
163 Months : constant array (1 .. 12) of S13 :=
164 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
165 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
167 M1 : constant Character := Opt.Compilation_Time (6);
168 M2 : constant Character := Opt.Compilation_Time (7);
170 MM : constant Natural range 1 .. 12 :=
171 (Character'Pos (M1) - Character'Pos ('0')) * 10 +
172 (Character'Pos (M2) - Character'Pos ('0'));
174 begin
175 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
177 Append (Buf, Months (MM));
178 Append (Buf, ' ');
179 Append (Buf, Opt.Compilation_Time (9 .. 10));
180 Append (Buf, ' ');
181 Append (Buf, Opt.Compilation_Time (1 .. 4));
182 end;
184 when Name_Compilation_Time =>
185 Append (Buf, Opt.Compilation_Time (12 .. 19));
187 when others =>
188 raise Program_Error;
189 end case;
190 end Add_Source_Info;
192 -----------------------
193 -- Append_Entity_Name --
194 -----------------------
196 procedure Append_Entity_Name (Buf : in out Bounded_String; E : Entity_Id) is
197 Temp : Bounded_String;
199 procedure Inner (E : Entity_Id);
200 -- Inner recursive routine, keep outer routine nonrecursive to ease
201 -- debugging when we get strange results from this routine.
203 -----------
204 -- Inner --
205 -----------
207 procedure Inner (E : Entity_Id) is
208 begin
209 -- If entity has an internal name, skip by it, and print its scope.
210 -- Note that we strip a final R from the name before the test; this
211 -- is needed for some cases of instantiations.
213 declare
214 E_Name : Bounded_String;
216 begin
217 Append (E_Name, Chars (E));
219 if E_Name.Chars (E_Name.Length) = 'R' then
220 E_Name.Length := E_Name.Length - 1;
221 end if;
223 if Is_Internal_Name (E_Name) then
224 Inner (Scope (E));
225 return;
226 end if;
227 end;
229 -- Just print entity name if its scope is at the outer level
231 if Scope (E) = Standard_Standard then
232 null;
234 -- If scope comes from source, write scope and entity
236 elsif Comes_From_Source (Scope (E)) then
237 Append_Entity_Name (Temp, Scope (E));
238 Append (Temp, '.');
240 -- If in wrapper package skip past it
242 elsif Is_Wrapper_Package (Scope (E)) then
243 Append_Entity_Name (Temp, Scope (Scope (E)));
244 Append (Temp, '.');
246 -- Otherwise nothing to output (happens in unnamed block statements)
248 else
249 null;
250 end if;
252 -- Output the name
254 declare
255 E_Name : Bounded_String;
257 begin
258 Append_Unqualified_Decoded (E_Name, Chars (E));
260 -- Remove trailing upper-case letters from the name (useful for
261 -- dealing with some cases of internal names generated in the case
262 -- of references from within a generic).
264 while E_Name.Length > 1
265 and then E_Name.Chars (E_Name.Length) in 'A' .. 'Z'
266 loop
267 E_Name.Length := E_Name.Length - 1;
268 end loop;
270 -- Adjust casing appropriately (gets name from source if possible)
272 Adjust_Name_Case (E_Name, Sloc (E));
273 Append (Temp, E_Name);
274 end;
275 end Inner;
277 -- Start of processing for Append_Entity_Name
279 begin
280 Inner (E);
281 Append (Buf, Temp);
282 end Append_Entity_Name;
284 ---------------------------------
285 -- Expand_Binary_Operator_Call --
286 ---------------------------------
288 procedure Expand_Binary_Operator_Call (N : Node_Id) is
289 T1 : constant Entity_Id := Underlying_Type (Etype (Left_Opnd (N)));
290 T2 : constant Entity_Id := Underlying_Type (Etype (Right_Opnd (N)));
291 TR : constant Entity_Id := Etype (N);
292 T3 : Entity_Id;
293 Res : Node_Id;
295 Siz : constant Uint := UI_Max (RM_Size (T1), RM_Size (T2));
296 -- Maximum of operand sizes
298 begin
299 -- Nothing to do if the operands have the same modular type
301 if Base_Type (T1) = Base_Type (T2)
302 and then Is_Modular_Integer_Type (T1)
303 then
304 return;
305 end if;
307 -- Use Unsigned_32 for sizes of 32 or below, else Unsigned_64
309 if Siz > 32 then
310 T3 := RTE (RE_Unsigned_64);
311 else
312 T3 := RTE (RE_Unsigned_32);
313 end if;
315 -- Copy operator node, and reset type and entity fields, for
316 -- subsequent reanalysis.
318 Res := New_Copy (N);
319 Set_Etype (Res, T3);
321 case Nkind (N) is
322 when N_Op_And => Set_Entity (Res, Standard_Op_And);
323 when N_Op_Or => Set_Entity (Res, Standard_Op_Or);
324 when N_Op_Xor => Set_Entity (Res, Standard_Op_Xor);
325 when others => raise Program_Error;
326 end case;
328 -- Convert operands to large enough intermediate type
330 Set_Left_Opnd (Res,
331 Unchecked_Convert_To (T3, Relocate_Node (Left_Opnd (N))));
332 Set_Right_Opnd (Res,
333 Unchecked_Convert_To (T3, Relocate_Node (Right_Opnd (N))));
335 -- Analyze and resolve result formed by conversion to target type
337 Rewrite (N, Unchecked_Convert_To (TR, Res));
338 Analyze_And_Resolve (N, TR);
339 end Expand_Binary_Operator_Call;
341 -----------------------------------------
342 -- Expand_Dispatching_Constructor_Call --
343 -----------------------------------------
345 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
346 -- of the form:
348 -- GDC_Instance (The_Tag, Parameters'Access)
350 -- to a class-wide conversion of a dispatching call to the actual
351 -- associated with the formal subprogram Construct, designating The_Tag
352 -- as the controlling tag of the call:
354 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
356 -- which will eventually be expanded to the following:
358 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
360 -- A class-wide membership test is also generated, preceding the call, to
361 -- ensure that the controlling tag denotes a type in T'Class.
363 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
364 Loc : constant Source_Ptr := Sloc (N);
365 Tag_Arg : constant Node_Id := First_Actual (N);
366 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
367 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
368 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
369 Act_Rename : Node_Id;
370 Act_Constr : Entity_Id;
371 Iface_Tag : Node_Id := Empty;
372 Cnstr_Call : Node_Id;
373 Result_Typ : Entity_Id;
375 begin
376 -- Remove side effects from tag argument early, before rewriting
377 -- the dispatching constructor call, as Remove_Side_Effects relies
378 -- on Tag_Arg's Parent link properly attached to the tree (once the
379 -- call is rewritten, the Parent is inconsistent as it points to the
380 -- rewritten node, which is not the syntactic parent of the Tag_Arg
381 -- anymore).
383 Remove_Side_Effects (Tag_Arg);
385 -- Check that we have a proper tag
387 Insert_Action (N,
388 Make_Implicit_If_Statement (N,
389 Condition => Make_Op_Eq (Loc,
390 Left_Opnd => New_Copy_Tree (Tag_Arg),
391 Right_Opnd => New_Occurrence_Of (RTE (RE_No_Tag), Loc)),
393 Then_Statements => New_List (
394 Make_Raise_Statement (Loc,
395 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
397 -- Check that it is not the tag of an abstract type
399 Insert_Action (N,
400 Make_Implicit_If_Statement (N,
401 Condition => Make_Function_Call (Loc,
402 Name =>
403 New_Occurrence_Of (RTE (RE_Is_Abstract), Loc),
404 Parameter_Associations => New_List (New_Copy_Tree (Tag_Arg))),
406 Then_Statements => New_List (
407 Make_Raise_Statement (Loc,
408 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
410 -- The subprogram is the third actual in the instantiation, and is
411 -- retrieved from the corresponding renaming declaration. However,
412 -- freeze nodes may appear before, so we retrieve the declaration
413 -- with an explicit loop.
415 Act_Rename := First (Visible_Declarations (Inst_Pkg));
416 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
417 Next (Act_Rename);
418 end loop;
420 Act_Constr := Entity (Name (Act_Rename));
421 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
423 -- Check that the accessibility level of the tag is no deeper than that
424 -- of the constructor function (unless CodePeer_Mode)
426 if not CodePeer_Mode then
427 Insert_Action (N,
428 Make_Implicit_If_Statement (N,
429 Condition =>
430 Make_Op_Gt (Loc,
431 Left_Opnd =>
432 Build_Get_Access_Level (Loc, New_Copy_Tree (Tag_Arg)),
433 Right_Opnd =>
434 Make_Integer_Literal (Loc, Scope_Depth (Act_Constr))),
436 Then_Statements => New_List (
437 Make_Raise_Statement (Loc,
438 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
439 end if;
441 if Is_Interface (Etype (Act_Constr)) then
443 -- If the result type is not known to be a parent of Tag_Arg then we
444 -- need to locate the tag of the secondary dispatch table.
446 if not Is_Ancestor (Etype (Result_Typ), Etype (Tag_Arg),
447 Use_Full_View => True)
448 and then Tagged_Type_Expansion
449 then
450 -- Obtain the reference to the Ada.Tags service before generating
451 -- the Object_Declaration node to ensure that if this service is
452 -- not available in the runtime then we generate a clear error.
454 declare
455 Fname : constant Node_Id :=
456 New_Occurrence_Of (RTE (RE_Secondary_Tag), Loc);
458 begin
459 pragma Assert (not Is_Interface (Etype (Tag_Arg)));
461 -- The tag is the first entry in the dispatch table of the
462 -- return type of the constructor.
464 Iface_Tag :=
465 Make_Object_Declaration (Loc,
466 Defining_Identifier => Make_Temporary (Loc, 'V'),
467 Object_Definition =>
468 New_Occurrence_Of (RTE (RE_Tag), Loc),
469 Expression =>
470 Make_Function_Call (Loc,
471 Name => Fname,
472 Parameter_Associations => New_List (
473 Relocate_Node (Tag_Arg),
474 New_Occurrence_Of
475 (Node (First_Elmt
476 (Access_Disp_Table (Etype (Act_Constr)))),
477 Loc))));
478 Insert_Action (N, Iface_Tag);
479 end;
480 end if;
481 end if;
483 -- Create the call to the actual Constructor function
485 Cnstr_Call :=
486 Make_Function_Call (Loc,
487 Name => New_Occurrence_Of (Act_Constr, Loc),
488 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
490 -- Establish its controlling tag from the tag passed to the instance
491 -- The tag may be given by a function call, in which case a temporary
492 -- should be generated now, to prevent out-of-order insertions during
493 -- the expansion of that call when stack-checking is enabled.
495 if Present (Iface_Tag) then
496 Set_Controlling_Argument (Cnstr_Call,
497 New_Occurrence_Of (Defining_Identifier (Iface_Tag), Loc));
498 else
499 Set_Controlling_Argument (Cnstr_Call,
500 Relocate_Node (Tag_Arg));
501 end if;
503 -- Rewrite and analyze the call to the instance as a class-wide
504 -- conversion of the call to the actual constructor.
506 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
508 -- Do not generate a run-time check on the built object if tag
509 -- checks are suppressed for the result type or tagged type expansion
510 -- is disabled or if CodePeer_Mode.
512 if Tag_Checks_Suppressed (Etype (Result_Typ))
513 or else not Tagged_Type_Expansion
514 or else CodePeer_Mode
515 then
516 null;
518 -- Generate a class-wide membership test to ensure that the call's tag
519 -- argument denotes a type within the class. We must keep separate the
520 -- case in which the Result_Type of the constructor function is a tagged
521 -- type from the case in which it is an abstract interface because the
522 -- run-time subprogram required to check these cases differ (and have
523 -- one difference in their parameters profile).
525 -- Call CW_Membership if the Result_Type is a tagged type to look for
526 -- the tag in the table of ancestor tags.
528 elsif not Is_Interface (Result_Typ) then
529 declare
530 Obj_Tag_Node : Node_Id := New_Copy_Tree (Tag_Arg);
531 CW_Test_Node : Node_Id;
533 begin
534 Build_CW_Membership (Loc,
535 Obj_Tag_Node => Obj_Tag_Node,
536 Typ_Tag_Node =>
537 New_Occurrence_Of (
538 Node (First_Elmt (Access_Disp_Table (
539 Root_Type (Result_Typ)))), Loc),
540 Related_Nod => N,
541 New_Node => CW_Test_Node);
543 Insert_Action (N,
544 Make_Implicit_If_Statement (N,
545 Condition =>
546 Make_Op_Not (Loc, CW_Test_Node),
547 Then_Statements =>
548 New_List (Make_Raise_Statement (Loc,
549 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
550 end;
552 -- Call IW_Membership test if the Result_Type is an abstract interface
553 -- to look for the tag in the table of interface tags.
555 else
556 Insert_Action (N,
557 Make_Implicit_If_Statement (N,
558 Condition =>
559 Make_Op_Not (Loc,
560 Make_Function_Call (Loc,
561 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
562 Parameter_Associations => New_List (
563 Make_Attribute_Reference (Loc,
564 Prefix => New_Copy_Tree (Tag_Arg),
565 Attribute_Name => Name_Address),
567 New_Occurrence_Of (
568 Node (First_Elmt (Access_Disp_Table (
569 Root_Type (Result_Typ)))), Loc)))),
570 Then_Statements =>
571 New_List (
572 Make_Raise_Statement (Loc,
573 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
574 end if;
576 Analyze_And_Resolve (N, Etype (Act_Constr));
577 end Expand_Dispatching_Constructor_Call;
579 ---------------------------
580 -- Expand_Exception_Call --
581 ---------------------------
583 -- If the function call is not within an exception handler, then the call
584 -- is replaced by a null string. Otherwise the appropriate routine in
585 -- Ada.Exceptions is called passing the choice parameter specification
586 -- from the enclosing handler. If the enclosing handler lacks a choice
587 -- parameter, then one is supplied.
589 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
590 Loc : constant Source_Ptr := Sloc (N);
591 P : Node_Id;
592 E : Entity_Id;
594 begin
595 -- Climb up parents to see if we are in exception handler
597 P := Parent (N);
598 loop
599 -- Case of not in exception handler, replace by null string
601 if No (P) then
602 Rewrite (N,
603 Make_String_Literal (Loc,
604 Strval => ""));
605 exit;
607 -- Case of in exception handler
609 elsif Nkind (P) = N_Exception_Handler then
611 -- Handler cannot be used for a local raise, and furthermore, this
612 -- is a violation of the No_Exception_Propagation restriction.
614 Set_Local_Raise_Not_OK (P);
615 Check_Restriction (No_Exception_Propagation, N);
617 -- If no choice parameter present, then put one there. Note that
618 -- we do not need to put it on the entity chain, since no one will
619 -- be referencing it by normal visibility methods.
621 if No (Choice_Parameter (P)) then
622 E := Make_Temporary (Loc, 'E');
623 Set_Choice_Parameter (P, E);
624 Set_Ekind (E, E_Variable);
625 Set_Etype (E, RTE (RE_Exception_Occurrence));
626 Set_Scope (E, Current_Scope);
627 end if;
629 Rewrite (N,
630 Make_Function_Call (Loc,
631 Name => New_Occurrence_Of (RTE (Ent), Loc),
632 Parameter_Associations => New_List (
633 New_Occurrence_Of (Choice_Parameter (P), Loc))));
634 exit;
636 -- Keep climbing
638 else
639 P := Parent (P);
640 end if;
641 end loop;
643 Analyze_And_Resolve (N, Standard_String);
644 end Expand_Exception_Call;
646 ------------------------
647 -- Expand_Import_Call --
648 ------------------------
650 -- The function call must have a static string as its argument. We create
651 -- a dummy variable which uses this string as the external name in an
652 -- Import pragma. The result is then obtained as the address of this
653 -- dummy variable, converted to the appropriate target type.
655 procedure Expand_Import_Call (N : Node_Id) is
656 Loc : constant Source_Ptr := Sloc (N);
657 Ent : constant Entity_Id := Entity (Name (N));
658 Str : constant Node_Id := First_Actual (N);
659 Dum : constant Entity_Id := Make_Temporary (Loc, 'D');
661 begin
662 Insert_Actions (N, New_List (
663 Make_Object_Declaration (Loc,
664 Defining_Identifier => Dum,
665 Object_Definition =>
666 New_Occurrence_Of (Standard_Character, Loc)),
668 Make_Pragma (Loc,
669 Chars => Name_Import,
670 Pragma_Argument_Associations => New_List (
671 Make_Pragma_Argument_Association (Loc,
672 Expression => Make_Identifier (Loc, Name_Ada)),
674 Make_Pragma_Argument_Association (Loc,
675 Expression => Make_Identifier (Loc, Chars (Dum))),
677 Make_Pragma_Argument_Association (Loc,
678 Chars => Name_Link_Name,
679 Expression => Relocate_Node (Str))))));
681 Rewrite (N,
682 Unchecked_Convert_To (Etype (Ent),
683 Make_Attribute_Reference (Loc,
684 Prefix => Make_Identifier (Loc, Chars (Dum)),
685 Attribute_Name => Name_Address)));
687 Analyze_And_Resolve (N, Etype (Ent));
688 end Expand_Import_Call;
690 ---------------------------
691 -- Expand_Intrinsic_Call --
692 ---------------------------
694 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
695 Nam : Name_Id;
697 begin
698 -- If an external name is specified for the intrinsic, it is handled
699 -- by the back-end: leave the call node unchanged for now.
701 if Present (Interface_Name (E)) then
702 return;
703 end if;
705 -- If the intrinsic subprogram is generic, gets its original name
707 if Present (Parent (E))
708 and then Present (Generic_Parent (Parent (E)))
709 then
710 Nam := Chars (Generic_Parent (Parent (E)));
711 else
712 Nam := Chars (E);
713 end if;
715 if Nam = Name_Asm then
716 Expand_Asm_Call (N);
718 elsif Nam = Name_Divide then
719 Expand_Decimal_Divide_Call (N);
721 elsif Nam = Name_Exception_Information then
722 Expand_Exception_Call (N, RE_Exception_Information);
724 elsif Nam = Name_Exception_Message then
725 Expand_Exception_Call (N, RE_Exception_Message);
727 elsif Nam = Name_Exception_Name then
728 Expand_Exception_Call (N, RE_Exception_Name_Simple);
730 elsif Nam = Name_Generic_Dispatching_Constructor then
731 Expand_Dispatching_Constructor_Call (N);
733 elsif Nam_In (Nam, Name_Import_Address,
734 Name_Import_Largest_Value,
735 Name_Import_Value)
736 then
737 Expand_Import_Call (N);
739 elsif Nam = Name_Is_Negative then
740 Expand_Is_Negative (N);
742 elsif Nam = Name_Rotate_Left then
743 Expand_Shift (N, E, N_Op_Rotate_Left);
745 elsif Nam = Name_Rotate_Right then
746 Expand_Shift (N, E, N_Op_Rotate_Right);
748 elsif Nam = Name_Shift_Left then
749 Expand_Shift (N, E, N_Op_Shift_Left);
751 elsif Nam = Name_Shift_Right then
752 Expand_Shift (N, E, N_Op_Shift_Right);
754 elsif Nam = Name_Shift_Right_Arithmetic then
755 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
757 elsif Nam = Name_Unchecked_Conversion then
758 Expand_Unc_Conversion (N, E);
760 elsif Nam = Name_Unchecked_Deallocation then
761 Expand_Unc_Deallocation (N);
763 elsif Nam = Name_To_Address then
764 Expand_To_Address (N);
766 elsif Nam = Name_To_Pointer then
767 Expand_To_Pointer (N);
769 elsif Nam_In (Nam, Name_File,
770 Name_Line,
771 Name_Source_Location,
772 Name_Enclosing_Entity,
773 Name_Compilation_ISO_Date,
774 Name_Compilation_Date,
775 Name_Compilation_Time)
776 then
777 Expand_Source_Info (N, Nam);
779 -- If we have a renaming, expand the call to the original operation,
780 -- which must itself be intrinsic, since renaming requires matching
781 -- conventions and this has already been checked.
783 elsif Present (Alias (E)) then
784 Expand_Intrinsic_Call (N, Alias (E));
786 elsif Nkind (N) in N_Binary_Op then
787 Expand_Binary_Operator_Call (N);
789 -- The only other case is where an external name was specified, since
790 -- this is the only way that an otherwise unrecognized name could
791 -- escape the checking in Sem_Prag. Nothing needs to be done in such
792 -- a case, since we pass such a call to the back end unchanged.
794 else
795 null;
796 end if;
797 end Expand_Intrinsic_Call;
799 ------------------------
800 -- Expand_Is_Negative --
801 ------------------------
803 procedure Expand_Is_Negative (N : Node_Id) is
804 Loc : constant Source_Ptr := Sloc (N);
805 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
807 begin
809 -- We replace the function call by the following expression
811 -- if Opnd < 0.0 then
812 -- True
813 -- else
814 -- if Opnd > 0.0 then
815 -- False;
816 -- else
817 -- Float_Unsigned!(Float (Opnd)) /= 0
818 -- end if;
819 -- end if;
821 Rewrite (N,
822 Make_If_Expression (Loc,
823 Expressions => New_List (
824 Make_Op_Lt (Loc,
825 Left_Opnd => Duplicate_Subexpr (Opnd),
826 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
828 New_Occurrence_Of (Standard_True, Loc),
830 Make_If_Expression (Loc,
831 Expressions => New_List (
832 Make_Op_Gt (Loc,
833 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
834 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
836 New_Occurrence_Of (Standard_False, Loc),
838 Make_Op_Ne (Loc,
839 Left_Opnd =>
840 Unchecked_Convert_To
841 (RTE (RE_Float_Unsigned),
842 Convert_To
843 (Standard_Float,
844 Duplicate_Subexpr_No_Checks (Opnd))),
845 Right_Opnd =>
846 Make_Integer_Literal (Loc, 0)))))));
848 Analyze_And_Resolve (N, Standard_Boolean);
849 end Expand_Is_Negative;
851 ------------------
852 -- Expand_Shift --
853 ------------------
855 -- This procedure is used to convert a call to a shift function to the
856 -- corresponding operator node. This conversion is not done by the usual
857 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
858 -- operator nodes, because shifts are not predefined operators.
860 -- As a result, whenever a shift is used in the source program, it will
861 -- remain as a call until converted by this routine to the operator node
862 -- form which the back end is expecting to see.
864 -- Note: it is possible for the expander to generate shift operator nodes
865 -- directly, which will be analyzed in the normal manner by calling Analyze
866 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
868 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
869 Entyp : constant Entity_Id := Etype (E);
870 Left : constant Node_Id := First_Actual (N);
871 Loc : constant Source_Ptr := Sloc (N);
872 Right : constant Node_Id := Next_Actual (Left);
873 Ltyp : constant Node_Id := Etype (Left);
874 Rtyp : constant Node_Id := Etype (Right);
875 Typ : constant Entity_Id := Etype (N);
876 Snode : Node_Id;
878 begin
879 Snode := New_Node (K, Loc);
880 Set_Right_Opnd (Snode, Relocate_Node (Right));
881 Set_Chars (Snode, Chars (E));
882 Set_Etype (Snode, Base_Type (Entyp));
883 Set_Entity (Snode, E);
885 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
886 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
887 then
888 Set_Shift_Count_OK (Snode, True);
889 end if;
891 if Typ = Entyp then
893 -- Note that we don't call Analyze and Resolve on this node, because
894 -- it already got analyzed and resolved when it was a function call.
896 Set_Left_Opnd (Snode, Relocate_Node (Left));
897 Rewrite (N, Snode);
898 Set_Analyzed (N);
900 -- However, we do call the expander, so that the expansion for
901 -- rotates and shift_right_arithmetic happens if Modify_Tree_For_C
902 -- is set.
904 if Expander_Active then
905 Expand (N);
906 end if;
908 else
909 -- If the context type is not the type of the operator, it is an
910 -- inherited operator for a derived type. Wrap the node in a
911 -- conversion so that it is type-consistent for possible further
912 -- expansion (e.g. within a lock-free protected type).
914 Set_Left_Opnd (Snode,
915 Unchecked_Convert_To (Base_Type (Entyp), Relocate_Node (Left)));
916 Rewrite (N, Unchecked_Convert_To (Typ, Snode));
918 -- Analyze and resolve result formed by conversion to target type
920 Analyze_And_Resolve (N, Typ);
921 end if;
922 end Expand_Shift;
924 ------------------------
925 -- Expand_Source_Info --
926 ------------------------
928 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
929 Loc : constant Source_Ptr := Sloc (N);
930 begin
931 -- Integer cases
933 if Nam = Name_Line then
934 Rewrite (N,
935 Make_Integer_Literal (Loc,
936 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
937 Analyze_And_Resolve (N, Standard_Positive);
939 -- String cases
941 else
942 declare
943 Buf : Bounded_String;
944 begin
945 Add_Source_Info (Buf, Loc, Nam);
946 Rewrite (N, Make_String_Literal (Loc, Strval => +Buf));
947 Analyze_And_Resolve (N, Standard_String);
948 end;
949 end if;
951 Set_Is_Static_Expression (N);
952 end Expand_Source_Info;
954 ---------------------------
955 -- Expand_Unc_Conversion --
956 ---------------------------
958 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
959 Func : constant Entity_Id := Entity (Name (N));
960 Conv : Node_Id;
961 Ftyp : Entity_Id;
962 Ttyp : Entity_Id;
964 begin
965 -- Rewrite as unchecked conversion node. Note that we must convert
966 -- the operand to the formal type of the input parameter of the
967 -- function, so that the resulting N_Unchecked_Type_Conversion
968 -- call indicates the correct types for Gigi.
970 -- Right now, we only do this if a scalar type is involved. It is
971 -- not clear if it is needed in other cases. If we do attempt to
972 -- do the conversion unconditionally, it crashes 3411-018. To be
973 -- investigated further ???
975 Conv := Relocate_Node (First_Actual (N));
976 Ftyp := Etype (First_Formal (Func));
978 if Is_Scalar_Type (Ftyp) then
979 Conv := Convert_To (Ftyp, Conv);
980 Set_Parent (Conv, N);
981 Analyze_And_Resolve (Conv);
982 end if;
984 -- The instantiation of Unchecked_Conversion creates a wrapper package,
985 -- and the target type is declared as a subtype of the actual. Recover
986 -- the actual, which is the subtype indic. in the subtype declaration
987 -- for the target type. This is semantically correct, and avoids
988 -- anomalies with access subtypes. For entities, leave type as is.
990 -- We do the analysis here, because we do not want the compiler
991 -- to try to optimize or otherwise reorganize the unchecked
992 -- conversion node.
994 Ttyp := Etype (E);
996 if Is_Entity_Name (Conv) then
997 null;
999 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
1000 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
1002 elsif Is_Itype (Ttyp) then
1003 Ttyp :=
1004 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
1005 else
1006 raise Program_Error;
1007 end if;
1009 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
1010 Set_Etype (N, Ttyp);
1011 Set_Analyzed (N);
1013 if Nkind (N) = N_Unchecked_Type_Conversion then
1014 Expand_N_Unchecked_Type_Conversion (N);
1015 end if;
1016 end Expand_Unc_Conversion;
1018 -----------------------------
1019 -- Expand_Unc_Deallocation --
1020 -----------------------------
1022 procedure Expand_Unc_Deallocation (N : Node_Id) is
1023 Arg : constant Node_Id := First_Actual (N);
1024 Loc : constant Source_Ptr := Sloc (N);
1025 Typ : constant Entity_Id := Etype (Arg);
1026 Desig_Typ : constant Entity_Id := Designated_Type (Typ);
1027 Needs_Fin : constant Boolean := Needs_Finalization (Desig_Typ);
1028 Root_Typ : constant Entity_Id := Underlying_Type (Root_Type (Typ));
1029 Pool : constant Entity_Id := Associated_Storage_Pool (Root_Typ);
1030 Stmts : constant List_Id := New_List;
1032 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
1033 -- This captures whether we know the argument to be non-null so that
1034 -- we can avoid the test. The reason that we need to capture this is
1035 -- that we analyze some generated statements before properly attaching
1036 -- them to the tree, and that can disturb current value settings.
1038 Exceptions_OK : constant Boolean :=
1039 not Restriction_Active (No_Exception_Propagation);
1041 Abrt_Blk : Node_Id := Empty;
1042 Abrt_Blk_Id : Entity_Id;
1043 Abrt_HSS : Node_Id;
1044 AUD : Entity_Id;
1045 Fin_Blk : Node_Id;
1046 Fin_Call : Node_Id;
1047 Fin_Data : Finalization_Exception_Data;
1048 Free_Arg : Node_Id;
1049 Free_Nod : Node_Id;
1050 Gen_Code : Node_Id;
1051 Obj_Ref : Node_Id;
1053 begin
1054 -- Nothing to do if we know the argument is null
1056 if Known_Null (N) then
1057 return;
1058 end if;
1060 -- Processing for pointer to controlled types. Generate:
1062 -- Abrt : constant Boolean := ...;
1063 -- Ex : Exception_Occurrence;
1064 -- Raised : Boolean := False;
1066 -- begin
1067 -- Abort_Defer;
1069 -- begin
1070 -- [Deep_]Finalize (Obj_Ref);
1072 -- exception
1073 -- when others =>
1074 -- if not Raised then
1075 -- Raised := True;
1076 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
1077 -- end;
1078 -- at end
1079 -- Abort_Undefer_Direct;
1080 -- end;
1082 -- Depending on whether exception propagation is enabled and/or aborts
1083 -- are allowed, the generated code may lack block statements.
1085 if Needs_Fin then
1086 Obj_Ref :=
1087 Make_Explicit_Dereference (Loc,
1088 Prefix => Duplicate_Subexpr_No_Checks (Arg));
1090 -- If the designated type is tagged, the finalization call must
1091 -- dispatch because the designated type may not be the actual type
1092 -- of the object. If the type is synchronized, the deallocation
1093 -- applies to the corresponding record type.
1095 if Is_Tagged_Type (Desig_Typ) then
1096 if Is_Concurrent_Type (Desig_Typ) then
1097 Obj_Ref :=
1098 Unchecked_Convert_To
1099 (Class_Wide_Type (Corresponding_Record_Type (Desig_Typ)),
1100 Obj_Ref);
1102 elsif not Is_Class_Wide_Type (Desig_Typ) then
1103 Obj_Ref :=
1104 Unchecked_Convert_To (Class_Wide_Type (Desig_Typ), Obj_Ref);
1105 end if;
1107 -- Otherwise the designated type is untagged. Set the type of the
1108 -- dereference explicitly to force a conversion when needed given
1109 -- that [Deep_]Finalize may be inherited from a parent type.
1111 else
1112 Set_Etype (Obj_Ref, Desig_Typ);
1113 end if;
1115 -- Generate:
1116 -- [Deep_]Finalize (Obj_Ref);
1118 Fin_Call := Make_Final_Call (Obj_Ref => Obj_Ref, Typ => Desig_Typ);
1120 -- Generate:
1121 -- Abrt : constant Boolean := ...;
1122 -- Ex : Exception_Occurrence;
1123 -- Raised : Boolean := False;
1125 -- begin
1126 -- <Fin_Call>
1128 -- exception
1129 -- when others =>
1130 -- if not Raised then
1131 -- Raised := True;
1132 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
1133 -- end;
1135 if Exceptions_OK then
1136 Build_Object_Declarations (Fin_Data, Stmts, Loc);
1138 Fin_Blk :=
1139 Make_Block_Statement (Loc,
1140 Handled_Statement_Sequence =>
1141 Make_Handled_Sequence_Of_Statements (Loc,
1142 Statements => New_List (Fin_Call),
1143 Exception_Handlers => New_List (
1144 Build_Exception_Handler (Fin_Data))));
1146 -- Otherwise exception propagation is not allowed
1148 else
1149 Fin_Blk := Fin_Call;
1150 end if;
1152 -- The finalization action must be protected by an abort defer and
1153 -- undefer pair when aborts are allowed. Generate:
1155 -- begin
1156 -- Abort_Defer;
1157 -- <Fin_Blk>
1158 -- at end
1159 -- Abort_Undefer_Direct;
1160 -- end;
1162 if Abort_Allowed then
1163 AUD := RTE (RE_Abort_Undefer_Direct);
1165 Abrt_HSS :=
1166 Make_Handled_Sequence_Of_Statements (Loc,
1167 Statements => New_List (
1168 Build_Runtime_Call (Loc, RE_Abort_Defer),
1169 Fin_Blk),
1170 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1172 Abrt_Blk :=
1173 Make_Block_Statement (Loc,
1174 Handled_Statement_Sequence => Abrt_HSS);
1176 Add_Block_Identifier (Abrt_Blk, Abrt_Blk_Id);
1177 Expand_At_End_Handler (Abrt_HSS, Abrt_Blk_Id);
1179 -- Present the Abort_Undefer_Direct function to the backend so
1180 -- that it can inline the call to the function.
1182 Add_Inlined_Body (AUD, N);
1184 -- Otherwise aborts are not allowed
1186 else
1187 Abrt_Blk := Fin_Blk;
1188 end if;
1190 Append_To (Stmts, Abrt_Blk);
1191 end if;
1193 -- For a task type, call Free_Task before freeing the ATCB. We used to
1194 -- detect the case of Abort followed by a Free here, because the Free
1195 -- wouldn't actually free if it happens before the aborted task actually
1196 -- terminates. The warning was removed, because Free now works properly
1197 -- (the task will be freed once it terminates).
1199 if Is_Task_Type (Desig_Typ) then
1200 Append_To (Stmts,
1201 Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
1203 -- For composite types that contain tasks, recurse over the structure
1204 -- to build the selectors for the task subcomponents.
1206 elsif Has_Task (Desig_Typ) then
1207 if Is_Array_Type (Desig_Typ) then
1208 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_Typ));
1210 elsif Is_Record_Type (Desig_Typ) then
1211 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_Typ));
1212 end if;
1213 end if;
1215 -- Same for simple protected types. Eventually call Finalize_Protection
1216 -- before freeing the PO for each protected component.
1218 if Is_Simple_Protected_Type (Desig_Typ) then
1219 Append_To (Stmts,
1220 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
1222 elsif Has_Simple_Protected_Object (Desig_Typ) then
1223 if Is_Array_Type (Desig_Typ) then
1224 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_Typ));
1226 elsif Is_Record_Type (Desig_Typ) then
1227 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_Typ));
1228 end if;
1229 end if;
1231 -- Normal processing for non-controlled types. The argument to free is
1232 -- a renaming rather than a constant to ensure that the original context
1233 -- is always set to null after the deallocation takes place.
1235 Free_Arg := Duplicate_Subexpr_No_Checks (Arg, Renaming_Req => True);
1236 Free_Nod := Make_Free_Statement (Loc, Empty);
1237 Append_To (Stmts, Free_Nod);
1238 Set_Storage_Pool (Free_Nod, Pool);
1240 -- Attach to tree before analysis of generated subtypes below
1242 Set_Parent (Stmts, Parent (N));
1244 -- Deal with storage pool
1246 if Present (Pool) then
1248 -- Freeing the secondary stack is meaningless
1250 if Is_RTE (Pool, RE_SS_Pool) then
1251 null;
1253 -- If the pool object is of a simple storage pool type, then attempt
1254 -- to locate the type's Deallocate procedure, if any, and set the
1255 -- free operation's procedure to call. If the type doesn't have a
1256 -- Deallocate (which is allowed), then the actual will simply be set
1257 -- to null.
1259 elsif Present
1260 (Get_Rep_Pragma (Etype (Pool), Name_Simple_Storage_Pool_Type))
1261 then
1262 declare
1263 Pool_Typ : constant Entity_Id := Base_Type (Etype (Pool));
1264 Dealloc : Entity_Id;
1266 begin
1267 Dealloc := Get_Name_Entity_Id (Name_Deallocate);
1268 while Present (Dealloc) loop
1269 if Scope (Dealloc) = Scope (Pool_Typ)
1270 and then Present (First_Formal (Dealloc))
1271 and then Etype (First_Formal (Dealloc)) = Pool_Typ
1272 then
1273 Set_Procedure_To_Call (Free_Nod, Dealloc);
1274 exit;
1275 else
1276 Dealloc := Homonym (Dealloc);
1277 end if;
1278 end loop;
1279 end;
1281 -- Case of a class-wide pool type: make a dispatching call to
1282 -- Deallocate through the class-wide Deallocate_Any.
1284 elsif Is_Class_Wide_Type (Etype (Pool)) then
1285 Set_Procedure_To_Call (Free_Nod, RTE (RE_Deallocate_Any));
1287 -- Case of a specific pool type: make a statically bound call
1289 else
1290 Set_Procedure_To_Call
1291 (Free_Nod, Find_Prim_Op (Etype (Pool), Name_Deallocate));
1292 end if;
1293 end if;
1295 if Present (Procedure_To_Call (Free_Nod)) then
1297 -- For all cases of a Deallocate call, the back-end needs to be able
1298 -- to compute the size of the object being freed. This may require
1299 -- some adjustments for objects of dynamic size.
1301 -- If the type is class wide, we generate an implicit type with the
1302 -- right dynamic size, so that the deallocate call gets the right
1303 -- size parameter computed by GIGI. Same for an access to
1304 -- unconstrained packed array.
1306 if Is_Class_Wide_Type (Desig_Typ)
1307 or else
1308 (Is_Array_Type (Desig_Typ)
1309 and then not Is_Constrained (Desig_Typ)
1310 and then Is_Packed (Desig_Typ))
1311 then
1312 declare
1313 Deref : constant Node_Id :=
1314 Make_Explicit_Dereference (Loc,
1315 Duplicate_Subexpr_No_Checks (Arg));
1316 D_Subtyp : Node_Id;
1317 D_Type : Entity_Id;
1319 begin
1320 -- Perform minor decoration as it is needed by the side effect
1321 -- removal mechanism.
1323 Set_Etype (Deref, Desig_Typ);
1324 Set_Parent (Deref, Free_Nod);
1325 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_Typ);
1327 if Nkind (D_Subtyp) in N_Has_Entity then
1328 D_Type := Entity (D_Subtyp);
1330 else
1331 D_Type := Make_Temporary (Loc, 'A');
1332 Insert_Action (Deref,
1333 Make_Subtype_Declaration (Loc,
1334 Defining_Identifier => D_Type,
1335 Subtype_Indication => D_Subtyp));
1336 end if;
1338 -- Force freezing at the point of the dereference. For the
1339 -- class wide case, this avoids having the subtype frozen
1340 -- before the equivalent type.
1342 Freeze_Itype (D_Type, Deref);
1344 Set_Actual_Designated_Subtype (Free_Nod, D_Type);
1345 end;
1346 end if;
1347 end if;
1349 -- Ada 2005 (AI-251): In case of abstract interface type we must
1350 -- displace the pointer to reference the base of the object to
1351 -- deallocate its memory, unless we're targetting a VM, in which case
1352 -- no special processing is required.
1354 -- Generate:
1355 -- free (Base_Address (Obj_Ptr))
1357 if Is_Interface (Directly_Designated_Type (Typ))
1358 and then Tagged_Type_Expansion
1359 then
1360 Set_Expression (Free_Nod,
1361 Unchecked_Convert_To (Typ,
1362 Make_Function_Call (Loc,
1363 Name =>
1364 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
1365 Parameter_Associations => New_List (
1366 Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1368 -- Generate:
1369 -- free (Obj_Ptr)
1371 else
1372 Set_Expression (Free_Nod, Free_Arg);
1373 end if;
1375 -- Only remaining step is to set result to null, or generate a raise of
1376 -- Constraint_Error if the target object is "not null".
1378 if Can_Never_Be_Null (Etype (Arg)) then
1379 Append_To (Stmts,
1380 Make_Raise_Constraint_Error (Loc,
1381 Reason => CE_Access_Check_Failed));
1383 else
1384 declare
1385 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1386 begin
1387 Set_Assignment_OK (Lhs);
1388 Append_To (Stmts,
1389 Make_Assignment_Statement (Loc,
1390 Name => Lhs,
1391 Expression => Make_Null (Loc)));
1392 end;
1393 end if;
1395 -- Generate a test of whether any earlier finalization raised an
1396 -- exception, and in that case raise Program_Error with the previous
1397 -- exception occurrence.
1399 -- Generate:
1400 -- if Raised and then not Abrt then
1401 -- raise Program_Error; -- for restricted RTS
1402 -- <or>
1403 -- Raise_From_Controlled_Operation (E); -- all other cases
1404 -- end if;
1406 if Needs_Fin and then Exceptions_OK then
1407 Append_To (Stmts, Build_Raise_Statement (Fin_Data));
1408 end if;
1410 -- If we know the argument is non-null, then make a block statement
1411 -- that contains the required statements, no need for a test.
1413 if Arg_Known_Non_Null then
1414 Gen_Code :=
1415 Make_Block_Statement (Loc,
1416 Handled_Statement_Sequence =>
1417 Make_Handled_Sequence_Of_Statements (Loc,
1418 Statements => Stmts));
1420 -- If the argument may be null, wrap the statements inside an IF that
1421 -- does an explicit test to exclude the null case.
1423 else
1424 Gen_Code :=
1425 Make_Implicit_If_Statement (N,
1426 Condition =>
1427 Make_Op_Ne (Loc,
1428 Left_Opnd => Duplicate_Subexpr (Arg),
1429 Right_Opnd => Make_Null (Loc)),
1430 Then_Statements => Stmts);
1431 end if;
1433 -- Rewrite the call
1435 Rewrite (N, Gen_Code);
1436 Analyze (N);
1437 end Expand_Unc_Deallocation;
1439 -----------------------
1440 -- Expand_To_Address --
1441 -----------------------
1443 procedure Expand_To_Address (N : Node_Id) is
1444 Loc : constant Source_Ptr := Sloc (N);
1445 Arg : constant Node_Id := First_Actual (N);
1446 Obj : Node_Id;
1448 begin
1449 Remove_Side_Effects (Arg);
1451 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1453 Rewrite (N,
1454 Make_If_Expression (Loc,
1455 Expressions => New_List (
1456 Make_Op_Eq (Loc,
1457 Left_Opnd => New_Copy_Tree (Arg),
1458 Right_Opnd => Make_Null (Loc)),
1459 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1460 Make_Attribute_Reference (Loc,
1461 Prefix => Obj,
1462 Attribute_Name => Name_Address))));
1464 Analyze_And_Resolve (N, RTE (RE_Address));
1465 end Expand_To_Address;
1467 -----------------------
1468 -- Expand_To_Pointer --
1469 -----------------------
1471 procedure Expand_To_Pointer (N : Node_Id) is
1472 Arg : constant Node_Id := First_Actual (N);
1474 begin
1475 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1476 Analyze (N);
1477 end Expand_To_Pointer;
1479 end Exp_Intr;