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 -- This module handles four kinds of inlining activity:
28 -- a) Instantiation of generic bodies. This is done unconditionally, after
29 -- analysis and expansion of the main unit.
31 -- b) Compilation of unit bodies that contain the bodies of inlined sub-
32 -- programs. This is done only if inlining is enabled (-gnatn). Full inlining
33 -- requires that a) and b) be mutually recursive, because each step may
34 -- generate another generic expansion and further inlined calls.
36 -- c) Front-end inlining for Inline_Always subprograms. This is primarily an
37 -- expansion activity that is performed for performance reasons, and when the
38 -- target does not use the GCC back end.
40 -- d) Front-end inlining for GNATprove, to perform source transformations
41 -- to simplify formal verification. The machinery used is the same as for
42 -- Inline_Always subprograms, but there are fewer restrictions on the source
47 with Types
; use Types
;
48 with Warnsw
; use Warnsw
;
52 --------------------------------
53 -- Generic Body Instantiation --
54 --------------------------------
56 -- The bodies of generic instantiations are built after semantic analysis
57 -- of the main unit is complete. Generic instantiations are saved in a
58 -- global data structure, and the bodies constructed by means of a separate
59 -- analysis and expansion step.
61 -- See full description in body of Sem_Ch12 for more details
63 type Pending_Body_Info
is record
65 -- Node for instantiation that requires the body
68 -- Declaration for package or subprogram spec for instantiation
71 -- Enclosing finalization scope for package instantiation
73 Config_Switches
: Config_Switches_Type
;
74 -- Capture the values of configuration switches
76 Current_Sem_Unit
: Unit_Number_Type
;
77 -- The semantic unit within which the instantiation is found. Must be
78 -- restored when compiling the body, to insure that internal entities
79 -- use the same counter and are unique over spec and body.
81 Expander_Status
: Boolean;
82 -- If the body is instantiated only for semantic checking, expansion
85 Scope_Suppress
: Suppress_Record
;
86 Local_Suppress_Stack_Top
: Suppress_Stack_Entry_Ptr
;
87 -- Save suppress information at the point of instantiation. Used to
88 -- properly inherit check status active at this point (see RM 11.5
89 -- (7.2/2), AI95-00224-01):
91 -- "If a checking pragma applies to a generic instantiation, then the
92 -- checking pragma also applies to the instance. If a checking pragma
93 -- applies to a call to a subprogram that has a pragma Inline applied
94 -- to it, then the checking pragma also applies to the inlined
97 -- This means we have to capture this information from the current scope
98 -- at the point of instantiation.
100 Warnings
: Warnings_State
;
101 -- Capture values of warning flags
108 procedure Initialize
;
109 -- Initialize internal tables
112 -- Lock internal tables before calling backend
114 procedure Instantiate_Bodies
;
115 -- This procedure is called after semantic analysis is complete, to
116 -- instantiate the bodies of generic instantiations that appear in the
119 procedure Add_Inlined_Body
(E
: Entity_Id
; N
: Node_Id
);
120 -- E is an inlined subprogram appearing in a call, either explicitly or in
121 -- a discriminant check for which gigi builds a call or an at-end handler.
122 -- Add E's enclosing unit to Inlined_Bodies so that E can be subsequently
123 -- retrieved and analyzed. N is the node giving rise to the call to E.
125 procedure Add_Pending_Instantiation
128 Fin_Scop
: Node_Id
:= Empty
);
129 -- Add an entry in the table of generic bodies to be instantiated.
131 procedure Analyze_Inlined_Bodies
;
132 -- At end of compilation, analyze the bodies of all units that contain
133 -- inlined subprograms that are actually called.
135 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
136 -- If a subprogram has pragma Inline and inlining is active, use generic
137 -- machinery to build an unexpanded body for the subprogram. This body is
138 -- subsequently used for inline expansions at call sites. If subprogram can
139 -- be inlined (depending on size and nature of local declarations) the
140 -- template body is created. Otherwise subprogram body is treated normally
141 -- and calls are not inlined in the frontend. If proper warnings are
142 -- enabled and the subprogram contains a construct that cannot be inlined,
143 -- the problematic construct is flagged accordingly.
145 function Call_Can_Be_Inlined_In_GNATprove_Mode
147 Subp
: Entity_Id
) return Boolean;
148 -- Returns False if the call in node N to subprogram Subp cannot be inlined
149 -- in GNATprove mode, because it may lead to missing a check on type
150 -- conversion of input parameters otherwise. Returns True otherwise.
152 function Can_Be_Inlined_In_GNATprove_Mode
153 (Spec_Id
: Entity_Id
;
154 Body_Id
: Entity_Id
) return Boolean;
155 -- Returns True if the subprogram identified by Spec_Id and Body_Id can
156 -- be inlined in GNATprove mode. One but not both of Spec_Id and Body_Id
157 -- can be Empty. Body_Id is Empty when doing a partial check on a call
158 -- to a subprogram whose body has not been seen yet, to know whether this
159 -- subprogram could possibly be inlined. GNATprove relies on this to adapt
160 -- its treatment of the subprogram.
162 procedure Cannot_Inline
166 Is_Serious
: Boolean := False;
167 Suppress_Info
: Boolean := False);
168 -- This procedure is called if the node N, an instance of a call to
169 -- subprogram Subp, cannot be inlined. Msg is the message to be issued,
170 -- which ends with ? (it does not end with ?p?, this routine takes care of
171 -- the need to change ? to ?p?). Suppress_Info is set to True to prevent
172 -- issuing an info message in GNATprove mode. The behavior of this routine
173 -- depends on the value of Back_End_Inlining:
175 -- * If Back_End_Inlining is not set (ie. legacy frontend inlining model)
176 -- then if Subp has a pragma Always_Inlined, then an error message is
177 -- issued (by removing the last character of Msg). If Subp is not
178 -- Always_Inlined, then a warning is issued if the flag Ineffective_
179 -- Inline_Warnings is set, adding ?p to the msg, and if not, the call
182 -- * If Back_End_Inlining is set then:
183 -- - If Is_Serious is true, then an error is reported (by removing the
184 -- last character of Msg);
188 -- * Compiling without optimizations if Subp has a pragma
189 -- Always_Inlined, then an error message is issued; if Subp is
190 -- not Always_Inlined, then a warning is issued if the flag
191 -- Ineffective_Inline_Warnings is set (adding p?), and if not,
192 -- the call has no effect.
194 -- * Compiling with optimizations then a warning is issued if the
195 -- flag Ineffective_Inline_Warnings is set (adding p?); otherwise
196 -- no effect since inlining may be performed by the backend.
198 procedure Check_And_Split_Unconstrained_Function
201 Body_Id
: Entity_Id
);
202 -- Spec_Id and Body_Id are the entities of the specification and body of
203 -- the subprogram body N. If N can be inlined by the frontend (supported
204 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
205 -- associated with N and attach it to the declaration node of Spec_Id.
207 procedure Check_Object_Renaming_In_GNATprove_Mode
(Spec_Id
: Entity_Id
)
209 Pre
=> GNATprove_Mode
;
210 -- This procedure is called only in GNATprove mode, on subprograms for
211 -- which a Body_To_Inline was created, to check if the subprogram has
212 -- references to object renamings which will be replaced by the special
213 -- SPARK expansion into nodes of a different kind, which is not expected
214 -- by the inlining mechanism. In that case, the Body_To_Inline is deleted.
216 procedure Check_Package_Body_For_Inlining
(N
: Node_Id
; P
: Entity_Id
);
217 -- If front-end inlining is enabled and a package declaration contains
218 -- inlined subprograms, load and compile the package body to collect the
219 -- bodies of these subprograms, so they are available to inline calls.
220 -- N is the compilation unit for the package.
222 procedure Expand_Inlined_Call
225 Orig_Subp
: Entity_Id
);
226 -- If called subprogram can be inlined by the front-end, retrieve the
227 -- analyzed body, replace formals with actuals and expand call in place.
228 -- Generate thunks for actuals that are expressions, and insert the
229 -- corresponding constant declarations before the call. If the original
230 -- call is to a derived operation, the return type is the one of the
231 -- derived operation, but the body is that of the original, so return
232 -- expressions in the body must be converted to the desired type (which
233 -- is simply not noted in the tree without inline expansion).
235 function Has_Excluded_Declaration
237 Decls
: List_Id
) return Boolean;
238 -- Check a list of declarations, Decls, that make the inlining of Subp not
241 function Has_Excluded_Statement
243 Stats
: List_Id
) return Boolean;
244 -- Check a list of statements, Stats, that make inlining of Subp not
245 -- worthwhile, including any tasking statement, nested at any level.
247 procedure Inline_Static_Function_Call
248 (N
: Node_Id
; Subp
: Entity_Id
);
249 -- Evaluate static call to a static function Subp, substituting actuals in
250 -- place of references to their corresponding formals and rewriting the
251 -- call N as a fully folded and static result expression.
253 procedure List_Inlining_Info
;
254 -- Generate listing of calls inlined by the frontend plus listing of
255 -- calls to inline subprograms passed to the backend.
257 procedure Remove_Dead_Instance
(N
: Node_Id
);
258 -- If an instantiation appears in unreachable code, delete the pending