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