1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
30 with Expander
; use Expander
;
31 with Exp_Atag
; use Exp_Atag
;
32 with Exp_Ch4
; use Exp_Ch4
;
33 with Exp_Ch7
; use Exp_Ch7
;
34 with Exp_Ch11
; use Exp_Ch11
;
35 with Exp_Code
; use Exp_Code
;
36 with Exp_Fixd
; use Exp_Fixd
;
37 with Exp_Util
; use Exp_Util
;
38 with Freeze
; use Freeze
;
39 with Namet
; use Namet
;
40 with Nmake
; use Nmake
;
41 with Nlists
; use Nlists
;
43 with Restrict
; use Restrict
;
44 with Rident
; use Rident
;
45 with Rtsfind
; use Rtsfind
;
47 with Sem_Aux
; use Sem_Aux
;
48 with Sem_Eval
; use Sem_Eval
;
49 with Sem_Res
; use Sem_Res
;
50 with Sem_Type
; use Sem_Type
;
51 with Sem_Util
; use Sem_Util
;
52 with Sinfo
; use Sinfo
;
53 with Sinput
; use Sinput
;
54 with Snames
; use Snames
;
55 with Stand
; use Stand
;
56 with Stringt
; use Stringt
;
57 with Targparm
; use Targparm
;
58 with Tbuild
; use Tbuild
;
59 with Uintp
; use Uintp
;
60 with Urealp
; use Urealp
;
62 package body Exp_Intr
is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 procedure Expand_Binary_Operator_Call
(N
: Node_Id
);
69 -- Expand a call to an intrinsic arithmetic operator when the operand
70 -- types or sizes are not identical.
72 procedure Expand_Is_Negative
(N
: Node_Id
);
73 -- Expand a call to the intrinsic Is_Negative function
75 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
);
76 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
77 -- into a dispatching call to the actual subprogram associated with the
78 -- Constructor formal subprogram, passing it the Parameters actual of
79 -- the call to the instantiation and dispatching based on call's Tag
82 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
);
83 -- Expand a call to Exception_Information/Message/Name. The first
84 -- parameter, N, is the node for the function call, and Ent is the
85 -- entity for the corresponding routine in the Ada.Exceptions package.
87 procedure Expand_Import_Call
(N
: Node_Id
);
88 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
89 -- N is the node for the function call.
91 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
);
92 -- Expand an intrinsic shift operation, N and E are from the call to
93 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
94 -- K is the kind for the shift node
96 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
);
97 -- Expand a call to an instantiation of Unchecked_Conversion into a node
98 -- N_Unchecked_Type_Conversion.
100 procedure Expand_Unc_Deallocation
(N
: Node_Id
);
101 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
102 -- N_Free_Statement and appropriate context.
104 procedure Expand_To_Address
(N
: Node_Id
);
105 procedure Expand_To_Pointer
(N
: Node_Id
);
106 -- Expand a call to corresponding function, declared in an instance of
107 -- System.Address_To_Access_Conversions.
109 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
);
110 -- Rewrite the node by the appropriate string or positive constant.
111 -- Nam can be one of the following:
112 -- Name_File - expand string name of source file
113 -- Name_Line - expand integer line number
114 -- Name_Source_Location - expand string of form file:line
115 -- Name_Enclosing_Entity - expand string name of enclosing entity
116 -- Name_Compilation_Date - expand string with compilation date
117 -- Name_Compilation_Time - expand string with compilation time
119 ---------------------------------
120 -- Expand_Binary_Operator_Call --
121 ---------------------------------
123 procedure Expand_Binary_Operator_Call
(N
: Node_Id
) is
124 T1
: constant Entity_Id
:= Underlying_Type
(Etype
(Left_Opnd
(N
)));
125 T2
: constant Entity_Id
:= Underlying_Type
(Etype
(Right_Opnd
(N
)));
126 TR
: constant Entity_Id
:= Etype
(N
);
130 Siz
: constant Uint
:= UI_Max
(RM_Size
(T1
), RM_Size
(T2
));
131 -- Maximum of operand sizes
134 -- Nothing to do if the operands have the same modular type
136 if Base_Type
(T1
) = Base_Type
(T2
)
137 and then Is_Modular_Integer_Type
(T1
)
142 -- Use Unsigned_32 for sizes of 32 or below, else Unsigned_64
145 T3
:= RTE
(RE_Unsigned_64
);
147 T3
:= RTE
(RE_Unsigned_32
);
150 -- Copy operator node, and reset type and entity fields, for
151 -- subsequent reanalysis.
158 Set_Entity
(Res
, Standard_Op_And
);
160 Set_Entity
(Res
, Standard_Op_Or
);
162 Set_Entity
(Res
, Standard_Op_Xor
);
167 -- Convert operands to large enough intermediate type
170 Unchecked_Convert_To
(T3
, Relocate_Node
(Left_Opnd
(N
))));
172 Unchecked_Convert_To
(T3
, Relocate_Node
(Right_Opnd
(N
))));
174 -- Analyze and resolve result formed by conversion to target type
176 Rewrite
(N
, Unchecked_Convert_To
(TR
, Res
));
177 Analyze_And_Resolve
(N
, TR
);
178 end Expand_Binary_Operator_Call
;
180 -----------------------------------------
181 -- Expand_Dispatching_Constructor_Call --
182 -----------------------------------------
184 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
187 -- GDC_Instance (The_Tag, Parameters'Access)
189 -- to a class-wide conversion of a dispatching call to the actual
190 -- associated with the formal subprogram Construct, designating The_Tag
191 -- as the controlling tag of the call:
193 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
195 -- which will eventually be expanded to the following:
197 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
199 -- A class-wide membership test is also generated, preceding the call, to
200 -- ensure that the controlling tag denotes a type in T'Class.
202 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
) is
203 Loc
: constant Source_Ptr
:= Sloc
(N
);
204 Tag_Arg
: constant Node_Id
:= First_Actual
(N
);
205 Param_Arg
: constant Node_Id
:= Next_Actual
(Tag_Arg
);
206 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Entity
(Name
(N
))));
207 Inst_Pkg
: constant Node_Id
:= Parent
(Subp_Decl
);
208 Act_Rename
: Node_Id
;
209 Act_Constr
: Entity_Id
;
210 Iface_Tag
: Node_Id
:= Empty
;
211 Cnstr_Call
: Node_Id
;
212 Result_Typ
: Entity_Id
;
215 -- Remove side effects from tag argument early, before rewriting
216 -- the dispatching constructor call, as Remove_Side_Effects relies
217 -- on Tag_Arg's Parent link properly attached to the tree (once the
218 -- call is rewritten, the Parent is inconsistent as it points to the
219 -- rewritten node, which is not the syntactic parent of the Tag_Arg
222 Remove_Side_Effects
(Tag_Arg
);
224 -- The subprogram is the third actual in the instantiation, and is
225 -- retrieved from the corresponding renaming declaration. However,
226 -- freeze nodes may appear before, so we retrieve the declaration
227 -- with an explicit loop.
229 Act_Rename
:= First
(Visible_Declarations
(Inst_Pkg
));
230 while Nkind
(Act_Rename
) /= N_Subprogram_Renaming_Declaration
loop
234 Act_Constr
:= Entity
(Name
(Act_Rename
));
235 Result_Typ
:= Class_Wide_Type
(Etype
(Act_Constr
));
237 if Is_Interface
(Etype
(Act_Constr
)) then
239 -- If the result type is not known to be a parent of Tag_Arg then we
240 -- need to locate the tag of the secondary dispatch table.
242 if not Is_Ancestor
(Etype
(Result_Typ
), Etype
(Tag_Arg
),
243 Use_Full_View
=> True)
244 and then Tagged_Type_Expansion
246 -- Obtain the reference to the Ada.Tags service before generating
247 -- the Object_Declaration node to ensure that if this service is
248 -- not available in the runtime then we generate a clear error.
251 Fname
: constant Node_Id
:=
252 New_Occurrence_Of
(RTE
(RE_Secondary_Tag
), Loc
);
255 pragma Assert
(not Is_Interface
(Etype
(Tag_Arg
)));
258 Make_Object_Declaration
(Loc
,
259 Defining_Identifier
=> Make_Temporary
(Loc
, 'V'),
261 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
263 Make_Function_Call
(Loc
,
265 Parameter_Associations
=> New_List
(
266 Relocate_Node
(Tag_Arg
),
268 (Node
(First_Elmt
(Access_Disp_Table
269 (Etype
(Etype
(Act_Constr
))))),
271 Insert_Action
(N
, Iface_Tag
);
276 -- Create the call to the actual Constructor function
279 Make_Function_Call
(Loc
,
280 Name
=> New_Occurrence_Of
(Act_Constr
, Loc
),
281 Parameter_Associations
=> New_List
(Relocate_Node
(Param_Arg
)));
283 -- Establish its controlling tag from the tag passed to the instance
284 -- The tag may be given by a function call, in which case a temporary
285 -- should be generated now, to prevent out-of-order insertions during
286 -- the expansion of that call when stack-checking is enabled.
288 if Present
(Iface_Tag
) then
289 Set_Controlling_Argument
(Cnstr_Call
,
290 New_Occurrence_Of
(Defining_Identifier
(Iface_Tag
), Loc
));
292 Set_Controlling_Argument
(Cnstr_Call
,
293 Relocate_Node
(Tag_Arg
));
296 -- Rewrite and analyze the call to the instance as a class-wide
297 -- conversion of the call to the actual constructor.
299 Rewrite
(N
, Convert_To
(Result_Typ
, Cnstr_Call
));
300 Analyze_And_Resolve
(N
, Etype
(Act_Constr
));
302 -- Do not generate a run-time check on the built object if tag
303 -- checks are suppressed for the result type or VM_Target /= No_VM
305 if Tag_Checks_Suppressed
(Etype
(Result_Typ
))
306 or else not Tagged_Type_Expansion
310 -- Generate a class-wide membership test to ensure that the call's tag
311 -- argument denotes a type within the class. We must keep separate the
312 -- case in which the Result_Type of the constructor function is a tagged
313 -- type from the case in which it is an abstract interface because the
314 -- run-time subprogram required to check these cases differ (and have
315 -- one difference in their parameters profile).
317 -- Call CW_Membership if the Result_Type is a tagged type to look for
318 -- the tag in the table of ancestor tags.
320 elsif not Is_Interface
(Result_Typ
) then
322 Obj_Tag_Node
: Node_Id
:= New_Copy_Tree
(Tag_Arg
);
323 CW_Test_Node
: Node_Id
;
326 Build_CW_Membership
(Loc
,
327 Obj_Tag_Node
=> Obj_Tag_Node
,
330 Node
(First_Elmt
(Access_Disp_Table
(
331 Root_Type
(Result_Typ
)))), Loc
),
333 New_Node
=> CW_Test_Node
);
336 Make_Implicit_If_Statement
(N
,
338 Make_Op_Not
(Loc
, CW_Test_Node
),
340 New_List
(Make_Raise_Statement
(Loc
,
341 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
344 -- Call IW_Membership test if the Result_Type is an abstract interface
345 -- to look for the tag in the table of interface tags.
349 Make_Implicit_If_Statement
(N
,
352 Make_Function_Call
(Loc
,
353 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
354 Parameter_Associations
=> New_List
(
355 Make_Attribute_Reference
(Loc
,
356 Prefix
=> New_Copy_Tree
(Tag_Arg
),
357 Attribute_Name
=> Name_Address
),
360 Node
(First_Elmt
(Access_Disp_Table
(
361 Root_Type
(Result_Typ
)))), Loc
)))),
364 Make_Raise_Statement
(Loc
,
365 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
367 end Expand_Dispatching_Constructor_Call
;
369 ---------------------------
370 -- Expand_Exception_Call --
371 ---------------------------
373 -- If the function call is not within an exception handler, then the call
374 -- is replaced by a null string. Otherwise the appropriate routine in
375 -- Ada.Exceptions is called passing the choice parameter specification
376 -- from the enclosing handler. If the enclosing handler lacks a choice
377 -- parameter, then one is supplied.
379 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
) is
380 Loc
: constant Source_Ptr
:= Sloc
(N
);
385 -- Climb up parents to see if we are in exception handler
389 -- Case of not in exception handler, replace by null string
393 Make_String_Literal
(Loc
,
397 -- Case of in exception handler
399 elsif Nkind
(P
) = N_Exception_Handler
then
401 -- Handler cannot be used for a local raise, and furthermore, this
402 -- is a violation of the No_Exception_Propagation restriction.
404 Set_Local_Raise_Not_OK
(P
);
405 Check_Restriction
(No_Exception_Propagation
, N
);
407 -- If no choice parameter present, then put one there. Note that
408 -- we do not need to put it on the entity chain, since no one will
409 -- be referencing it by normal visibility methods.
411 if No
(Choice_Parameter
(P
)) then
412 E
:= Make_Temporary
(Loc
, 'E');
413 Set_Choice_Parameter
(P
, E
);
414 Set_Ekind
(E
, E_Variable
);
415 Set_Etype
(E
, RTE
(RE_Exception_Occurrence
));
416 Set_Scope
(E
, Current_Scope
);
420 Make_Function_Call
(Loc
,
421 Name
=> New_Occurrence_Of
(RTE
(Ent
), Loc
),
422 Parameter_Associations
=> New_List
(
423 New_Occurrence_Of
(Choice_Parameter
(P
), Loc
))));
433 Analyze_And_Resolve
(N
, Standard_String
);
434 end Expand_Exception_Call
;
436 ------------------------
437 -- Expand_Import_Call --
438 ------------------------
440 -- The function call must have a static string as its argument. We create
441 -- a dummy variable which uses this string as the external name in an
442 -- Import pragma. The result is then obtained as the address of this
443 -- dummy variable, converted to the appropriate target type.
445 procedure Expand_Import_Call
(N
: Node_Id
) is
446 Loc
: constant Source_Ptr
:= Sloc
(N
);
447 Ent
: constant Entity_Id
:= Entity
(Name
(N
));
448 Str
: constant Node_Id
:= First_Actual
(N
);
449 Dum
: constant Entity_Id
:= Make_Temporary
(Loc
, 'D');
452 Insert_Actions
(N
, New_List
(
453 Make_Object_Declaration
(Loc
,
454 Defining_Identifier
=> Dum
,
456 New_Occurrence_Of
(Standard_Character
, Loc
)),
459 Chars
=> Name_Import
,
460 Pragma_Argument_Associations
=> New_List
(
461 Make_Pragma_Argument_Association
(Loc
,
462 Expression
=> Make_Identifier
(Loc
, Name_Ada
)),
464 Make_Pragma_Argument_Association
(Loc
,
465 Expression
=> Make_Identifier
(Loc
, Chars
(Dum
))),
467 Make_Pragma_Argument_Association
(Loc
,
468 Chars
=> Name_Link_Name
,
469 Expression
=> Relocate_Node
(Str
))))));
472 Unchecked_Convert_To
(Etype
(Ent
),
473 Make_Attribute_Reference
(Loc
,
474 Prefix
=> Make_Identifier
(Loc
, Chars
(Dum
)),
475 Attribute_Name
=> Name_Address
)));
477 Analyze_And_Resolve
(N
, Etype
(Ent
));
478 end Expand_Import_Call
;
480 ---------------------------
481 -- Expand_Intrinsic_Call --
482 ---------------------------
484 procedure Expand_Intrinsic_Call
(N
: Node_Id
; E
: Entity_Id
) is
488 -- If an external name is specified for the intrinsic, it is handled
489 -- by the back-end: leave the call node unchanged for now.
491 if Present
(Interface_Name
(E
)) then
495 -- If the intrinsic subprogram is generic, gets its original name
497 if Present
(Parent
(E
))
498 and then Present
(Generic_Parent
(Parent
(E
)))
500 Nam
:= Chars
(Generic_Parent
(Parent
(E
)));
505 if Nam
= Name_Asm
then
508 elsif Nam
= Name_Divide
then
509 Expand_Decimal_Divide_Call
(N
);
511 elsif Nam
= Name_Exception_Information
then
512 Expand_Exception_Call
(N
, RE_Exception_Information
);
514 elsif Nam
= Name_Exception_Message
then
515 Expand_Exception_Call
(N
, RE_Exception_Message
);
517 elsif Nam
= Name_Exception_Name
then
518 Expand_Exception_Call
(N
, RE_Exception_Name_Simple
);
520 elsif Nam
= Name_Generic_Dispatching_Constructor
then
521 Expand_Dispatching_Constructor_Call
(N
);
523 elsif Nam_In
(Nam
, Name_Import_Address
,
524 Name_Import_Largest_Value
,
527 Expand_Import_Call
(N
);
529 elsif Nam
= Name_Is_Negative
then
530 Expand_Is_Negative
(N
);
532 elsif Nam
= Name_Rotate_Left
then
533 Expand_Shift
(N
, E
, N_Op_Rotate_Left
);
535 elsif Nam
= Name_Rotate_Right
then
536 Expand_Shift
(N
, E
, N_Op_Rotate_Right
);
538 elsif Nam
= Name_Shift_Left
then
539 Expand_Shift
(N
, E
, N_Op_Shift_Left
);
541 elsif Nam
= Name_Shift_Right
then
542 Expand_Shift
(N
, E
, N_Op_Shift_Right
);
544 elsif Nam
= Name_Shift_Right_Arithmetic
then
545 Expand_Shift
(N
, E
, N_Op_Shift_Right_Arithmetic
);
547 elsif Nam
= Name_Unchecked_Conversion
then
548 Expand_Unc_Conversion
(N
, E
);
550 elsif Nam
= Name_Unchecked_Deallocation
then
551 Expand_Unc_Deallocation
(N
);
553 elsif Nam
= Name_To_Address
then
554 Expand_To_Address
(N
);
556 elsif Nam
= Name_To_Pointer
then
557 Expand_To_Pointer
(N
);
559 elsif Nam_In
(Nam
, Name_File
,
561 Name_Source_Location
,
562 Name_Enclosing_Entity
,
563 Name_Compilation_Date
,
564 Name_Compilation_Time
)
566 Expand_Source_Info
(N
, Nam
);
568 -- If we have a renaming, expand the call to the original operation,
569 -- which must itself be intrinsic, since renaming requires matching
570 -- conventions and this has already been checked.
572 elsif Present
(Alias
(E
)) then
573 Expand_Intrinsic_Call
(N
, Alias
(E
));
575 elsif Nkind
(N
) in N_Binary_Op
then
576 Expand_Binary_Operator_Call
(N
);
578 -- The only other case is where an external name was specified, since
579 -- this is the only way that an otherwise unrecognized name could
580 -- escape the checking in Sem_Prag. Nothing needs to be done in such
581 -- a case, since we pass such a call to the back end unchanged.
586 end Expand_Intrinsic_Call
;
588 ------------------------
589 -- Expand_Is_Negative --
590 ------------------------
592 procedure Expand_Is_Negative
(N
: Node_Id
) is
593 Loc
: constant Source_Ptr
:= Sloc
(N
);
594 Opnd
: constant Node_Id
:= Relocate_Node
(First_Actual
(N
));
598 -- We replace the function call by the following expression
600 -- if Opnd < 0.0 then
603 -- if Opnd > 0.0 then
606 -- Float_Unsigned!(Float (Opnd)) /= 0
611 Make_If_Expression
(Loc
,
612 Expressions
=> New_List
(
614 Left_Opnd
=> Duplicate_Subexpr
(Opnd
),
615 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
617 New_Occurrence_Of
(Standard_True
, Loc
),
619 Make_If_Expression
(Loc
,
620 Expressions
=> New_List
(
622 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Opnd
),
623 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
625 New_Occurrence_Of
(Standard_False
, Loc
),
630 (RTE
(RE_Float_Unsigned
),
633 Duplicate_Subexpr_No_Checks
(Opnd
))),
635 Make_Integer_Literal
(Loc
, 0)))))));
637 Analyze_And_Resolve
(N
, Standard_Boolean
);
638 end Expand_Is_Negative
;
644 -- This procedure is used to convert a call to a shift function to the
645 -- corresponding operator node. This conversion is not done by the usual
646 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
647 -- operator nodes, because shifts are not predefined operators.
649 -- As a result, whenever a shift is used in the source program, it will
650 -- remain as a call until converted by this routine to the operator node
651 -- form which the back end is expecting to see.
653 -- Note: it is possible for the expander to generate shift operator nodes
654 -- directly, which will be analyzed in the normal manner by calling Analyze
655 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
657 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
) is
658 Entyp
: constant Entity_Id
:= Etype
(E
);
659 Left
: constant Node_Id
:= First_Actual
(N
);
660 Loc
: constant Source_Ptr
:= Sloc
(N
);
661 Right
: constant Node_Id
:= Next_Actual
(Left
);
662 Ltyp
: constant Node_Id
:= Etype
(Left
);
663 Rtyp
: constant Node_Id
:= Etype
(Right
);
664 Typ
: constant Entity_Id
:= Etype
(N
);
668 Snode
:= New_Node
(K
, Loc
);
669 Set_Right_Opnd
(Snode
, Relocate_Node
(Right
));
670 Set_Chars
(Snode
, Chars
(E
));
671 Set_Etype
(Snode
, Base_Type
(Entyp
));
672 Set_Entity
(Snode
, E
);
674 if Compile_Time_Known_Value
(Type_High_Bound
(Rtyp
))
675 and then Expr_Value
(Type_High_Bound
(Rtyp
)) < Esize
(Ltyp
)
677 Set_Shift_Count_OK
(Snode
, True);
682 -- Note that we don't call Analyze and Resolve on this node, because
683 -- it already got analyzed and resolved when it was a function call.
685 Set_Left_Opnd
(Snode
, Relocate_Node
(Left
));
689 -- However, we do call the expander, so that the expansion for
690 -- rotates and shift_right_arithmetic happens if Modify_Tree_For_C
693 if Expander_Active
then
698 -- If the context type is not the type of the operator, it is an
699 -- inherited operator for a derived type. Wrap the node in a
700 -- conversion so that it is type-consistent for possible further
701 -- expansion (e.g. within a lock-free protected type).
703 Set_Left_Opnd
(Snode
,
704 Unchecked_Convert_To
(Base_Type
(Entyp
), Relocate_Node
(Left
)));
705 Rewrite
(N
, Unchecked_Convert_To
(Typ
, Snode
));
707 -- Analyze and resolve result formed by conversion to target type
709 Analyze_And_Resolve
(N
, Typ
);
713 ------------------------
714 -- Expand_Source_Info --
715 ------------------------
717 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
) is
718 Loc
: constant Source_Ptr
:= Sloc
(N
);
721 procedure Write_Entity_Name
(E
: Entity_Id
);
722 -- Recursive procedure to construct string for qualified name of
723 -- enclosing program unit. The qualification stops at an enclosing
724 -- scope has no source name (block or loop). If entity is a subprogram
725 -- instance, skip enclosing wrapper package.
727 -----------------------
728 -- Write_Entity_Name --
729 -----------------------
731 procedure Write_Entity_Name
(E
: Entity_Id
) is
733 TDef
: constant Source_Buffer_Ptr
:=
734 Source_Text
(Get_Source_File_Index
(Sloc
(E
)));
737 -- Nothing to do if at outer level
739 if Scope
(E
) = Standard_Standard
then
742 -- If scope comes from source, write its name
744 elsif Comes_From_Source
(Scope
(E
)) then
745 Write_Entity_Name
(Scope
(E
));
746 Add_Char_To_Name_Buffer
('.');
748 -- If in wrapper package skip past it
750 elsif Is_Wrapper_Package
(Scope
(E
)) then
751 Write_Entity_Name
(Scope
(Scope
(E
)));
752 Add_Char_To_Name_Buffer
('.');
754 -- Otherwise nothing to output (happens in unnamed block statements)
760 -- Loop to output the name
762 -- This is not right wrt wide char encodings ??? ()
765 while TDef
(SDef
) in '0' .. '9'
766 or else TDef
(SDef
) >= 'A'
767 or else TDef
(SDef
) = ASCII
.ESC
769 Add_Char_To_Name_Buffer
(TDef
(SDef
));
772 end Write_Entity_Name
;
774 -- Start of processing for Expand_Source_Info
779 if Nam
= Name_Line
then
781 Make_Integer_Literal
(Loc
,
782 Intval
=> UI_From_Int
(Int
(Get_Logical_Line_Number
(Loc
)))));
783 Analyze_And_Resolve
(N
, Standard_Positive
);
792 Get_Decoded_Name_String
793 (Reference_Name
(Get_Source_File_Index
(Loc
)));
795 when Name_Source_Location
=>
796 Build_Location_String
(Loc
);
798 when Name_Enclosing_Entity
=>
800 -- Skip enclosing blocks to reach enclosing unit
802 Ent
:= Current_Scope
;
803 while Present
(Ent
) loop
804 exit when Ekind
(Ent
) /= E_Block
805 and then Ekind
(Ent
) /= E_Loop
;
809 -- Ent now points to the relevant defining entity
811 Write_Entity_Name
(Ent
);
813 when Name_Compilation_Date
=>
815 subtype S13
is String (1 .. 3);
816 Months
: constant array (1 .. 12) of S13
:=
817 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
818 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
820 M1
: constant Character := Opt
.Compilation_Time
(6);
821 M2
: constant Character := Opt
.Compilation_Time
(7);
823 MM
: constant Natural range 1 .. 12 :=
824 (Character'Pos (M1
) - Character'Pos ('0')) * 10 +
825 (Character'Pos (M2
) - Character'Pos ('0'));
828 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
830 Name_Buffer
(1 .. 3) := Months
(MM
);
831 Name_Buffer
(4) := ' ';
832 Name_Buffer
(5 .. 6) := Opt
.Compilation_Time
(9 .. 10);
833 Name_Buffer
(7) := ' ';
834 Name_Buffer
(8 .. 11) := Opt
.Compilation_Time
(1 .. 4);
838 when Name_Compilation_Time
=>
839 Name_Buffer
(1 .. 8) := Opt
.Compilation_Time
(12 .. 19);
847 Make_String_Literal
(Loc
,
848 Strval
=> String_From_Name_Buffer
));
849 Analyze_And_Resolve
(N
, Standard_String
);
852 Set_Is_Static_Expression
(N
);
853 end Expand_Source_Info
;
855 ---------------------------
856 -- Expand_Unc_Conversion --
857 ---------------------------
859 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
) is
860 Func
: constant Entity_Id
:= Entity
(Name
(N
));
866 -- Rewrite as unchecked conversion node. Note that we must convert
867 -- the operand to the formal type of the input parameter of the
868 -- function, so that the resulting N_Unchecked_Type_Conversion
869 -- call indicates the correct types for Gigi.
871 -- Right now, we only do this if a scalar type is involved. It is
872 -- not clear if it is needed in other cases. If we do attempt to
873 -- do the conversion unconditionally, it crashes 3411-018. To be
874 -- investigated further ???
876 Conv
:= Relocate_Node
(First_Actual
(N
));
877 Ftyp
:= Etype
(First_Formal
(Func
));
879 if Is_Scalar_Type
(Ftyp
) then
880 Conv
:= Convert_To
(Ftyp
, Conv
);
881 Set_Parent
(Conv
, N
);
882 Analyze_And_Resolve
(Conv
);
885 -- The instantiation of Unchecked_Conversion creates a wrapper package,
886 -- and the target type is declared as a subtype of the actual. Recover
887 -- the actual, which is the subtype indic. in the subtype declaration
888 -- for the target type. This is semantically correct, and avoids
889 -- anomalies with access subtypes. For entities, leave type as is.
891 -- We do the analysis here, because we do not want the compiler
892 -- to try to optimize or otherwise reorganize the unchecked
897 if Is_Entity_Name
(Conv
) then
900 elsif Nkind
(Parent
(Ttyp
)) = N_Subtype_Declaration
then
901 Ttyp
:= Entity
(Subtype_Indication
(Parent
(Etype
(E
))));
903 elsif Is_Itype
(Ttyp
) then
905 Entity
(Subtype_Indication
(Associated_Node_For_Itype
(Ttyp
)));
910 Rewrite
(N
, Unchecked_Convert_To
(Ttyp
, Conv
));
914 if Nkind
(N
) = N_Unchecked_Type_Conversion
then
915 Expand_N_Unchecked_Type_Conversion
(N
);
917 end Expand_Unc_Conversion
;
919 -----------------------------
920 -- Expand_Unc_Deallocation --
921 -----------------------------
923 -- Generate the following Code :
925 -- if Arg /= null then
926 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
931 -- For a task, we also generate a call to Free_Task to ensure that the
932 -- task itself is freed if it is terminated, ditto for a simple protected
933 -- object, with a call to Finalize_Protection. For composite types that
934 -- have tasks or simple protected objects as components, we traverse the
935 -- structures to find and terminate those components.
937 procedure Expand_Unc_Deallocation
(N
: Node_Id
) is
938 Arg
: constant Node_Id
:= First_Actual
(N
);
939 Loc
: constant Source_Ptr
:= Sloc
(N
);
940 Typ
: constant Entity_Id
:= Etype
(Arg
);
941 Desig_T
: constant Entity_Id
:= Designated_Type
(Typ
);
942 Rtyp
: constant Entity_Id
:= Underlying_Type
(Root_Type
(Typ
));
943 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Rtyp
);
944 Stmts
: constant List_Id
:= New_List
;
945 Needs_Fin
: constant Boolean := Needs_Finalization
(Desig_T
);
947 Finalizer_Data
: Finalization_Exception_Data
;
949 Blk
: Node_Id
:= Empty
;
952 Final_Code
: List_Id
;
957 Arg_Known_Non_Null
: constant Boolean := Known_Non_Null
(N
);
958 -- This captures whether we know the argument to be non-null so that
959 -- we can avoid the test. The reason that we need to capture this is
960 -- that we analyze some generated statements before properly attaching
961 -- them to the tree, and that can disturb current value settings.
964 -- This variable captures an unused dummy internal entity, see the
965 -- comment associated with its use.
968 -- Nothing to do if we know the argument is null
970 if Known_Null
(N
) then
974 -- Processing for pointer to controlled type
978 Make_Explicit_Dereference
(Loc
,
979 Prefix
=> Duplicate_Subexpr_No_Checks
(Arg
));
981 -- If the type is tagged, then we must force dispatching on the
982 -- finalization call because the designated type may not be the
983 -- actual type of the object.
985 if Is_Tagged_Type
(Desig_T
)
986 and then not Is_Class_Wide_Type
(Desig_T
)
988 Deref
:= Unchecked_Convert_To
(Class_Wide_Type
(Desig_T
), Deref
);
990 elsif not Is_Tagged_Type
(Desig_T
) then
992 -- Set type of result, to force a conversion when needed (see
993 -- exp_ch7, Convert_View), given that Deep_Finalize may be
994 -- inherited from the parent type, and we need the type of the
995 -- expression to see whether the conversion is in fact needed.
997 Set_Etype
(Deref
, Desig_T
);
1000 -- The finalization call is expanded wrapped in a block to catch any
1001 -- possible exception. If an exception does occur, then Program_Error
1002 -- must be raised following the freeing of the object and its removal
1003 -- from the finalization collection's list. We set a flag to record
1004 -- that an exception was raised, and save its occurrence for use in
1008 -- Abort : constant Boolean :=
1009 -- Exception_Occurrence (Get_Current_Excep.all.all) =
1010 -- Standard'Abort_Signal'Identity;
1012 -- Abort : constant Boolean := False; -- no abort
1014 -- E : Exception_Occurrence;
1015 -- Raised : Boolean := False;
1018 -- [Deep_]Finalize (Obj);
1022 -- Save_Occurrence (E, Get_Current_Excep.all.all);
1025 Build_Object_Declarations
(Finalizer_Data
, Stmts
, Loc
);
1027 Final_Code
:= New_List
(
1028 Make_Block_Statement
(Loc
,
1029 Handled_Statement_Sequence
=>
1030 Make_Handled_Sequence_Of_Statements
(Loc
,
1031 Statements
=> New_List
(
1032 Make_Final_Call
(Obj_Ref
=> Deref
, Typ
=> Desig_T
)),
1033 Exception_Handlers
=> New_List
(
1034 Build_Exception_Handler
(Finalizer_Data
)))));
1036 -- For .NET/JVM, detach the object from the containing finalization
1037 -- collection before finalizing it.
1039 if VM_Target
/= No_VM
and then Is_Controlled
(Desig_T
) then
1040 Prepend_To
(Final_Code
,
1041 Make_Detach_Call
(New_Copy_Tree
(Arg
)));
1044 -- If aborts are allowed, then the finalization code must be
1045 -- protected by an abort defer/undefer pair.
1047 if Abort_Allowed
then
1048 Prepend_To
(Final_Code
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1051 Make_Block_Statement
(Loc
, Handled_Statement_Sequence
=>
1052 Make_Handled_Sequence_Of_Statements
(Loc
,
1053 Statements
=> Final_Code
,
1055 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
)));
1056 Add_Block_Identifier
(Blk
, Blk_Id
);
1058 Append
(Blk
, Stmts
);
1061 -- Generate a dummy entity to ensure that the internal symbols are
1062 -- in sync when a unit is compiled with and without aborts.
1064 Dummy
:= New_Internal_Entity
(E_Block
, Current_Scope
, Loc
, 'B');
1065 Append_List_To
(Stmts
, Final_Code
);
1069 -- For a task type, call Free_Task before freeing the ATCB
1071 if Is_Task_Type
(Desig_T
) then
1073 -- We used to detect the case of Abort followed by a Free here,
1074 -- because the Free wouldn't actually free if it happens before
1075 -- the aborted task actually terminates. The warning was removed,
1076 -- because Free now works properly (the task will be freed once
1080 (Stmts
, Cleanup_Task
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1082 -- For composite types that contain tasks, recurse over the structure
1083 -- to build the selectors for the task subcomponents.
1085 elsif Has_Task
(Desig_T
) then
1086 if Is_Record_Type
(Desig_T
) then
1087 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_T
));
1089 elsif Is_Array_Type
(Desig_T
) then
1090 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_T
));
1094 -- Same for simple protected types. Eventually call Finalize_Protection
1095 -- before freeing the PO for each protected component.
1097 if Is_Simple_Protected_Type
(Desig_T
) then
1099 Cleanup_Protected_Object
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1101 elsif Has_Simple_Protected_Object
(Desig_T
) then
1102 if Is_Record_Type
(Desig_T
) then
1103 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_T
));
1104 elsif Is_Array_Type
(Desig_T
) then
1105 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_T
));
1109 -- Normal processing for non-controlled types. The argument to free is
1110 -- a renaming rather than a constant to ensure that the original context
1111 -- is always set to null after the deallocation takes place.
1113 Free_Arg
:= Duplicate_Subexpr_No_Checks
(Arg
, Renaming_Req
=> True);
1114 Free_Node
:= Make_Free_Statement
(Loc
, Empty
);
1115 Append_To
(Stmts
, Free_Node
);
1116 Set_Storage_Pool
(Free_Node
, Pool
);
1118 -- Attach to tree before analysis of generated subtypes below
1120 Set_Parent
(Stmts
, Parent
(N
));
1122 -- Deal with storage pool
1124 if Present
(Pool
) then
1126 -- Freeing the secondary stack is meaningless
1128 if Is_RTE
(Pool
, RE_SS_Pool
) then
1131 -- If the pool object is of a simple storage pool type, then attempt
1132 -- to locate the type's Deallocate procedure, if any, and set the
1133 -- free operation's procedure to call. If the type doesn't have a
1134 -- Deallocate (which is allowed), then the actual will simply be set
1137 elsif Present
(Get_Rep_Pragma
1138 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
1141 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
1142 Dealloc_Op
: Entity_Id
;
1144 Dealloc_Op
:= Get_Name_Entity_Id
(Name_Deallocate
);
1145 while Present
(Dealloc_Op
) loop
1146 if Scope
(Dealloc_Op
) = Scope
(Pool_Type
)
1147 and then Present
(First_Formal
(Dealloc_Op
))
1148 and then Etype
(First_Formal
(Dealloc_Op
)) = Pool_Type
1150 Set_Procedure_To_Call
(Free_Node
, Dealloc_Op
);
1153 Dealloc_Op
:= Homonym
(Dealloc_Op
);
1158 -- Case of a class-wide pool type: make a dispatching call to
1159 -- Deallocate through the class-wide Deallocate_Any.
1161 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
1162 Set_Procedure_To_Call
(Free_Node
, RTE
(RE_Deallocate_Any
));
1164 -- Case of a specific pool type: make a statically bound call
1167 Set_Procedure_To_Call
(Free_Node
,
1168 Find_Prim_Op
(Etype
(Pool
), Name_Deallocate
));
1172 if Present
(Procedure_To_Call
(Free_Node
)) then
1174 -- For all cases of a Deallocate call, the back-end needs to be able
1175 -- to compute the size of the object being freed. This may require
1176 -- some adjustments for objects of dynamic size.
1178 -- If the type is class wide, we generate an implicit type with the
1179 -- right dynamic size, so that the deallocate call gets the right
1180 -- size parameter computed by GIGI. Same for an access to
1181 -- unconstrained packed array.
1183 if Is_Class_Wide_Type
(Desig_T
)
1185 (Is_Array_Type
(Desig_T
)
1186 and then not Is_Constrained
(Desig_T
)
1187 and then Is_Packed
(Desig_T
))
1190 Deref
: constant Node_Id
:=
1191 Make_Explicit_Dereference
(Loc
,
1192 Duplicate_Subexpr_No_Checks
(Arg
));
1197 -- Perform minor decoration as it is needed by the side effect
1198 -- removal mechanism.
1200 Set_Etype
(Deref
, Desig_T
);
1201 Set_Parent
(Deref
, Free_Node
);
1202 D_Subtyp
:= Make_Subtype_From_Expr
(Deref
, Desig_T
);
1204 if Nkind
(D_Subtyp
) in N_Has_Entity
then
1205 D_Type
:= Entity
(D_Subtyp
);
1208 D_Type
:= Make_Temporary
(Loc
, 'A');
1209 Insert_Action
(Deref
,
1210 Make_Subtype_Declaration
(Loc
,
1211 Defining_Identifier
=> D_Type
,
1212 Subtype_Indication
=> D_Subtyp
));
1215 -- Force freezing at the point of the dereference. For the
1216 -- class wide case, this avoids having the subtype frozen
1217 -- before the equivalent type.
1219 Freeze_Itype
(D_Type
, Deref
);
1221 Set_Actual_Designated_Subtype
(Free_Node
, D_Type
);
1227 -- Ada 2005 (AI-251): In case of abstract interface type we must
1228 -- displace the pointer to reference the base of the object to
1229 -- deallocate its memory, unless we're targetting a VM, in which case
1230 -- no special processing is required.
1233 -- free (Base_Address (Obj_Ptr))
1235 if Is_Interface
(Directly_Designated_Type
(Typ
))
1236 and then Tagged_Type_Expansion
1238 Set_Expression
(Free_Node
,
1239 Unchecked_Convert_To
(Typ
,
1240 Make_Function_Call
(Loc
,
1241 Name
=> New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
1242 Parameter_Associations
=> New_List
(
1243 Unchecked_Convert_To
(RTE
(RE_Address
), Free_Arg
)))));
1249 Set_Expression
(Free_Node
, Free_Arg
);
1252 -- Only remaining step is to set result to null, or generate a raise of
1253 -- Constraint_Error if the target object is "not null".
1255 if Can_Never_Be_Null
(Etype
(Arg
)) then
1257 Make_Raise_Constraint_Error
(Loc
,
1258 Reason
=> CE_Access_Check_Failed
));
1262 Lhs
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Arg
);
1264 Set_Assignment_OK
(Lhs
);
1266 Make_Assignment_Statement
(Loc
,
1268 Expression
=> Make_Null
(Loc
)));
1272 -- Generate a test of whether any earlier finalization raised an
1273 -- exception, and in that case raise Program_Error with the previous
1274 -- exception occurrence.
1277 -- if Raised and then not Abort then
1278 -- raise Program_Error; -- for .NET and
1279 -- -- restricted RTS
1281 -- Raise_From_Controlled_Operation (E); -- all other cases
1285 Append_To
(Stmts
, Build_Raise_Statement
(Finalizer_Data
));
1288 -- If we know the argument is non-null, then make a block statement
1289 -- that contains the required statements, no need for a test.
1291 if Arg_Known_Non_Null
then
1293 Make_Block_Statement
(Loc
,
1294 Handled_Statement_Sequence
=>
1295 Make_Handled_Sequence_Of_Statements
(Loc
,
1296 Statements
=> Stmts
));
1298 -- If the argument may be null, wrap the statements inside an IF that
1299 -- does an explicit test to exclude the null case.
1303 Make_Implicit_If_Statement
(N
,
1306 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
1307 Right_Opnd
=> Make_Null
(Loc
)),
1308 Then_Statements
=> Stmts
);
1313 Rewrite
(N
, Gen_Code
);
1316 -- If we generated a block with an At_End_Proc, expand the exception
1317 -- handler. We need to wait until after everything else is analyzed.
1319 if Present
(Blk
) then
1320 Expand_At_End_Handler
1321 (Handled_Statement_Sequence
(Blk
), Entity
(Identifier
(Blk
)));
1323 end Expand_Unc_Deallocation
;
1325 -----------------------
1326 -- Expand_To_Address --
1327 -----------------------
1329 procedure Expand_To_Address
(N
: Node_Id
) is
1330 Loc
: constant Source_Ptr
:= Sloc
(N
);
1331 Arg
: constant Node_Id
:= First_Actual
(N
);
1335 Remove_Side_Effects
(Arg
);
1337 Obj
:= Make_Explicit_Dereference
(Loc
, Relocate_Node
(Arg
));
1340 Make_If_Expression
(Loc
,
1341 Expressions
=> New_List
(
1343 Left_Opnd
=> New_Copy_Tree
(Arg
),
1344 Right_Opnd
=> Make_Null
(Loc
)),
1345 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
1346 Make_Attribute_Reference
(Loc
,
1348 Attribute_Name
=> Name_Address
))));
1350 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1351 end Expand_To_Address
;
1353 -----------------------
1354 -- Expand_To_Pointer --
1355 -----------------------
1357 procedure Expand_To_Pointer
(N
: Node_Id
) is
1358 Arg
: constant Node_Id
:= First_Actual
(N
);
1361 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1363 end Expand_To_Pointer
;