1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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 ---------------------
113 -- Add_Source_Info --
114 ---------------------
116 procedure Add_Source_Info
117 (Buf
: in out Bounded_String
;
124 Append
(Buf
, Nat
(Get_Logical_Line_Number
(Loc
)));
127 Append
(Buf
, Reference_Name
(Get_Source_File_Index
(Loc
)));
129 when Name_Source_Location
=>
130 Build_Location_String
(Buf
, Loc
);
132 when Name_Enclosing_Entity
=>
134 -- Skip enclosing blocks to reach enclosing unit
137 Ent
: Entity_Id
:= Current_Scope
;
139 while Present
(Ent
) loop
140 exit when Ekind
(Ent
) not in E_Block | E_Loop
;
144 -- Ent now points to the relevant defining entity
146 Append_Entity_Name
(Buf
, Ent
);
149 when Name_Compilation_ISO_Date
=>
150 Append
(Buf
, Opt
.Compilation_Time
(1 .. 10));
152 when Name_Compilation_Date
=>
154 subtype S13
is String (1 .. 3);
155 Months
: constant array (1 .. 12) of S13
:=
156 ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
157 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
159 M1
: constant Character := Opt
.Compilation_Time
(6);
160 M2
: constant Character := Opt
.Compilation_Time
(7);
162 MM
: constant Natural range 1 .. 12 :=
163 (Character'Pos (M1
) - Character'Pos ('0')) * 10 +
164 (Character'Pos (M2
) - Character'Pos ('0'));
167 -- Reformat ISO date into MMM DD YYYY (__DATE__) format
169 Append
(Buf
, Months
(MM
));
171 Append
(Buf
, Opt
.Compilation_Time
(9 .. 10));
173 Append
(Buf
, Opt
.Compilation_Time
(1 .. 4));
176 when Name_Compilation_Time
=>
177 Append
(Buf
, Opt
.Compilation_Time
(12 .. 19));
184 ---------------------------------
185 -- Expand_Binary_Operator_Call --
186 ---------------------------------
188 procedure Expand_Binary_Operator_Call
(N
: Node_Id
) is
189 T1
: constant Entity_Id
:= Underlying_Type
(Etype
(Left_Opnd
(N
)));
190 T2
: constant Entity_Id
:= Underlying_Type
(Etype
(Right_Opnd
(N
)));
191 TR
: constant Entity_Id
:= Etype
(N
);
195 Siz
: constant Uint
:= UI_Max
(RM_Size
(T1
), RM_Size
(T2
));
196 -- Maximum of operand sizes
199 -- Nothing to do if the operands have the same modular type
201 if Base_Type
(T1
) = Base_Type
(T2
)
202 and then Is_Modular_Integer_Type
(T1
)
207 -- Use the appropriate type for the size
210 T3
:= RTE
(RE_Unsigned_32
);
213 T3
:= RTE
(RE_Unsigned_64
);
215 else pragma Assert
(Siz
<= 128);
216 T3
:= RTE
(RE_Unsigned_128
);
219 -- Copy operator node, and reset type and entity fields, for
220 -- subsequent reanalysis.
226 when N_Op_And
=> Set_Entity
(Res
, Standard_Op_And
);
227 when N_Op_Or
=> Set_Entity
(Res
, Standard_Op_Or
);
228 when N_Op_Xor
=> Set_Entity
(Res
, Standard_Op_Xor
);
229 when others => raise Program_Error
;
232 -- Convert operands to large enough intermediate type
235 Unchecked_Convert_To
(T3
, Relocate_Node
(Left_Opnd
(N
))));
237 Unchecked_Convert_To
(T3
, Relocate_Node
(Right_Opnd
(N
))));
239 -- Analyze and resolve result formed by conversion to target type
241 Rewrite
(N
, Unchecked_Convert_To
(TR
, Res
));
242 Analyze_And_Resolve
(N
, TR
);
243 end Expand_Binary_Operator_Call
;
245 -----------------------------------------
246 -- Expand_Dispatching_Constructor_Call --
247 -----------------------------------------
249 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
252 -- GDC_Instance (The_Tag, Parameters'Access)
254 -- to a class-wide conversion of a dispatching call to the actual
255 -- associated with the formal subprogram Construct, designating The_Tag
256 -- as the controlling tag of the call:
258 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
260 -- which will eventually be expanded to the following:
262 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
264 -- A class-wide membership test is also generated, preceding the call, to
265 -- ensure that the controlling tag denotes a type in T'Class.
267 procedure Expand_Dispatching_Constructor_Call
(N
: Node_Id
) is
268 Loc
: constant Source_Ptr
:= Sloc
(N
);
269 Tag_Arg
: constant Node_Id
:= First_Actual
(N
);
270 Param_Arg
: constant Node_Id
:= Next_Actual
(Tag_Arg
);
271 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Entity
(Name
(N
))));
272 Inst_Pkg
: constant Node_Id
:= Parent
(Subp_Decl
);
273 Act_Rename
: Node_Id
;
274 Act_Constr
: Entity_Id
;
275 Iface_Tag
: Node_Id
:= Empty
;
276 Cnstr_Call
: Node_Id
;
277 Result_Typ
: Entity_Id
;
280 pragma Assert
(Is_Class_Wide_Type
(Etype
(Entity
(Name
(N
)))));
282 -- Remove side effects from tag argument early, before rewriting
283 -- the dispatching constructor call, as Remove_Side_Effects relies
284 -- on Tag_Arg's Parent link properly attached to the tree (once the
285 -- call is rewritten, the Parent is inconsistent as it points to the
286 -- rewritten node, which is not the syntactic parent of the Tag_Arg
289 Remove_Side_Effects
(Tag_Arg
);
291 -- Check that we have a proper tag
294 Make_Implicit_If_Statement
(N
,
295 Condition
=> Make_Op_Eq
(Loc
,
296 Left_Opnd
=> New_Copy_Tree
(Tag_Arg
),
297 Right_Opnd
=> New_Occurrence_Of
(RTE
(RE_No_Tag
), Loc
)),
299 Then_Statements
=> New_List
(
300 Make_Raise_Statement
(Loc
,
301 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
303 -- Check that it is not the tag of an abstract type
306 Make_Implicit_If_Statement
(N
,
307 Condition
=> Make_Function_Call
(Loc
,
309 New_Occurrence_Of
(RTE
(RE_Is_Abstract
), Loc
),
310 Parameter_Associations
=> New_List
(New_Copy_Tree
(Tag_Arg
))),
312 Then_Statements
=> New_List
(
313 Make_Raise_Statement
(Loc
,
314 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
316 -- The subprogram is the third actual in the instantiation, and is
317 -- retrieved from the corresponding renaming declaration. However,
318 -- freeze nodes may appear before, so we retrieve the declaration
319 -- with an explicit loop.
321 Act_Rename
:= First
(Visible_Declarations
(Inst_Pkg
));
322 while Nkind
(Act_Rename
) /= N_Subprogram_Renaming_Declaration
loop
326 Act_Constr
:= Entity
(Name
(Act_Rename
));
327 Result_Typ
:= Class_Wide_Type
(Etype
(Act_Constr
));
329 -- Check that the accessibility level of the tag is no deeper than that
330 -- of the constructor function (unless CodePeer_Mode).
332 if not CodePeer_Mode
then
334 Make_Implicit_If_Statement
(N
,
338 Build_Get_Access_Level
(Loc
, New_Copy_Tree
(Tag_Arg
)),
341 (Loc
, Scope_Depth_Default_0
(Act_Constr
))),
343 Then_Statements
=> New_List
(
344 Make_Raise_Statement
(Loc
,
345 New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
348 if Is_Interface
(Etype
(Act_Constr
)) then
350 -- If the result type is not known to be a parent of Tag_Arg then we
351 -- need to locate the tag of the secondary dispatch table.
353 if not Is_Ancestor
(Etype
(Result_Typ
), Etype
(Tag_Arg
),
354 Use_Full_View
=> True)
355 and then Tagged_Type_Expansion
357 -- Obtain the reference to the Ada.Tags service before generating
358 -- the Object_Declaration node to ensure that if this service is
359 -- not available in the runtime then we generate a clear error.
362 Fname
: constant Node_Id
:=
363 New_Occurrence_Of
(RTE
(RE_Secondary_Tag
), Loc
);
366 pragma Assert
(not Is_Interface
(Etype
(Tag_Arg
)));
368 -- The tag is the first entry in the dispatch table of the
369 -- return type of the constructor.
372 Make_Object_Declaration
(Loc
,
373 Defining_Identifier
=> Make_Temporary
(Loc
, 'V'),
375 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
377 Make_Function_Call
(Loc
,
379 Parameter_Associations
=> New_List
(
380 Relocate_Node
(Tag_Arg
),
383 (Access_Disp_Table
(Etype
(Act_Constr
)))),
385 Insert_Action
(N
, Iface_Tag
);
390 -- Create the call to the actual Constructor function
393 Make_Function_Call
(Loc
,
394 Name
=> New_Occurrence_Of
(Act_Constr
, Loc
),
395 Parameter_Associations
=> New_List
(Relocate_Node
(Param_Arg
)));
397 -- Establish its controlling tag from the tag passed to the instance
398 -- The tag may be given by a function call, in which case a temporary
399 -- should be generated now, to prevent out-of-order insertions during
400 -- the expansion of that call when stack-checking is enabled.
402 if Present
(Iface_Tag
) then
403 Set_Controlling_Argument
(Cnstr_Call
,
404 New_Occurrence_Of
(Defining_Identifier
(Iface_Tag
), Loc
));
406 Set_Controlling_Argument
(Cnstr_Call
,
407 Relocate_Node
(Tag_Arg
));
410 -- Rewrite and analyze the call to the instance as a class-wide
411 -- conversion of the call to the actual constructor. When the result
412 -- type is a class-wide interface type this conversion is required to
413 -- force the displacement of the pointer to the object to reference the
414 -- corresponding dispatch table.
416 Rewrite
(N
, Convert_To
(Result_Typ
, Cnstr_Call
));
418 -- Do not generate a run-time check on the built object if tag
419 -- checks are suppressed for the result type or tagged type expansion
420 -- is disabled or if CodePeer_Mode.
422 if Tag_Checks_Suppressed
(Etype
(Result_Typ
))
423 or else not Tagged_Type_Expansion
424 or else CodePeer_Mode
428 -- Generate a class-wide membership test to ensure that the call's tag
429 -- argument denotes a type within the class. We must keep separate the
430 -- case in which the Result_Type of the constructor function is a tagged
431 -- type from the case in which it is an abstract interface because the
432 -- run-time subprogram required to check these cases differ (and have
433 -- one difference in their parameters profile).
435 -- Call CW_Membership if the Result_Type is a tagged type to look for
436 -- the tag in the table of ancestor tags.
438 elsif not Is_Interface
(Result_Typ
) then
440 Make_Implicit_If_Statement
(N
,
443 Make_Function_Call
(Loc
,
444 Name
=> New_Occurrence_Of
(RTE
(RE_CW_Membership
), Loc
),
445 Parameter_Associations
=> New_List
(
446 New_Copy_Tree
(Tag_Arg
),
448 Node
(First_Elmt
(Access_Disp_Table
(
449 Root_Type
(Result_Typ
)))), Loc
)))),
452 Make_Raise_Statement
(Loc
,
453 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
455 -- Call IW_Membership test if the Result_Type is an abstract interface
456 -- to look for the tag in the table of interface tags.
460 Make_Implicit_If_Statement
(N
,
463 Make_Function_Call
(Loc
,
464 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
465 Parameter_Associations
=> New_List
(
466 Make_Attribute_Reference
(Loc
,
467 Prefix
=> New_Copy_Tree
(Tag_Arg
),
468 Attribute_Name
=> Name_Address
),
471 Node
(First_Elmt
(Access_Disp_Table
(
472 Root_Type
(Result_Typ
)))), Loc
)))),
475 Make_Raise_Statement
(Loc
,
476 Name
=> New_Occurrence_Of
(RTE
(RE_Tag_Error
), Loc
)))));
479 Analyze_And_Resolve
(N
, Etype
(Act_Constr
));
480 end Expand_Dispatching_Constructor_Call
;
482 ---------------------------
483 -- Expand_Exception_Call --
484 ---------------------------
486 -- If the function call is not within an exception handler, then the call
487 -- is replaced by a null string. Otherwise the appropriate routine in
488 -- Ada.Exceptions is called passing the choice parameter specification
489 -- from the enclosing handler. If the enclosing handler lacks a choice
490 -- parameter, then one is supplied.
492 procedure Expand_Exception_Call
(N
: Node_Id
; Ent
: RE_Id
) is
493 Loc
: constant Source_Ptr
:= Sloc
(N
);
498 -- Climb up parents to see if we are in exception handler
502 -- Case of not in exception handler, replace by null string
506 Make_String_Literal
(Loc
,
510 -- Case of in exception handler
512 elsif Nkind
(P
) = N_Exception_Handler
then
514 -- Handler cannot be used for a local raise, and furthermore, this
515 -- is a violation of the No_Exception_Propagation restriction.
517 Set_Local_Raise_Not_OK
(P
);
518 Check_Restriction
(No_Exception_Propagation
, N
);
520 -- If no choice parameter present, then put one there. Note that
521 -- we do not need to put it on the entity chain, since no one will
522 -- be referencing it by normal visibility methods.
524 if No
(Choice_Parameter
(P
)) then
525 E
:= Make_Temporary
(Loc
, 'E');
526 Set_Choice_Parameter
(P
, E
);
527 Mutate_Ekind
(E
, E_Variable
);
528 Set_Etype
(E
, RTE
(RE_Exception_Occurrence
));
529 Set_Scope
(E
, Current_Scope
);
533 Make_Function_Call
(Loc
,
534 Name
=> New_Occurrence_Of
(RTE
(Ent
), Loc
),
535 Parameter_Associations
=> New_List
(
536 New_Occurrence_Of
(Choice_Parameter
(P
), Loc
))));
546 Analyze_And_Resolve
(N
, Standard_String
);
547 end Expand_Exception_Call
;
549 ------------------------
550 -- Expand_Import_Call --
551 ------------------------
553 -- The function call must have a static string as its argument. We create
554 -- a dummy variable which uses this string as the external name in an
555 -- Import pragma. The result is then obtained as the address of this
556 -- dummy variable, converted to the appropriate target type.
558 procedure Expand_Import_Call
(N
: Node_Id
) is
559 Loc
: constant Source_Ptr
:= Sloc
(N
);
560 Ent
: constant Entity_Id
:= Entity
(Name
(N
));
561 Str
: constant Node_Id
:= First_Actual
(N
);
562 Dum
: constant Entity_Id
:= Make_Temporary
(Loc
, 'D');
565 Insert_Actions
(N
, New_List
(
566 Make_Object_Declaration
(Loc
,
567 Defining_Identifier
=> Dum
,
569 New_Occurrence_Of
(Standard_Character
, Loc
)),
572 Chars
=> Name_Import
,
573 Pragma_Argument_Associations
=> New_List
(
574 Make_Pragma_Argument_Association
(Loc
,
575 Expression
=> Make_Identifier
(Loc
, Name_Ada
)),
577 Make_Pragma_Argument_Association
(Loc
,
578 Expression
=> Make_Identifier
(Loc
, Chars
(Dum
))),
580 Make_Pragma_Argument_Association
(Loc
,
581 Chars
=> Name_Link_Name
,
582 Expression
=> Relocate_Node
(Str
))))));
585 Unchecked_Convert_To
(Etype
(Ent
),
586 Make_Attribute_Reference
(Loc
,
587 Prefix
=> Make_Identifier
(Loc
, Chars
(Dum
)),
588 Attribute_Name
=> Name_Address
)));
590 Analyze_And_Resolve
(N
, Etype
(Ent
));
591 end Expand_Import_Call
;
593 ---------------------------
594 -- Expand_Intrinsic_Call --
595 ---------------------------
597 procedure Expand_Intrinsic_Call
(N
: Node_Id
; E
: Entity_Id
) is
601 -- If an external name is specified for the intrinsic, it is handled
602 -- by the back-end: leave the call node unchanged for now.
604 if Present
(Interface_Name
(E
)) then
608 -- If the intrinsic subprogram is generic, gets its original name
610 if Present
(Parent
(E
))
611 and then Present
(Generic_Parent
(Parent
(E
)))
613 Nam
:= Chars
(Generic_Parent
(Parent
(E
)));
618 if Nam
= Name_Asm
then
621 elsif Nam
= Name_Divide
then
622 Expand_Decimal_Divide_Call
(N
);
624 elsif Nam
= Name_Exception_Information
then
625 Expand_Exception_Call
(N
, RE_Exception_Information
);
627 elsif Nam
= Name_Exception_Message
then
628 Expand_Exception_Call
(N
, RE_Exception_Message
);
630 elsif Nam
= Name_Exception_Name
then
631 Expand_Exception_Call
(N
, RE_Exception_Name_Simple
);
633 elsif Nam
= Name_Generic_Dispatching_Constructor
then
634 Expand_Dispatching_Constructor_Call
(N
);
636 elsif Nam
in Name_Import_Address
637 | Name_Import_Largest_Value
640 Expand_Import_Call
(N
);
642 elsif Nam
= Name_Rotate_Left
then
643 Expand_Shift
(N
, E
, N_Op_Rotate_Left
);
645 elsif Nam
= Name_Rotate_Right
then
646 Expand_Shift
(N
, E
, N_Op_Rotate_Right
);
648 elsif Nam
= Name_Shift_Left
then
649 Expand_Shift
(N
, E
, N_Op_Shift_Left
);
651 elsif Nam
= Name_Shift_Right
then
652 Expand_Shift
(N
, E
, N_Op_Shift_Right
);
654 elsif Nam
= Name_Shift_Right_Arithmetic
then
655 Expand_Shift
(N
, E
, N_Op_Shift_Right_Arithmetic
);
657 elsif Nam
= Name_Unchecked_Conversion
then
658 Expand_Unc_Conversion
(N
, E
);
660 elsif Nam
= Name_Unchecked_Deallocation
then
661 Expand_Unc_Deallocation
(N
);
663 elsif Nam
= Name_To_Address
then
664 Expand_To_Address
(N
);
666 elsif Nam
= Name_To_Integer
then
667 Expand_To_Integer
(N
);
669 elsif Nam
= Name_To_Pointer
then
670 Expand_To_Pointer
(N
);
672 elsif Nam
in Name_File
674 | Name_Source_Location
675 | Name_Enclosing_Entity
676 | Name_Compilation_ISO_Date
677 | Name_Compilation_Date
678 | Name_Compilation_Time
680 Expand_Source_Info
(N
, Nam
);
682 -- If we have a renaming, expand the call to the original operation,
683 -- which must itself be intrinsic, since renaming requires matching
684 -- conventions and this has already been checked.
686 elsif Present
(Alias
(E
)) then
687 Expand_Intrinsic_Call
(N
, Alias
(E
));
689 elsif Nkind
(N
) in N_Binary_Op
then
690 Expand_Binary_Operator_Call
(N
);
692 -- The only other case is where an external name was specified, since
693 -- this is the only way that an otherwise unrecognized name could
694 -- escape the checking in Sem_Prag. Nothing needs to be done in such
695 -- a case, since we pass such a call to the back end unchanged.
700 end Expand_Intrinsic_Call
;
706 -- This procedure is used to convert a call to a shift function to the
707 -- corresponding operator node. This conversion is not done by the usual
708 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
709 -- operator nodes, because shifts are not predefined operators.
711 -- As a result, whenever a shift is used in the source program, it will
712 -- remain as a call until converted by this routine to the operator node
713 -- form which the back end is expecting to see.
715 -- Note: it is possible for the expander to generate shift operator nodes
716 -- directly, which will be analyzed in the normal manner by calling Analyze
717 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
719 procedure Expand_Shift
(N
: Node_Id
; E
: Entity_Id
; K
: Node_Kind
) is
720 Entyp
: constant Entity_Id
:= Etype
(E
);
721 Left
: constant Node_Id
:= First_Actual
(N
);
722 Loc
: constant Source_Ptr
:= Sloc
(N
);
723 Right
: constant Node_Id
:= Next_Actual
(Left
);
724 Ltyp
: constant Node_Id
:= Etype
(Left
);
725 Rtyp
: constant Node_Id
:= Etype
(Right
);
726 Typ
: constant Entity_Id
:= Etype
(N
);
730 Snode
:= New_Node
(K
, Loc
);
731 Set_Right_Opnd
(Snode
, Relocate_Node
(Right
));
732 Set_Chars
(Snode
, Chars
(E
));
733 Set_Etype
(Snode
, Base_Type
(Entyp
));
734 Set_Entity
(Snode
, E
);
736 if Compile_Time_Known_Value
(Type_High_Bound
(Rtyp
))
737 and then Expr_Value
(Type_High_Bound
(Rtyp
)) < Esize
(Ltyp
)
739 Set_Shift_Count_OK
(Snode
, True);
744 -- Note that we don't call Analyze and Resolve on this node, because
745 -- it already got analyzed and resolved when it was a function call.
747 Set_Left_Opnd
(Snode
, Relocate_Node
(Left
));
751 if Expander_Active
then
755 -- If the context type is not the type of the operator, it is an
756 -- inherited operator for a derived type. Wrap the node in a
757 -- conversion so that it is type-consistent for possible further
758 -- expansion (e.g. within a lock-free protected type).
760 Set_Left_Opnd
(Snode
,
761 Unchecked_Convert_To
(Base_Type
(Entyp
), Relocate_Node
(Left
)));
762 Rewrite
(N
, Unchecked_Convert_To
(Typ
, Snode
));
764 -- Analyze and resolve result formed by conversion to target type
766 Analyze_And_Resolve
(N
, Typ
);
770 ------------------------
771 -- Expand_Source_Info --
772 ------------------------
774 procedure Expand_Source_Info
(N
: Node_Id
; Nam
: Name_Id
) is
775 Loc
: constant Source_Ptr
:= Sloc
(N
);
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
);
789 Buf
: Bounded_String
;
791 Add_Source_Info
(Buf
, Loc
, Nam
);
792 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> +Buf
));
793 Analyze_And_Resolve
(N
, Standard_String
);
797 Set_Is_Static_Expression
(N
);
798 end Expand_Source_Info
;
800 ---------------------------
801 -- Expand_Unc_Conversion --
802 ---------------------------
804 procedure Expand_Unc_Conversion
(N
: Node_Id
; E
: Entity_Id
) is
805 Func
: constant Entity_Id
:= Entity
(Name
(N
));
811 -- Rewrite as unchecked conversion node. Note that we must convert
812 -- the operand to the formal type of the input parameter of the
813 -- function, so that the resulting N_Unchecked_Type_Conversion
814 -- call indicates the correct types for Gigi.
816 -- Right now, we only do this if a scalar type is involved. It is
817 -- not clear if it is needed in other cases. If we do attempt to
818 -- do the conversion unconditionally, it crashes 3411-018. To be
819 -- investigated further ???
821 Conv
:= Relocate_Node
(First_Actual
(N
));
822 Ftyp
:= Etype
(First_Formal
(Func
));
824 if Is_Scalar_Type
(Ftyp
) then
825 Conv
:= Convert_To
(Ftyp
, Conv
);
826 Set_Parent
(Conv
, N
);
827 Analyze_And_Resolve
(Conv
);
830 -- The instantiation of Unchecked_Conversion creates a wrapper package,
831 -- and the target type is declared as a subtype of the actual. Recover
832 -- the actual, which is the subtype indic. in the subtype declaration
833 -- for the target type. This is semantically correct, and avoids
834 -- anomalies with access subtypes. For entities, leave type as is.
836 -- We do the analysis here, because we do not want the compiler
837 -- to try to optimize or otherwise reorganize the unchecked
842 if Is_Entity_Name
(Conv
) then
845 elsif Nkind
(Parent
(Ttyp
)) = N_Subtype_Declaration
then
846 Ttyp
:= Entity
(Subtype_Indication
(Parent
(Etype
(E
))));
848 elsif Is_Itype
(Ttyp
) then
850 Entity
(Subtype_Indication
(Associated_Node_For_Itype
(Ttyp
)));
855 Rewrite
(N
, Unchecked_Convert_To
(Ttyp
, Conv
));
856 Analyze_And_Resolve
(N
, Ttyp
);
857 end Expand_Unc_Conversion
;
859 -----------------------------
860 -- Expand_Unc_Deallocation --
861 -----------------------------
863 procedure Expand_Unc_Deallocation
(N
: Node_Id
) is
864 Arg
: constant Node_Id
:= First_Actual
(N
);
865 Loc
: constant Source_Ptr
:= Sloc
(N
);
866 Typ
: constant Entity_Id
:= Etype
(Arg
);
867 Desig_Typ
: constant Entity_Id
:=
868 Available_View
(Designated_Type
(Typ
));
869 Needs_Fin
: constant Boolean := Needs_Finalization
(Desig_Typ
);
870 Root_Typ
: constant Entity_Id
:= Underlying_Type
(Root_Type
(Typ
));
871 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Root_Typ
);
872 Stmts
: constant List_Id
:= New_List
;
874 Arg_Known_Non_Null
: constant Boolean := Known_Non_Null
(N
);
875 -- This captures whether we know the argument to be non-null so that
876 -- we can avoid the test. The reason that we need to capture this is
877 -- that we analyze some generated statements before properly attaching
878 -- them to the tree, and that can disturb current value settings.
880 Exceptions_OK
: constant Boolean :=
881 not Restriction_Active
(No_Exception_Propagation
);
883 Abrt_Blk
: Node_Id
:= Empty
;
884 Abrt_Blk_Id
: Entity_Id
;
889 Fin_Data
: Finalization_Exception_Data
;
896 -- Nothing to do if we know the argument is null
898 if Known_Null
(N
) then
902 -- Processing for pointer to controlled types. Generate:
904 -- Abrt : constant Boolean := ...;
905 -- Ex : Exception_Occurrence;
906 -- Raised : Boolean := False;
912 -- [Deep_]Finalize (Obj_Ref);
916 -- if not Raised then
918 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
921 -- Abort_Undefer_Direct;
924 -- Depending on whether exception propagation is enabled and/or aborts
925 -- are allowed, the generated code may lack block statements.
929 -- Ada 2005 (AI-251): In case of abstract interface type we displace
930 -- the pointer to reference the base of the object to deallocate its
931 -- memory, unless we're targetting a VM, in which case no special
932 -- processing is required.
934 if Is_Interface
(Directly_Designated_Type
(Typ
))
935 and then Tagged_Type_Expansion
938 Make_Explicit_Dereference
(Loc
,
940 Unchecked_Convert_To
(Typ
,
941 Make_Function_Call
(Loc
,
943 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
944 Parameter_Associations
=> New_List
(
945 Unchecked_Convert_To
(RTE
(RE_Address
),
946 Duplicate_Subexpr_No_Checks
(Arg
))))));
950 Make_Explicit_Dereference
(Loc
,
951 Prefix
=> Duplicate_Subexpr_No_Checks
(Arg
));
954 -- If the designated type is tagged, the finalization call must
955 -- dispatch because the designated type may not be the actual type
956 -- of the object. If the type is synchronized, the deallocation
957 -- applies to the corresponding record type.
959 if Is_Tagged_Type
(Desig_Typ
) then
960 if Is_Concurrent_Type
(Desig_Typ
) then
963 (Class_Wide_Type
(Corresponding_Record_Type
(Desig_Typ
)),
966 elsif not Is_Class_Wide_Type
(Desig_Typ
) then
968 Unchecked_Convert_To
(Class_Wide_Type
(Desig_Typ
), Obj_Ref
);
971 -- Otherwise the designated type is untagged. Set the type of the
972 -- dereference explicitly to force a conversion when needed given
973 -- that [Deep_]Finalize may be inherited from a parent type.
976 Set_Etype
(Obj_Ref
, Desig_Typ
);
980 -- [Deep_]Finalize (Obj_Ref);
982 Fin_Call
:= Make_Final_Call
(Obj_Ref
=> Obj_Ref
, Typ
=> Desig_Typ
);
985 -- Abrt : constant Boolean := ...;
986 -- Ex : Exception_Occurrence;
987 -- Raised : Boolean := False;
994 -- if not Raised then
996 -- Save_Occurrence (Ex, Get_Current_Excep.all.all);
999 if Exceptions_OK
then
1000 Build_Object_Declarations
(Fin_Data
, Stmts
, Loc
);
1003 Make_Block_Statement
(Loc
,
1004 Handled_Statement_Sequence
=>
1005 Make_Handled_Sequence_Of_Statements
(Loc
,
1006 Statements
=> New_List
(Fin_Call
),
1007 Exception_Handlers
=> New_List
(
1008 Build_Exception_Handler
(Fin_Data
))));
1010 -- Otherwise exception propagation is not allowed
1013 Fin_Blk
:= Fin_Call
;
1016 -- The finalization action must be protected by an abort defer and
1017 -- undefer pair when aborts are allowed. Generate:
1023 -- Abort_Undefer_Direct;
1026 if Abort_Allowed
then
1027 AUD
:= RTE
(RE_Abort_Undefer_Direct
);
1030 Make_Handled_Sequence_Of_Statements
(Loc
,
1031 Statements
=> New_List
(
1032 Build_Runtime_Call
(Loc
, RE_Abort_Defer
),
1034 At_End_Proc
=> New_Occurrence_Of
(AUD
, Loc
));
1037 Make_Block_Statement
(Loc
,
1038 Handled_Statement_Sequence
=> Abrt_HSS
);
1040 Add_Block_Identifier
(Abrt_Blk
, Abrt_Blk_Id
);
1041 Expand_At_End_Handler
(Abrt_HSS
, Abrt_Blk_Id
);
1043 -- Present the Abort_Undefer_Direct function to the backend so
1044 -- that it can inline the call to the function.
1046 Add_Inlined_Body
(AUD
, N
);
1048 -- Otherwise aborts are not allowed
1051 Abrt_Blk
:= Fin_Blk
;
1054 Append_To
(Stmts
, Abrt_Blk
);
1057 -- For a task type, call Free_Task before freeing the ATCB. We used to
1058 -- detect the case of Abort followed by a Free here, because the Free
1059 -- wouldn't actually free if it happens before the aborted task actually
1060 -- terminates. The warning was removed, because Free now works properly
1061 -- (the task will be freed once it terminates).
1063 if Is_Task_Type
(Desig_Typ
) then
1065 Cleanup_Task
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1067 -- For composite types that contain tasks, recurse over the structure
1068 -- to build the selectors for the task subcomponents.
1070 elsif Has_Task
(Desig_Typ
) then
1071 if Is_Array_Type
(Desig_Typ
) then
1072 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_Typ
));
1074 elsif Is_Record_Type
(Desig_Typ
) then
1075 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_Typ
));
1079 -- Same for simple protected types. Eventually call Finalize_Protection
1080 -- before freeing the PO for each protected component.
1082 if Is_Simple_Protected_Type
(Desig_Typ
) then
1084 Cleanup_Protected_Object
(N
, Duplicate_Subexpr_No_Checks
(Arg
)));
1086 elsif Has_Simple_Protected_Object
(Desig_Typ
) then
1087 if Is_Array_Type
(Desig_Typ
) then
1088 Append_List_To
(Stmts
, Cleanup_Array
(N
, Arg
, Desig_Typ
));
1090 elsif Is_Record_Type
(Desig_Typ
) then
1091 Append_List_To
(Stmts
, Cleanup_Record
(N
, Arg
, Desig_Typ
));
1095 -- Normal processing for non-controlled types. The argument to free is
1096 -- a renaming rather than a constant to ensure that the original context
1097 -- is always set to null after the deallocation takes place.
1099 Free_Arg
:= Duplicate_Subexpr_No_Checks
(Arg
, Renaming_Req
=> True);
1100 Free_Nod
:= Make_Free_Statement
(Loc
, Empty
);
1101 Append_To
(Stmts
, Free_Nod
);
1102 Set_Storage_Pool
(Free_Nod
, Pool
);
1104 -- Attach to tree before analysis of generated subtypes below
1106 Set_Parent
(Stmts
, Parent
(N
));
1108 -- Deal with storage pool
1110 if Present
(Pool
) then
1112 -- Freeing the secondary stack is meaningless
1114 if Is_RTE
(Pool
, RE_SS_Pool
) then
1117 -- If the pool object is of a simple storage pool type, then attempt
1118 -- to locate the type's Deallocate procedure, if any, and set the
1119 -- free operation's procedure to call. If the type doesn't have a
1120 -- Deallocate (which is allowed), then the actual will simply be set
1124 (Get_Rep_Pragma
(Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
1127 Pool_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
1128 Dealloc
: Entity_Id
;
1131 Dealloc
:= Get_Name_Entity_Id
(Name_Deallocate
);
1132 while Present
(Dealloc
) loop
1133 if Scope
(Dealloc
) = Scope
(Pool_Typ
)
1134 and then Present
(First_Formal
(Dealloc
))
1135 and then Etype
(First_Formal
(Dealloc
)) = Pool_Typ
1137 Set_Procedure_To_Call
(Free_Nod
, Dealloc
);
1140 Dealloc
:= Homonym
(Dealloc
);
1145 -- Case of a class-wide pool type: make a dispatching call to
1146 -- Deallocate through the class-wide Deallocate_Any.
1148 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
1149 Set_Procedure_To_Call
(Free_Nod
, RTE
(RE_Deallocate_Any
));
1151 -- Case of a specific pool type: make a statically bound call
1154 Set_Procedure_To_Call
1155 (Free_Nod
, Find_Storage_Op
(Etype
(Pool
), Name_Deallocate
));
1159 if Present
(Procedure_To_Call
(Free_Nod
)) then
1161 -- For all cases of a Deallocate call, the back-end needs to be able
1162 -- to compute the size of the object being freed. This may require
1163 -- some adjustments for objects of dynamic size.
1165 -- If the type is class wide, we generate an implicit type with the
1166 -- right dynamic size, so that the deallocate call gets the right
1167 -- size parameter computed by GIGI. Same for an access to
1168 -- unconstrained packed array.
1170 if Is_Class_Wide_Type
(Desig_Typ
)
1172 (Is_Packed_Array
(Desig_Typ
)
1173 and then not Is_Constrained
(Desig_Typ
))
1176 Deref
: constant Node_Id
:=
1177 Make_Explicit_Dereference
(Loc
,
1178 Duplicate_Subexpr_No_Checks
(Arg
));
1183 -- Perform minor decoration as it is needed by the side effect
1184 -- removal mechanism.
1186 Set_Etype
(Deref
, Desig_Typ
);
1187 Set_Parent
(Deref
, Free_Nod
);
1188 D_Subtyp
:= Make_Subtype_From_Expr
(Deref
, Desig_Typ
);
1190 if Nkind
(D_Subtyp
) in N_Has_Entity
then
1191 D_Type
:= Entity
(D_Subtyp
);
1194 D_Type
:= Make_Temporary
(Loc
, 'A');
1195 Insert_Action
(Deref
,
1196 Make_Subtype_Declaration
(Loc
,
1197 Defining_Identifier
=> D_Type
,
1198 Subtype_Indication
=> D_Subtyp
));
1201 -- Force freezing at the point of the dereference. For the
1202 -- class wide case, this avoids having the subtype frozen
1203 -- before the equivalent type.
1205 Freeze_Itype
(D_Type
, Deref
);
1207 Set_Actual_Designated_Subtype
(Free_Nod
, D_Type
);
1212 -- Ada 2005 (AI-251): In case of abstract interface type we must
1213 -- displace the pointer to reference the base of the object to
1214 -- deallocate its memory, unless we're targetting a VM, in which case
1215 -- no special processing is required.
1218 -- free (Base_Address (Obj_Ptr))
1220 if Is_Interface
(Directly_Designated_Type
(Typ
))
1221 and then Tagged_Type_Expansion
1223 Set_Expression
(Free_Nod
,
1224 Unchecked_Convert_To
(Typ
,
1225 Make_Function_Call
(Loc
,
1227 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
1228 Parameter_Associations
=> New_List
(
1229 Unchecked_Convert_To
(RTE
(RE_Address
), Free_Arg
)))));
1235 Set_Expression
(Free_Nod
, Free_Arg
);
1238 -- Only remaining step is to set result to null, or generate a raise of
1239 -- Constraint_Error if the target object is "not null".
1241 if Can_Never_Be_Null
(Etype
(Arg
)) then
1243 Make_Raise_Constraint_Error
(Loc
,
1244 Reason
=> CE_Access_Check_Failed
));
1248 Lhs
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Arg
);
1250 Set_Assignment_OK
(Lhs
);
1252 Make_Assignment_Statement
(Loc
,
1254 Expression
=> Make_Null
(Loc
)));
1258 -- Generate a test of whether any earlier finalization raised an
1259 -- exception, and in that case raise Program_Error with the previous
1260 -- exception occurrence.
1263 -- if Raised and then not Abrt then
1264 -- raise Program_Error; -- for restricted RTS
1266 -- Raise_From_Controlled_Operation (E); -- all other cases
1269 if Needs_Fin
and then Exceptions_OK
then
1270 Append_To
(Stmts
, Build_Raise_Statement
(Fin_Data
));
1273 -- If we know the argument is non-null, then make a block statement
1274 -- that contains the required statements, no need for a test.
1276 if Arg_Known_Non_Null
then
1278 Make_Block_Statement
(Loc
,
1279 Handled_Statement_Sequence
=>
1280 Make_Handled_Sequence_Of_Statements
(Loc
,
1281 Statements
=> Stmts
));
1283 -- If the argument may be null, wrap the statements inside an IF that
1284 -- does an explicit test to exclude the null case.
1288 Make_Implicit_If_Statement
(N
,
1291 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
1292 Right_Opnd
=> Make_Null
(Loc
)),
1293 Then_Statements
=> Stmts
);
1298 Rewrite
(N
, Gen_Code
);
1300 end Expand_Unc_Deallocation
;
1302 -----------------------
1303 -- Expand_To_Address --
1304 -----------------------
1306 procedure Expand_To_Address
(N
: Node_Id
) is
1307 Loc
: constant Source_Ptr
:= Sloc
(N
);
1308 Arg
: constant Node_Id
:= First_Actual
(N
);
1312 if Is_Modular_Integer_Type
(Etype
(Arg
)) then
1313 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1318 Remove_Side_Effects
(Arg
);
1320 Obj
:= Make_Explicit_Dereference
(Loc
, Relocate_Node
(Arg
));
1323 Make_If_Expression
(Loc
,
1324 Expressions
=> New_List
(
1326 Left_Opnd
=> New_Copy_Tree
(Arg
),
1327 Right_Opnd
=> Make_Null
(Loc
)),
1328 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
1329 Make_Attribute_Reference
(Loc
,
1331 Attribute_Name
=> Name_Address
))));
1333 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1334 end Expand_To_Address
;
1336 -----------------------
1337 -- Expand_To_Integer --
1338 -----------------------
1340 procedure Expand_To_Integer
(N
: Node_Id
) is
1341 Arg
: constant Node_Id
:= First_Actual
(N
);
1344 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1346 end Expand_To_Integer
;
1348 -----------------------
1349 -- Expand_To_Pointer --
1350 -----------------------
1352 procedure Expand_To_Pointer
(N
: Node_Id
) is
1353 Arg
: constant Node_Id
:= First_Actual
(N
);
1356 Rewrite
(N
, Unchecked_Convert_To
(Etype
(N
), Arg
));
1358 end Expand_To_Pointer
;