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