1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 Errout
; use Errout
;
31 with Exp_Atag
; use Exp_Atag
;
32 with Exp_Ch4
; use Exp_Ch4
;
33 with Exp_Ch7
; use Exp_Ch7
;
34 with Exp_Ch11
; use Exp_Ch11
;
35 with Exp_Code
; use Exp_Code
;
36 with Exp_Fixd
; use Exp_Fixd
;
37 with Exp_Util
; use Exp_Util
;
38 with Freeze
; use Freeze
;
39 with Namet
; use Namet
;
40 with Nmake
; use Nmake
;
41 with Nlists
; use Nlists
;
43 with Restrict
; use Restrict
;
44 with Rident
; use Rident
;
45 with Rtsfind
; use Rtsfind
;
47 with Sem_Eval
; use Sem_Eval
;
48 with Sem_Res
; use Sem_Res
;
49 with Sem_Type
; use Sem_Type
;
50 with Sem_Util
; use Sem_Util
;
51 with Sinfo
; use Sinfo
;
52 with Sinput
; use Sinput
;
53 with Snames
; use Snames
;
54 with Stand
; use Stand
;
55 with Stringt
; use Stringt
;
56 with Tbuild
; use Tbuild
;
57 with Uintp
; use Uintp
;
58 with Urealp
; use Urealp
;
60 package body Exp_Intr
is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Expand_Is_Negative
(N
: Node_Id
);
67 -- Expand a call to the intrinsic Is_Negative function
69 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
);
70 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
71 -- into a dispatching call to the actual subprogram associated with the
72 -- Constructor formal subprogram, passing it the Parameters actual of
73 -- the call to the instantiation and dispatching based on call's Tag
76 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
);
77 -- Expand a call to Exception_Information/Message/Name. The first
78 -- parameter, N, is the node for the function call, and Ent is the
79 -- entity for the corresponding routine in the Ada.Exceptions package.
81 procedure Expand_Import_Call
(N
: Node_Id
);
82 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
83 -- N is the node for the function call.
85 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
);
86 -- Expand an intrinsic shift operation, N and E are from the call to
87 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
88 -- K is the kind for the shift node
90 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
);
91 -- Expand a call to an instantiation of Unchecked_Conversion into a node
92 -- N_Unchecked_Type_Conversion.
94 procedure Expand_Unc_Deallocation
(N
: Node_Id
);
95 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
96 -- N_Free_Statement and appropriate context.
98 procedure Expand_To_Address
(N
: Node_Id
);
99 procedure Expand_To_Pointer
(N
: Node_Id
);
100 -- Expand a call to corresponding function, declared in an instance of
101 -- System.Address_To_Access_Conversions.
103 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
);
104 -- Rewrite the node by the appropriate string or positive constant.
105 -- Nam can be one of the following:
106 -- Name_File - expand string that is the name of source file
107 -- Name_Line - expand integer line number
108 -- Name_Source_Location - expand string of form file:line
109 -- Name_Enclosing_Entity - expand string with name of enclosing entity
111 -----------------------------------------
112 -- Expand_Dispatching_Constructor_Call --
113 -----------------------------------------
115 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
118 -- GDC_Instance (The_Tag, Parameters'Access)
120 -- to a class-wide conversion of a dispatching call to the actual
121 -- associated with the formal subprogram Construct, designating The_Tag
122 -- as the controlling tag of the call:
124 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
126 -- which will eventually be expanded to the following:
128 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
130 -- A class-wide membership test is also generated, preceding the call, to
131 -- ensure that the controlling tag denotes a type in T'Class.
133 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
) is
134 Loc
: constant Source_Ptr
:= Sloc
(N
);
135 Tag_Arg
: constant Node_Id
:= First_Actual
(N
);
136 Param_Arg
: constant Node_Id
:= Next_Actual
(Tag_Arg
);
137 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Entity
(Name
(N
))));
138 Inst_Pkg
: constant Node_Id
:= Parent
(Subp_Decl
);
139 Act_Rename
: Node_Id
;
140 Act_Constr
: Entity_Id
;
141 Iface_Tag
: Node_Id
:= Empty
;
142 Cnstr_Call
: Node_Id
;
143 Result_Typ
: Entity_Id
;
146 -- The subprogram is the third actual in the instantiation, and is
147 -- retrieved from the corresponding renaming declaration. However,
148 -- freeze nodes may appear before, so we retrieve the declaration
149 -- with an explicit loop.
151 Act_Rename
:= First
(Visible_Declarations
(Inst_Pkg
));
152 while Nkind
(Act_Rename
) /= N_Subprogram_Renaming_Declaration
loop
156 Act_Constr
:= Entity
(Name
(Act_Rename
));
157 Result_Typ
:= Class_Wide_Type
(Etype
(Act_Constr
));
159 -- Ada 2005 (AI-251): If the result is an interface type, the function
160 -- returns a class-wide interface type (otherwise the resulting object
161 -- would be abstract!)
163 if Is_Interface
(Etype
(Act_Constr
)) then
164 Set_Etype
(Act_Constr
, Result_Typ
);
166 -- If the result type is not parent of Tag_Arg then we need to
167 -- locate the tag of the secondary dispatch table.
169 if not Is_Ancestor
(Etype
(Result_Typ
), Etype
(Tag_Arg
)) then
170 pragma Assert
(not Is_Interface
(Etype
(Tag_Arg
)));
173 Make_Object_Declaration
(Loc
,
174 Defining_Identifier
=>
175 Make_Defining_Identifier
(Loc
, New_Internal_Name
('V')),
177 New_Reference_To
(RTE
(RE_Tag
), Loc
),
179 Make_Function_Call
(Loc
,
180 Name
=> New_Reference_To
(RTE
(RE_Secondary_Tag
), Loc
),
181 Parameter_Associations
=> New_List
(
182 Relocate_Node
(Tag_Arg
),
184 (Node
(First_Elmt
(Access_Disp_Table
185 (Etype
(Etype
(Act_Constr
))))),
187 Insert_Action
(N
, Iface_Tag
);
191 -- Create the call to the actual Constructor function
194 Make_Function_Call
(Loc
,
195 Name
=> New_Occurrence_Of
(Act_Constr
, Loc
),
196 Parameter_Associations
=> New_List
(Relocate_Node
(Param_Arg
)));
198 -- Establish its controlling tag from the tag passed to the instance
199 -- The tag may be given by a function call, in which case a temporary
200 -- should be generated now, to prevent out-of-order insertions during
201 -- the expansion of that call when stack-checking is enabled.
203 if Present
(Iface_Tag
) then
204 Set_Controlling_Argument
(Cnstr_Call
,
205 New_Occurrence_Of
(Defining_Identifier
(Iface_Tag
), Loc
));
207 Remove_Side_Effects
(Tag_Arg
);
208 Set_Controlling_Argument
(Cnstr_Call
,
209 Relocate_Node
(Tag_Arg
));
212 -- Rewrite and analyze the call to the instance as a class-wide
213 -- conversion of the call to the actual constructor.
215 Rewrite
(N
, Convert_To
(Result_Typ
, Cnstr_Call
));
216 Analyze_And_Resolve
(N
, Etype
(Act_Constr
));
218 -- Do not generate a run-time check on the built object if tag
219 -- checks are suppressed for the result type or VM_Target /= No_VM
221 if Tag_Checks_Suppressed
(Etype
(Result_Typ
))
222 or else not Tagged_Type_Expansion
226 -- Generate a class-wide membership test to ensure that the call's tag
227 -- argument denotes a type within the class. We must keep separate the
228 -- case in which the Result_Type of the constructor function is a tagged
229 -- type from the case in which it is an abstract interface because the
230 -- run-time subprogram required to check these cases differ (and have
231 -- one difference in their parameters profile).
233 -- Call CW_Membership if the Result_Type is a tagged type to look for
234 -- the tag in the table of ancestor tags.
236 elsif not Is_Interface
(Result_Typ
) then
238 Obj_Tag_Node
: Node_Id
:= Duplicate_Subexpr
(Tag_Arg
);
239 CW_Test_Node
: Node_Id
;
242 Build_CW_Membership
(Loc
,
243 Obj_Tag_Node
=> Obj_Tag_Node
,
246 Node
(First_Elmt
(Access_Disp_Table
(
247 Root_Type
(Result_Typ
)))), Loc
),
249 New_Node
=> CW_Test_Node
);
252 Make_Implicit_If_Statement
(N
,
254 Make_Op_Not
(Loc
, CW_Test_Node
),
256 New_List
(Make_Raise_Statement
(Loc
,
257 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
260 -- Call IW_Membership test if the Result_Type is an abstract interface
261 -- to look for the tag in the table of interface tags.
265 Make_Implicit_If_Statement
(N
,
268 Make_Function_Call
(Loc
,
269 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
270 Parameter_Associations
=> New_List
(
271 Make_Attribute_Reference
(Loc
,
272 Prefix
=> Duplicate_Subexpr
(Tag_Arg
),
273 Attribute_Name
=> Name_Address
),
276 Node
(First_Elmt
(Access_Disp_Table
(
277 Root_Type
(Result_Typ
)))), Loc
)))),
280 Make_Raise_Statement
(Loc
,
281 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
283 end Expand_Dispatching_Constructor_Call
;
285 ---------------------------
286 -- Expand_Exception_Call --
287 ---------------------------
289 -- If the function call is not within an exception handler, then the call
290 -- is replaced by a null string. Otherwise the appropriate routine in
291 -- Ada.Exceptions is called passing the choice parameter specification
292 -- from the enclosing handler. If the enclosing handler lacks a choice
293 -- parameter, then one is supplied.
295 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
) is
296 Loc
: constant Source_Ptr
:= Sloc
(N
);
301 -- Climb up parents to see if we are in exception handler
305 -- Case of not in exception handler, replace by null string
309 Make_String_Literal
(Loc
,
313 -- Case of in exception handler
315 elsif Nkind
(P
) = N_Exception_Handler
then
317 -- Handler cannot be used for a local raise, and furthermore, this
318 -- is a violation of the No_Exception_Propagation restriction.
320 Set_Local_Raise_Not_OK
(P
);
321 Check_Restriction
(No_Exception_Propagation
, N
);
323 -- If no choice parameter present, then put one there. Note that
324 -- we do not need to put it on the entity chain, since no one will
325 -- be referencing it by normal visibility methods.
327 if No
(Choice_Parameter
(P
)) then
328 E
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('E'));
329 Set_Choice_Parameter
(P
, E
);
330 Set_Ekind
(E
, E_Variable
);
331 Set_Etype
(E
, RTE
(RE_Exception_Occurrence
));
332 Set_Scope
(E
, Current_Scope
);
336 Make_Function_Call
(Loc
,
337 Name
=> New_Occurrence_Of
(RTE
(Ent
), Loc
),
338 Parameter_Associations
=> New_List
(
339 New_Occurrence_Of
(Choice_Parameter
(P
), Loc
))));
349 Analyze_And_Resolve
(N
, Standard_String
);
350 end Expand_Exception_Call
;
352 ------------------------
353 -- Expand_Import_Call --
354 ------------------------
356 -- The function call must have a static string as its argument. We create
357 -- a dummy variable which uses this string as the external name in an
358 -- Import pragma. The result is then obtained as the address of this
359 -- dummy variable, converted to the appropriate target type.
361 procedure Expand_Import_Call
(N
: Node_Id
) is
362 Loc
: constant Source_Ptr
:= Sloc
(N
);
363 Ent
: constant Entity_Id
:= Entity
(Name
(N
));
364 Str
: constant Node_Id
:= First_Actual
(N
);
368 Dum
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('D'));
370 Insert_Actions
(N
, New_List
(
371 Make_Object_Declaration
(Loc
,
372 Defining_Identifier
=> Dum
,
374 New_Occurrence_Of
(Standard_Character
, Loc
)),
377 Chars
=> Name_Import
,
378 Pragma_Argument_Associations
=> New_List
(
379 Make_Pragma_Argument_Association
(Loc
,
380 Expression
=> Make_Identifier
(Loc
, Name_Ada
)),
382 Make_Pragma_Argument_Association
(Loc
,
383 Expression
=> Make_Identifier
(Loc
, Chars
(Dum
))),
385 Make_Pragma_Argument_Association
(Loc
,
386 Chars
=> Name_Link_Name
,
387 Expression
=> Relocate_Node
(Str
))))));
390 Unchecked_Convert_To
(Etype
(Ent
),
391 Make_Attribute_Reference
(Loc
,
392 Prefix
=> Make_Identifier
(Loc
, Chars
(Dum
)),
393 Attribute_Name
=> Name_Address
)));
395 Analyze_And_Resolve
(N
, Etype
(Ent
));
396 end Expand_Import_Call
;
398 ---------------------------
399 -- Expand_Intrinsic_Call --
400 ---------------------------
402 procedure Expand_Intrinsic_Call
(N
: Node_Id
; E
: Entity_Id
) is
406 -- If an external name is specified for the intrinsic, it is handled
407 -- by the back-end: leave the call node unchanged for now.
409 if Present
(Interface_Name
(E
)) then
413 -- If the intrinsic subprogram is generic, gets its original name
415 if Present
(Parent
(E
))
416 and then Present
(Generic_Parent
(Parent
(E
)))
418 Nam
:= Chars
(Generic_Parent
(Parent
(E
)));
423 if Nam
= Name_Asm
then
426 elsif Nam
= Name_Divide
then
427 Expand_Decimal_Divide_Call
(N
);
429 elsif Nam
= Name_Exception_Information
then
430 Expand_Exception_Call
(N
, RE_Exception_Information
);
432 elsif Nam
= Name_Exception_Message
then
433 Expand_Exception_Call
(N
, RE_Exception_Message
);
435 elsif Nam
= Name_Exception_Name
then
436 Expand_Exception_Call
(N
, RE_Exception_Name_Simple
);
438 elsif Nam
= Name_Generic_Dispatching_Constructor
then
439 Expand_Dispatching_Constructor_Call
(N
);
441 elsif Nam
= Name_Import_Address
443 Nam
= Name_Import_Largest_Value
445 Nam
= Name_Import_Value
447 Expand_Import_Call
(N
);
449 elsif Nam
= Name_Is_Negative
then
450 Expand_Is_Negative
(N
);
452 elsif Nam
= Name_Rotate_Left
then
453 Expand_Shift
(N
, E
, N_Op_Rotate_Left
);
455 elsif Nam
= Name_Rotate_Right
then
456 Expand_Shift
(N
, E
, N_Op_Rotate_Right
);
458 elsif Nam
= Name_Shift_Left
then
459 Expand_Shift
(N
, E
, N_Op_Shift_Left
);
461 elsif Nam
= Name_Shift_Right
then
462 Expand_Shift
(N
, E
, N_Op_Shift_Right
);
464 elsif Nam
= Name_Shift_Right_Arithmetic
then
465 Expand_Shift
(N
, E
, N_Op_Shift_Right_Arithmetic
);
467 elsif Nam
= Name_Unchecked_Conversion
then
468 Expand_Unc_Conversion
(N
, E
);
470 elsif Nam
= Name_Unchecked_Deallocation
then
471 Expand_Unc_Deallocation
(N
);
473 elsif Nam
= Name_To_Address
then
474 Expand_To_Address
(N
);
476 elsif Nam
= Name_To_Pointer
then
477 Expand_To_Pointer
(N
);
479 elsif Nam
= Name_File
480 or else Nam
= Name_Line
481 or else Nam
= Name_Source_Location
482 or else Nam
= Name_Enclosing_Entity
484 Expand_Source_Info
(N
, Nam
);
486 -- If we have a renaming, expand the call to the original operation,
487 -- which must itself be intrinsic, since renaming requires matching
488 -- conventions and this has already been checked.
490 elsif Present
(Alias
(E
)) then
491 Expand_Intrinsic_Call
(N
, Alias
(E
));
493 -- The only other case is where an external name was specified,
494 -- since this is the only way that an otherwise unrecognized
495 -- name could escape the checking in Sem_Prag. Nothing needs
496 -- to be done in such a case, since we pass such a call to the
497 -- back end unchanged.
502 end Expand_Intrinsic_Call
;
504 ------------------------
505 -- Expand_Is_Negative --
506 ------------------------
508 procedure Expand_Is_Negative
(N
: Node_Id
) is
509 Loc
: constant Source_Ptr
:= Sloc
(N
);
510 Opnd
: constant Node_Id
:= Relocate_Node
(First_Actual
(N
));
514 -- We replace the function call by the following expression
516 -- if Opnd < 0.0 then
519 -- if Opnd > 0.0 then
522 -- Float_Unsigned!(Float (Opnd)) /= 0
527 Make_Conditional_Expression
(Loc
,
528 Expressions
=> New_List
(
530 Left_Opnd
=> Duplicate_Subexpr
(Opnd
),
531 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
533 New_Occurrence_Of
(Standard_True
, Loc
),
535 Make_Conditional_Expression
(Loc
,
536 Expressions
=> New_List
(
538 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Opnd
),
539 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
541 New_Occurrence_Of
(Standard_False
, Loc
),
546 (RTE
(RE_Float_Unsigned
),
549 Duplicate_Subexpr_No_Checks
(Opnd
))),
551 Make_Integer_Literal
(Loc
, 0)))))));
553 Analyze_And_Resolve
(N
, Standard_Boolean
);
554 end Expand_Is_Negative
;
560 -- This procedure is used to convert a call to a shift function to the
561 -- corresponding operator node. This conversion is not done by the usual
562 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
563 -- operator nodes, because shifts are not predefined operators.
565 -- As a result, whenever a shift is used in the source program, it will
566 -- remain as a call until converted by this routine to the operator node
567 -- form which Gigi is expecting to see.
569 -- Note: it is possible for the expander to generate shift operator nodes
570 -- directly, which will be analyzed in the normal manner by calling Analyze
571 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
573 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
) is
574 Loc
: constant Source_Ptr
:= Sloc
(N
);
575 Typ
: constant Entity_Id
:= Etype
(N
);
576 Left
: constant Node_Id
:= First_Actual
(N
);
577 Right
: constant Node_Id
:= Next_Actual
(Left
);
578 Ltyp
: constant Node_Id
:= Etype
(Left
);
579 Rtyp
: constant Node_Id
:= Etype
(Right
);
583 Snode
:= New_Node
(K
, Loc
);
584 Set_Left_Opnd
(Snode
, Relocate_Node
(Left
));
585 Set_Right_Opnd
(Snode
, Relocate_Node
(Right
));
586 Set_Chars
(Snode
, Chars
(E
));
587 Set_Etype
(Snode
, Base_Type
(Typ
));
588 Set_Entity
(Snode
, E
);
590 if Compile_Time_Known_Value
(Type_High_Bound
(Rtyp
))
591 and then Expr_Value
(Type_High_Bound
(Rtyp
)) < Esize
(Ltyp
)
593 Set_Shift_Count_OK
(Snode
, True);
596 -- Do the rewrite. Note that we don't call Analyze and Resolve on
597 -- this node, because it already got analyzed and resolved when
598 -- it was a function call!
604 ------------------------
605 -- Expand_Source_Info --
606 ------------------------
608 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
) is
609 Loc
: constant Source_Ptr
:= Sloc
(N
);
612 procedure Write_Entity_Name
(E
: Entity_Id
);
613 -- Recursive procedure to construct string for qualified name of
614 -- enclosing program unit. The qualification stops at an enclosing
615 -- scope has no source name (block or loop). If entity is a subprogram
616 -- instance, skip enclosing wrapper package.
618 -----------------------
619 -- Write_Entity_Name --
620 -----------------------
622 procedure Write_Entity_Name
(E
: Entity_Id
) is
624 TDef
: constant Source_Buffer_Ptr
:=
625 Source_Text
(Get_Source_File_Index
(Sloc
(E
)));
628 -- Nothing to do if at outer level
630 if Scope
(E
) = Standard_Standard
then
633 -- If scope comes from source, write its name
635 elsif Comes_From_Source
(Scope
(E
)) then
636 Write_Entity_Name
(Scope
(E
));
637 Add_Char_To_Name_Buffer
('.');
639 -- If in wrapper package skip past it
641 elsif Is_Wrapper_Package
(Scope
(E
)) then
642 Write_Entity_Name
(Scope
(Scope
(E
)));
643 Add_Char_To_Name_Buffer
('.');
645 -- Otherwise nothing to output (happens in unnamed block statements)
651 -- Loop to output the name
653 -- is this right wrt wide char encodings ??? (no!)
656 while TDef
(SDef
) in '0' .. '9'
657 or else TDef
(SDef
) >= 'A'
658 or else TDef
(SDef
) = ASCII
.ESC
660 Add_Char_To_Name_Buffer
(TDef
(SDef
));
663 end Write_Entity_Name
;
665 -- Start of processing for Expand_Source_Info
670 if Nam
= Name_Line
then
672 Make_Integer_Literal
(Loc
,
673 Intval
=> UI_From_Int
(Int
(Get_Logical_Line_Number
(Loc
)))));
674 Analyze_And_Resolve
(N
, Standard_Positive
);
683 Get_Decoded_Name_String
684 (Reference_Name
(Get_Source_File_Index
(Loc
)));
686 when Name_Source_Location
=>
687 Build_Location_String
(Loc
);
689 when Name_Enclosing_Entity
=>
691 -- Skip enclosing blocks to reach enclosing unit
693 Ent
:= Current_Scope
;
694 while Present
(Ent
) loop
695 exit when Ekind
(Ent
) /= E_Block
696 and then Ekind
(Ent
) /= E_Loop
;
700 -- Ent now points to the relevant defining entity
702 Write_Entity_Name
(Ent
);
709 Make_String_Literal
(Loc
,
710 Strval
=> String_From_Name_Buffer
));
711 Analyze_And_Resolve
(N
, Standard_String
);
714 Set_Is_Static_Expression
(N
);
715 end Expand_Source_Info
;
717 ---------------------------
718 -- Expand_Unc_Conversion --
719 ---------------------------
721 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
) is
722 Func
: constant Entity_Id
:= Entity
(Name
(N
));
728 -- Rewrite as unchecked conversion node. Note that we must convert
729 -- the operand to the formal type of the input parameter of the
730 -- function, so that the resulting N_Unchecked_Type_Conversion
731 -- call indicates the correct types for Gigi.
733 -- Right now, we only do this if a scalar type is involved. It is
734 -- not clear if it is needed in other cases. If we do attempt to
735 -- do the conversion unconditionally, it crashes 3411-018. To be
736 -- investigated further ???
738 Conv
:= Relocate_Node
(First_Actual
(N
));
739 Ftyp
:= Etype
(First_Formal
(Func
));
741 if Is_Scalar_Type
(Ftyp
) then
742 Conv
:= Convert_To
(Ftyp
, Conv
);
743 Set_Parent
(Conv
, N
);
744 Analyze_And_Resolve
(Conv
);
747 -- The instantiation of Unchecked_Conversion creates a wrapper package,
748 -- and the target type is declared as a subtype of the actual. Recover
749 -- the actual, which is the subtype indic. in the subtype declaration
750 -- for the target type. This is semantically correct, and avoids
751 -- anomalies with access subtypes. For entities, leave type as is.
753 -- We do the analysis here, because we do not want the compiler
754 -- to try to optimize or otherwise reorganize the unchecked
759 if Is_Entity_Name
(Conv
) then
762 elsif Nkind
(Parent
(Ttyp
)) = N_Subtype_Declaration
then
763 Ttyp
:= Entity
(Subtype_Indication
(Parent
(Etype
(E
))));
765 elsif Is_Itype
(Ttyp
) then
767 Entity
(Subtype_Indication
(Associated_Node_For_Itype
(Ttyp
)));
772 Rewrite
(N
, Unchecked_Convert_To
(Ttyp
, Conv
));
776 if Nkind
(N
) = N_Unchecked_Type_Conversion
then
777 Expand_N_Unchecked_Type_Conversion
(N
);
779 end Expand_Unc_Conversion
;
781 -----------------------------
782 -- Expand_Unc_Deallocation --
783 -----------------------------
785 -- Generate the following Code :
787 -- if Arg /= null then
788 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
793 -- For a task, we also generate a call to Free_Task to ensure that the
794 -- task itself is freed if it is terminated, ditto for a simple protected
795 -- object, with a call to Finalize_Protection. For composite types that
796 -- have tasks or simple protected objects as components, we traverse the
797 -- structures to find and terminate those components.
799 procedure Expand_Unc_Deallocation
(N
: Node_Id
) is
800 Loc
: constant Source_Ptr
:= Sloc
(N
);
801 Arg
: constant Node_Id
:= First_Actual
(N
);
802 Typ
: constant Entity_Id
:= Etype
(Arg
);
803 Stmts
: constant List_Id
:= New_List
;
804 Rtyp
: constant Entity_Id
:= Underlying_Type
(Root_Type
(Typ
));
805 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Rtyp
);
807 Desig_T
: constant Entity_Id
:= Designated_Type
(Typ
);
815 Arg_Known_Non_Null
: constant Boolean := Known_Non_Null
(N
);
816 -- This captures whether we know the argument to be non-null so that
817 -- we can avoid the test. The reason that we need to capture this is
818 -- that we analyze some generated statements before properly attaching
819 -- them to the tree, and that can disturb current value settings.
822 if No_Pool_Assigned
(Rtyp
) then
823 Error_Msg_N
("?deallocation from empty storage pool!", N
);
826 -- Nothing to do if we know the argument is null
828 if Known_Null
(N
) then
832 -- Processing for pointer to controlled type
834 if Needs_Finalization
(Desig_T
) then
836 Make_Explicit_Dereference
(Loc
,
837 Prefix
=> Duplicate_Subexpr_No_Checks
(Arg
));
839 -- If the type is tagged, then we must force dispatching on the
840 -- finalization call because the designated type may not be the
841 -- actual type of the object.
843 if Is_Tagged_Type
(Desig_T
)
844 and then not Is_Class_Wide_Type
(Desig_T
)
846 Deref
:= Unchecked_Convert_To
(Class_Wide_Type
(Desig_T
), Deref
);
848 elsif not Is_Tagged_Type
(Desig_T
) then
850 -- Set type of result, to force a conversion when needed (see
851 -- exp_ch7, Convert_View), given that Deep_Finalize may be
852 -- inherited from the parent type, and we need the type of the
853 -- expression to see whether the conversion is in fact needed.
855 Set_Etype
(Deref
, Desig_T
);
862 With_Detach
=> New_Reference_To
(Standard_True
, Loc
));
864 if Abort_Allowed
then
865 Prepend_To
(Free_Cod
,
866 Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
869 Make_Block_Statement
(Loc
, Handled_Statement_Sequence
=>
870 Make_Handled_Sequence_Of_Statements
(Loc
,
871 Statements
=> Free_Cod
,
873 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
)));
875 -- We now expand the exception (at end) handler. We set a
876 -- temporary parent pointer since we have not attached Blk
881 Expand_At_End_Handler
882 (Handled_Statement_Sequence
(Blk
), Entity
(Identifier
(Blk
)));
885 -- We kill saved current values, since analyzing statements not
886 -- properly attached to the tree can set wrong current values.
891 Append_List_To
(Stmts
, Free_Cod
);
895 -- For a task type, call Free_Task before freeing the ATCB
897 if Is_Task_Type
(Desig_T
) then
899 Stat
: Node_Id
:= Prev
(N
);
904 -- An Abort followed by a Free will not do what the user
905 -- expects, because the abort is not immediate. This is
906 -- worth a friendly warning.
909 and then not Comes_From_Source
(Original_Node
(Stat
))
915 and then Nkind
(Original_Node
(Stat
)) = N_Abort_Statement
917 Stat
:= Original_Node
(Stat
);
918 Nam1
:= First
(Names
(Stat
));
919 Nam2
:= Original_Node
(First
(Parameter_Associations
(N
)));
921 if Nkind
(Nam1
) = N_Explicit_Dereference
922 and then Is_Entity_Name
(Prefix
(Nam1
))
923 and then Is_Entity_Name
(Nam2
)
924 and then Entity
(Prefix
(Nam1
)) = Entity
(Nam2
)
926 Error_Msg_N
("abort may take time to complete?", N
);
927 Error_Msg_N
("\deallocation might have no effect?", N
);
928 Error_Msg_N
("\safer to wait for termination.?", N
);
934 (Stmts
, Cleanup_Task
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
936 -- For composite types that contain tasks, recurse over the structure
937 -- to build the selectors for the task subcomponents.
939 elsif Has_Task
(Desig_T
) then
940 if Is_Record_Type
(Desig_T
) then
941 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_T
));
943 elsif Is_Array_Type
(Desig_T
) then
944 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_T
));
948 -- Same for simple protected types. Eventually call Finalize_Protection
949 -- before freeing the PO for each protected component.
951 if Is_Simple_Protected_Type
(Desig_T
) then
953 Cleanup_Protected_Object
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
955 elsif Has_Simple_Protected_Object
(Desig_T
) then
956 if Is_Record_Type
(Desig_T
) then
957 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_T
));
958 elsif Is_Array_Type
(Desig_T
) then
959 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_T
));
963 -- Normal processing for non-controlled types
965 Free_Arg
:= Duplicate_Subexpr_No_Checks
(Arg
);
966 Free_Node
:= Make_Free_Statement
(Loc
, Empty
);
967 Append_To
(Stmts
, Free_Node
);
968 Set_Storage_Pool
(Free_Node
, Pool
);
970 -- Deal with storage pool
972 if Present
(Pool
) then
974 -- Freeing the secondary stack is meaningless
976 if Is_RTE
(Pool
, RE_SS_Pool
) then
979 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
981 -- Case of a class-wide pool type: make a dispatching call
982 -- to Deallocate through the class-wide Deallocate_Any.
984 Set_Procedure_To_Call
(Free_Node
,
985 RTE
(RE_Deallocate_Any
));
988 -- Case of a specific pool type: make a statically bound call
990 Set_Procedure_To_Call
(Free_Node
,
991 Find_Prim_Op
(Etype
(Pool
), Name_Deallocate
));
995 if Present
(Procedure_To_Call
(Free_Node
)) then
997 -- For all cases of a Deallocate call, the back-end needs to be
998 -- able to compute the size of the object being freed. This may
999 -- require some adjustments for objects of dynamic size.
1001 -- If the type is class wide, we generate an implicit type with the
1002 -- right dynamic size, so that the deallocate call gets the right
1003 -- size parameter computed by GIGI. Same for an access to
1004 -- unconstrained packed array.
1006 if Is_Class_Wide_Type
(Desig_T
)
1008 (Is_Array_Type
(Desig_T
)
1009 and then not Is_Constrained
(Desig_T
)
1010 and then Is_Packed
(Desig_T
))
1013 Deref
: constant Node_Id
:=
1014 Make_Explicit_Dereference
(Loc
,
1015 Duplicate_Subexpr_No_Checks
(Arg
));
1020 Set_Etype
(Deref
, Typ
);
1021 Set_Parent
(Deref
, Free_Node
);
1022 D_Subtyp
:= Make_Subtype_From_Expr
(Deref
, Desig_T
);
1024 if Nkind
(D_Subtyp
) in N_Has_Entity
then
1025 D_Type
:= Entity
(D_Subtyp
);
1028 D_Type
:= Make_Defining_Identifier
(Loc
,
1029 New_Internal_Name
('A'));
1030 Insert_Action
(Deref
,
1031 Make_Subtype_Declaration
(Loc
,
1032 Defining_Identifier
=> D_Type
,
1033 Subtype_Indication
=> D_Subtyp
));
1037 -- Force freezing at the point of the dereference. For the
1038 -- class wide case, this avoids having the subtype frozen
1039 -- before the equivalent type.
1041 Freeze_Itype
(D_Type
, Deref
);
1043 Set_Actual_Designated_Subtype
(Free_Node
, D_Type
);
1049 -- Ada 2005 (AI-251): In case of abstract interface type we must
1050 -- displace the pointer to reference the base of the object to
1051 -- deallocate its memory, unless we're targetting a VM, in which case
1052 -- no special processing is required.
1055 -- free (Base_Address (Obj_Ptr))
1057 if Is_Interface
(Directly_Designated_Type
(Typ
))
1058 and then Tagged_Type_Expansion
1060 Set_Expression
(Free_Node
,
1061 Unchecked_Convert_To
(Typ
,
1062 Make_Function_Call
(Loc
,
1063 Name
=> New_Reference_To
(RTE
(RE_Base_Address
), Loc
),
1064 Parameter_Associations
=> New_List
(
1065 Unchecked_Convert_To
(RTE
(RE_Address
), Free_Arg
)))));
1071 Set_Expression
(Free_Node
, Free_Arg
);
1074 -- Only remaining step is to set result to null, or generate a
1075 -- raise of constraint error if the target object is "not null".
1077 if Can_Never_Be_Null
(Etype
(Arg
)) then
1079 Make_Raise_Constraint_Error
(Loc
,
1080 Reason
=> CE_Access_Check_Failed
));
1084 Lhs
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Arg
);
1086 Set_Assignment_OK
(Lhs
);
1088 Make_Assignment_Statement
(Loc
,
1090 Expression
=> Make_Null
(Loc
)));
1094 -- If we know the argument is non-null, then make a block statement
1095 -- that contains the required statements, no need for a test.
1097 if Arg_Known_Non_Null
then
1099 Make_Block_Statement
(Loc
,
1100 Handled_Statement_Sequence
=>
1101 Make_Handled_Sequence_Of_Statements
(Loc
,
1102 Statements
=> Stmts
));
1104 -- If the argument may be null, wrap the statements inside an IF that
1105 -- does an explicit test to exclude the null case.
1109 Make_Implicit_If_Statement
(N
,
1112 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
1113 Right_Opnd
=> Make_Null
(Loc
)),
1114 Then_Statements
=> Stmts
);
1119 Rewrite
(N
, Gen_Code
);
1121 end Expand_Unc_Deallocation
;
1123 -----------------------
1124 -- Expand_To_Address --
1125 -----------------------
1127 procedure Expand_To_Address
(N
: Node_Id
) is
1128 Loc
: constant Source_Ptr
:= Sloc
(N
);
1129 Arg
: constant Node_Id
:= First_Actual
(N
);
1133 Remove_Side_Effects
(Arg
);
1135 Obj
:= Make_Explicit_Dereference
(Loc
, Relocate_Node
(Arg
));
1138 Make_Conditional_Expression
(Loc
,
1139 Expressions
=> New_List
(
1141 Left_Opnd
=> New_Copy_Tree
(Arg
),
1142 Right_Opnd
=> Make_Null
(Loc
)),
1143 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
1144 Make_Attribute_Reference
(Loc
,
1146 Attribute_Name
=> Name_Address
))));
1148 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1149 end Expand_To_Address
;
1151 -----------------------
1152 -- Expand_To_Pointer --
1153 -----------------------
1155 procedure Expand_To_Pointer
(N
: Node_Id
) is
1156 Arg
: constant Node_Id
:= First_Actual
(N
);
1159 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1161 end Expand_To_Pointer
;