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