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