PR middle-end/61455
[official-gcc.git] / gcc / ada / inline.adb
blobc2865ea93e5d79757f8c934ca91030f4cd10bf3b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- I N L I N E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
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 --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
38 with Lib; use Lib;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Output; use Output;
43 with Sem_Aux; use Sem_Aux;
44 with Sem_Ch8; use Sem_Ch8;
45 with Sem_Ch10; use Sem_Ch10;
46 with Sem_Ch12; use Sem_Ch12;
47 with Sem_Prag; use Sem_Prag;
48 with Sem_Util; use Sem_Util;
49 with Sinfo; use Sinfo;
50 with Sinput; use Sinput;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Uname; use Uname;
54 with Tbuild; use Tbuild;
56 package body Inline is
58 Check_Inlining_Restrictions : constant Boolean := True;
59 -- In the following cases the frontend rejects inlining because they
60 -- are not handled well by the backend. This variable facilitates
61 -- disabling these restrictions to evaluate future versions of the
62 -- GCC backend in which some of the restrictions may be supported.
64 -- - subprograms that have:
65 -- - nested subprograms
66 -- - instantiations
67 -- - package declarations
68 -- - task or protected object declarations
69 -- - some of the following statements:
70 -- - abort
71 -- - asynchronous-select
72 -- - conditional-entry-call
73 -- - delay-relative
74 -- - delay-until
75 -- - selective-accept
76 -- - timed-entry-call
78 Inlined_Calls : Elist_Id;
79 -- List of frontend inlined calls
81 Backend_Calls : Elist_Id;
82 -- List of inline calls passed to the backend
84 Backend_Inlined_Subps : Elist_Id;
85 -- List of subprograms inlined by the backend
87 Backend_Not_Inlined_Subps : Elist_Id;
88 -- List of subprograms that cannot be inlined by the backend
90 --------------------
91 -- Inlined Bodies --
92 --------------------
94 -- Inlined functions are actually placed in line by the backend if the
95 -- corresponding bodies are available (i.e. compiled). Whenever we find
96 -- a call to an inlined subprogram, we add the name of the enclosing
97 -- compilation unit to a worklist. After all compilation, and after
98 -- expansion of generic bodies, we traverse the list of pending bodies
99 -- and compile them as well.
101 package Inlined_Bodies is new Table.Table (
102 Table_Component_Type => Entity_Id,
103 Table_Index_Type => Int,
104 Table_Low_Bound => 0,
105 Table_Initial => Alloc.Inlined_Bodies_Initial,
106 Table_Increment => Alloc.Inlined_Bodies_Increment,
107 Table_Name => "Inlined_Bodies");
109 -----------------------
110 -- Inline Processing --
111 -----------------------
113 -- For each call to an inlined subprogram, we make entries in a table
114 -- that stores caller and callee, and indicates the call direction from
115 -- one to the other. We also record the compilation unit that contains
116 -- the callee. After analyzing the bodies of all such compilation units,
117 -- we compute the transitive closure of inlined subprograms called from
118 -- the main compilation unit and make it available to the code generator
119 -- in no particular order, thus allowing cycles in the call graph.
121 Last_Inlined : Entity_Id := Empty;
123 -- For each entry in the table we keep a list of successors in topological
124 -- order, i.e. callers of the current subprogram.
126 type Subp_Index is new Nat;
127 No_Subp : constant Subp_Index := 0;
129 -- The subprogram entities are hashed into the Inlined table
131 Num_Hash_Headers : constant := 512;
133 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
134 of Subp_Index;
136 type Succ_Index is new Nat;
137 No_Succ : constant Succ_Index := 0;
139 type Succ_Info is record
140 Subp : Subp_Index;
141 Next : Succ_Index;
142 end record;
144 -- The following table stores list elements for the successor lists. These
145 -- lists cannot be chained directly through entries in the Inlined table,
146 -- because a given subprogram can appear in several such lists.
148 package Successors is new Table.Table (
149 Table_Component_Type => Succ_Info,
150 Table_Index_Type => Succ_Index,
151 Table_Low_Bound => 1,
152 Table_Initial => Alloc.Successors_Initial,
153 Table_Increment => Alloc.Successors_Increment,
154 Table_Name => "Successors");
156 type Subp_Info is record
157 Name : Entity_Id := Empty;
158 Next : Subp_Index := No_Subp;
159 First_Succ : Succ_Index := No_Succ;
160 Listed : Boolean := False;
161 Main_Call : Boolean := False;
162 Processed : Boolean := False;
163 end record;
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");
173 -----------------------
174 -- Local Subprograms --
175 -----------------------
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.
182 procedure Add_Inlined_Subprogram (Index : Subp_Index);
183 -- Add the subprogram to the list of inlined subprogram for the unit
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.
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.
194 function Has_Initialized_Type (E : Entity_Id) return Boolean;
195 -- If a candidate for inlining contains type declarations for types with
196 -- non-trivial initialization procedures, they are not worth inlining.
198 function Has_Single_Return (N : Node_Id) return Boolean;
199 -- In general we cannot inline functions that return unconstrained type.
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.
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
208 function Is_Nested (E : Entity_Id) return Boolean;
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).
215 procedure Remove_Pragmas (Bod : Node_Id);
216 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
217 -- parameter has no meaning when the body is inlined and the formals
218 -- are rewritten. Remove it from body to inline. The analysis of the
219 -- non-inlined body will handle the pragma properly.
221 ------------------------------
222 -- Deferred Cleanup Actions --
223 ------------------------------
225 -- The cleanup actions for scopes that contain instantiations is delayed
226 -- until after expansion of those instantiations, because they may contain
227 -- finalizable objects or tasks that affect the cleanup code. A scope
228 -- that contains instantiations only needs to be finalized once, even
229 -- if it contains more than one instance. We keep a list of scopes
230 -- that must still be finalized, and call cleanup_actions after all
231 -- the instantiations have been completed.
233 To_Clean : Elist_Id;
235 procedure Add_Scope_To_Clean (Inst : Entity_Id);
236 -- Build set of scopes on which cleanup actions must be performed
238 procedure Cleanup_Scopes;
239 -- Complete cleanup actions on scopes that need it
241 --------------
242 -- Add_Call --
243 --------------
245 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
246 P1 : constant Subp_Index := Add_Subp (Called);
247 P2 : Subp_Index;
248 J : Succ_Index;
250 begin
251 if Present (Caller) then
252 P2 := Add_Subp (Caller);
254 -- Add P1 to the list of successors of P2, if not already there.
255 -- Note that P2 may contain more than one call to P1, and only
256 -- one needs to be recorded.
258 J := Inlined.Table (P2).First_Succ;
259 while J /= No_Succ loop
260 if Successors.Table (J).Subp = P1 then
261 return;
262 end if;
264 J := Successors.Table (J).Next;
265 end loop;
267 -- On exit, make a successor entry for P1
269 Successors.Increment_Last;
270 Successors.Table (Successors.Last).Subp := P1;
271 Successors.Table (Successors.Last).Next :=
272 Inlined.Table (P2).First_Succ;
273 Inlined.Table (P2).First_Succ := Successors.Last;
274 else
275 Inlined.Table (P1).Main_Call := True;
276 end if;
277 end Add_Call;
279 ----------------------
280 -- Add_Inlined_Body --
281 ----------------------
283 procedure Add_Inlined_Body (E : Entity_Id) is
285 type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package);
286 -- Level of inlining for the call: Dont_Inline means no inlining,
287 -- Inline_Call means that only the call is considered for inlining,
288 -- Inline_Package means that the call is considered for inlining and
289 -- its package compiled and scanned for more inlining opportunities.
291 function Must_Inline return Inline_Level_Type;
292 -- Inlining is only done if the call statement N is in the main unit,
293 -- or within the body of another inlined subprogram.
295 -----------------
296 -- Must_Inline --
297 -----------------
299 function Must_Inline return Inline_Level_Type is
300 Scop : Entity_Id;
301 Comp : Node_Id;
303 begin
304 -- Check if call is in main unit
306 Scop := Current_Scope;
308 -- Do not try to inline if scope is standard. This could happen, for
309 -- example, for a call to Add_Global_Declaration, and it causes
310 -- trouble to try to inline at this level.
312 if Scop = Standard_Standard then
313 return Dont_Inline;
314 end if;
316 -- Otherwise lookup scope stack to outer scope
318 while Scope (Scop) /= Standard_Standard
319 and then not Is_Child_Unit (Scop)
320 loop
321 Scop := Scope (Scop);
322 end loop;
324 Comp := Parent (Scop);
325 while Nkind (Comp) /= N_Compilation_Unit loop
326 Comp := Parent (Comp);
327 end loop;
329 -- If the call is in the main unit, inline the call and compile the
330 -- package of the subprogram to find more calls to be inlined.
332 if Comp = Cunit (Main_Unit)
333 or else Comp = Library_Unit (Cunit (Main_Unit))
334 then
335 Add_Call (E);
336 return Inline_Package;
337 end if;
339 -- The call is not in the main unit. See if it is in some inlined
340 -- subprogram. If so, inline the call and, if the inlining level is
341 -- set to 1, stop there; otherwise also compile the package as above.
343 Scop := Current_Scope;
344 while Scope (Scop) /= Standard_Standard
345 and then not Is_Child_Unit (Scop)
346 loop
347 if Is_Overloadable (Scop) and then Is_Inlined (Scop) then
348 Add_Call (E, Scop);
350 if Inline_Level = 1 then
351 return Inline_Call;
352 else
353 return Inline_Package;
354 end if;
355 end if;
357 Scop := Scope (Scop);
358 end loop;
360 return Dont_Inline;
361 end Must_Inline;
363 Level : Inline_Level_Type;
365 -- Start of processing for Add_Inlined_Body
367 begin
368 -- Find unit containing E, and add to list of inlined bodies if needed.
369 -- If the body is already present, no need to load any other unit. This
370 -- is the case for an initialization procedure, which appears in the
371 -- package declaration that contains the type. It is also the case if
372 -- the body has already been analyzed. Finally, if the unit enclosing
373 -- E is an instance, the instance body will be analyzed in any case,
374 -- and there is no need to add the enclosing unit (whose body might not
375 -- be available).
377 -- Library-level functions must be handled specially, because there is
378 -- no enclosing package to retrieve. In this case, it is the body of
379 -- the function that will have to be loaded.
381 if Is_Abstract_Subprogram (E)
382 or else Is_Nested (E)
383 or else Convention (E) = Convention_Protected
384 then
385 return;
386 end if;
388 Level := Must_Inline;
389 if Level /= Dont_Inline then
390 declare
391 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
393 begin
394 if Pack = E then
396 -- Library-level inlined function. Add function itself to
397 -- list of needed units.
399 Set_Is_Called (E);
400 Inlined_Bodies.Increment_Last;
401 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
403 elsif Ekind (Pack) = E_Package then
404 Set_Is_Called (E);
406 if Is_Generic_Instance (Pack) then
407 null;
409 -- Do not inline the package if the subprogram is an init proc
410 -- or other internally generated subprogram, because in that
411 -- case the subprogram body appears in the same unit that
412 -- declares the type, and that body is visible to the back end.
413 -- Do not inline it either if it is in the main unit.
415 elsif Level = Inline_Package
416 and then not Is_Inlined (Pack)
417 and then Comes_From_Source (E)
418 and then not In_Main_Unit_Or_Subunit (Pack)
419 then
420 Set_Is_Inlined (Pack);
421 Inlined_Bodies.Increment_Last;
422 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
424 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
425 -- calls if the back-end takes care of inlining the call.
427 elsif Level = Inline_Call
428 and then Has_Pragma_Inline_Always (E)
429 and then Back_End_Inlining
430 then
431 Set_Is_Inlined (Pack);
432 Inlined_Bodies.Increment_Last;
433 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
434 end if;
435 end if;
436 end;
437 end if;
438 end Add_Inlined_Body;
440 ----------------------------
441 -- Add_Inlined_Subprogram --
442 ----------------------------
444 procedure Add_Inlined_Subprogram (Index : Subp_Index) is
445 E : constant Entity_Id := Inlined.Table (Index).Name;
446 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
448 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean;
449 -- There are various conditions under which back-end inlining cannot
450 -- be done reliably:
452 -- a) If a body has handlers, it must not be inlined, because this
453 -- may violate program semantics, and because in zero-cost exception
454 -- mode it will lead to undefined symbols at link time.
456 -- b) If a body contains inlined function instances, it cannot be
457 -- inlined under ZCX because the numeric suffix generated by gigi
458 -- will be different in the body and the place of the inlined call.
460 -- This procedure must be carefully coordinated with the back end.
462 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
463 -- Append Subp to the list of subprograms inlined by the backend
465 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
466 -- Append Subp to the list of subprograms that cannot be inlined by
467 -- the backend.
469 ----------------------------
470 -- Back_End_Cannot_Inline --
471 ----------------------------
473 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean is
474 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
475 Body_Ent : Entity_Id;
476 Ent : Entity_Id;
478 begin
479 if Nkind (Decl) = N_Subprogram_Declaration
480 and then Present (Corresponding_Body (Decl))
481 then
482 Body_Ent := Corresponding_Body (Decl);
483 else
484 return False;
485 end if;
487 -- If subprogram is marked Inline_Always, inlining is mandatory
489 if Has_Pragma_Inline_Always (Subp) then
490 return False;
491 end if;
493 if Present
494 (Exception_Handlers
495 (Handled_Statement_Sequence
496 (Unit_Declaration_Node (Corresponding_Body (Decl)))))
497 then
498 return True;
499 end if;
501 Ent := First_Entity (Body_Ent);
502 while Present (Ent) loop
503 if Is_Subprogram (Ent)
504 and then Is_Generic_Instance (Ent)
505 then
506 return True;
507 end if;
509 Next_Entity (Ent);
510 end loop;
512 return False;
513 end Back_End_Cannot_Inline;
515 -----------------------------------------
516 -- Register_Backend_Inlined_Subprogram --
517 -----------------------------------------
519 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
520 begin
521 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
522 end Register_Backend_Inlined_Subprogram;
524 ---------------------------------------------
525 -- Register_Backend_Not_Inlined_Subprogram --
526 ---------------------------------------------
528 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
529 begin
530 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
531 end Register_Backend_Not_Inlined_Subprogram;
533 -- Start of processing for Add_Inlined_Subprogram
535 begin
536 -- If the subprogram is to be inlined, and if its unit is known to be
537 -- inlined or is an instance whose body will be analyzed anyway or the
538 -- subprogram has been generated by the compiler, and if it is declared
539 -- at the library level not in the main unit, and if it can be inlined
540 -- by the back-end, then insert it in the list of inlined subprograms.
542 if Is_Inlined (E)
543 and then (Is_Inlined (Pack)
544 or else Is_Generic_Instance (Pack)
545 or else Is_Internal (E))
546 and then not In_Main_Unit_Or_Subunit (E)
547 and then not Is_Nested (E)
548 and then not Has_Initialized_Type (E)
549 then
550 if Back_End_Cannot_Inline (E) then
551 Set_Is_Inlined (E, False);
552 Register_Backend_Not_Inlined_Subprogram (E);
554 else
555 Register_Backend_Inlined_Subprogram (E);
557 if No (Last_Inlined) then
558 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
559 else
560 Set_Next_Inlined_Subprogram (Last_Inlined, E);
561 end if;
563 Last_Inlined := E;
564 end if;
565 else
566 Register_Backend_Not_Inlined_Subprogram (E);
567 end if;
569 Inlined.Table (Index).Listed := True;
570 end Add_Inlined_Subprogram;
572 ------------------------
573 -- Add_Scope_To_Clean --
574 ------------------------
576 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
577 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
578 Elmt : Elmt_Id;
580 begin
581 -- If the instance appears in a library-level package declaration,
582 -- all finalization is global, and nothing needs doing here.
584 if Scop = Standard_Standard then
585 return;
586 end if;
588 -- If the instance is within a generic unit, no finalization code
589 -- can be generated. Note that at this point all bodies have been
590 -- analyzed, and the scope stack itself is not present, and the flag
591 -- Inside_A_Generic is not set.
593 declare
594 S : Entity_Id;
596 begin
597 S := Scope (Inst);
598 while Present (S) and then S /= Standard_Standard loop
599 if Is_Generic_Unit (S) then
600 return;
601 end if;
603 S := Scope (S);
604 end loop;
605 end;
607 Elmt := First_Elmt (To_Clean);
608 while Present (Elmt) loop
609 if Node (Elmt) = Scop then
610 return;
611 end if;
613 Elmt := Next_Elmt (Elmt);
614 end loop;
616 Append_Elmt (Scop, To_Clean);
617 end Add_Scope_To_Clean;
619 --------------
620 -- Add_Subp --
621 --------------
623 function Add_Subp (E : Entity_Id) return Subp_Index is
624 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
625 J : Subp_Index;
627 procedure New_Entry;
628 -- Initialize entry in Inlined table
630 procedure New_Entry is
631 begin
632 Inlined.Increment_Last;
633 Inlined.Table (Inlined.Last).Name := E;
634 Inlined.Table (Inlined.Last).Next := No_Subp;
635 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
636 Inlined.Table (Inlined.Last).Listed := False;
637 Inlined.Table (Inlined.Last).Main_Call := False;
638 Inlined.Table (Inlined.Last).Processed := False;
639 end New_Entry;
641 -- Start of processing for Add_Subp
643 begin
644 if Hash_Headers (Index) = No_Subp then
645 New_Entry;
646 Hash_Headers (Index) := Inlined.Last;
647 return Inlined.Last;
649 else
650 J := Hash_Headers (Index);
651 while J /= No_Subp loop
652 if Inlined.Table (J).Name = E then
653 return J;
654 else
655 Index := J;
656 J := Inlined.Table (J).Next;
657 end if;
658 end loop;
660 -- On exit, subprogram was not found. Enter in table. Index is
661 -- the current last entry on the hash chain.
663 New_Entry;
664 Inlined.Table (Index).Next := Inlined.Last;
665 return Inlined.Last;
666 end if;
667 end Add_Subp;
669 ----------------------------
670 -- Analyze_Inlined_Bodies --
671 ----------------------------
673 procedure Analyze_Inlined_Bodies is
674 Comp_Unit : Node_Id;
675 J : Int;
676 Pack : Entity_Id;
677 Subp : Subp_Index;
678 S : Succ_Index;
680 type Pending_Index is new Nat;
682 package Pending_Inlined is new Table.Table (
683 Table_Component_Type => Subp_Index,
684 Table_Index_Type => Pending_Index,
685 Table_Low_Bound => 1,
686 Table_Initial => Alloc.Inlined_Initial,
687 Table_Increment => Alloc.Inlined_Increment,
688 Table_Name => "Pending_Inlined");
689 -- The workpile used to compute the transitive closure
691 function Is_Ancestor_Of_Main
692 (U_Name : Entity_Id;
693 Nam : Node_Id) return Boolean;
694 -- Determine whether the unit whose body is loaded is an ancestor of
695 -- the main unit, and has a with_clause on it. The body is not
696 -- analyzed yet, so the check is purely lexical: the name of the with
697 -- clause is a selected component, and names of ancestors must match.
699 -------------------------
700 -- Is_Ancestor_Of_Main --
701 -------------------------
703 function Is_Ancestor_Of_Main
704 (U_Name : Entity_Id;
705 Nam : Node_Id) return Boolean
707 Pref : Node_Id;
709 begin
710 if Nkind (Nam) /= N_Selected_Component then
711 return False;
713 else
714 if Chars (Selector_Name (Nam)) /=
715 Chars (Cunit_Entity (Main_Unit))
716 then
717 return False;
718 end if;
720 Pref := Prefix (Nam);
721 if Nkind (Pref) = N_Identifier then
723 -- Par is an ancestor of Par.Child.
725 return Chars (Pref) = Chars (U_Name);
727 elsif Nkind (Pref) = N_Selected_Component
728 and then Chars (Selector_Name (Pref)) = Chars (U_Name)
729 then
730 -- Par.Child is an ancestor of Par.Child.Grand.
732 return True; -- should check that ancestor match
734 else
735 -- A is an ancestor of A.B.C if it is an ancestor of A.B
737 return Is_Ancestor_Of_Main (U_Name, Pref);
738 end if;
739 end if;
740 end Is_Ancestor_Of_Main;
742 -- Start of processing for Analyze_Inlined_Bodies
744 begin
745 if Serious_Errors_Detected = 0 then
746 Push_Scope (Standard_Standard);
748 J := 0;
749 while J <= Inlined_Bodies.Last
750 and then Serious_Errors_Detected = 0
751 loop
752 Pack := Inlined_Bodies.Table (J);
753 while Present (Pack)
754 and then Scope (Pack) /= Standard_Standard
755 and then not Is_Child_Unit (Pack)
756 loop
757 Pack := Scope (Pack);
758 end loop;
760 Comp_Unit := Parent (Pack);
761 while Present (Comp_Unit)
762 and then Nkind (Comp_Unit) /= N_Compilation_Unit
763 loop
764 Comp_Unit := Parent (Comp_Unit);
765 end loop;
767 -- Load the body, unless it is the main unit, or is an instance
768 -- whose body has already been analyzed.
770 if Present (Comp_Unit)
771 and then Comp_Unit /= Cunit (Main_Unit)
772 and then Body_Required (Comp_Unit)
773 and then (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
774 or else No (Corresponding_Body (Unit (Comp_Unit))))
775 then
776 declare
777 Bname : constant Unit_Name_Type :=
778 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
780 OK : Boolean;
782 begin
783 if not Is_Loaded (Bname) then
784 Style_Check := False;
785 Load_Needed_Body (Comp_Unit, OK, Do_Analyze => False);
787 if not OK then
789 -- Warn that a body was not available for inlining
790 -- by the back-end.
792 Error_Msg_Unit_1 := Bname;
793 Error_Msg_N
794 ("one or more inlined subprograms accessed in $!??",
795 Comp_Unit);
796 Error_Msg_File_1 :=
797 Get_File_Name (Bname, Subunit => False);
798 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
800 else
801 -- If the package to be inlined is an ancestor unit of
802 -- the main unit, and it has a semantic dependence on
803 -- it, the inlining cannot take place to prevent an
804 -- elaboration circularity. The desired body is not
805 -- analyzed yet, to prevent the completion of Taft
806 -- amendment types that would lead to elaboration
807 -- circularities in gigi.
809 declare
810 U_Id : constant Entity_Id :=
811 Defining_Entity (Unit (Comp_Unit));
812 Body_Unit : constant Node_Id :=
813 Library_Unit (Comp_Unit);
814 Item : Node_Id;
816 begin
817 Item := First (Context_Items (Body_Unit));
818 while Present (Item) loop
819 if Nkind (Item) = N_With_Clause
820 and then
821 Is_Ancestor_Of_Main (U_Id, Name (Item))
822 then
823 Set_Is_Inlined (U_Id, False);
824 exit;
825 end if;
827 Next (Item);
828 end loop;
830 -- If no suspicious with_clauses, analyze the body.
832 if Is_Inlined (U_Id) then
833 Semantics (Body_Unit);
834 end if;
835 end;
836 end if;
837 end if;
838 end;
839 end if;
841 J := J + 1;
842 end loop;
844 -- The analysis of required bodies may have produced additional
845 -- generic instantiations. To obtain further inlining, we perform
846 -- another round of generic body instantiations. Establishing a
847 -- fully recursive loop between inlining and generic instantiations
848 -- is unlikely to yield more than this one additional pass.
850 Instantiate_Bodies;
852 -- The list of inlined subprograms is an overestimate, because it
853 -- includes inlined functions called from functions that are compiled
854 -- as part of an inlined package, but are not themselves called. An
855 -- accurate computation of just those subprograms that are needed
856 -- requires that we perform a transitive closure over the call graph,
857 -- starting from calls in the main program.
859 for Index in Inlined.First .. Inlined.Last loop
860 if not Is_Called (Inlined.Table (Index).Name) then
862 -- This means that Add_Inlined_Body added the subprogram to the
863 -- table but wasn't able to handle its code unit. Do nothing.
865 Inlined.Table (Index).Processed := True;
867 elsif Inlined.Table (Index).Main_Call then
868 Pending_Inlined.Increment_Last;
869 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
870 Inlined.Table (Index).Processed := True;
872 else
873 Set_Is_Called (Inlined.Table (Index).Name, False);
874 end if;
875 end loop;
877 -- Iterate over the workpile until it is emptied, propagating the
878 -- Is_Called flag to the successors of the processed subprogram.
880 while Pending_Inlined.Last >= Pending_Inlined.First loop
881 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
882 Pending_Inlined.Decrement_Last;
884 S := Inlined.Table (Subp).First_Succ;
886 while S /= No_Succ loop
887 Subp := Successors.Table (S).Subp;
889 if not Inlined.Table (Subp).Processed then
890 Set_Is_Called (Inlined.Table (Subp).Name);
891 Pending_Inlined.Increment_Last;
892 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
893 Inlined.Table (Subp).Processed := True;
894 end if;
896 S := Successors.Table (S).Next;
897 end loop;
898 end loop;
900 -- Finally add the called subprograms to the list of inlined
901 -- subprograms for the unit.
903 for Index in Inlined.First .. Inlined.Last loop
904 if Is_Called (Inlined.Table (Index).Name)
905 and then not Inlined.Table (Index).Listed
906 then
907 Add_Inlined_Subprogram (Index);
908 end if;
909 end loop;
911 Pop_Scope;
912 end if;
913 end Analyze_Inlined_Bodies;
915 --------------------------
916 -- Build_Body_To_Inline --
917 --------------------------
919 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
920 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
921 Analysis_Status : constant Boolean := Full_Analysis;
922 Original_Body : Node_Id;
923 Body_To_Analyze : Node_Id;
924 Max_Size : constant := 10;
926 function Has_Pending_Instantiation return Boolean;
927 -- If some enclosing body contains instantiations that appear before
928 -- the corresponding generic body, the enclosing body has a freeze node
929 -- so that it can be elaborated after the generic itself. This might
930 -- conflict with subsequent inlinings, so that it is unsafe to try to
931 -- inline in such a case.
933 function Has_Single_Return_In_GNATprove_Mode return Boolean;
934 -- This function is called only in GNATprove mode, and it returns
935 -- True if the subprogram has no return statement or a single return
936 -- statement as last statement.
938 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
939 -- If the body of the subprogram includes a call that returns an
940 -- unconstrained type, the secondary stack is involved, and it
941 -- is not worth inlining.
943 -------------------------------
944 -- Has_Pending_Instantiation --
945 -------------------------------
947 function Has_Pending_Instantiation return Boolean is
948 S : Entity_Id;
950 begin
951 S := Current_Scope;
952 while Present (S) loop
953 if Is_Compilation_Unit (S)
954 or else Is_Child_Unit (S)
955 then
956 return False;
958 elsif Ekind (S) = E_Package
959 and then Has_Forward_Instantiation (S)
960 then
961 return True;
962 end if;
964 S := Scope (S);
965 end loop;
967 return False;
968 end Has_Pending_Instantiation;
970 -----------------------------------------
971 -- Has_Single_Return_In_GNATprove_Mode --
972 -----------------------------------------
974 function Has_Single_Return_In_GNATprove_Mode return Boolean is
975 Last_Statement : Node_Id := Empty;
977 function Check_Return (N : Node_Id) return Traverse_Result;
978 -- Returns OK on node N if this is not a return statement different
979 -- from the last statement in the subprogram.
981 ------------------
982 -- Check_Return --
983 ------------------
985 function Check_Return (N : Node_Id) return Traverse_Result is
986 begin
987 if Nkind_In (N, N_Simple_Return_Statement,
988 N_Extended_Return_Statement)
989 then
990 if N = Last_Statement then
991 return OK;
992 else
993 return Abandon;
994 end if;
996 else
997 return OK;
998 end if;
999 end Check_Return;
1001 function Check_All_Returns is new Traverse_Func (Check_Return);
1003 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1005 begin
1006 -- Retrieve last statement inside possible block statements
1008 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
1010 while Nkind (Last_Statement) = N_Block_Statement loop
1011 Last_Statement :=
1012 Last (Statements (Handled_Statement_Sequence (Last_Statement)));
1013 end loop;
1015 -- Check that the last statement is the only possible return
1016 -- statement in the subprogram.
1018 return Check_All_Returns (N) = OK;
1019 end Has_Single_Return_In_GNATprove_Mode;
1021 --------------------------
1022 -- Uses_Secondary_Stack --
1023 --------------------------
1025 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1026 function Check_Call (N : Node_Id) return Traverse_Result;
1027 -- Look for function calls that return an unconstrained type
1029 ----------------
1030 -- Check_Call --
1031 ----------------
1033 function Check_Call (N : Node_Id) return Traverse_Result is
1034 begin
1035 if Nkind (N) = N_Function_Call
1036 and then Is_Entity_Name (Name (N))
1037 and then Is_Composite_Type (Etype (Entity (Name (N))))
1038 and then not Is_Constrained (Etype (Entity (Name (N))))
1039 then
1040 Cannot_Inline
1041 ("cannot inline & (call returns unconstrained type)?",
1042 N, Spec_Id);
1043 return Abandon;
1044 else
1045 return OK;
1046 end if;
1047 end Check_Call;
1049 function Check_Calls is new Traverse_Func (Check_Call);
1051 begin
1052 return Check_Calls (Bod) = Abandon;
1053 end Uses_Secondary_Stack;
1055 -- Start of processing for Build_Body_To_Inline
1057 begin
1058 -- Return immediately if done already
1060 if Nkind (Decl) = N_Subprogram_Declaration
1061 and then Present (Body_To_Inline (Decl))
1062 then
1063 return;
1065 -- Subprograms that have return statements in the middle of the body are
1066 -- inlined with gotos. GNATprove does not currently support gotos, so
1067 -- we prevent such inlining.
1069 elsif GNATprove_Mode
1070 and then not Has_Single_Return_In_GNATprove_Mode
1071 then
1072 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1073 return;
1075 -- Functions that return unconstrained composite types require
1076 -- secondary stack handling, and cannot currently be inlined, unless
1077 -- all return statements return a local variable that is the first
1078 -- local declaration in the body.
1080 elsif Ekind (Spec_Id) = E_Function
1081 and then not Is_Scalar_Type (Etype (Spec_Id))
1082 and then not Is_Access_Type (Etype (Spec_Id))
1083 and then not Is_Constrained (Etype (Spec_Id))
1084 then
1085 if not Has_Single_Return (N) then
1086 Cannot_Inline
1087 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1088 return;
1089 end if;
1091 -- Ditto for functions that return controlled types, where controlled
1092 -- actions interfere in complex ways with inlining.
1094 elsif Ekind (Spec_Id) = E_Function
1095 and then Needs_Finalization (Etype (Spec_Id))
1096 then
1097 Cannot_Inline
1098 ("cannot inline & (controlled return type)?", N, Spec_Id);
1099 return;
1100 end if;
1102 if Present (Declarations (N))
1103 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
1104 then
1105 return;
1106 end if;
1108 if Present (Handled_Statement_Sequence (N)) then
1109 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1110 Cannot_Inline
1111 ("cannot inline& (exception handler)?",
1112 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1113 Spec_Id);
1114 return;
1116 elsif Has_Excluded_Statement
1117 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1118 then
1119 return;
1120 end if;
1121 end if;
1123 -- We do not inline a subprogram that is too large, unless it is marked
1124 -- Inline_Always or we are in GNATprove mode. This pragma does not
1125 -- suppress the other checks on inlining (forbidden declarations,
1126 -- handlers, etc).
1128 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1129 and then List_Length
1130 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1131 then
1132 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1133 return;
1134 end if;
1136 if Has_Pending_Instantiation then
1137 Cannot_Inline
1138 ("cannot inline& (forward instance within enclosing body)?",
1139 N, Spec_Id);
1140 return;
1141 end if;
1143 -- Within an instance, the body to inline must be treated as a nested
1144 -- generic, so that the proper global references are preserved.
1146 -- Note that we do not do this at the library level, because it is not
1147 -- needed, and furthermore this causes trouble if front end inlining
1148 -- is activated (-gnatN).
1150 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1151 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1152 Original_Body := Copy_Generic_Node (N, Empty, True);
1153 else
1154 Original_Body := Copy_Separate_Tree (N);
1155 end if;
1157 -- We need to capture references to the formals in order to substitute
1158 -- the actuals at the point of inlining, i.e. instantiation. To treat
1159 -- the formals as globals to the body to inline, we nest it within a
1160 -- dummy parameterless subprogram, declared within the real one. To
1161 -- avoid generating an internal name (which is never public, and which
1162 -- affects serial numbers of other generated names), we use an internal
1163 -- symbol that cannot conflict with user declarations.
1165 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1166 Set_Defining_Unit_Name
1167 (Specification (Original_Body),
1168 Make_Defining_Identifier (Sloc (N), Name_uParent));
1169 Set_Corresponding_Spec (Original_Body, Empty);
1171 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1173 -- Set return type of function, which is also global and does not need
1174 -- to be resolved.
1176 if Ekind (Spec_Id) = E_Function then
1177 Set_Result_Definition (Specification (Body_To_Analyze),
1178 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1179 end if;
1181 if No (Declarations (N)) then
1182 Set_Declarations (N, New_List (Body_To_Analyze));
1183 else
1184 Append (Body_To_Analyze, Declarations (N));
1185 end if;
1187 -- The body to inline is pre-analyzed. In GNATprove mode we must
1188 -- disable full analysis as well so that light expansion does not
1189 -- take place either, and name resolution is unaffected.
1191 Expander_Mode_Save_And_Set (False);
1192 Full_Analysis := False;
1193 Remove_Pragmas (Body_To_Analyze);
1195 Analyze (Body_To_Analyze);
1196 Push_Scope (Defining_Entity (Body_To_Analyze));
1197 Save_Global_References (Original_Body);
1198 End_Scope;
1199 Remove (Body_To_Analyze);
1201 Expander_Mode_Restore;
1202 Full_Analysis := Analysis_Status;
1204 -- Restore environment if previously saved
1206 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1207 Restore_Env;
1208 end if;
1210 -- If secondary stack is used, there is no point in inlining. We have
1211 -- already issued the warning in this case, so nothing to do.
1213 if Uses_Secondary_Stack (Body_To_Analyze) then
1214 return;
1215 end if;
1217 Set_Body_To_Inline (Decl, Original_Body);
1218 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1219 Set_Is_Inlined (Spec_Id);
1220 end Build_Body_To_Inline;
1222 -------------------
1223 -- Cannot_Inline --
1224 -------------------
1226 procedure Cannot_Inline
1227 (Msg : String;
1228 N : Node_Id;
1229 Subp : Entity_Id;
1230 Is_Serious : Boolean := False)
1232 begin
1233 -- In GNATprove mode, inlining is the technical means by which the
1234 -- higher-level goal of contextual analysis is reached, so issue
1235 -- messages about failure to apply contextual analysis to a
1236 -- subprogram, rather than failure to inline it.
1238 if GNATprove_Mode
1239 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1240 then
1241 declare
1242 Len1 : constant Positive :=
1243 String (String'("cannot inline"))'Length;
1244 Len2 : constant Positive :=
1245 String (String'("info: no contextual analysis of"))'Length;
1247 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1249 begin
1250 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1251 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1252 Msg (Msg'First + Len1 .. Msg'Last);
1253 Cannot_Inline (New_Msg, N, Subp, Is_Serious);
1254 return;
1255 end;
1256 end if;
1258 pragma Assert (Msg (Msg'Last) = '?');
1260 -- Legacy front end inlining model
1262 if not Back_End_Inlining then
1264 -- Do not emit warning if this is a predefined unit which is not
1265 -- the main unit. With validity checks enabled, some predefined
1266 -- subprograms may contain nested subprograms and become ineligible
1267 -- for inlining.
1269 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1270 and then not In_Extended_Main_Source_Unit (Subp)
1271 then
1272 null;
1274 -- In GNATprove mode, issue a warning, and indicate that the
1275 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1276 -- to False.
1278 elsif GNATprove_Mode then
1279 Set_Is_Inlined_Always (Subp, False);
1280 Error_Msg_NE (Msg & "p?", N, Subp);
1282 elsif Has_Pragma_Inline_Always (Subp) then
1284 -- Remove last character (question mark) to make this into an
1285 -- error, because the Inline_Always pragma cannot be obeyed.
1287 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1289 elsif Ineffective_Inline_Warnings then
1290 Error_Msg_NE (Msg & "p?", N, Subp);
1291 end if;
1293 return;
1295 -- New semantics
1297 elsif Is_Serious then
1299 -- Remove last character (question mark) to make this into an error.
1301 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1303 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1304 -- is not always inlined by setting flag Is_Inlined_Always to False.
1306 elsif GNATprove_Mode then
1307 Set_Is_Inlined_Always (Subp, False);
1308 Error_Msg_NE (Msg & "p?", N, Subp);
1310 -- Do not issue errors/warnings when compiling with optimizations
1312 elsif Optimization_Level = 0 then
1314 -- Do not emit warning if this is a predefined unit which is not
1315 -- the main unit. This behavior is currently provided for backward
1316 -- compatibility but it will be removed when we enforce the
1317 -- strictness of the new rules.
1319 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1320 and then not In_Extended_Main_Source_Unit (Subp)
1321 then
1322 null;
1324 elsif Has_Pragma_Inline_Always (Subp) then
1326 -- Emit a warning if this is a call to a runtime subprogram
1327 -- which is located inside a generic. Previously this call
1328 -- was silently skipped.
1330 if Is_Generic_Instance (Subp) then
1331 declare
1332 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
1333 begin
1334 if Is_Predefined_File_Name
1335 (Unit_File_Name (Get_Source_Unit (Gen_P)))
1336 then
1337 Set_Is_Inlined (Subp, False);
1338 Error_Msg_NE (Msg & "p?", N, Subp);
1339 return;
1340 end if;
1341 end;
1342 end if;
1344 -- Remove last character (question mark) to make this into an
1345 -- error, because the Inline_Always pragma cannot be obeyed.
1347 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1349 else pragma Assert (Front_End_Inlining);
1350 Set_Is_Inlined (Subp, False);
1352 -- When inlining cannot take place we must issue an error.
1353 -- For backward compatibility we still report a warning.
1355 if Ineffective_Inline_Warnings then
1356 Error_Msg_NE (Msg & "p?", N, Subp);
1357 end if;
1358 end if;
1360 -- Compiling with optimizations enabled it is too early to report
1361 -- problems since the backend may still perform inlining. In order
1362 -- to report unhandled inlinings the program must be compiled with
1363 -- -Winline and the error is reported by the backend.
1365 else
1366 null;
1367 end if;
1368 end Cannot_Inline;
1370 --------------------------------------
1371 -- Can_Be_Inlined_In_GNATprove_Mode --
1372 --------------------------------------
1374 function Can_Be_Inlined_In_GNATprove_Mode
1375 (Spec_Id : Entity_Id;
1376 Body_Id : Entity_Id) return Boolean
1378 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1379 -- Returns True if subprogram Id has any contract (Pre, Post, Global,
1380 -- Depends, etc.)
1382 function In_Some_Private_Part (N : Node_Id) return Boolean;
1383 -- Returns True if node N is defined in the private part of a package
1385 function In_Unit_Body (N : Node_Id) return Boolean;
1386 -- Returns True if node N is defined in the body of a unit
1388 function Is_Expression_Function (Id : Entity_Id) return Boolean;
1389 -- Returns True if subprogram Id was defined originally as an expression
1390 -- function.
1392 -----------------------
1393 -- Has_Some_Contract --
1394 -----------------------
1396 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1397 Items : constant Node_Id := Contract (Id);
1398 begin
1399 return Present (Items)
1400 and then (Present (Pre_Post_Conditions (Items)) or else
1401 Present (Contract_Test_Cases (Items)) or else
1402 Present (Classifications (Items)));
1403 end Has_Some_Contract;
1405 --------------------------
1406 -- In_Some_Private_Part --
1407 --------------------------
1409 function In_Some_Private_Part (N : Node_Id) return Boolean is
1410 P : Node_Id;
1411 PP : Node_Id;
1413 begin
1414 P := N;
1415 while Present (P) and then Present (Parent (P)) loop
1416 PP := Parent (P);
1418 if Nkind (PP) = N_Package_Specification
1419 and then List_Containing (P) = Private_Declarations (PP)
1420 then
1421 return True;
1422 end if;
1424 P := PP;
1425 end loop;
1427 return False;
1428 end In_Some_Private_Part;
1430 ------------------
1431 -- In_Unit_Body --
1432 ------------------
1434 function In_Unit_Body (N : Node_Id) return Boolean is
1435 CU : constant Node_Id := Enclosing_Comp_Unit_Node (N);
1436 begin
1437 return Present (CU)
1438 and then Nkind_In (Unit (CU), N_Package_Body,
1439 N_Subprogram_Body,
1440 N_Subunit);
1441 end In_Unit_Body;
1443 ----------------------------
1444 -- Is_Expression_Function --
1445 ----------------------------
1447 function Is_Expression_Function (Id : Entity_Id) return Boolean is
1448 Decl : constant Node_Id := Parent (Parent (Id));
1449 begin
1450 return Nkind (Original_Node (Decl)) = N_Expression_Function;
1451 end Is_Expression_Function;
1453 -- Local declarations
1455 Id : Entity_Id; -- Procedure or function entity for the subprogram
1457 -- Start of Can_Be_Inlined_In_GNATprove_Mode
1459 begin
1460 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1462 if Present (Spec_Id) then
1463 Id := Spec_Id;
1464 else
1465 Id := Body_Id;
1466 end if;
1468 -- Only local subprograms without contracts are inlined in GNATprove
1469 -- mode, as these are the subprograms which a user is not interested in
1470 -- analyzing in isolation, but rather in the context of their call. This
1471 -- is a convenient convention, that could be changed for an explicit
1472 -- pragma/aspect one day.
1474 -- In a number of special cases, inlining is not desirable or not
1475 -- possible, see below.
1477 -- Do not inline unit-level subprograms
1479 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1480 return False;
1482 -- Do not inline subprograms declared in the visible part of a library
1483 -- package.
1485 elsif Is_Library_Level_Entity (Id)
1486 and then not In_Unit_Body (Id)
1487 and then not In_Some_Private_Part (Id)
1488 then
1489 return False;
1491 -- Do not inline subprograms that have a contract on the spec or the
1492 -- body. Use the contract(s) instead in GNATprove.
1494 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1495 or else
1496 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1497 then
1498 return False;
1500 -- Do not inline expression functions, which are directly inlined at the
1501 -- prover level.
1503 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1504 or else
1505 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1506 then
1507 return False;
1509 -- Do not inline generic subprogram instances. The visibility rules of
1510 -- generic instances plays badly with inlining.
1512 elsif Is_Generic_Instance (Spec_Id) then
1513 return False;
1515 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1516 -- the subprogram body, a similar check is performed after the body
1517 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1519 elsif Present (Spec_Id)
1520 and then
1521 (No (SPARK_Pragma (Spec_Id))
1522 or else Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) /= On)
1523 then
1524 return False;
1526 -- Subprograms in generic instances are currently not inlined, to avoid
1527 -- problems with inlining of standard library subprograms.
1529 elsif Instantiation_Location (Sloc (Id)) /= No_Location then
1530 return False;
1532 -- Don't inline predicate functions (treated specially by GNATprove)
1534 elsif Is_Predicate_Function (Id) then
1535 return False;
1537 -- Otherwise, this is a subprogram declared inside the private part of a
1538 -- package, or inside a package body, or locally in a subprogram, and it
1539 -- does not have any contract. Inline it.
1541 else
1542 return True;
1543 end if;
1544 end Can_Be_Inlined_In_GNATprove_Mode;
1546 --------------------------------------------
1547 -- Check_And_Split_Unconstrained_Function --
1548 --------------------------------------------
1550 procedure Check_And_Split_Unconstrained_Function
1551 (N : Node_Id;
1552 Spec_Id : Entity_Id;
1553 Body_Id : Entity_Id)
1555 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
1556 -- Use generic machinery to build an unexpanded body for the subprogram.
1557 -- This body is subsequently used for inline expansions at call sites.
1559 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
1560 -- Return true if we generate code for the function body N, the function
1561 -- body N has no local declarations and its unique statement is a single
1562 -- extended return statement with a handled statements sequence.
1564 procedure Generate_Subprogram_Body
1565 (N : Node_Id;
1566 Body_To_Inline : out Node_Id);
1567 -- Generate a parameterless duplicate of subprogram body N. Occurrences
1568 -- of pragmas referencing the formals are removed since they have no
1569 -- meaning when the body is inlined and the formals are rewritten (the
1570 -- analysis of the non-inlined body will handle these pragmas properly).
1571 -- A new internal name is associated with Body_To_Inline.
1573 procedure Split_Unconstrained_Function
1574 (N : Node_Id;
1575 Spec_Id : Entity_Id);
1576 -- N is an inlined function body that returns an unconstrained type and
1577 -- has a single extended return statement. Split N in two subprograms:
1578 -- a procedure P' and a function F'. The formals of P' duplicate the
1579 -- formals of N plus an extra formal which is used return a value;
1580 -- its body is composed by the declarations and list of statements
1581 -- of the extended return statement of N.
1583 --------------------------
1584 -- Build_Body_To_Inline --
1585 --------------------------
1587 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1588 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1589 Original_Body : Node_Id;
1590 Body_To_Analyze : Node_Id;
1592 begin
1593 pragma Assert (Current_Scope = Spec_Id);
1595 -- Within an instance, the body to inline must be treated as a nested
1596 -- generic, so that the proper global references are preserved. We
1597 -- do not do this at the library level, because it is not needed, and
1598 -- furthermore this causes trouble if front end inlining is activated
1599 -- (-gnatN).
1601 if In_Instance
1602 and then Scope (Current_Scope) /= Standard_Standard
1603 then
1604 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1605 end if;
1607 -- We need to capture references to the formals in order
1608 -- to substitute the actuals at the point of inlining, i.e.
1609 -- instantiation. To treat the formals as globals to the body to
1610 -- inline, we nest it within a dummy parameterless subprogram,
1611 -- declared within the real one.
1613 Generate_Subprogram_Body (N, Original_Body);
1614 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1616 -- Set return type of function, which is also global and does not
1617 -- need to be resolved.
1619 if Ekind (Spec_Id) = E_Function then
1620 Set_Result_Definition (Specification (Body_To_Analyze),
1621 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1622 end if;
1624 if No (Declarations (N)) then
1625 Set_Declarations (N, New_List (Body_To_Analyze));
1626 else
1627 Append_To (Declarations (N), Body_To_Analyze);
1628 end if;
1630 Preanalyze (Body_To_Analyze);
1632 Push_Scope (Defining_Entity (Body_To_Analyze));
1633 Save_Global_References (Original_Body);
1634 End_Scope;
1635 Remove (Body_To_Analyze);
1637 -- Restore environment if previously saved
1639 if In_Instance
1640 and then Scope (Current_Scope) /= Standard_Standard
1641 then
1642 Restore_Env;
1643 end if;
1645 pragma Assert (No (Body_To_Inline (Decl)));
1646 Set_Body_To_Inline (Decl, Original_Body);
1647 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1648 end Build_Body_To_Inline;
1650 --------------------------------------
1651 -- Can_Split_Unconstrained_Function --
1652 --------------------------------------
1654 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
1656 Ret_Node : constant Node_Id :=
1657 First (Statements (Handled_Statement_Sequence (N)));
1658 D : Node_Id;
1660 begin
1661 -- No user defined declarations allowed in the function except inside
1662 -- the unique return statement; implicit labels are the only allowed
1663 -- declarations.
1665 if not Is_Empty_List (Declarations (N)) then
1666 D := First (Declarations (N));
1667 while Present (D) loop
1668 if Nkind (D) /= N_Implicit_Label_Declaration then
1669 return False;
1670 end if;
1672 Next (D);
1673 end loop;
1674 end if;
1676 -- We only split the inlined function when we are generating the code
1677 -- of its body; otherwise we leave duplicated split subprograms in
1678 -- the tree which (if referenced) generate wrong references at link
1679 -- time.
1681 return In_Extended_Main_Code_Unit (N)
1682 and then Present (Ret_Node)
1683 and then Nkind (Ret_Node) = N_Extended_Return_Statement
1684 and then No (Next (Ret_Node))
1685 and then Present (Handled_Statement_Sequence (Ret_Node));
1686 end Can_Split_Unconstrained_Function;
1688 -----------------------------
1689 -- Generate_Body_To_Inline --
1690 -----------------------------
1692 procedure Generate_Subprogram_Body
1693 (N : Node_Id;
1694 Body_To_Inline : out Node_Id)
1696 begin
1697 -- Within an instance, the body to inline must be treated as a nested
1698 -- generic, so that the proper global references are preserved.
1700 -- Note that we do not do this at the library level, because it
1701 -- is not needed, and furthermore this causes trouble if front
1702 -- end inlining is activated (-gnatN).
1704 if In_Instance
1705 and then Scope (Current_Scope) /= Standard_Standard
1706 then
1707 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
1708 else
1709 Body_To_Inline := Copy_Separate_Tree (N);
1710 end if;
1712 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
1713 -- parameter has no meaning when the body is inlined and the formals
1714 -- are rewritten. Remove it from body to inline. The analysis of the
1715 -- non-inlined body will handle the pragma properly.
1717 Remove_Pragmas (Body_To_Inline);
1719 -- We need to capture references to the formals in order
1720 -- to substitute the actuals at the point of inlining, i.e.
1721 -- instantiation. To treat the formals as globals to the body to
1722 -- inline, we nest it within a dummy parameterless subprogram,
1723 -- declared within the real one.
1725 Set_Parameter_Specifications
1726 (Specification (Body_To_Inline), No_List);
1728 -- A new internal name is associated with Body_To_Inline to avoid
1729 -- conflicts when the non-inlined body N is analyzed.
1731 Set_Defining_Unit_Name (Specification (Body_To_Inline),
1732 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
1733 Set_Corresponding_Spec (Body_To_Inline, Empty);
1734 end Generate_Subprogram_Body;
1736 ----------------------------------
1737 -- Split_Unconstrained_Function --
1738 ----------------------------------
1740 procedure Split_Unconstrained_Function
1741 (N : Node_Id;
1742 Spec_Id : Entity_Id)
1744 Loc : constant Source_Ptr := Sloc (N);
1745 Ret_Node : constant Node_Id :=
1746 First (Statements (Handled_Statement_Sequence (N)));
1747 Ret_Obj : constant Node_Id :=
1748 First (Return_Object_Declarations (Ret_Node));
1750 procedure Build_Procedure
1751 (Proc_Id : out Entity_Id;
1752 Decl_List : out List_Id);
1753 -- Build a procedure containing the statements found in the extended
1754 -- return statement of the unconstrained function body N.
1756 ---------------------
1757 -- Build_Procedure --
1758 ---------------------
1760 procedure Build_Procedure
1761 (Proc_Id : out Entity_Id;
1762 Decl_List : out List_Id)
1764 Formal : Entity_Id;
1765 Formal_List : constant List_Id := New_List;
1766 Proc_Spec : Node_Id;
1767 Proc_Body : Node_Id;
1768 Subp_Name : constant Name_Id := New_Internal_Name ('F');
1769 Body_Decl_List : List_Id := No_List;
1770 Param_Type : Node_Id;
1772 begin
1773 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
1774 Param_Type :=
1775 New_Copy (Object_Definition (Ret_Obj));
1776 else
1777 Param_Type :=
1778 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
1779 end if;
1781 Append_To (Formal_List,
1782 Make_Parameter_Specification (Loc,
1783 Defining_Identifier =>
1784 Make_Defining_Identifier (Loc,
1785 Chars => Chars (Defining_Identifier (Ret_Obj))),
1786 In_Present => False,
1787 Out_Present => True,
1788 Null_Exclusion_Present => False,
1789 Parameter_Type => Param_Type));
1791 Formal := First_Formal (Spec_Id);
1792 while Present (Formal) loop
1793 Append_To (Formal_List,
1794 Make_Parameter_Specification (Loc,
1795 Defining_Identifier =>
1796 Make_Defining_Identifier (Sloc (Formal),
1797 Chars => Chars (Formal)),
1798 In_Present => In_Present (Parent (Formal)),
1799 Out_Present => Out_Present (Parent (Formal)),
1800 Null_Exclusion_Present =>
1801 Null_Exclusion_Present (Parent (Formal)),
1802 Parameter_Type =>
1803 New_Occurrence_Of (Etype (Formal), Loc),
1804 Expression =>
1805 Copy_Separate_Tree (Expression (Parent (Formal)))));
1807 Next_Formal (Formal);
1808 end loop;
1810 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
1812 Proc_Spec :=
1813 Make_Procedure_Specification (Loc,
1814 Defining_Unit_Name => Proc_Id,
1815 Parameter_Specifications => Formal_List);
1817 Decl_List := New_List;
1819 Append_To (Decl_List,
1820 Make_Subprogram_Declaration (Loc, Proc_Spec));
1822 -- Can_Convert_Unconstrained_Function checked that the function
1823 -- has no local declarations except implicit label declarations.
1824 -- Copy these declarations to the built procedure.
1826 if Present (Declarations (N)) then
1827 Body_Decl_List := New_List;
1829 declare
1830 D : Node_Id;
1831 New_D : Node_Id;
1833 begin
1834 D := First (Declarations (N));
1835 while Present (D) loop
1836 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
1838 New_D :=
1839 Make_Implicit_Label_Declaration (Loc,
1840 Make_Defining_Identifier (Loc,
1841 Chars => Chars (Defining_Identifier (D))),
1842 Label_Construct => Empty);
1843 Append_To (Body_Decl_List, New_D);
1845 Next (D);
1846 end loop;
1847 end;
1848 end if;
1850 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
1852 Proc_Body :=
1853 Make_Subprogram_Body (Loc,
1854 Specification => Copy_Separate_Tree (Proc_Spec),
1855 Declarations => Body_Decl_List,
1856 Handled_Statement_Sequence =>
1857 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
1859 Set_Defining_Unit_Name (Specification (Proc_Body),
1860 Make_Defining_Identifier (Loc, Subp_Name));
1862 Append_To (Decl_List, Proc_Body);
1863 end Build_Procedure;
1865 -- Local variables
1867 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
1868 Blk_Stmt : Node_Id;
1869 Proc_Id : Entity_Id;
1870 Proc_Call : Node_Id;
1872 -- Start of processing for Split_Unconstrained_Function
1874 begin
1875 -- Build the associated procedure, analyze it and insert it before
1876 -- the function body N.
1878 declare
1879 Scope : constant Entity_Id := Current_Scope;
1880 Decl_List : List_Id;
1881 begin
1882 Pop_Scope;
1883 Build_Procedure (Proc_Id, Decl_List);
1884 Insert_Actions (N, Decl_List);
1885 Push_Scope (Scope);
1886 end;
1888 -- Build the call to the generated procedure
1890 declare
1891 Actual_List : constant List_Id := New_List;
1892 Formal : Entity_Id;
1894 begin
1895 Append_To (Actual_List,
1896 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
1898 Formal := First_Formal (Spec_Id);
1899 while Present (Formal) loop
1900 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
1902 -- Avoid spurious warning on unreferenced formals
1904 Set_Referenced (Formal);
1905 Next_Formal (Formal);
1906 end loop;
1908 Proc_Call :=
1909 Make_Procedure_Call_Statement (Loc,
1910 Name => New_Occurrence_Of (Proc_Id, Loc),
1911 Parameter_Associations => Actual_List);
1912 end;
1914 -- Generate
1916 -- declare
1917 -- New_Obj : ...
1918 -- begin
1919 -- main_1__F1b (New_Obj, ...);
1920 -- return Obj;
1921 -- end B10b;
1923 Blk_Stmt :=
1924 Make_Block_Statement (Loc,
1925 Declarations => New_List (New_Obj),
1926 Handled_Statement_Sequence =>
1927 Make_Handled_Sequence_Of_Statements (Loc,
1928 Statements => New_List (
1930 Proc_Call,
1932 Make_Simple_Return_Statement (Loc,
1933 Expression =>
1934 New_Occurrence_Of
1935 (Defining_Identifier (New_Obj), Loc)))));
1937 Rewrite (Ret_Node, Blk_Stmt);
1938 end Split_Unconstrained_Function;
1940 -- Local variables
1942 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1944 -- Start of processing for Check_And_Split_Unconstrained_Function
1946 begin
1947 pragma Assert (Back_End_Inlining
1948 and then Ekind (Spec_Id) = E_Function
1949 and then Returns_Unconstrained_Type (Spec_Id)
1950 and then Comes_From_Source (Body_Id)
1951 and then (Has_Pragma_Inline_Always (Spec_Id)
1952 or else Optimization_Level > 0));
1954 -- This routine must not be used in GNATprove mode since GNATprove
1955 -- relies on frontend inlining
1957 pragma Assert (not GNATprove_Mode);
1959 -- No need to split the function if we cannot generate the code
1961 if Serious_Errors_Detected /= 0 then
1962 return;
1963 end if;
1965 -- Do not inline any subprogram that contains nested subprograms,
1966 -- since the backend inlining circuit seems to generate uninitialized
1967 -- references in this case. We know this happens in the case of front
1968 -- end ZCX support, but it also appears it can happen in other cases
1969 -- as well. The backend often rejects attempts to inline in the case
1970 -- of nested procedures anyway, so little if anything is lost by this.
1971 -- Note that this is test is for the benefit of the back-end. There
1972 -- is a separate test for front-end inlining that also rejects nested
1973 -- subprograms.
1975 -- Do not do this test if errors have been detected, because in some
1976 -- error cases, this code blows up, and we don't need it anyway if
1977 -- there have been errors, since we won't get to the linker anyway.
1979 declare
1980 P_Ent : Node_Id;
1982 begin
1983 P_Ent := Body_Id;
1984 loop
1985 P_Ent := Scope (P_Ent);
1986 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1988 if Is_Subprogram (P_Ent) then
1989 Set_Is_Inlined (P_Ent, False);
1991 if Comes_From_Source (P_Ent)
1992 and then (Has_Pragma_Inline (P_Ent))
1993 then
1994 Cannot_Inline
1995 ("cannot inline& (nested subprogram)?", N, P_Ent,
1996 Is_Serious => True);
1997 return;
1998 end if;
1999 end if;
2000 end loop;
2001 end;
2003 -- No action needed in stubs since the attribute Body_To_Inline
2004 -- is not available
2006 if Nkind (Decl) = N_Subprogram_Body_Stub then
2007 return;
2009 -- Cannot build the body to inline if the attribute is already set.
2010 -- This attribute may have been set if this is a subprogram renaming
2011 -- declarations (see Freeze.Build_Renamed_Body).
2013 elsif Present (Body_To_Inline (Decl)) then
2014 return;
2016 -- Check excluded declarations
2018 elsif Present (Declarations (N))
2019 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
2020 then
2021 return;
2023 -- Check excluded statements. There is no need to protect us against
2024 -- exception handlers since they are supported by the GCC backend.
2026 elsif Present (Handled_Statement_Sequence (N))
2027 and then Has_Excluded_Statement
2028 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2029 then
2030 return;
2031 end if;
2033 -- Build the body to inline only if really needed
2035 if Can_Split_Unconstrained_Function (N) then
2036 Split_Unconstrained_Function (N, Spec_Id);
2037 Build_Body_To_Inline (N, Spec_Id);
2038 Set_Is_Inlined (Spec_Id);
2039 end if;
2040 end Check_And_Split_Unconstrained_Function;
2042 -------------------------------------
2043 -- Check_Package_Body_For_Inlining --
2044 -------------------------------------
2046 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2047 Bname : Unit_Name_Type;
2048 E : Entity_Id;
2049 OK : Boolean;
2051 begin
2052 if Is_Compilation_Unit (P)
2053 and then not Is_Generic_Instance (P)
2054 then
2055 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2057 E := First_Entity (P);
2058 while Present (E) loop
2059 if Has_Pragma_Inline_Always (E)
2060 or else (Front_End_Inlining and then Has_Pragma_Inline (E))
2061 then
2062 if not Is_Loaded (Bname) then
2063 Load_Needed_Body (N, OK);
2065 if OK then
2067 -- Check we are not trying to inline a parent whose body
2068 -- depends on a child, when we are compiling the body of
2069 -- the child. Otherwise we have a potential elaboration
2070 -- circularity with inlined subprograms and with
2071 -- Taft-Amendment types.
2073 declare
2074 Comp : Node_Id; -- Body just compiled
2075 Child_Spec : Entity_Id; -- Spec of main unit
2076 Ent : Entity_Id; -- For iteration
2077 With_Clause : Node_Id; -- Context of body.
2079 begin
2080 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2081 and then Present (Body_Entity (P))
2082 then
2083 Child_Spec :=
2084 Defining_Entity
2085 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2087 Comp :=
2088 Parent (Unit_Declaration_Node (Body_Entity (P)));
2090 -- Check whether the context of the body just
2091 -- compiled includes a child of itself, and that
2092 -- child is the spec of the main compilation.
2094 With_Clause := First (Context_Items (Comp));
2095 while Present (With_Clause) loop
2096 if Nkind (With_Clause) = N_With_Clause
2097 and then
2098 Scope (Entity (Name (With_Clause))) = P
2099 and then
2100 Entity (Name (With_Clause)) = Child_Spec
2101 then
2102 Error_Msg_Node_2 := Child_Spec;
2103 Error_Msg_NE
2104 ("body of & depends on child unit&??",
2105 With_Clause, P);
2106 Error_Msg_N
2107 ("\subprograms in body cannot be inlined??",
2108 With_Clause);
2110 -- Disable further inlining from this unit,
2111 -- and keep Taft-amendment types incomplete.
2113 Ent := First_Entity (P);
2114 while Present (Ent) loop
2115 if Is_Type (Ent)
2116 and then Has_Completion_In_Body (Ent)
2117 then
2118 Set_Full_View (Ent, Empty);
2120 elsif Is_Subprogram (Ent) then
2121 Set_Is_Inlined (Ent, False);
2122 end if;
2124 Next_Entity (Ent);
2125 end loop;
2127 return;
2128 end if;
2130 Next (With_Clause);
2131 end loop;
2132 end if;
2133 end;
2135 elsif Ineffective_Inline_Warnings then
2136 Error_Msg_Unit_1 := Bname;
2137 Error_Msg_N
2138 ("unable to inline subprograms defined in $??", P);
2139 Error_Msg_N ("\body not found??", P);
2140 return;
2141 end if;
2142 end if;
2144 return;
2145 end if;
2147 Next_Entity (E);
2148 end loop;
2149 end if;
2150 end Check_Package_Body_For_Inlining;
2152 --------------------
2153 -- Cleanup_Scopes --
2154 --------------------
2156 procedure Cleanup_Scopes is
2157 Elmt : Elmt_Id;
2158 Decl : Node_Id;
2159 Scop : Entity_Id;
2161 begin
2162 Elmt := First_Elmt (To_Clean);
2163 while Present (Elmt) loop
2164 Scop := Node (Elmt);
2166 if Ekind (Scop) = E_Entry then
2167 Scop := Protected_Body_Subprogram (Scop);
2169 elsif Is_Subprogram (Scop)
2170 and then Is_Protected_Type (Scope (Scop))
2171 and then Present (Protected_Body_Subprogram (Scop))
2172 then
2173 -- If a protected operation contains an instance, its cleanup
2174 -- operations have been delayed, and the subprogram has been
2175 -- rewritten in the expansion of the enclosing protected body. It
2176 -- is the corresponding subprogram that may require the cleanup
2177 -- operations, so propagate the information that triggers cleanup
2178 -- activity.
2180 Set_Uses_Sec_Stack
2181 (Protected_Body_Subprogram (Scop),
2182 Uses_Sec_Stack (Scop));
2184 Scop := Protected_Body_Subprogram (Scop);
2185 end if;
2187 if Ekind (Scop) = E_Block then
2188 Decl := Parent (Block_Node (Scop));
2190 else
2191 Decl := Unit_Declaration_Node (Scop);
2193 if Nkind_In (Decl, N_Subprogram_Declaration,
2194 N_Task_Type_Declaration,
2195 N_Subprogram_Body_Stub)
2196 then
2197 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2198 end if;
2199 end if;
2201 Push_Scope (Scop);
2202 Expand_Cleanup_Actions (Decl);
2203 End_Scope;
2205 Elmt := Next_Elmt (Elmt);
2206 end loop;
2207 end Cleanup_Scopes;
2209 -------------------------
2210 -- Expand_Inlined_Call --
2211 -------------------------
2213 procedure Expand_Inlined_Call
2214 (N : Node_Id;
2215 Subp : Entity_Id;
2216 Orig_Subp : Entity_Id)
2218 Loc : constant Source_Ptr := Sloc (N);
2219 Is_Predef : constant Boolean :=
2220 Is_Predefined_File_Name
2221 (Unit_File_Name (Get_Source_Unit (Subp)));
2222 Orig_Bod : constant Node_Id :=
2223 Body_To_Inline (Unit_Declaration_Node (Subp));
2225 Blk : Node_Id;
2226 Decl : Node_Id;
2227 Decls : constant List_Id := New_List;
2228 Exit_Lab : Entity_Id := Empty;
2229 F : Entity_Id;
2230 A : Node_Id;
2231 Lab_Decl : Node_Id;
2232 Lab_Id : Node_Id;
2233 New_A : Node_Id;
2234 Num_Ret : Int := 0;
2235 Ret_Type : Entity_Id;
2237 Targ : Node_Id;
2238 -- The target of the call. If context is an assignment statement then
2239 -- this is the left-hand side of the assignment, else it is a temporary
2240 -- to which the return value is assigned prior to rewriting the call.
2242 Targ1 : Node_Id;
2243 -- A separate target used when the return type is unconstrained
2245 Temp : Entity_Id;
2246 Temp_Typ : Entity_Id;
2248 Return_Object : Entity_Id := Empty;
2249 -- Entity in declaration in an extended_return_statement
2251 Is_Unc : Boolean;
2252 Is_Unc_Decl : Boolean;
2253 -- If the type returned by the function is unconstrained and the call
2254 -- can be inlined, special processing is required.
2256 procedure Make_Exit_Label;
2257 -- Build declaration for exit label to be used in Return statements,
2258 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2259 -- declaration). Does nothing if Exit_Lab already set.
2261 function Process_Formals (N : Node_Id) return Traverse_Result;
2262 -- Replace occurrence of a formal with the corresponding actual, or the
2263 -- thunk generated for it. Replace a return statement with an assignment
2264 -- to the target of the call, with appropriate conversions if needed.
2266 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2267 -- If the call being expanded is that of an internal subprogram, set the
2268 -- sloc of the generated block to that of the call itself, so that the
2269 -- expansion is skipped by the "next" command in gdb. Same processing
2270 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2271 -- Debug_Generated_Code is true, suppress this change to simplify our
2272 -- own development. Same in GNATprove mode, to ensure that warnings and
2273 -- diagnostics point to the proper location.
2275 procedure Reset_Dispatching_Calls (N : Node_Id);
2276 -- In subtree N search for occurrences of dispatching calls that use the
2277 -- Ada 2005 Object.Operation notation and the object is a formal of the
2278 -- inlined subprogram. Reset the entity associated with Operation in all
2279 -- the found occurrences.
2281 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2282 -- If the function body is a single expression, replace call with
2283 -- expression, else insert block appropriately.
2285 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2286 -- If procedure body has no local variables, inline body without
2287 -- creating block, otherwise rewrite call with block.
2289 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2290 -- Determine whether a formal parameter is used only once in Orig_Bod
2292 ---------------------
2293 -- Make_Exit_Label --
2294 ---------------------
2296 procedure Make_Exit_Label is
2297 Lab_Ent : Entity_Id;
2298 begin
2299 if No (Exit_Lab) then
2300 Lab_Ent := Make_Temporary (Loc, 'L');
2301 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
2302 Exit_Lab := Make_Label (Loc, Lab_Id);
2303 Lab_Decl :=
2304 Make_Implicit_Label_Declaration (Loc,
2305 Defining_Identifier => Lab_Ent,
2306 Label_Construct => Exit_Lab);
2307 end if;
2308 end Make_Exit_Label;
2310 ---------------------
2311 -- Process_Formals --
2312 ---------------------
2314 function Process_Formals (N : Node_Id) return Traverse_Result is
2315 A : Entity_Id;
2316 E : Entity_Id;
2317 Ret : Node_Id;
2319 begin
2320 if Is_Entity_Name (N) and then Present (Entity (N)) then
2321 E := Entity (N);
2323 if Is_Formal (E) and then Scope (E) = Subp then
2324 A := Renamed_Object (E);
2326 -- Rewrite the occurrence of the formal into an occurrence of
2327 -- the actual. Also establish visibility on the proper view of
2328 -- the actual's subtype for the body's context (if the actual's
2329 -- subtype is private at the call point but its full view is
2330 -- visible to the body, then the inlined tree here must be
2331 -- analyzed with the full view).
2333 if Is_Entity_Name (A) then
2334 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2335 Check_Private_View (N);
2337 elsif Nkind (A) = N_Defining_Identifier then
2338 Rewrite (N, New_Occurrence_Of (A, Loc));
2339 Check_Private_View (N);
2341 -- Numeric literal
2343 else
2344 Rewrite (N, New_Copy (A));
2345 end if;
2346 end if;
2348 return Skip;
2350 elsif Is_Entity_Name (N)
2351 and then Present (Return_Object)
2352 and then Chars (N) = Chars (Return_Object)
2353 then
2354 -- Occurrence within an extended return statement. The return
2355 -- object is local to the body been inlined, and thus the generic
2356 -- copy is not analyzed yet, so we match by name, and replace it
2357 -- with target of call.
2359 if Nkind (Targ) = N_Defining_Identifier then
2360 Rewrite (N, New_Occurrence_Of (Targ, Loc));
2361 else
2362 Rewrite (N, New_Copy_Tree (Targ));
2363 end if;
2365 return Skip;
2367 elsif Nkind (N) = N_Simple_Return_Statement then
2368 if No (Expression (N)) then
2369 Make_Exit_Label;
2370 Rewrite (N,
2371 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2373 else
2374 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2375 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2376 then
2377 -- Function body is a single expression. No need for
2378 -- exit label.
2380 null;
2382 else
2383 Num_Ret := Num_Ret + 1;
2384 Make_Exit_Label;
2385 end if;
2387 -- Because of the presence of private types, the views of the
2388 -- expression and the context may be different, so place an
2389 -- unchecked conversion to the context type to avoid spurious
2390 -- errors, e.g. when the expression is a numeric literal and
2391 -- the context is private. If the expression is an aggregate,
2392 -- use a qualified expression, because an aggregate is not a
2393 -- legal argument of a conversion. Ditto for numeric literals,
2394 -- which must be resolved to a specific type.
2396 if Nkind_In (Expression (N), N_Aggregate,
2397 N_Null,
2398 N_Real_Literal,
2399 N_Integer_Literal)
2400 then
2401 Ret :=
2402 Make_Qualified_Expression (Sloc (N),
2403 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2404 Expression => Relocate_Node (Expression (N)));
2405 else
2406 Ret :=
2407 Unchecked_Convert_To
2408 (Ret_Type, Relocate_Node (Expression (N)));
2409 end if;
2411 if Nkind (Targ) = N_Defining_Identifier then
2412 Rewrite (N,
2413 Make_Assignment_Statement (Loc,
2414 Name => New_Occurrence_Of (Targ, Loc),
2415 Expression => Ret));
2416 else
2417 Rewrite (N,
2418 Make_Assignment_Statement (Loc,
2419 Name => New_Copy (Targ),
2420 Expression => Ret));
2421 end if;
2423 Set_Assignment_OK (Name (N));
2425 if Present (Exit_Lab) then
2426 Insert_After (N,
2427 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2428 end if;
2429 end if;
2431 return OK;
2433 -- An extended return becomes a block whose first statement is the
2434 -- assignment of the initial expression of the return object to the
2435 -- target of the call itself.
2437 elsif Nkind (N) = N_Extended_Return_Statement then
2438 declare
2439 Return_Decl : constant Entity_Id :=
2440 First (Return_Object_Declarations (N));
2441 Assign : Node_Id;
2443 begin
2444 Return_Object := Defining_Identifier (Return_Decl);
2446 if Present (Expression (Return_Decl)) then
2447 if Nkind (Targ) = N_Defining_Identifier then
2448 Assign :=
2449 Make_Assignment_Statement (Loc,
2450 Name => New_Occurrence_Of (Targ, Loc),
2451 Expression => Expression (Return_Decl));
2452 else
2453 Assign :=
2454 Make_Assignment_Statement (Loc,
2455 Name => New_Copy (Targ),
2456 Expression => Expression (Return_Decl));
2457 end if;
2459 Set_Assignment_OK (Name (Assign));
2461 if No (Handled_Statement_Sequence (N)) then
2462 Set_Handled_Statement_Sequence (N,
2463 Make_Handled_Sequence_Of_Statements (Loc,
2464 Statements => New_List));
2465 end if;
2467 Prepend (Assign,
2468 Statements (Handled_Statement_Sequence (N)));
2469 end if;
2471 Rewrite (N,
2472 Make_Block_Statement (Loc,
2473 Handled_Statement_Sequence =>
2474 Handled_Statement_Sequence (N)));
2476 return OK;
2477 end;
2479 -- Remove pragma Unreferenced since it may refer to formals that
2480 -- are not visible in the inlined body, and in any case we will
2481 -- not be posting warnings on the inlined body so it is unneeded.
2483 elsif Nkind (N) = N_Pragma
2484 and then Pragma_Name (N) = Name_Unreferenced
2485 then
2486 Rewrite (N, Make_Null_Statement (Sloc (N)));
2487 return OK;
2489 else
2490 return OK;
2491 end if;
2492 end Process_Formals;
2494 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2496 ------------------
2497 -- Process_Sloc --
2498 ------------------
2500 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2501 begin
2502 if not Debug_Generated_Code then
2503 Set_Sloc (Nod, Sloc (N));
2504 Set_Comes_From_Source (Nod, False);
2505 end if;
2507 return OK;
2508 end Process_Sloc;
2510 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2512 ------------------------------
2513 -- Reset_Dispatching_Calls --
2514 ------------------------------
2516 procedure Reset_Dispatching_Calls (N : Node_Id) is
2518 function Do_Reset (N : Node_Id) return Traverse_Result;
2519 -- Comment required ???
2521 --------------
2522 -- Do_Reset --
2523 --------------
2525 function Do_Reset (N : Node_Id) return Traverse_Result is
2526 begin
2527 if Nkind (N) = N_Procedure_Call_Statement
2528 and then Nkind (Name (N)) = N_Selected_Component
2529 and then Nkind (Prefix (Name (N))) = N_Identifier
2530 and then Is_Formal (Entity (Prefix (Name (N))))
2531 and then Is_Dispatching_Operation
2532 (Entity (Selector_Name (Name (N))))
2533 then
2534 Set_Entity (Selector_Name (Name (N)), Empty);
2535 end if;
2537 return OK;
2538 end Do_Reset;
2540 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
2542 -- Local variables
2544 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
2545 pragma Unreferenced (Dummy);
2547 -- Start of processing for Reset_Dispatching_Calls
2549 begin
2550 null;
2551 end Reset_Dispatching_Calls;
2553 ---------------------------
2554 -- Rewrite_Function_Call --
2555 ---------------------------
2557 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2558 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2559 Fst : constant Node_Id := First (Statements (HSS));
2561 begin
2562 -- Optimize simple case: function body is a single return statement,
2563 -- which has been expanded into an assignment.
2565 if Is_Empty_List (Declarations (Blk))
2566 and then Nkind (Fst) = N_Assignment_Statement
2567 and then No (Next (Fst))
2568 then
2569 -- The function call may have been rewritten as the temporary
2570 -- that holds the result of the call, in which case remove the
2571 -- now useless declaration.
2573 if Nkind (N) = N_Identifier
2574 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2575 then
2576 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2577 end if;
2579 Rewrite (N, Expression (Fst));
2581 elsif Nkind (N) = N_Identifier
2582 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2583 then
2584 -- The block assigns the result of the call to the temporary
2586 Insert_After (Parent (Entity (N)), Blk);
2588 -- If the context is an assignment, and the left-hand side is free of
2589 -- side-effects, the replacement is also safe.
2590 -- Can this be generalized further???
2592 elsif Nkind (Parent (N)) = N_Assignment_Statement
2593 and then
2594 (Is_Entity_Name (Name (Parent (N)))
2595 or else
2596 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
2597 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
2599 or else
2600 (Nkind (Name (Parent (N))) = N_Selected_Component
2601 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
2602 then
2603 -- Replace assignment with the block
2605 declare
2606 Original_Assignment : constant Node_Id := Parent (N);
2608 begin
2609 -- Preserve the original assignment node to keep the complete
2610 -- assignment subtree consistent enough for Analyze_Assignment
2611 -- to proceed (specifically, the original Lhs node must still
2612 -- have an assignment statement as its parent).
2614 -- We cannot rely on Original_Node to go back from the block
2615 -- node to the assignment node, because the assignment might
2616 -- already be a rewrite substitution.
2618 Discard_Node (Relocate_Node (Original_Assignment));
2619 Rewrite (Original_Assignment, Blk);
2620 end;
2622 elsif Nkind (Parent (N)) = N_Object_Declaration then
2624 -- A call to a function which returns an unconstrained type
2625 -- found in the expression initializing an object-declaration is
2626 -- expanded into a procedure call which must be added after the
2627 -- object declaration.
2629 if Is_Unc_Decl and Back_End_Inlining then
2630 Insert_Action_After (Parent (N), Blk);
2631 else
2632 Set_Expression (Parent (N), Empty);
2633 Insert_After (Parent (N), Blk);
2634 end if;
2636 elsif Is_Unc and then not Back_End_Inlining then
2637 Insert_Before (Parent (N), Blk);
2638 end if;
2639 end Rewrite_Function_Call;
2641 ----------------------------
2642 -- Rewrite_Procedure_Call --
2643 ----------------------------
2645 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2646 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2648 begin
2649 -- If there is a transient scope for N, this will be the scope of the
2650 -- actions for N, and the statements in Blk need to be within this
2651 -- scope. For example, they need to have visibility on the constant
2652 -- declarations created for the formals.
2654 -- If N needs no transient scope, and if there are no declarations in
2655 -- the inlined body, we can do a little optimization and insert the
2656 -- statements for the body directly after N, and rewrite N to a
2657 -- null statement, instead of rewriting N into a full-blown block
2658 -- statement.
2660 if not Scope_Is_Transient
2661 and then Is_Empty_List (Declarations (Blk))
2662 then
2663 Insert_List_After (N, Statements (HSS));
2664 Rewrite (N, Make_Null_Statement (Loc));
2665 else
2666 Rewrite (N, Blk);
2667 end if;
2668 end Rewrite_Procedure_Call;
2670 -------------------------
2671 -- Formal_Is_Used_Once --
2672 -------------------------
2674 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2675 Use_Counter : Int := 0;
2677 function Count_Uses (N : Node_Id) return Traverse_Result;
2678 -- Traverse the tree and count the uses of the formal parameter.
2679 -- In this case, for optimization purposes, we do not need to
2680 -- continue the traversal once more than one use is encountered.
2682 ----------------
2683 -- Count_Uses --
2684 ----------------
2686 function Count_Uses (N : Node_Id) return Traverse_Result is
2687 begin
2688 -- The original node is an identifier
2690 if Nkind (N) = N_Identifier
2691 and then Present (Entity (N))
2693 -- Original node's entity points to the one in the copied body
2695 and then Nkind (Entity (N)) = N_Identifier
2696 and then Present (Entity (Entity (N)))
2698 -- The entity of the copied node is the formal parameter
2700 and then Entity (Entity (N)) = Formal
2701 then
2702 Use_Counter := Use_Counter + 1;
2704 if Use_Counter > 1 then
2706 -- Denote more than one use and abandon the traversal
2708 Use_Counter := 2;
2709 return Abandon;
2711 end if;
2712 end if;
2714 return OK;
2715 end Count_Uses;
2717 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2719 -- Start of processing for Formal_Is_Used_Once
2721 begin
2722 Count_Formal_Uses (Orig_Bod);
2723 return Use_Counter = 1;
2724 end Formal_Is_Used_Once;
2726 -- Start of processing for Expand_Inlined_Call
2728 begin
2729 -- Initializations for old/new semantics
2731 if not Back_End_Inlining then
2732 Is_Unc := Is_Array_Type (Etype (Subp))
2733 and then not Is_Constrained (Etype (Subp));
2734 Is_Unc_Decl := False;
2735 else
2736 Is_Unc := Returns_Unconstrained_Type (Subp)
2737 and then Optimization_Level > 0;
2738 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
2739 and then Is_Unc;
2740 end if;
2742 -- Check for an illegal attempt to inline a recursive procedure. If the
2743 -- subprogram has parameters this is detected when trying to supply a
2744 -- binding for parameters that already have one. For parameterless
2745 -- subprograms this must be done explicitly.
2747 if In_Open_Scopes (Subp) then
2748 Error_Msg_N ("call to recursive subprogram cannot be inlined??", N);
2749 Set_Is_Inlined (Subp, False);
2751 -- In GNATprove mode, issue a warning, and indicate that the
2752 -- subprogram is not always inlined by setting flag Is_Inlined_Always
2753 -- to False.
2755 if GNATprove_Mode then
2756 Set_Is_Inlined_Always (Subp, False);
2757 end if;
2759 return;
2761 -- Skip inlining if this is not a true inlining since the attribute
2762 -- Body_To_Inline is also set for renamings (see sinfo.ads)
2764 elsif Nkind (Orig_Bod) in N_Entity then
2765 return;
2767 -- Skip inlining if the function returns an unconstrained type using
2768 -- an extended return statement since this part of the new inlining
2769 -- model which is not yet supported by the current implementation. ???
2771 elsif Is_Unc
2772 and then
2773 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod))))
2774 = N_Extended_Return_Statement
2775 and then not Back_End_Inlining
2776 then
2777 return;
2778 end if;
2780 if Nkind (Orig_Bod) = N_Defining_Identifier
2781 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2782 then
2783 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2784 -- can be replaced with calls to the renamed entity directly, because
2785 -- the subprograms are subtype conformant. If the renamed subprogram
2786 -- is an inherited operation, we must redo the expansion because
2787 -- implicit conversions may be needed. Similarly, if the renamed
2788 -- entity is inlined, expand the call for further optimizations.
2790 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2792 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
2793 Expand_Call (N);
2794 end if;
2796 return;
2797 end if;
2799 -- Register the call in the list of inlined calls
2801 Append_New_Elmt (N, To => Inlined_Calls);
2803 -- Use generic machinery to copy body of inlined subprogram, as if it
2804 -- were an instantiation, resetting source locations appropriately, so
2805 -- that nested inlined calls appear in the main unit.
2807 Save_Env (Subp, Empty);
2808 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2810 -- Old semantics
2812 if not Back_End_Inlining then
2813 declare
2814 Bod : Node_Id;
2816 begin
2817 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2818 Blk :=
2819 Make_Block_Statement (Loc,
2820 Declarations => Declarations (Bod),
2821 Handled_Statement_Sequence =>
2822 Handled_Statement_Sequence (Bod));
2824 if No (Declarations (Bod)) then
2825 Set_Declarations (Blk, New_List);
2826 end if;
2828 -- For the unconstrained case, capture the name of the local
2829 -- variable that holds the result. This must be the first
2830 -- declaration in the block, because its bounds cannot depend
2831 -- on local variables. Otherwise there is no way to declare the
2832 -- result outside of the block. Needless to say, in general the
2833 -- bounds will depend on the actuals in the call.
2835 -- If the context is an assignment statement, as is the case
2836 -- for the expansion of an extended return, the left-hand side
2837 -- provides bounds even if the return type is unconstrained.
2839 if Is_Unc then
2840 declare
2841 First_Decl : Node_Id;
2843 begin
2844 First_Decl := First (Declarations (Blk));
2846 if Nkind (First_Decl) /= N_Object_Declaration then
2847 return;
2848 end if;
2850 if Nkind (Parent (N)) /= N_Assignment_Statement then
2851 Targ1 := Defining_Identifier (First_Decl);
2852 else
2853 Targ1 := Name (Parent (N));
2854 end if;
2855 end;
2856 end if;
2857 end;
2859 -- New semantics
2861 else
2862 declare
2863 Bod : Node_Id;
2865 begin
2866 -- General case
2868 if not Is_Unc then
2869 Bod :=
2870 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2871 Blk :=
2872 Make_Block_Statement (Loc,
2873 Declarations => Declarations (Bod),
2874 Handled_Statement_Sequence =>
2875 Handled_Statement_Sequence (Bod));
2877 -- Inline a call to a function that returns an unconstrained type.
2878 -- The semantic analyzer checked that frontend-inlined functions
2879 -- returning unconstrained types have no declarations and have
2880 -- a single extended return statement. As part of its processing
2881 -- the function was split in two subprograms: a procedure P and
2882 -- a function F that has a block with a call to procedure P (see
2883 -- Split_Unconstrained_Function).
2885 else
2886 pragma Assert
2887 (Nkind
2888 (First
2889 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
2890 N_Block_Statement);
2892 declare
2893 Blk_Stmt : constant Node_Id :=
2894 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
2895 First_Stmt : constant Node_Id :=
2896 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
2897 Second_Stmt : constant Node_Id := Next (First_Stmt);
2899 begin
2900 pragma Assert
2901 (Nkind (First_Stmt) = N_Procedure_Call_Statement
2902 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
2903 and then No (Next (Second_Stmt)));
2905 Bod :=
2906 Copy_Generic_Node
2907 (First
2908 (Statements (Handled_Statement_Sequence (Orig_Bod))),
2909 Empty, Instantiating => True);
2910 Blk := Bod;
2912 -- Capture the name of the local variable that holds the
2913 -- result. This must be the first declaration in the block,
2914 -- because its bounds cannot depend on local variables.
2915 -- Otherwise there is no way to declare the result outside
2916 -- of the block. Needless to say, in general the bounds will
2917 -- depend on the actuals in the call.
2919 if Nkind (Parent (N)) /= N_Assignment_Statement then
2920 Targ1 := Defining_Identifier (First (Declarations (Blk)));
2922 -- If the context is an assignment statement, as is the case
2923 -- for the expansion of an extended return, the left-hand
2924 -- side provides bounds even if the return type is
2925 -- unconstrained.
2927 else
2928 Targ1 := Name (Parent (N));
2929 end if;
2930 end;
2931 end if;
2933 if No (Declarations (Bod)) then
2934 Set_Declarations (Blk, New_List);
2935 end if;
2936 end;
2937 end if;
2939 -- If this is a derived function, establish the proper return type
2941 if Present (Orig_Subp) and then Orig_Subp /= Subp then
2942 Ret_Type := Etype (Orig_Subp);
2943 else
2944 Ret_Type := Etype (Subp);
2945 end if;
2947 -- Create temporaries for the actuals that are expressions, or that are
2948 -- scalars and require copying to preserve semantics.
2950 F := First_Formal (Subp);
2951 A := First_Actual (N);
2952 while Present (F) loop
2953 if Present (Renamed_Object (F)) then
2955 -- If expander is active, it is an error to try to inline a
2956 -- recursive program. In GNATprove mode, just indicate that the
2957 -- inlining will not happen, and mark the subprogram as not always
2958 -- inlined.
2960 if GNATprove_Mode then
2961 Cannot_Inline
2962 ("cannot inline call to recursive subprogram?", N, Subp);
2963 Set_Is_Inlined_Always (Subp, False);
2964 else
2965 Error_Msg_N
2966 ("cannot inline call to recursive subprogram", N);
2967 end if;
2969 return;
2970 end if;
2972 -- Reset Last_Assignment for any parameters of mode out or in out, to
2973 -- prevent spurious warnings about overwriting for assignments to the
2974 -- formal in the inlined code.
2976 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
2977 Set_Last_Assignment (Entity (A), Empty);
2978 end if;
2980 -- If the argument may be a controlling argument in a call within
2981 -- the inlined body, we must preserve its classwide nature to insure
2982 -- that dynamic dispatching take place subsequently. If the formal
2983 -- has a constraint it must be preserved to retain the semantics of
2984 -- the body.
2986 if Is_Class_Wide_Type (Etype (F))
2987 or else (Is_Access_Type (Etype (F))
2988 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
2989 then
2990 Temp_Typ := Etype (F);
2992 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
2993 and then Etype (F) /= Base_Type (Etype (F))
2994 then
2995 Temp_Typ := Etype (F);
2996 else
2997 Temp_Typ := Etype (A);
2998 end if;
3000 -- If the actual is a simple name or a literal, no need to
3001 -- create a temporary, object can be used directly.
3003 -- If the actual is a literal and the formal has its address taken,
3004 -- we cannot pass the literal itself as an argument, so its value
3005 -- must be captured in a temporary.
3007 if (Is_Entity_Name (A)
3008 and then
3009 (not Is_Scalar_Type (Etype (A))
3010 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3012 -- When the actual is an identifier and the corresponding formal is
3013 -- used only once in the original body, the formal can be substituted
3014 -- directly with the actual parameter.
3016 or else (Nkind (A) = N_Identifier
3017 and then Formal_Is_Used_Once (F))
3019 or else
3020 (Nkind_In (A, N_Real_Literal,
3021 N_Integer_Literal,
3022 N_Character_Literal)
3023 and then not Address_Taken (F))
3024 then
3025 if Etype (F) /= Etype (A) then
3026 Set_Renamed_Object
3027 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3028 else
3029 Set_Renamed_Object (F, A);
3030 end if;
3032 else
3033 Temp := Make_Temporary (Loc, 'C');
3035 -- If the actual for an in/in-out parameter is a view conversion,
3036 -- make it into an unchecked conversion, given that an untagged
3037 -- type conversion is not a proper object for a renaming.
3039 -- In-out conversions that involve real conversions have already
3040 -- been transformed in Expand_Actuals.
3042 if Nkind (A) = N_Type_Conversion
3043 and then Ekind (F) /= E_In_Parameter
3044 then
3045 New_A :=
3046 Make_Unchecked_Type_Conversion (Loc,
3047 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3048 Expression => Relocate_Node (Expression (A)));
3050 elsif Etype (F) /= Etype (A) then
3051 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3052 Temp_Typ := Etype (F);
3054 else
3055 New_A := Relocate_Node (A);
3056 end if;
3058 Set_Sloc (New_A, Sloc (N));
3060 -- If the actual has a by-reference type, it cannot be copied,
3061 -- so its value is captured in a renaming declaration. Otherwise
3062 -- declare a local constant initialized with the actual.
3064 -- We also use a renaming declaration for expressions of an array
3065 -- type that is not bit-packed, both for efficiency reasons and to
3066 -- respect the semantics of the call: in most cases the original
3067 -- call will pass the parameter by reference, and thus the inlined
3068 -- code will have the same semantics.
3070 -- Finally, we need a renaming declaration in the case of limited
3071 -- types for which initialization cannot be by copy either.
3073 if Ekind (F) = E_In_Parameter
3074 and then not Is_By_Reference_Type (Etype (A))
3075 and then not Is_Limited_Type (Etype (A))
3076 and then
3077 (not Is_Array_Type (Etype (A))
3078 or else not Is_Object_Reference (A)
3079 or else Is_Bit_Packed_Array (Etype (A)))
3080 then
3081 Decl :=
3082 Make_Object_Declaration (Loc,
3083 Defining_Identifier => Temp,
3084 Constant_Present => True,
3085 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3086 Expression => New_A);
3087 else
3088 Decl :=
3089 Make_Object_Renaming_Declaration (Loc,
3090 Defining_Identifier => Temp,
3091 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3092 Name => New_A);
3093 end if;
3095 Append (Decl, Decls);
3096 Set_Renamed_Object (F, Temp);
3097 end if;
3099 Next_Formal (F);
3100 Next_Actual (A);
3101 end loop;
3103 -- Establish target of function call. If context is not assignment or
3104 -- declaration, create a temporary as a target. The declaration for the
3105 -- temporary may be subsequently optimized away if the body is a single
3106 -- expression, or if the left-hand side of the assignment is simple
3107 -- enough, i.e. an entity or an explicit dereference of one.
3109 if Ekind (Subp) = E_Function then
3110 if Nkind (Parent (N)) = N_Assignment_Statement
3111 and then Is_Entity_Name (Name (Parent (N)))
3112 then
3113 Targ := Name (Parent (N));
3115 elsif Nkind (Parent (N)) = N_Assignment_Statement
3116 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3117 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3118 then
3119 Targ := Name (Parent (N));
3121 elsif Nkind (Parent (N)) = N_Assignment_Statement
3122 and then Nkind (Name (Parent (N))) = N_Selected_Component
3123 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3124 then
3125 Targ := New_Copy_Tree (Name (Parent (N)));
3127 elsif Nkind (Parent (N)) = N_Object_Declaration
3128 and then Is_Limited_Type (Etype (Subp))
3129 then
3130 Targ := Defining_Identifier (Parent (N));
3132 -- New semantics: In an object declaration avoid an extra copy
3133 -- of the result of a call to an inlined function that returns
3134 -- an unconstrained type
3136 elsif Back_End_Inlining
3137 and then Nkind (Parent (N)) = N_Object_Declaration
3138 and then Is_Unc
3139 then
3140 Targ := Defining_Identifier (Parent (N));
3142 else
3143 -- Replace call with temporary and create its declaration
3145 Temp := Make_Temporary (Loc, 'C');
3146 Set_Is_Internal (Temp);
3148 -- For the unconstrained case, the generated temporary has the
3149 -- same constrained declaration as the result variable. It may
3150 -- eventually be possible to remove that temporary and use the
3151 -- result variable directly.
3153 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
3154 then
3155 Decl :=
3156 Make_Object_Declaration (Loc,
3157 Defining_Identifier => Temp,
3158 Object_Definition =>
3159 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3161 Replace_Formals (Decl);
3163 else
3164 Decl :=
3165 Make_Object_Declaration (Loc,
3166 Defining_Identifier => Temp,
3167 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
3169 Set_Etype (Temp, Ret_Type);
3170 end if;
3172 Set_No_Initialization (Decl);
3173 Append (Decl, Decls);
3174 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3175 Targ := Temp;
3176 end if;
3177 end if;
3179 Insert_Actions (N, Decls);
3181 if Is_Unc_Decl then
3183 -- Special management for inlining a call to a function that returns
3184 -- an unconstrained type and initializes an object declaration: we
3185 -- avoid generating undesired extra calls and goto statements.
3187 -- Given:
3188 -- function Func (...) return ...
3189 -- begin
3190 -- declare
3191 -- Result : String (1 .. 4);
3192 -- begin
3193 -- Proc (Result, ...);
3194 -- return Result;
3195 -- end;
3196 -- end F;
3198 -- Result : String := Func (...);
3200 -- Replace this object declaration by:
3202 -- Result : String (1 .. 4);
3203 -- Proc (Result, ...);
3205 Remove_Homonym (Targ);
3207 Decl :=
3208 Make_Object_Declaration
3209 (Loc,
3210 Defining_Identifier => Targ,
3211 Object_Definition =>
3212 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3213 Replace_Formals (Decl);
3214 Rewrite (Parent (N), Decl);
3215 Analyze (Parent (N));
3217 -- Avoid spurious warnings since we know that this declaration is
3218 -- referenced by the procedure call.
3220 Set_Never_Set_In_Source (Targ, False);
3222 -- Remove the local declaration of the extended return stmt from the
3223 -- inlined code
3225 Remove (Parent (Targ1));
3227 -- Update the reference to the result (since we have rewriten the
3228 -- object declaration)
3230 declare
3231 Blk_Call_Stmt : Node_Id;
3233 begin
3234 -- Capture the call to the procedure
3236 Blk_Call_Stmt :=
3237 First (Statements (Handled_Statement_Sequence (Blk)));
3238 pragma Assert
3239 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
3241 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
3242 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
3243 New_Occurrence_Of (Targ, Loc));
3244 end;
3246 -- Remove the return statement
3248 pragma Assert
3249 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3250 N_Simple_Return_Statement);
3252 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3253 end if;
3255 -- Traverse the tree and replace formals with actuals or their thunks.
3256 -- Attach block to tree before analysis and rewriting.
3258 Replace_Formals (Blk);
3259 Set_Parent (Blk, N);
3261 if GNATprove_Mode then
3262 null;
3264 elsif not Comes_From_Source (Subp) or else Is_Predef then
3265 Reset_Slocs (Blk);
3266 end if;
3268 if Is_Unc_Decl then
3270 -- No action needed since return statement has been already removed
3272 null;
3274 elsif Present (Exit_Lab) then
3276 -- If the body was a single expression, the single return statement
3277 -- and the corresponding label are useless.
3279 if Num_Ret = 1
3280 and then
3281 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3282 N_Goto_Statement
3283 then
3284 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3285 else
3286 Append (Lab_Decl, (Declarations (Blk)));
3287 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3288 end if;
3289 end if;
3291 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3292 -- on conflicting private views that Gigi would ignore. If this is a
3293 -- predefined unit, analyze with checks off, as is done in the non-
3294 -- inlined run-time units.
3296 declare
3297 I_Flag : constant Boolean := In_Inlined_Body;
3299 begin
3300 In_Inlined_Body := True;
3302 if Is_Predef then
3303 declare
3304 Style : constant Boolean := Style_Check;
3306 begin
3307 Style_Check := False;
3309 -- Search for dispatching calls that use the Object.Operation
3310 -- notation using an Object that is a parameter of the inlined
3311 -- function. We reset the decoration of Operation to force
3312 -- the reanalysis of the inlined dispatching call because
3313 -- the actual object has been inlined.
3315 Reset_Dispatching_Calls (Blk);
3317 Analyze (Blk, Suppress => All_Checks);
3318 Style_Check := Style;
3319 end;
3321 else
3322 Analyze (Blk);
3323 end if;
3325 In_Inlined_Body := I_Flag;
3326 end;
3328 if Ekind (Subp) = E_Procedure then
3329 Rewrite_Procedure_Call (N, Blk);
3331 else
3332 Rewrite_Function_Call (N, Blk);
3334 if Is_Unc_Decl then
3335 null;
3337 -- For the unconstrained case, the replacement of the call has been
3338 -- made prior to the complete analysis of the generated declarations.
3339 -- Propagate the proper type now.
3341 elsif Is_Unc then
3342 if Nkind (N) = N_Identifier then
3343 Set_Etype (N, Etype (Entity (N)));
3344 else
3345 Set_Etype (N, Etype (Targ1));
3346 end if;
3347 end if;
3348 end if;
3350 Restore_Env;
3352 -- Cleanup mapping between formals and actuals for other expansions
3354 F := First_Formal (Subp);
3355 while Present (F) loop
3356 Set_Renamed_Object (F, Empty);
3357 Next_Formal (F);
3358 end loop;
3359 end Expand_Inlined_Call;
3361 --------------------------
3362 -- Get_Code_Unit_Entity --
3363 --------------------------
3365 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
3366 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
3368 begin
3369 if Ekind (Unit) = E_Package_Body then
3370 Unit := Spec_Entity (Unit);
3371 end if;
3373 return Unit;
3374 end Get_Code_Unit_Entity;
3376 ------------------------------
3377 -- Has_Excluded_Declaration --
3378 ------------------------------
3380 function Has_Excluded_Declaration
3381 (Subp : Entity_Id;
3382 Decls : List_Id) return Boolean
3384 D : Node_Id;
3386 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3387 -- Nested subprograms make a given body ineligible for inlining, but
3388 -- we make an exception for instantiations of unchecked conversion.
3389 -- The body has not been analyzed yet, so check the name, and verify
3390 -- that the visible entity with that name is the predefined unit.
3392 -----------------------------
3393 -- Is_Unchecked_Conversion --
3394 -----------------------------
3396 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3397 Id : constant Node_Id := Name (D);
3398 Conv : Entity_Id;
3400 begin
3401 if Nkind (Id) = N_Identifier
3402 and then Chars (Id) = Name_Unchecked_Conversion
3403 then
3404 Conv := Current_Entity (Id);
3406 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3407 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3408 then
3409 Conv := Current_Entity (Selector_Name (Id));
3410 else
3411 return False;
3412 end if;
3414 return Present (Conv)
3415 and then Is_Predefined_File_Name
3416 (Unit_File_Name (Get_Source_Unit (Conv)))
3417 and then Is_Intrinsic_Subprogram (Conv);
3418 end Is_Unchecked_Conversion;
3420 -- Start of processing for Has_Excluded_Declaration
3422 begin
3423 -- No action needed if the check is not needed
3425 if not Check_Inlining_Restrictions then
3426 return False;
3427 end if;
3429 D := First (Decls);
3430 while Present (D) loop
3431 if Nkind (D) = N_Subprogram_Body then
3432 Cannot_Inline
3433 ("cannot inline & (nested subprogram)?",
3434 D, Subp);
3435 return True;
3437 elsif Nkind (D) = N_Task_Type_Declaration
3438 or else Nkind (D) = N_Single_Task_Declaration
3439 then
3440 Cannot_Inline
3441 ("cannot inline & (nested task type declaration)?",
3442 D, Subp);
3443 return True;
3445 elsif Nkind (D) = N_Protected_Type_Declaration
3446 or else Nkind (D) = N_Single_Protected_Declaration
3447 then
3448 Cannot_Inline
3449 ("cannot inline & (nested protected type declaration)?",
3450 D, Subp);
3451 return True;
3453 elsif Nkind (D) = N_Package_Declaration then
3454 Cannot_Inline
3455 ("cannot inline & (nested package declaration)?",
3456 D, Subp);
3457 return True;
3459 elsif Nkind (D) = N_Function_Instantiation
3460 and then not Is_Unchecked_Conversion (D)
3461 then
3462 Cannot_Inline
3463 ("cannot inline & (nested function instantiation)?",
3464 D, Subp);
3465 return True;
3467 elsif Nkind (D) = N_Procedure_Instantiation then
3468 Cannot_Inline
3469 ("cannot inline & (nested procedure instantiation)?",
3470 D, Subp);
3471 return True;
3473 elsif Nkind (D) = N_Package_Instantiation then
3474 Cannot_Inline
3475 ("cannot inline & (nested package instantiation)?",
3476 D, Subp);
3477 return True;
3478 end if;
3480 Next (D);
3481 end loop;
3483 return False;
3484 end Has_Excluded_Declaration;
3486 ----------------------------
3487 -- Has_Excluded_Statement --
3488 ----------------------------
3490 function Has_Excluded_Statement
3491 (Subp : Entity_Id;
3492 Stats : List_Id) return Boolean
3494 S : Node_Id;
3495 E : Node_Id;
3497 begin
3498 -- No action needed if the check is not needed
3500 if not Check_Inlining_Restrictions then
3501 return False;
3502 end if;
3504 S := First (Stats);
3505 while Present (S) loop
3506 if Nkind_In (S, N_Abort_Statement,
3507 N_Asynchronous_Select,
3508 N_Conditional_Entry_Call,
3509 N_Delay_Relative_Statement,
3510 N_Delay_Until_Statement,
3511 N_Selective_Accept,
3512 N_Timed_Entry_Call)
3513 then
3514 Cannot_Inline
3515 ("cannot inline & (non-allowed statement)?", S, Subp);
3516 return True;
3518 elsif Nkind (S) = N_Block_Statement then
3519 if Present (Declarations (S))
3520 and then Has_Excluded_Declaration (Subp, Declarations (S))
3521 then
3522 return True;
3524 elsif Present (Handled_Statement_Sequence (S)) then
3525 if not Back_End_Inlining
3526 and then
3527 Present
3528 (Exception_Handlers (Handled_Statement_Sequence (S)))
3529 then
3530 Cannot_Inline
3531 ("cannot inline& (exception handler)?",
3532 First (Exception_Handlers
3533 (Handled_Statement_Sequence (S))),
3534 Subp);
3535 return True;
3537 elsif Has_Excluded_Statement
3538 (Subp, Statements (Handled_Statement_Sequence (S)))
3539 then
3540 return True;
3541 end if;
3542 end if;
3544 elsif Nkind (S) = N_Case_Statement then
3545 E := First (Alternatives (S));
3546 while Present (E) loop
3547 if Has_Excluded_Statement (Subp, Statements (E)) then
3548 return True;
3549 end if;
3551 Next (E);
3552 end loop;
3554 elsif Nkind (S) = N_If_Statement then
3555 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
3556 return True;
3557 end if;
3559 if Present (Elsif_Parts (S)) then
3560 E := First (Elsif_Parts (S));
3561 while Present (E) loop
3562 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
3563 return True;
3564 end if;
3566 Next (E);
3567 end loop;
3568 end if;
3570 if Present (Else_Statements (S))
3571 and then Has_Excluded_Statement (Subp, Else_Statements (S))
3572 then
3573 return True;
3574 end if;
3576 elsif Nkind (S) = N_Loop_Statement
3577 and then Has_Excluded_Statement (Subp, Statements (S))
3578 then
3579 return True;
3581 elsif Nkind (S) = N_Extended_Return_Statement then
3582 if Present (Handled_Statement_Sequence (S))
3583 and then
3584 Has_Excluded_Statement
3585 (Subp, Statements (Handled_Statement_Sequence (S)))
3586 then
3587 return True;
3589 elsif not Back_End_Inlining
3590 and then Present (Handled_Statement_Sequence (S))
3591 and then
3592 Present (Exception_Handlers
3593 (Handled_Statement_Sequence (S)))
3594 then
3595 Cannot_Inline
3596 ("cannot inline& (exception handler)?",
3597 First (Exception_Handlers (Handled_Statement_Sequence (S))),
3598 Subp);
3599 return True;
3600 end if;
3601 end if;
3603 Next (S);
3604 end loop;
3606 return False;
3607 end Has_Excluded_Statement;
3609 --------------------------
3610 -- Has_Initialized_Type --
3611 --------------------------
3613 function Has_Initialized_Type (E : Entity_Id) return Boolean is
3614 E_Body : constant Node_Id := Get_Subprogram_Body (E);
3615 Decl : Node_Id;
3617 begin
3618 if No (E_Body) then -- imported subprogram
3619 return False;
3621 else
3622 Decl := First (Declarations (E_Body));
3623 while Present (Decl) loop
3624 if Nkind (Decl) = N_Full_Type_Declaration
3625 and then Present (Init_Proc (Defining_Identifier (Decl)))
3626 then
3627 return True;
3628 end if;
3630 Next (Decl);
3631 end loop;
3632 end if;
3634 return False;
3635 end Has_Initialized_Type;
3637 -----------------------
3638 -- Has_Single_Return --
3639 -----------------------
3641 function Has_Single_Return (N : Node_Id) return Boolean is
3642 Return_Statement : Node_Id := Empty;
3644 function Check_Return (N : Node_Id) return Traverse_Result;
3646 ------------------
3647 -- Check_Return --
3648 ------------------
3650 function Check_Return (N : Node_Id) return Traverse_Result is
3651 begin
3652 if Nkind (N) = N_Simple_Return_Statement then
3653 if Present (Expression (N))
3654 and then Is_Entity_Name (Expression (N))
3655 then
3656 if No (Return_Statement) then
3657 Return_Statement := N;
3658 return OK;
3660 elsif Chars (Expression (N)) =
3661 Chars (Expression (Return_Statement))
3662 then
3663 return OK;
3665 else
3666 return Abandon;
3667 end if;
3669 -- A return statement within an extended return is a noop
3670 -- after inlining.
3672 elsif No (Expression (N))
3673 and then
3674 Nkind (Parent (Parent (N))) = N_Extended_Return_Statement
3675 then
3676 return OK;
3678 else
3679 -- Expression has wrong form
3681 return Abandon;
3682 end if;
3684 -- We can only inline a build-in-place function if it has a single
3685 -- extended return.
3687 elsif Nkind (N) = N_Extended_Return_Statement then
3688 if No (Return_Statement) then
3689 Return_Statement := N;
3690 return OK;
3692 else
3693 return Abandon;
3694 end if;
3696 else
3697 return OK;
3698 end if;
3699 end Check_Return;
3701 function Check_All_Returns is new Traverse_Func (Check_Return);
3703 -- Start of processing for Has_Single_Return
3705 begin
3706 if Check_All_Returns (N) /= OK then
3707 return False;
3709 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3710 return True;
3712 else
3713 return Present (Declarations (N))
3714 and then Present (First (Declarations (N)))
3715 and then Chars (Expression (Return_Statement)) =
3716 Chars (Defining_Identifier (First (Declarations (N))));
3717 end if;
3718 end Has_Single_Return;
3720 -----------------------------
3721 -- In_Main_Unit_Or_Subunit --
3722 -----------------------------
3724 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
3725 Comp : Node_Id := Cunit (Get_Code_Unit (E));
3727 begin
3728 -- Check whether the subprogram or package to inline is within the main
3729 -- unit or its spec or within a subunit. In either case there are no
3730 -- additional bodies to process. If the subprogram appears in a parent
3731 -- of the current unit, the check on whether inlining is possible is
3732 -- done in Analyze_Inlined_Bodies.
3734 while Nkind (Unit (Comp)) = N_Subunit loop
3735 Comp := Library_Unit (Comp);
3736 end loop;
3738 return Comp = Cunit (Main_Unit)
3739 or else Comp = Library_Unit (Cunit (Main_Unit));
3740 end In_Main_Unit_Or_Subunit;
3742 ----------------
3743 -- Initialize --
3744 ----------------
3746 procedure Initialize is
3747 begin
3748 Pending_Descriptor.Init;
3749 Pending_Instantiations.Init;
3750 Inlined_Bodies.Init;
3751 Successors.Init;
3752 Inlined.Init;
3754 for J in Hash_Headers'Range loop
3755 Hash_Headers (J) := No_Subp;
3756 end loop;
3758 Inlined_Calls := No_Elist;
3759 Backend_Calls := No_Elist;
3760 Backend_Inlined_Subps := No_Elist;
3761 Backend_Not_Inlined_Subps := No_Elist;
3762 end Initialize;
3764 ------------------------
3765 -- Instantiate_Bodies --
3766 ------------------------
3768 -- Generic bodies contain all the non-local references, so an
3769 -- instantiation does not need any more context than Standard
3770 -- itself, even if the instantiation appears in an inner scope.
3771 -- Generic associations have verified that the contract model is
3772 -- satisfied, so that any error that may occur in the analysis of
3773 -- the body is an internal error.
3775 procedure Instantiate_Bodies is
3776 J : Int;
3777 Info : Pending_Body_Info;
3779 begin
3780 if Serious_Errors_Detected = 0 then
3781 Expander_Active := (Operating_Mode = Opt.Generate_Code);
3782 Push_Scope (Standard_Standard);
3783 To_Clean := New_Elmt_List;
3785 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3786 Start_Generic;
3787 end if;
3789 -- A body instantiation may generate additional instantiations, so
3790 -- the following loop must scan to the end of a possibly expanding
3791 -- set (that's why we can't simply use a FOR loop here).
3793 J := 0;
3794 while J <= Pending_Instantiations.Last
3795 and then Serious_Errors_Detected = 0
3796 loop
3797 Info := Pending_Instantiations.Table (J);
3799 -- If the instantiation node is absent, it has been removed
3800 -- as part of unreachable code.
3802 if No (Info.Inst_Node) then
3803 null;
3805 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
3806 Instantiate_Package_Body (Info);
3807 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
3809 else
3810 Instantiate_Subprogram_Body (Info);
3811 end if;
3813 J := J + 1;
3814 end loop;
3816 -- Reset the table of instantiations. Additional instantiations
3817 -- may be added through inlining, when additional bodies are
3818 -- analyzed.
3820 Pending_Instantiations.Init;
3822 -- We can now complete the cleanup actions of scopes that contain
3823 -- pending instantiations (skipped for generic units, since we
3824 -- never need any cleanups in generic units).
3825 -- pending instantiations.
3827 if Expander_Active
3828 and then not Is_Generic_Unit (Main_Unit_Entity)
3829 then
3830 Cleanup_Scopes;
3831 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3832 End_Generic;
3833 end if;
3835 Pop_Scope;
3836 end if;
3837 end Instantiate_Bodies;
3839 ---------------
3840 -- Is_Nested --
3841 ---------------
3843 function Is_Nested (E : Entity_Id) return Boolean is
3844 Scop : Entity_Id;
3846 begin
3847 Scop := Scope (E);
3848 while Scop /= Standard_Standard loop
3849 if Ekind (Scop) in Subprogram_Kind then
3850 return True;
3852 elsif Ekind (Scop) = E_Task_Type
3853 or else Ekind (Scop) = E_Entry
3854 or else Ekind (Scop) = E_Entry_Family
3855 then
3856 return True;
3857 end if;
3859 Scop := Scope (Scop);
3860 end loop;
3862 return False;
3863 end Is_Nested;
3865 ------------------------
3866 -- List_Inlining_Info --
3867 ------------------------
3869 procedure List_Inlining_Info is
3870 Elmt : Elmt_Id;
3871 Nod : Node_Id;
3872 Count : Nat;
3874 begin
3875 if not Debug_Flag_Dot_J then
3876 return;
3877 end if;
3879 -- Generate listing of calls inlined by the frontend
3881 if Present (Inlined_Calls) then
3882 Count := 0;
3883 Elmt := First_Elmt (Inlined_Calls);
3884 while Present (Elmt) loop
3885 Nod := Node (Elmt);
3887 if In_Extended_Main_Code_Unit (Nod) then
3888 Count := Count + 1;
3890 if Count = 1 then
3891 Write_Str ("Listing of frontend inlined calls");
3892 Write_Eol;
3893 end if;
3895 Write_Str (" ");
3896 Write_Int (Count);
3897 Write_Str (":");
3898 Write_Location (Sloc (Nod));
3899 Write_Str (":");
3900 Output.Write_Eol;
3901 end if;
3903 Next_Elmt (Elmt);
3904 end loop;
3905 end if;
3907 -- Generate listing of calls passed to the backend
3909 if Present (Backend_Calls) then
3910 Count := 0;
3912 Elmt := First_Elmt (Backend_Calls);
3913 while Present (Elmt) loop
3914 Nod := Node (Elmt);
3916 if In_Extended_Main_Code_Unit (Nod) then
3917 Count := Count + 1;
3919 if Count = 1 then
3920 Write_Str ("Listing of inlined calls passed to the backend");
3921 Write_Eol;
3922 end if;
3924 Write_Str (" ");
3925 Write_Int (Count);
3926 Write_Str (":");
3927 Write_Location (Sloc (Nod));
3928 Output.Write_Eol;
3929 end if;
3931 Next_Elmt (Elmt);
3932 end loop;
3933 end if;
3935 -- Generate listing of subprograms passed to the backend
3937 if Present (Backend_Inlined_Subps)
3938 and then Back_End_Inlining
3939 then
3940 Count := 0;
3942 Elmt := First_Elmt (Backend_Inlined_Subps);
3943 while Present (Elmt) loop
3944 Nod := Node (Elmt);
3946 Count := Count + 1;
3948 if Count = 1 then
3949 Write_Str
3950 ("Listing of inlined subprograms passed to the backend");
3951 Write_Eol;
3952 end if;
3954 Write_Str (" ");
3955 Write_Int (Count);
3956 Write_Str (":");
3957 Write_Name (Chars (Nod));
3958 Write_Str (" (");
3959 Write_Location (Sloc (Nod));
3960 Write_Str (")");
3961 Output.Write_Eol;
3963 Next_Elmt (Elmt);
3964 end loop;
3965 end if;
3967 -- Generate listing of subprogram that cannot be inlined by the backend
3969 if Present (Backend_Not_Inlined_Subps)
3970 and then Back_End_Inlining
3971 then
3972 Count := 0;
3974 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
3975 while Present (Elmt) loop
3976 Nod := Node (Elmt);
3978 Count := Count + 1;
3980 if Count = 1 then
3981 Write_Str
3982 ("Listing of subprograms that cannot inline the backend");
3983 Write_Eol;
3984 end if;
3986 Write_Str (" ");
3987 Write_Int (Count);
3988 Write_Str (":");
3989 Write_Name (Chars (Nod));
3990 Write_Str (" (");
3991 Write_Location (Sloc (Nod));
3992 Write_Str (")");
3993 Output.Write_Eol;
3995 Next_Elmt (Elmt);
3996 end loop;
3997 end if;
3998 end List_Inlining_Info;
4000 ----------
4001 -- Lock --
4002 ----------
4004 procedure Lock is
4005 begin
4006 Pending_Instantiations.Locked := True;
4007 Inlined_Bodies.Locked := True;
4008 Successors.Locked := True;
4009 Inlined.Locked := True;
4010 Pending_Instantiations.Release;
4011 Inlined_Bodies.Release;
4012 Successors.Release;
4013 Inlined.Release;
4014 end Lock;
4016 ---------------------------
4017 -- Register_Backend_Call --
4018 ---------------------------
4020 procedure Register_Backend_Call (N : Node_Id) is
4021 begin
4022 Append_New_Elmt (N, To => Backend_Calls);
4023 end Register_Backend_Call;
4025 --------------------------
4026 -- Remove_Dead_Instance --
4027 --------------------------
4029 procedure Remove_Dead_Instance (N : Node_Id) is
4030 J : Int;
4032 begin
4033 J := 0;
4034 while J <= Pending_Instantiations.Last loop
4035 if Pending_Instantiations.Table (J).Inst_Node = N then
4036 Pending_Instantiations.Table (J).Inst_Node := Empty;
4037 return;
4038 end if;
4040 J := J + 1;
4041 end loop;
4042 end Remove_Dead_Instance;
4044 --------------------
4045 -- Remove_Pragmas --
4046 --------------------
4048 procedure Remove_Pragmas (Bod : Node_Id) is
4049 Decl : Node_Id;
4050 Nxt : Node_Id;
4052 begin
4053 Decl := First (Declarations (Bod));
4054 while Present (Decl) loop
4055 Nxt := Next (Decl);
4057 if Nkind (Decl) = N_Pragma
4058 and then Nam_In (Pragma_Name (Decl), Name_Contract_Cases,
4059 Name_Precondition,
4060 Name_Postcondition,
4061 Name_Unreferenced,
4062 Name_Unmodified)
4063 then
4064 Remove (Decl);
4065 end if;
4067 Decl := Nxt;
4068 end loop;
4069 end Remove_Pragmas;
4071 end Inline;