1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Einfo
.Entities
; use Einfo
.Entities
;
30 with Einfo
.Utils
; use Einfo
.Utils
;
31 with Elists
; use Elists
;
32 with Expander
; use Expander
;
33 with Exp_Atag
; use Exp_Atag
;
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
;
44 with Restrict
; use Restrict
;
45 with Rident
; use Rident
;
46 with Rtsfind
; use Rtsfind
;
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 Sinfo
.Nodes
; use Sinfo
.Nodes
;
55 with Sinfo
.Utils
; use Sinfo
.Utils
;
56 with Sinput
; use Sinput
;
57 with Snames
; use Snames
;
58 with Stand
; use Stand
;
59 with Tbuild
; use Tbuild
;
60 with Uintp
; use Uintp
;
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_Dispatching_Constructor_Call
(N
: Node_Id
);
73 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
74 -- into a dispatching call to the actual subprogram associated with the
75 -- Constructor formal subprogram, passing it the Parameters actual of
76 -- the call to the instantiation and dispatching based on call's Tag
79 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
);
80 -- Expand a call to Exception_Information/Message/Name. The first
81 -- parameter, N, is the node for the function call, and Ent is the
82 -- entity for the corresponding routine in the Ada.Exceptions package.
84 procedure Expand_Import_Call
(N
: Node_Id
);
85 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
86 -- N is the node for the function call.
88 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
);
89 -- Expand an intrinsic shift operation, N and E are from the call to
90 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
91 -- K is the kind for the shift node
93 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
);
94 -- Expand a call to an instantiation of Unchecked_Conversion into a node
95 -- N_Unchecked_Type_Conversion.
97 procedure Expand_Unc_Deallocation
(N
: Node_Id
);
98 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
99 -- N_Free_Statement and appropriate context.
101 procedure Expand_To_Address
(N
: Node_Id
);
102 -- Expand a call to corresponding function from System.Storage_Elements or
103 -- declared in an instance of System.Address_To_Access_Conversions.
105 procedure Expand_To_Integer
(N
: Node_Id
);
106 -- Expand a call to corresponding function from System.Storage_Elements
108 procedure Expand_To_Pointer
(N
: Node_Id
);
109 -- Expand a call to corresponding function, declared in an instance of
110 -- System.Address_To_Access_Conversions.
112 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
);
113 -- Rewrite the node as the appropriate string literal or positive
114 -- constant. Nam is the name of one of the intrinsics declared in
115 -- GNAT.Source_Info; see g-souinf.ads for documentation of these
118 ---------------------
119 -- Add_Source_Info --
120 ---------------------
122 procedure Add_Source_Info
123 (Buf
: in out Bounded_String
;
130 Append
(Buf
, Nat
(Get_Logical_Line_Number
(Loc
)));
133 Append
(Buf
, Reference_Name
(Get_Source_File_Index
(Loc
)));
135 when Name_Source_Location
=>
136 Build_Location_String
(Buf
, Loc
);
138 when Name_Enclosing_Entity
=>
140 -- Skip enclosing blocks to reach enclosing unit
143 Ent
: Entity_Id
:= Current_Scope
;
145 while Present
(Ent
) loop
146 exit when Ekind
(Ent
) not in E_Block | E_Loop
;
150 -- Ent now points to the relevant defining entity
152 Append_Entity_Name
(Buf
, Ent
);
155 when Name_Compilation_ISO_Date
=>
156 Append
(Buf
, Opt
.Compilation_Time
(1 .. 10));
158 when Name_Compilation_Date
=>
160 subtype S13
is String (1 .. 3);
161 Months
: constant array (1 .. 12) of S13
:=
162 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
163 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
165 M1
: constant Character := Opt
.Compilation_Time
(6);
166 M2
: constant Character := Opt
.Compilation_Time
(7);
168 MM
: constant Natural range 1 .. 12 :=
169 (Character'Pos (M1
) - Character'Pos ('0')) * 10 +
170 (Character'Pos (M2
) - Character'Pos ('0'));
173 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
175 Append
(Buf
, Months
(MM
));
177 Append
(Buf
, Opt
.Compilation_Time
(9 .. 10));
179 Append
(Buf
, Opt
.Compilation_Time
(1 .. 4));
182 when Name_Compilation_Time
=>
183 Append
(Buf
, Opt
.Compilation_Time
(12 .. 19));
190 ---------------------------------
191 -- Expand_Binary_Operator_Call --
192 ---------------------------------
194 procedure Expand_Binary_Operator_Call
(N
: Node_Id
) is
195 T1
: constant Entity_Id
:= Underlying_Type
(Etype
(Left_Opnd
(N
)));
196 T2
: constant Entity_Id
:= Underlying_Type
(Etype
(Right_Opnd
(N
)));
197 TR
: constant Entity_Id
:= Etype
(N
);
201 Siz
: constant Uint
:= UI_Max
(RM_Size
(T1
), RM_Size
(T2
));
202 -- Maximum of operand sizes
205 -- Nothing to do if the operands have the same modular type
207 if Base_Type
(T1
) = Base_Type
(T2
)
208 and then Is_Modular_Integer_Type
(T1
)
213 -- Use the appropriate type for the size
216 T3
:= RTE
(RE_Unsigned_32
);
219 T3
:= RTE
(RE_Unsigned_64
);
221 else pragma Assert
(Siz
<= 128);
222 T3
:= RTE
(RE_Unsigned_128
);
225 -- Copy operator node, and reset type and entity fields, for
226 -- subsequent reanalysis.
232 when N_Op_And
=> Set_Entity
(Res
, Standard_Op_And
);
233 when N_Op_Or
=> Set_Entity
(Res
, Standard_Op_Or
);
234 when N_Op_Xor
=> Set_Entity
(Res
, Standard_Op_Xor
);
235 when others => raise Program_Error
;
238 -- Convert operands to large enough intermediate type
241 Unchecked_Convert_To
(T3
, Relocate_Node
(Left_Opnd
(N
))));
243 Unchecked_Convert_To
(T3
, Relocate_Node
(Right_Opnd
(N
))));
245 -- Analyze and resolve result formed by conversion to target type
247 Rewrite
(N
, Unchecked_Convert_To
(TR
, Res
));
248 Analyze_And_Resolve
(N
, TR
);
249 end Expand_Binary_Operator_Call
;
251 -----------------------------------------
252 -- Expand_Dispatching_Constructor_Call --
253 -----------------------------------------
255 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
258 -- GDC_Instance (The_Tag, Parameters'Access)
260 -- to a class-wide conversion of a dispatching call to the actual
261 -- associated with the formal subprogram Construct, designating The_Tag
262 -- as the controlling tag of the call:
264 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
266 -- which will eventually be expanded to the following:
268 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
270 -- A class-wide membership test is also generated, preceding the call, to
271 -- ensure that the controlling tag denotes a type in T'Class.
273 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
) is
274 Loc
: constant Source_Ptr
:= Sloc
(N
);
275 Tag_Arg
: constant Node_Id
:= First_Actual
(N
);
276 Param_Arg
: constant Node_Id
:= Next_Actual
(Tag_Arg
);
277 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Entity
(Name
(N
))));
278 Inst_Pkg
: constant Node_Id
:= Parent
(Subp_Decl
);
279 Act_Rename
: Node_Id
;
280 Act_Constr
: Entity_Id
;
281 Iface_Tag
: Node_Id
:= Empty
;
282 Cnstr_Call
: Node_Id
;
283 Result_Typ
: Entity_Id
;
286 pragma Assert
(Is_Class_Wide_Type
(Etype
(Entity
(Name
(N
)))));
288 -- Remove side effects from tag argument early, before rewriting
289 -- the dispatching constructor call, as Remove_Side_Effects relies
290 -- on Tag_Arg's Parent link properly attached to the tree (once the
291 -- call is rewritten, the Parent is inconsistent as it points to the
292 -- rewritten node, which is not the syntactic parent of the Tag_Arg
295 Remove_Side_Effects
(Tag_Arg
);
297 -- Check that we have a proper tag
300 Make_Implicit_If_Statement
(N
,
301 Condition
=> Make_Op_Eq
(Loc
,
302 Left_Opnd
=> New_Copy_Tree
(Tag_Arg
),
303 Right_Opnd
=> New_Occurrence_Of
(RTE
(RE_No_Tag
), Loc
)),
305 Then_Statements
=> New_List
(
306 Make_Raise_Statement
(Loc
,
307 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
309 -- Check that it is not the tag of an abstract type
312 Make_Implicit_If_Statement
(N
,
313 Condition
=> Make_Function_Call
(Loc
,
315 New_Occurrence_Of
(RTE
(RE_Is_Abstract
), Loc
),
316 Parameter_Associations
=> New_List
(New_Copy_Tree
(Tag_Arg
))),
318 Then_Statements
=> New_List
(
319 Make_Raise_Statement
(Loc
,
320 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
322 -- The subprogram is the third actual in the instantiation, and is
323 -- retrieved from the corresponding renaming declaration. However,
324 -- freeze nodes may appear before, so we retrieve the declaration
325 -- with an explicit loop.
327 Act_Rename
:= First
(Visible_Declarations
(Inst_Pkg
));
328 while Nkind
(Act_Rename
) /= N_Subprogram_Renaming_Declaration
loop
332 Act_Constr
:= Entity
(Name
(Act_Rename
));
333 Result_Typ
:= Class_Wide_Type
(Etype
(Act_Constr
));
335 -- Check that the accessibility level of the tag is no deeper than that
336 -- of the constructor function (unless CodePeer_Mode).
338 if not CodePeer_Mode
then
340 Make_Implicit_If_Statement
(N
,
344 Build_Get_Access_Level
(Loc
, New_Copy_Tree
(Tag_Arg
)),
347 (Loc
, Scope_Depth_Default_0
(Act_Constr
))),
349 Then_Statements
=> New_List
(
350 Make_Raise_Statement
(Loc
,
351 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
354 if Is_Interface
(Etype
(Act_Constr
)) then
356 -- If the result type is not known to be a parent of Tag_Arg then we
357 -- need to locate the tag of the secondary dispatch table.
359 if not Is_Ancestor
(Etype
(Result_Typ
), Etype
(Tag_Arg
),
360 Use_Full_View
=> True)
361 and then Tagged_Type_Expansion
363 -- Obtain the reference to the Ada.Tags service before generating
364 -- the Object_Declaration node to ensure that if this service is
365 -- not available in the runtime then we generate a clear error.
368 Fname
: constant Node_Id
:=
369 New_Occurrence_Of
(RTE
(RE_Secondary_Tag
), Loc
);
372 pragma Assert
(not Is_Interface
(Etype
(Tag_Arg
)));
374 -- The tag is the first entry in the dispatch table of the
375 -- return type of the constructor.
378 Make_Object_Declaration
(Loc
,
379 Defining_Identifier
=> Make_Temporary
(Loc
, 'V'),
381 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
383 Make_Function_Call
(Loc
,
385 Parameter_Associations
=> New_List
(
386 Relocate_Node
(Tag_Arg
),
389 (Access_Disp_Table
(Etype
(Act_Constr
)))),
391 Insert_Action
(N
, Iface_Tag
);
396 -- Create the call to the actual Constructor function
399 Make_Function_Call
(Loc
,
400 Name
=> New_Occurrence_Of
(Act_Constr
, Loc
),
401 Parameter_Associations
=> New_List
(Relocate_Node
(Param_Arg
)));
403 -- Establish its controlling tag from the tag passed to the instance
404 -- The tag may be given by a function call, in which case a temporary
405 -- should be generated now, to prevent out-of-order insertions during
406 -- the expansion of that call when stack-checking is enabled.
408 if Present
(Iface_Tag
) then
409 Set_Controlling_Argument
(Cnstr_Call
,
410 New_Occurrence_Of
(Defining_Identifier
(Iface_Tag
), Loc
));
412 Set_Controlling_Argument
(Cnstr_Call
,
413 Relocate_Node
(Tag_Arg
));
416 -- Rewrite and analyze the call to the instance as a class-wide
417 -- conversion of the call to the actual constructor. When the result
418 -- type is a class-wide interface type this conversion is required to
419 -- force the displacement of the pointer to the object to reference the
420 -- corresponding dispatch table.
422 Rewrite
(N
, Convert_To
(Result_Typ
, Cnstr_Call
));
424 -- Do not generate a run-time check on the built object if tag
425 -- checks are suppressed for the result type or tagged type expansion
426 -- is disabled or if CodePeer_Mode.
428 if Tag_Checks_Suppressed
(Etype
(Result_Typ
))
429 or else not Tagged_Type_Expansion
430 or else CodePeer_Mode
434 -- Generate a class-wide membership test to ensure that the call's tag
435 -- argument denotes a type within the class. We must keep separate the
436 -- case in which the Result_Type of the constructor function is a tagged
437 -- type from the case in which it is an abstract interface because the
438 -- run-time subprogram required to check these cases differ (and have
439 -- one difference in their parameters profile).
441 -- Call CW_Membership if the Result_Type is a tagged type to look for
442 -- the tag in the table of ancestor tags.
444 elsif not Is_Interface
(Result_Typ
) then
446 Make_Implicit_If_Statement
(N
,
449 Make_Function_Call
(Loc
,
450 Name
=> New_Occurrence_Of
(RTE
(RE_CW_Membership
), Loc
),
451 Parameter_Associations
=> New_List
(
452 New_Copy_Tree
(Tag_Arg
),
454 Node
(First_Elmt
(Access_Disp_Table
(
455 Root_Type
(Result_Typ
)))), Loc
)))),
458 Make_Raise_Statement
(Loc
,
459 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
461 -- Call IW_Membership test if the Result_Type is an abstract interface
462 -- to look for the tag in the table of interface tags.
466 Make_Implicit_If_Statement
(N
,
469 Make_Function_Call
(Loc
,
470 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
471 Parameter_Associations
=> New_List
(
472 Make_Attribute_Reference
(Loc
,
473 Prefix
=> New_Copy_Tree
(Tag_Arg
),
474 Attribute_Name
=> Name_Address
),
477 Node
(First_Elmt
(Access_Disp_Table
(
478 Root_Type
(Result_Typ
)))), Loc
)))),
481 Make_Raise_Statement
(Loc
,
482 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
485 Analyze_And_Resolve
(N
, Etype
(Act_Constr
));
486 end Expand_Dispatching_Constructor_Call
;
488 ---------------------------
489 -- Expand_Exception_Call --
490 ---------------------------
492 -- If the function call is not within an exception handler, then the call
493 -- is replaced by a null string. Otherwise the appropriate routine in
494 -- Ada.Exceptions is called passing the choice parameter specification
495 -- from the enclosing handler. If the enclosing handler lacks a choice
496 -- parameter, then one is supplied.
498 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
) is
499 Loc
: constant Source_Ptr
:= Sloc
(N
);
504 -- Climb up parents to see if we are in exception handler
508 -- Case of not in exception handler, replace by null string
512 Make_String_Literal
(Loc
,
516 -- Case of in exception handler
518 elsif Nkind
(P
) = N_Exception_Handler
then
520 -- Handler cannot be used for a local raise, and furthermore, this
521 -- is a violation of the No_Exception_Propagation restriction.
523 Set_Local_Raise_Not_OK
(P
);
524 Check_Restriction
(No_Exception_Propagation
, N
);
526 -- If no choice parameter present, then put one there. Note that
527 -- we do not need to put it on the entity chain, since no one will
528 -- be referencing it by normal visibility methods.
530 if No
(Choice_Parameter
(P
)) then
531 E
:= Make_Temporary
(Loc
, 'E');
532 Set_Choice_Parameter
(P
, E
);
533 Mutate_Ekind
(E
, E_Variable
);
534 Set_Etype
(E
, RTE
(RE_Exception_Occurrence
));
535 Set_Scope
(E
, Current_Scope
);
539 Make_Function_Call
(Loc
,
540 Name
=> New_Occurrence_Of
(RTE
(Ent
), Loc
),
541 Parameter_Associations
=> New_List
(
542 New_Occurrence_Of
(Choice_Parameter
(P
), Loc
))));
552 Analyze_And_Resolve
(N
, Standard_String
);
553 end Expand_Exception_Call
;
555 ------------------------
556 -- Expand_Import_Call --
557 ------------------------
559 -- The function call must have a static string as its argument. We create
560 -- a dummy variable which uses this string as the external name in an
561 -- Import pragma. The result is then obtained as the address of this
562 -- dummy variable, converted to the appropriate target type.
564 procedure Expand_Import_Call
(N
: Node_Id
) is
565 Loc
: constant Source_Ptr
:= Sloc
(N
);
566 Ent
: constant Entity_Id
:= Entity
(Name
(N
));
567 Str
: constant Node_Id
:= First_Actual
(N
);
568 Dum
: constant Entity_Id
:= Make_Temporary
(Loc
, 'D');
571 Insert_Actions
(N
, New_List
(
572 Make_Object_Declaration
(Loc
,
573 Defining_Identifier
=> Dum
,
575 New_Occurrence_Of
(Standard_Character
, Loc
)),
578 Chars
=> Name_Import
,
579 Pragma_Argument_Associations
=> New_List
(
580 Make_Pragma_Argument_Association
(Loc
,
581 Expression
=> Make_Identifier
(Loc
, Name_Ada
)),
583 Make_Pragma_Argument_Association
(Loc
,
584 Expression
=> Make_Identifier
(Loc
, Chars
(Dum
))),
586 Make_Pragma_Argument_Association
(Loc
,
587 Chars
=> Name_Link_Name
,
588 Expression
=> Relocate_Node
(Str
))))));
591 Unchecked_Convert_To
(Etype
(Ent
),
592 Make_Attribute_Reference
(Loc
,
593 Prefix
=> Make_Identifier
(Loc
, Chars
(Dum
)),
594 Attribute_Name
=> Name_Address
)));
596 Analyze_And_Resolve
(N
, Etype
(Ent
));
597 end Expand_Import_Call
;
599 ---------------------------
600 -- Expand_Intrinsic_Call --
601 ---------------------------
603 procedure Expand_Intrinsic_Call
(N
: Node_Id
; E
: Entity_Id
) is
607 -- If an external name is specified for the intrinsic, it is handled
608 -- by the back-end: leave the call node unchanged for now.
610 if Present
(Interface_Name
(E
)) then
614 -- If the intrinsic subprogram is generic, gets its original name
616 if Present
(Parent
(E
))
617 and then Present
(Generic_Parent
(Parent
(E
)))
619 Nam
:= Chars
(Generic_Parent
(Parent
(E
)));
624 if Nam
= Name_Asm
then
627 elsif Nam
= Name_Divide
then
628 Expand_Decimal_Divide_Call
(N
);
630 elsif Nam
= Name_Exception_Information
then
631 Expand_Exception_Call
(N
, RE_Exception_Information
);
633 elsif Nam
= Name_Exception_Message
then
634 Expand_Exception_Call
(N
, RE_Exception_Message
);
636 elsif Nam
= Name_Exception_Name
then
637 Expand_Exception_Call
(N
, RE_Exception_Name_Simple
);
639 elsif Nam
= Name_Generic_Dispatching_Constructor
then
640 Expand_Dispatching_Constructor_Call
(N
);
642 elsif Nam
in Name_Import_Address
643 | Name_Import_Largest_Value
646 Expand_Import_Call
(N
);
648 elsif Nam
= Name_Rotate_Left
then
649 Expand_Shift
(N
, E
, N_Op_Rotate_Left
);
651 elsif Nam
= Name_Rotate_Right
then
652 Expand_Shift
(N
, E
, N_Op_Rotate_Right
);
654 elsif Nam
= Name_Shift_Left
then
655 Expand_Shift
(N
, E
, N_Op_Shift_Left
);
657 elsif Nam
= Name_Shift_Right
then
658 Expand_Shift
(N
, E
, N_Op_Shift_Right
);
660 elsif Nam
= Name_Shift_Right_Arithmetic
then
661 Expand_Shift
(N
, E
, N_Op_Shift_Right_Arithmetic
);
663 elsif Nam
= Name_Unchecked_Conversion
then
664 Expand_Unc_Conversion
(N
, E
);
666 elsif Nam
= Name_Unchecked_Deallocation
then
667 Expand_Unc_Deallocation
(N
);
669 elsif Nam
= Name_To_Address
then
670 Expand_To_Address
(N
);
672 elsif Nam
= Name_To_Integer
then
673 Expand_To_Integer
(N
);
675 elsif Nam
= Name_To_Pointer
then
676 Expand_To_Pointer
(N
);
678 elsif Nam
in Name_File
680 | Name_Source_Location
681 | Name_Enclosing_Entity
682 | Name_Compilation_ISO_Date
683 | Name_Compilation_Date
684 | Name_Compilation_Time
686 Expand_Source_Info
(N
, Nam
);
688 -- If we have a renaming, expand the call to the original operation,
689 -- which must itself be intrinsic, since renaming requires matching
690 -- conventions and this has already been checked.
692 elsif Present
(Alias
(E
)) then
693 Expand_Intrinsic_Call
(N
, Alias
(E
));
695 elsif Nkind
(N
) in N_Binary_Op
then
696 Expand_Binary_Operator_Call
(N
);
698 -- The only other case is where an external name was specified, since
699 -- this is the only way that an otherwise unrecognized name could
700 -- escape the checking in Sem_Prag. Nothing needs to be done in such
701 -- a case, since we pass such a call to the back end unchanged.
706 end Expand_Intrinsic_Call
;
712 -- This procedure is used to convert a call to a shift function to the
713 -- corresponding operator node. This conversion is not done by the usual
714 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
715 -- operator nodes, because shifts are not predefined operators.
717 -- As a result, whenever a shift is used in the source program, it will
718 -- remain as a call until converted by this routine to the operator node
719 -- form which the back end is expecting to see.
721 -- Note: it is possible for the expander to generate shift operator nodes
722 -- directly, which will be analyzed in the normal manner by calling Analyze
723 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
725 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
) is
726 Entyp
: constant Entity_Id
:= Etype
(E
);
727 Left
: constant Node_Id
:= First_Actual
(N
);
728 Loc
: constant Source_Ptr
:= Sloc
(N
);
729 Right
: constant Node_Id
:= Next_Actual
(Left
);
730 Ltyp
: constant Node_Id
:= Etype
(Left
);
731 Rtyp
: constant Node_Id
:= Etype
(Right
);
732 Typ
: constant Entity_Id
:= Etype
(N
);
736 Snode
:= New_Node
(K
, Loc
);
737 Set_Right_Opnd
(Snode
, Relocate_Node
(Right
));
738 Set_Chars
(Snode
, Chars
(E
));
739 Set_Etype
(Snode
, Base_Type
(Entyp
));
740 Set_Entity
(Snode
, E
);
742 if Compile_Time_Known_Value
(Type_High_Bound
(Rtyp
))
743 and then Expr_Value
(Type_High_Bound
(Rtyp
)) < Esize
(Ltyp
)
745 Set_Shift_Count_OK
(Snode
, True);
750 -- Note that we don't call Analyze and Resolve on this node, because
751 -- it already got analyzed and resolved when it was a function call.
753 Set_Left_Opnd
(Snode
, Relocate_Node
(Left
));
757 -- However, we do call the expander, so that the expansion for
758 -- rotates and shift_right_arithmetic happens if Modify_Tree_For_C
761 if Expander_Active
then
766 -- If the context type is not the type of the operator, it is an
767 -- inherited operator for a derived type. Wrap the node in a
768 -- conversion so that it is type-consistent for possible further
769 -- expansion (e.g. within a lock-free protected type).
771 Set_Left_Opnd
(Snode
,
772 Unchecked_Convert_To
(Base_Type
(Entyp
), Relocate_Node
(Left
)));
773 Rewrite
(N
, Unchecked_Convert_To
(Typ
, Snode
));
775 -- Analyze and resolve result formed by conversion to target type
777 Analyze_And_Resolve
(N
, Typ
);
781 ------------------------
782 -- Expand_Source_Info --
783 ------------------------
785 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
) is
786 Loc
: constant Source_Ptr
:= Sloc
(N
);
790 if Nam
= Name_Line
then
792 Make_Integer_Literal
(Loc
,
793 Intval
=> UI_From_Int
(Int
(Get_Logical_Line_Number
(Loc
)))));
794 Analyze_And_Resolve
(N
, Standard_Positive
);
800 Buf
: Bounded_String
;
802 Add_Source_Info
(Buf
, Loc
, Nam
);
803 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> +Buf
));
804 Analyze_And_Resolve
(N
, Standard_String
);
808 Set_Is_Static_Expression
(N
);
809 end Expand_Source_Info
;
811 ---------------------------
812 -- Expand_Unc_Conversion --
813 ---------------------------
815 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
) is
816 Func
: constant Entity_Id
:= Entity
(Name
(N
));
822 -- Rewrite as unchecked conversion node. Note that we must convert
823 -- the operand to the formal type of the input parameter of the
824 -- function, so that the resulting N_Unchecked_Type_Conversion
825 -- call indicates the correct types for Gigi.
827 -- Right now, we only do this if a scalar type is involved. It is
828 -- not clear if it is needed in other cases. If we do attempt to
829 -- do the conversion unconditionally, it crashes 3411-018. To be
830 -- investigated further ???
832 Conv
:= Relocate_Node
(First_Actual
(N
));
833 Ftyp
:= Etype
(First_Formal
(Func
));
835 if Is_Scalar_Type
(Ftyp
) then
836 Conv
:= Convert_To
(Ftyp
, Conv
);
837 Set_Parent
(Conv
, N
);
838 Analyze_And_Resolve
(Conv
);
841 -- The instantiation of Unchecked_Conversion creates a wrapper package,
842 -- and the target type is declared as a subtype of the actual. Recover
843 -- the actual, which is the subtype indic. in the subtype declaration
844 -- for the target type. This is semantically correct, and avoids
845 -- anomalies with access subtypes. For entities, leave type as is.
847 -- We do the analysis here, because we do not want the compiler
848 -- to try to optimize or otherwise reorganize the unchecked
853 if Is_Entity_Name
(Conv
) then
856 elsif Nkind
(Parent
(Ttyp
)) = N_Subtype_Declaration
then
857 Ttyp
:= Entity
(Subtype_Indication
(Parent
(Etype
(E
))));
859 elsif Is_Itype
(Ttyp
) then
861 Entity
(Subtype_Indication
(Associated_Node_For_Itype
(Ttyp
)));
866 Rewrite
(N
, Unchecked_Convert_To
(Ttyp
, Conv
));
867 Analyze_And_Resolve
(N
, Ttyp
);
868 end Expand_Unc_Conversion
;
870 -----------------------------
871 -- Expand_Unc_Deallocation --
872 -----------------------------
874 procedure Expand_Unc_Deallocation
(N
: Node_Id
) is
875 Arg
: constant Node_Id
:= First_Actual
(N
);
876 Loc
: constant Source_Ptr
:= Sloc
(N
);
877 Typ
: constant Entity_Id
:= Etype
(Arg
);
878 Desig_Typ
: constant Entity_Id
:=
879 Available_View
(Designated_Type
(Typ
));
880 Needs_Fin
: constant Boolean := Needs_Finalization
(Desig_Typ
);
881 Root_Typ
: constant Entity_Id
:= Underlying_Type
(Root_Type
(Typ
));
882 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Root_Typ
);
883 Stmts
: constant List_Id
:= New_List
;
885 Arg_Known_Non_Null
: constant Boolean := Known_Non_Null
(N
);
886 -- This captures whether we know the argument to be non-null so that
887 -- we can avoid the test. The reason that we need to capture this is
888 -- that we analyze some generated statements before properly attaching
889 -- them to the tree, and that can disturb current value settings.
891 Exceptions_OK
: constant Boolean :=
892 not Restriction_Active
(No_Exception_Propagation
);
894 Abrt_Blk
: Node_Id
:= Empty
;
895 Abrt_Blk_Id
: Entity_Id
;
900 Fin_Data
: Finalization_Exception_Data
;
907 -- Nothing to do if we know the argument is null
909 if Known_Null
(N
) then
913 -- Processing for pointer to controlled types. Generate:
915 -- Abrt : constant Boolean := ...;
916 -- Ex : Exception_Occurrence;
917 -- Raised : Boolean := False;
923 -- [Deep_]Finalize (Obj_Ref);
927 -- if not Raised then
929 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
932 -- Abort_Undefer_Direct;
935 -- Depending on whether exception propagation is enabled and/or aborts
936 -- are allowed, the generated code may lack block statements.
940 -- Ada 2005 (AI-251): In case of abstract interface type we displace
941 -- the pointer to reference the base of the object to deallocate its
942 -- memory, unless we're targetting a VM, in which case no special
943 -- processing is required.
945 if Is_Interface
(Directly_Designated_Type
(Typ
))
946 and then Tagged_Type_Expansion
949 Make_Explicit_Dereference
(Loc
,
951 Unchecked_Convert_To
(Typ
,
952 Make_Function_Call
(Loc
,
954 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
955 Parameter_Associations
=> New_List
(
956 Unchecked_Convert_To
(RTE
(RE_Address
),
957 Duplicate_Subexpr_No_Checks
(Arg
))))));
961 Make_Explicit_Dereference
(Loc
,
962 Prefix
=> Duplicate_Subexpr_No_Checks
(Arg
));
965 -- If the designated type is tagged, the finalization call must
966 -- dispatch because the designated type may not be the actual type
967 -- of the object. If the type is synchronized, the deallocation
968 -- applies to the corresponding record type.
970 if Is_Tagged_Type
(Desig_Typ
) then
971 if Is_Concurrent_Type
(Desig_Typ
) then
974 (Class_Wide_Type
(Corresponding_Record_Type
(Desig_Typ
)),
977 elsif not Is_Class_Wide_Type
(Desig_Typ
) then
979 Unchecked_Convert_To
(Class_Wide_Type
(Desig_Typ
), Obj_Ref
);
982 -- Otherwise the designated type is untagged. Set the type of the
983 -- dereference explicitly to force a conversion when needed given
984 -- that [Deep_]Finalize may be inherited from a parent type.
987 Set_Etype
(Obj_Ref
, Desig_Typ
);
991 -- [Deep_]Finalize (Obj_Ref);
993 Fin_Call
:= Make_Final_Call
(Obj_Ref
=> Obj_Ref
, Typ
=> Desig_Typ
);
996 -- Abrt : constant Boolean := ...;
997 -- Ex : Exception_Occurrence;
998 -- Raised : Boolean := False;
1005 -- if not Raised then
1007 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
1010 if Exceptions_OK
then
1011 Build_Object_Declarations
(Fin_Data
, Stmts
, Loc
);
1014 Make_Block_Statement
(Loc
,
1015 Handled_Statement_Sequence
=>
1016 Make_Handled_Sequence_Of_Statements
(Loc
,
1017 Statements
=> New_List
(Fin_Call
),
1018 Exception_Handlers
=> New_List
(
1019 Build_Exception_Handler
(Fin_Data
))));
1021 -- Otherwise exception propagation is not allowed
1024 Fin_Blk
:= Fin_Call
;
1027 -- The finalization action must be protected by an abort defer and
1028 -- undefer pair when aborts are allowed. Generate:
1034 -- Abort_Undefer_Direct;
1037 if Abort_Allowed
then
1038 AUD
:= RTE
(RE_Abort_Undefer_Direct
);
1041 Make_Handled_Sequence_Of_Statements
(Loc
,
1042 Statements
=> New_List
(
1043 Build_Runtime_Call
(Loc
, RE_Abort_Defer
),
1045 At_End_Proc
=> New_Occurrence_Of
(AUD
, Loc
));
1048 Make_Block_Statement
(Loc
,
1049 Handled_Statement_Sequence
=> Abrt_HSS
);
1051 Add_Block_Identifier
(Abrt_Blk
, Abrt_Blk_Id
);
1052 Expand_At_End_Handler
(Abrt_HSS
, Abrt_Blk_Id
);
1054 -- Present the Abort_Undefer_Direct function to the backend so
1055 -- that it can inline the call to the function.
1057 Add_Inlined_Body
(AUD
, N
);
1059 -- Otherwise aborts are not allowed
1062 Abrt_Blk
:= Fin_Blk
;
1065 Append_To
(Stmts
, Abrt_Blk
);
1068 -- For a task type, call Free_Task before freeing the ATCB. We used to
1069 -- detect the case of Abort followed by a Free here, because the Free
1070 -- wouldn't actually free if it happens before the aborted task actually
1071 -- terminates. The warning was removed, because Free now works properly
1072 -- (the task will be freed once it terminates).
1074 if Is_Task_Type
(Desig_Typ
) then
1076 Cleanup_Task
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1078 -- For composite types that contain tasks, recurse over the structure
1079 -- to build the selectors for the task subcomponents.
1081 elsif Has_Task
(Desig_Typ
) then
1082 if Is_Array_Type
(Desig_Typ
) then
1083 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_Typ
));
1085 elsif Is_Record_Type
(Desig_Typ
) then
1086 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_Typ
));
1090 -- Same for simple protected types. Eventually call Finalize_Protection
1091 -- before freeing the PO for each protected component.
1093 if Is_Simple_Protected_Type
(Desig_Typ
) then
1095 Cleanup_Protected_Object
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1097 elsif Has_Simple_Protected_Object
(Desig_Typ
) then
1098 if Is_Array_Type
(Desig_Typ
) then
1099 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_Typ
));
1101 elsif Is_Record_Type
(Desig_Typ
) then
1102 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_Typ
));
1106 -- Normal processing for non-controlled types. The argument to free is
1107 -- a renaming rather than a constant to ensure that the original context
1108 -- is always set to null after the deallocation takes place.
1110 Free_Arg
:= Duplicate_Subexpr_No_Checks
(Arg
, Renaming_Req
=> True);
1111 Free_Nod
:= Make_Free_Statement
(Loc
, Empty
);
1112 Append_To
(Stmts
, Free_Nod
);
1113 Set_Storage_Pool
(Free_Nod
, Pool
);
1115 -- Attach to tree before analysis of generated subtypes below
1117 Set_Parent
(Stmts
, Parent
(N
));
1119 -- Deal with storage pool
1121 if Present
(Pool
) then
1123 -- Freeing the secondary stack is meaningless
1125 if Is_RTE
(Pool
, RE_SS_Pool
) then
1128 -- If the pool object is of a simple storage pool type, then attempt
1129 -- to locate the type's Deallocate procedure, if any, and set the
1130 -- free operation's procedure to call. If the type doesn't have a
1131 -- Deallocate (which is allowed), then the actual will simply be set
1135 (Get_Rep_Pragma
(Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
1138 Pool_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
1139 Dealloc
: Entity_Id
;
1142 Dealloc
:= Get_Name_Entity_Id
(Name_Deallocate
);
1143 while Present
(Dealloc
) loop
1144 if Scope
(Dealloc
) = Scope
(Pool_Typ
)
1145 and then Present
(First_Formal
(Dealloc
))
1146 and then Etype
(First_Formal
(Dealloc
)) = Pool_Typ
1148 Set_Procedure_To_Call
(Free_Nod
, Dealloc
);
1151 Dealloc
:= Homonym
(Dealloc
);
1156 -- Case of a class-wide pool type: make a dispatching call to
1157 -- Deallocate through the class-wide Deallocate_Any.
1159 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
1160 Set_Procedure_To_Call
(Free_Nod
, RTE
(RE_Deallocate_Any
));
1162 -- Case of a specific pool type: make a statically bound call
1165 Set_Procedure_To_Call
1166 (Free_Nod
, Find_Storage_Op
(Etype
(Pool
), Name_Deallocate
));
1170 if Present
(Procedure_To_Call
(Free_Nod
)) then
1172 -- For all cases of a Deallocate call, the back-end needs to be able
1173 -- to compute the size of the object being freed. This may require
1174 -- some adjustments for objects of dynamic size.
1176 -- If the type is class wide, we generate an implicit type with the
1177 -- right dynamic size, so that the deallocate call gets the right
1178 -- size parameter computed by GIGI. Same for an access to
1179 -- unconstrained packed array.
1181 if Is_Class_Wide_Type
(Desig_Typ
)
1183 (Is_Packed_Array
(Desig_Typ
)
1184 and then not Is_Constrained
(Desig_Typ
))
1187 Deref
: constant Node_Id
:=
1188 Make_Explicit_Dereference
(Loc
,
1189 Duplicate_Subexpr_No_Checks
(Arg
));
1194 -- Perform minor decoration as it is needed by the side effect
1195 -- removal mechanism.
1197 Set_Etype
(Deref
, Desig_Typ
);
1198 Set_Parent
(Deref
, Free_Nod
);
1199 D_Subtyp
:= Make_Subtype_From_Expr
(Deref
, Desig_Typ
);
1201 if Nkind
(D_Subtyp
) in N_Has_Entity
then
1202 D_Type
:= Entity
(D_Subtyp
);
1205 D_Type
:= Make_Temporary
(Loc
, 'A');
1206 Insert_Action
(Deref
,
1207 Make_Subtype_Declaration
(Loc
,
1208 Defining_Identifier
=> D_Type
,
1209 Subtype_Indication
=> D_Subtyp
));
1212 -- Force freezing at the point of the dereference. For the
1213 -- class wide case, this avoids having the subtype frozen
1214 -- before the equivalent type.
1216 Freeze_Itype
(D_Type
, Deref
);
1218 Set_Actual_Designated_Subtype
(Free_Nod
, D_Type
);
1223 -- Ada 2005 (AI-251): In case of abstract interface type we must
1224 -- displace the pointer to reference the base of the object to
1225 -- deallocate its memory, unless we're targetting a VM, in which case
1226 -- no special processing is required.
1229 -- free (Base_Address (Obj_Ptr))
1231 if Is_Interface
(Directly_Designated_Type
(Typ
))
1232 and then Tagged_Type_Expansion
1234 Set_Expression
(Free_Nod
,
1235 Unchecked_Convert_To
(Typ
,
1236 Make_Function_Call
(Loc
,
1238 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
1239 Parameter_Associations
=> New_List
(
1240 Unchecked_Convert_To
(RTE
(RE_Address
), Free_Arg
)))));
1246 Set_Expression
(Free_Nod
, Free_Arg
);
1249 -- Only remaining step is to set result to null, or generate a raise of
1250 -- Constraint_Error if the target object is "not null".
1252 if Can_Never_Be_Null
(Etype
(Arg
)) then
1254 Make_Raise_Constraint_Error
(Loc
,
1255 Reason
=> CE_Access_Check_Failed
));
1259 Lhs
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Arg
);
1261 Set_Assignment_OK
(Lhs
);
1263 Make_Assignment_Statement
(Loc
,
1265 Expression
=> Make_Null
(Loc
)));
1269 -- Generate a test of whether any earlier finalization raised an
1270 -- exception, and in that case raise Program_Error with the previous
1271 -- exception occurrence.
1274 -- if Raised and then not Abrt then
1275 -- raise Program_Error; -- for restricted RTS
1277 -- Raise_From_Controlled_Operation (E); -- all other cases
1280 if Needs_Fin
and then Exceptions_OK
then
1281 Append_To
(Stmts
, Build_Raise_Statement
(Fin_Data
));
1284 -- If we know the argument is non-null, then make a block statement
1285 -- that contains the required statements, no need for a test.
1287 if Arg_Known_Non_Null
then
1289 Make_Block_Statement
(Loc
,
1290 Handled_Statement_Sequence
=>
1291 Make_Handled_Sequence_Of_Statements
(Loc
,
1292 Statements
=> Stmts
));
1294 -- If the argument may be null, wrap the statements inside an IF that
1295 -- does an explicit test to exclude the null case.
1299 Make_Implicit_If_Statement
(N
,
1302 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
1303 Right_Opnd
=> Make_Null
(Loc
)),
1304 Then_Statements
=> Stmts
);
1309 Rewrite
(N
, Gen_Code
);
1311 end Expand_Unc_Deallocation
;
1313 -----------------------
1314 -- Expand_To_Address --
1315 -----------------------
1317 procedure Expand_To_Address
(N
: Node_Id
) is
1318 Loc
: constant Source_Ptr
:= Sloc
(N
);
1319 Arg
: constant Node_Id
:= First_Actual
(N
);
1323 if Is_Modular_Integer_Type
(Etype
(Arg
)) then
1324 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1329 Remove_Side_Effects
(Arg
);
1331 Obj
:= Make_Explicit_Dereference
(Loc
, Relocate_Node
(Arg
));
1334 Make_If_Expression
(Loc
,
1335 Expressions
=> New_List
(
1337 Left_Opnd
=> New_Copy_Tree
(Arg
),
1338 Right_Opnd
=> Make_Null
(Loc
)),
1339 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
1340 Make_Attribute_Reference
(Loc
,
1342 Attribute_Name
=> Name_Address
))));
1344 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1345 end Expand_To_Address
;
1347 -----------------------
1348 -- Expand_To_Integer --
1349 -----------------------
1351 procedure Expand_To_Integer
(N
: Node_Id
) is
1352 Arg
: constant Node_Id
:= First_Actual
(N
);
1355 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1357 end Expand_To_Integer
;
1359 -----------------------
1360 -- Expand_To_Pointer --
1361 -----------------------
1363 procedure Expand_To_Pointer
(N
: Node_Id
) is
1364 Arg
: constant Node_Id
:= First_Actual
(N
);
1367 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1369 end Expand_To_Pointer
;