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