111
|
1 ------------------------------------------------------------------------------
|
|
2 -- --
|
|
3 -- GNAT COMPILER COMPONENTS --
|
|
4 -- --
|
|
5 -- I N L I N E --
|
|
6 -- --
|
|
7 -- S p e c --
|
|
8 -- --
|
145
|
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
|
111
|
10 -- --
|
|
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. --
|
|
20 -- --
|
|
21 -- GNAT was originally developed by the GNAT team at New York University. --
|
|
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
|
|
23 -- --
|
|
24 ------------------------------------------------------------------------------
|
|
25
|
|
26 -- This module handles four kinds of inlining activity:
|
|
27
|
|
28 -- a) Instantiation of generic bodies. This is done unconditionally, after
|
|
29 -- analysis and expansion of the main unit.
|
|
30
|
|
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.
|
|
35
|
|
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.
|
|
39
|
|
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
|
|
43 -- of subprograms.
|
|
44
|
|
45 with Opt; use Opt;
|
|
46 with Sem; use Sem;
|
|
47 with Types; use Types;
|
|
48 with Warnsw; use Warnsw;
|
|
49
|
|
50 package Inline is
|
|
51
|
|
52 --------------------------------
|
|
53 -- Generic Body Instantiation --
|
|
54 --------------------------------
|
|
55
|
|
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.
|
|
60
|
|
61 -- See full description in body of Sem_Ch12 for more details
|
|
62
|
|
63 type Pending_Body_Info is record
|
|
64 Act_Decl : Node_Id;
|
|
65 -- Declaration for package or subprogram spec for instantiation
|
|
66
|
131
|
67 Config_Switches : Config_Switches_Type;
|
|
68 -- Capture the values of configuration switches
|
|
69
|
|
70 Current_Sem_Unit : Unit_Number_Type;
|
|
71 -- The semantic unit within which the instantiation is found. Must be
|
|
72 -- restored when compiling the body, to insure that internal entities
|
|
73 -- use the same counter and are unique over spec and body.
|
|
74
|
111
|
75 Expander_Status : Boolean;
|
|
76 -- If the body is instantiated only for semantic checking, expansion
|
|
77 -- must be inhibited.
|
|
78
|
131
|
79 Inst_Node : Node_Id;
|
|
80 -- Node for instantiation that requires the body
|
111
|
81
|
|
82 Scope_Suppress : Suppress_Record;
|
|
83 Local_Suppress_Stack_Top : Suppress_Stack_Entry_Ptr;
|
|
84 -- Save suppress information at the point of instantiation. Used to
|
|
85 -- properly inherit check status active at this point (see RM 11.5
|
|
86 -- (7.2/2), AI95-00224-01):
|
|
87 --
|
|
88 -- "If a checking pragma applies to a generic instantiation, then the
|
|
89 -- checking pragma also applies to the instance. If a checking pragma
|
|
90 -- applies to a call to a subprogram that has a pragma Inline applied
|
|
91 -- to it, then the checking pragma also applies to the inlined
|
|
92 -- subprogram body".
|
|
93 --
|
|
94 -- This means we have to capture this information from the current scope
|
|
95 -- at the point of instantiation.
|
|
96
|
|
97 Warnings : Warning_Record;
|
|
98 -- Capture values of warning flags
|
|
99 end record;
|
|
100
|
|
101 -----------------
|
|
102 -- Subprograms --
|
|
103 -----------------
|
|
104
|
|
105 procedure Initialize;
|
|
106 -- Initialize internal tables
|
|
107
|
|
108 procedure Lock;
|
|
109 -- Lock internal tables before calling backend
|
|
110
|
|
111 procedure Instantiate_Bodies;
|
|
112 -- This procedure is called after semantic analysis is complete, to
|
|
113 -- instantiate the bodies of generic instantiations that appear in the
|
|
114 -- compilation unit.
|
|
115
|
|
116 procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id);
|
|
117 -- E is an inlined subprogram appearing in a call, either explicitly or in
|
|
118 -- a discriminant check for which gigi builds a call or an at-end handler.
|
|
119 -- Add E's enclosing unit to Inlined_Bodies so that E can be subsequently
|
|
120 -- retrieved and analyzed. N is the node giving rise to the call to E.
|
|
121
|
145
|
122 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id);
|
|
123 -- Add an entry in the table of generic bodies to be instantiated.
|
|
124
|
111
|
125 procedure Analyze_Inlined_Bodies;
|
|
126 -- At end of compilation, analyze the bodies of all units that contain
|
|
127 -- inlined subprograms that are actually called.
|
|
128
|
|
129 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
|
|
130 -- If a subprogram has pragma Inline and inlining is active, use generic
|
|
131 -- machinery to build an unexpanded body for the subprogram. This body is
|
|
132 -- subsequently used for inline expansions at call sites. If subprogram can
|
|
133 -- be inlined (depending on size and nature of local declarations) the
|
|
134 -- template body is created. Otherwise subprogram body is treated normally
|
|
135 -- and calls are not inlined in the frontend. If proper warnings are
|
|
136 -- enabled and the subprogram contains a construct that cannot be inlined,
|
|
137 -- the problematic construct is flagged accordingly.
|
|
138
|
|
139 function Call_Can_Be_Inlined_In_GNATprove_Mode
|
|
140 (N : Node_Id;
|
|
141 Subp : Entity_Id) return Boolean;
|
|
142 -- Returns False if the call in node N to subprogram Subp cannot be inlined
|
|
143 -- in GNATprove mode, because it may lead to missing a check on type
|
|
144 -- conversion of input parameters otherwise. Returns True otherwise.
|
|
145
|
|
146 function Can_Be_Inlined_In_GNATprove_Mode
|
|
147 (Spec_Id : Entity_Id;
|
|
148 Body_Id : Entity_Id) return Boolean;
|
|
149 -- Returns True if the subprogram identified by Spec_Id and Body_Id can
|
|
150 -- be inlined in GNATprove mode. One but not both of Spec_Id and Body_Id
|
|
151 -- can be Empty. Body_Id is Empty when doing a partial check on a call
|
|
152 -- to a subprogram whose body has not been seen yet, to know whether this
|
|
153 -- subprogram could possibly be inlined. GNATprove relies on this to adapt
|
|
154 -- its treatment of the subprogram.
|
|
155
|
|
156 procedure Cannot_Inline
|
|
157 (Msg : String;
|
|
158 N : Node_Id;
|
|
159 Subp : Entity_Id;
|
|
160 Is_Serious : Boolean := False);
|
|
161 -- This procedure is called if the node N, an instance of a call to
|
|
162 -- subprogram Subp, cannot be inlined. Msg is the message to be issued,
|
|
163 -- which ends with ? (it does not end with ?p?, this routine takes care of
|
|
164 -- the need to change ? to ?p?). The behavior of this routine depends on
|
|
165 -- the value of Back_End_Inlining:
|
|
166 --
|
|
167 -- * If Back_End_Inlining is not set (ie. legacy frontend inlining model)
|
|
168 -- then if Subp has a pragma Always_Inlined, then an error message is
|
|
169 -- issued (by removing the last character of Msg). If Subp is not
|
|
170 -- Always_Inlined, then a warning is issued if the flag Ineffective_
|
|
171 -- Inline_Warnings is set, adding ?p to the msg, and if not, the call
|
|
172 -- has no effect.
|
|
173 --
|
|
174 -- * If Back_End_Inlining is set then:
|
|
175 -- - If Is_Serious is true, then an error is reported (by removing the
|
|
176 -- last character of Msg);
|
|
177 --
|
|
178 -- - otherwise:
|
|
179 --
|
|
180 -- * Compiling without optimizations if Subp has a pragma
|
|
181 -- Always_Inlined, then an error message is issued; if Subp is
|
|
182 -- not Always_Inlined, then a warning is issued if the flag
|
|
183 -- Ineffective_Inline_Warnings is set (adding p?), and if not,
|
|
184 -- the call has no effect.
|
|
185 --
|
|
186 -- * Compiling with optimizations then a warning is issued if the
|
|
187 -- flag Ineffective_Inline_Warnings is set (adding p?); otherwise
|
|
188 -- no effect since inlining may be performed by the backend.
|
|
189
|
|
190 procedure Check_And_Split_Unconstrained_Function
|
|
191 (N : Node_Id;
|
|
192 Spec_Id : Entity_Id;
|
|
193 Body_Id : Entity_Id);
|
|
194 -- Spec_Id and Body_Id are the entities of the specification and body of
|
|
195 -- the subprogram body N. If N can be inlined by the frontend (supported
|
|
196 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
|
|
197 -- associated with N and attach it to the declaration node of Spec_Id.
|
|
198
|
|
199 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id);
|
|
200 -- If front-end inlining is enabled and a package declaration contains
|
|
201 -- inlined subprograms, load and compile the package body to collect the
|
|
202 -- bodies of these subprograms, so they are available to inline calls.
|
|
203 -- N is the compilation unit for the package.
|
|
204
|
|
205 procedure Expand_Inlined_Call
|
|
206 (N : Node_Id;
|
|
207 Subp : Entity_Id;
|
|
208 Orig_Subp : Entity_Id);
|
|
209 -- If called subprogram can be inlined by the front-end, retrieve the
|
|
210 -- analyzed body, replace formals with actuals and expand call in place.
|
|
211 -- Generate thunks for actuals that are expressions, and insert the
|
|
212 -- corresponding constant declarations before the call. If the original
|
|
213 -- call is to a derived operation, the return type is the one of the
|
|
214 -- derived operation, but the body is that of the original, so return
|
|
215 -- expressions in the body must be converted to the desired type (which
|
|
216 -- is simply not noted in the tree without inline expansion).
|
|
217
|
|
218 function Has_Excluded_Declaration
|
|
219 (Subp : Entity_Id;
|
|
220 Decls : List_Id) return Boolean;
|
|
221 -- Check a list of declarations, Decls, that make the inlining of Subp not
|
|
222 -- worthwhile
|
|
223
|
|
224 function Has_Excluded_Statement
|
|
225 (Subp : Entity_Id;
|
|
226 Stats : List_Id) return Boolean;
|
|
227 -- Check a list of statements, Stats, that make inlining of Subp not
|
|
228 -- worthwhile, including any tasking statement, nested at any level.
|
|
229
|
|
230 procedure List_Inlining_Info;
|
|
231 -- Generate listing of calls inlined by the frontend plus listing of
|
|
232 -- calls to inline subprograms passed to the backend.
|
|
233
|
|
234 procedure Remove_Dead_Instance (N : Node_Id);
|
|
235 -- If an instantiation appears in unreachable code, delete the pending
|
|
236 -- body instance.
|
|
237
|
|
238 end Inline;
|