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