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