libgo: correct golang_org Makefile variables not used on all systems
[official-gcc.git] / gcc / ada / inline.adb
blob9fb47ef13cdfe90c934c23d30bdd29541b3f8c88
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 Last_Statement : Node_Id := Empty;
963 function Check_Return (N : Node_Id) return Traverse_Result;
964 -- Returns OK on node N if this is not a return statement different
965 -- from the last statement in the subprogram.
967 ------------------
968 -- Check_Return --
969 ------------------
971 function Check_Return (N : Node_Id) return Traverse_Result is
972 begin
973 if Nkind_In (N, N_Simple_Return_Statement,
974 N_Extended_Return_Statement)
975 then
976 if N = Last_Statement then
977 return OK;
978 else
979 return Abandon;
980 end if;
982 else
983 return OK;
984 end if;
985 end Check_Return;
987 function Check_All_Returns is new Traverse_Func (Check_Return);
989 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
991 begin
992 -- Retrieve the last statement
994 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
996 -- Check that the last statement is the only possible return
997 -- statement in the subprogram.
999 return Check_All_Returns (N) = OK;
1000 end Has_Single_Return_In_GNATprove_Mode;
1002 --------------------------
1003 -- Uses_Secondary_Stack --
1004 --------------------------
1006 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1007 function Check_Call (N : Node_Id) return Traverse_Result;
1008 -- Look for function calls that return an unconstrained type
1010 ----------------
1011 -- Check_Call --
1012 ----------------
1014 function Check_Call (N : Node_Id) return Traverse_Result is
1015 begin
1016 if Nkind (N) = N_Function_Call
1017 and then Is_Entity_Name (Name (N))
1018 and then Is_Composite_Type (Etype (Entity (Name (N))))
1019 and then not Is_Constrained (Etype (Entity (Name (N))))
1020 then
1021 Cannot_Inline
1022 ("cannot inline & (call returns unconstrained type)?",
1023 N, Spec_Id);
1024 return Abandon;
1025 else
1026 return OK;
1027 end if;
1028 end Check_Call;
1030 function Check_Calls is new Traverse_Func (Check_Call);
1032 begin
1033 return Check_Calls (Bod) = Abandon;
1034 end Uses_Secondary_Stack;
1036 -- Start of processing for Build_Body_To_Inline
1038 begin
1039 -- Return immediately if done already
1041 if Nkind (Decl) = N_Subprogram_Declaration
1042 and then Present (Body_To_Inline (Decl))
1043 then
1044 return;
1046 -- Subprograms that have return statements in the middle of the body are
1047 -- inlined with gotos. GNATprove does not currently support gotos, so
1048 -- we prevent such inlining.
1050 elsif GNATprove_Mode
1051 and then not Has_Single_Return_In_GNATprove_Mode
1052 then
1053 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1054 return;
1056 -- Functions that return unconstrained composite types require
1057 -- secondary stack handling, and cannot currently be inlined, unless
1058 -- all return statements return a local variable that is the first
1059 -- local declaration in the body.
1061 elsif Ekind (Spec_Id) = E_Function
1062 and then not Is_Scalar_Type (Etype (Spec_Id))
1063 and then not Is_Access_Type (Etype (Spec_Id))
1064 and then not Is_Constrained (Etype (Spec_Id))
1065 then
1066 if not Has_Single_Return (N) then
1067 Cannot_Inline
1068 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1069 return;
1070 end if;
1072 -- Ditto for functions that return controlled types, where controlled
1073 -- actions interfere in complex ways with inlining.
1075 elsif Ekind (Spec_Id) = E_Function
1076 and then Needs_Finalization (Etype (Spec_Id))
1077 then
1078 Cannot_Inline
1079 ("cannot inline & (controlled return type)?", N, Spec_Id);
1080 return;
1081 end if;
1083 if Present (Declarations (N))
1084 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
1085 then
1086 return;
1087 end if;
1089 if Present (Handled_Statement_Sequence (N)) then
1090 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1091 Cannot_Inline
1092 ("cannot inline& (exception handler)?",
1093 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1094 Spec_Id);
1095 return;
1097 elsif Has_Excluded_Statement
1098 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1099 then
1100 return;
1101 end if;
1102 end if;
1104 -- We do not inline a subprogram that is too large, unless it is marked
1105 -- Inline_Always or we are in GNATprove mode. This pragma does not
1106 -- suppress the other checks on inlining (forbidden declarations,
1107 -- handlers, etc).
1109 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1110 and then List_Length
1111 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1112 then
1113 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1114 return;
1115 end if;
1117 if Has_Pending_Instantiation then
1118 Cannot_Inline
1119 ("cannot inline& (forward instance within enclosing body)?",
1120 N, Spec_Id);
1121 return;
1122 end if;
1124 -- Within an instance, the body to inline must be treated as a nested
1125 -- generic, so that the proper global references are preserved.
1127 -- Note that we do not do this at the library level, because it is not
1128 -- needed, and furthermore this causes trouble if front end inlining
1129 -- is activated (-gnatN).
1131 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1132 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1133 Original_Body := Copy_Generic_Node (N, Empty, True);
1134 else
1135 Original_Body := Copy_Separate_Tree (N);
1136 end if;
1138 -- We need to capture references to the formals in order to substitute
1139 -- the actuals at the point of inlining, i.e. instantiation. To treat
1140 -- the formals as globals to the body to inline, we nest it within a
1141 -- dummy parameterless subprogram, declared within the real one. To
1142 -- avoid generating an internal name (which is never public, and which
1143 -- affects serial numbers of other generated names), we use an internal
1144 -- symbol that cannot conflict with user declarations.
1146 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1147 Set_Defining_Unit_Name
1148 (Specification (Original_Body),
1149 Make_Defining_Identifier (Sloc (N), Name_uParent));
1150 Set_Corresponding_Spec (Original_Body, Empty);
1152 -- Remove all aspects/pragmas that have no meaning in an inlined body
1154 Remove_Aspects_And_Pragmas (Original_Body);
1156 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1158 -- Set return type of function, which is also global and does not need
1159 -- to be resolved.
1161 if Ekind (Spec_Id) = E_Function then
1162 Set_Result_Definition
1163 (Specification (Body_To_Analyze),
1164 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1165 end if;
1167 if No (Declarations (N)) then
1168 Set_Declarations (N, New_List (Body_To_Analyze));
1169 else
1170 Append (Body_To_Analyze, Declarations (N));
1171 end if;
1173 -- The body to inline is pre-analyzed. In GNATprove mode we must disable
1174 -- full analysis as well so that light expansion does not take place
1175 -- either, and name resolution is unaffected.
1177 Expander_Mode_Save_And_Set (False);
1178 Full_Analysis := False;
1180 Analyze (Body_To_Analyze);
1181 Push_Scope (Defining_Entity (Body_To_Analyze));
1182 Save_Global_References (Original_Body);
1183 End_Scope;
1184 Remove (Body_To_Analyze);
1186 Expander_Mode_Restore;
1187 Full_Analysis := Analysis_Status;
1189 -- Restore environment if previously saved
1191 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1192 Restore_Env;
1193 end if;
1195 -- If secondary stack is used, there is no point in inlining. We have
1196 -- already issued the warning in this case, so nothing to do.
1198 if Uses_Secondary_Stack (Body_To_Analyze) then
1199 return;
1200 end if;
1202 Set_Body_To_Inline (Decl, Original_Body);
1203 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1204 Set_Is_Inlined (Spec_Id);
1205 end Build_Body_To_Inline;
1207 -------------------------------------------
1208 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1209 -------------------------------------------
1211 function Call_Can_Be_Inlined_In_GNATprove_Mode
1212 (N : Node_Id;
1213 Subp : Entity_Id) return Boolean
1215 F : Entity_Id;
1216 A : Node_Id;
1218 begin
1219 F := First_Formal (Subp);
1220 A := First_Actual (N);
1221 while Present (F) loop
1222 if Ekind (F) /= E_Out_Parameter
1223 and then not Same_Type (Etype (F), Etype (A))
1224 and then
1225 (Is_By_Reference_Type (Etype (A))
1226 or else Is_Limited_Type (Etype (A)))
1227 then
1228 return False;
1229 end if;
1231 Next_Formal (F);
1232 Next_Actual (A);
1233 end loop;
1235 return True;
1236 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1238 --------------------------------------
1239 -- Can_Be_Inlined_In_GNATprove_Mode --
1240 --------------------------------------
1242 function Can_Be_Inlined_In_GNATprove_Mode
1243 (Spec_Id : Entity_Id;
1244 Body_Id : Entity_Id) return Boolean
1246 function Has_Formal_With_Discriminant_Dependent_Fields
1247 (Id : Entity_Id) return Boolean;
1248 -- Returns true if the subprogram has at least one formal parameter of
1249 -- an unconstrained record type with per-object constraints on component
1250 -- types.
1252 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1253 -- Returns True if subprogram Id has any contract (Pre, Post, Global,
1254 -- Depends, etc.)
1256 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1257 -- Returns True if subprogram Id defines a compilation unit
1258 -- Shouldn't this be in Sem_Aux???
1260 function In_Package_Visible_Spec (Id : Node_Id) return Boolean;
1261 -- Returns True if subprogram Id is defined in the visible part of a
1262 -- package specification.
1264 ---------------------------------------------------
1265 -- Has_Formal_With_Discriminant_Dependent_Fields --
1266 ---------------------------------------------------
1268 function Has_Formal_With_Discriminant_Dependent_Fields
1269 (Id : Entity_Id) return Boolean is
1271 function Has_Discriminant_Dependent_Component
1272 (Typ : Entity_Id) return Boolean;
1273 -- Determine whether unconstrained record type Typ has at least
1274 -- one component that depends on a discriminant.
1276 ------------------------------------------
1277 -- Has_Discriminant_Dependent_Component --
1278 ------------------------------------------
1280 function Has_Discriminant_Dependent_Component
1281 (Typ : Entity_Id) return Boolean
1283 Comp : Entity_Id;
1285 begin
1286 -- Inspect all components of the record type looking for one
1287 -- that depends on a discriminant.
1289 Comp := First_Component (Typ);
1290 while Present (Comp) loop
1291 if Has_Discriminant_Dependent_Constraint (Comp) then
1292 return True;
1293 end if;
1295 Next_Component (Comp);
1296 end loop;
1298 return False;
1299 end Has_Discriminant_Dependent_Component;
1301 -- Local variables
1303 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1304 Formal : Entity_Id;
1305 Formal_Typ : Entity_Id;
1307 -- Start of processing for
1308 -- Has_Formal_With_Discriminant_Dependent_Fields
1310 begin
1311 -- Inspect all parameters of the subprogram looking for a formal
1312 -- of an unconstrained record type with at least one discriminant
1313 -- dependent component.
1315 Formal := First_Formal (Subp_Id);
1316 while Present (Formal) loop
1317 Formal_Typ := Etype (Formal);
1319 if Is_Record_Type (Formal_Typ)
1320 and then not Is_Constrained (Formal_Typ)
1321 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1322 then
1323 return True;
1324 end if;
1326 Next_Formal (Formal);
1327 end loop;
1329 return False;
1330 end Has_Formal_With_Discriminant_Dependent_Fields;
1332 -----------------------
1333 -- Has_Some_Contract --
1334 -----------------------
1336 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1337 Items : Node_Id;
1339 begin
1340 -- A call to an expression function may precede the actual body which
1341 -- is inserted at the end of the enclosing declarations. Ensure that
1342 -- the related entity is decorated before inspecting the contract.
1344 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1345 Items := Contract (Id);
1347 return Present (Items)
1348 and then (Present (Pre_Post_Conditions (Items)) or else
1349 Present (Contract_Test_Cases (Items)) or else
1350 Present (Classifications (Items)));
1351 end if;
1353 return False;
1354 end Has_Some_Contract;
1356 -----------------------------
1357 -- In_Package_Visible_Spec --
1358 -----------------------------
1360 function In_Package_Visible_Spec (Id : Node_Id) return Boolean is
1361 Decl : Node_Id := Parent (Parent (Id));
1362 P : Node_Id;
1364 begin
1365 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1366 Decl := Parent (Decl);
1367 end if;
1369 P := Parent (Decl);
1371 return Nkind (P) = N_Package_Specification
1372 and then List_Containing (Decl) = Visible_Declarations (P);
1373 end In_Package_Visible_Spec;
1375 ------------------------
1376 -- Is_Unit_Subprogram --
1377 ------------------------
1379 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1380 Decl : Node_Id := Parent (Parent (Id));
1381 begin
1382 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1383 Decl := Parent (Decl);
1384 end if;
1386 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1387 end Is_Unit_Subprogram;
1389 -- Local declarations
1391 Id : Entity_Id;
1392 -- Procedure or function entity for the subprogram
1394 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1396 begin
1397 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1399 if Present (Spec_Id) then
1400 Id := Spec_Id;
1401 else
1402 Id := Body_Id;
1403 end if;
1405 -- Only local subprograms without contracts are inlined in GNATprove
1406 -- mode, as these are the subprograms which a user is not interested in
1407 -- analyzing in isolation, but rather in the context of their call. This
1408 -- is a convenient convention, that could be changed for an explicit
1409 -- pragma/aspect one day.
1411 -- In a number of special cases, inlining is not desirable or not
1412 -- possible, see below.
1414 -- Do not inline unit-level subprograms
1416 if Is_Unit_Subprogram (Id) then
1417 return False;
1419 -- Do not inline subprograms declared in the visible part of a package
1421 elsif In_Package_Visible_Spec (Id) then
1422 return False;
1424 -- Do not inline subprograms marked No_Return, possibly used for
1425 -- signaling errors, which GNATprove handles specially.
1427 elsif No_Return (Id) then
1428 return False;
1430 -- Do not inline subprograms that have a contract on the spec or the
1431 -- body. Use the contract(s) instead in GNATprove.
1433 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1434 or else
1435 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1436 then
1437 return False;
1439 -- Do not inline expression functions, which are directly inlined at the
1440 -- prover level.
1442 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1443 or else
1444 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1445 then
1446 return False;
1448 -- Do not inline generic subprogram instances. The visibility rules of
1449 -- generic instances plays badly with inlining.
1451 elsif Is_Generic_Instance (Spec_Id) then
1452 return False;
1454 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1455 -- the subprogram body, a similar check is performed after the body
1456 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1458 elsif Present (Spec_Id)
1459 and then
1460 (No (SPARK_Pragma (Spec_Id))
1461 or else
1462 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1463 then
1464 return False;
1466 -- Subprograms in generic instances are currently not inlined, to avoid
1467 -- problems with inlining of standard library subprograms.
1469 elsif Instantiation_Location (Sloc (Id)) /= No_Location then
1470 return False;
1472 -- Do not inline predicate functions (treated specially by GNATprove)
1474 elsif Is_Predicate_Function (Id) then
1475 return False;
1477 -- Do not inline subprograms with a parameter of an unconstrained
1478 -- record type if it has discrimiant dependent fields. Indeed, with
1479 -- such parameters, the frontend cannot always ensure type compliance
1480 -- in record component accesses (in particular with records containing
1481 -- packed arrays).
1483 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1484 return False;
1486 -- Otherwise, this is a subprogram declared inside the private part of a
1487 -- package, or inside a package body, or locally in a subprogram, and it
1488 -- does not have any contract. Inline it.
1490 else
1491 return True;
1492 end if;
1493 end Can_Be_Inlined_In_GNATprove_Mode;
1495 -------------------
1496 -- Cannot_Inline --
1497 -------------------
1499 procedure Cannot_Inline
1500 (Msg : String;
1501 N : Node_Id;
1502 Subp : Entity_Id;
1503 Is_Serious : Boolean := False)
1505 begin
1506 -- In GNATprove mode, inlining is the technical means by which the
1507 -- higher-level goal of contextual analysis is reached, so issue
1508 -- messages about failure to apply contextual analysis to a
1509 -- subprogram, rather than failure to inline it.
1511 if GNATprove_Mode
1512 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1513 then
1514 declare
1515 Len1 : constant Positive :=
1516 String (String'("cannot inline"))'Length;
1517 Len2 : constant Positive :=
1518 String (String'("info: no contextual analysis of"))'Length;
1520 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1522 begin
1523 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1524 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1525 Msg (Msg'First + Len1 .. Msg'Last);
1526 Cannot_Inline (New_Msg, N, Subp, Is_Serious);
1527 return;
1528 end;
1529 end if;
1531 pragma Assert (Msg (Msg'Last) = '?');
1533 -- Legacy front end inlining model
1535 if not Back_End_Inlining then
1537 -- Do not emit warning if this is a predefined unit which is not
1538 -- the main unit. With validity checks enabled, some predefined
1539 -- subprograms may contain nested subprograms and become ineligible
1540 -- for inlining.
1542 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1543 and then not In_Extended_Main_Source_Unit (Subp)
1544 then
1545 null;
1547 -- In GNATprove mode, issue a warning, and indicate that the
1548 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1549 -- to False.
1551 elsif GNATprove_Mode then
1552 Set_Is_Inlined_Always (Subp, False);
1553 Error_Msg_NE (Msg & "p?", N, Subp);
1555 elsif Has_Pragma_Inline_Always (Subp) then
1557 -- Remove last character (question mark) to make this into an
1558 -- error, because the Inline_Always pragma cannot be obeyed.
1560 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1562 elsif Ineffective_Inline_Warnings then
1563 Error_Msg_NE (Msg & "p?", N, Subp);
1564 end if;
1566 -- New semantics relying on back end inlining
1568 elsif Is_Serious then
1570 -- Remove last character (question mark) to make this into an error.
1572 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1574 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1575 -- is not always inlined by setting flag Is_Inlined_Always to False.
1577 elsif GNATprove_Mode then
1578 Set_Is_Inlined_Always (Subp, False);
1579 Error_Msg_NE (Msg & "p?", N, Subp);
1581 else
1583 -- Do not emit warning if this is a predefined unit which is not
1584 -- the main unit. This behavior is currently provided for backward
1585 -- compatibility but it will be removed when we enforce the
1586 -- strictness of the new rules.
1588 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1589 and then not In_Extended_Main_Source_Unit (Subp)
1590 then
1591 null;
1593 elsif Has_Pragma_Inline_Always (Subp) then
1595 -- Emit a warning if this is a call to a runtime subprogram
1596 -- which is located inside a generic. Previously this call
1597 -- was silently skipped.
1599 if Is_Generic_Instance (Subp) then
1600 declare
1601 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
1602 begin
1603 if Is_Predefined_File_Name
1604 (Unit_File_Name (Get_Source_Unit (Gen_P)))
1605 then
1606 Set_Is_Inlined (Subp, False);
1607 Error_Msg_NE (Msg & "p?", N, Subp);
1608 return;
1609 end if;
1610 end;
1611 end if;
1613 -- Remove last character (question mark) to make this into an
1614 -- error, because the Inline_Always pragma cannot be obeyed.
1616 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1618 else
1619 Set_Is_Inlined (Subp, False);
1621 if Ineffective_Inline_Warnings then
1622 Error_Msg_NE (Msg & "p?", N, Subp);
1623 end if;
1624 end if;
1625 end if;
1626 end Cannot_Inline;
1628 --------------------------------------------
1629 -- Check_And_Split_Unconstrained_Function --
1630 --------------------------------------------
1632 procedure Check_And_Split_Unconstrained_Function
1633 (N : Node_Id;
1634 Spec_Id : Entity_Id;
1635 Body_Id : Entity_Id)
1637 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
1638 -- Use generic machinery to build an unexpanded body for the subprogram.
1639 -- This body is subsequently used for inline expansions at call sites.
1641 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
1642 -- Return true if we generate code for the function body N, the function
1643 -- body N has no local declarations and its unique statement is a single
1644 -- extended return statement with a handled statements sequence.
1646 procedure Generate_Subprogram_Body
1647 (N : Node_Id;
1648 Body_To_Inline : out Node_Id);
1649 -- Generate a parameterless duplicate of subprogram body N. Occurrences
1650 -- of pragmas referencing the formals are removed since they have no
1651 -- meaning when the body is inlined and the formals are rewritten (the
1652 -- analysis of the non-inlined body will handle these pragmas properly).
1653 -- A new internal name is associated with Body_To_Inline.
1655 procedure Split_Unconstrained_Function
1656 (N : Node_Id;
1657 Spec_Id : Entity_Id);
1658 -- N is an inlined function body that returns an unconstrained type and
1659 -- has a single extended return statement. Split N in two subprograms:
1660 -- a procedure P' and a function F'. The formals of P' duplicate the
1661 -- formals of N plus an extra formal which is used return a value;
1662 -- its body is composed by the declarations and list of statements
1663 -- of the extended return statement of N.
1665 --------------------------
1666 -- Build_Body_To_Inline --
1667 --------------------------
1669 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1670 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1671 Original_Body : Node_Id;
1672 Body_To_Analyze : Node_Id;
1674 begin
1675 pragma Assert (Current_Scope = Spec_Id);
1677 -- Within an instance, the body to inline must be treated as a nested
1678 -- generic, so that the proper global references are preserved. We
1679 -- do not do this at the library level, because it is not needed, and
1680 -- furthermore this causes trouble if front end inlining is activated
1681 -- (-gnatN).
1683 if In_Instance
1684 and then Scope (Current_Scope) /= Standard_Standard
1685 then
1686 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1687 end if;
1689 -- We need to capture references to the formals in order
1690 -- to substitute the actuals at the point of inlining, i.e.
1691 -- instantiation. To treat the formals as globals to the body to
1692 -- inline, we nest it within a dummy parameterless subprogram,
1693 -- declared within the real one.
1695 Generate_Subprogram_Body (N, Original_Body);
1696 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1698 -- Set return type of function, which is also global and does not
1699 -- need to be resolved.
1701 if Ekind (Spec_Id) = E_Function then
1702 Set_Result_Definition (Specification (Body_To_Analyze),
1703 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1704 end if;
1706 if No (Declarations (N)) then
1707 Set_Declarations (N, New_List (Body_To_Analyze));
1708 else
1709 Append_To (Declarations (N), Body_To_Analyze);
1710 end if;
1712 Preanalyze (Body_To_Analyze);
1714 Push_Scope (Defining_Entity (Body_To_Analyze));
1715 Save_Global_References (Original_Body);
1716 End_Scope;
1717 Remove (Body_To_Analyze);
1719 -- Restore environment if previously saved
1721 if In_Instance
1722 and then Scope (Current_Scope) /= Standard_Standard
1723 then
1724 Restore_Env;
1725 end if;
1727 pragma Assert (No (Body_To_Inline (Decl)));
1728 Set_Body_To_Inline (Decl, Original_Body);
1729 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1730 end Build_Body_To_Inline;
1732 --------------------------------------
1733 -- Can_Split_Unconstrained_Function --
1734 --------------------------------------
1736 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
1738 Ret_Node : constant Node_Id :=
1739 First (Statements (Handled_Statement_Sequence (N)));
1740 D : Node_Id;
1742 begin
1743 -- No user defined declarations allowed in the function except inside
1744 -- the unique return statement; implicit labels are the only allowed
1745 -- declarations.
1747 if not Is_Empty_List (Declarations (N)) then
1748 D := First (Declarations (N));
1749 while Present (D) loop
1750 if Nkind (D) /= N_Implicit_Label_Declaration then
1751 return False;
1752 end if;
1754 Next (D);
1755 end loop;
1756 end if;
1758 -- We only split the inlined function when we are generating the code
1759 -- of its body; otherwise we leave duplicated split subprograms in
1760 -- the tree which (if referenced) generate wrong references at link
1761 -- time.
1763 return In_Extended_Main_Code_Unit (N)
1764 and then Present (Ret_Node)
1765 and then Nkind (Ret_Node) = N_Extended_Return_Statement
1766 and then No (Next (Ret_Node))
1767 and then Present (Handled_Statement_Sequence (Ret_Node));
1768 end Can_Split_Unconstrained_Function;
1770 -----------------------------
1771 -- Generate_Body_To_Inline --
1772 -----------------------------
1774 procedure Generate_Subprogram_Body
1775 (N : Node_Id;
1776 Body_To_Inline : out Node_Id)
1778 begin
1779 -- Within an instance, the body to inline must be treated as a nested
1780 -- generic, so that the proper global references are preserved.
1782 -- Note that we do not do this at the library level, because it
1783 -- is not needed, and furthermore this causes trouble if front
1784 -- end inlining is activated (-gnatN).
1786 if In_Instance
1787 and then Scope (Current_Scope) /= Standard_Standard
1788 then
1789 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
1790 else
1791 Body_To_Inline := Copy_Separate_Tree (N);
1792 end if;
1794 -- Remove all aspects/pragmas that have no meaning in an inlined body
1796 Remove_Aspects_And_Pragmas (Body_To_Inline);
1798 -- We need to capture references to the formals in order
1799 -- to substitute the actuals at the point of inlining, i.e.
1800 -- instantiation. To treat the formals as globals to the body to
1801 -- inline, we nest it within a dummy parameterless subprogram,
1802 -- declared within the real one.
1804 Set_Parameter_Specifications
1805 (Specification (Body_To_Inline), No_List);
1807 -- A new internal name is associated with Body_To_Inline to avoid
1808 -- conflicts when the non-inlined body N is analyzed.
1810 Set_Defining_Unit_Name (Specification (Body_To_Inline),
1811 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
1812 Set_Corresponding_Spec (Body_To_Inline, Empty);
1813 end Generate_Subprogram_Body;
1815 ----------------------------------
1816 -- Split_Unconstrained_Function --
1817 ----------------------------------
1819 procedure Split_Unconstrained_Function
1820 (N : Node_Id;
1821 Spec_Id : Entity_Id)
1823 Loc : constant Source_Ptr := Sloc (N);
1824 Ret_Node : constant Node_Id :=
1825 First (Statements (Handled_Statement_Sequence (N)));
1826 Ret_Obj : constant Node_Id :=
1827 First (Return_Object_Declarations (Ret_Node));
1829 procedure Build_Procedure
1830 (Proc_Id : out Entity_Id;
1831 Decl_List : out List_Id);
1832 -- Build a procedure containing the statements found in the extended
1833 -- return statement of the unconstrained function body N.
1835 ---------------------
1836 -- Build_Procedure --
1837 ---------------------
1839 procedure Build_Procedure
1840 (Proc_Id : out Entity_Id;
1841 Decl_List : out List_Id)
1843 Formal : Entity_Id;
1844 Formal_List : constant List_Id := New_List;
1845 Proc_Spec : Node_Id;
1846 Proc_Body : Node_Id;
1847 Subp_Name : constant Name_Id := New_Internal_Name ('F');
1848 Body_Decl_List : List_Id := No_List;
1849 Param_Type : Node_Id;
1851 begin
1852 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
1853 Param_Type :=
1854 New_Copy (Object_Definition (Ret_Obj));
1855 else
1856 Param_Type :=
1857 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
1858 end if;
1860 Append_To (Formal_List,
1861 Make_Parameter_Specification (Loc,
1862 Defining_Identifier =>
1863 Make_Defining_Identifier (Loc,
1864 Chars => Chars (Defining_Identifier (Ret_Obj))),
1865 In_Present => False,
1866 Out_Present => True,
1867 Null_Exclusion_Present => False,
1868 Parameter_Type => Param_Type));
1870 Formal := First_Formal (Spec_Id);
1872 -- Note that we copy the parameter type rather than creating
1873 -- a reference to it, because it may be a class-wide entity
1874 -- that will not be retrieved by name.
1876 while Present (Formal) loop
1877 Append_To (Formal_List,
1878 Make_Parameter_Specification (Loc,
1879 Defining_Identifier =>
1880 Make_Defining_Identifier (Sloc (Formal),
1881 Chars => Chars (Formal)),
1882 In_Present => In_Present (Parent (Formal)),
1883 Out_Present => Out_Present (Parent (Formal)),
1884 Null_Exclusion_Present =>
1885 Null_Exclusion_Present (Parent (Formal)),
1886 Parameter_Type =>
1887 New_Copy_Tree (Parameter_Type (Parent (Formal))),
1888 Expression =>
1889 Copy_Separate_Tree (Expression (Parent (Formal)))));
1891 Next_Formal (Formal);
1892 end loop;
1894 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
1896 Proc_Spec :=
1897 Make_Procedure_Specification (Loc,
1898 Defining_Unit_Name => Proc_Id,
1899 Parameter_Specifications => Formal_List);
1901 Decl_List := New_List;
1903 Append_To (Decl_List,
1904 Make_Subprogram_Declaration (Loc, Proc_Spec));
1906 -- Can_Convert_Unconstrained_Function checked that the function
1907 -- has no local declarations except implicit label declarations.
1908 -- Copy these declarations to the built procedure.
1910 if Present (Declarations (N)) then
1911 Body_Decl_List := New_List;
1913 declare
1914 D : Node_Id;
1915 New_D : Node_Id;
1917 begin
1918 D := First (Declarations (N));
1919 while Present (D) loop
1920 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
1922 New_D :=
1923 Make_Implicit_Label_Declaration (Loc,
1924 Make_Defining_Identifier (Loc,
1925 Chars => Chars (Defining_Identifier (D))),
1926 Label_Construct => Empty);
1927 Append_To (Body_Decl_List, New_D);
1929 Next (D);
1930 end loop;
1931 end;
1932 end if;
1934 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
1936 Proc_Body :=
1937 Make_Subprogram_Body (Loc,
1938 Specification => Copy_Separate_Tree (Proc_Spec),
1939 Declarations => Body_Decl_List,
1940 Handled_Statement_Sequence =>
1941 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
1943 Set_Defining_Unit_Name (Specification (Proc_Body),
1944 Make_Defining_Identifier (Loc, Subp_Name));
1946 Append_To (Decl_List, Proc_Body);
1947 end Build_Procedure;
1949 -- Local variables
1951 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
1952 Blk_Stmt : Node_Id;
1953 Proc_Id : Entity_Id;
1954 Proc_Call : Node_Id;
1956 -- Start of processing for Split_Unconstrained_Function
1958 begin
1959 -- Build the associated procedure, analyze it and insert it before
1960 -- the function body N.
1962 declare
1963 Scope : constant Entity_Id := Current_Scope;
1964 Decl_List : List_Id;
1965 begin
1966 Pop_Scope;
1967 Build_Procedure (Proc_Id, Decl_List);
1968 Insert_Actions (N, Decl_List);
1969 Push_Scope (Scope);
1970 end;
1972 -- Build the call to the generated procedure
1974 declare
1975 Actual_List : constant List_Id := New_List;
1976 Formal : Entity_Id;
1978 begin
1979 Append_To (Actual_List,
1980 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
1982 Formal := First_Formal (Spec_Id);
1983 while Present (Formal) loop
1984 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
1986 -- Avoid spurious warning on unreferenced formals
1988 Set_Referenced (Formal);
1989 Next_Formal (Formal);
1990 end loop;
1992 Proc_Call :=
1993 Make_Procedure_Call_Statement (Loc,
1994 Name => New_Occurrence_Of (Proc_Id, Loc),
1995 Parameter_Associations => Actual_List);
1996 end;
1998 -- Generate
2000 -- declare
2001 -- New_Obj : ...
2002 -- begin
2003 -- main_1__F1b (New_Obj, ...);
2004 -- return Obj;
2005 -- end B10b;
2007 Blk_Stmt :=
2008 Make_Block_Statement (Loc,
2009 Declarations => New_List (New_Obj),
2010 Handled_Statement_Sequence =>
2011 Make_Handled_Sequence_Of_Statements (Loc,
2012 Statements => New_List (
2014 Proc_Call,
2016 Make_Simple_Return_Statement (Loc,
2017 Expression =>
2018 New_Occurrence_Of
2019 (Defining_Identifier (New_Obj), Loc)))));
2021 Rewrite (Ret_Node, Blk_Stmt);
2022 end Split_Unconstrained_Function;
2024 -- Local variables
2026 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2028 -- Start of processing for Check_And_Split_Unconstrained_Function
2030 begin
2031 pragma Assert (Back_End_Inlining
2032 and then Ekind (Spec_Id) = E_Function
2033 and then Returns_Unconstrained_Type (Spec_Id)
2034 and then Comes_From_Source (Body_Id)
2035 and then (Has_Pragma_Inline_Always (Spec_Id)
2036 or else Optimization_Level > 0));
2038 -- This routine must not be used in GNATprove mode since GNATprove
2039 -- relies on frontend inlining
2041 pragma Assert (not GNATprove_Mode);
2043 -- No need to split the function if we cannot generate the code
2045 if Serious_Errors_Detected /= 0 then
2046 return;
2047 end if;
2049 -- No action needed in stubs since the attribute Body_To_Inline
2050 -- is not available
2052 if Nkind (Decl) = N_Subprogram_Body_Stub then
2053 return;
2055 -- Cannot build the body to inline if the attribute is already set.
2056 -- This attribute may have been set if this is a subprogram renaming
2057 -- declarations (see Freeze.Build_Renamed_Body).
2059 elsif Present (Body_To_Inline (Decl)) then
2060 return;
2062 -- Check excluded declarations
2064 elsif Present (Declarations (N))
2065 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
2066 then
2067 return;
2069 -- Check excluded statements. There is no need to protect us against
2070 -- exception handlers since they are supported by the GCC backend.
2072 elsif Present (Handled_Statement_Sequence (N))
2073 and then Has_Excluded_Statement
2074 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2075 then
2076 return;
2077 end if;
2079 -- Build the body to inline only if really needed
2081 if Can_Split_Unconstrained_Function (N) then
2082 Split_Unconstrained_Function (N, Spec_Id);
2083 Build_Body_To_Inline (N, Spec_Id);
2084 Set_Is_Inlined (Spec_Id);
2085 end if;
2086 end Check_And_Split_Unconstrained_Function;
2088 -------------------------------------
2089 -- Check_Package_Body_For_Inlining --
2090 -------------------------------------
2092 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2093 Bname : Unit_Name_Type;
2094 E : Entity_Id;
2095 OK : Boolean;
2097 begin
2098 -- Legacy implementation (relying on frontend inlining)
2100 if not Back_End_Inlining
2101 and then Is_Compilation_Unit (P)
2102 and then not Is_Generic_Instance (P)
2103 then
2104 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2106 E := First_Entity (P);
2107 while Present (E) loop
2108 if Has_Pragma_Inline_Always (E)
2109 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2110 then
2111 if not Is_Loaded (Bname) then
2112 Load_Needed_Body (N, OK);
2114 if OK then
2116 -- Check we are not trying to inline a parent whose body
2117 -- depends on a child, when we are compiling the body of
2118 -- the child. Otherwise we have a potential elaboration
2119 -- circularity with inlined subprograms and with
2120 -- Taft-Amendment types.
2122 declare
2123 Comp : Node_Id; -- Body just compiled
2124 Child_Spec : Entity_Id; -- Spec of main unit
2125 Ent : Entity_Id; -- For iteration
2126 With_Clause : Node_Id; -- Context of body.
2128 begin
2129 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2130 and then Present (Body_Entity (P))
2131 then
2132 Child_Spec :=
2133 Defining_Entity
2134 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2136 Comp :=
2137 Parent (Unit_Declaration_Node (Body_Entity (P)));
2139 -- Check whether the context of the body just
2140 -- compiled includes a child of itself, and that
2141 -- child is the spec of the main compilation.
2143 With_Clause := First (Context_Items (Comp));
2144 while Present (With_Clause) loop
2145 if Nkind (With_Clause) = N_With_Clause
2146 and then
2147 Scope (Entity (Name (With_Clause))) = P
2148 and then
2149 Entity (Name (With_Clause)) = Child_Spec
2150 then
2151 Error_Msg_Node_2 := Child_Spec;
2152 Error_Msg_NE
2153 ("body of & depends on child unit&??",
2154 With_Clause, P);
2155 Error_Msg_N
2156 ("\subprograms in body cannot be inlined??",
2157 With_Clause);
2159 -- Disable further inlining from this unit,
2160 -- and keep Taft-amendment types incomplete.
2162 Ent := First_Entity (P);
2163 while Present (Ent) loop
2164 if Is_Type (Ent)
2165 and then Has_Completion_In_Body (Ent)
2166 then
2167 Set_Full_View (Ent, Empty);
2169 elsif Is_Subprogram (Ent) then
2170 Set_Is_Inlined (Ent, False);
2171 end if;
2173 Next_Entity (Ent);
2174 end loop;
2176 return;
2177 end if;
2179 Next (With_Clause);
2180 end loop;
2181 end if;
2182 end;
2184 elsif Ineffective_Inline_Warnings then
2185 Error_Msg_Unit_1 := Bname;
2186 Error_Msg_N
2187 ("unable to inline subprograms defined in $??", P);
2188 Error_Msg_N ("\body not found??", P);
2189 return;
2190 end if;
2191 end if;
2193 return;
2194 end if;
2196 Next_Entity (E);
2197 end loop;
2198 end if;
2199 end Check_Package_Body_For_Inlining;
2201 --------------------
2202 -- Cleanup_Scopes --
2203 --------------------
2205 procedure Cleanup_Scopes is
2206 Elmt : Elmt_Id;
2207 Decl : Node_Id;
2208 Scop : Entity_Id;
2210 begin
2211 Elmt := First_Elmt (To_Clean);
2212 while Present (Elmt) loop
2213 Scop := Node (Elmt);
2215 if Ekind (Scop) = E_Entry then
2216 Scop := Protected_Body_Subprogram (Scop);
2218 elsif Is_Subprogram (Scop)
2219 and then Is_Protected_Type (Scope (Scop))
2220 and then Present (Protected_Body_Subprogram (Scop))
2221 then
2222 -- If a protected operation contains an instance, its cleanup
2223 -- operations have been delayed, and the subprogram has been
2224 -- rewritten in the expansion of the enclosing protected body. It
2225 -- is the corresponding subprogram that may require the cleanup
2226 -- operations, so propagate the information that triggers cleanup
2227 -- activity.
2229 Set_Uses_Sec_Stack
2230 (Protected_Body_Subprogram (Scop),
2231 Uses_Sec_Stack (Scop));
2233 Scop := Protected_Body_Subprogram (Scop);
2234 end if;
2236 if Ekind (Scop) = E_Block then
2237 Decl := Parent (Block_Node (Scop));
2239 else
2240 Decl := Unit_Declaration_Node (Scop);
2242 if Nkind_In (Decl, N_Subprogram_Declaration,
2243 N_Task_Type_Declaration,
2244 N_Subprogram_Body_Stub)
2245 then
2246 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2247 end if;
2248 end if;
2250 Push_Scope (Scop);
2251 Expand_Cleanup_Actions (Decl);
2252 End_Scope;
2254 Elmt := Next_Elmt (Elmt);
2255 end loop;
2256 end Cleanup_Scopes;
2258 -------------------------
2259 -- Expand_Inlined_Call --
2260 -------------------------
2262 procedure Expand_Inlined_Call
2263 (N : Node_Id;
2264 Subp : Entity_Id;
2265 Orig_Subp : Entity_Id)
2267 Loc : constant Source_Ptr := Sloc (N);
2268 Is_Predef : constant Boolean :=
2269 Is_Predefined_File_Name
2270 (Unit_File_Name (Get_Source_Unit (Subp)));
2271 Orig_Bod : constant Node_Id :=
2272 Body_To_Inline (Unit_Declaration_Node (Subp));
2274 Blk : Node_Id;
2275 Decl : Node_Id;
2276 Decls : constant List_Id := New_List;
2277 Exit_Lab : Entity_Id := Empty;
2278 F : Entity_Id;
2279 A : Node_Id;
2280 Lab_Decl : Node_Id;
2281 Lab_Id : Node_Id;
2282 New_A : Node_Id;
2283 Num_Ret : Nat := 0;
2284 Ret_Type : Entity_Id;
2286 Targ : Node_Id;
2287 -- The target of the call. If context is an assignment statement then
2288 -- this is the left-hand side of the assignment, else it is a temporary
2289 -- to which the return value is assigned prior to rewriting the call.
2291 Targ1 : Node_Id;
2292 -- A separate target used when the return type is unconstrained
2294 Temp : Entity_Id;
2295 Temp_Typ : Entity_Id;
2297 Return_Object : Entity_Id := Empty;
2298 -- Entity in declaration in an extended_return_statement
2300 Is_Unc : Boolean;
2301 Is_Unc_Decl : Boolean;
2302 -- If the type returned by the function is unconstrained and the call
2303 -- can be inlined, special processing is required.
2305 procedure Declare_Postconditions_Result;
2306 -- When generating C code, declare _Result, which may be used in the
2307 -- inlined _Postconditions procedure to verify the return value.
2309 procedure Make_Exit_Label;
2310 -- Build declaration for exit label to be used in Return statements,
2311 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2312 -- declaration). Does nothing if Exit_Lab already set.
2314 function Process_Formals (N : Node_Id) return Traverse_Result;
2315 -- Replace occurrence of a formal with the corresponding actual, or the
2316 -- thunk generated for it. Replace a return statement with an assignment
2317 -- to the target of the call, with appropriate conversions if needed.
2319 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2320 -- If the call being expanded is that of an internal subprogram, set the
2321 -- sloc of the generated block to that of the call itself, so that the
2322 -- expansion is skipped by the "next" command in gdb. Same processing
2323 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2324 -- Debug_Generated_Code is true, suppress this change to simplify our
2325 -- own development. Same in GNATprove mode, to ensure that warnings and
2326 -- diagnostics point to the proper location.
2328 procedure Reset_Dispatching_Calls (N : Node_Id);
2329 -- In subtree N search for occurrences of dispatching calls that use the
2330 -- Ada 2005 Object.Operation notation and the object is a formal of the
2331 -- inlined subprogram. Reset the entity associated with Operation in all
2332 -- the found occurrences.
2334 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2335 -- If the function body is a single expression, replace call with
2336 -- expression, else insert block appropriately.
2338 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2339 -- If procedure body has no local variables, inline body without
2340 -- creating block, otherwise rewrite call with block.
2342 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2343 -- Determine whether a formal parameter is used only once in Orig_Bod
2345 -----------------------------------
2346 -- Declare_Postconditions_Result --
2347 -----------------------------------
2349 procedure Declare_Postconditions_Result is
2350 Enclosing_Subp : constant Entity_Id := Scope (Subp);
2352 begin
2353 pragma Assert
2354 (Modify_Tree_For_C
2355 and then Is_Subprogram (Enclosing_Subp)
2356 and then Present (Postconditions_Proc (Enclosing_Subp)));
2358 if Ekind (Enclosing_Subp) = E_Function then
2359 if Nkind (First (Parameter_Associations (N))) in
2360 N_Numeric_Or_String_Literal
2361 then
2362 Append_To (Declarations (Blk),
2363 Make_Object_Declaration (Loc,
2364 Defining_Identifier =>
2365 Make_Defining_Identifier (Loc, Name_uResult),
2366 Constant_Present => True,
2367 Object_Definition =>
2368 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2369 Expression =>
2370 New_Copy_Tree (First (Parameter_Associations (N)))));
2371 else
2372 Append_To (Declarations (Blk),
2373 Make_Object_Renaming_Declaration (Loc,
2374 Defining_Identifier =>
2375 Make_Defining_Identifier (Loc, Name_uResult),
2376 Subtype_Mark =>
2377 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2378 Name =>
2379 New_Copy_Tree (First (Parameter_Associations (N)))));
2380 end if;
2381 end if;
2382 end Declare_Postconditions_Result;
2384 ---------------------
2385 -- Make_Exit_Label --
2386 ---------------------
2388 procedure Make_Exit_Label is
2389 Lab_Ent : Entity_Id;
2390 begin
2391 if No (Exit_Lab) then
2392 Lab_Ent := Make_Temporary (Loc, 'L');
2393 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
2394 Exit_Lab := Make_Label (Loc, Lab_Id);
2395 Lab_Decl :=
2396 Make_Implicit_Label_Declaration (Loc,
2397 Defining_Identifier => Lab_Ent,
2398 Label_Construct => Exit_Lab);
2399 end if;
2400 end Make_Exit_Label;
2402 ---------------------
2403 -- Process_Formals --
2404 ---------------------
2406 function Process_Formals (N : Node_Id) return Traverse_Result is
2407 A : Entity_Id;
2408 E : Entity_Id;
2409 Ret : Node_Id;
2411 begin
2412 if Is_Entity_Name (N) and then Present (Entity (N)) then
2413 E := Entity (N);
2415 if Is_Formal (E) and then Scope (E) = Subp then
2416 A := Renamed_Object (E);
2418 -- Rewrite the occurrence of the formal into an occurrence of
2419 -- the actual. Also establish visibility on the proper view of
2420 -- the actual's subtype for the body's context (if the actual's
2421 -- subtype is private at the call point but its full view is
2422 -- visible to the body, then the inlined tree here must be
2423 -- analyzed with the full view).
2425 if Is_Entity_Name (A) then
2426 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
2427 Check_Private_View (N);
2429 elsif Nkind (A) = N_Defining_Identifier then
2430 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
2431 Check_Private_View (N);
2433 -- Numeric literal
2435 else
2436 Rewrite (N, New_Copy (A));
2437 end if;
2438 end if;
2440 return Skip;
2442 elsif Is_Entity_Name (N)
2443 and then Present (Return_Object)
2444 and then Chars (N) = Chars (Return_Object)
2445 then
2446 -- Occurrence within an extended return statement. The return
2447 -- object is local to the body been inlined, and thus the generic
2448 -- copy is not analyzed yet, so we match by name, and replace it
2449 -- with target of call.
2451 if Nkind (Targ) = N_Defining_Identifier then
2452 Rewrite (N, New_Occurrence_Of (Targ, Loc));
2453 else
2454 Rewrite (N, New_Copy_Tree (Targ));
2455 end if;
2457 return Skip;
2459 elsif Nkind (N) = N_Simple_Return_Statement then
2460 if No (Expression (N)) then
2461 Num_Ret := Num_Ret + 1;
2462 Make_Exit_Label;
2463 Rewrite (N,
2464 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2466 else
2467 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2468 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2469 then
2470 -- Function body is a single expression. No need for
2471 -- exit label.
2473 null;
2475 else
2476 Num_Ret := Num_Ret + 1;
2477 Make_Exit_Label;
2478 end if;
2480 -- Because of the presence of private types, the views of the
2481 -- expression and the context may be different, so place an
2482 -- unchecked conversion to the context type to avoid spurious
2483 -- errors, e.g. when the expression is a numeric literal and
2484 -- the context is private. If the expression is an aggregate,
2485 -- use a qualified expression, because an aggregate is not a
2486 -- legal argument of a conversion. Ditto for numeric literals,
2487 -- which must be resolved to a specific type.
2489 if Nkind_In (Expression (N), N_Aggregate,
2490 N_Null,
2491 N_Real_Literal,
2492 N_Integer_Literal)
2493 then
2494 Ret :=
2495 Make_Qualified_Expression (Sloc (N),
2496 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2497 Expression => Relocate_Node (Expression (N)));
2498 else
2499 Ret :=
2500 Unchecked_Convert_To
2501 (Ret_Type, Relocate_Node (Expression (N)));
2502 end if;
2504 if Nkind (Targ) = N_Defining_Identifier then
2505 Rewrite (N,
2506 Make_Assignment_Statement (Loc,
2507 Name => New_Occurrence_Of (Targ, Loc),
2508 Expression => Ret));
2509 else
2510 Rewrite (N,
2511 Make_Assignment_Statement (Loc,
2512 Name => New_Copy (Targ),
2513 Expression => Ret));
2514 end if;
2516 Set_Assignment_OK (Name (N));
2518 if Present (Exit_Lab) then
2519 Insert_After (N,
2520 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2521 end if;
2522 end if;
2524 return OK;
2526 -- An extended return becomes a block whose first statement is the
2527 -- assignment of the initial expression of the return object to the
2528 -- target of the call itself.
2530 elsif Nkind (N) = N_Extended_Return_Statement then
2531 declare
2532 Return_Decl : constant Entity_Id :=
2533 First (Return_Object_Declarations (N));
2534 Assign : Node_Id;
2536 begin
2537 Return_Object := Defining_Identifier (Return_Decl);
2539 if Present (Expression (Return_Decl)) then
2540 if Nkind (Targ) = N_Defining_Identifier then
2541 Assign :=
2542 Make_Assignment_Statement (Loc,
2543 Name => New_Occurrence_Of (Targ, Loc),
2544 Expression => Expression (Return_Decl));
2545 else
2546 Assign :=
2547 Make_Assignment_Statement (Loc,
2548 Name => New_Copy (Targ),
2549 Expression => Expression (Return_Decl));
2550 end if;
2552 Set_Assignment_OK (Name (Assign));
2554 if No (Handled_Statement_Sequence (N)) then
2555 Set_Handled_Statement_Sequence (N,
2556 Make_Handled_Sequence_Of_Statements (Loc,
2557 Statements => New_List));
2558 end if;
2560 Prepend (Assign,
2561 Statements (Handled_Statement_Sequence (N)));
2562 end if;
2564 Rewrite (N,
2565 Make_Block_Statement (Loc,
2566 Handled_Statement_Sequence =>
2567 Handled_Statement_Sequence (N)));
2569 return OK;
2570 end;
2572 -- Remove pragma Unreferenced since it may refer to formals that
2573 -- are not visible in the inlined body, and in any case we will
2574 -- not be posting warnings on the inlined body so it is unneeded.
2576 elsif Nkind (N) = N_Pragma
2577 and then Pragma_Name (N) = Name_Unreferenced
2578 then
2579 Rewrite (N, Make_Null_Statement (Sloc (N)));
2580 return OK;
2582 else
2583 return OK;
2584 end if;
2585 end Process_Formals;
2587 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2589 ------------------
2590 -- Process_Sloc --
2591 ------------------
2593 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2594 begin
2595 if not Debug_Generated_Code then
2596 Set_Sloc (Nod, Sloc (N));
2597 Set_Comes_From_Source (Nod, False);
2598 end if;
2600 return OK;
2601 end Process_Sloc;
2603 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2605 ------------------------------
2606 -- Reset_Dispatching_Calls --
2607 ------------------------------
2609 procedure Reset_Dispatching_Calls (N : Node_Id) is
2611 function Do_Reset (N : Node_Id) return Traverse_Result;
2612 -- Comment required ???
2614 --------------
2615 -- Do_Reset --
2616 --------------
2618 function Do_Reset (N : Node_Id) return Traverse_Result is
2619 begin
2620 if Nkind (N) = N_Procedure_Call_Statement
2621 and then Nkind (Name (N)) = N_Selected_Component
2622 and then Nkind (Prefix (Name (N))) = N_Identifier
2623 and then Is_Formal (Entity (Prefix (Name (N))))
2624 and then Is_Dispatching_Operation
2625 (Entity (Selector_Name (Name (N))))
2626 then
2627 Set_Entity (Selector_Name (Name (N)), Empty);
2628 end if;
2630 return OK;
2631 end Do_Reset;
2633 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
2635 -- Local variables
2637 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
2638 pragma Unreferenced (Dummy);
2640 -- Start of processing for Reset_Dispatching_Calls
2642 begin
2643 null;
2644 end Reset_Dispatching_Calls;
2646 ---------------------------
2647 -- Rewrite_Function_Call --
2648 ---------------------------
2650 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2651 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2652 Fst : constant Node_Id := First (Statements (HSS));
2654 begin
2655 -- Optimize simple case: function body is a single return statement,
2656 -- which has been expanded into an assignment.
2658 if Is_Empty_List (Declarations (Blk))
2659 and then Nkind (Fst) = N_Assignment_Statement
2660 and then No (Next (Fst))
2661 then
2662 -- The function call may have been rewritten as the temporary
2663 -- that holds the result of the call, in which case remove the
2664 -- now useless declaration.
2666 if Nkind (N) = N_Identifier
2667 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2668 then
2669 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2670 end if;
2672 Rewrite (N, Expression (Fst));
2674 elsif Nkind (N) = N_Identifier
2675 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2676 then
2677 -- The block assigns the result of the call to the temporary
2679 Insert_After (Parent (Entity (N)), Blk);
2681 -- If the context is an assignment, and the left-hand side is free of
2682 -- side-effects, the replacement is also safe.
2683 -- Can this be generalized further???
2685 elsif Nkind (Parent (N)) = N_Assignment_Statement
2686 and then
2687 (Is_Entity_Name (Name (Parent (N)))
2688 or else
2689 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
2690 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
2692 or else
2693 (Nkind (Name (Parent (N))) = N_Selected_Component
2694 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
2695 then
2696 -- Replace assignment with the block
2698 declare
2699 Original_Assignment : constant Node_Id := Parent (N);
2701 begin
2702 -- Preserve the original assignment node to keep the complete
2703 -- assignment subtree consistent enough for Analyze_Assignment
2704 -- to proceed (specifically, the original Lhs node must still
2705 -- have an assignment statement as its parent).
2707 -- We cannot rely on Original_Node to go back from the block
2708 -- node to the assignment node, because the assignment might
2709 -- already be a rewrite substitution.
2711 Discard_Node (Relocate_Node (Original_Assignment));
2712 Rewrite (Original_Assignment, Blk);
2713 end;
2715 elsif Nkind (Parent (N)) = N_Object_Declaration then
2717 -- A call to a function which returns an unconstrained type
2718 -- found in the expression initializing an object-declaration is
2719 -- expanded into a procedure call which must be added after the
2720 -- object declaration.
2722 if Is_Unc_Decl and Back_End_Inlining then
2723 Insert_Action_After (Parent (N), Blk);
2724 else
2725 Set_Expression (Parent (N), Empty);
2726 Insert_After (Parent (N), Blk);
2727 end if;
2729 elsif Is_Unc and then not Back_End_Inlining then
2730 Insert_Before (Parent (N), Blk);
2731 end if;
2732 end Rewrite_Function_Call;
2734 ----------------------------
2735 -- Rewrite_Procedure_Call --
2736 ----------------------------
2738 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2739 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2741 begin
2742 -- If there is a transient scope for N, this will be the scope of the
2743 -- actions for N, and the statements in Blk need to be within this
2744 -- scope. For example, they need to have visibility on the constant
2745 -- declarations created for the formals.
2747 -- If N needs no transient scope, and if there are no declarations in
2748 -- the inlined body, we can do a little optimization and insert the
2749 -- statements for the body directly after N, and rewrite N to a
2750 -- null statement, instead of rewriting N into a full-blown block
2751 -- statement.
2753 if not Scope_Is_Transient
2754 and then Is_Empty_List (Declarations (Blk))
2755 then
2756 Insert_List_After (N, Statements (HSS));
2757 Rewrite (N, Make_Null_Statement (Loc));
2758 else
2759 Rewrite (N, Blk);
2760 end if;
2761 end Rewrite_Procedure_Call;
2763 -------------------------
2764 -- Formal_Is_Used_Once --
2765 -------------------------
2767 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2768 Use_Counter : Int := 0;
2770 function Count_Uses (N : Node_Id) return Traverse_Result;
2771 -- Traverse the tree and count the uses of the formal parameter.
2772 -- In this case, for optimization purposes, we do not need to
2773 -- continue the traversal once more than one use is encountered.
2775 ----------------
2776 -- Count_Uses --
2777 ----------------
2779 function Count_Uses (N : Node_Id) return Traverse_Result is
2780 begin
2781 -- The original node is an identifier
2783 if Nkind (N) = N_Identifier
2784 and then Present (Entity (N))
2786 -- Original node's entity points to the one in the copied body
2788 and then Nkind (Entity (N)) = N_Identifier
2789 and then Present (Entity (Entity (N)))
2791 -- The entity of the copied node is the formal parameter
2793 and then Entity (Entity (N)) = Formal
2794 then
2795 Use_Counter := Use_Counter + 1;
2797 if Use_Counter > 1 then
2799 -- Denote more than one use and abandon the traversal
2801 Use_Counter := 2;
2802 return Abandon;
2804 end if;
2805 end if;
2807 return OK;
2808 end Count_Uses;
2810 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2812 -- Start of processing for Formal_Is_Used_Once
2814 begin
2815 Count_Formal_Uses (Orig_Bod);
2816 return Use_Counter = 1;
2817 end Formal_Is_Used_Once;
2819 -- Start of processing for Expand_Inlined_Call
2821 begin
2822 -- Initializations for old/new semantics
2824 if not Back_End_Inlining then
2825 Is_Unc := Is_Array_Type (Etype (Subp))
2826 and then not Is_Constrained (Etype (Subp));
2827 Is_Unc_Decl := False;
2828 else
2829 Is_Unc := Returns_Unconstrained_Type (Subp)
2830 and then Optimization_Level > 0;
2831 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
2832 and then Is_Unc;
2833 end if;
2835 -- Check for an illegal attempt to inline a recursive procedure. If the
2836 -- subprogram has parameters this is detected when trying to supply a
2837 -- binding for parameters that already have one. For parameterless
2838 -- subprograms this must be done explicitly.
2840 if In_Open_Scopes (Subp) then
2841 Cannot_Inline
2842 ("cannot inline call to recursive subprogram?", N, Subp);
2843 Set_Is_Inlined (Subp, False);
2844 return;
2846 -- Skip inlining if this is not a true inlining since the attribute
2847 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
2848 -- true inlining, Orig_Bod has code rather than being an entity.
2850 elsif Nkind (Orig_Bod) in N_Entity then
2851 return;
2853 -- Skip inlining if the function returns an unconstrained type using
2854 -- an extended return statement since this part of the new inlining
2855 -- model which is not yet supported by the current implementation. ???
2857 elsif Is_Unc
2858 and then
2859 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
2860 N_Extended_Return_Statement
2861 and then not Back_End_Inlining
2862 then
2863 return;
2864 end if;
2866 if Nkind (Orig_Bod) = N_Defining_Identifier
2867 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2868 then
2869 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2870 -- can be replaced with calls to the renamed entity directly, because
2871 -- the subprograms are subtype conformant. If the renamed subprogram
2872 -- is an inherited operation, we must redo the expansion because
2873 -- implicit conversions may be needed. Similarly, if the renamed
2874 -- entity is inlined, expand the call for further optimizations.
2876 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2878 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
2879 Expand_Call (N);
2880 end if;
2882 return;
2883 end if;
2885 -- Register the call in the list of inlined calls
2887 Append_New_Elmt (N, To => Inlined_Calls);
2889 -- Use generic machinery to copy body of inlined subprogram, as if it
2890 -- were an instantiation, resetting source locations appropriately, so
2891 -- that nested inlined calls appear in the main unit.
2893 Save_Env (Subp, Empty);
2894 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2896 -- Old semantics
2898 if not Back_End_Inlining then
2899 declare
2900 Bod : Node_Id;
2902 begin
2903 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2904 Blk :=
2905 Make_Block_Statement (Loc,
2906 Declarations => Declarations (Bod),
2907 Handled_Statement_Sequence =>
2908 Handled_Statement_Sequence (Bod));
2910 if No (Declarations (Bod)) then
2911 Set_Declarations (Blk, New_List);
2912 end if;
2914 -- When generating C code, declare _Result, which may be used to
2915 -- verify the return value.
2917 if Modify_Tree_For_C
2918 and then Nkind (N) = N_Procedure_Call_Statement
2919 and then Chars (Name (N)) = Name_uPostconditions
2920 then
2921 Declare_Postconditions_Result;
2922 end if;
2924 -- For the unconstrained case, capture the name of the local
2925 -- variable that holds the result. This must be the first
2926 -- declaration in the block, because its bounds cannot depend
2927 -- on local variables. Otherwise there is no way to declare the
2928 -- result outside of the block. Needless to say, in general the
2929 -- bounds will depend on the actuals in the call.
2931 -- If the context is an assignment statement, as is the case
2932 -- for the expansion of an extended return, the left-hand side
2933 -- provides bounds even if the return type is unconstrained.
2935 if Is_Unc then
2936 declare
2937 First_Decl : Node_Id;
2939 begin
2940 First_Decl := First (Declarations (Blk));
2942 if Nkind (First_Decl) /= N_Object_Declaration then
2943 return;
2944 end if;
2946 if Nkind (Parent (N)) /= N_Assignment_Statement then
2947 Targ1 := Defining_Identifier (First_Decl);
2948 else
2949 Targ1 := Name (Parent (N));
2950 end if;
2951 end;
2952 end if;
2953 end;
2955 -- New semantics
2957 else
2958 declare
2959 Bod : Node_Id;
2961 begin
2962 -- General case
2964 if not Is_Unc then
2965 Bod :=
2966 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2967 Blk :=
2968 Make_Block_Statement (Loc,
2969 Declarations => Declarations (Bod),
2970 Handled_Statement_Sequence =>
2971 Handled_Statement_Sequence (Bod));
2973 -- Inline a call to a function that returns an unconstrained type.
2974 -- The semantic analyzer checked that frontend-inlined functions
2975 -- returning unconstrained types have no declarations and have
2976 -- a single extended return statement. As part of its processing
2977 -- the function was split in two subprograms: a procedure P and
2978 -- a function F that has a block with a call to procedure P (see
2979 -- Split_Unconstrained_Function).
2981 else
2982 pragma Assert
2983 (Nkind
2984 (First
2985 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
2986 N_Block_Statement);
2988 declare
2989 Blk_Stmt : constant Node_Id :=
2990 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
2991 First_Stmt : constant Node_Id :=
2992 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
2993 Second_Stmt : constant Node_Id := Next (First_Stmt);
2995 begin
2996 pragma Assert
2997 (Nkind (First_Stmt) = N_Procedure_Call_Statement
2998 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
2999 and then No (Next (Second_Stmt)));
3001 Bod :=
3002 Copy_Generic_Node
3003 (First
3004 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3005 Empty, Instantiating => True);
3006 Blk := Bod;
3008 -- Capture the name of the local variable that holds the
3009 -- result. This must be the first declaration in the block,
3010 -- because its bounds cannot depend on local variables.
3011 -- Otherwise there is no way to declare the result outside
3012 -- of the block. Needless to say, in general the bounds will
3013 -- depend on the actuals in the call.
3015 if Nkind (Parent (N)) /= N_Assignment_Statement then
3016 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3018 -- If the context is an assignment statement, as is the case
3019 -- for the expansion of an extended return, the left-hand
3020 -- side provides bounds even if the return type is
3021 -- unconstrained.
3023 else
3024 Targ1 := Name (Parent (N));
3025 end if;
3026 end;
3027 end if;
3029 if No (Declarations (Bod)) then
3030 Set_Declarations (Blk, New_List);
3031 end if;
3032 end;
3033 end if;
3035 -- If this is a derived function, establish the proper return type
3037 if Present (Orig_Subp) and then Orig_Subp /= Subp then
3038 Ret_Type := Etype (Orig_Subp);
3039 else
3040 Ret_Type := Etype (Subp);
3041 end if;
3043 -- Create temporaries for the actuals that are expressions, or that are
3044 -- scalars and require copying to preserve semantics.
3046 F := First_Formal (Subp);
3047 A := First_Actual (N);
3048 while Present (F) loop
3049 if Present (Renamed_Object (F)) then
3051 -- If expander is active, it is an error to try to inline a
3052 -- recursive program. In GNATprove mode, just indicate that the
3053 -- inlining will not happen, and mark the subprogram as not always
3054 -- inlined.
3056 if GNATprove_Mode then
3057 Cannot_Inline
3058 ("cannot inline call to recursive subprogram?", N, Subp);
3059 Set_Is_Inlined_Always (Subp, False);
3060 else
3061 Error_Msg_N
3062 ("cannot inline call to recursive subprogram", N);
3063 end if;
3065 return;
3066 end if;
3068 -- Reset Last_Assignment for any parameters of mode out or in out, to
3069 -- prevent spurious warnings about overwriting for assignments to the
3070 -- formal in the inlined code.
3072 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
3073 Set_Last_Assignment (Entity (A), Empty);
3074 end if;
3076 -- If the argument may be a controlling argument in a call within
3077 -- the inlined body, we must preserve its classwide nature to insure
3078 -- that dynamic dispatching take place subsequently. If the formal
3079 -- has a constraint it must be preserved to retain the semantics of
3080 -- the body.
3082 if Is_Class_Wide_Type (Etype (F))
3083 or else (Is_Access_Type (Etype (F))
3084 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3085 then
3086 Temp_Typ := Etype (F);
3088 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3089 and then Etype (F) /= Base_Type (Etype (F))
3090 then
3091 Temp_Typ := Etype (F);
3092 else
3093 Temp_Typ := Etype (A);
3094 end if;
3096 -- If the actual is a simple name or a literal, no need to
3097 -- create a temporary, object can be used directly.
3099 -- If the actual is a literal and the formal has its address taken,
3100 -- we cannot pass the literal itself as an argument, so its value
3101 -- must be captured in a temporary. Skip this optimization in
3102 -- GNATprove mode, to make sure any check on a type conversion
3103 -- will be issued.
3105 if (Is_Entity_Name (A)
3106 and then
3107 (not Is_Scalar_Type (Etype (A))
3108 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3109 and then not GNATprove_Mode)
3111 -- When the actual is an identifier and the corresponding formal is
3112 -- used only once in the original body, the formal can be substituted
3113 -- directly with the actual parameter. Skip this optimization in
3114 -- GNATprove mode, to make sure any check on a type conversion
3115 -- will be issued.
3117 or else
3118 (Nkind (A) = N_Identifier
3119 and then Formal_Is_Used_Once (F)
3120 and then not GNATprove_Mode)
3122 or else
3123 (Nkind_In (A, N_Real_Literal,
3124 N_Integer_Literal,
3125 N_Character_Literal)
3126 and then not Address_Taken (F))
3127 then
3128 if Etype (F) /= Etype (A) then
3129 Set_Renamed_Object
3130 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3131 else
3132 Set_Renamed_Object (F, A);
3133 end if;
3135 else
3136 Temp := Make_Temporary (Loc, 'C');
3138 -- If the actual for an in/in-out parameter is a view conversion,
3139 -- make it into an unchecked conversion, given that an untagged
3140 -- type conversion is not a proper object for a renaming.
3142 -- In-out conversions that involve real conversions have already
3143 -- been transformed in Expand_Actuals.
3145 if Nkind (A) = N_Type_Conversion
3146 and then Ekind (F) /= E_In_Parameter
3147 then
3148 New_A :=
3149 Make_Unchecked_Type_Conversion (Loc,
3150 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3151 Expression => Relocate_Node (Expression (A)));
3153 elsif Etype (F) /= Etype (A) then
3154 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3155 Temp_Typ := Etype (F);
3157 else
3158 New_A := Relocate_Node (A);
3159 end if;
3161 Set_Sloc (New_A, Sloc (N));
3163 -- If the actual has a by-reference type, it cannot be copied,
3164 -- so its value is captured in a renaming declaration. Otherwise
3165 -- declare a local constant initialized with the actual.
3167 -- We also use a renaming declaration for expressions of an array
3168 -- type that is not bit-packed, both for efficiency reasons and to
3169 -- respect the semantics of the call: in most cases the original
3170 -- call will pass the parameter by reference, and thus the inlined
3171 -- code will have the same semantics.
3173 -- Finally, we need a renaming declaration in the case of limited
3174 -- types for which initialization cannot be by copy either.
3176 if Ekind (F) = E_In_Parameter
3177 and then not Is_By_Reference_Type (Etype (A))
3178 and then not Is_Limited_Type (Etype (A))
3179 and then
3180 (not Is_Array_Type (Etype (A))
3181 or else not Is_Object_Reference (A)
3182 or else Is_Bit_Packed_Array (Etype (A)))
3183 then
3184 Decl :=
3185 Make_Object_Declaration (Loc,
3186 Defining_Identifier => Temp,
3187 Constant_Present => True,
3188 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3189 Expression => New_A);
3191 else
3192 -- In GNATprove mode, make an explicit copy of input
3193 -- parameters when formal and actual types differ, to make
3194 -- sure any check on the type conversion will be issued.
3195 -- The legality of the copy is ensured by calling first
3196 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3198 if GNATprove_Mode
3199 and then Ekind (F) /= E_Out_Parameter
3200 and then not Same_Type (Etype (F), Etype (A))
3201 then
3202 pragma Assert (not (Is_By_Reference_Type (Etype (A))));
3203 pragma Assert (not (Is_Limited_Type (Etype (A))));
3205 Append_To (Decls,
3206 Make_Object_Declaration (Loc,
3207 Defining_Identifier => Make_Temporary (Loc, 'C'),
3208 Constant_Present => True,
3209 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3210 Expression => New_Copy_Tree (New_A)));
3211 end if;
3213 Decl :=
3214 Make_Object_Renaming_Declaration (Loc,
3215 Defining_Identifier => Temp,
3216 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3217 Name => New_A);
3218 end if;
3220 Append (Decl, Decls);
3221 Set_Renamed_Object (F, Temp);
3222 end if;
3224 Next_Formal (F);
3225 Next_Actual (A);
3226 end loop;
3228 -- Establish target of function call. If context is not assignment or
3229 -- declaration, create a temporary as a target. The declaration for the
3230 -- temporary may be subsequently optimized away if the body is a single
3231 -- expression, or if the left-hand side of the assignment is simple
3232 -- enough, i.e. an entity or an explicit dereference of one.
3234 if Ekind (Subp) = E_Function then
3235 if Nkind (Parent (N)) = N_Assignment_Statement
3236 and then Is_Entity_Name (Name (Parent (N)))
3237 then
3238 Targ := Name (Parent (N));
3240 elsif Nkind (Parent (N)) = N_Assignment_Statement
3241 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3242 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3243 then
3244 Targ := Name (Parent (N));
3246 elsif Nkind (Parent (N)) = N_Assignment_Statement
3247 and then Nkind (Name (Parent (N))) = N_Selected_Component
3248 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3249 then
3250 Targ := New_Copy_Tree (Name (Parent (N)));
3252 elsif Nkind (Parent (N)) = N_Object_Declaration
3253 and then Is_Limited_Type (Etype (Subp))
3254 then
3255 Targ := Defining_Identifier (Parent (N));
3257 -- New semantics: In an object declaration avoid an extra copy
3258 -- of the result of a call to an inlined function that returns
3259 -- an unconstrained type
3261 elsif Back_End_Inlining
3262 and then Nkind (Parent (N)) = N_Object_Declaration
3263 and then Is_Unc
3264 then
3265 Targ := Defining_Identifier (Parent (N));
3267 else
3268 -- Replace call with temporary and create its declaration
3270 Temp := Make_Temporary (Loc, 'C');
3271 Set_Is_Internal (Temp);
3273 -- For the unconstrained case, the generated temporary has the
3274 -- same constrained declaration as the result variable. It may
3275 -- eventually be possible to remove that temporary and use the
3276 -- result variable directly.
3278 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
3279 then
3280 Decl :=
3281 Make_Object_Declaration (Loc,
3282 Defining_Identifier => Temp,
3283 Object_Definition =>
3284 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3286 Replace_Formals (Decl);
3288 else
3289 Decl :=
3290 Make_Object_Declaration (Loc,
3291 Defining_Identifier => Temp,
3292 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
3294 Set_Etype (Temp, Ret_Type);
3295 end if;
3297 Set_No_Initialization (Decl);
3298 Append (Decl, Decls);
3299 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3300 Targ := Temp;
3301 end if;
3302 end if;
3304 Insert_Actions (N, Decls);
3306 if Is_Unc_Decl then
3308 -- Special management for inlining a call to a function that returns
3309 -- an unconstrained type and initializes an object declaration: we
3310 -- avoid generating undesired extra calls and goto statements.
3312 -- Given:
3313 -- function Func (...) return ...
3314 -- begin
3315 -- declare
3316 -- Result : String (1 .. 4);
3317 -- begin
3318 -- Proc (Result, ...);
3319 -- return Result;
3320 -- end;
3321 -- end F;
3323 -- Result : String := Func (...);
3325 -- Replace this object declaration by:
3327 -- Result : String (1 .. 4);
3328 -- Proc (Result, ...);
3330 Remove_Homonym (Targ);
3332 Decl :=
3333 Make_Object_Declaration
3334 (Loc,
3335 Defining_Identifier => Targ,
3336 Object_Definition =>
3337 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3338 Replace_Formals (Decl);
3339 Rewrite (Parent (N), Decl);
3340 Analyze (Parent (N));
3342 -- Avoid spurious warnings since we know that this declaration is
3343 -- referenced by the procedure call.
3345 Set_Never_Set_In_Source (Targ, False);
3347 -- Remove the local declaration of the extended return stmt from the
3348 -- inlined code
3350 Remove (Parent (Targ1));
3352 -- Update the reference to the result (since we have rewriten the
3353 -- object declaration)
3355 declare
3356 Blk_Call_Stmt : Node_Id;
3358 begin
3359 -- Capture the call to the procedure
3361 Blk_Call_Stmt :=
3362 First (Statements (Handled_Statement_Sequence (Blk)));
3363 pragma Assert
3364 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
3366 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
3367 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
3368 New_Occurrence_Of (Targ, Loc));
3369 end;
3371 -- Remove the return statement
3373 pragma Assert
3374 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3375 N_Simple_Return_Statement);
3377 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3378 end if;
3380 -- Traverse the tree and replace formals with actuals or their thunks.
3381 -- Attach block to tree before analysis and rewriting.
3383 Replace_Formals (Blk);
3384 Set_Parent (Blk, N);
3386 if GNATprove_Mode then
3387 null;
3389 elsif not Comes_From_Source (Subp) or else Is_Predef then
3390 Reset_Slocs (Blk);
3391 end if;
3393 if Is_Unc_Decl then
3395 -- No action needed since return statement has been already removed
3397 null;
3399 elsif Present (Exit_Lab) then
3401 -- If there's a single return statement at the end of the subprogram,
3402 -- the corresponding goto statement and the corresponding label are
3403 -- useless.
3405 if Num_Ret = 1
3406 and then
3407 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3408 N_Goto_Statement
3409 then
3410 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3411 else
3412 Append (Lab_Decl, (Declarations (Blk)));
3413 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3414 end if;
3415 end if;
3417 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3418 -- on conflicting private views that Gigi would ignore. If this is a
3419 -- predefined unit, analyze with checks off, as is done in the non-
3420 -- inlined run-time units.
3422 declare
3423 I_Flag : constant Boolean := In_Inlined_Body;
3425 begin
3426 In_Inlined_Body := True;
3428 if Is_Predef then
3429 declare
3430 Style : constant Boolean := Style_Check;
3432 begin
3433 Style_Check := False;
3435 -- Search for dispatching calls that use the Object.Operation
3436 -- notation using an Object that is a parameter of the inlined
3437 -- function. We reset the decoration of Operation to force
3438 -- the reanalysis of the inlined dispatching call because
3439 -- the actual object has been inlined.
3441 Reset_Dispatching_Calls (Blk);
3443 Analyze (Blk, Suppress => All_Checks);
3444 Style_Check := Style;
3445 end;
3447 else
3448 Analyze (Blk);
3449 end if;
3451 In_Inlined_Body := I_Flag;
3452 end;
3454 if Ekind (Subp) = E_Procedure then
3455 Rewrite_Procedure_Call (N, Blk);
3457 else
3458 Rewrite_Function_Call (N, Blk);
3460 if Is_Unc_Decl then
3461 null;
3463 -- For the unconstrained case, the replacement of the call has been
3464 -- made prior to the complete analysis of the generated declarations.
3465 -- Propagate the proper type now.
3467 elsif Is_Unc then
3468 if Nkind (N) = N_Identifier then
3469 Set_Etype (N, Etype (Entity (N)));
3470 else
3471 Set_Etype (N, Etype (Targ1));
3472 end if;
3473 end if;
3474 end if;
3476 Restore_Env;
3478 -- Cleanup mapping between formals and actuals for other expansions
3480 F := First_Formal (Subp);
3481 while Present (F) loop
3482 Set_Renamed_Object (F, Empty);
3483 Next_Formal (F);
3484 end loop;
3485 end Expand_Inlined_Call;
3487 --------------------------
3488 -- Get_Code_Unit_Entity --
3489 --------------------------
3491 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
3492 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
3494 begin
3495 if Ekind (Unit) = E_Package_Body then
3496 Unit := Spec_Entity (Unit);
3497 end if;
3499 return Unit;
3500 end Get_Code_Unit_Entity;
3502 ------------------------------
3503 -- Has_Excluded_Declaration --
3504 ------------------------------
3506 function Has_Excluded_Declaration
3507 (Subp : Entity_Id;
3508 Decls : List_Id) return Boolean
3510 D : Node_Id;
3512 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3513 -- Nested subprograms make a given body ineligible for inlining, but
3514 -- we make an exception for instantiations of unchecked conversion.
3515 -- The body has not been analyzed yet, so check the name, and verify
3516 -- that the visible entity with that name is the predefined unit.
3518 -----------------------------
3519 -- Is_Unchecked_Conversion --
3520 -----------------------------
3522 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3523 Id : constant Node_Id := Name (D);
3524 Conv : Entity_Id;
3526 begin
3527 if Nkind (Id) = N_Identifier
3528 and then Chars (Id) = Name_Unchecked_Conversion
3529 then
3530 Conv := Current_Entity (Id);
3532 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3533 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3534 then
3535 Conv := Current_Entity (Selector_Name (Id));
3536 else
3537 return False;
3538 end if;
3540 return Present (Conv)
3541 and then Is_Predefined_File_Name
3542 (Unit_File_Name (Get_Source_Unit (Conv)))
3543 and then Is_Intrinsic_Subprogram (Conv);
3544 end Is_Unchecked_Conversion;
3546 -- Start of processing for Has_Excluded_Declaration
3548 begin
3549 -- No action needed if the check is not needed
3551 if not Check_Inlining_Restrictions then
3552 return False;
3553 end if;
3555 D := First (Decls);
3556 while Present (D) loop
3558 -- First declarations universally excluded
3560 if Nkind (D) = N_Package_Declaration then
3561 Cannot_Inline
3562 ("cannot inline & (nested package declaration)?", D, Subp);
3563 return True;
3565 elsif Nkind (D) = N_Package_Instantiation then
3566 Cannot_Inline
3567 ("cannot inline & (nested package instantiation)?", D, Subp);
3568 return True;
3569 end if;
3571 -- Then declarations excluded only for front end inlining
3573 if Back_End_Inlining then
3574 null;
3576 elsif Nkind (D) = N_Task_Type_Declaration
3577 or else Nkind (D) = N_Single_Task_Declaration
3578 then
3579 Cannot_Inline
3580 ("cannot inline & (nested task type declaration)?", D, Subp);
3581 return True;
3583 elsif Nkind (D) = N_Protected_Type_Declaration
3584 or else Nkind (D) = N_Single_Protected_Declaration
3585 then
3586 Cannot_Inline
3587 ("cannot inline & (nested protected type declaration)?",
3588 D, Subp);
3589 return True;
3591 elsif Nkind (D) = N_Subprogram_Body then
3592 Cannot_Inline
3593 ("cannot inline & (nested subprogram)?", D, Subp);
3594 return True;
3596 elsif Nkind (D) = N_Function_Instantiation
3597 and then not Is_Unchecked_Conversion (D)
3598 then
3599 Cannot_Inline
3600 ("cannot inline & (nested function instantiation)?", D, Subp);
3601 return True;
3603 elsif Nkind (D) = N_Procedure_Instantiation then
3604 Cannot_Inline
3605 ("cannot inline & (nested procedure instantiation)?", D, Subp);
3606 return True;
3608 -- Subtype declarations with predicates will generate predicate
3609 -- functions, i.e. nested subprogram bodies, so inlining is not
3610 -- possible.
3612 elsif Nkind (D) = N_Subtype_Declaration
3613 and then Present (Aspect_Specifications (D))
3614 then
3615 declare
3616 A : Node_Id;
3617 A_Id : Aspect_Id;
3619 begin
3620 A := First (Aspect_Specifications (D));
3621 while Present (A) loop
3622 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3624 if A_Id = Aspect_Predicate
3625 or else A_Id = Aspect_Static_Predicate
3626 or else A_Id = Aspect_Dynamic_Predicate
3627 then
3628 Cannot_Inline
3629 ("cannot inline & (subtype declaration with "
3630 & "predicate)?", D, Subp);
3631 return True;
3632 end if;
3634 Next (A);
3635 end loop;
3636 end;
3637 end if;
3639 Next (D);
3640 end loop;
3642 return False;
3643 end Has_Excluded_Declaration;
3645 ----------------------------
3646 -- Has_Excluded_Statement --
3647 ----------------------------
3649 function Has_Excluded_Statement
3650 (Subp : Entity_Id;
3651 Stats : List_Id) return Boolean
3653 S : Node_Id;
3654 E : Node_Id;
3656 begin
3657 -- No action needed if the check is not needed
3659 if not Check_Inlining_Restrictions then
3660 return False;
3661 end if;
3663 S := First (Stats);
3664 while Present (S) loop
3665 if Nkind_In (S, N_Abort_Statement,
3666 N_Asynchronous_Select,
3667 N_Conditional_Entry_Call,
3668 N_Delay_Relative_Statement,
3669 N_Delay_Until_Statement,
3670 N_Selective_Accept,
3671 N_Timed_Entry_Call)
3672 then
3673 Cannot_Inline
3674 ("cannot inline & (non-allowed statement)?", S, Subp);
3675 return True;
3677 elsif Nkind (S) = N_Block_Statement then
3678 if Present (Declarations (S))
3679 and then Has_Excluded_Declaration (Subp, Declarations (S))
3680 then
3681 return True;
3683 elsif Present (Handled_Statement_Sequence (S)) then
3684 if not Back_End_Inlining
3685 and then
3686 Present
3687 (Exception_Handlers (Handled_Statement_Sequence (S)))
3688 then
3689 Cannot_Inline
3690 ("cannot inline& (exception handler)?",
3691 First (Exception_Handlers
3692 (Handled_Statement_Sequence (S))),
3693 Subp);
3694 return True;
3696 elsif Has_Excluded_Statement
3697 (Subp, Statements (Handled_Statement_Sequence (S)))
3698 then
3699 return True;
3700 end if;
3701 end if;
3703 elsif Nkind (S) = N_Case_Statement then
3704 E := First (Alternatives (S));
3705 while Present (E) loop
3706 if Has_Excluded_Statement (Subp, Statements (E)) then
3707 return True;
3708 end if;
3710 Next (E);
3711 end loop;
3713 elsif Nkind (S) = N_If_Statement then
3714 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
3715 return True;
3716 end if;
3718 if Present (Elsif_Parts (S)) then
3719 E := First (Elsif_Parts (S));
3720 while Present (E) loop
3721 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
3722 return True;
3723 end if;
3725 Next (E);
3726 end loop;
3727 end if;
3729 if Present (Else_Statements (S))
3730 and then Has_Excluded_Statement (Subp, Else_Statements (S))
3731 then
3732 return True;
3733 end if;
3735 elsif Nkind (S) = N_Loop_Statement
3736 and then Has_Excluded_Statement (Subp, Statements (S))
3737 then
3738 return True;
3740 elsif Nkind (S) = N_Extended_Return_Statement then
3741 if Present (Handled_Statement_Sequence (S))
3742 and then
3743 Has_Excluded_Statement
3744 (Subp, Statements (Handled_Statement_Sequence (S)))
3745 then
3746 return True;
3748 elsif not Back_End_Inlining
3749 and then Present (Handled_Statement_Sequence (S))
3750 and then
3751 Present (Exception_Handlers
3752 (Handled_Statement_Sequence (S)))
3753 then
3754 Cannot_Inline
3755 ("cannot inline& (exception handler)?",
3756 First (Exception_Handlers (Handled_Statement_Sequence (S))),
3757 Subp);
3758 return True;
3759 end if;
3760 end if;
3762 Next (S);
3763 end loop;
3765 return False;
3766 end Has_Excluded_Statement;
3768 --------------------------
3769 -- Has_Initialized_Type --
3770 --------------------------
3772 function Has_Initialized_Type (E : Entity_Id) return Boolean is
3773 E_Body : constant Node_Id := Subprogram_Body (E);
3774 Decl : Node_Id;
3776 begin
3777 if No (E_Body) then -- imported subprogram
3778 return False;
3780 else
3781 Decl := First (Declarations (E_Body));
3782 while Present (Decl) loop
3783 if Nkind (Decl) = N_Full_Type_Declaration
3784 and then Present (Init_Proc (Defining_Identifier (Decl)))
3785 then
3786 return True;
3787 end if;
3789 Next (Decl);
3790 end loop;
3791 end if;
3793 return False;
3794 end Has_Initialized_Type;
3796 -----------------------
3797 -- Has_Single_Return --
3798 -----------------------
3800 function Has_Single_Return (N : Node_Id) return Boolean is
3801 Return_Statement : Node_Id := Empty;
3803 function Check_Return (N : Node_Id) return Traverse_Result;
3805 ------------------
3806 -- Check_Return --
3807 ------------------
3809 function Check_Return (N : Node_Id) return Traverse_Result is
3810 begin
3811 if Nkind (N) = N_Simple_Return_Statement then
3812 if Present (Expression (N))
3813 and then Is_Entity_Name (Expression (N))
3814 then
3815 if No (Return_Statement) then
3816 Return_Statement := N;
3817 return OK;
3819 elsif Chars (Expression (N)) =
3820 Chars (Expression (Return_Statement))
3821 then
3822 return OK;
3824 else
3825 return Abandon;
3826 end if;
3828 -- A return statement within an extended return is a noop
3829 -- after inlining.
3831 elsif No (Expression (N))
3832 and then
3833 Nkind (Parent (Parent (N))) = N_Extended_Return_Statement
3834 then
3835 return OK;
3837 else
3838 -- Expression has wrong form
3840 return Abandon;
3841 end if;
3843 -- We can only inline a build-in-place function if it has a single
3844 -- extended return.
3846 elsif Nkind (N) = N_Extended_Return_Statement then
3847 if No (Return_Statement) then
3848 Return_Statement := N;
3849 return OK;
3851 else
3852 return Abandon;
3853 end if;
3855 else
3856 return OK;
3857 end if;
3858 end Check_Return;
3860 function Check_All_Returns is new Traverse_Func (Check_Return);
3862 -- Start of processing for Has_Single_Return
3864 begin
3865 if Check_All_Returns (N) /= OK then
3866 return False;
3868 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3869 return True;
3871 else
3872 return Present (Declarations (N))
3873 and then Present (First (Declarations (N)))
3874 and then Chars (Expression (Return_Statement)) =
3875 Chars (Defining_Identifier (First (Declarations (N))));
3876 end if;
3877 end Has_Single_Return;
3879 -----------------------------
3880 -- In_Main_Unit_Or_Subunit --
3881 -----------------------------
3883 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
3884 Comp : Node_Id := Cunit (Get_Code_Unit (E));
3886 begin
3887 -- Check whether the subprogram or package to inline is within the main
3888 -- unit or its spec or within a subunit. In either case there are no
3889 -- additional bodies to process. If the subprogram appears in a parent
3890 -- of the current unit, the check on whether inlining is possible is
3891 -- done in Analyze_Inlined_Bodies.
3893 while Nkind (Unit (Comp)) = N_Subunit loop
3894 Comp := Library_Unit (Comp);
3895 end loop;
3897 return Comp = Cunit (Main_Unit)
3898 or else Comp = Library_Unit (Cunit (Main_Unit));
3899 end In_Main_Unit_Or_Subunit;
3901 ----------------
3902 -- Initialize --
3903 ----------------
3905 procedure Initialize is
3906 begin
3907 Pending_Descriptor.Init;
3908 Pending_Instantiations.Init;
3909 Inlined_Bodies.Init;
3910 Successors.Init;
3911 Inlined.Init;
3913 for J in Hash_Headers'Range loop
3914 Hash_Headers (J) := No_Subp;
3915 end loop;
3917 Inlined_Calls := No_Elist;
3918 Backend_Calls := No_Elist;
3919 Backend_Inlined_Subps := No_Elist;
3920 Backend_Not_Inlined_Subps := No_Elist;
3921 end Initialize;
3923 ------------------------
3924 -- Instantiate_Bodies --
3925 ------------------------
3927 -- Generic bodies contain all the non-local references, so an
3928 -- instantiation does not need any more context than Standard
3929 -- itself, even if the instantiation appears in an inner scope.
3930 -- Generic associations have verified that the contract model is
3931 -- satisfied, so that any error that may occur in the analysis of
3932 -- the body is an internal error.
3934 procedure Instantiate_Bodies is
3935 J : Nat;
3936 Info : Pending_Body_Info;
3938 begin
3939 if Serious_Errors_Detected = 0 then
3940 Expander_Active := (Operating_Mode = Opt.Generate_Code);
3941 Push_Scope (Standard_Standard);
3942 To_Clean := New_Elmt_List;
3944 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3945 Start_Generic;
3946 end if;
3948 -- A body instantiation may generate additional instantiations, so
3949 -- the following loop must scan to the end of a possibly expanding
3950 -- set (that's why we can't simply use a FOR loop here).
3952 J := 0;
3953 while J <= Pending_Instantiations.Last
3954 and then Serious_Errors_Detected = 0
3955 loop
3956 Info := Pending_Instantiations.Table (J);
3958 -- If the instantiation node is absent, it has been removed
3959 -- as part of unreachable code.
3961 if No (Info.Inst_Node) then
3962 null;
3964 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
3965 Instantiate_Package_Body (Info);
3966 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
3968 else
3969 Instantiate_Subprogram_Body (Info);
3970 end if;
3972 J := J + 1;
3973 end loop;
3975 -- Reset the table of instantiations. Additional instantiations
3976 -- may be added through inlining, when additional bodies are
3977 -- analyzed.
3979 Pending_Instantiations.Init;
3981 -- We can now complete the cleanup actions of scopes that contain
3982 -- pending instantiations (skipped for generic units, since we
3983 -- never need any cleanups in generic units).
3985 if Expander_Active
3986 and then not Is_Generic_Unit (Main_Unit_Entity)
3987 then
3988 Cleanup_Scopes;
3989 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3990 End_Generic;
3991 end if;
3993 Pop_Scope;
3994 end if;
3995 end Instantiate_Bodies;
3997 ---------------
3998 -- Is_Nested --
3999 ---------------
4001 function Is_Nested (E : Entity_Id) return Boolean is
4002 Scop : Entity_Id;
4004 begin
4005 Scop := Scope (E);
4006 while Scop /= Standard_Standard loop
4007 if Ekind (Scop) in Subprogram_Kind then
4008 return True;
4010 elsif Ekind (Scop) = E_Task_Type
4011 or else Ekind (Scop) = E_Entry
4012 or else Ekind (Scop) = E_Entry_Family
4013 then
4014 return True;
4015 end if;
4017 Scop := Scope (Scop);
4018 end loop;
4020 return False;
4021 end Is_Nested;
4023 ------------------------
4024 -- List_Inlining_Info --
4025 ------------------------
4027 procedure List_Inlining_Info is
4028 Elmt : Elmt_Id;
4029 Nod : Node_Id;
4030 Count : Nat;
4032 begin
4033 if not Debug_Flag_Dot_J then
4034 return;
4035 end if;
4037 -- Generate listing of calls inlined by the frontend
4039 if Present (Inlined_Calls) then
4040 Count := 0;
4041 Elmt := First_Elmt (Inlined_Calls);
4042 while Present (Elmt) loop
4043 Nod := Node (Elmt);
4045 if In_Extended_Main_Code_Unit (Nod) then
4046 Count := Count + 1;
4048 if Count = 1 then
4049 Write_Str ("List of calls inlined by the frontend");
4050 Write_Eol;
4051 end if;
4053 Write_Str (" ");
4054 Write_Int (Count);
4055 Write_Str (":");
4056 Write_Location (Sloc (Nod));
4057 Write_Str (":");
4058 Output.Write_Eol;
4059 end if;
4061 Next_Elmt (Elmt);
4062 end loop;
4063 end if;
4065 -- Generate listing of calls passed to the backend
4067 if Present (Backend_Calls) then
4068 Count := 0;
4070 Elmt := First_Elmt (Backend_Calls);
4071 while Present (Elmt) loop
4072 Nod := Node (Elmt);
4074 if In_Extended_Main_Code_Unit (Nod) then
4075 Count := Count + 1;
4077 if Count = 1 then
4078 Write_Str ("List of inlined calls passed to the backend");
4079 Write_Eol;
4080 end if;
4082 Write_Str (" ");
4083 Write_Int (Count);
4084 Write_Str (":");
4085 Write_Location (Sloc (Nod));
4086 Output.Write_Eol;
4087 end if;
4089 Next_Elmt (Elmt);
4090 end loop;
4091 end if;
4093 -- Generate listing of subprograms passed to the backend
4095 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
4096 Count := 0;
4098 Elmt := First_Elmt (Backend_Inlined_Subps);
4099 while Present (Elmt) loop
4100 Nod := Node (Elmt);
4102 Count := Count + 1;
4104 if Count = 1 then
4105 Write_Str
4106 ("List of inlined subprograms passed to the backend");
4107 Write_Eol;
4108 end if;
4110 Write_Str (" ");
4111 Write_Int (Count);
4112 Write_Str (":");
4113 Write_Name (Chars (Nod));
4114 Write_Str (" (");
4115 Write_Location (Sloc (Nod));
4116 Write_Str (")");
4117 Output.Write_Eol;
4119 Next_Elmt (Elmt);
4120 end loop;
4121 end if;
4123 -- Generate listing of subprograms that cannot be inlined by the backend
4125 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
4126 Count := 0;
4128 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
4129 while Present (Elmt) loop
4130 Nod := Node (Elmt);
4132 Count := Count + 1;
4134 if Count = 1 then
4135 Write_Str
4136 ("List of subprograms that cannot be inlined by the backend");
4137 Write_Eol;
4138 end if;
4140 Write_Str (" ");
4141 Write_Int (Count);
4142 Write_Str (":");
4143 Write_Name (Chars (Nod));
4144 Write_Str (" (");
4145 Write_Location (Sloc (Nod));
4146 Write_Str (")");
4147 Output.Write_Eol;
4149 Next_Elmt (Elmt);
4150 end loop;
4151 end if;
4152 end List_Inlining_Info;
4154 ----------
4155 -- Lock --
4156 ----------
4158 procedure Lock is
4159 begin
4160 Pending_Instantiations.Locked := True;
4161 Inlined_Bodies.Locked := True;
4162 Successors.Locked := True;
4163 Inlined.Locked := True;
4164 Pending_Instantiations.Release;
4165 Inlined_Bodies.Release;
4166 Successors.Release;
4167 Inlined.Release;
4168 end Lock;
4170 --------------------------------
4171 -- Remove_Aspects_And_Pragmas --
4172 --------------------------------
4174 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
4175 procedure Remove_Items (List : List_Id);
4176 -- Remove all useless aspects/pragmas from a particular list
4178 ------------------
4179 -- Remove_Items --
4180 ------------------
4182 procedure Remove_Items (List : List_Id) is
4183 Item : Node_Id;
4184 Item_Id : Node_Id;
4185 Next_Item : Node_Id;
4187 begin
4188 -- Traverse the list looking for an aspect specification or a pragma
4190 Item := First (List);
4191 while Present (Item) loop
4192 Next_Item := Next (Item);
4194 if Nkind (Item) = N_Aspect_Specification then
4195 Item_Id := Identifier (Item);
4196 elsif Nkind (Item) = N_Pragma then
4197 Item_Id := Pragma_Identifier (Item);
4198 else
4199 Item_Id := Empty;
4200 end if;
4202 if Present (Item_Id)
4203 and then Nam_In (Chars (Item_Id), Name_Contract_Cases,
4204 Name_Global,
4205 Name_Depends,
4206 Name_Postcondition,
4207 Name_Precondition,
4208 Name_Refined_Global,
4209 Name_Refined_Depends,
4210 Name_Refined_Post,
4211 Name_Test_Case,
4212 Name_Unmodified,
4213 Name_Unreferenced,
4214 Name_Unused)
4215 then
4216 Remove (Item);
4217 end if;
4219 Item := Next_Item;
4220 end loop;
4221 end Remove_Items;
4223 -- Start of processing for Remove_Aspects_And_Pragmas
4225 begin
4226 Remove_Items (Aspect_Specifications (Body_Decl));
4227 Remove_Items (Declarations (Body_Decl));
4229 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
4230 -- in the body of the subprogram.
4232 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
4233 end Remove_Aspects_And_Pragmas;
4235 --------------------------
4236 -- Remove_Dead_Instance --
4237 --------------------------
4239 procedure Remove_Dead_Instance (N : Node_Id) is
4240 J : Int;
4242 begin
4243 J := 0;
4244 while J <= Pending_Instantiations.Last loop
4245 if Pending_Instantiations.Table (J).Inst_Node = N then
4246 Pending_Instantiations.Table (J).Inst_Node := Empty;
4247 return;
4248 end if;
4250 J := J + 1;
4251 end loop;
4252 end Remove_Dead_Instance;
4254 end Inline;