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