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