]>
Commit | Line | Data |
---|---|---|
38cbfe40 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- I N L I N E -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
2e885a6f | 9 | -- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- |
38cbfe40 RK |
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- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
38cbfe40 RK |
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 -- | |
b5c84c3c RD |
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. -- | |
38cbfe40 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
38cbfe40 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
697b781a | 26 | with Aspects; use Aspects; |
38cbfe40 | 27 | with Atree; use Atree; |
16b10ccc | 28 | with Debug; use Debug; |
38cbfe40 RK |
29 | with Einfo; use Einfo; |
30 | with Elists; use Elists; | |
31 | with Errout; use Errout; | |
540d8610 ES |
32 | with Expander; use Expander; |
33 | with Exp_Ch6; use Exp_Ch6; | |
38cbfe40 | 34 | with Exp_Ch7; use Exp_Ch7; |
38cbfe40 | 35 | with Exp_Tss; use Exp_Tss; |
540d8610 | 36 | with Exp_Util; use Exp_Util; |
38cbfe40 RK |
37 | with Fname; use Fname; |
38 | with Fname.UF; use Fname.UF; | |
39 | with Lib; use Lib; | |
a99ada67 | 40 | with Namet; use Namet; |
540d8610 | 41 | with Nmake; use Nmake; |
38cbfe40 | 42 | with Nlists; use Nlists; |
16b10ccc | 43 | with Output; use Output; |
a4100e55 | 44 | with Sem_Aux; use Sem_Aux; |
38cbfe40 RK |
45 | with Sem_Ch8; use Sem_Ch8; |
46 | with Sem_Ch10; use Sem_Ch10; | |
47 | with Sem_Ch12; use Sem_Ch12; | |
2d180af1 | 48 | with Sem_Prag; use Sem_Prag; |
38cbfe40 RK |
49 | with Sem_Util; use Sem_Util; |
50 | with Sinfo; use Sinfo; | |
2d180af1 | 51 | with Sinput; use Sinput; |
38cbfe40 RK |
52 | with Snames; use Snames; |
53 | with Stand; use Stand; | |
54 | with Uname; use Uname; | |
540d8610 | 55 | with Tbuild; use Tbuild; |
38cbfe40 RK |
56 | |
57 | package body Inline is | |
58 | ||
16b10ccc AC |
59 | Check_Inlining_Restrictions : constant Boolean := True; |
60 | -- In the following cases the frontend rejects inlining because they | |
61 | -- are not handled well by the backend. This variable facilitates | |
62 | -- disabling these restrictions to evaluate future versions of the | |
63 | -- GCC backend in which some of the restrictions may be supported. | |
64 | -- | |
65 | -- - subprograms that have: | |
66 | -- - nested subprograms | |
67 | -- - instantiations | |
68 | -- - package declarations | |
69 | -- - task or protected object declarations | |
70 | -- - some of the following statements: | |
71 | -- - abort | |
72 | -- - asynchronous-select | |
73 | -- - conditional-entry-call | |
74 | -- - delay-relative | |
75 | -- - delay-until | |
76 | -- - selective-accept | |
77 | -- - timed-entry-call | |
78 | ||
79 | Inlined_Calls : Elist_Id; | |
80 | -- List of frontend inlined calls | |
81 | ||
82 | Backend_Calls : Elist_Id; | |
83 | -- List of inline calls passed to the backend | |
84 | ||
85 | Backend_Inlined_Subps : Elist_Id; | |
86 | -- List of subprograms inlined by the backend | |
87 | ||
88 | Backend_Not_Inlined_Subps : Elist_Id; | |
89 | -- List of subprograms that cannot be inlined by the backend | |
90 | ||
38cbfe40 RK |
91 | -------------------- |
92 | -- Inlined Bodies -- | |
93 | -------------------- | |
94 | ||
95 | -- Inlined functions are actually placed in line by the backend if the | |
96 | -- corresponding bodies are available (i.e. compiled). Whenever we find | |
97 | -- a call to an inlined subprogram, we add the name of the enclosing | |
98 | -- compilation unit to a worklist. After all compilation, and after | |
99 | -- expansion of generic bodies, we traverse the list of pending bodies | |
100 | -- and compile them as well. | |
101 | ||
102 | package Inlined_Bodies is new Table.Table ( | |
103 | Table_Component_Type => Entity_Id, | |
104 | Table_Index_Type => Int, | |
105 | Table_Low_Bound => 0, | |
106 | Table_Initial => Alloc.Inlined_Bodies_Initial, | |
107 | Table_Increment => Alloc.Inlined_Bodies_Increment, | |
108 | Table_Name => "Inlined_Bodies"); | |
109 | ||
110 | ----------------------- | |
111 | -- Inline Processing -- | |
112 | ----------------------- | |
113 | ||
114 | -- For each call to an inlined subprogram, we make entries in a table | |
8a49a499 | 115 | -- that stores caller and callee, and indicates the call direction from |
38cbfe40 RK |
116 | -- one to the other. We also record the compilation unit that contains |
117 | -- the callee. After analyzing the bodies of all such compilation units, | |
8a49a499 AC |
118 | -- we compute the transitive closure of inlined subprograms called from |
119 | -- the main compilation unit and make it available to the code generator | |
120 | -- in no particular order, thus allowing cycles in the call graph. | |
38cbfe40 RK |
121 | |
122 | Last_Inlined : Entity_Id := Empty; | |
123 | ||
124 | -- For each entry in the table we keep a list of successors in topological | |
125 | -- order, i.e. callers of the current subprogram. | |
126 | ||
127 | type Subp_Index is new Nat; | |
128 | No_Subp : constant Subp_Index := 0; | |
129 | ||
9de61fcb | 130 | -- The subprogram entities are hashed into the Inlined table |
38cbfe40 RK |
131 | |
132 | Num_Hash_Headers : constant := 512; | |
133 | ||
134 | Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1) | |
135 | of Subp_Index; | |
136 | ||
137 | type Succ_Index is new Nat; | |
138 | No_Succ : constant Succ_Index := 0; | |
139 | ||
140 | type Succ_Info is record | |
141 | Subp : Subp_Index; | |
142 | Next : Succ_Index; | |
143 | end record; | |
144 | ||
3f80a182 AC |
145 | -- The following table stores list elements for the successor lists. These |
146 | -- lists cannot be chained directly through entries in the Inlined table, | |
147 | -- because a given subprogram can appear in several such lists. | |
38cbfe40 RK |
148 | |
149 | package Successors is new Table.Table ( | |
150 | Table_Component_Type => Succ_Info, | |
151 | Table_Index_Type => Succ_Index, | |
152 | Table_Low_Bound => 1, | |
153 | Table_Initial => Alloc.Successors_Initial, | |
154 | Table_Increment => Alloc.Successors_Increment, | |
155 | Table_Name => "Successors"); | |
156 | ||
157 | type Subp_Info is record | |
158 | Name : Entity_Id := Empty; | |
8a49a499 | 159 | Next : Subp_Index := No_Subp; |
38cbfe40 | 160 | First_Succ : Succ_Index := No_Succ; |
38cbfe40 | 161 | Main_Call : Boolean := False; |
8a49a499 | 162 | Processed : Boolean := False; |
38cbfe40 RK |
163 | end record; |
164 | ||
165 | package Inlined is new Table.Table ( | |
166 | Table_Component_Type => Subp_Info, | |
167 | Table_Index_Type => Subp_Index, | |
168 | Table_Low_Bound => 1, | |
169 | Table_Initial => Alloc.Inlined_Initial, | |
170 | Table_Increment => Alloc.Inlined_Increment, | |
171 | Table_Name => "Inlined"); | |
172 | ||
173 | ----------------------- | |
174 | -- Local Subprograms -- | |
175 | ----------------------- | |
176 | ||
38cbfe40 RK |
177 | procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty); |
178 | -- Make two entries in Inlined table, for an inlined subprogram being | |
179 | -- called, and for the inlined subprogram that contains the call. If | |
180 | -- the call is in the main compilation unit, Caller is Empty. | |
181 | ||
4ef36ac7 AC |
182 | procedure Add_Inlined_Subprogram (E : Entity_Id); |
183 | -- Add subprogram E to the list of inlined subprogram for the unit | |
6c26bac2 | 184 | |
38cbfe40 RK |
185 | function Add_Subp (E : Entity_Id) return Subp_Index; |
186 | -- Make entry in Inlined table for subprogram E, or return table index | |
187 | -- that already holds E. | |
188 | ||
6c26bac2 AC |
189 | function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id; |
190 | pragma Inline (Get_Code_Unit_Entity); | |
191 | -- Return the entity node for the unit containing E. Always return the spec | |
192 | -- for a package. | |
193 | ||
38cbfe40 RK |
194 | function Has_Initialized_Type (E : Entity_Id) return Boolean; |
195 | -- If a candidate for inlining contains type declarations for types with | |
31101470 | 196 | -- nontrivial initialization procedures, they are not worth inlining. |
38cbfe40 | 197 | |
6c26bac2 AC |
198 | function Has_Single_Return (N : Node_Id) return Boolean; |
199 | -- In general we cannot inline functions that return unconstrained type. | |
ea0c8cfb RD |
200 | -- However, we can handle such functions if all return statements return a |
201 | -- local variable that is the only declaration in the body of the function. | |
202 | -- In that case the call can be replaced by that local variable as is done | |
203 | -- for other inlined calls. | |
6c26bac2 AC |
204 | |
205 | function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean; | |
206 | -- Return True if E is in the main unit or its spec or in a subunit | |
207 | ||
38cbfe40 | 208 | function Is_Nested (E : Entity_Id) return Boolean; |
3f80a182 AC |
209 | -- If the function is nested inside some other function, it will always |
210 | -- be compiled if that function is, so don't add it to the inline list. | |
211 | -- We cannot compile a nested function outside the scope of the containing | |
212 | -- function anyway. This is also the case if the function is defined in a | |
213 | -- task body or within an entry (for example, an initialization procedure). | |
38cbfe40 | 214 | |
697b781a AC |
215 | procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id); |
216 | -- Remove all aspects and/or pragmas that have no meaning in inlined body | |
217 | -- Body_Decl. The analysis of these items is performed on the non-inlined | |
218 | -- body. The items currently removed are: | |
219 | -- Contract_Cases | |
220 | -- Global | |
221 | -- Depends | |
222 | -- Postcondition | |
223 | -- Precondition | |
224 | -- Refined_Global | |
225 | -- Refined_Depends | |
226 | -- Refined_Post | |
227 | -- Test_Case | |
228 | -- Unmodified | |
229 | -- Unreferenced | |
38cbfe40 RK |
230 | |
231 | ------------------------------ | |
232 | -- Deferred Cleanup Actions -- | |
233 | ------------------------------ | |
234 | ||
235 | -- The cleanup actions for scopes that contain instantiations is delayed | |
3f80a182 AC |
236 | -- until after expansion of those instantiations, because they may contain |
237 | -- finalizable objects or tasks that affect the cleanup code. A scope | |
238 | -- that contains instantiations only needs to be finalized once, even | |
239 | -- if it contains more than one instance. We keep a list of scopes | |
240 | -- that must still be finalized, and call cleanup_actions after all | |
241 | -- the instantiations have been completed. | |
38cbfe40 RK |
242 | |
243 | To_Clean : Elist_Id; | |
244 | ||
245 | procedure Add_Scope_To_Clean (Inst : Entity_Id); | |
9de61fcb | 246 | -- Build set of scopes on which cleanup actions must be performed |
38cbfe40 RK |
247 | |
248 | procedure Cleanup_Scopes; | |
9de61fcb | 249 | -- Complete cleanup actions on scopes that need it |
38cbfe40 RK |
250 | |
251 | -------------- | |
252 | -- Add_Call -- | |
253 | -------------- | |
254 | ||
255 | procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is | |
fbf5a39b | 256 | P1 : constant Subp_Index := Add_Subp (Called); |
38cbfe40 RK |
257 | P2 : Subp_Index; |
258 | J : Succ_Index; | |
259 | ||
260 | begin | |
261 | if Present (Caller) then | |
262 | P2 := Add_Subp (Caller); | |
263 | ||
8a49a499 | 264 | -- Add P1 to the list of successors of P2, if not already there. |
38cbfe40 RK |
265 | -- Note that P2 may contain more than one call to P1, and only |
266 | -- one needs to be recorded. | |
267 | ||
8a49a499 | 268 | J := Inlined.Table (P2).First_Succ; |
38cbfe40 | 269 | while J /= No_Succ loop |
8a49a499 | 270 | if Successors.Table (J).Subp = P1 then |
38cbfe40 RK |
271 | return; |
272 | end if; | |
273 | ||
274 | J := Successors.Table (J).Next; | |
275 | end loop; | |
276 | ||
8a49a499 | 277 | -- On exit, make a successor entry for P1 |
38cbfe40 RK |
278 | |
279 | Successors.Increment_Last; | |
8a49a499 | 280 | Successors.Table (Successors.Last).Subp := P1; |
38cbfe40 | 281 | Successors.Table (Successors.Last).Next := |
8a49a499 AC |
282 | Inlined.Table (P2).First_Succ; |
283 | Inlined.Table (P2).First_Succ := Successors.Last; | |
38cbfe40 RK |
284 | else |
285 | Inlined.Table (P1).Main_Call := True; | |
286 | end if; | |
287 | end Add_Call; | |
288 | ||
289 | ---------------------- | |
290 | -- Add_Inlined_Body -- | |
291 | ---------------------- | |
292 | ||
cf27c5a2 | 293 | procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id) is |
38cbfe40 | 294 | |
4c7be310 AC |
295 | type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package); |
296 | -- Level of inlining for the call: Dont_Inline means no inlining, | |
297 | -- Inline_Call means that only the call is considered for inlining, | |
298 | -- Inline_Package means that the call is considered for inlining and | |
299 | -- its package compiled and scanned for more inlining opportunities. | |
300 | ||
301 | function Must_Inline return Inline_Level_Type; | |
38cbfe40 RK |
302 | -- Inlining is only done if the call statement N is in the main unit, |
303 | -- or within the body of another inlined subprogram. | |
304 | ||
fbf5a39b AC |
305 | ----------------- |
306 | -- Must_Inline -- | |
307 | ----------------- | |
308 | ||
4c7be310 | 309 | function Must_Inline return Inline_Level_Type is |
a99ada67 | 310 | Scop : Entity_Id; |
38cbfe40 RK |
311 | Comp : Node_Id; |
312 | ||
313 | begin | |
fbf5a39b | 314 | -- Check if call is in main unit |
38cbfe40 | 315 | |
a99ada67 RD |
316 | Scop := Current_Scope; |
317 | ||
318 | -- Do not try to inline if scope is standard. This could happen, for | |
319 | -- example, for a call to Add_Global_Declaration, and it causes | |
320 | -- trouble to try to inline at this level. | |
321 | ||
322 | if Scop = Standard_Standard then | |
4c7be310 | 323 | return Dont_Inline; |
a99ada67 RD |
324 | end if; |
325 | ||
326 | -- Otherwise lookup scope stack to outer scope | |
327 | ||
38cbfe40 RK |
328 | while Scope (Scop) /= Standard_Standard |
329 | and then not Is_Child_Unit (Scop) | |
330 | loop | |
331 | Scop := Scope (Scop); | |
332 | end loop; | |
333 | ||
334 | Comp := Parent (Scop); | |
38cbfe40 RK |
335 | while Nkind (Comp) /= N_Compilation_Unit loop |
336 | Comp := Parent (Comp); | |
337 | end loop; | |
338 | ||
4c7be310 AC |
339 | -- If the call is in the main unit, inline the call and compile the |
340 | -- package of the subprogram to find more calls to be inlined. | |
341 | ||
fbf5a39b AC |
342 | if Comp = Cunit (Main_Unit) |
343 | or else Comp = Library_Unit (Cunit (Main_Unit)) | |
38cbfe40 RK |
344 | then |
345 | Add_Call (E); | |
4c7be310 | 346 | return Inline_Package; |
38cbfe40 RK |
347 | end if; |
348 | ||
4ef36ac7 AC |
349 | -- The call is not in the main unit. See if it is in some subprogram |
350 | -- that can be inlined outside its unit. If so, inline the call and, | |
351 | -- if the inlining level is set to 1, stop there; otherwise also | |
352 | -- compile the package as above. | |
38cbfe40 RK |
353 | |
354 | Scop := Current_Scope; | |
355 | while Scope (Scop) /= Standard_Standard | |
356 | and then not Is_Child_Unit (Scop) | |
357 | loop | |
4ef36ac7 AC |
358 | if Is_Overloadable (Scop) |
359 | and then Is_Inlined (Scop) | |
360 | and then not Is_Nested (Scop) | |
361 | then | |
38cbfe40 | 362 | Add_Call (E, Scop); |
2137e8a6 | 363 | |
4c7be310 AC |
364 | if Inline_Level = 1 then |
365 | return Inline_Call; | |
366 | else | |
367 | return Inline_Package; | |
368 | end if; | |
38cbfe40 RK |
369 | end if; |
370 | ||
371 | Scop := Scope (Scop); | |
372 | end loop; | |
373 | ||
4c7be310 | 374 | return Dont_Inline; |
38cbfe40 RK |
375 | end Must_Inline; |
376 | ||
4c7be310 AC |
377 | Level : Inline_Level_Type; |
378 | ||
38cbfe40 RK |
379 | -- Start of processing for Add_Inlined_Body |
380 | ||
381 | begin | |
cf27c5a2 EB |
382 | Append_New_Elmt (N, To => Backend_Calls); |
383 | ||
4ef36ac7 AC |
384 | -- Skip subprograms that cannot be inlined outside their unit |
385 | ||
386 | if Is_Abstract_Subprogram (E) | |
387 | or else Convention (E) = Convention_Protected | |
388 | or else Is_Nested (E) | |
389 | then | |
390 | return; | |
391 | end if; | |
392 | ||
2e885a6f AC |
393 | -- Find out whether the call must be inlined. Unless the result is |
394 | -- Dont_Inline, Must_Inline also creates an edge for the call in the | |
395 | -- callgraph; however, it will not be activated until after Is_Called | |
396 | -- is set on the subprogram. | |
397 | ||
398 | Level := Must_Inline; | |
399 | ||
400 | if Level = Dont_Inline then | |
401 | return; | |
402 | end if; | |
403 | ||
404 | -- If the call was generated by the compiler and is to a subprogram in | |
405 | -- a run-time unit, we need to suppress debugging information for it, | |
406 | -- so that the code that is eventually inlined will not affect the | |
407 | -- debugging of the program. We do not do it if the call comes from | |
408 | -- source because, even if the call is inlined, the user may expect it | |
409 | -- to be present in the debugging information. | |
410 | ||
411 | if not Comes_From_Source (N) | |
412 | and then In_Extended_Main_Source_Unit (N) | |
413 | and then | |
414 | Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (E))) | |
415 | then | |
416 | Set_Needs_Debug_Info (E, False); | |
417 | end if; | |
418 | ||
419 | -- If the subprogram is an expression function, then there is no need to | |
420 | -- load any package body since the body of the function is in the spec. | |
421 | ||
422 | if Is_Expression_Function (E) then | |
423 | Set_Is_Called (E); | |
424 | return; | |
425 | end if; | |
426 | ||
38cbfe40 RK |
427 | -- Find unit containing E, and add to list of inlined bodies if needed. |
428 | -- If the body is already present, no need to load any other unit. This | |
429 | -- is the case for an initialization procedure, which appears in the | |
430 | -- package declaration that contains the type. It is also the case if | |
431 | -- the body has already been analyzed. Finally, if the unit enclosing | |
432 | -- E is an instance, the instance body will be analyzed in any case, | |
433 | -- and there is no need to add the enclosing unit (whose body might not | |
434 | -- be available). | |
435 | ||
436 | -- Library-level functions must be handled specially, because there is | |
437 | -- no enclosing package to retrieve. In this case, it is the body of | |
438 | -- the function that will have to be loaded. | |
439 | ||
2e885a6f AC |
440 | declare |
441 | Pack : constant Entity_Id := Get_Code_Unit_Entity (E); | |
cf27c5a2 | 442 | |
2e885a6f AC |
443 | begin |
444 | if Pack = E then | |
445 | Set_Is_Called (E); | |
446 | Inlined_Bodies.Increment_Last; | |
447 | Inlined_Bodies.Table (Inlined_Bodies.Last) := E; | |
448 | ||
449 | elsif Ekind (Pack) = E_Package then | |
450 | Set_Is_Called (E); | |
451 | ||
452 | if Is_Generic_Instance (Pack) then | |
453 | null; | |
454 | ||
455 | -- Do not inline the package if the subprogram is an init proc | |
456 | -- or other internally generated subprogram, because in that | |
457 | -- case the subprogram body appears in the same unit that | |
458 | -- declares the type, and that body is visible to the back end. | |
459 | -- Do not inline it either if it is in the main unit. | |
460 | -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always | |
461 | -- calls if the back-end takes care of inlining the call. | |
e49de265 | 462 | -- Note that Level in Inline_Package | Inline_Call here. |
2e885a6f | 463 | |
e49de265 BD |
464 | elsif ((Level = Inline_Call |
465 | and then Has_Pragma_Inline_Always (E) | |
466 | and then Back_End_Inlining) | |
467 | or else Level = Inline_Package) | |
2e885a6f AC |
468 | and then not Is_Inlined (Pack) |
469 | and then not Is_Internal (E) | |
470 | and then not In_Main_Unit_Or_Subunit (Pack) | |
471 | then | |
472 | Set_Is_Inlined (Pack); | |
38cbfe40 | 473 | Inlined_Bodies.Increment_Last; |
2e885a6f | 474 | Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack; |
38cbfe40 | 475 | end if; |
2e885a6f | 476 | end if; |
cf27c5a2 | 477 | |
2e885a6f AC |
478 | -- Ensure that Analyze_Inlined_Bodies will be invoked after |
479 | -- completing the analysis of the current unit. | |
480 | ||
481 | Inline_Processing_Required := True; | |
482 | end; | |
38cbfe40 RK |
483 | end Add_Inlined_Body; |
484 | ||
485 | ---------------------------- | |
486 | -- Add_Inlined_Subprogram -- | |
487 | ---------------------------- | |
488 | ||
4ef36ac7 | 489 | procedure Add_Inlined_Subprogram (E : Entity_Id) is |
d8d7e809 | 490 | Decl : constant Node_Id := Parent (Declaration_Node (E)); |
feecad68 | 491 | Pack : constant Entity_Id := Get_Code_Unit_Entity (E); |
38cbfe40 | 492 | |
6c26bac2 AC |
493 | procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id); |
494 | -- Append Subp to the list of subprograms inlined by the backend | |
495 | ||
496 | procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id); | |
497 | -- Append Subp to the list of subprograms that cannot be inlined by | |
ea0c8cfb | 498 | -- the backend. |
6c26bac2 | 499 | |
6c26bac2 AC |
500 | ----------------------------------------- |
501 | -- Register_Backend_Inlined_Subprogram -- | |
502 | ----------------------------------------- | |
503 | ||
504 | procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is | |
505 | begin | |
21c51f53 | 506 | Append_New_Elmt (Subp, To => Backend_Inlined_Subps); |
6c26bac2 AC |
507 | end Register_Backend_Inlined_Subprogram; |
508 | ||
509 | --------------------------------------------- | |
510 | -- Register_Backend_Not_Inlined_Subprogram -- | |
511 | --------------------------------------------- | |
512 | ||
513 | procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is | |
514 | begin | |
21c51f53 | 515 | Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps); |
6c26bac2 AC |
516 | end Register_Backend_Not_Inlined_Subprogram; |
517 | ||
fbf5a39b AC |
518 | -- Start of processing for Add_Inlined_Subprogram |
519 | ||
38cbfe40 | 520 | begin |
9466892f AC |
521 | -- If the subprogram is to be inlined, and if its unit is known to be |
522 | -- inlined or is an instance whose body will be analyzed anyway or the | |
d8d7e809 AC |
523 | -- subprogram was generated as a body by the compiler (for example an |
524 | -- initialization procedure) or its declaration was provided along with | |
525 | -- the body (for example an expression function), and if it is declared | |
9466892f AC |
526 | -- at the library level not in the main unit, and if it can be inlined |
527 | -- by the back-end, then insert it in the list of inlined subprograms. | |
528 | ||
529 | if Is_Inlined (E) | |
530 | and then (Is_Inlined (Pack) | |
3f80a182 | 531 | or else Is_Generic_Instance (Pack) |
d8d7e809 AC |
532 | or else Nkind (Decl) = N_Subprogram_Body |
533 | or else Present (Corresponding_Body (Decl))) | |
053cf994 | 534 | and then not In_Main_Unit_Or_Subunit (E) |
38cbfe40 RK |
535 | and then not Is_Nested (E) |
536 | and then not Has_Initialized_Type (E) | |
537 | then | |
71ff3d18 | 538 | Register_Backend_Inlined_Subprogram (E); |
fbf5a39b | 539 | |
71ff3d18 AC |
540 | if No (Last_Inlined) then |
541 | Set_First_Inlined_Subprogram (Cunit (Main_Unit), E); | |
38cbfe40 | 542 | else |
71ff3d18 | 543 | Set_Next_Inlined_Subprogram (Last_Inlined, E); |
fbf5a39b | 544 | end if; |
71ff3d18 AC |
545 | |
546 | Last_Inlined := E; | |
3c756b76 | 547 | |
6c26bac2 AC |
548 | else |
549 | Register_Backend_Not_Inlined_Subprogram (E); | |
38cbfe40 | 550 | end if; |
38cbfe40 RK |
551 | end Add_Inlined_Subprogram; |
552 | ||
553 | ------------------------ | |
554 | -- Add_Scope_To_Clean -- | |
555 | ------------------------ | |
556 | ||
557 | procedure Add_Scope_To_Clean (Inst : Entity_Id) is | |
fbf5a39b | 558 | Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst); |
38cbfe40 | 559 | Elmt : Elmt_Id; |
38cbfe40 RK |
560 | |
561 | begin | |
562 | -- If the instance appears in a library-level package declaration, | |
563 | -- all finalization is global, and nothing needs doing here. | |
564 | ||
565 | if Scop = Standard_Standard then | |
566 | return; | |
567 | end if; | |
568 | ||
ddf67a1d AC |
569 | -- If the instance is within a generic unit, no finalization code |
570 | -- can be generated. Note that at this point all bodies have been | |
571 | -- analyzed, and the scope stack itself is not present, and the flag | |
572 | -- Inside_A_Generic is not set. | |
0fb2ea01 AC |
573 | |
574 | declare | |
575 | S : Entity_Id; | |
5132708f | 576 | |
0fb2ea01 AC |
577 | begin |
578 | S := Scope (Inst); | |
579 | while Present (S) and then S /= Standard_Standard loop | |
ddf67a1d | 580 | if Is_Generic_Unit (S) then |
0fb2ea01 AC |
581 | return; |
582 | end if; | |
583 | ||
584 | S := Scope (S); | |
585 | end loop; | |
586 | end; | |
587 | ||
38cbfe40 | 588 | Elmt := First_Elmt (To_Clean); |
38cbfe40 | 589 | while Present (Elmt) loop |
38cbfe40 RK |
590 | if Node (Elmt) = Scop then |
591 | return; | |
592 | end if; | |
593 | ||
594 | Elmt := Next_Elmt (Elmt); | |
595 | end loop; | |
596 | ||
597 | Append_Elmt (Scop, To_Clean); | |
598 | end Add_Scope_To_Clean; | |
599 | ||
600 | -------------- | |
601 | -- Add_Subp -- | |
602 | -------------- | |
603 | ||
604 | function Add_Subp (E : Entity_Id) return Subp_Index is | |
605 | Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers; | |
606 | J : Subp_Index; | |
607 | ||
608 | procedure New_Entry; | |
9de61fcb | 609 | -- Initialize entry in Inlined table |
38cbfe40 RK |
610 | |
611 | procedure New_Entry is | |
612 | begin | |
613 | Inlined.Increment_Last; | |
614 | Inlined.Table (Inlined.Last).Name := E; | |
8a49a499 | 615 | Inlined.Table (Inlined.Last).Next := No_Subp; |
38cbfe40 | 616 | Inlined.Table (Inlined.Last).First_Succ := No_Succ; |
38cbfe40 | 617 | Inlined.Table (Inlined.Last).Main_Call := False; |
8a49a499 | 618 | Inlined.Table (Inlined.Last).Processed := False; |
38cbfe40 RK |
619 | end New_Entry; |
620 | ||
621 | -- Start of processing for Add_Subp | |
622 | ||
623 | begin | |
624 | if Hash_Headers (Index) = No_Subp then | |
625 | New_Entry; | |
626 | Hash_Headers (Index) := Inlined.Last; | |
627 | return Inlined.Last; | |
628 | ||
629 | else | |
630 | J := Hash_Headers (Index); | |
38cbfe40 | 631 | while J /= No_Subp loop |
38cbfe40 RK |
632 | if Inlined.Table (J).Name = E then |
633 | return J; | |
634 | else | |
635 | Index := J; | |
636 | J := Inlined.Table (J).Next; | |
637 | end if; | |
638 | end loop; | |
639 | ||
640 | -- On exit, subprogram was not found. Enter in table. Index is | |
641 | -- the current last entry on the hash chain. | |
642 | ||
643 | New_Entry; | |
644 | Inlined.Table (Index).Next := Inlined.Last; | |
645 | return Inlined.Last; | |
646 | end if; | |
647 | end Add_Subp; | |
648 | ||
649 | ---------------------------- | |
650 | -- Analyze_Inlined_Bodies -- | |
651 | ---------------------------- | |
652 | ||
653 | procedure Analyze_Inlined_Bodies is | |
654 | Comp_Unit : Node_Id; | |
655 | J : Int; | |
656 | Pack : Entity_Id; | |
8a49a499 | 657 | Subp : Subp_Index; |
38cbfe40 RK |
658 | S : Succ_Index; |
659 | ||
8a49a499 AC |
660 | type Pending_Index is new Nat; |
661 | ||
662 | package Pending_Inlined is new Table.Table ( | |
663 | Table_Component_Type => Subp_Index, | |
664 | Table_Index_Type => Pending_Index, | |
665 | Table_Low_Bound => 1, | |
666 | Table_Initial => Alloc.Inlined_Initial, | |
667 | Table_Increment => Alloc.Inlined_Increment, | |
668 | Table_Name => "Pending_Inlined"); | |
669 | -- The workpile used to compute the transitive closure | |
670 | ||
92cbddaa | 671 | function Is_Ancestor_Of_Main |
1237d6ef AC |
672 | (U_Name : Entity_Id; |
673 | Nam : Node_Id) return Boolean; | |
674 | -- Determine whether the unit whose body is loaded is an ancestor of | |
92cbddaa | 675 | -- the main unit, and has a with_clause on it. The body is not |
1237d6ef AC |
676 | -- analyzed yet, so the check is purely lexical: the name of the with |
677 | -- clause is a selected component, and names of ancestors must match. | |
678 | ||
92cbddaa AC |
679 | ------------------------- |
680 | -- Is_Ancestor_Of_Main -- | |
681 | ------------------------- | |
1237d6ef | 682 | |
92cbddaa | 683 | function Is_Ancestor_Of_Main |
1237d6ef AC |
684 | (U_Name : Entity_Id; |
685 | Nam : Node_Id) return Boolean | |
686 | is | |
687 | Pref : Node_Id; | |
688 | ||
689 | begin | |
690 | if Nkind (Nam) /= N_Selected_Component then | |
691 | return False; | |
692 | ||
693 | else | |
92cbddaa AC |
694 | if Chars (Selector_Name (Nam)) /= |
695 | Chars (Cunit_Entity (Main_Unit)) | |
696 | then | |
697 | return False; | |
698 | end if; | |
699 | ||
1237d6ef AC |
700 | Pref := Prefix (Nam); |
701 | if Nkind (Pref) = N_Identifier then | |
702 | ||
703 | -- Par is an ancestor of Par.Child. | |
704 | ||
705 | return Chars (Pref) = Chars (U_Name); | |
706 | ||
707 | elsif Nkind (Pref) = N_Selected_Component | |
708 | and then Chars (Selector_Name (Pref)) = Chars (U_Name) | |
709 | then | |
710 | -- Par.Child is an ancestor of Par.Child.Grand. | |
711 | ||
712 | return True; -- should check that ancestor match | |
713 | ||
714 | else | |
715 | -- A is an ancestor of A.B.C if it is an ancestor of A.B | |
716 | ||
92cbddaa | 717 | return Is_Ancestor_Of_Main (U_Name, Pref); |
1237d6ef AC |
718 | end if; |
719 | end if; | |
92cbddaa | 720 | end Is_Ancestor_Of_Main; |
1237d6ef | 721 | |
84f4072a | 722 | -- Start of processing for Analyze_Inlined_Bodies |
1237d6ef | 723 | |
38cbfe40 | 724 | begin |
07fc65c4 | 725 | if Serious_Errors_Detected = 0 then |
a99ada67 | 726 | Push_Scope (Standard_Standard); |
38cbfe40 RK |
727 | |
728 | J := 0; | |
729 | while J <= Inlined_Bodies.Last | |
07fc65c4 | 730 | and then Serious_Errors_Detected = 0 |
38cbfe40 RK |
731 | loop |
732 | Pack := Inlined_Bodies.Table (J); | |
38cbfe40 RK |
733 | while Present (Pack) |
734 | and then Scope (Pack) /= Standard_Standard | |
735 | and then not Is_Child_Unit (Pack) | |
736 | loop | |
737 | Pack := Scope (Pack); | |
738 | end loop; | |
739 | ||
740 | Comp_Unit := Parent (Pack); | |
38cbfe40 RK |
741 | while Present (Comp_Unit) |
742 | and then Nkind (Comp_Unit) /= N_Compilation_Unit | |
743 | loop | |
744 | Comp_Unit := Parent (Comp_Unit); | |
745 | end loop; | |
746 | ||
84f4072a JM |
747 | -- Load the body, unless it is the main unit, or is an instance |
748 | -- whose body has already been analyzed. | |
07fc65c4 | 749 | |
38cbfe40 RK |
750 | if Present (Comp_Unit) |
751 | and then Comp_Unit /= Cunit (Main_Unit) | |
752 | and then Body_Required (Comp_Unit) | |
07fc65c4 GB |
753 | and then (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration |
754 | or else No (Corresponding_Body (Unit (Comp_Unit)))) | |
38cbfe40 RK |
755 | then |
756 | declare | |
757 | Bname : constant Unit_Name_Type := | |
758 | Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit))); | |
759 | ||
760 | OK : Boolean; | |
761 | ||
762 | begin | |
763 | if not Is_Loaded (Bname) then | |
1237d6ef AC |
764 | Style_Check := False; |
765 | Load_Needed_Body (Comp_Unit, OK, Do_Analyze => False); | |
38cbfe40 RK |
766 | |
767 | if not OK then | |
46ff89f3 AC |
768 | |
769 | -- Warn that a body was not available for inlining | |
770 | -- by the back-end. | |
771 | ||
38cbfe40 RK |
772 | Error_Msg_Unit_1 := Bname; |
773 | Error_Msg_N | |
685bc70f | 774 | ("one or more inlined subprograms accessed in $!??", |
38cbfe40 | 775 | Comp_Unit); |
a99ada67 | 776 | Error_Msg_File_1 := |
38cbfe40 | 777 | Get_File_Name (Bname, Subunit => False); |
685bc70f | 778 | Error_Msg_N ("\but file{ was not found!??", Comp_Unit); |
1237d6ef AC |
779 | |
780 | else | |
781 | -- If the package to be inlined is an ancestor unit of | |
782 | -- the main unit, and it has a semantic dependence on | |
783 | -- it, the inlining cannot take place to prevent an | |
784 | -- elaboration circularity. The desired body is not | |
785 | -- analyzed yet, to prevent the completion of Taft | |
786 | -- amendment types that would lead to elaboration | |
787 | -- circularities in gigi. | |
788 | ||
789 | declare | |
790 | U_Id : constant Entity_Id := | |
791 | Defining_Entity (Unit (Comp_Unit)); | |
792 | Body_Unit : constant Node_Id := | |
793 | Library_Unit (Comp_Unit); | |
794 | Item : Node_Id; | |
795 | ||
796 | begin | |
797 | Item := First (Context_Items (Body_Unit)); | |
798 | while Present (Item) loop | |
799 | if Nkind (Item) = N_With_Clause | |
92cbddaa AC |
800 | and then |
801 | Is_Ancestor_Of_Main (U_Id, Name (Item)) | |
1237d6ef AC |
802 | then |
803 | Set_Is_Inlined (U_Id, False); | |
804 | exit; | |
805 | end if; | |
806 | ||
807 | Next (Item); | |
808 | end loop; | |
809 | ||
810 | -- If no suspicious with_clauses, analyze the body. | |
811 | ||
812 | if Is_Inlined (U_Id) then | |
813 | Semantics (Body_Unit); | |
814 | end if; | |
815 | end; | |
38cbfe40 RK |
816 | end if; |
817 | end if; | |
818 | end; | |
819 | end if; | |
820 | ||
821 | J := J + 1; | |
38cbfe40 | 822 | |
04e9213d AC |
823 | if J > Inlined_Bodies.Last then |
824 | ||
825 | -- The analysis of required bodies may have produced additional | |
826 | -- generic instantiations. To obtain further inlining, we need | |
827 | -- to perform another round of generic body instantiations. | |
828 | ||
829 | Instantiate_Bodies; | |
38cbfe40 | 830 | |
04e9213d AC |
831 | -- Symmetrically, the instantiation of required generic bodies |
832 | -- may have caused additional bodies to be inlined. To obtain | |
833 | -- further inlining, we keep looping over the inlined bodies. | |
834 | end if; | |
835 | end loop; | |
38cbfe40 | 836 | |
1237d6ef AC |
837 | -- The list of inlined subprograms is an overestimate, because it |
838 | -- includes inlined functions called from functions that are compiled | |
839 | -- as part of an inlined package, but are not themselves called. An | |
840 | -- accurate computation of just those subprograms that are needed | |
841 | -- requires that we perform a transitive closure over the call graph, | |
4ef36ac7 | 842 | -- starting from calls in the main compilation unit. |
38cbfe40 RK |
843 | |
844 | for Index in Inlined.First .. Inlined.Last loop | |
8a49a499 | 845 | if not Is_Called (Inlined.Table (Index).Name) then |
5b5b27ad | 846 | |
8a49a499 AC |
847 | -- This means that Add_Inlined_Body added the subprogram to the |
848 | -- table but wasn't able to handle its code unit. Do nothing. | |
849 | ||
053cf994 | 850 | Inlined.Table (Index).Processed := True; |
5b5b27ad | 851 | |
8a49a499 AC |
852 | elsif Inlined.Table (Index).Main_Call then |
853 | Pending_Inlined.Increment_Last; | |
854 | Pending_Inlined.Table (Pending_Inlined.Last) := Index; | |
855 | Inlined.Table (Index).Processed := True; | |
5b5b27ad | 856 | |
8a49a499 | 857 | else |
38cbfe40 | 858 | Set_Is_Called (Inlined.Table (Index).Name, False); |
38cbfe40 RK |
859 | end if; |
860 | end loop; | |
861 | ||
8a49a499 AC |
862 | -- Iterate over the workpile until it is emptied, propagating the |
863 | -- Is_Called flag to the successors of the processed subprogram. | |
38cbfe40 | 864 | |
8a49a499 AC |
865 | while Pending_Inlined.Last >= Pending_Inlined.First loop |
866 | Subp := Pending_Inlined.Table (Pending_Inlined.Last); | |
867 | Pending_Inlined.Decrement_Last; | |
38cbfe40 | 868 | |
8a49a499 AC |
869 | S := Inlined.Table (Subp).First_Succ; |
870 | ||
871 | while S /= No_Succ loop | |
872 | Subp := Successors.Table (S).Subp; | |
8a49a499 AC |
873 | |
874 | if not Inlined.Table (Subp).Processed then | |
053cf994 | 875 | Set_Is_Called (Inlined.Table (Subp).Name); |
8a49a499 AC |
876 | Pending_Inlined.Increment_Last; |
877 | Pending_Inlined.Table (Pending_Inlined.Last) := Subp; | |
878 | Inlined.Table (Subp).Processed := True; | |
879 | end if; | |
880 | ||
881 | S := Successors.Table (S).Next; | |
882 | end loop; | |
38cbfe40 RK |
883 | end loop; |
884 | ||
8a49a499 AC |
885 | -- Finally add the called subprograms to the list of inlined |
886 | -- subprograms for the unit. | |
38cbfe40 RK |
887 | |
888 | for Index in Inlined.First .. Inlined.Last loop | |
4ef36ac7 AC |
889 | if Is_Called (Inlined.Table (Index).Name) then |
890 | Add_Inlined_Subprogram (Inlined.Table (Index).Name); | |
38cbfe40 RK |
891 | end if; |
892 | end loop; | |
893 | ||
894 | Pop_Scope; | |
895 | end if; | |
896 | end Analyze_Inlined_Bodies; | |
897 | ||
540d8610 ES |
898 | -------------------------- |
899 | -- Build_Body_To_Inline -- | |
900 | -------------------------- | |
38cbfe40 | 901 | |
16b10ccc AC |
902 | procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is |
903 | Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id); | |
274d2584 | 904 | Analysis_Status : constant Boolean := Full_Analysis; |
540d8610 ES |
905 | Original_Body : Node_Id; |
906 | Body_To_Analyze : Node_Id; | |
907 | Max_Size : constant := 10; | |
540d8610 ES |
908 | |
909 | function Has_Pending_Instantiation return Boolean; | |
3f80a182 AC |
910 | -- If some enclosing body contains instantiations that appear before |
911 | -- the corresponding generic body, the enclosing body has a freeze node | |
912 | -- so that it can be elaborated after the generic itself. This might | |
540d8610 ES |
913 | -- conflict with subsequent inlinings, so that it is unsafe to try to |
914 | -- inline in such a case. | |
915 | ||
7b2888e6 AC |
916 | function Has_Single_Return_In_GNATprove_Mode return Boolean; |
917 | -- This function is called only in GNATprove mode, and it returns | |
16b10ccc | 918 | -- True if the subprogram has no return statement or a single return |
039538bc AC |
919 | -- statement as last statement. It returns False for subprogram with |
920 | -- a single return as last statement inside one or more blocks, as | |
921 | -- inlining would generate gotos in that case as well (although the | |
922 | -- goto is useless in that case). | |
540d8610 ES |
923 | |
924 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean; | |
925 | -- If the body of the subprogram includes a call that returns an | |
926 | -- unconstrained type, the secondary stack is involved, and it | |
927 | -- is not worth inlining. | |
928 | ||
540d8610 ES |
929 | ------------------------------- |
930 | -- Has_Pending_Instantiation -- | |
931 | ------------------------------- | |
38cbfe40 | 932 | |
540d8610 ES |
933 | function Has_Pending_Instantiation return Boolean is |
934 | S : Entity_Id; | |
38cbfe40 | 935 | |
540d8610 ES |
936 | begin |
937 | S := Current_Scope; | |
938 | while Present (S) loop | |
939 | if Is_Compilation_Unit (S) | |
940 | or else Is_Child_Unit (S) | |
941 | then | |
942 | return False; | |
fbf5a39b | 943 | |
540d8610 ES |
944 | elsif Ekind (S) = E_Package |
945 | and then Has_Forward_Instantiation (S) | |
946 | then | |
947 | return True; | |
948 | end if; | |
fbf5a39b | 949 | |
540d8610 ES |
950 | S := Scope (S); |
951 | end loop; | |
df3e68b1 | 952 | |
540d8610 ES |
953 | return False; |
954 | end Has_Pending_Instantiation; | |
38cbfe40 | 955 | |
7b2888e6 AC |
956 | ----------------------------------------- |
957 | -- Has_Single_Return_In_GNATprove_Mode -- | |
958 | ----------------------------------------- | |
959 | ||
960 | function Has_Single_Return_In_GNATprove_Mode return Boolean is | |
961 | Last_Statement : Node_Id := Empty; | |
962 | ||
963 | function Check_Return (N : Node_Id) return Traverse_Result; | |
964 | -- Returns OK on node N if this is not a return statement different | |
965 | -- from the last statement in the subprogram. | |
966 | ||
967 | ------------------ | |
968 | -- Check_Return -- | |
969 | ------------------ | |
970 | ||
971 | function Check_Return (N : Node_Id) return Traverse_Result is | |
972 | begin | |
973 | if Nkind_In (N, N_Simple_Return_Statement, | |
974 | N_Extended_Return_Statement) | |
975 | then | |
976 | if N = Last_Statement then | |
977 | return OK; | |
978 | else | |
979 | return Abandon; | |
980 | end if; | |
981 | ||
982 | else | |
983 | return OK; | |
984 | end if; | |
985 | end Check_Return; | |
986 | ||
987 | function Check_All_Returns is new Traverse_Func (Check_Return); | |
988 | ||
989 | -- Start of processing for Has_Single_Return_In_GNATprove_Mode | |
990 | ||
991 | begin | |
039538bc | 992 | -- Retrieve the last statement |
7b2888e6 AC |
993 | |
994 | Last_Statement := Last (Statements (Handled_Statement_Sequence (N))); | |
995 | ||
7b2888e6 AC |
996 | -- Check that the last statement is the only possible return |
997 | -- statement in the subprogram. | |
998 | ||
999 | return Check_All_Returns (N) = OK; | |
1000 | end Has_Single_Return_In_GNATprove_Mode; | |
1001 | ||
540d8610 ES |
1002 | -------------------------- |
1003 | -- Uses_Secondary_Stack -- | |
1004 | -------------------------- | |
1005 | ||
1006 | function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is | |
1007 | function Check_Call (N : Node_Id) return Traverse_Result; | |
1008 | -- Look for function calls that return an unconstrained type | |
1009 | ||
1010 | ---------------- | |
1011 | -- Check_Call -- | |
1012 | ---------------- | |
1013 | ||
1014 | function Check_Call (N : Node_Id) return Traverse_Result is | |
1015 | begin | |
1016 | if Nkind (N) = N_Function_Call | |
1017 | and then Is_Entity_Name (Name (N)) | |
1018 | and then Is_Composite_Type (Etype (Entity (Name (N)))) | |
1019 | and then not Is_Constrained (Etype (Entity (Name (N)))) | |
1020 | then | |
1021 | Cannot_Inline | |
1022 | ("cannot inline & (call returns unconstrained type)?", | |
16b10ccc | 1023 | N, Spec_Id); |
540d8610 ES |
1024 | return Abandon; |
1025 | else | |
1026 | return OK; | |
38cbfe40 | 1027 | end if; |
540d8610 ES |
1028 | end Check_Call; |
1029 | ||
1030 | function Check_Calls is new Traverse_Func (Check_Call); | |
1031 | ||
1032 | begin | |
1033 | return Check_Calls (Bod) = Abandon; | |
1034 | end Uses_Secondary_Stack; | |
1035 | ||
1036 | -- Start of processing for Build_Body_To_Inline | |
1037 | ||
1038 | begin | |
1039 | -- Return immediately if done already | |
1040 | ||
1041 | if Nkind (Decl) = N_Subprogram_Declaration | |
1042 | and then Present (Body_To_Inline (Decl)) | |
1043 | then | |
1044 | return; | |
1045 | ||
7b2888e6 AC |
1046 | -- Subprograms that have return statements in the middle of the body are |
1047 | -- inlined with gotos. GNATprove does not currently support gotos, so | |
1048 | -- we prevent such inlining. | |
1049 | ||
1050 | elsif GNATprove_Mode | |
1051 | and then not Has_Single_Return_In_GNATprove_Mode | |
1052 | then | |
16b10ccc | 1053 | Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id); |
7b2888e6 AC |
1054 | return; |
1055 | ||
540d8610 ES |
1056 | -- Functions that return unconstrained composite types require |
1057 | -- secondary stack handling, and cannot currently be inlined, unless | |
1058 | -- all return statements return a local variable that is the first | |
1059 | -- local declaration in the body. | |
1060 | ||
16b10ccc AC |
1061 | elsif Ekind (Spec_Id) = E_Function |
1062 | and then not Is_Scalar_Type (Etype (Spec_Id)) | |
1063 | and then not Is_Access_Type (Etype (Spec_Id)) | |
1064 | and then not Is_Constrained (Etype (Spec_Id)) | |
540d8610 | 1065 | then |
6c26bac2 | 1066 | if not Has_Single_Return (N) then |
540d8610 | 1067 | Cannot_Inline |
16b10ccc | 1068 | ("cannot inline & (unconstrained return type)?", N, Spec_Id); |
540d8610 | 1069 | return; |
38cbfe40 RK |
1070 | end if; |
1071 | ||
540d8610 ES |
1072 | -- Ditto for functions that return controlled types, where controlled |
1073 | -- actions interfere in complex ways with inlining. | |
38cbfe40 | 1074 | |
16b10ccc AC |
1075 | elsif Ekind (Spec_Id) = E_Function |
1076 | and then Needs_Finalization (Etype (Spec_Id)) | |
540d8610 ES |
1077 | then |
1078 | Cannot_Inline | |
16b10ccc | 1079 | ("cannot inline & (controlled return type)?", N, Spec_Id); |
540d8610 ES |
1080 | return; |
1081 | end if; | |
1082 | ||
1083 | if Present (Declarations (N)) | |
16b10ccc | 1084 | and then Has_Excluded_Declaration (Spec_Id, Declarations (N)) |
540d8610 ES |
1085 | then |
1086 | return; | |
1087 | end if; | |
1088 | ||
1089 | if Present (Handled_Statement_Sequence (N)) then | |
1090 | if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then | |
1091 | Cannot_Inline | |
1092 | ("cannot inline& (exception handler)?", | |
1093 | First (Exception_Handlers (Handled_Statement_Sequence (N))), | |
16b10ccc | 1094 | Spec_Id); |
540d8610 | 1095 | return; |
3f80a182 | 1096 | |
16b10ccc AC |
1097 | elsif Has_Excluded_Statement |
1098 | (Spec_Id, Statements (Handled_Statement_Sequence (N))) | |
540d8610 ES |
1099 | then |
1100 | return; | |
1101 | end if; | |
1102 | end if; | |
1103 | ||
2d180af1 YM |
1104 | -- We do not inline a subprogram that is too large, unless it is marked |
1105 | -- Inline_Always or we are in GNATprove mode. This pragma does not | |
1106 | -- suppress the other checks on inlining (forbidden declarations, | |
1107 | -- handlers, etc). | |
540d8610 | 1108 | |
16b10ccc AC |
1109 | if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode) |
1110 | and then List_Length | |
1111 | (Statements (Handled_Statement_Sequence (N))) > Max_Size | |
540d8610 | 1112 | then |
16b10ccc | 1113 | Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id); |
540d8610 ES |
1114 | return; |
1115 | end if; | |
1116 | ||
1117 | if Has_Pending_Instantiation then | |
1118 | Cannot_Inline | |
1119 | ("cannot inline& (forward instance within enclosing body)?", | |
16b10ccc | 1120 | N, Spec_Id); |
540d8610 ES |
1121 | return; |
1122 | end if; | |
1123 | ||
1124 | -- Within an instance, the body to inline must be treated as a nested | |
1125 | -- generic, so that the proper global references are preserved. | |
1126 | ||
1127 | -- Note that we do not do this at the library level, because it is not | |
1128 | -- needed, and furthermore this causes trouble if front end inlining | |
1129 | -- is activated (-gnatN). | |
1130 | ||
1131 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
1132 | Save_Env (Scope (Current_Scope), Scope (Current_Scope)); | |
1133 | Original_Body := Copy_Generic_Node (N, Empty, True); | |
1134 | else | |
1135 | Original_Body := Copy_Separate_Tree (N); | |
1136 | end if; | |
1137 | ||
1138 | -- We need to capture references to the formals in order to substitute | |
1139 | -- the actuals at the point of inlining, i.e. instantiation. To treat | |
3f80a182 AC |
1140 | -- the formals as globals to the body to inline, we nest it within a |
1141 | -- dummy parameterless subprogram, declared within the real one. To | |
1142 | -- avoid generating an internal name (which is never public, and which | |
1143 | -- affects serial numbers of other generated names), we use an internal | |
1144 | -- symbol that cannot conflict with user declarations. | |
38cbfe40 | 1145 | |
540d8610 ES |
1146 | Set_Parameter_Specifications (Specification (Original_Body), No_List); |
1147 | Set_Defining_Unit_Name | |
1148 | (Specification (Original_Body), | |
697b781a | 1149 | Make_Defining_Identifier (Sloc (N), Name_uParent)); |
540d8610 ES |
1150 | Set_Corresponding_Spec (Original_Body, Empty); |
1151 | ||
3de3a1be | 1152 | -- Remove all aspects/pragmas that have no meaning in an inlined body |
6d0b56ad | 1153 | |
697b781a | 1154 | Remove_Aspects_And_Pragmas (Original_Body); |
6d0b56ad | 1155 | |
540d8610 ES |
1156 | Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False); |
1157 | ||
1158 | -- Set return type of function, which is also global and does not need | |
1159 | -- to be resolved. | |
1160 | ||
16b10ccc | 1161 | if Ekind (Spec_Id) = E_Function then |
697b781a AC |
1162 | Set_Result_Definition |
1163 | (Specification (Body_To_Analyze), | |
1164 | New_Occurrence_Of (Etype (Spec_Id), Sloc (N))); | |
540d8610 ES |
1165 | end if; |
1166 | ||
1167 | if No (Declarations (N)) then | |
1168 | Set_Declarations (N, New_List (Body_To_Analyze)); | |
1169 | else | |
1170 | Append (Body_To_Analyze, Declarations (N)); | |
1171 | end if; | |
1172 | ||
697b781a AC |
1173 | -- The body to inline is pre-analyzed. In GNATprove mode we must disable |
1174 | -- full analysis as well so that light expansion does not take place | |
1175 | -- either, and name resolution is unaffected. | |
274d2584 | 1176 | |
540d8610 | 1177 | Expander_Mode_Save_And_Set (False); |
274d2584 | 1178 | Full_Analysis := False; |
540d8610 ES |
1179 | |
1180 | Analyze (Body_To_Analyze); | |
1181 | Push_Scope (Defining_Entity (Body_To_Analyze)); | |
1182 | Save_Global_References (Original_Body); | |
1183 | End_Scope; | |
1184 | Remove (Body_To_Analyze); | |
1185 | ||
1186 | Expander_Mode_Restore; | |
274d2584 | 1187 | Full_Analysis := Analysis_Status; |
540d8610 ES |
1188 | |
1189 | -- Restore environment if previously saved | |
1190 | ||
1191 | if In_Instance and then Scope (Current_Scope) /= Standard_Standard then | |
1192 | Restore_Env; | |
1193 | end if; | |
1194 | ||
43478196 | 1195 | -- If secondary stack is used, there is no point in inlining. We have |
540d8610 ES |
1196 | -- already issued the warning in this case, so nothing to do. |
1197 | ||
1198 | if Uses_Secondary_Stack (Body_To_Analyze) then | |
1199 | return; | |
1200 | end if; | |
1201 | ||
1202 | Set_Body_To_Inline (Decl, Original_Body); | |
16b10ccc AC |
1203 | Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id)); |
1204 | Set_Is_Inlined (Spec_Id); | |
540d8610 ES |
1205 | end Build_Body_To_Inline; |
1206 | ||
3de3a1be YM |
1207 | ------------------------------------------- |
1208 | -- Call_Can_Be_Inlined_In_GNATprove_Mode -- | |
1209 | ------------------------------------------- | |
1210 | ||
1211 | function Call_Can_Be_Inlined_In_GNATprove_Mode | |
1212 | (N : Node_Id; | |
1213 | Subp : Entity_Id) return Boolean | |
1214 | is | |
1215 | F : Entity_Id; | |
1216 | A : Node_Id; | |
1217 | ||
1218 | begin | |
1219 | F := First_Formal (Subp); | |
1220 | A := First_Actual (N); | |
1221 | while Present (F) loop | |
1222 | if Ekind (F) /= E_Out_Parameter | |
1223 | and then not Same_Type (Etype (F), Etype (A)) | |
1224 | and then | |
1225 | (Is_By_Reference_Type (Etype (A)) | |
da9683f4 | 1226 | or else Is_Limited_Type (Etype (A))) |
3de3a1be YM |
1227 | then |
1228 | return False; | |
1229 | end if; | |
1230 | ||
1231 | Next_Formal (F); | |
1232 | Next_Actual (A); | |
1233 | end loop; | |
1234 | ||
1235 | return True; | |
1236 | end Call_Can_Be_Inlined_In_GNATprove_Mode; | |
1237 | ||
2d180af1 YM |
1238 | -------------------------------------- |
1239 | -- Can_Be_Inlined_In_GNATprove_Mode -- | |
1240 | -------------------------------------- | |
1241 | ||
1242 | function Can_Be_Inlined_In_GNATprove_Mode | |
1243 | (Spec_Id : Entity_Id; | |
1244 | Body_Id : Entity_Id) return Boolean | |
1245 | is | |
57d08392 | 1246 | function Has_Formal_With_Discriminant_Dependent_Fields |
d3ef4bd6 | 1247 | (Id : Entity_Id) return Boolean; |
5f6061af | 1248 | -- Returns true if the subprogram has at least one formal parameter of |
57d08392 AC |
1249 | -- an unconstrained record type with per-object constraints on component |
1250 | -- types. | |
d3ef4bd6 | 1251 | |
2d180af1 YM |
1252 | function Has_Some_Contract (Id : Entity_Id) return Boolean; |
1253 | -- Returns True if subprogram Id has any contract (Pre, Post, Global, | |
1254 | -- Depends, etc.) | |
1255 | ||
82701811 AC |
1256 | function Is_Unit_Subprogram (Id : Entity_Id) return Boolean; |
1257 | -- Returns True if subprogram Id defines a compilation unit | |
2e1295ad | 1258 | -- Shouldn't this be in Sem_Aux??? |
82701811 | 1259 | |
1e3689bd AC |
1260 | function In_Package_Visible_Spec (Id : Node_Id) return Boolean; |
1261 | -- Returns True if subprogram Id is defined in the visible part of a | |
1262 | -- package specification. | |
2d180af1 | 1263 | |
57d08392 AC |
1264 | --------------------------------------------------- |
1265 | -- Has_Formal_With_Discriminant_Dependent_Fields -- | |
1266 | --------------------------------------------------- | |
d3ef4bd6 | 1267 | |
57d08392 AC |
1268 | function Has_Formal_With_Discriminant_Dependent_Fields |
1269 | (Id : Entity_Id) return Boolean is | |
d3ef4bd6 | 1270 | |
57d08392 AC |
1271 | function Has_Discriminant_Dependent_Component |
1272 | (Typ : Entity_Id) return Boolean; | |
1273 | -- Determine whether unconstrained record type Typ has at least | |
1274 | -- one component that depends on a discriminant. | |
d3ef4bd6 | 1275 | |
57d08392 AC |
1276 | ------------------------------------------ |
1277 | -- Has_Discriminant_Dependent_Component -- | |
1278 | ------------------------------------------ | |
d3ef4bd6 | 1279 | |
57d08392 AC |
1280 | function Has_Discriminant_Dependent_Component |
1281 | (Typ : Entity_Id) return Boolean | |
1282 | is | |
1283 | Comp : Entity_Id; | |
d3ef4bd6 | 1284 | |
57d08392 AC |
1285 | begin |
1286 | -- Inspect all components of the record type looking for one | |
1287 | -- that depends on a discriminant. | |
d3ef4bd6 | 1288 | |
57d08392 AC |
1289 | Comp := First_Component (Typ); |
1290 | while Present (Comp) loop | |
1291 | if Has_Discriminant_Dependent_Constraint (Comp) then | |
1292 | return True; | |
1293 | end if; | |
d3ef4bd6 | 1294 | |
57d08392 AC |
1295 | Next_Component (Comp); |
1296 | end loop; | |
1297 | ||
1298 | return False; | |
1299 | end Has_Discriminant_Dependent_Component; | |
d3ef4bd6 | 1300 | |
57d08392 | 1301 | -- Local variables |
d3ef4bd6 | 1302 | |
57d08392 AC |
1303 | Subp_Id : constant Entity_Id := Ultimate_Alias (Id); |
1304 | Formal : Entity_Id; | |
1305 | Formal_Typ : Entity_Id; | |
d3ef4bd6 | 1306 | |
3de3a1be YM |
1307 | -- Start of processing for |
1308 | -- Has_Formal_With_Discriminant_Dependent_Fields | |
d3ef4bd6 | 1309 | |
57d08392 AC |
1310 | begin |
1311 | -- Inspect all parameters of the subprogram looking for a formal | |
1312 | -- of an unconstrained record type with at least one discriminant | |
1313 | -- dependent component. | |
1314 | ||
1315 | Formal := First_Formal (Subp_Id); | |
1316 | while Present (Formal) loop | |
1317 | Formal_Typ := Etype (Formal); | |
d3ef4bd6 | 1318 | |
57d08392 AC |
1319 | if Is_Record_Type (Formal_Typ) |
1320 | and then not Is_Constrained (Formal_Typ) | |
1321 | and then Has_Discriminant_Dependent_Component (Formal_Typ) | |
1322 | then | |
1323 | return True; | |
d3ef4bd6 | 1324 | end if; |
57d08392 AC |
1325 | |
1326 | Next_Formal (Formal); | |
1327 | end loop; | |
d3ef4bd6 AC |
1328 | |
1329 | return False; | |
57d08392 | 1330 | end Has_Formal_With_Discriminant_Dependent_Fields; |
d3ef4bd6 | 1331 | |
2d180af1 YM |
1332 | ----------------------- |
1333 | -- Has_Some_Contract -- | |
1334 | ----------------------- | |
1335 | ||
1336 | function Has_Some_Contract (Id : Entity_Id) return Boolean is | |
a98480dd AC |
1337 | Items : Node_Id; |
1338 | ||
2d180af1 | 1339 | begin |
a98480dd AC |
1340 | -- A call to an expression function may precede the actual body which |
1341 | -- is inserted at the end of the enclosing declarations. Ensure that | |
c05ba1f1 | 1342 | -- the related entity is decorated before inspecting the contract. |
a98480dd | 1343 | |
c05ba1f1 | 1344 | if Is_Subprogram_Or_Generic_Subprogram (Id) then |
a98480dd AC |
1345 | Items := Contract (Id); |
1346 | ||
1347 | return Present (Items) | |
1348 | and then (Present (Pre_Post_Conditions (Items)) or else | |
1349 | Present (Contract_Test_Cases (Items)) or else | |
1350 | Present (Classifications (Items))); | |
1351 | end if; | |
1352 | ||
1353 | return False; | |
2d180af1 YM |
1354 | end Has_Some_Contract; |
1355 | ||
1e3689bd AC |
1356 | ----------------------------- |
1357 | -- In_Package_Visible_Spec -- | |
1358 | ----------------------------- | |
2d180af1 | 1359 | |
1e3689bd AC |
1360 | function In_Package_Visible_Spec (Id : Node_Id) return Boolean is |
1361 | Decl : Node_Id := Parent (Parent (Id)); | |
1362 | P : Node_Id; | |
fc27e20e | 1363 | |
2d180af1 | 1364 | begin |
1e3689bd AC |
1365 | if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then |
1366 | Decl := Parent (Decl); | |
1367 | end if; | |
2d180af1 | 1368 | |
1e3689bd | 1369 | P := Parent (Decl); |
2d180af1 | 1370 | |
1e3689bd AC |
1371 | return Nkind (P) = N_Package_Specification |
1372 | and then List_Containing (Decl) = Visible_Declarations (P); | |
1373 | end In_Package_Visible_Spec; | |
2d180af1 | 1374 | |
82701811 AC |
1375 | ------------------------ |
1376 | -- Is_Unit_Subprogram -- | |
1377 | ------------------------ | |
1378 | ||
1379 | function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is | |
1380 | Decl : Node_Id := Parent (Parent (Id)); | |
1381 | begin | |
1382 | if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then | |
1383 | Decl := Parent (Decl); | |
1384 | end if; | |
1385 | ||
1386 | return Nkind (Parent (Decl)) = N_Compilation_Unit; | |
1387 | end Is_Unit_Subprogram; | |
1388 | ||
fc27e20e RD |
1389 | -- Local declarations |
1390 | ||
da9683f4 AC |
1391 | Id : Entity_Id; |
1392 | -- Procedure or function entity for the subprogram | |
2d180af1 | 1393 | |
704228bd | 1394 | -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode |
2d180af1 YM |
1395 | |
1396 | begin | |
4bd4bb7f AC |
1397 | pragma Assert (Present (Spec_Id) or else Present (Body_Id)); |
1398 | ||
2d180af1 YM |
1399 | if Present (Spec_Id) then |
1400 | Id := Spec_Id; | |
1401 | else | |
1402 | Id := Body_Id; | |
1403 | end if; | |
1404 | ||
52c1498c YM |
1405 | -- Only local subprograms without contracts are inlined in GNATprove |
1406 | -- mode, as these are the subprograms which a user is not interested in | |
1407 | -- analyzing in isolation, but rather in the context of their call. This | |
1408 | -- is a convenient convention, that could be changed for an explicit | |
1409 | -- pragma/aspect one day. | |
1410 | ||
1411 | -- In a number of special cases, inlining is not desirable or not | |
1412 | -- possible, see below. | |
1399d355 | 1413 | |
2d180af1 YM |
1414 | -- Do not inline unit-level subprograms |
1415 | ||
82701811 | 1416 | if Is_Unit_Subprogram (Id) then |
2d180af1 YM |
1417 | return False; |
1418 | ||
1e3689bd | 1419 | -- Do not inline subprograms declared in the visible part of a package |
2d180af1 | 1420 | |
1e3689bd | 1421 | elsif In_Package_Visible_Spec (Id) then |
2d180af1 YM |
1422 | return False; |
1423 | ||
7188885e AC |
1424 | -- Do not inline subprograms marked No_Return, possibly used for |
1425 | -- signaling errors, which GNATprove handles specially. | |
1426 | ||
1427 | elsif No_Return (Id) then | |
1428 | return False; | |
1429 | ||
2d180af1 YM |
1430 | -- Do not inline subprograms that have a contract on the spec or the |
1431 | -- body. Use the contract(s) instead in GNATprove. | |
1432 | ||
1433 | elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id)) | |
4bd4bb7f AC |
1434 | or else |
1435 | (Present (Body_Id) and then Has_Some_Contract (Body_Id)) | |
2d180af1 YM |
1436 | then |
1437 | return False; | |
1438 | ||
52c1498c YM |
1439 | -- Do not inline expression functions, which are directly inlined at the |
1440 | -- prover level. | |
2d180af1 YM |
1441 | |
1442 | elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id)) | |
4bd4bb7f AC |
1443 | or else |
1444 | (Present (Body_Id) and then Is_Expression_Function (Body_Id)) | |
2d180af1 YM |
1445 | then |
1446 | return False; | |
1447 | ||
52c1498c YM |
1448 | -- Do not inline generic subprogram instances. The visibility rules of |
1449 | -- generic instances plays badly with inlining. | |
1399d355 | 1450 | |
ac072cb2 AC |
1451 | elsif Is_Generic_Instance (Spec_Id) then |
1452 | return False; | |
1453 | ||
2178830b AC |
1454 | -- Only inline subprograms whose spec is marked SPARK_Mode On. For |
1455 | -- the subprogram body, a similar check is performed after the body | |
1456 | -- is analyzed, as this is where a pragma SPARK_Mode might be inserted. | |
1457 | ||
1458 | elsif Present (Spec_Id) | |
eb1ee757 AC |
1459 | and then |
1460 | (No (SPARK_Pragma (Spec_Id)) | |
933aa0ac AC |
1461 | or else |
1462 | Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On) | |
2d180af1 YM |
1463 | then |
1464 | return False; | |
1465 | ||
1466 | -- Subprograms in generic instances are currently not inlined, to avoid | |
1467 | -- problems with inlining of standard library subprograms. | |
1468 | ||
1469 | elsif Instantiation_Location (Sloc (Id)) /= No_Location then | |
1470 | return False; | |
1471 | ||
d3ef4bd6 | 1472 | -- Do not inline predicate functions (treated specially by GNATprove) |
2178830b AC |
1473 | |
1474 | elsif Is_Predicate_Function (Id) then | |
1475 | return False; | |
1476 | ||
d3ef4bd6 AC |
1477 | -- Do not inline subprograms with a parameter of an unconstrained |
1478 | -- record type if it has discrimiant dependent fields. Indeed, with | |
1479 | -- such parameters, the frontend cannot always ensure type compliance | |
1480 | -- in record component accesses (in particular with records containing | |
1481 | -- packed arrays). | |
1482 | ||
57d08392 | 1483 | elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then |
d3ef4bd6 AC |
1484 | return False; |
1485 | ||
2d180af1 YM |
1486 | -- Otherwise, this is a subprogram declared inside the private part of a |
1487 | -- package, or inside a package body, or locally in a subprogram, and it | |
1488 | -- does not have any contract. Inline it. | |
1489 | ||
1490 | else | |
1491 | return True; | |
1492 | end if; | |
1493 | end Can_Be_Inlined_In_GNATprove_Mode; | |
1494 | ||
da9683f4 AC |
1495 | ------------------- |
1496 | -- Cannot_Inline -- | |
1497 | ------------------- | |
1498 | ||
1499 | procedure Cannot_Inline | |
1500 | (Msg : String; | |
1501 | N : Node_Id; | |
1502 | Subp : Entity_Id; | |
1503 | Is_Serious : Boolean := False) | |
1504 | is | |
1505 | begin | |
1506 | -- In GNATprove mode, inlining is the technical means by which the | |
1507 | -- higher-level goal of contextual analysis is reached, so issue | |
1508 | -- messages about failure to apply contextual analysis to a | |
1509 | -- subprogram, rather than failure to inline it. | |
1510 | ||
1511 | if GNATprove_Mode | |
1512 | and then Msg (Msg'First .. Msg'First + 12) = "cannot inline" | |
1513 | then | |
1514 | declare | |
1515 | Len1 : constant Positive := | |
1516 | String (String'("cannot inline"))'Length; | |
1517 | Len2 : constant Positive := | |
1518 | String (String'("info: no contextual analysis of"))'Length; | |
1519 | ||
1520 | New_Msg : String (1 .. Msg'Length + Len2 - Len1); | |
1521 | ||
1522 | begin | |
1523 | New_Msg (1 .. Len2) := "info: no contextual analysis of"; | |
1524 | New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) := | |
1525 | Msg (Msg'First + Len1 .. Msg'Last); | |
1526 | Cannot_Inline (New_Msg, N, Subp, Is_Serious); | |
1527 | return; | |
1528 | end; | |
1529 | end if; | |
1530 | ||
1531 | pragma Assert (Msg (Msg'Last) = '?'); | |
1532 | ||
1533 | -- Legacy front end inlining model | |
1534 | ||
1535 | if not Back_End_Inlining then | |
1536 | ||
1537 | -- Do not emit warning if this is a predefined unit which is not | |
1538 | -- the main unit. With validity checks enabled, some predefined | |
1539 | -- subprograms may contain nested subprograms and become ineligible | |
1540 | -- for inlining. | |
1541 | ||
1542 | if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp))) | |
1543 | and then not In_Extended_Main_Source_Unit (Subp) | |
1544 | then | |
1545 | null; | |
1546 | ||
1547 | -- In GNATprove mode, issue a warning, and indicate that the | |
1548 | -- subprogram is not always inlined by setting flag Is_Inlined_Always | |
1549 | -- to False. | |
1550 | ||
1551 | elsif GNATprove_Mode then | |
1552 | Set_Is_Inlined_Always (Subp, False); | |
1553 | Error_Msg_NE (Msg & "p?", N, Subp); | |
1554 | ||
1555 | elsif Has_Pragma_Inline_Always (Subp) then | |
1556 | ||
1557 | -- Remove last character (question mark) to make this into an | |
1558 | -- error, because the Inline_Always pragma cannot be obeyed. | |
1559 | ||
1560 | Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp); | |
1561 | ||
1562 | elsif Ineffective_Inline_Warnings then | |
1563 | Error_Msg_NE (Msg & "p?", N, Subp); | |
1564 | end if; | |
1565 | ||
1566 | -- New semantics relying on back end inlining | |
1567 | ||
1568 | elsif Is_Serious then | |
1569 | ||
1570 | -- Remove last character (question mark) to make this into an error. | |
1571 | ||
1572 | Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp); | |
1573 | ||
1574 | -- In GNATprove mode, issue a warning, and indicate that the subprogram | |
1575 | -- is not always inlined by setting flag Is_Inlined_Always to False. | |
1576 | ||
1577 | elsif GNATprove_Mode then | |
1578 | Set_Is_Inlined_Always (Subp, False); | |
1579 | Error_Msg_NE (Msg & "p?", N, Subp); | |
1580 | ||
1581 | else | |
1582 | ||
1583 | -- Do not emit warning if this is a predefined unit which is not | |
1584 | -- the main unit. This behavior is currently provided for backward | |
1585 | -- compatibility but it will be removed when we enforce the | |
1586 | -- strictness of the new rules. | |
1587 | ||
1588 | if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp))) | |
1589 | and then not In_Extended_Main_Source_Unit (Subp) | |
1590 | then | |
1591 | null; | |
1592 | ||
1593 | elsif Has_Pragma_Inline_Always (Subp) then | |
1594 | ||
1595 | -- Emit a warning if this is a call to a runtime subprogram | |
1596 | -- which is located inside a generic. Previously this call | |
1597 | -- was silently skipped. | |
1598 | ||
1599 | if Is_Generic_Instance (Subp) then | |
1600 | declare | |
1601 | Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp)); | |
1602 | begin | |
1603 | if Is_Predefined_File_Name | |
1604 | (Unit_File_Name (Get_Source_Unit (Gen_P))) | |
1605 | then | |
1606 | Set_Is_Inlined (Subp, False); | |
1607 | Error_Msg_NE (Msg & "p?", N, Subp); | |
1608 | return; | |
1609 | end if; | |
1610 | end; | |
1611 | end if; | |
1612 | ||
1613 | -- Remove last character (question mark) to make this into an | |
1614 | -- error, because the Inline_Always pragma cannot be obeyed. | |
1615 | ||
1616 | Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp); | |
1617 | ||
1618 | else | |
1619 | Set_Is_Inlined (Subp, False); | |
1620 | ||
1621 | if Ineffective_Inline_Warnings then | |
1622 | Error_Msg_NE (Msg & "p?", N, Subp); | |
1623 | end if; | |
1624 | end if; | |
1625 | end if; | |
1626 | end Cannot_Inline; | |
1627 | ||
16b10ccc AC |
1628 | -------------------------------------------- |
1629 | -- Check_And_Split_Unconstrained_Function -- | |
1630 | -------------------------------------------- | |
540d8610 | 1631 | |
16b10ccc | 1632 | procedure Check_And_Split_Unconstrained_Function |
540d8610 ES |
1633 | (N : Node_Id; |
1634 | Spec_Id : Entity_Id; | |
1635 | Body_Id : Entity_Id) | |
1636 | is | |
1637 | procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id); | |
1638 | -- Use generic machinery to build an unexpanded body for the subprogram. | |
1639 | -- This body is subsequently used for inline expansions at call sites. | |
1640 | ||
1641 | function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean; | |
1642 | -- Return true if we generate code for the function body N, the function | |
1643 | -- body N has no local declarations and its unique statement is a single | |
1644 | -- extended return statement with a handled statements sequence. | |
1645 | ||
16b10ccc | 1646 | procedure Generate_Subprogram_Body |
540d8610 ES |
1647 | (N : Node_Id; |
1648 | Body_To_Inline : out Node_Id); | |
1649 | -- Generate a parameterless duplicate of subprogram body N. Occurrences | |
1650 | -- of pragmas referencing the formals are removed since they have no | |
1651 | -- meaning when the body is inlined and the formals are rewritten (the | |
1652 | -- analysis of the non-inlined body will handle these pragmas properly). | |
1653 | -- A new internal name is associated with Body_To_Inline. | |
1654 | ||
1655 | procedure Split_Unconstrained_Function | |
1656 | (N : Node_Id; | |
1657 | Spec_Id : Entity_Id); | |
1658 | -- N is an inlined function body that returns an unconstrained type and | |
1659 | -- has a single extended return statement. Split N in two subprograms: | |
1660 | -- a procedure P' and a function F'. The formals of P' duplicate the | |
1661 | -- formals of N plus an extra formal which is used return a value; | |
1662 | -- its body is composed by the declarations and list of statements | |
1663 | -- of the extended return statement of N. | |
1664 | ||
1665 | -------------------------- | |
1666 | -- Build_Body_To_Inline -- | |
1667 | -------------------------- | |
1668 | ||
1669 | procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is | |
1670 | Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id); | |
1671 | Original_Body : Node_Id; | |
1672 | Body_To_Analyze : Node_Id; | |
1673 | ||
1674 | begin | |
1675 | pragma Assert (Current_Scope = Spec_Id); | |
1676 | ||
1677 | -- Within an instance, the body to inline must be treated as a nested | |
1678 | -- generic, so that the proper global references are preserved. We | |
1679 | -- do not do this at the library level, because it is not needed, and | |
1680 | -- furthermore this causes trouble if front end inlining is activated | |
1681 | -- (-gnatN). | |
1682 | ||
1683 | if In_Instance | |
1684 | and then Scope (Current_Scope) /= Standard_Standard | |
1685 | then | |
1686 | Save_Env (Scope (Current_Scope), Scope (Current_Scope)); | |
1687 | end if; | |
1688 | ||
1689 | -- We need to capture references to the formals in order | |
1690 | -- to substitute the actuals at the point of inlining, i.e. | |
1691 | -- instantiation. To treat the formals as globals to the body to | |
1692 | -- inline, we nest it within a dummy parameterless subprogram, | |
1693 | -- declared within the real one. | |
1694 | ||
16b10ccc | 1695 | Generate_Subprogram_Body (N, Original_Body); |
540d8610 ES |
1696 | Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False); |
1697 | ||
1698 | -- Set return type of function, which is also global and does not | |
1699 | -- need to be resolved. | |
1700 | ||
1701 | if Ekind (Spec_Id) = E_Function then | |
1702 | Set_Result_Definition (Specification (Body_To_Analyze), | |
1703 | New_Occurrence_Of (Etype (Spec_Id), Sloc (N))); | |
1704 | end if; | |
1705 | ||
1706 | if No (Declarations (N)) then | |
1707 | Set_Declarations (N, New_List (Body_To_Analyze)); | |
1708 | else | |
1709 | Append_To (Declarations (N), Body_To_Analyze); | |
1710 | end if; | |
1711 | ||
1712 | Preanalyze (Body_To_Analyze); | |
1713 | ||
1714 | Push_Scope (Defining_Entity (Body_To_Analyze)); | |
1715 | Save_Global_References (Original_Body); | |
1716 | End_Scope; | |
1717 | Remove (Body_To_Analyze); | |
1718 | ||
1719 | -- Restore environment if previously saved | |
1720 | ||
1721 | if In_Instance | |
1722 | and then Scope (Current_Scope) /= Standard_Standard | |
1723 | then | |
1724 | Restore_Env; | |
1725 | end if; | |
1726 | ||
1727 | pragma Assert (No (Body_To_Inline (Decl))); | |
1728 | Set_Body_To_Inline (Decl, Original_Body); | |
1729 | Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id)); | |
1730 | end Build_Body_To_Inline; | |
1731 | ||
540d8610 ES |
1732 | -------------------------------------- |
1733 | -- Can_Split_Unconstrained_Function -- | |
1734 | -------------------------------------- | |
1735 | ||
1736 | function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean | |
1737 | is | |
1738 | Ret_Node : constant Node_Id := | |
1739 | First (Statements (Handled_Statement_Sequence (N))); | |
1740 | D : Node_Id; | |
1741 | ||
1742 | begin | |
1743 | -- No user defined declarations allowed in the function except inside | |
1744 | -- the unique return statement; implicit labels are the only allowed | |
1745 | -- declarations. | |
1746 | ||
1747 | if not Is_Empty_List (Declarations (N)) then | |
1748 | D := First (Declarations (N)); | |
1749 | while Present (D) loop | |
1750 | if Nkind (D) /= N_Implicit_Label_Declaration then | |
1751 | return False; | |
1752 | end if; | |
1753 | ||
1754 | Next (D); | |
1755 | end loop; | |
1756 | end if; | |
1757 | ||
1758 | -- We only split the inlined function when we are generating the code | |
1759 | -- of its body; otherwise we leave duplicated split subprograms in | |
1760 | -- the tree which (if referenced) generate wrong references at link | |
1761 | -- time. | |
1762 | ||
1763 | return In_Extended_Main_Code_Unit (N) | |
1764 | and then Present (Ret_Node) | |
1765 | and then Nkind (Ret_Node) = N_Extended_Return_Statement | |
1766 | and then No (Next (Ret_Node)) | |
1767 | and then Present (Handled_Statement_Sequence (Ret_Node)); | |
1768 | end Can_Split_Unconstrained_Function; | |
1769 | ||
1770 | ----------------------------- | |
1771 | -- Generate_Body_To_Inline -- | |
1772 | ----------------------------- | |
1773 | ||
16b10ccc | 1774 | procedure Generate_Subprogram_Body |
540d8610 ES |
1775 | (N : Node_Id; |
1776 | Body_To_Inline : out Node_Id) | |
1777 | is | |
540d8610 ES |
1778 | begin |
1779 | -- Within an instance, the body to inline must be treated as a nested | |
1780 | -- generic, so that the proper global references are preserved. | |
1781 | ||
1782 | -- Note that we do not do this at the library level, because it | |
1783 | -- is not needed, and furthermore this causes trouble if front | |
1784 | -- end inlining is activated (-gnatN). | |
1785 | ||
1786 | if In_Instance | |
1787 | and then Scope (Current_Scope) /= Standard_Standard | |
1788 | then | |
1789 | Body_To_Inline := Copy_Generic_Node (N, Empty, True); | |
1790 | else | |
1791 | Body_To_Inline := Copy_Separate_Tree (N); | |
1792 | end if; | |
1793 | ||
eefd2467 | 1794 | -- Remove all aspects/pragmas that have no meaning in an inlined body |
540d8610 | 1795 | |
697b781a | 1796 | Remove_Aspects_And_Pragmas (Body_To_Inline); |
540d8610 ES |
1797 | |
1798 | -- We need to capture references to the formals in order | |
1799 | -- to substitute the actuals at the point of inlining, i.e. | |
1800 | -- instantiation. To treat the formals as globals to the body to | |
1801 | -- inline, we nest it within a dummy parameterless subprogram, | |
1802 | -- declared within the real one. | |
1803 | ||
1804 | Set_Parameter_Specifications | |
1805 | (Specification (Body_To_Inline), No_List); | |
1806 | ||
1807 | -- A new internal name is associated with Body_To_Inline to avoid | |
1808 | -- conflicts when the non-inlined body N is analyzed. | |
1809 | ||
1810 | Set_Defining_Unit_Name (Specification (Body_To_Inline), | |
1811 | Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P'))); | |
1812 | Set_Corresponding_Spec (Body_To_Inline, Empty); | |
16b10ccc | 1813 | end Generate_Subprogram_Body; |
540d8610 ES |
1814 | |
1815 | ---------------------------------- | |
1816 | -- Split_Unconstrained_Function -- | |
1817 | ---------------------------------- | |
1818 | ||
1819 | procedure Split_Unconstrained_Function | |
1820 | (N : Node_Id; | |
1821 | Spec_Id : Entity_Id) | |
1822 | is | |
1823 | Loc : constant Source_Ptr := Sloc (N); | |
1824 | Ret_Node : constant Node_Id := | |
1825 | First (Statements (Handled_Statement_Sequence (N))); | |
1826 | Ret_Obj : constant Node_Id := | |
1827 | First (Return_Object_Declarations (Ret_Node)); | |
1828 | ||
1829 | procedure Build_Procedure | |
1830 | (Proc_Id : out Entity_Id; | |
1831 | Decl_List : out List_Id); | |
1832 | -- Build a procedure containing the statements found in the extended | |
1833 | -- return statement of the unconstrained function body N. | |
1834 | ||
3f80a182 AC |
1835 | --------------------- |
1836 | -- Build_Procedure -- | |
1837 | --------------------- | |
1838 | ||
540d8610 ES |
1839 | procedure Build_Procedure |
1840 | (Proc_Id : out Entity_Id; | |
1841 | Decl_List : out List_Id) | |
1842 | is | |
3f80a182 AC |
1843 | Formal : Entity_Id; |
1844 | Formal_List : constant List_Id := New_List; | |
1845 | Proc_Spec : Node_Id; | |
1846 | Proc_Body : Node_Id; | |
1847 | Subp_Name : constant Name_Id := New_Internal_Name ('F'); | |
540d8610 | 1848 | Body_Decl_List : List_Id := No_List; |
3f80a182 | 1849 | Param_Type : Node_Id; |
540d8610 ES |
1850 | |
1851 | begin | |
1852 | if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then | |
3f80a182 AC |
1853 | Param_Type := |
1854 | New_Copy (Object_Definition (Ret_Obj)); | |
540d8610 ES |
1855 | else |
1856 | Param_Type := | |
1857 | New_Copy (Subtype_Mark (Object_Definition (Ret_Obj))); | |
1858 | end if; | |
1859 | ||
1860 | Append_To (Formal_List, | |
1861 | Make_Parameter_Specification (Loc, | |
3f80a182 | 1862 | Defining_Identifier => |
540d8610 ES |
1863 | Make_Defining_Identifier (Loc, |
1864 | Chars => Chars (Defining_Identifier (Ret_Obj))), | |
3f80a182 AC |
1865 | In_Present => False, |
1866 | Out_Present => True, | |
540d8610 | 1867 | Null_Exclusion_Present => False, |
3f80a182 | 1868 | Parameter_Type => Param_Type)); |
540d8610 ES |
1869 | |
1870 | Formal := First_Formal (Spec_Id); | |
596f7139 AC |
1871 | |
1872 | -- Note that we copy the parameter type rather than creating | |
1873 | -- a reference to it, because it may be a class-wide entity | |
1874 | -- that will not be retrieved by name. | |
1875 | ||
540d8610 ES |
1876 | while Present (Formal) loop |
1877 | Append_To (Formal_List, | |
1878 | Make_Parameter_Specification (Loc, | |
3f80a182 | 1879 | Defining_Identifier => |
540d8610 ES |
1880 | Make_Defining_Identifier (Sloc (Formal), |
1881 | Chars => Chars (Formal)), | |
3f80a182 AC |
1882 | In_Present => In_Present (Parent (Formal)), |
1883 | Out_Present => Out_Present (Parent (Formal)), | |
540d8610 ES |
1884 | Null_Exclusion_Present => |
1885 | Null_Exclusion_Present (Parent (Formal)), | |
3f80a182 | 1886 | Parameter_Type => |
596f7139 | 1887 | New_Copy_Tree (Parameter_Type (Parent (Formal))), |
3f80a182 | 1888 | Expression => |
540d8610 ES |
1889 | Copy_Separate_Tree (Expression (Parent (Formal))))); |
1890 | ||
1891 | Next_Formal (Formal); | |
1892 | end loop; | |
1893 | ||
3f80a182 | 1894 | Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name); |
540d8610 ES |
1895 | |
1896 | Proc_Spec := | |
1897 | Make_Procedure_Specification (Loc, | |
3f80a182 | 1898 | Defining_Unit_Name => Proc_Id, |
540d8610 ES |
1899 | Parameter_Specifications => Formal_List); |
1900 | ||
1901 | Decl_List := New_List; | |
1902 | ||
1903 | Append_To (Decl_List, | |
1904 | Make_Subprogram_Declaration (Loc, Proc_Spec)); | |
1905 | ||
1906 | -- Can_Convert_Unconstrained_Function checked that the function | |
1907 | -- has no local declarations except implicit label declarations. | |
1908 | -- Copy these declarations to the built procedure. | |
1909 | ||
1910 | if Present (Declarations (N)) then | |
1911 | Body_Decl_List := New_List; | |
1912 | ||
1913 | declare | |
1914 | D : Node_Id; | |
1915 | New_D : Node_Id; | |
1916 | ||
1917 | begin | |
1918 | D := First (Declarations (N)); | |
1919 | while Present (D) loop | |
1920 | pragma Assert (Nkind (D) = N_Implicit_Label_Declaration); | |
1921 | ||
1922 | New_D := | |
1923 | Make_Implicit_Label_Declaration (Loc, | |
1924 | Make_Defining_Identifier (Loc, | |
1925 | Chars => Chars (Defining_Identifier (D))), | |
1926 | Label_Construct => Empty); | |
1927 | Append_To (Body_Decl_List, New_D); | |
1928 | ||
1929 | Next (D); | |
1930 | end loop; | |
1931 | end; | |
1932 | end if; | |
1933 | ||
1934 | pragma Assert (Present (Handled_Statement_Sequence (Ret_Node))); | |
1935 | ||
1936 | Proc_Body := | |
1937 | Make_Subprogram_Body (Loc, | |
1938 | Specification => Copy_Separate_Tree (Proc_Spec), | |
1939 | Declarations => Body_Decl_List, | |
1940 | Handled_Statement_Sequence => | |
1941 | Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node))); | |
1942 | ||
1943 | Set_Defining_Unit_Name (Specification (Proc_Body), | |
1944 | Make_Defining_Identifier (Loc, Subp_Name)); | |
1945 | ||
1946 | Append_To (Decl_List, Proc_Body); | |
1947 | end Build_Procedure; | |
1948 | ||
1949 | -- Local variables | |
1950 | ||
1951 | New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj); | |
1952 | Blk_Stmt : Node_Id; | |
1953 | Proc_Id : Entity_Id; | |
1954 | Proc_Call : Node_Id; | |
1955 | ||
1956 | -- Start of processing for Split_Unconstrained_Function | |
1957 | ||
1958 | begin | |
1959 | -- Build the associated procedure, analyze it and insert it before | |
3f80a182 | 1960 | -- the function body N. |
540d8610 ES |
1961 | |
1962 | declare | |
1963 | Scope : constant Entity_Id := Current_Scope; | |
1964 | Decl_List : List_Id; | |
1965 | begin | |
1966 | Pop_Scope; | |
1967 | Build_Procedure (Proc_Id, Decl_List); | |
1968 | Insert_Actions (N, Decl_List); | |
1969 | Push_Scope (Scope); | |
1970 | end; | |
1971 | ||
1972 | -- Build the call to the generated procedure | |
1973 | ||
1974 | declare | |
1975 | Actual_List : constant List_Id := New_List; | |
1976 | Formal : Entity_Id; | |
1977 | ||
1978 | begin | |
1979 | Append_To (Actual_List, | |
1980 | New_Occurrence_Of (Defining_Identifier (New_Obj), Loc)); | |
1981 | ||
1982 | Formal := First_Formal (Spec_Id); | |
1983 | while Present (Formal) loop | |
1984 | Append_To (Actual_List, New_Occurrence_Of (Formal, Loc)); | |
1985 | ||
1986 | -- Avoid spurious warning on unreferenced formals | |
1987 | ||
1988 | Set_Referenced (Formal); | |
1989 | Next_Formal (Formal); | |
1990 | end loop; | |
1991 | ||
1992 | Proc_Call := | |
1993 | Make_Procedure_Call_Statement (Loc, | |
3f80a182 | 1994 | Name => New_Occurrence_Of (Proc_Id, Loc), |
540d8610 ES |
1995 | Parameter_Associations => Actual_List); |
1996 | end; | |
1997 | ||
1998 | -- Generate | |
1999 | ||
2000 | -- declare | |
2001 | -- New_Obj : ... | |
2002 | -- begin | |
2003 | -- main_1__F1b (New_Obj, ...); | |
2004 | -- return Obj; | |
2005 | -- end B10b; | |
2006 | ||
2007 | Blk_Stmt := | |
2008 | Make_Block_Statement (Loc, | |
3f80a182 | 2009 | Declarations => New_List (New_Obj), |
540d8610 ES |
2010 | Handled_Statement_Sequence => |
2011 | Make_Handled_Sequence_Of_Statements (Loc, | |
2012 | Statements => New_List ( | |
2013 | ||
2014 | Proc_Call, | |
2015 | ||
2016 | Make_Simple_Return_Statement (Loc, | |
2017 | Expression => | |
2018 | New_Occurrence_Of | |
2019 | (Defining_Identifier (New_Obj), Loc))))); | |
2020 | ||
2021 | Rewrite (Ret_Node, Blk_Stmt); | |
2022 | end Split_Unconstrained_Function; | |
2023 | ||
16b10ccc AC |
2024 | -- Local variables |
2025 | ||
2026 | Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id); | |
2027 | ||
2028 | -- Start of processing for Check_And_Split_Unconstrained_Function | |
540d8610 ES |
2029 | |
2030 | begin | |
16b10ccc AC |
2031 | pragma Assert (Back_End_Inlining |
2032 | and then Ekind (Spec_Id) = E_Function | |
2033 | and then Returns_Unconstrained_Type (Spec_Id) | |
2034 | and then Comes_From_Source (Body_Id) | |
2035 | and then (Has_Pragma_Inline_Always (Spec_Id) | |
2036 | or else Optimization_Level > 0)); | |
2037 | ||
2038 | -- This routine must not be used in GNATprove mode since GNATprove | |
2039 | -- relies on frontend inlining | |
2040 | ||
2041 | pragma Assert (not GNATprove_Mode); | |
2042 | ||
2043 | -- No need to split the function if we cannot generate the code | |
2044 | ||
2045 | if Serious_Errors_Detected /= 0 then | |
2046 | return; | |
2047 | end if; | |
2048 | ||
16b10ccc AC |
2049 | -- No action needed in stubs since the attribute Body_To_Inline |
2050 | -- is not available | |
4bd4bb7f | 2051 | |
16b10ccc AC |
2052 | if Nkind (Decl) = N_Subprogram_Body_Stub then |
2053 | return; | |
2054 | ||
2055 | -- Cannot build the body to inline if the attribute is already set. | |
2056 | -- This attribute may have been set if this is a subprogram renaming | |
2057 | -- declarations (see Freeze.Build_Renamed_Body). | |
2058 | ||
2059 | elsif Present (Body_To_Inline (Decl)) then | |
2060 | return; | |
2061 | ||
2062 | -- Check excluded declarations | |
2063 | ||
2064 | elsif Present (Declarations (N)) | |
2065 | and then Has_Excluded_Declaration (Spec_Id, Declarations (N)) | |
2066 | then | |
2067 | return; | |
2068 | ||
2069 | -- Check excluded statements. There is no need to protect us against | |
2070 | -- exception handlers since they are supported by the GCC backend. | |
2071 | ||
2072 | elsif Present (Handled_Statement_Sequence (N)) | |
2073 | and then Has_Excluded_Statement | |
2074 | (Spec_Id, Statements (Handled_Statement_Sequence (N))) | |
2075 | then | |
2076 | return; | |
540d8610 ES |
2077 | end if; |
2078 | ||
2079 | -- Build the body to inline only if really needed | |
2080 | ||
16b10ccc AC |
2081 | if Can_Split_Unconstrained_Function (N) then |
2082 | Split_Unconstrained_Function (N, Spec_Id); | |
2083 | Build_Body_To_Inline (N, Spec_Id); | |
2084 | Set_Is_Inlined (Spec_Id); | |
540d8610 | 2085 | end if; |
16b10ccc | 2086 | end Check_And_Split_Unconstrained_Function; |
3f80a182 | 2087 | |
1773d80b AC |
2088 | ------------------------------------- |
2089 | -- Check_Package_Body_For_Inlining -- | |
2090 | ------------------------------------- | |
540d8610 | 2091 | |
1773d80b | 2092 | procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is |
540d8610 ES |
2093 | Bname : Unit_Name_Type; |
2094 | E : Entity_Id; | |
2095 | OK : Boolean; | |
2096 | ||
2097 | begin | |
88f7d2d1 AC |
2098 | -- Legacy implementation (relying on frontend inlining) |
2099 | ||
2100 | if not Back_End_Inlining | |
039538bc | 2101 | and then Is_Compilation_Unit (P) |
540d8610 ES |
2102 | and then not Is_Generic_Instance (P) |
2103 | then | |
2104 | Bname := Get_Body_Name (Get_Unit_Name (Unit (N))); | |
2105 | ||
2106 | E := First_Entity (P); | |
2107 | while Present (E) loop | |
88f7d2d1 AC |
2108 | if Has_Pragma_Inline_Always (E) |
2109 | or else (Has_Pragma_Inline (E) and Front_End_Inlining) | |
2110 | then | |
540d8610 ES |
2111 | if not Is_Loaded (Bname) then |
2112 | Load_Needed_Body (N, OK); | |
2113 | ||
2114 | if OK then | |
2115 | ||
2116 | -- Check we are not trying to inline a parent whose body | |
2117 | -- depends on a child, when we are compiling the body of | |
2118 | -- the child. Otherwise we have a potential elaboration | |
2119 | -- circularity with inlined subprograms and with | |
2120 | -- Taft-Amendment types. | |
2121 | ||
2122 | declare | |
2123 | Comp : Node_Id; -- Body just compiled | |
2124 | Child_Spec : Entity_Id; -- Spec of main unit | |
2125 | Ent : Entity_Id; -- For iteration | |
2126 | With_Clause : Node_Id; -- Context of body. | |
2127 | ||
2128 | begin | |
2129 | if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body | |
2130 | and then Present (Body_Entity (P)) | |
2131 | then | |
2132 | Child_Spec := | |
2133 | Defining_Entity | |
2134 | ((Unit (Library_Unit (Cunit (Main_Unit))))); | |
2135 | ||
2136 | Comp := | |
2137 | Parent (Unit_Declaration_Node (Body_Entity (P))); | |
2138 | ||
2139 | -- Check whether the context of the body just | |
2140 | -- compiled includes a child of itself, and that | |
2141 | -- child is the spec of the main compilation. | |
2142 | ||
2143 | With_Clause := First (Context_Items (Comp)); | |
2144 | while Present (With_Clause) loop | |
2145 | if Nkind (With_Clause) = N_With_Clause | |
2146 | and then | |
2147 | Scope (Entity (Name (With_Clause))) = P | |
2148 | and then | |
2149 | Entity (Name (With_Clause)) = Child_Spec | |
2150 | then | |
2151 | Error_Msg_Node_2 := Child_Spec; | |
2152 | Error_Msg_NE | |
2153 | ("body of & depends on child unit&??", | |
2154 | With_Clause, P); | |
2155 | Error_Msg_N | |
2156 | ("\subprograms in body cannot be inlined??", | |
2157 | With_Clause); | |
2158 | ||
2159 | -- Disable further inlining from this unit, | |
2160 | -- and keep Taft-amendment types incomplete. | |
2161 | ||
2162 | Ent := First_Entity (P); | |
2163 | while Present (Ent) loop | |
2164 | if Is_Type (Ent) | |
3f80a182 | 2165 | and then Has_Completion_In_Body (Ent) |
540d8610 ES |
2166 | then |
2167 | Set_Full_View (Ent, Empty); | |
2168 | ||
2169 | elsif Is_Subprogram (Ent) then | |
2170 | Set_Is_Inlined (Ent, False); | |
2171 | end if; | |
2172 | ||
2173 | Next_Entity (Ent); | |
2174 | end loop; | |
2175 | ||
2176 | return; | |
2177 | end if; | |
2178 | ||
2179 | Next (With_Clause); | |
2180 | end loop; | |
2181 | end if; | |
2182 | end; | |
2183 | ||
2184 | elsif Ineffective_Inline_Warnings then | |
2185 | Error_Msg_Unit_1 := Bname; | |
2186 | Error_Msg_N | |
2187 | ("unable to inline subprograms defined in $??", P); | |
2188 | Error_Msg_N ("\body not found??", P); | |
2189 | return; | |
2190 | end if; | |
2191 | end if; | |
2192 | ||
2193 | return; | |
2194 | end if; | |
2195 | ||
2196 | Next_Entity (E); | |
2197 | end loop; | |
2198 | end if; | |
1773d80b | 2199 | end Check_Package_Body_For_Inlining; |
540d8610 ES |
2200 | |
2201 | -------------------- | |
2202 | -- Cleanup_Scopes -- | |
2203 | -------------------- | |
2204 | ||
2205 | procedure Cleanup_Scopes is | |
2206 | Elmt : Elmt_Id; | |
2207 | Decl : Node_Id; | |
2208 | Scop : Entity_Id; | |
2209 | ||
2210 | begin | |
2211 | Elmt := First_Elmt (To_Clean); | |
2212 | while Present (Elmt) loop | |
2213 | Scop := Node (Elmt); | |
2214 | ||
2215 | if Ekind (Scop) = E_Entry then | |
2216 | Scop := Protected_Body_Subprogram (Scop); | |
2217 | ||
2218 | elsif Is_Subprogram (Scop) | |
2219 | and then Is_Protected_Type (Scope (Scop)) | |
2220 | and then Present (Protected_Body_Subprogram (Scop)) | |
2221 | then | |
3f80a182 AC |
2222 | -- If a protected operation contains an instance, its cleanup |
2223 | -- operations have been delayed, and the subprogram has been | |
2224 | -- rewritten in the expansion of the enclosing protected body. It | |
2225 | -- is the corresponding subprogram that may require the cleanup | |
2226 | -- operations, so propagate the information that triggers cleanup | |
2227 | -- activity. | |
540d8610 ES |
2228 | |
2229 | Set_Uses_Sec_Stack | |
2230 | (Protected_Body_Subprogram (Scop), | |
2231 | Uses_Sec_Stack (Scop)); | |
2232 | ||
2233 | Scop := Protected_Body_Subprogram (Scop); | |
2234 | end if; | |
2235 | ||
2236 | if Ekind (Scop) = E_Block then | |
2237 | Decl := Parent (Block_Node (Scop)); | |
2238 | ||
2239 | else | |
2240 | Decl := Unit_Declaration_Node (Scop); | |
2241 | ||
3f80a182 AC |
2242 | if Nkind_In (Decl, N_Subprogram_Declaration, |
2243 | N_Task_Type_Declaration, | |
2244 | N_Subprogram_Body_Stub) | |
540d8610 ES |
2245 | then |
2246 | Decl := Unit_Declaration_Node (Corresponding_Body (Decl)); | |
2247 | end if; | |
2248 | end if; | |
2249 | ||
2250 | Push_Scope (Scop); | |
2251 | Expand_Cleanup_Actions (Decl); | |
2252 | End_Scope; | |
2253 | ||
2254 | Elmt := Next_Elmt (Elmt); | |
2255 | end loop; | |
2256 | end Cleanup_Scopes; | |
2257 | ||
2258 | ------------------------- | |
2259 | -- Expand_Inlined_Call -- | |
2260 | ------------------------- | |
2261 | ||
2262 | procedure Expand_Inlined_Call | |
2263 | (N : Node_Id; | |
2264 | Subp : Entity_Id; | |
2265 | Orig_Subp : Entity_Id) | |
2266 | is | |
2267 | Loc : constant Source_Ptr := Sloc (N); | |
2268 | Is_Predef : constant Boolean := | |
3f80a182 AC |
2269 | Is_Predefined_File_Name |
2270 | (Unit_File_Name (Get_Source_Unit (Subp))); | |
540d8610 ES |
2271 | Orig_Bod : constant Node_Id := |
2272 | Body_To_Inline (Unit_Declaration_Node (Subp)); | |
2273 | ||
2274 | Blk : Node_Id; | |
2275 | Decl : Node_Id; | |
2276 | Decls : constant List_Id := New_List; | |
3f80a182 | 2277 | Exit_Lab : Entity_Id := Empty; |
540d8610 ES |
2278 | F : Entity_Id; |
2279 | A : Node_Id; | |
2280 | Lab_Decl : Node_Id; | |
2281 | Lab_Id : Node_Id; | |
2282 | New_A : Node_Id; | |
e90e9503 | 2283 | Num_Ret : Nat := 0; |
540d8610 ES |
2284 | Ret_Type : Entity_Id; |
2285 | ||
2286 | Targ : Node_Id; | |
2287 | -- The target of the call. If context is an assignment statement then | |
2288 | -- this is the left-hand side of the assignment, else it is a temporary | |
2289 | -- to which the return value is assigned prior to rewriting the call. | |
2290 | ||
2291 | Targ1 : Node_Id; | |
2292 | -- A separate target used when the return type is unconstrained | |
2293 | ||
2294 | Temp : Entity_Id; | |
2295 | Temp_Typ : Entity_Id; | |
2296 | ||
2297 | Return_Object : Entity_Id := Empty; | |
2298 | -- Entity in declaration in an extended_return_statement | |
2299 | ||
2300 | Is_Unc : Boolean; | |
2301 | Is_Unc_Decl : Boolean; | |
2302 | -- If the type returned by the function is unconstrained and the call | |
2303 | -- can be inlined, special processing is required. | |
2304 | ||
64f5d139 JM |
2305 | procedure Declare_Postconditions_Result; |
2306 | -- When generating C code, declare _Result, which may be used in the | |
2307 | -- inlined _Postconditions procedure to verify the return value. | |
2308 | ||
540d8610 ES |
2309 | procedure Make_Exit_Label; |
2310 | -- Build declaration for exit label to be used in Return statements, | |
2311 | -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit | |
2312 | -- declaration). Does nothing if Exit_Lab already set. | |
2313 | ||
2314 | function Process_Formals (N : Node_Id) return Traverse_Result; | |
2315 | -- Replace occurrence of a formal with the corresponding actual, or the | |
2316 | -- thunk generated for it. Replace a return statement with an assignment | |
2317 | -- to the target of the call, with appropriate conversions if needed. | |
2318 | ||
2319 | function Process_Sloc (Nod : Node_Id) return Traverse_Result; | |
2320 | -- If the call being expanded is that of an internal subprogram, set the | |
2321 | -- sloc of the generated block to that of the call itself, so that the | |
52c1498c YM |
2322 | -- expansion is skipped by the "next" command in gdb. Same processing |
2323 | -- for a subprogram in a predefined file, e.g. Ada.Tags. If | |
2324 | -- Debug_Generated_Code is true, suppress this change to simplify our | |
2325 | -- own development. Same in GNATprove mode, to ensure that warnings and | |
2326 | -- diagnostics point to the proper location. | |
540d8610 ES |
2327 | |
2328 | procedure Reset_Dispatching_Calls (N : Node_Id); | |
2329 | -- In subtree N search for occurrences of dispatching calls that use the | |
2330 | -- Ada 2005 Object.Operation notation and the object is a formal of the | |
2331 | -- inlined subprogram. Reset the entity associated with Operation in all | |
2332 | -- the found occurrences. | |
2333 | ||
2334 | procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id); | |
2335 | -- If the function body is a single expression, replace call with | |
2336 | -- expression, else insert block appropriately. | |
2337 | ||
2338 | procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id); | |
2339 | -- If procedure body has no local variables, inline body without | |
2340 | -- creating block, otherwise rewrite call with block. | |
2341 | ||
2342 | function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean; | |
2343 | -- Determine whether a formal parameter is used only once in Orig_Bod | |
2344 | ||
64f5d139 JM |
2345 | ----------------------------------- |
2346 | -- Declare_Postconditions_Result -- | |
2347 | ----------------------------------- | |
2348 | ||
2349 | procedure Declare_Postconditions_Result is | |
2350 | Enclosing_Subp : constant Entity_Id := Scope (Subp); | |
2351 | ||
2352 | begin | |
2353 | pragma Assert | |
2354 | (Modify_Tree_For_C | |
2355 | and then Is_Subprogram (Enclosing_Subp) | |
2356 | and then Present (Postconditions_Proc (Enclosing_Subp))); | |
2357 | ||
2358 | if Ekind (Enclosing_Subp) = E_Function then | |
fb757f7d AC |
2359 | if Nkind (First (Parameter_Associations (N))) in |
2360 | N_Numeric_Or_String_Literal | |
64f5d139 JM |
2361 | then |
2362 | Append_To (Declarations (Blk), | |
2363 | Make_Object_Declaration (Loc, | |
2364 | Defining_Identifier => | |
2365 | Make_Defining_Identifier (Loc, Name_uResult), | |
2366 | Constant_Present => True, | |
2367 | Object_Definition => | |
2368 | New_Occurrence_Of (Etype (Enclosing_Subp), Loc), | |
2369 | Expression => | |
2370 | New_Copy_Tree (First (Parameter_Associations (N))))); | |
2371 | else | |
2372 | Append_To (Declarations (Blk), | |
2373 | Make_Object_Renaming_Declaration (Loc, | |
2374 | Defining_Identifier => | |
2375 | Make_Defining_Identifier (Loc, Name_uResult), | |
2376 | Subtype_Mark => | |
2377 | New_Occurrence_Of (Etype (Enclosing_Subp), Loc), | |
2378 | Name => | |
2379 | New_Copy_Tree (First (Parameter_Associations (N))))); | |
2380 | end if; | |
2381 | end if; | |
2382 | end Declare_Postconditions_Result; | |
2383 | ||
540d8610 ES |
2384 | --------------------- |
2385 | -- Make_Exit_Label -- | |
2386 | --------------------- | |
2387 | ||
2388 | procedure Make_Exit_Label is | |
2389 | Lab_Ent : Entity_Id; | |
2390 | begin | |
2391 | if No (Exit_Lab) then | |
2392 | Lab_Ent := Make_Temporary (Loc, 'L'); | |
2393 | Lab_Id := New_Occurrence_Of (Lab_Ent, Loc); | |
2394 | Exit_Lab := Make_Label (Loc, Lab_Id); | |
2395 | Lab_Decl := | |
2396 | Make_Implicit_Label_Declaration (Loc, | |
3f80a182 AC |
2397 | Defining_Identifier => Lab_Ent, |
2398 | Label_Construct => Exit_Lab); | |
540d8610 ES |
2399 | end if; |
2400 | end Make_Exit_Label; | |
2401 | ||
2402 | --------------------- | |
2403 | -- Process_Formals -- | |
2404 | --------------------- | |
2405 | ||
2406 | function Process_Formals (N : Node_Id) return Traverse_Result is | |
2407 | A : Entity_Id; | |
2408 | E : Entity_Id; | |
2409 | Ret : Node_Id; | |
2410 | ||
2411 | begin | |
2412 | if Is_Entity_Name (N) and then Present (Entity (N)) then | |
2413 | E := Entity (N); | |
2414 | ||
2415 | if Is_Formal (E) and then Scope (E) = Subp then | |
2416 | A := Renamed_Object (E); | |
2417 | ||
2418 | -- Rewrite the occurrence of the formal into an occurrence of | |
2419 | -- the actual. Also establish visibility on the proper view of | |
2420 | -- the actual's subtype for the body's context (if the actual's | |
2421 | -- subtype is private at the call point but its full view is | |
2422 | -- visible to the body, then the inlined tree here must be | |
2423 | -- analyzed with the full view). | |
2424 | ||
2425 | if Is_Entity_Name (A) then | |
1db700c3 | 2426 | Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N))); |
540d8610 ES |
2427 | Check_Private_View (N); |
2428 | ||
2429 | elsif Nkind (A) = N_Defining_Identifier then | |
1db700c3 | 2430 | Rewrite (N, New_Occurrence_Of (A, Sloc (N))); |
540d8610 ES |
2431 | Check_Private_View (N); |
2432 | ||
2433 | -- Numeric literal | |
2434 | ||
2435 | else | |
2436 | Rewrite (N, New_Copy (A)); | |
2437 | end if; | |
2438 | end if; | |
2439 | ||
2440 | return Skip; | |
2441 | ||
2442 | elsif Is_Entity_Name (N) | |
2443 | and then Present (Return_Object) | |
2444 | and then Chars (N) = Chars (Return_Object) | |
2445 | then | |
2446 | -- Occurrence within an extended return statement. The return | |
2447 | -- object is local to the body been inlined, and thus the generic | |
2448 | -- copy is not analyzed yet, so we match by name, and replace it | |
2449 | -- with target of call. | |
2450 | ||
2451 | if Nkind (Targ) = N_Defining_Identifier then | |
2452 | Rewrite (N, New_Occurrence_Of (Targ, Loc)); | |
2453 | else | |
2454 | Rewrite (N, New_Copy_Tree (Targ)); | |
2455 | end if; | |
2456 | ||
2457 | return Skip; | |
2458 | ||
2459 | elsif Nkind (N) = N_Simple_Return_Statement then | |
2460 | if No (Expression (N)) then | |
00f45f30 | 2461 | Num_Ret := Num_Ret + 1; |
540d8610 ES |
2462 | Make_Exit_Label; |
2463 | Rewrite (N, | |
2464 | Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id))); | |
2465 | ||
2466 | else | |
2467 | if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements | |
2468 | and then Nkind (Parent (Parent (N))) = N_Subprogram_Body | |
2469 | then | |
2470 | -- Function body is a single expression. No need for | |
2471 | -- exit label. | |
2472 | ||
2473 | null; | |
2474 | ||
2475 | else | |
2476 | Num_Ret := Num_Ret + 1; | |
2477 | Make_Exit_Label; | |
2478 | end if; | |
2479 | ||
2480 | -- Because of the presence of private types, the views of the | |
2481 | -- expression and the context may be different, so place an | |
2482 | -- unchecked conversion to the context type to avoid spurious | |
2483 | -- errors, e.g. when the expression is a numeric literal and | |
2484 | -- the context is private. If the expression is an aggregate, | |
2485 | -- use a qualified expression, because an aggregate is not a | |
2486 | -- legal argument of a conversion. Ditto for numeric literals, | |
2487 | -- which must be resolved to a specific type. | |
2488 | ||
2489 | if Nkind_In (Expression (N), N_Aggregate, | |
2490 | N_Null, | |
2491 | N_Real_Literal, | |
2492 | N_Integer_Literal) | |
2493 | then | |
2494 | Ret := | |
2495 | Make_Qualified_Expression (Sloc (N), | |
2496 | Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)), | |
3f80a182 | 2497 | Expression => Relocate_Node (Expression (N))); |
540d8610 ES |
2498 | else |
2499 | Ret := | |
2500 | Unchecked_Convert_To | |
2501 | (Ret_Type, Relocate_Node (Expression (N))); | |
2502 | end if; | |
2503 | ||
2504 | if Nkind (Targ) = N_Defining_Identifier then | |
2505 | Rewrite (N, | |
2506 | Make_Assignment_Statement (Loc, | |
2507 | Name => New_Occurrence_Of (Targ, Loc), | |
2508 | Expression => Ret)); | |
2509 | else | |
2510 | Rewrite (N, | |
2511 | Make_Assignment_Statement (Loc, | |
2512 | Name => New_Copy (Targ), | |
2513 | Expression => Ret)); | |
2514 | end if; | |
2515 | ||
2516 | Set_Assignment_OK (Name (N)); | |
2517 | ||
2518 | if Present (Exit_Lab) then | |
2519 | Insert_After (N, | |
2520 | Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id))); | |
2521 | end if; | |
2522 | end if; | |
2523 | ||
2524 | return OK; | |
2525 | ||
2526 | -- An extended return becomes a block whose first statement is the | |
2527 | -- assignment of the initial expression of the return object to the | |
2528 | -- target of the call itself. | |
2529 | ||
2530 | elsif Nkind (N) = N_Extended_Return_Statement then | |
2531 | declare | |
2532 | Return_Decl : constant Entity_Id := | |
2533 | First (Return_Object_Declarations (N)); | |
2534 | Assign : Node_Id; | |
2535 | ||
2536 | begin | |
2537 | Return_Object := Defining_Identifier (Return_Decl); | |
2538 | ||
2539 | if Present (Expression (Return_Decl)) then | |
2540 | if Nkind (Targ) = N_Defining_Identifier then | |
2541 | Assign := | |
2542 | Make_Assignment_Statement (Loc, | |
2543 | Name => New_Occurrence_Of (Targ, Loc), | |
2544 | Expression => Expression (Return_Decl)); | |
2545 | else | |
2546 | Assign := | |
2547 | Make_Assignment_Statement (Loc, | |
2548 | Name => New_Copy (Targ), | |
2549 | Expression => Expression (Return_Decl)); | |
2550 | end if; | |
2551 | ||
2552 | Set_Assignment_OK (Name (Assign)); | |
2553 | ||
2554 | if No (Handled_Statement_Sequence (N)) then | |
2555 | Set_Handled_Statement_Sequence (N, | |
2556 | Make_Handled_Sequence_Of_Statements (Loc, | |
2557 | Statements => New_List)); | |
2558 | end if; | |
2559 | ||
2560 | Prepend (Assign, | |
2561 | Statements (Handled_Statement_Sequence (N))); | |
2562 | end if; | |
2563 | ||
2564 | Rewrite (N, | |
2565 | Make_Block_Statement (Loc, | |
2566 | Handled_Statement_Sequence => | |
2567 | Handled_Statement_Sequence (N))); | |
2568 | ||
2569 | return OK; | |
2570 | end; | |
2571 | ||
2572 | -- Remove pragma Unreferenced since it may refer to formals that | |
2573 | -- are not visible in the inlined body, and in any case we will | |
2574 | -- not be posting warnings on the inlined body so it is unneeded. | |
2575 | ||
2576 | elsif Nkind (N) = N_Pragma | |
6e759c2a | 2577 | and then Pragma_Name (N) = Name_Unreferenced |
540d8610 ES |
2578 | then |
2579 | Rewrite (N, Make_Null_Statement (Sloc (N))); | |
2580 | return OK; | |
2581 | ||
2582 | else | |
2583 | return OK; | |
2584 | end if; | |
2585 | end Process_Formals; | |
2586 | ||
2587 | procedure Replace_Formals is new Traverse_Proc (Process_Formals); | |
2588 | ||
2589 | ------------------ | |
2590 | -- Process_Sloc -- | |
2591 | ------------------ | |
2592 | ||
2593 | function Process_Sloc (Nod : Node_Id) return Traverse_Result is | |
2594 | begin | |
2595 | if not Debug_Generated_Code then | |
2596 | Set_Sloc (Nod, Sloc (N)); | |
2597 | Set_Comes_From_Source (Nod, False); | |
2598 | end if; | |
2599 | ||
2600 | return OK; | |
2601 | end Process_Sloc; | |
2602 | ||
2603 | procedure Reset_Slocs is new Traverse_Proc (Process_Sloc); | |
2604 | ||
2605 | ------------------------------ | |
2606 | -- Reset_Dispatching_Calls -- | |
2607 | ------------------------------ | |
2608 | ||
2609 | procedure Reset_Dispatching_Calls (N : Node_Id) is | |
2610 | ||
2611 | function Do_Reset (N : Node_Id) return Traverse_Result; | |
2612 | -- Comment required ??? | |
2613 | ||
2614 | -------------- | |
2615 | -- Do_Reset -- | |
2616 | -------------- | |
2617 | ||
2618 | function Do_Reset (N : Node_Id) return Traverse_Result is | |
2619 | begin | |
2620 | if Nkind (N) = N_Procedure_Call_Statement | |
2621 | and then Nkind (Name (N)) = N_Selected_Component | |
2622 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
2623 | and then Is_Formal (Entity (Prefix (Name (N)))) | |
2624 | and then Is_Dispatching_Operation | |
2625 | (Entity (Selector_Name (Name (N)))) | |
2626 | then | |
2627 | Set_Entity (Selector_Name (Name (N)), Empty); | |
2628 | end if; | |
2629 | ||
2630 | return OK; | |
2631 | end Do_Reset; | |
2632 | ||
2633 | function Do_Reset_Calls is new Traverse_Func (Do_Reset); | |
2634 | ||
2635 | -- Local variables | |
2636 | ||
2637 | Dummy : constant Traverse_Result := Do_Reset_Calls (N); | |
2638 | pragma Unreferenced (Dummy); | |
2639 | ||
2640 | -- Start of processing for Reset_Dispatching_Calls | |
2641 | ||
2642 | begin | |
2643 | null; | |
2644 | end Reset_Dispatching_Calls; | |
2645 | ||
2646 | --------------------------- | |
2647 | -- Rewrite_Function_Call -- | |
2648 | --------------------------- | |
2649 | ||
2650 | procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is | |
2651 | HSS : constant Node_Id := Handled_Statement_Sequence (Blk); | |
2652 | Fst : constant Node_Id := First (Statements (HSS)); | |
2653 | ||
2654 | begin | |
2655 | -- Optimize simple case: function body is a single return statement, | |
2656 | -- which has been expanded into an assignment. | |
2657 | ||
2658 | if Is_Empty_List (Declarations (Blk)) | |
2659 | and then Nkind (Fst) = N_Assignment_Statement | |
2660 | and then No (Next (Fst)) | |
2661 | then | |
2662 | -- The function call may have been rewritten as the temporary | |
2663 | -- that holds the result of the call, in which case remove the | |
2664 | -- now useless declaration. | |
2665 | ||
2666 | if Nkind (N) = N_Identifier | |
2667 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
2668 | then | |
2669 | Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc)); | |
2670 | end if; | |
2671 | ||
2672 | Rewrite (N, Expression (Fst)); | |
2673 | ||
2674 | elsif Nkind (N) = N_Identifier | |
2675 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
2676 | then | |
2677 | -- The block assigns the result of the call to the temporary | |
2678 | ||
2679 | Insert_After (Parent (Entity (N)), Blk); | |
2680 | ||
2681 | -- If the context is an assignment, and the left-hand side is free of | |
2682 | -- side-effects, the replacement is also safe. | |
2683 | -- Can this be generalized further??? | |
2684 | ||
2685 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
2686 | and then | |
2687 | (Is_Entity_Name (Name (Parent (N))) | |
2688 | or else | |
2689 | (Nkind (Name (Parent (N))) = N_Explicit_Dereference | |
2690 | and then Is_Entity_Name (Prefix (Name (Parent (N))))) | |
2691 | ||
2692 | or else | |
2693 | (Nkind (Name (Parent (N))) = N_Selected_Component | |
2694 | and then Is_Entity_Name (Prefix (Name (Parent (N)))))) | |
2695 | then | |
2696 | -- Replace assignment with the block | |
2697 | ||
2698 | declare | |
2699 | Original_Assignment : constant Node_Id := Parent (N); | |
2700 | ||
2701 | begin | |
2702 | -- Preserve the original assignment node to keep the complete | |
2703 | -- assignment subtree consistent enough for Analyze_Assignment | |
2704 | -- to proceed (specifically, the original Lhs node must still | |
2705 | -- have an assignment statement as its parent). | |
2706 | ||
2707 | -- We cannot rely on Original_Node to go back from the block | |
2708 | -- node to the assignment node, because the assignment might | |
2709 | -- already be a rewrite substitution. | |
2710 | ||
2711 | Discard_Node (Relocate_Node (Original_Assignment)); | |
2712 | Rewrite (Original_Assignment, Blk); | |
2713 | end; | |
2714 | ||
2715 | elsif Nkind (Parent (N)) = N_Object_Declaration then | |
2716 | ||
2717 | -- A call to a function which returns an unconstrained type | |
2718 | -- found in the expression initializing an object-declaration is | |
2719 | -- expanded into a procedure call which must be added after the | |
2720 | -- object declaration. | |
2721 | ||
ea0c8cfb | 2722 | if Is_Unc_Decl and Back_End_Inlining then |
540d8610 ES |
2723 | Insert_Action_After (Parent (N), Blk); |
2724 | else | |
2725 | Set_Expression (Parent (N), Empty); | |
2726 | Insert_After (Parent (N), Blk); | |
2727 | end if; | |
2728 | ||
6c26bac2 | 2729 | elsif Is_Unc and then not Back_End_Inlining then |
540d8610 ES |
2730 | Insert_Before (Parent (N), Blk); |
2731 | end if; | |
2732 | end Rewrite_Function_Call; | |
2733 | ||
2734 | ---------------------------- | |
2735 | -- Rewrite_Procedure_Call -- | |
2736 | ---------------------------- | |
2737 | ||
2738 | procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is | |
2739 | HSS : constant Node_Id := Handled_Statement_Sequence (Blk); | |
2740 | ||
2741 | begin | |
2742 | -- If there is a transient scope for N, this will be the scope of the | |
2743 | -- actions for N, and the statements in Blk need to be within this | |
2744 | -- scope. For example, they need to have visibility on the constant | |
2745 | -- declarations created for the formals. | |
2746 | ||
2747 | -- If N needs no transient scope, and if there are no declarations in | |
2748 | -- the inlined body, we can do a little optimization and insert the | |
2749 | -- statements for the body directly after N, and rewrite N to a | |
2750 | -- null statement, instead of rewriting N into a full-blown block | |
2751 | -- statement. | |
2752 | ||
2753 | if not Scope_Is_Transient | |
2754 | and then Is_Empty_List (Declarations (Blk)) | |
2755 | then | |
2756 | Insert_List_After (N, Statements (HSS)); | |
2757 | Rewrite (N, Make_Null_Statement (Loc)); | |
2758 | else | |
2759 | Rewrite (N, Blk); | |
2760 | end if; | |
2761 | end Rewrite_Procedure_Call; | |
2762 | ||
2763 | ------------------------- | |
2764 | -- Formal_Is_Used_Once -- | |
2765 | ------------------------- | |
2766 | ||
2767 | function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is | |
2768 | Use_Counter : Int := 0; | |
2769 | ||
2770 | function Count_Uses (N : Node_Id) return Traverse_Result; | |
2771 | -- Traverse the tree and count the uses of the formal parameter. | |
2772 | -- In this case, for optimization purposes, we do not need to | |
2773 | -- continue the traversal once more than one use is encountered. | |
2774 | ||
2775 | ---------------- | |
2776 | -- Count_Uses -- | |
2777 | ---------------- | |
2778 | ||
2779 | function Count_Uses (N : Node_Id) return Traverse_Result is | |
2780 | begin | |
2781 | -- The original node is an identifier | |
2782 | ||
2783 | if Nkind (N) = N_Identifier | |
2784 | and then Present (Entity (N)) | |
2785 | ||
2786 | -- Original node's entity points to the one in the copied body | |
2787 | ||
2788 | and then Nkind (Entity (N)) = N_Identifier | |
2789 | and then Present (Entity (Entity (N))) | |
2790 | ||
2791 | -- The entity of the copied node is the formal parameter | |
2792 | ||
2793 | and then Entity (Entity (N)) = Formal | |
2794 | then | |
2795 | Use_Counter := Use_Counter + 1; | |
2796 | ||
2797 | if Use_Counter > 1 then | |
2798 | ||
2799 | -- Denote more than one use and abandon the traversal | |
2800 | ||
2801 | Use_Counter := 2; | |
2802 | return Abandon; | |
2803 | ||
2804 | end if; | |
2805 | end if; | |
2806 | ||
2807 | return OK; | |
2808 | end Count_Uses; | |
2809 | ||
2810 | procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses); | |
2811 | ||
2812 | -- Start of processing for Formal_Is_Used_Once | |
2813 | ||
2814 | begin | |
2815 | Count_Formal_Uses (Orig_Bod); | |
2816 | return Use_Counter = 1; | |
2817 | end Formal_Is_Used_Once; | |
2818 | ||
2819 | -- Start of processing for Expand_Inlined_Call | |
2820 | ||
2821 | begin | |
2822 | -- Initializations for old/new semantics | |
2823 | ||
6c26bac2 | 2824 | if not Back_End_Inlining then |
540d8610 ES |
2825 | Is_Unc := Is_Array_Type (Etype (Subp)) |
2826 | and then not Is_Constrained (Etype (Subp)); | |
2827 | Is_Unc_Decl := False; | |
2828 | else | |
2829 | Is_Unc := Returns_Unconstrained_Type (Subp) | |
2830 | and then Optimization_Level > 0; | |
2831 | Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration | |
2832 | and then Is_Unc; | |
2833 | end if; | |
2834 | ||
2835 | -- Check for an illegal attempt to inline a recursive procedure. If the | |
2836 | -- subprogram has parameters this is detected when trying to supply a | |
2837 | -- binding for parameters that already have one. For parameterless | |
2838 | -- subprograms this must be done explicitly. | |
2839 | ||
2840 | if In_Open_Scopes (Subp) then | |
db99c46e AC |
2841 | Cannot_Inline |
2842 | ("cannot inline call to recursive subprogram?", N, Subp); | |
540d8610 ES |
2843 | Set_Is_Inlined (Subp, False); |
2844 | return; | |
2845 | ||
2846 | -- Skip inlining if this is not a true inlining since the attribute | |
09edc2c2 AC |
2847 | -- Body_To_Inline is also set for renamings (see sinfo.ads). For a |
2848 | -- true inlining, Orig_Bod has code rather than being an entity. | |
540d8610 ES |
2849 | |
2850 | elsif Nkind (Orig_Bod) in N_Entity then | |
09edc2c2 | 2851 | return; |
540d8610 ES |
2852 | |
2853 | -- Skip inlining if the function returns an unconstrained type using | |
2854 | -- an extended return statement since this part of the new inlining | |
2855 | -- model which is not yet supported by the current implementation. ??? | |
2856 | ||
2857 | elsif Is_Unc | |
2858 | and then | |
db99c46e AC |
2859 | Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod)))) = |
2860 | N_Extended_Return_Statement | |
6c26bac2 | 2861 | and then not Back_End_Inlining |
540d8610 ES |
2862 | then |
2863 | return; | |
2864 | end if; | |
2865 | ||
2866 | if Nkind (Orig_Bod) = N_Defining_Identifier | |
2867 | or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol | |
2868 | then | |
2869 | -- Subprogram is renaming_as_body. Calls occurring after the renaming | |
2870 | -- can be replaced with calls to the renamed entity directly, because | |
2871 | -- the subprograms are subtype conformant. If the renamed subprogram | |
2872 | -- is an inherited operation, we must redo the expansion because | |
2873 | -- implicit conversions may be needed. Similarly, if the renamed | |
2874 | -- entity is inlined, expand the call for further optimizations. | |
2875 | ||
2876 | Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc)); | |
2877 | ||
2878 | if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then | |
2879 | Expand_Call (N); | |
2880 | end if; | |
2881 | ||
2882 | return; | |
2883 | end if; | |
2884 | ||
2885 | -- Register the call in the list of inlined calls | |
2886 | ||
21c51f53 | 2887 | Append_New_Elmt (N, To => Inlined_Calls); |
540d8610 ES |
2888 | |
2889 | -- Use generic machinery to copy body of inlined subprogram, as if it | |
2890 | -- were an instantiation, resetting source locations appropriately, so | |
2891 | -- that nested inlined calls appear in the main unit. | |
2892 | ||
2893 | Save_Env (Subp, Empty); | |
2894 | Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod)); | |
2895 | ||
2896 | -- Old semantics | |
2897 | ||
6c26bac2 | 2898 | if not Back_End_Inlining then |
540d8610 ES |
2899 | declare |
2900 | Bod : Node_Id; | |
2901 | ||
2902 | begin | |
2903 | Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True); | |
2904 | Blk := | |
2905 | Make_Block_Statement (Loc, | |
3f80a182 | 2906 | Declarations => Declarations (Bod), |
540d8610 ES |
2907 | Handled_Statement_Sequence => |
2908 | Handled_Statement_Sequence (Bod)); | |
2909 | ||
2910 | if No (Declarations (Bod)) then | |
2911 | Set_Declarations (Blk, New_List); | |
2912 | end if; | |
2913 | ||
64f5d139 JM |
2914 | -- When generating C code, declare _Result, which may be used to |
2915 | -- verify the return value. | |
2916 | ||
2917 | if Modify_Tree_For_C | |
2918 | and then Nkind (N) = N_Procedure_Call_Statement | |
2919 | and then Chars (Name (N)) = Name_uPostconditions | |
2920 | then | |
2921 | Declare_Postconditions_Result; | |
2922 | end if; | |
2923 | ||
540d8610 ES |
2924 | -- For the unconstrained case, capture the name of the local |
2925 | -- variable that holds the result. This must be the first | |
2926 | -- declaration in the block, because its bounds cannot depend | |
2927 | -- on local variables. Otherwise there is no way to declare the | |
2928 | -- result outside of the block. Needless to say, in general the | |
2929 | -- bounds will depend on the actuals in the call. | |
2930 | ||
2931 | -- If the context is an assignment statement, as is the case | |
2932 | -- for the expansion of an extended return, the left-hand side | |
2933 | -- provides bounds even if the return type is unconstrained. | |
2934 | ||
2935 | if Is_Unc then | |
2936 | declare | |
2937 | First_Decl : Node_Id; | |
2938 | ||
2939 | begin | |
2940 | First_Decl := First (Declarations (Blk)); | |
2941 | ||
2942 | if Nkind (First_Decl) /= N_Object_Declaration then | |
2943 | return; | |
2944 | end if; | |
2945 | ||
2946 | if Nkind (Parent (N)) /= N_Assignment_Statement then | |
2947 | Targ1 := Defining_Identifier (First_Decl); | |
2948 | else | |
2949 | Targ1 := Name (Parent (N)); | |
2950 | end if; | |
2951 | end; | |
2952 | end if; | |
2953 | end; | |
2954 | ||
2955 | -- New semantics | |
2956 | ||
2957 | else | |
2958 | declare | |
2959 | Bod : Node_Id; | |
2960 | ||
2961 | begin | |
2962 | -- General case | |
2963 | ||
2964 | if not Is_Unc then | |
2965 | Bod := | |
2966 | Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True); | |
2967 | Blk := | |
2968 | Make_Block_Statement (Loc, | |
3f80a182 AC |
2969 | Declarations => Declarations (Bod), |
2970 | Handled_Statement_Sequence => | |
2971 | Handled_Statement_Sequence (Bod)); | |
540d8610 ES |
2972 | |
2973 | -- Inline a call to a function that returns an unconstrained type. | |
2974 | -- The semantic analyzer checked that frontend-inlined functions | |
2975 | -- returning unconstrained types have no declarations and have | |
2976 | -- a single extended return statement. As part of its processing | |
2977 | -- the function was split in two subprograms: a procedure P and | |
2978 | -- a function F that has a block with a call to procedure P (see | |
2979 | -- Split_Unconstrained_Function). | |
2980 | ||
2981 | else | |
2982 | pragma Assert | |
2983 | (Nkind | |
2984 | (First | |
3f80a182 AC |
2985 | (Statements (Handled_Statement_Sequence (Orig_Bod)))) = |
2986 | N_Block_Statement); | |
540d8610 ES |
2987 | |
2988 | declare | |
2989 | Blk_Stmt : constant Node_Id := | |
3f80a182 | 2990 | First (Statements (Handled_Statement_Sequence (Orig_Bod))); |
540d8610 | 2991 | First_Stmt : constant Node_Id := |
3f80a182 | 2992 | First (Statements (Handled_Statement_Sequence (Blk_Stmt))); |
540d8610 ES |
2993 | Second_Stmt : constant Node_Id := Next (First_Stmt); |
2994 | ||
2995 | begin | |
2996 | pragma Assert | |
2997 | (Nkind (First_Stmt) = N_Procedure_Call_Statement | |
2998 | and then Nkind (Second_Stmt) = N_Simple_Return_Statement | |
2999 | and then No (Next (Second_Stmt))); | |
3000 | ||
3001 | Bod := | |
3002 | Copy_Generic_Node | |
3003 | (First | |
3004 | (Statements (Handled_Statement_Sequence (Orig_Bod))), | |
3005 | Empty, Instantiating => True); | |
3006 | Blk := Bod; | |
3007 | ||
3008 | -- Capture the name of the local variable that holds the | |
3009 | -- result. This must be the first declaration in the block, | |
3010 | -- because its bounds cannot depend on local variables. | |
3011 | -- Otherwise there is no way to declare the result outside | |
3012 | -- of the block. Needless to say, in general the bounds will | |
3013 | -- depend on the actuals in the call. | |
3014 | ||
3015 | if Nkind (Parent (N)) /= N_Assignment_Statement then | |
3016 | Targ1 := Defining_Identifier (First (Declarations (Blk))); | |
3017 | ||
3018 | -- If the context is an assignment statement, as is the case | |
3019 | -- for the expansion of an extended return, the left-hand | |
3020 | -- side provides bounds even if the return type is | |
3021 | -- unconstrained. | |
3022 | ||
3023 | else | |
3024 | Targ1 := Name (Parent (N)); | |
3025 | end if; | |
3026 | end; | |
3027 | end if; | |
3028 | ||
3029 | if No (Declarations (Bod)) then | |
3030 | Set_Declarations (Blk, New_List); | |
3031 | end if; | |
3032 | end; | |
3033 | end if; | |
3034 | ||
3035 | -- If this is a derived function, establish the proper return type | |
3036 | ||
3037 | if Present (Orig_Subp) and then Orig_Subp /= Subp then | |
3038 | Ret_Type := Etype (Orig_Subp); | |
3039 | else | |
3040 | Ret_Type := Etype (Subp); | |
3041 | end if; | |
3042 | ||
3043 | -- Create temporaries for the actuals that are expressions, or that are | |
3044 | -- scalars and require copying to preserve semantics. | |
3045 | ||
3046 | F := First_Formal (Subp); | |
3047 | A := First_Actual (N); | |
3048 | while Present (F) loop | |
3049 | if Present (Renamed_Object (F)) then | |
4e6768ab | 3050 | |
662c2ad4 | 3051 | -- If expander is active, it is an error to try to inline a |
52c1498c YM |
3052 | -- recursive program. In GNATprove mode, just indicate that the |
3053 | -- inlining will not happen, and mark the subprogram as not always | |
3054 | -- inlined. | |
4e6768ab | 3055 | |
4bd4bb7f | 3056 | if GNATprove_Mode then |
4e6768ab AC |
3057 | Cannot_Inline |
3058 | ("cannot inline call to recursive subprogram?", N, Subp); | |
4bd4bb7f AC |
3059 | Set_Is_Inlined_Always (Subp, False); |
3060 | else | |
3061 | Error_Msg_N | |
3062 | ("cannot inline call to recursive subprogram", N); | |
4e6768ab AC |
3063 | end if; |
3064 | ||
540d8610 ES |
3065 | return; |
3066 | end if; | |
3067 | ||
3068 | -- Reset Last_Assignment for any parameters of mode out or in out, to | |
3069 | -- prevent spurious warnings about overwriting for assignments to the | |
3070 | -- formal in the inlined code. | |
3071 | ||
3072 | if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then | |
3073 | Set_Last_Assignment (Entity (A), Empty); | |
3074 | end if; | |
3075 | ||
3076 | -- If the argument may be a controlling argument in a call within | |
3077 | -- the inlined body, we must preserve its classwide nature to insure | |
3078 | -- that dynamic dispatching take place subsequently. If the formal | |
3079 | -- has a constraint it must be preserved to retain the semantics of | |
3080 | -- the body. | |
3081 | ||
3082 | if Is_Class_Wide_Type (Etype (F)) | |
3083 | or else (Is_Access_Type (Etype (F)) | |
3084 | and then Is_Class_Wide_Type (Designated_Type (Etype (F)))) | |
3085 | then | |
3086 | Temp_Typ := Etype (F); | |
3087 | ||
3088 | elsif Base_Type (Etype (F)) = Base_Type (Etype (A)) | |
3089 | and then Etype (F) /= Base_Type (Etype (F)) | |
f4ef7b06 | 3090 | and then Is_Constrained (Etype (F)) |
540d8610 ES |
3091 | then |
3092 | Temp_Typ := Etype (F); | |
f4ef7b06 | 3093 | |
540d8610 ES |
3094 | else |
3095 | Temp_Typ := Etype (A); | |
3096 | end if; | |
3097 | ||
3098 | -- If the actual is a simple name or a literal, no need to | |
3099 | -- create a temporary, object can be used directly. | |
3100 | ||
3101 | -- If the actual is a literal and the formal has its address taken, | |
3102 | -- we cannot pass the literal itself as an argument, so its value | |
3de3a1be YM |
3103 | -- must be captured in a temporary. Skip this optimization in |
3104 | -- GNATprove mode, to make sure any check on a type conversion | |
3105 | -- will be issued. | |
540d8610 ES |
3106 | |
3107 | if (Is_Entity_Name (A) | |
3108 | and then | |
da9683f4 AC |
3109 | (not Is_Scalar_Type (Etype (A)) |
3110 | or else Ekind (Entity (A)) = E_Enumeration_Literal) | |
3de3a1be | 3111 | and then not GNATprove_Mode) |
540d8610 ES |
3112 | |
3113 | -- When the actual is an identifier and the corresponding formal is | |
3114 | -- used only once in the original body, the formal can be substituted | |
3de3a1be YM |
3115 | -- directly with the actual parameter. Skip this optimization in |
3116 | -- GNATprove mode, to make sure any check on a type conversion | |
3117 | -- will be issued. | |
540d8610 | 3118 | |
da9683f4 AC |
3119 | or else |
3120 | (Nkind (A) = N_Identifier | |
3121 | and then Formal_Is_Used_Once (F) | |
3122 | and then not GNATprove_Mode) | |
540d8610 ES |
3123 | |
3124 | or else | |
3125 | (Nkind_In (A, N_Real_Literal, | |
3126 | N_Integer_Literal, | |
3127 | N_Character_Literal) | |
3128 | and then not Address_Taken (F)) | |
3129 | then | |
3130 | if Etype (F) /= Etype (A) then | |
3131 | Set_Renamed_Object | |
3132 | (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A))); | |
3133 | else | |
3134 | Set_Renamed_Object (F, A); | |
3135 | end if; | |
3136 | ||
3137 | else | |
3138 | Temp := Make_Temporary (Loc, 'C'); | |
3139 | ||
3140 | -- If the actual for an in/in-out parameter is a view conversion, | |
3141 | -- make it into an unchecked conversion, given that an untagged | |
3142 | -- type conversion is not a proper object for a renaming. | |
3143 | ||
3144 | -- In-out conversions that involve real conversions have already | |
3145 | -- been transformed in Expand_Actuals. | |
3146 | ||
3147 | if Nkind (A) = N_Type_Conversion | |
3148 | and then Ekind (F) /= E_In_Parameter | |
3149 | then | |
3150 | New_A := | |
3151 | Make_Unchecked_Type_Conversion (Loc, | |
3152 | Subtype_Mark => New_Occurrence_Of (Etype (F), Loc), | |
3153 | Expression => Relocate_Node (Expression (A))); | |
3154 | ||
f4ef7b06 AC |
3155 | -- In GNATprove mode, keep the most precise type of the actual |
3156 | -- for the temporary variable. Otherwise, the AST may contain | |
3157 | -- unexpected assignment statements to a temporary variable of | |
3158 | -- unconstrained type renaming a local variable of constrained | |
3159 | -- type, which is not expected by GNATprove. | |
3160 | ||
4f324de2 AC |
3161 | elsif Etype (F) /= Etype (A) and then not GNATprove_Mode then |
3162 | New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A)); | |
540d8610 ES |
3163 | Temp_Typ := Etype (F); |
3164 | ||
3165 | else | |
3166 | New_A := Relocate_Node (A); | |
3167 | end if; | |
3168 | ||
3169 | Set_Sloc (New_A, Sloc (N)); | |
3170 | ||
3171 | -- If the actual has a by-reference type, it cannot be copied, | |
3172 | -- so its value is captured in a renaming declaration. Otherwise | |
3173 | -- declare a local constant initialized with the actual. | |
3174 | ||
3175 | -- We also use a renaming declaration for expressions of an array | |
3176 | -- type that is not bit-packed, both for efficiency reasons and to | |
3177 | -- respect the semantics of the call: in most cases the original | |
3178 | -- call will pass the parameter by reference, and thus the inlined | |
3179 | -- code will have the same semantics. | |
3180 | ||
36428cc4 AC |
3181 | -- Finally, we need a renaming declaration in the case of limited |
3182 | -- types for which initialization cannot be by copy either. | |
3183 | ||
540d8610 ES |
3184 | if Ekind (F) = E_In_Parameter |
3185 | and then not Is_By_Reference_Type (Etype (A)) | |
36428cc4 | 3186 | and then not Is_Limited_Type (Etype (A)) |
540d8610 ES |
3187 | and then |
3188 | (not Is_Array_Type (Etype (A)) | |
3189 | or else not Is_Object_Reference (A) | |
3190 | or else Is_Bit_Packed_Array (Etype (A))) | |
3191 | then | |
3192 | Decl := | |
3193 | Make_Object_Declaration (Loc, | |
3194 | Defining_Identifier => Temp, | |
3195 | Constant_Present => True, | |
3196 | Object_Definition => New_Occurrence_Of (Temp_Typ, Loc), | |
3197 | Expression => New_A); | |
3de3a1be | 3198 | |
540d8610 | 3199 | else |
3de3a1be YM |
3200 | -- In GNATprove mode, make an explicit copy of input |
3201 | -- parameters when formal and actual types differ, to make | |
3202 | -- sure any check on the type conversion will be issued. | |
3203 | -- The legality of the copy is ensured by calling first | |
3204 | -- Call_Can_Be_Inlined_In_GNATprove_Mode. | |
3205 | ||
3206 | if GNATprove_Mode | |
3207 | and then Ekind (F) /= E_Out_Parameter | |
3208 | and then not Same_Type (Etype (F), Etype (A)) | |
3209 | then | |
3210 | pragma Assert (not (Is_By_Reference_Type (Etype (A)))); | |
3211 | pragma Assert (not (Is_Limited_Type (Etype (A)))); | |
72cdccfa | 3212 | |
3abbc5c2 | 3213 | Append_To (Decls, |
3de3a1be | 3214 | Make_Object_Declaration (Loc, |
3abbc5c2 | 3215 | Defining_Identifier => Make_Temporary (Loc, 'C'), |
3de3a1be YM |
3216 | Constant_Present => True, |
3217 | Object_Definition => New_Occurrence_Of (Temp_Typ, Loc), | |
3abbc5c2 | 3218 | Expression => New_Copy_Tree (New_A))); |
3de3a1be YM |
3219 | end if; |
3220 | ||
540d8610 ES |
3221 | Decl := |
3222 | Make_Object_Renaming_Declaration (Loc, | |
3223 | Defining_Identifier => Temp, | |
3224 | Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc), | |
3225 | Name => New_A); | |
3226 | end if; | |
3227 | ||
3228 | Append (Decl, Decls); | |
3229 | Set_Renamed_Object (F, Temp); | |
3230 | end if; | |
3231 | ||
3232 | Next_Formal (F); | |
3233 | Next_Actual (A); | |
3234 | end loop; | |
3235 | ||
3236 | -- Establish target of function call. If context is not assignment or | |
3237 | -- declaration, create a temporary as a target. The declaration for the | |
3238 | -- temporary may be subsequently optimized away if the body is a single | |
3239 | -- expression, or if the left-hand side of the assignment is simple | |
3240 | -- enough, i.e. an entity or an explicit dereference of one. | |
3241 | ||
3242 | if Ekind (Subp) = E_Function then | |
3243 | if Nkind (Parent (N)) = N_Assignment_Statement | |
3244 | and then Is_Entity_Name (Name (Parent (N))) | |
3245 | then | |
3246 | Targ := Name (Parent (N)); | |
3247 | ||
3248 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
3249 | and then Nkind (Name (Parent (N))) = N_Explicit_Dereference | |
3250 | and then Is_Entity_Name (Prefix (Name (Parent (N)))) | |
3251 | then | |
3252 | Targ := Name (Parent (N)); | |
3253 | ||
3254 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
3255 | and then Nkind (Name (Parent (N))) = N_Selected_Component | |
3256 | and then Is_Entity_Name (Prefix (Name (Parent (N)))) | |
3257 | then | |
3258 | Targ := New_Copy_Tree (Name (Parent (N))); | |
3259 | ||
3260 | elsif Nkind (Parent (N)) = N_Object_Declaration | |
3261 | and then Is_Limited_Type (Etype (Subp)) | |
3262 | then | |
3263 | Targ := Defining_Identifier (Parent (N)); | |
3264 | ||
3265 | -- New semantics: In an object declaration avoid an extra copy | |
3266 | -- of the result of a call to an inlined function that returns | |
3267 | -- an unconstrained type | |
3268 | ||
6c26bac2 | 3269 | elsif Back_End_Inlining |
540d8610 ES |
3270 | and then Nkind (Parent (N)) = N_Object_Declaration |
3271 | and then Is_Unc | |
3272 | then | |
3273 | Targ := Defining_Identifier (Parent (N)); | |
3274 | ||
3275 | else | |
3276 | -- Replace call with temporary and create its declaration | |
3277 | ||
3278 | Temp := Make_Temporary (Loc, 'C'); | |
3279 | Set_Is_Internal (Temp); | |
3280 | ||
3281 | -- For the unconstrained case, the generated temporary has the | |
3282 | -- same constrained declaration as the result variable. It may | |
3283 | -- eventually be possible to remove that temporary and use the | |
3284 | -- result variable directly. | |
3285 | ||
3f80a182 | 3286 | if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement |
540d8610 ES |
3287 | then |
3288 | Decl := | |
3289 | Make_Object_Declaration (Loc, | |
3290 | Defining_Identifier => Temp, | |
3291 | Object_Definition => | |
3292 | New_Copy_Tree (Object_Definition (Parent (Targ1)))); | |
3293 | ||
3294 | Replace_Formals (Decl); | |
3295 | ||
3296 | else | |
3297 | Decl := | |
3298 | Make_Object_Declaration (Loc, | |
3299 | Defining_Identifier => Temp, | |
3300 | Object_Definition => New_Occurrence_Of (Ret_Type, Loc)); | |
3301 | ||
3302 | Set_Etype (Temp, Ret_Type); | |
3303 | end if; | |
3304 | ||
3305 | Set_No_Initialization (Decl); | |
3306 | Append (Decl, Decls); | |
3307 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
3308 | Targ := Temp; | |
3309 | end if; | |
3310 | end if; | |
3311 | ||
3312 | Insert_Actions (N, Decls); | |
3313 | ||
3314 | if Is_Unc_Decl then | |
3315 | ||
3316 | -- Special management for inlining a call to a function that returns | |
3317 | -- an unconstrained type and initializes an object declaration: we | |
3318 | -- avoid generating undesired extra calls and goto statements. | |
3319 | ||
3320 | -- Given: | |
3321 | -- function Func (...) return ... | |
3322 | -- begin | |
3323 | -- declare | |
3324 | -- Result : String (1 .. 4); | |
3325 | -- begin | |
3326 | -- Proc (Result, ...); | |
3327 | -- return Result; | |
3328 | -- end; | |
3329 | -- end F; | |
3330 | ||
3331 | -- Result : String := Func (...); | |
3332 | ||
3333 | -- Replace this object declaration by: | |
3334 | ||
3335 | -- Result : String (1 .. 4); | |
3336 | -- Proc (Result, ...); | |
3337 | ||
3338 | Remove_Homonym (Targ); | |
3339 | ||
3340 | Decl := | |
3341 | Make_Object_Declaration | |
3342 | (Loc, | |
3343 | Defining_Identifier => Targ, | |
3344 | Object_Definition => | |
3345 | New_Copy_Tree (Object_Definition (Parent (Targ1)))); | |
3346 | Replace_Formals (Decl); | |
3347 | Rewrite (Parent (N), Decl); | |
3348 | Analyze (Parent (N)); | |
3349 | ||
3350 | -- Avoid spurious warnings since we know that this declaration is | |
3351 | -- referenced by the procedure call. | |
3352 | ||
3353 | Set_Never_Set_In_Source (Targ, False); | |
3354 | ||
3355 | -- Remove the local declaration of the extended return stmt from the | |
3356 | -- inlined code | |
3357 | ||
3358 | Remove (Parent (Targ1)); | |
3359 | ||
3360 | -- Update the reference to the result (since we have rewriten the | |
3361 | -- object declaration) | |
3362 | ||
3363 | declare | |
3364 | Blk_Call_Stmt : Node_Id; | |
3365 | ||
3366 | begin | |
3367 | -- Capture the call to the procedure | |
3368 | ||
3369 | Blk_Call_Stmt := | |
3370 | First (Statements (Handled_Statement_Sequence (Blk))); | |
3371 | pragma Assert | |
3372 | (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement); | |
3373 | ||
3374 | Remove (First (Parameter_Associations (Blk_Call_Stmt))); | |
3375 | Prepend_To (Parameter_Associations (Blk_Call_Stmt), | |
3376 | New_Occurrence_Of (Targ, Loc)); | |
3377 | end; | |
3378 | ||
3379 | -- Remove the return statement | |
3380 | ||
3381 | pragma Assert | |
3382 | (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) = | |
3383 | N_Simple_Return_Statement); | |
3384 | ||
3385 | Remove (Last (Statements (Handled_Statement_Sequence (Blk)))); | |
3386 | end if; | |
3387 | ||
3388 | -- Traverse the tree and replace formals with actuals or their thunks. | |
3389 | -- Attach block to tree before analysis and rewriting. | |
3390 | ||
3391 | Replace_Formals (Blk); | |
3392 | Set_Parent (Blk, N); | |
3393 | ||
e5c4e2bc AC |
3394 | if GNATprove_Mode then |
3395 | null; | |
3396 | ||
3397 | elsif not Comes_From_Source (Subp) or else Is_Predef then | |
540d8610 ES |
3398 | Reset_Slocs (Blk); |
3399 | end if; | |
3400 | ||
3401 | if Is_Unc_Decl then | |
3402 | ||
3403 | -- No action needed since return statement has been already removed | |
3404 | ||
3405 | null; | |
3406 | ||
3407 | elsif Present (Exit_Lab) then | |
3408 | ||
fae8eb5b GD |
3409 | -- If there's a single return statement at the end of the subprogram, |
3410 | -- the corresponding goto statement and the corresponding label are | |
3411 | -- useless. | |
540d8610 ES |
3412 | |
3413 | if Num_Ret = 1 | |
3414 | and then | |
3415 | Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) = | |
3416 | N_Goto_Statement | |
3417 | then | |
3418 | Remove (Last (Statements (Handled_Statement_Sequence (Blk)))); | |
3419 | else | |
3420 | Append (Lab_Decl, (Declarations (Blk))); | |
3421 | Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk))); | |
3422 | end if; | |
3423 | end if; | |
3424 | ||
3425 | -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors | |
3426 | -- on conflicting private views that Gigi would ignore. If this is a | |
3427 | -- predefined unit, analyze with checks off, as is done in the non- | |
3428 | -- inlined run-time units. | |
3429 | ||
3430 | declare | |
3431 | I_Flag : constant Boolean := In_Inlined_Body; | |
3432 | ||
3433 | begin | |
3434 | In_Inlined_Body := True; | |
3435 | ||
3436 | if Is_Predef then | |
3437 | declare | |
3438 | Style : constant Boolean := Style_Check; | |
3439 | ||
3440 | begin | |
3441 | Style_Check := False; | |
3442 | ||
3443 | -- Search for dispatching calls that use the Object.Operation | |
3444 | -- notation using an Object that is a parameter of the inlined | |
3445 | -- function. We reset the decoration of Operation to force | |
3446 | -- the reanalysis of the inlined dispatching call because | |
3447 | -- the actual object has been inlined. | |
3448 | ||
3449 | Reset_Dispatching_Calls (Blk); | |
3450 | ||
3451 | Analyze (Blk, Suppress => All_Checks); | |
3452 | Style_Check := Style; | |
3453 | end; | |
3454 | ||
3455 | else | |
3456 | Analyze (Blk); | |
3457 | end if; | |
3458 | ||
3459 | In_Inlined_Body := I_Flag; | |
3460 | end; | |
3461 | ||
3462 | if Ekind (Subp) = E_Procedure then | |
3463 | Rewrite_Procedure_Call (N, Blk); | |
3464 | ||
3465 | else | |
3466 | Rewrite_Function_Call (N, Blk); | |
3467 | ||
3468 | if Is_Unc_Decl then | |
3469 | null; | |
3470 | ||
3471 | -- For the unconstrained case, the replacement of the call has been | |
3472 | -- made prior to the complete analysis of the generated declarations. | |
3473 | -- Propagate the proper type now. | |
3474 | ||
3475 | elsif Is_Unc then | |
3476 | if Nkind (N) = N_Identifier then | |
3477 | Set_Etype (N, Etype (Entity (N))); | |
3478 | else | |
3479 | Set_Etype (N, Etype (Targ1)); | |
3480 | end if; | |
3481 | end if; | |
3482 | end if; | |
3483 | ||
3484 | Restore_Env; | |
3485 | ||
3486 | -- Cleanup mapping between formals and actuals for other expansions | |
3487 | ||
3488 | F := First_Formal (Subp); | |
3489 | while Present (F) loop | |
3490 | Set_Renamed_Object (F, Empty); | |
3491 | Next_Formal (F); | |
3492 | end loop; | |
3493 | end Expand_Inlined_Call; | |
3f80a182 | 3494 | |
70c34e1c AC |
3495 | -------------------------- |
3496 | -- Get_Code_Unit_Entity -- | |
3497 | -------------------------- | |
3498 | ||
3499 | function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is | |
8a49a499 | 3500 | Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E)); |
5b5b27ad | 3501 | |
70c34e1c | 3502 | begin |
8a49a499 AC |
3503 | if Ekind (Unit) = E_Package_Body then |
3504 | Unit := Spec_Entity (Unit); | |
3505 | end if; | |
5b5b27ad | 3506 | |
8a49a499 | 3507 | return Unit; |
70c34e1c AC |
3508 | end Get_Code_Unit_Entity; |
3509 | ||
6c26bac2 AC |
3510 | ------------------------------ |
3511 | -- Has_Excluded_Declaration -- | |
3512 | ------------------------------ | |
3513 | ||
3514 | function Has_Excluded_Declaration | |
3515 | (Subp : Entity_Id; | |
3516 | Decls : List_Id) return Boolean | |
3517 | is | |
3518 | D : Node_Id; | |
3519 | ||
3520 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean; | |
3521 | -- Nested subprograms make a given body ineligible for inlining, but | |
3522 | -- we make an exception for instantiations of unchecked conversion. | |
3523 | -- The body has not been analyzed yet, so check the name, and verify | |
3524 | -- that the visible entity with that name is the predefined unit. | |
3525 | ||
3526 | ----------------------------- | |
3527 | -- Is_Unchecked_Conversion -- | |
3528 | ----------------------------- | |
3529 | ||
3530 | function Is_Unchecked_Conversion (D : Node_Id) return Boolean is | |
3531 | Id : constant Node_Id := Name (D); | |
3532 | Conv : Entity_Id; | |
3533 | ||
3534 | begin | |
3535 | if Nkind (Id) = N_Identifier | |
3536 | and then Chars (Id) = Name_Unchecked_Conversion | |
3537 | then | |
3538 | Conv := Current_Entity (Id); | |
3539 | ||
3540 | elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name) | |
3541 | and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion | |
3542 | then | |
3543 | Conv := Current_Entity (Selector_Name (Id)); | |
3544 | else | |
3545 | return False; | |
3546 | end if; | |
3547 | ||
3548 | return Present (Conv) | |
3549 | and then Is_Predefined_File_Name | |
3550 | (Unit_File_Name (Get_Source_Unit (Conv))) | |
3551 | and then Is_Intrinsic_Subprogram (Conv); | |
3552 | end Is_Unchecked_Conversion; | |
3553 | ||
3554 | -- Start of processing for Has_Excluded_Declaration | |
3555 | ||
3556 | begin | |
16b10ccc AC |
3557 | -- No action needed if the check is not needed |
3558 | ||
3559 | if not Check_Inlining_Restrictions then | |
3560 | return False; | |
3561 | end if; | |
3562 | ||
6c26bac2 AC |
3563 | D := First (Decls); |
3564 | while Present (D) loop | |
3c756b76 | 3565 | |
6fd52b78 AC |
3566 | -- First declarations universally excluded |
3567 | ||
3568 | if Nkind (D) = N_Package_Declaration then | |
6c26bac2 | 3569 | Cannot_Inline |
ca7e6c26 | 3570 | ("cannot inline & (nested package declaration)?", D, Subp); |
6fd52b78 AC |
3571 | return True; |
3572 | ||
3573 | elsif Nkind (D) = N_Package_Instantiation then | |
3574 | Cannot_Inline | |
ca7e6c26 | 3575 | ("cannot inline & (nested package instantiation)?", D, Subp); |
6c26bac2 | 3576 | return True; |
6fd52b78 AC |
3577 | end if; |
3578 | ||
3579 | -- Then declarations excluded only for front end inlining | |
3580 | ||
3581 | if Back_End_Inlining then | |
3582 | null; | |
6c26bac2 AC |
3583 | |
3584 | elsif Nkind (D) = N_Task_Type_Declaration | |
3585 | or else Nkind (D) = N_Single_Task_Declaration | |
3586 | then | |
3587 | Cannot_Inline | |
ca7e6c26 | 3588 | ("cannot inline & (nested task type declaration)?", D, Subp); |
6c26bac2 AC |
3589 | return True; |
3590 | ||
3591 | elsif Nkind (D) = N_Protected_Type_Declaration | |
3592 | or else Nkind (D) = N_Single_Protected_Declaration | |
3593 | then | |
3594 | Cannot_Inline | |
3595 | ("cannot inline & (nested protected type declaration)?", | |
3596 | D, Subp); | |
3597 | return True; | |
3598 | ||
6fd52b78 | 3599 | elsif Nkind (D) = N_Subprogram_Body then |
6c26bac2 | 3600 | Cannot_Inline |
ca7e6c26 | 3601 | ("cannot inline & (nested subprogram)?", D, Subp); |
6c26bac2 AC |
3602 | return True; |
3603 | ||
3604 | elsif Nkind (D) = N_Function_Instantiation | |
3605 | and then not Is_Unchecked_Conversion (D) | |
3606 | then | |
3607 | Cannot_Inline | |
ca7e6c26 | 3608 | ("cannot inline & (nested function instantiation)?", D, Subp); |
6c26bac2 AC |
3609 | return True; |
3610 | ||
3611 | elsif Nkind (D) = N_Procedure_Instantiation then | |
3612 | Cannot_Inline | |
ca7e6c26 | 3613 | ("cannot inline & (nested procedure instantiation)?", D, Subp); |
6c26bac2 | 3614 | return True; |
f99ff327 AC |
3615 | |
3616 | -- Subtype declarations with predicates will generate predicate | |
3617 | -- functions, i.e. nested subprogram bodies, so inlining is not | |
3618 | -- possible. | |
3619 | ||
3620 | elsif Nkind (D) = N_Subtype_Declaration | |
3621 | and then Present (Aspect_Specifications (D)) | |
3622 | then | |
3623 | declare | |
3624 | A : Node_Id; | |
3625 | A_Id : Aspect_Id; | |
3626 | ||
3627 | begin | |
3628 | A := First (Aspect_Specifications (D)); | |
3629 | while Present (A) loop | |
3630 | A_Id := Get_Aspect_Id (Chars (Identifier (A))); | |
3631 | ||
3632 | if A_Id = Aspect_Predicate | |
3633 | or else A_Id = Aspect_Static_Predicate | |
3634 | or else A_Id = Aspect_Dynamic_Predicate | |
3635 | then | |
3636 | Cannot_Inline | |
ca7e6c26 AC |
3637 | ("cannot inline & (subtype declaration with " |
3638 | & "predicate)?", D, Subp); | |
f99ff327 AC |
3639 | return True; |
3640 | end if; | |
3641 | ||
3642 | Next (A); | |
3643 | end loop; | |
3644 | end; | |
6c26bac2 AC |
3645 | end if; |
3646 | ||
3647 | Next (D); | |
3648 | end loop; | |
3649 | ||
3650 | return False; | |
3651 | end Has_Excluded_Declaration; | |
3652 | ||
3653 | ---------------------------- | |
3654 | -- Has_Excluded_Statement -- | |
3655 | ---------------------------- | |
3656 | ||
3657 | function Has_Excluded_Statement | |
3658 | (Subp : Entity_Id; | |
3659 | Stats : List_Id) return Boolean | |
3660 | is | |
3661 | S : Node_Id; | |
3662 | E : Node_Id; | |
3663 | ||
3664 | begin | |
16b10ccc AC |
3665 | -- No action needed if the check is not needed |
3666 | ||
3667 | if not Check_Inlining_Restrictions then | |
3668 | return False; | |
3669 | end if; | |
3670 | ||
6c26bac2 AC |
3671 | S := First (Stats); |
3672 | while Present (S) loop | |
3673 | if Nkind_In (S, N_Abort_Statement, | |
3674 | N_Asynchronous_Select, | |
3675 | N_Conditional_Entry_Call, | |
3676 | N_Delay_Relative_Statement, | |
3677 | N_Delay_Until_Statement, | |
3678 | N_Selective_Accept, | |
3679 | N_Timed_Entry_Call) | |
3680 | then | |
3681 | Cannot_Inline | |
3682 | ("cannot inline & (non-allowed statement)?", S, Subp); | |
3683 | return True; | |
3684 | ||
3685 | elsif Nkind (S) = N_Block_Statement then | |
3686 | if Present (Declarations (S)) | |
3687 | and then Has_Excluded_Declaration (Subp, Declarations (S)) | |
3688 | then | |
3689 | return True; | |
3690 | ||
3691 | elsif Present (Handled_Statement_Sequence (S)) then | |
16b10ccc AC |
3692 | if not Back_End_Inlining |
3693 | and then | |
3694 | Present | |
3695 | (Exception_Handlers (Handled_Statement_Sequence (S))) | |
6c26bac2 AC |
3696 | then |
3697 | Cannot_Inline | |
3698 | ("cannot inline& (exception handler)?", | |
3699 | First (Exception_Handlers | |
3700 | (Handled_Statement_Sequence (S))), | |
3701 | Subp); | |
3702 | return True; | |
3703 | ||
3704 | elsif Has_Excluded_Statement | |
3705 | (Subp, Statements (Handled_Statement_Sequence (S))) | |
3706 | then | |
3707 | return True; | |
3708 | end if; | |
3709 | end if; | |
3710 | ||
3711 | elsif Nkind (S) = N_Case_Statement then | |
3712 | E := First (Alternatives (S)); | |
3713 | while Present (E) loop | |
3714 | if Has_Excluded_Statement (Subp, Statements (E)) then | |
3715 | return True; | |
3716 | end if; | |
3717 | ||
3718 | Next (E); | |
3719 | end loop; | |
3720 | ||
3721 | elsif Nkind (S) = N_If_Statement then | |
3722 | if Has_Excluded_Statement (Subp, Then_Statements (S)) then | |
3723 | return True; | |
3724 | end if; | |
3725 | ||
3726 | if Present (Elsif_Parts (S)) then | |
3727 | E := First (Elsif_Parts (S)); | |
3728 | while Present (E) loop | |
3729 | if Has_Excluded_Statement (Subp, Then_Statements (E)) then | |
3730 | return True; | |
3731 | end if; | |
3732 | ||
3733 | Next (E); | |
3734 | end loop; | |
3735 | end if; | |
3736 | ||
3737 | if Present (Else_Statements (S)) | |
3738 | and then Has_Excluded_Statement (Subp, Else_Statements (S)) | |
3739 | then | |
3740 | return True; | |
3741 | end if; | |
3742 | ||
3743 | elsif Nkind (S) = N_Loop_Statement | |
3744 | and then Has_Excluded_Statement (Subp, Statements (S)) | |
3745 | then | |
3746 | return True; | |
3747 | ||
3748 | elsif Nkind (S) = N_Extended_Return_Statement then | |
3749 | if Present (Handled_Statement_Sequence (S)) | |
3750 | and then | |
3751 | Has_Excluded_Statement | |
3752 | (Subp, Statements (Handled_Statement_Sequence (S))) | |
3753 | then | |
3754 | return True; | |
3755 | ||
16b10ccc AC |
3756 | elsif not Back_End_Inlining |
3757 | and then Present (Handled_Statement_Sequence (S)) | |
6c26bac2 AC |
3758 | and then |
3759 | Present (Exception_Handlers | |
3760 | (Handled_Statement_Sequence (S))) | |
3761 | then | |
3762 | Cannot_Inline | |
3763 | ("cannot inline& (exception handler)?", | |
3764 | First (Exception_Handlers (Handled_Statement_Sequence (S))), | |
3765 | Subp); | |
3766 | return True; | |
3767 | end if; | |
3768 | end if; | |
3769 | ||
3770 | Next (S); | |
3771 | end loop; | |
3772 | ||
3773 | return False; | |
3774 | end Has_Excluded_Statement; | |
3775 | ||
38cbfe40 RK |
3776 | -------------------------- |
3777 | -- Has_Initialized_Type -- | |
3778 | -------------------------- | |
3779 | ||
3780 | function Has_Initialized_Type (E : Entity_Id) return Boolean is | |
90a4b336 | 3781 | E_Body : constant Node_Id := Subprogram_Body (E); |
38cbfe40 RK |
3782 | Decl : Node_Id; |
3783 | ||
3784 | begin | |
3785 | if No (E_Body) then -- imported subprogram | |
3786 | return False; | |
3787 | ||
3788 | else | |
3789 | Decl := First (Declarations (E_Body)); | |
38cbfe40 | 3790 | while Present (Decl) loop |
38cbfe40 RK |
3791 | if Nkind (Decl) = N_Full_Type_Declaration |
3792 | and then Present (Init_Proc (Defining_Identifier (Decl))) | |
3793 | then | |
3794 | return True; | |
3795 | end if; | |
3796 | ||
3797 | Next (Decl); | |
3798 | end loop; | |
3799 | end if; | |
3800 | ||
3801 | return False; | |
3802 | end Has_Initialized_Type; | |
3803 | ||
ea0c8cfb RD |
3804 | ----------------------- |
3805 | -- Has_Single_Return -- | |
3806 | ----------------------- | |
6c26bac2 AC |
3807 | |
3808 | function Has_Single_Return (N : Node_Id) return Boolean is | |
3809 | Return_Statement : Node_Id := Empty; | |
3810 | ||
3811 | function Check_Return (N : Node_Id) return Traverse_Result; | |
3812 | ||
3813 | ------------------ | |
3814 | -- Check_Return -- | |
3815 | ------------------ | |
3816 | ||
3817 | function Check_Return (N : Node_Id) return Traverse_Result is | |
3818 | begin | |
3819 | if Nkind (N) = N_Simple_Return_Statement then | |
3820 | if Present (Expression (N)) | |
3821 | and then Is_Entity_Name (Expression (N)) | |
3822 | then | |
3823 | if No (Return_Statement) then | |
3824 | Return_Statement := N; | |
3825 | return OK; | |
3826 | ||
3827 | elsif Chars (Expression (N)) = | |
3828 | Chars (Expression (Return_Statement)) | |
3829 | then | |
3830 | return OK; | |
3831 | ||
3832 | else | |
3833 | return Abandon; | |
3834 | end if; | |
3835 | ||
3836 | -- A return statement within an extended return is a noop | |
3837 | -- after inlining. | |
3838 | ||
3839 | elsif No (Expression (N)) | |
3840 | and then | |
3841 | Nkind (Parent (Parent (N))) = N_Extended_Return_Statement | |
3842 | then | |
3843 | return OK; | |
3844 | ||
3845 | else | |
3846 | -- Expression has wrong form | |
3847 | ||
3848 | return Abandon; | |
3849 | end if; | |
3850 | ||
ea0c8cfb RD |
3851 | -- We can only inline a build-in-place function if it has a single |
3852 | -- extended return. | |
6c26bac2 AC |
3853 | |
3854 | elsif Nkind (N) = N_Extended_Return_Statement then | |
3855 | if No (Return_Statement) then | |
3856 | Return_Statement := N; | |
3857 | return OK; | |
3858 | ||
3859 | else | |
3860 | return Abandon; | |
3861 | end if; | |
3862 | ||
3863 | else | |
3864 | return OK; | |
3865 | end if; | |
3866 | end Check_Return; | |
3867 | ||
3868 | function Check_All_Returns is new Traverse_Func (Check_Return); | |
3869 | ||
3870 | -- Start of processing for Has_Single_Return | |
3871 | ||
3872 | begin | |
3873 | if Check_All_Returns (N) /= OK then | |
3874 | return False; | |
3875 | ||
3876 | elsif Nkind (Return_Statement) = N_Extended_Return_Statement then | |
3877 | return True; | |
3878 | ||
3879 | else | |
3880 | return Present (Declarations (N)) | |
3881 | and then Present (First (Declarations (N))) | |
3882 | and then Chars (Expression (Return_Statement)) = | |
3883 | Chars (Defining_Identifier (First (Declarations (N)))); | |
3884 | end if; | |
3885 | end Has_Single_Return; | |
3886 | ||
5b5b27ad AC |
3887 | ----------------------------- |
3888 | -- In_Main_Unit_Or_Subunit -- | |
3889 | ----------------------------- | |
3890 | ||
3891 | function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is | |
3892 | Comp : Node_Id := Cunit (Get_Code_Unit (E)); | |
3893 | ||
3894 | begin | |
3895 | -- Check whether the subprogram or package to inline is within the main | |
3896 | -- unit or its spec or within a subunit. In either case there are no | |
3897 | -- additional bodies to process. If the subprogram appears in a parent | |
3898 | -- of the current unit, the check on whether inlining is possible is | |
3899 | -- done in Analyze_Inlined_Bodies. | |
3900 | ||
3901 | while Nkind (Unit (Comp)) = N_Subunit loop | |
3902 | Comp := Library_Unit (Comp); | |
3903 | end loop; | |
3904 | ||
3905 | return Comp = Cunit (Main_Unit) | |
3906 | or else Comp = Library_Unit (Cunit (Main_Unit)); | |
3907 | end In_Main_Unit_Or_Subunit; | |
3908 | ||
38cbfe40 RK |
3909 | ---------------- |
3910 | -- Initialize -- | |
3911 | ---------------- | |
3912 | ||
3913 | procedure Initialize is | |
3914 | begin | |
38cbfe40 RK |
3915 | Pending_Descriptor.Init; |
3916 | Pending_Instantiations.Init; | |
3917 | Inlined_Bodies.Init; | |
3918 | Successors.Init; | |
3919 | Inlined.Init; | |
3920 | ||
3921 | for J in Hash_Headers'Range loop | |
3922 | Hash_Headers (J) := No_Subp; | |
3923 | end loop; | |
16b10ccc AC |
3924 | |
3925 | Inlined_Calls := No_Elist; | |
3926 | Backend_Calls := No_Elist; | |
3927 | Backend_Inlined_Subps := No_Elist; | |
3928 | Backend_Not_Inlined_Subps := No_Elist; | |
38cbfe40 RK |
3929 | end Initialize; |
3930 | ||
3931 | ------------------------ | |
3932 | -- Instantiate_Bodies -- | |
3933 | ------------------------ | |
3934 | ||
3935 | -- Generic bodies contain all the non-local references, so an | |
3936 | -- instantiation does not need any more context than Standard | |
3937 | -- itself, even if the instantiation appears in an inner scope. | |
3938 | -- Generic associations have verified that the contract model is | |
3939 | -- satisfied, so that any error that may occur in the analysis of | |
3940 | -- the body is an internal error. | |
3941 | ||
3942 | procedure Instantiate_Bodies is | |
3bb91f98 | 3943 | J : Nat; |
38cbfe40 RK |
3944 | Info : Pending_Body_Info; |
3945 | ||
3946 | begin | |
07fc65c4 | 3947 | if Serious_Errors_Detected = 0 then |
fbf5a39b | 3948 | Expander_Active := (Operating_Mode = Opt.Generate_Code); |
a99ada67 | 3949 | Push_Scope (Standard_Standard); |
38cbfe40 RK |
3950 | To_Clean := New_Elmt_List; |
3951 | ||
3952 | if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then | |
3953 | Start_Generic; | |
3954 | end if; | |
3955 | ||
3956 | -- A body instantiation may generate additional instantiations, so | |
3957 | -- the following loop must scan to the end of a possibly expanding | |
3958 | -- set (that's why we can't simply use a FOR loop here). | |
3959 | ||
3960 | J := 0; | |
38cbfe40 | 3961 | while J <= Pending_Instantiations.Last |
07fc65c4 | 3962 | and then Serious_Errors_Detected = 0 |
38cbfe40 | 3963 | loop |
38cbfe40 RK |
3964 | Info := Pending_Instantiations.Table (J); |
3965 | ||
fbf5a39b | 3966 | -- If the instantiation node is absent, it has been removed |
38cbfe40 RK |
3967 | -- as part of unreachable code. |
3968 | ||
3969 | if No (Info.Inst_Node) then | |
3970 | null; | |
3971 | ||
fbf5a39b | 3972 | elsif Nkind (Info.Act_Decl) = N_Package_Declaration then |
38cbfe40 RK |
3973 | Instantiate_Package_Body (Info); |
3974 | Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl)); | |
3975 | ||
3976 | else | |
3977 | Instantiate_Subprogram_Body (Info); | |
3978 | end if; | |
3979 | ||
3980 | J := J + 1; | |
3981 | end loop; | |
3982 | ||
3983 | -- Reset the table of instantiations. Additional instantiations | |
3984 | -- may be added through inlining, when additional bodies are | |
3985 | -- analyzed. | |
3986 | ||
3987 | Pending_Instantiations.Init; | |
3988 | ||
3989 | -- We can now complete the cleanup actions of scopes that contain | |
3990 | -- pending instantiations (skipped for generic units, since we | |
3991 | -- never need any cleanups in generic units). | |
38cbfe40 RK |
3992 | |
3993 | if Expander_Active | |
3994 | and then not Is_Generic_Unit (Main_Unit_Entity) | |
3995 | then | |
3996 | Cleanup_Scopes; | |
38cbfe40 RK |
3997 | elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then |
3998 | End_Generic; | |
3999 | end if; | |
4000 | ||
4001 | Pop_Scope; | |
4002 | end if; | |
4003 | end Instantiate_Bodies; | |
4004 | ||
4005 | --------------- | |
4006 | -- Is_Nested -- | |
4007 | --------------- | |
4008 | ||
4009 | function Is_Nested (E : Entity_Id) return Boolean is | |
5132708f | 4010 | Scop : Entity_Id; |
38cbfe40 RK |
4011 | |
4012 | begin | |
5132708f | 4013 | Scop := Scope (E); |
38cbfe40 RK |
4014 | while Scop /= Standard_Standard loop |
4015 | if Ekind (Scop) in Subprogram_Kind then | |
4016 | return True; | |
4017 | ||
4018 | elsif Ekind (Scop) = E_Task_Type | |
4019 | or else Ekind (Scop) = E_Entry | |
0b7f0f0e AC |
4020 | or else Ekind (Scop) = E_Entry_Family |
4021 | then | |
38cbfe40 RK |
4022 | return True; |
4023 | end if; | |
4024 | ||
4025 | Scop := Scope (Scop); | |
4026 | end loop; | |
4027 | ||
4028 | return False; | |
4029 | end Is_Nested; | |
4030 | ||
16b10ccc AC |
4031 | ------------------------ |
4032 | -- List_Inlining_Info -- | |
4033 | ------------------------ | |
4034 | ||
4035 | procedure List_Inlining_Info is | |
4036 | Elmt : Elmt_Id; | |
4037 | Nod : Node_Id; | |
4038 | Count : Nat; | |
4039 | ||
4040 | begin | |
4041 | if not Debug_Flag_Dot_J then | |
4042 | return; | |
4043 | end if; | |
4044 | ||
4045 | -- Generate listing of calls inlined by the frontend | |
4046 | ||
4047 | if Present (Inlined_Calls) then | |
4048 | Count := 0; | |
4049 | Elmt := First_Elmt (Inlined_Calls); | |
4050 | while Present (Elmt) loop | |
4051 | Nod := Node (Elmt); | |
4052 | ||
4053 | if In_Extended_Main_Code_Unit (Nod) then | |
4054 | Count := Count + 1; | |
4055 | ||
4056 | if Count = 1 then | |
1725676d | 4057 | Write_Str ("List of calls inlined by the frontend"); |
16b10ccc AC |
4058 | Write_Eol; |
4059 | end if; | |
4060 | ||
4061 | Write_Str (" "); | |
4062 | Write_Int (Count); | |
4063 | Write_Str (":"); | |
4064 | Write_Location (Sloc (Nod)); | |
4065 | Write_Str (":"); | |
4066 | Output.Write_Eol; | |
4067 | end if; | |
4068 | ||
4069 | Next_Elmt (Elmt); | |
4070 | end loop; | |
4071 | end if; | |
4072 | ||
4073 | -- Generate listing of calls passed to the backend | |
4074 | ||
4075 | if Present (Backend_Calls) then | |
4076 | Count := 0; | |
4077 | ||
4078 | Elmt := First_Elmt (Backend_Calls); | |
4079 | while Present (Elmt) loop | |
4080 | Nod := Node (Elmt); | |
4081 | ||
4082 | if In_Extended_Main_Code_Unit (Nod) then | |
4083 | Count := Count + 1; | |
4084 | ||
4085 | if Count = 1 then | |
1725676d | 4086 | Write_Str ("List of inlined calls passed to the backend"); |
16b10ccc AC |
4087 | Write_Eol; |
4088 | end if; | |
4089 | ||
4090 | Write_Str (" "); | |
4091 | Write_Int (Count); | |
4092 | Write_Str (":"); | |
4093 | Write_Location (Sloc (Nod)); | |
4094 | Output.Write_Eol; | |
4095 | end if; | |
4096 | ||
4097 | Next_Elmt (Elmt); | |
4098 | end loop; | |
4099 | end if; | |
4100 | ||
4101 | -- Generate listing of subprograms passed to the backend | |
4102 | ||
62a64085 | 4103 | if Present (Backend_Inlined_Subps) and then Back_End_Inlining then |
16b10ccc AC |
4104 | Count := 0; |
4105 | ||
4106 | Elmt := First_Elmt (Backend_Inlined_Subps); | |
4107 | while Present (Elmt) loop | |
4108 | Nod := Node (Elmt); | |
4109 | ||
4110 | Count := Count + 1; | |
4111 | ||
4112 | if Count = 1 then | |
4113 | Write_Str | |
1725676d | 4114 | ("List of inlined subprograms passed to the backend"); |
16b10ccc AC |
4115 | Write_Eol; |
4116 | end if; | |
4117 | ||
4118 | Write_Str (" "); | |
4119 | Write_Int (Count); | |
4120 | Write_Str (":"); | |
4121 | Write_Name (Chars (Nod)); | |
4122 | Write_Str (" ("); | |
4123 | Write_Location (Sloc (Nod)); | |
4124 | Write_Str (")"); | |
4125 | Output.Write_Eol; | |
4126 | ||
4127 | Next_Elmt (Elmt); | |
4128 | end loop; | |
4129 | end if; | |
4130 | ||
1725676d | 4131 | -- Generate listing of subprograms that cannot be inlined by the backend |
16b10ccc | 4132 | |
62a64085 | 4133 | if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then |
16b10ccc AC |
4134 | Count := 0; |
4135 | ||
4136 | Elmt := First_Elmt (Backend_Not_Inlined_Subps); | |
4137 | while Present (Elmt) loop | |
4138 | Nod := Node (Elmt); | |
4139 | ||
4140 | Count := Count + 1; | |
4141 | ||
4142 | if Count = 1 then | |
4143 | Write_Str | |
1725676d | 4144 | ("List of subprograms that cannot be inlined by the backend"); |
16b10ccc AC |
4145 | Write_Eol; |
4146 | end if; | |
4147 | ||
4148 | Write_Str (" "); | |
4149 | Write_Int (Count); | |
4150 | Write_Str (":"); | |
4151 | Write_Name (Chars (Nod)); | |
4152 | Write_Str (" ("); | |
4153 | Write_Location (Sloc (Nod)); | |
4154 | Write_Str (")"); | |
4155 | Output.Write_Eol; | |
4156 | ||
4157 | Next_Elmt (Elmt); | |
4158 | end loop; | |
4159 | end if; | |
4160 | end List_Inlining_Info; | |
4161 | ||
38cbfe40 RK |
4162 | ---------- |
4163 | -- Lock -- | |
4164 | ---------- | |
4165 | ||
4166 | procedure Lock is | |
4167 | begin | |
4168 | Pending_Instantiations.Locked := True; | |
4169 | Inlined_Bodies.Locked := True; | |
4170 | Successors.Locked := True; | |
4171 | Inlined.Locked := True; | |
4172 | Pending_Instantiations.Release; | |
4173 | Inlined_Bodies.Release; | |
4174 | Successors.Release; | |
4175 | Inlined.Release; | |
4176 | end Lock; | |
4177 | ||
697b781a AC |
4178 | -------------------------------- |
4179 | -- Remove_Aspects_And_Pragmas -- | |
4180 | -------------------------------- | |
16b10ccc | 4181 | |
697b781a AC |
4182 | procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is |
4183 | procedure Remove_Items (List : List_Id); | |
4184 | -- Remove all useless aspects/pragmas from a particular list | |
16b10ccc | 4185 | |
697b781a AC |
4186 | ------------------ |
4187 | -- Remove_Items -- | |
4188 | ------------------ | |
16b10ccc | 4189 | |
697b781a AC |
4190 | procedure Remove_Items (List : List_Id) is |
4191 | Item : Node_Id; | |
4192 | Item_Id : Node_Id; | |
4193 | Next_Item : Node_Id; | |
4194 | ||
4195 | begin | |
4196 | -- Traverse the list looking for an aspect specification or a pragma | |
4197 | ||
4198 | Item := First (List); | |
4199 | while Present (Item) loop | |
4200 | Next_Item := Next (Item); | |
4201 | ||
4202 | if Nkind (Item) = N_Aspect_Specification then | |
4203 | Item_Id := Identifier (Item); | |
4204 | elsif Nkind (Item) = N_Pragma then | |
4205 | Item_Id := Pragma_Identifier (Item); | |
4206 | else | |
4207 | Item_Id := Empty; | |
4208 | end if; | |
4209 | ||
4210 | if Present (Item_Id) | |
4211 | and then Nam_In (Chars (Item_Id), Name_Contract_Cases, | |
4212 | Name_Global, | |
4213 | Name_Depends, | |
16b10ccc | 4214 | Name_Postcondition, |
697b781a AC |
4215 | Name_Precondition, |
4216 | Name_Refined_Global, | |
4217 | Name_Refined_Depends, | |
4218 | Name_Refined_Post, | |
4219 | Name_Test_Case, | |
4220 | Name_Unmodified, | |
da9683f4 AC |
4221 | Name_Unreferenced, |
4222 | Name_Unused) | |
697b781a AC |
4223 | then |
4224 | Remove (Item); | |
4225 | end if; | |
16b10ccc | 4226 | |
697b781a AC |
4227 | Item := Next_Item; |
4228 | end loop; | |
4229 | end Remove_Items; | |
4230 | ||
4231 | -- Start of processing for Remove_Aspects_And_Pragmas | |
4232 | ||
4233 | begin | |
4234 | Remove_Items (Aspect_Specifications (Body_Decl)); | |
4235 | Remove_Items (Declarations (Body_Decl)); | |
da9683f4 | 4236 | |
fae8eb5b | 4237 | -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear |
da9683f4 AC |
4238 | -- in the body of the subprogram. |
4239 | ||
4240 | Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl))); | |
697b781a | 4241 | end Remove_Aspects_And_Pragmas; |
16b10ccc | 4242 | |
eefd2467 AC |
4243 | -------------------------- |
4244 | -- Remove_Dead_Instance -- | |
4245 | -------------------------- | |
4246 | ||
4247 | procedure Remove_Dead_Instance (N : Node_Id) is | |
4248 | J : Int; | |
4249 | ||
4250 | begin | |
4251 | J := 0; | |
4252 | while J <= Pending_Instantiations.Last loop | |
4253 | if Pending_Instantiations.Table (J).Inst_Node = N then | |
4254 | Pending_Instantiations.Table (J).Inst_Node := Empty; | |
4255 | return; | |
4256 | end if; | |
4257 | ||
4258 | J := J + 1; | |
4259 | end loop; | |
4260 | end Remove_Dead_Instance; | |
4261 | ||
38cbfe40 | 4262 | end Inline; |