Add initial version of C++17 <memory_resource> header
[official-gcc.git] / gcc / ada / inline.adb
blob8f0b75d44a14f732182649ac47581a4684cd09e8
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-2018, 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
201 -- a local variable that is the first declaration in the body of the
202 -- function. In that case the call can be replaced by that local
203 -- variable as is done 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 Is_Non_Loading_Expression_Function
302 (Id : Entity_Id) return Boolean;
303 -- Determine whether arbitrary entity Id denotes a subprogram which is
304 -- either
306 -- * An expression function
308 -- * A function completed by an expression function where both the
309 -- spec and body are in the same context.
311 function Must_Inline return Inline_Level_Type;
312 -- Inlining is only done if the call statement N is in the main unit,
313 -- or within the body of another inlined subprogram.
315 ----------------------------------------
316 -- Is_Non_Loading_Expression_Function --
317 ----------------------------------------
319 function Is_Non_Loading_Expression_Function
320 (Id : Entity_Id) return Boolean
322 Body_Decl : Node_Id;
323 Body_Id : Entity_Id;
324 Spec_Decl : Node_Id;
326 begin
327 -- A stand-alone expression function is transformed into a spec-body
328 -- pair in-place. Since both the spec and body are in the same list,
329 -- the inlining of such an expression function does not need to load
330 -- anything extra.
332 if Is_Expression_Function (Id) then
333 return True;
335 -- A function may be completed by an expression function
337 elsif Ekind (Id) = E_Function then
338 Spec_Decl := Unit_Declaration_Node (Id);
340 if Nkind (Spec_Decl) = N_Subprogram_Declaration then
341 Body_Id := Corresponding_Body (Spec_Decl);
343 if Present (Body_Id) then
344 Body_Decl := Unit_Declaration_Node (Body_Id);
346 -- The inlining of a completing expression function does
347 -- not need to load anything extra when both the spec and
348 -- body are in the same context.
350 return
351 Was_Expression_Function (Body_Decl)
352 and then Parent (Spec_Decl) = Parent (Body_Decl);
353 end if;
354 end if;
355 end if;
357 return False;
358 end Is_Non_Loading_Expression_Function;
360 -----------------
361 -- Must_Inline --
362 -----------------
364 function Must_Inline return Inline_Level_Type is
365 Scop : Entity_Id;
366 Comp : Node_Id;
368 begin
369 -- Check if call is in main unit
371 Scop := Current_Scope;
373 -- Do not try to inline if scope is standard. This could happen, for
374 -- example, for a call to Add_Global_Declaration, and it causes
375 -- trouble to try to inline at this level.
377 if Scop = Standard_Standard then
378 return Dont_Inline;
379 end if;
381 -- Otherwise lookup scope stack to outer scope
383 while Scope (Scop) /= Standard_Standard
384 and then not Is_Child_Unit (Scop)
385 loop
386 Scop := Scope (Scop);
387 end loop;
389 Comp := Parent (Scop);
390 while Nkind (Comp) /= N_Compilation_Unit loop
391 Comp := Parent (Comp);
392 end loop;
394 -- If the call is in the main unit, inline the call and compile the
395 -- package of the subprogram to find more calls to be inlined.
397 if Comp = Cunit (Main_Unit)
398 or else Comp = Library_Unit (Cunit (Main_Unit))
399 then
400 Add_Call (E);
401 return Inline_Package;
402 end if;
404 -- The call is not in the main unit. See if it is in some subprogram
405 -- that can be inlined outside its unit. If so, inline the call and,
406 -- if the inlining level is set to 1, stop there; otherwise also
407 -- compile the package as above.
409 Scop := Current_Scope;
410 while Scope (Scop) /= Standard_Standard
411 and then not Is_Child_Unit (Scop)
412 loop
413 if Is_Overloadable (Scop)
414 and then Is_Inlined (Scop)
415 and then not Is_Nested (Scop)
416 then
417 Add_Call (E, Scop);
419 if Inline_Level = 1 then
420 return Inline_Call;
421 else
422 return Inline_Package;
423 end if;
424 end if;
426 Scop := Scope (Scop);
427 end loop;
429 return Dont_Inline;
430 end Must_Inline;
432 Level : Inline_Level_Type;
434 -- Start of processing for Add_Inlined_Body
436 begin
437 Append_New_Elmt (N, To => Backend_Calls);
439 -- Skip subprograms that cannot be inlined outside their unit
441 if Is_Abstract_Subprogram (E)
442 or else Convention (E) = Convention_Protected
443 or else Is_Nested (E)
444 then
445 return;
446 end if;
448 -- Find out whether the call must be inlined. Unless the result is
449 -- Dont_Inline, Must_Inline also creates an edge for the call in the
450 -- callgraph; however, it will not be activated until after Is_Called
451 -- is set on the subprogram.
453 Level := Must_Inline;
455 if Level = Dont_Inline then
456 return;
457 end if;
459 -- If the call was generated by the compiler and is to a subprogram in
460 -- a run-time unit, we need to suppress debugging information for it,
461 -- so that the code that is eventually inlined will not affect the
462 -- debugging of the program. We do not do it if the call comes from
463 -- source because, even if the call is inlined, the user may expect it
464 -- to be present in the debugging information.
466 if not Comes_From_Source (N)
467 and then In_Extended_Main_Source_Unit (N)
468 and then Is_Predefined_Unit (Get_Source_Unit (E))
469 then
470 Set_Needs_Debug_Info (E, False);
471 end if;
473 -- If the subprogram is an expression function, or is completed by one
474 -- where both the spec and body are in the same context, then there is
475 -- no need to load any package body since the body of the function is
476 -- in the spec.
478 if Is_Non_Loading_Expression_Function (E) then
479 Set_Is_Called (E);
480 return;
481 end if;
483 -- Find unit containing E, and add to list of inlined bodies if needed.
484 -- If the body is already present, no need to load any other unit. This
485 -- is the case for an initialization procedure, which appears in the
486 -- package declaration that contains the type. It is also the case if
487 -- the body has already been analyzed. Finally, if the unit enclosing
488 -- E is an instance, the instance body will be analyzed in any case,
489 -- and there is no need to add the enclosing unit (whose body might not
490 -- be available).
492 -- Library-level functions must be handled specially, because there is
493 -- no enclosing package to retrieve. In this case, it is the body of
494 -- the function that will have to be loaded.
496 declare
497 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
499 begin
500 if Pack = E then
501 Set_Is_Called (E);
502 Inlined_Bodies.Increment_Last;
503 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
505 elsif Ekind (Pack) = E_Package then
506 Set_Is_Called (E);
508 if Is_Generic_Instance (Pack) then
509 null;
511 -- Do not inline the package if the subprogram is an init proc
512 -- or other internally generated subprogram, because in that
513 -- case the subprogram body appears in the same unit that
514 -- declares the type, and that body is visible to the back end.
515 -- Do not inline it either if it is in the main unit.
516 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
517 -- calls if the back-end takes care of inlining the call.
518 -- Note that Level in Inline_Package | Inline_Call here.
520 elsif ((Level = Inline_Call
521 and then Has_Pragma_Inline_Always (E)
522 and then Back_End_Inlining)
523 or else Level = Inline_Package)
524 and then not Is_Inlined (Pack)
525 and then not Is_Internal (E)
526 and then not In_Main_Unit_Or_Subunit (Pack)
527 then
528 Set_Is_Inlined (Pack);
529 Inlined_Bodies.Increment_Last;
530 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
531 end if;
532 end if;
534 -- Ensure that Analyze_Inlined_Bodies will be invoked after
535 -- completing the analysis of the current unit.
537 Inline_Processing_Required := True;
538 end;
539 end Add_Inlined_Body;
541 ----------------------------
542 -- Add_Inlined_Subprogram --
543 ----------------------------
545 procedure Add_Inlined_Subprogram (E : Entity_Id) is
546 Decl : constant Node_Id := Parent (Declaration_Node (E));
547 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
549 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
550 -- Append Subp to the list of subprograms inlined by the backend
552 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
553 -- Append Subp to the list of subprograms that cannot be inlined by
554 -- the backend.
556 -----------------------------------------
557 -- Register_Backend_Inlined_Subprogram --
558 -----------------------------------------
560 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
561 begin
562 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
563 end Register_Backend_Inlined_Subprogram;
565 ---------------------------------------------
566 -- Register_Backend_Not_Inlined_Subprogram --
567 ---------------------------------------------
569 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
570 begin
571 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
572 end Register_Backend_Not_Inlined_Subprogram;
574 -- Start of processing for Add_Inlined_Subprogram
576 begin
577 -- If the subprogram is to be inlined, and if its unit is known to be
578 -- inlined or is an instance whose body will be analyzed anyway or the
579 -- subprogram was generated as a body by the compiler (for example an
580 -- initialization procedure) or its declaration was provided along with
581 -- the body (for example an expression function), and if it is declared
582 -- at the library level not in the main unit, and if it can be inlined
583 -- by the back-end, then insert it in the list of inlined subprograms.
585 if Is_Inlined (E)
586 and then (Is_Inlined (Pack)
587 or else Is_Generic_Instance (Pack)
588 or else Nkind (Decl) = N_Subprogram_Body
589 or else Present (Corresponding_Body (Decl)))
590 and then not In_Main_Unit_Or_Subunit (E)
591 and then not Is_Nested (E)
592 and then not Has_Initialized_Type (E)
593 then
594 Register_Backend_Inlined_Subprogram (E);
596 if No (Last_Inlined) then
597 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
598 else
599 Set_Next_Inlined_Subprogram (Last_Inlined, E);
600 end if;
602 Last_Inlined := E;
604 else
605 Register_Backend_Not_Inlined_Subprogram (E);
606 end if;
607 end Add_Inlined_Subprogram;
609 ------------------------
610 -- Add_Scope_To_Clean --
611 ------------------------
613 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
614 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
615 Elmt : Elmt_Id;
617 begin
618 -- If the instance appears in a library-level package declaration,
619 -- all finalization is global, and nothing needs doing here.
621 if Scop = Standard_Standard then
622 return;
623 end if;
625 -- If the instance is within a generic unit, no finalization code
626 -- can be generated. Note that at this point all bodies have been
627 -- analyzed, and the scope stack itself is not present, and the flag
628 -- Inside_A_Generic is not set.
630 declare
631 S : Entity_Id;
633 begin
634 S := Scope (Inst);
635 while Present (S) and then S /= Standard_Standard loop
636 if Is_Generic_Unit (S) then
637 return;
638 end if;
640 S := Scope (S);
641 end loop;
642 end;
644 Elmt := First_Elmt (To_Clean);
645 while Present (Elmt) loop
646 if Node (Elmt) = Scop then
647 return;
648 end if;
650 Elmt := Next_Elmt (Elmt);
651 end loop;
653 Append_Elmt (Scop, To_Clean);
654 end Add_Scope_To_Clean;
656 --------------
657 -- Add_Subp --
658 --------------
660 function Add_Subp (E : Entity_Id) return Subp_Index is
661 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
662 J : Subp_Index;
664 procedure New_Entry;
665 -- Initialize entry in Inlined table
667 procedure New_Entry is
668 begin
669 Inlined.Increment_Last;
670 Inlined.Table (Inlined.Last).Name := E;
671 Inlined.Table (Inlined.Last).Next := No_Subp;
672 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
673 Inlined.Table (Inlined.Last).Main_Call := False;
674 Inlined.Table (Inlined.Last).Processed := False;
675 end New_Entry;
677 -- Start of processing for Add_Subp
679 begin
680 if Hash_Headers (Index) = No_Subp then
681 New_Entry;
682 Hash_Headers (Index) := Inlined.Last;
683 return Inlined.Last;
685 else
686 J := Hash_Headers (Index);
687 while J /= No_Subp loop
688 if Inlined.Table (J).Name = E then
689 return J;
690 else
691 Index := J;
692 J := Inlined.Table (J).Next;
693 end if;
694 end loop;
696 -- On exit, subprogram was not found. Enter in table. Index is
697 -- the current last entry on the hash chain.
699 New_Entry;
700 Inlined.Table (Index).Next := Inlined.Last;
701 return Inlined.Last;
702 end if;
703 end Add_Subp;
705 ----------------------------
706 -- Analyze_Inlined_Bodies --
707 ----------------------------
709 procedure Analyze_Inlined_Bodies is
710 Comp_Unit : Node_Id;
711 J : Int;
712 Pack : Entity_Id;
713 Subp : Subp_Index;
714 S : Succ_Index;
716 type Pending_Index is new Nat;
718 package Pending_Inlined is new Table.Table (
719 Table_Component_Type => Subp_Index,
720 Table_Index_Type => Pending_Index,
721 Table_Low_Bound => 1,
722 Table_Initial => Alloc.Inlined_Initial,
723 Table_Increment => Alloc.Inlined_Increment,
724 Table_Name => "Pending_Inlined");
725 -- The workpile used to compute the transitive closure
727 -- Start of processing for Analyze_Inlined_Bodies
729 begin
730 if Serious_Errors_Detected = 0 then
731 Push_Scope (Standard_Standard);
733 J := 0;
734 while J <= Inlined_Bodies.Last
735 and then Serious_Errors_Detected = 0
736 loop
737 Pack := Inlined_Bodies.Table (J);
738 while Present (Pack)
739 and then Scope (Pack) /= Standard_Standard
740 and then not Is_Child_Unit (Pack)
741 loop
742 Pack := Scope (Pack);
743 end loop;
745 Comp_Unit := Parent (Pack);
746 while Present (Comp_Unit)
747 and then Nkind (Comp_Unit) /= N_Compilation_Unit
748 loop
749 Comp_Unit := Parent (Comp_Unit);
750 end loop;
752 -- Load the body if it exists and contains inlineable entities,
753 -- unless it is the main unit, or is an instance whose body has
754 -- already been analyzed.
756 if Present (Comp_Unit)
757 and then Comp_Unit /= Cunit (Main_Unit)
758 and then Body_Required (Comp_Unit)
759 and then
760 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
761 or else
762 (No (Corresponding_Body (Unit (Comp_Unit)))
763 and then Body_Needed_For_Inlining
764 (Defining_Entity (Unit (Comp_Unit)))))
765 then
766 declare
767 Bname : constant Unit_Name_Type :=
768 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
770 OK : Boolean;
772 begin
773 if not Is_Loaded (Bname) then
774 Style_Check := False;
775 Load_Needed_Body (Comp_Unit, OK);
777 if not OK then
779 -- Warn that a body was not available for inlining
780 -- by the back-end.
782 Error_Msg_Unit_1 := Bname;
783 Error_Msg_N
784 ("one or more inlined subprograms accessed in $!??",
785 Comp_Unit);
786 Error_Msg_File_1 :=
787 Get_File_Name (Bname, Subunit => False);
788 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
789 end if;
790 end if;
791 end;
792 end if;
794 J := J + 1;
796 if J > Inlined_Bodies.Last then
798 -- The analysis of required bodies may have produced additional
799 -- generic instantiations. To obtain further inlining, we need
800 -- to perform another round of generic body instantiations.
802 Instantiate_Bodies;
804 -- Symmetrically, the instantiation of required generic bodies
805 -- may have caused additional bodies to be inlined. To obtain
806 -- further inlining, we keep looping over the inlined bodies.
807 end if;
808 end loop;
810 -- The list of inlined subprograms is an overestimate, because it
811 -- includes inlined functions called from functions that are compiled
812 -- as part of an inlined package, but are not themselves called. An
813 -- accurate computation of just those subprograms that are needed
814 -- requires that we perform a transitive closure over the call graph,
815 -- starting from calls in the main compilation unit.
817 for Index in Inlined.First .. Inlined.Last loop
818 if not Is_Called (Inlined.Table (Index).Name) then
820 -- This means that Add_Inlined_Body added the subprogram to the
821 -- table but wasn't able to handle its code unit. Do nothing.
823 Inlined.Table (Index).Processed := True;
825 elsif Inlined.Table (Index).Main_Call then
826 Pending_Inlined.Increment_Last;
827 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
828 Inlined.Table (Index).Processed := True;
830 else
831 Set_Is_Called (Inlined.Table (Index).Name, False);
832 end if;
833 end loop;
835 -- Iterate over the workpile until it is emptied, propagating the
836 -- Is_Called flag to the successors of the processed subprogram.
838 while Pending_Inlined.Last >= Pending_Inlined.First loop
839 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
840 Pending_Inlined.Decrement_Last;
842 S := Inlined.Table (Subp).First_Succ;
844 while S /= No_Succ loop
845 Subp := Successors.Table (S).Subp;
847 if not Inlined.Table (Subp).Processed then
848 Set_Is_Called (Inlined.Table (Subp).Name);
849 Pending_Inlined.Increment_Last;
850 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
851 Inlined.Table (Subp).Processed := True;
852 end if;
854 S := Successors.Table (S).Next;
855 end loop;
856 end loop;
858 -- Finally add the called subprograms to the list of inlined
859 -- subprograms for the unit.
861 for Index in Inlined.First .. Inlined.Last loop
862 if Is_Called (Inlined.Table (Index).Name) then
863 Add_Inlined_Subprogram (Inlined.Table (Index).Name);
864 end if;
865 end loop;
867 Pop_Scope;
868 end if;
869 end Analyze_Inlined_Bodies;
871 --------------------------
872 -- Build_Body_To_Inline --
873 --------------------------
875 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
876 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
877 Analysis_Status : constant Boolean := Full_Analysis;
878 Original_Body : Node_Id;
879 Body_To_Analyze : Node_Id;
880 Max_Size : constant := 10;
882 function Has_Extended_Return return Boolean;
883 -- This function returns True if the subprogram has an extended return
884 -- statement.
886 function Has_Pending_Instantiation return Boolean;
887 -- If some enclosing body contains instantiations that appear before
888 -- the corresponding generic body, the enclosing body has a freeze node
889 -- so that it can be elaborated after the generic itself. This might
890 -- conflict with subsequent inlinings, so that it is unsafe to try to
891 -- inline in such a case.
893 function Has_Single_Return_In_GNATprove_Mode return Boolean;
894 -- This function is called only in GNATprove mode, and it returns
895 -- True if the subprogram has no return statement or a single return
896 -- statement as last statement. It returns False for subprogram with
897 -- a single return as last statement inside one or more blocks, as
898 -- inlining would generate gotos in that case as well (although the
899 -- goto is useless in that case).
901 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
902 -- If the body of the subprogram includes a call that returns an
903 -- unconstrained type, the secondary stack is involved, and it is
904 -- not worth inlining.
906 -------------------------
907 -- Has_Extended_Return --
908 -------------------------
910 function Has_Extended_Return return Boolean is
911 Body_To_Inline : constant Node_Id := N;
913 function Check_Return (N : Node_Id) return Traverse_Result;
914 -- Returns OK on node N if this is not an extended return statement
916 ------------------
917 -- Check_Return --
918 ------------------
920 function Check_Return (N : Node_Id) return Traverse_Result is
921 begin
922 case Nkind (N) is
923 when N_Extended_Return_Statement =>
924 return Abandon;
926 -- Skip locally declared subprogram bodies inside the body to
927 -- inline, as the return statements inside those do not count.
929 when N_Subprogram_Body =>
930 if N = Body_To_Inline then
931 return OK;
932 else
933 return Skip;
934 end if;
936 when others =>
937 return OK;
938 end case;
939 end Check_Return;
941 function Check_All_Returns is new Traverse_Func (Check_Return);
943 -- Start of processing for Has_Extended_Return
945 begin
946 return Check_All_Returns (N) /= OK;
947 end Has_Extended_Return;
949 -------------------------------
950 -- Has_Pending_Instantiation --
951 -------------------------------
953 function Has_Pending_Instantiation return Boolean is
954 S : Entity_Id;
956 begin
957 S := Current_Scope;
958 while Present (S) loop
959 if Is_Compilation_Unit (S)
960 or else Is_Child_Unit (S)
961 then
962 return False;
964 elsif Ekind (S) = E_Package
965 and then Has_Forward_Instantiation (S)
966 then
967 return True;
968 end if;
970 S := Scope (S);
971 end loop;
973 return False;
974 end Has_Pending_Instantiation;
976 -----------------------------------------
977 -- Has_Single_Return_In_GNATprove_Mode --
978 -----------------------------------------
980 function Has_Single_Return_In_GNATprove_Mode return Boolean is
981 Body_To_Inline : constant Node_Id := N;
982 Last_Statement : Node_Id := Empty;
984 function Check_Return (N : Node_Id) return Traverse_Result;
985 -- Returns OK on node N if this is not a return statement different
986 -- from the last statement in the subprogram.
988 ------------------
989 -- Check_Return --
990 ------------------
992 function Check_Return (N : Node_Id) return Traverse_Result is
993 begin
994 case Nkind (N) is
995 when N_Extended_Return_Statement
996 | N_Simple_Return_Statement
998 if N = Last_Statement then
999 return OK;
1000 else
1001 return Abandon;
1002 end if;
1004 -- Skip locally declared subprogram bodies inside the body to
1005 -- inline, as the return statements inside those do not count.
1007 when N_Subprogram_Body =>
1008 if N = Body_To_Inline then
1009 return OK;
1010 else
1011 return Skip;
1012 end if;
1014 when others =>
1015 return OK;
1016 end case;
1017 end Check_Return;
1019 function Check_All_Returns is new Traverse_Func (Check_Return);
1021 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1023 begin
1024 -- Retrieve the last statement
1026 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
1028 -- Check that the last statement is the only possible return
1029 -- statement in the subprogram.
1031 return Check_All_Returns (N) = OK;
1032 end Has_Single_Return_In_GNATprove_Mode;
1034 --------------------------
1035 -- Uses_Secondary_Stack --
1036 --------------------------
1038 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1039 function Check_Call (N : Node_Id) return Traverse_Result;
1040 -- Look for function calls that return an unconstrained type
1042 ----------------
1043 -- Check_Call --
1044 ----------------
1046 function Check_Call (N : Node_Id) return Traverse_Result is
1047 begin
1048 if Nkind (N) = N_Function_Call
1049 and then Is_Entity_Name (Name (N))
1050 and then Is_Composite_Type (Etype (Entity (Name (N))))
1051 and then not Is_Constrained (Etype (Entity (Name (N))))
1052 then
1053 Cannot_Inline
1054 ("cannot inline & (call returns unconstrained type)?",
1055 N, Spec_Id);
1056 return Abandon;
1057 else
1058 return OK;
1059 end if;
1060 end Check_Call;
1062 function Check_Calls is new Traverse_Func (Check_Call);
1064 begin
1065 return Check_Calls (Bod) = Abandon;
1066 end Uses_Secondary_Stack;
1068 -- Start of processing for Build_Body_To_Inline
1070 begin
1071 -- Return immediately if done already
1073 if Nkind (Decl) = N_Subprogram_Declaration
1074 and then Present (Body_To_Inline (Decl))
1075 then
1076 return;
1078 -- Subprograms that have return statements in the middle of the body are
1079 -- inlined with gotos. GNATprove does not currently support gotos, so
1080 -- we prevent such inlining.
1082 elsif GNATprove_Mode
1083 and then not Has_Single_Return_In_GNATprove_Mode
1084 then
1085 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1086 return;
1088 -- Functions that return controlled types cannot currently be inlined
1089 -- because they require secondary stack handling; controlled actions
1090 -- may also interfere in complex ways with inlining.
1092 elsif Ekind (Spec_Id) = E_Function
1093 and then Needs_Finalization (Etype (Spec_Id))
1094 then
1095 Cannot_Inline
1096 ("cannot inline & (controlled return type)?", N, Spec_Id);
1097 return;
1098 end if;
1100 if Present (Declarations (N))
1101 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
1102 then
1103 return;
1104 end if;
1106 if Present (Handled_Statement_Sequence (N)) then
1107 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1108 Cannot_Inline
1109 ("cannot inline& (exception handler)?",
1110 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1111 Spec_Id);
1112 return;
1114 elsif Has_Excluded_Statement
1115 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1116 then
1117 return;
1118 end if;
1119 end if;
1121 -- We do not inline a subprogram that is too large, unless it is marked
1122 -- Inline_Always or we are in GNATprove mode. This pragma does not
1123 -- suppress the other checks on inlining (forbidden declarations,
1124 -- handlers, etc).
1126 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1127 and then List_Length
1128 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1129 then
1130 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1131 return;
1132 end if;
1134 if Has_Pending_Instantiation then
1135 Cannot_Inline
1136 ("cannot inline& (forward instance within enclosing body)?",
1137 N, Spec_Id);
1138 return;
1139 end if;
1141 -- Within an instance, the body to inline must be treated as a nested
1142 -- generic, so that the proper global references are preserved.
1144 -- Note that we do not do this at the library level, because it is not
1145 -- needed, and furthermore this causes trouble if front-end inlining
1146 -- is activated (-gnatN).
1148 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1149 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1150 Original_Body := Copy_Generic_Node (N, Empty, Instantiating => True);
1151 else
1152 Original_Body := Copy_Separate_Tree (N);
1153 end if;
1155 -- We need to capture references to the formals in order to substitute
1156 -- the actuals at the point of inlining, i.e. instantiation. To treat
1157 -- the formals as globals to the body to inline, we nest it within a
1158 -- dummy parameterless subprogram, declared within the real one. To
1159 -- avoid generating an internal name (which is never public, and which
1160 -- affects serial numbers of other generated names), we use an internal
1161 -- symbol that cannot conflict with user declarations.
1163 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1164 Set_Defining_Unit_Name
1165 (Specification (Original_Body),
1166 Make_Defining_Identifier (Sloc (N), Name_uParent));
1167 Set_Corresponding_Spec (Original_Body, Empty);
1169 -- Remove all aspects/pragmas that have no meaning in an inlined body
1171 Remove_Aspects_And_Pragmas (Original_Body);
1173 Body_To_Analyze :=
1174 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1176 -- Set return type of function, which is also global and does not need
1177 -- to be resolved.
1179 if Ekind (Spec_Id) = E_Function then
1180 Set_Result_Definition
1181 (Specification (Body_To_Analyze),
1182 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1183 end if;
1185 if No (Declarations (N)) then
1186 Set_Declarations (N, New_List (Body_To_Analyze));
1187 else
1188 Append (Body_To_Analyze, Declarations (N));
1189 end if;
1191 -- The body to inline is preanalyzed. In GNATprove mode we must disable
1192 -- full analysis as well so that light expansion does not take place
1193 -- either, and name resolution is unaffected.
1195 Expander_Mode_Save_And_Set (False);
1196 Full_Analysis := False;
1198 Analyze (Body_To_Analyze);
1199 Push_Scope (Defining_Entity (Body_To_Analyze));
1200 Save_Global_References (Original_Body);
1201 End_Scope;
1202 Remove (Body_To_Analyze);
1204 Expander_Mode_Restore;
1205 Full_Analysis := Analysis_Status;
1207 -- Restore environment if previously saved
1209 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1210 Restore_Env;
1211 end if;
1213 -- Functions that return unconstrained composite types require
1214 -- secondary stack handling, and cannot currently be inlined, unless
1215 -- all return statements return a local variable that is the first
1216 -- local declaration in the body. We had to delay this check until
1217 -- the body of the function is analyzed since Has_Single_Return()
1218 -- requires a minimum decoration.
1220 if Ekind (Spec_Id) = E_Function
1221 and then not Is_Scalar_Type (Etype (Spec_Id))
1222 and then not Is_Access_Type (Etype (Spec_Id))
1223 and then not Is_Constrained (Etype (Spec_Id))
1224 then
1225 if not Has_Single_Return (Body_To_Analyze)
1227 -- Skip inlining if the function returns an unconstrained type
1228 -- using an extended return statement, since this part of the
1229 -- new inlining model is not yet supported by the current
1230 -- implementation. ???
1232 or else (Returns_Unconstrained_Type (Spec_Id)
1233 and then Has_Extended_Return)
1234 then
1235 Cannot_Inline
1236 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1237 return;
1238 end if;
1240 -- If secondary stack is used, there is no point in inlining. We have
1241 -- already issued the warning in this case, so nothing to do.
1243 elsif Uses_Secondary_Stack (Body_To_Analyze) then
1244 return;
1245 end if;
1247 Set_Body_To_Inline (Decl, Original_Body);
1248 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1249 Set_Is_Inlined (Spec_Id);
1250 end Build_Body_To_Inline;
1252 -------------------------------------------
1253 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1254 -------------------------------------------
1256 function Call_Can_Be_Inlined_In_GNATprove_Mode
1257 (N : Node_Id;
1258 Subp : Entity_Id) return Boolean
1260 F : Entity_Id;
1261 A : Node_Id;
1263 begin
1264 F := First_Formal (Subp);
1265 A := First_Actual (N);
1266 while Present (F) loop
1267 if Ekind (F) /= E_Out_Parameter
1268 and then not Same_Type (Etype (F), Etype (A))
1269 and then
1270 (Is_By_Reference_Type (Etype (A))
1271 or else Is_Limited_Type (Etype (A)))
1272 then
1273 return False;
1274 end if;
1276 Next_Formal (F);
1277 Next_Actual (A);
1278 end loop;
1280 return True;
1281 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1283 --------------------------------------
1284 -- Can_Be_Inlined_In_GNATprove_Mode --
1285 --------------------------------------
1287 function Can_Be_Inlined_In_GNATprove_Mode
1288 (Spec_Id : Entity_Id;
1289 Body_Id : Entity_Id) return Boolean
1291 function Has_Formal_With_Discriminant_Dependent_Fields
1292 (Id : Entity_Id) return Boolean;
1293 -- Returns true if the subprogram has at least one formal parameter of
1294 -- an unconstrained record type with per-object constraints on component
1295 -- types.
1297 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1298 -- Return True if subprogram Id has any contract. The presence of
1299 -- Extensions_Visible or Volatile_Function is also considered as a
1300 -- contract here.
1302 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1303 -- Return True if subprogram Id defines a compilation unit
1304 -- Shouldn't this be in Sem_Aux???
1306 function In_Package_Spec (Id : Entity_Id) return Boolean;
1307 -- Return True if subprogram Id is defined in the package specification,
1308 -- either its visible or private part.
1310 ---------------------------------------------------
1311 -- Has_Formal_With_Discriminant_Dependent_Fields --
1312 ---------------------------------------------------
1314 function Has_Formal_With_Discriminant_Dependent_Fields
1315 (Id : Entity_Id) return Boolean
1317 function Has_Discriminant_Dependent_Component
1318 (Typ : Entity_Id) return Boolean;
1319 -- Determine whether unconstrained record type Typ has at least one
1320 -- component that depends on a discriminant.
1322 ------------------------------------------
1323 -- Has_Discriminant_Dependent_Component --
1324 ------------------------------------------
1326 function Has_Discriminant_Dependent_Component
1327 (Typ : Entity_Id) return Boolean
1329 Comp : Entity_Id;
1331 begin
1332 -- Inspect all components of the record type looking for one that
1333 -- depends on a discriminant.
1335 Comp := First_Component (Typ);
1336 while Present (Comp) loop
1337 if Has_Discriminant_Dependent_Constraint (Comp) then
1338 return True;
1339 end if;
1341 Next_Component (Comp);
1342 end loop;
1344 return False;
1345 end Has_Discriminant_Dependent_Component;
1347 -- Local variables
1349 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1350 Formal : Entity_Id;
1351 Formal_Typ : Entity_Id;
1353 -- Start of processing for
1354 -- Has_Formal_With_Discriminant_Dependent_Fields
1356 begin
1357 -- Inspect all parameters of the subprogram looking for a formal
1358 -- of an unconstrained record type with at least one discriminant
1359 -- dependent component.
1361 Formal := First_Formal (Subp_Id);
1362 while Present (Formal) loop
1363 Formal_Typ := Etype (Formal);
1365 if Is_Record_Type (Formal_Typ)
1366 and then not Is_Constrained (Formal_Typ)
1367 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1368 then
1369 return True;
1370 end if;
1372 Next_Formal (Formal);
1373 end loop;
1375 return False;
1376 end Has_Formal_With_Discriminant_Dependent_Fields;
1378 -----------------------
1379 -- Has_Some_Contract --
1380 -----------------------
1382 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1383 Items : Node_Id;
1385 begin
1386 -- A call to an expression function may precede the actual body which
1387 -- is inserted at the end of the enclosing declarations. Ensure that
1388 -- the related entity is decorated before inspecting the contract.
1390 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1391 Items := Contract (Id);
1393 -- Note that Classifications is not Empty when Extensions_Visible
1394 -- or Volatile_Function is present, which causes such subprograms
1395 -- to be considered to have a contract here. This is fine as we
1396 -- want to avoid inlining these too.
1398 return Present (Items)
1399 and then (Present (Pre_Post_Conditions (Items)) or else
1400 Present (Contract_Test_Cases (Items)) or else
1401 Present (Classifications (Items)));
1402 end if;
1404 return False;
1405 end Has_Some_Contract;
1407 ---------------------
1408 -- In_Package_Spec --
1409 ---------------------
1411 function In_Package_Spec (Id : Entity_Id) return Boolean is
1412 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1413 -- Parent of the subprogram's declaration
1415 begin
1416 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1417 end In_Package_Spec;
1419 ------------------------
1420 -- Is_Unit_Subprogram --
1421 ------------------------
1423 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1424 Decl : Node_Id := Parent (Parent (Id));
1425 begin
1426 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1427 Decl := Parent (Decl);
1428 end if;
1430 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1431 end Is_Unit_Subprogram;
1433 -- Local declarations
1435 Id : Entity_Id;
1436 -- Procedure or function entity for the subprogram
1438 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1440 begin
1441 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1443 if Present (Spec_Id) then
1444 Id := Spec_Id;
1445 else
1446 Id := Body_Id;
1447 end if;
1449 -- Only local subprograms without contracts are inlined in GNATprove
1450 -- mode, as these are the subprograms which a user is not interested in
1451 -- analyzing in isolation, but rather in the context of their call. This
1452 -- is a convenient convention, that could be changed for an explicit
1453 -- pragma/aspect one day.
1455 -- In a number of special cases, inlining is not desirable or not
1456 -- possible, see below.
1458 -- Do not inline unit-level subprograms
1460 if Is_Unit_Subprogram (Id) then
1461 return False;
1463 -- Do not inline subprograms declared in package specs, because they are
1464 -- not local, i.e. can be called either from anywhere (if declared in
1465 -- visible part) or from the child units (if declared in private part).
1467 elsif In_Package_Spec (Id) then
1468 return False;
1470 -- Do not inline subprograms declared in other units. This is important
1471 -- in particular for subprograms defined in the private part of a
1472 -- package spec, when analyzing one of its child packages, as otherwise
1473 -- we issue spurious messages about the impossibility to inline such
1474 -- calls.
1476 elsif not In_Extended_Main_Code_Unit (Id) then
1477 return False;
1479 -- Do not inline subprograms marked No_Return, possibly used for
1480 -- signaling errors, which GNATprove handles specially.
1482 elsif No_Return (Id) then
1483 return False;
1485 -- Do not inline subprograms that have a contract on the spec or the
1486 -- body. Use the contract(s) instead in GNATprove. This also prevents
1487 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1489 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1490 or else
1491 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1492 then
1493 return False;
1495 -- Do not inline expression functions, which are directly inlined at the
1496 -- prover level.
1498 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1499 or else
1500 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1501 then
1502 return False;
1504 -- Do not inline generic subprogram instances. The visibility rules of
1505 -- generic instances plays badly with inlining.
1507 elsif Is_Generic_Instance (Spec_Id) then
1508 return False;
1510 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1511 -- the subprogram body, a similar check is performed after the body
1512 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1514 elsif Present (Spec_Id)
1515 and then
1516 (No (SPARK_Pragma (Spec_Id))
1517 or else
1518 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1519 then
1520 return False;
1522 -- Subprograms in generic instances are currently not inlined, to avoid
1523 -- problems with inlining of standard library subprograms.
1525 elsif Instantiation_Location (Sloc (Id)) /= No_Location then
1526 return False;
1528 -- Do not inline subprograms and entries defined inside protected types,
1529 -- which typically are not helper subprograms, which also avoids getting
1530 -- spurious messages on calls that cannot be inlined.
1532 elsif Within_Protected_Type (Id) then
1533 return False;
1535 -- Do not inline predicate functions (treated specially by GNATprove)
1537 elsif Is_Predicate_Function (Id) then
1538 return False;
1540 -- Do not inline subprograms with a parameter of an unconstrained
1541 -- record type if it has discrimiant dependent fields. Indeed, with
1542 -- such parameters, the frontend cannot always ensure type compliance
1543 -- in record component accesses (in particular with records containing
1544 -- packed arrays).
1546 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1547 return False;
1549 -- Otherwise, this is a subprogram declared inside the private part of a
1550 -- package, or inside a package body, or locally in a subprogram, and it
1551 -- does not have any contract. Inline it.
1553 else
1554 return True;
1555 end if;
1556 end Can_Be_Inlined_In_GNATprove_Mode;
1558 -------------------
1559 -- Cannot_Inline --
1560 -------------------
1562 procedure Cannot_Inline
1563 (Msg : String;
1564 N : Node_Id;
1565 Subp : Entity_Id;
1566 Is_Serious : Boolean := False)
1568 begin
1569 -- In GNATprove mode, inlining is the technical means by which the
1570 -- higher-level goal of contextual analysis is reached, so issue
1571 -- messages about failure to apply contextual analysis to a
1572 -- subprogram, rather than failure to inline it.
1574 if GNATprove_Mode
1575 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1576 then
1577 declare
1578 Len1 : constant Positive :=
1579 String (String'("cannot inline"))'Length;
1580 Len2 : constant Positive :=
1581 String (String'("info: no contextual analysis of"))'Length;
1583 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1585 begin
1586 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1587 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1588 Msg (Msg'First + Len1 .. Msg'Last);
1589 Cannot_Inline (New_Msg, N, Subp, Is_Serious);
1590 return;
1591 end;
1592 end if;
1594 pragma Assert (Msg (Msg'Last) = '?');
1596 -- Legacy front-end inlining model
1598 if not Back_End_Inlining then
1600 -- Do not emit warning if this is a predefined unit which is not
1601 -- the main unit. With validity checks enabled, some predefined
1602 -- subprograms may contain nested subprograms and become ineligible
1603 -- for inlining.
1605 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1606 and then not In_Extended_Main_Source_Unit (Subp)
1607 then
1608 null;
1610 -- In GNATprove mode, issue a warning, and indicate that the
1611 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1612 -- to False.
1614 elsif GNATprove_Mode then
1615 Set_Is_Inlined_Always (Subp, False);
1616 Error_Msg_NE (Msg & "p?", N, Subp);
1618 elsif Has_Pragma_Inline_Always (Subp) then
1620 -- Remove last character (question mark) to make this into an
1621 -- error, because the Inline_Always pragma cannot be obeyed.
1623 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1625 elsif Ineffective_Inline_Warnings then
1626 Error_Msg_NE (Msg & "p?", N, Subp);
1627 end if;
1629 -- New semantics relying on back-end inlining
1631 elsif Is_Serious then
1633 -- Remove last character (question mark) to make this into an error.
1635 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1637 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1638 -- is not always inlined by setting flag Is_Inlined_Always to False.
1640 elsif GNATprove_Mode then
1641 Set_Is_Inlined_Always (Subp, False);
1642 Error_Msg_NE (Msg & "p?", N, Subp);
1644 else
1646 -- Do not emit warning if this is a predefined unit which is not
1647 -- the main unit. This behavior is currently provided for backward
1648 -- compatibility but it will be removed when we enforce the
1649 -- strictness of the new rules.
1651 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1652 and then not In_Extended_Main_Source_Unit (Subp)
1653 then
1654 null;
1656 elsif Has_Pragma_Inline_Always (Subp) then
1658 -- Emit a warning if this is a call to a runtime subprogram
1659 -- which is located inside a generic. Previously this call
1660 -- was silently skipped.
1662 if Is_Generic_Instance (Subp) then
1663 declare
1664 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
1665 begin
1666 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
1667 Set_Is_Inlined (Subp, False);
1668 Error_Msg_NE (Msg & "p?", N, Subp);
1669 return;
1670 end if;
1671 end;
1672 end if;
1674 -- Remove last character (question mark) to make this into an
1675 -- error, because the Inline_Always pragma cannot be obeyed.
1677 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1679 else
1680 Set_Is_Inlined (Subp, False);
1682 if Ineffective_Inline_Warnings then
1683 Error_Msg_NE (Msg & "p?", N, Subp);
1684 end if;
1685 end if;
1686 end if;
1687 end Cannot_Inline;
1689 --------------------------------------------
1690 -- Check_And_Split_Unconstrained_Function --
1691 --------------------------------------------
1693 procedure Check_And_Split_Unconstrained_Function
1694 (N : Node_Id;
1695 Spec_Id : Entity_Id;
1696 Body_Id : Entity_Id)
1698 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
1699 -- Use generic machinery to build an unexpanded body for the subprogram.
1700 -- This body is subsequently used for inline expansions at call sites.
1702 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
1703 -- Return true if we generate code for the function body N, the function
1704 -- body N has no local declarations and its unique statement is a single
1705 -- extended return statement with a handled statements sequence.
1707 procedure Split_Unconstrained_Function
1708 (N : Node_Id;
1709 Spec_Id : Entity_Id);
1710 -- N is an inlined function body that returns an unconstrained type and
1711 -- has a single extended return statement. Split N in two subprograms:
1712 -- a procedure P' and a function F'. The formals of P' duplicate the
1713 -- formals of N plus an extra formal which is used to return a value;
1714 -- its body is composed by the declarations and list of statements
1715 -- of the extended return statement of N.
1717 --------------------------
1718 -- Build_Body_To_Inline --
1719 --------------------------
1721 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1722 procedure Generate_Subprogram_Body
1723 (N : Node_Id;
1724 Body_To_Inline : out Node_Id);
1725 -- Generate a parameterless duplicate of subprogram body N. Note that
1726 -- occurrences of pragmas referencing the formals are removed since
1727 -- they have no meaning when the body is inlined and the formals are
1728 -- rewritten (the analysis of the non-inlined body will handle these
1729 -- pragmas). A new internal name is associated with Body_To_Inline.
1731 ------------------------------
1732 -- Generate_Subprogram_Body --
1733 ------------------------------
1735 procedure Generate_Subprogram_Body
1736 (N : Node_Id;
1737 Body_To_Inline : out Node_Id)
1739 begin
1740 -- Within an instance, the body to inline must be treated as a
1741 -- nested generic so that proper global references are preserved.
1743 -- Note that we do not do this at the library level, because it
1744 -- is not needed, and furthermore this causes trouble if front
1745 -- end inlining is activated (-gnatN).
1747 if In_Instance
1748 and then Scope (Current_Scope) /= Standard_Standard
1749 then
1750 Body_To_Inline :=
1751 Copy_Generic_Node (N, Empty, Instantiating => True);
1752 else
1753 Body_To_Inline := Copy_Separate_Tree (N);
1754 end if;
1756 -- Remove aspects/pragmas that have no meaning in an inlined body
1758 Remove_Aspects_And_Pragmas (Body_To_Inline);
1760 -- We need to capture references to the formals in order
1761 -- to substitute the actuals at the point of inlining, i.e.
1762 -- instantiation. To treat the formals as globals to the body to
1763 -- inline, we nest it within a dummy parameterless subprogram,
1764 -- declared within the real one.
1766 Set_Parameter_Specifications
1767 (Specification (Body_To_Inline), No_List);
1769 -- A new internal name is associated with Body_To_Inline to avoid
1770 -- conflicts when the non-inlined body N is analyzed.
1772 Set_Defining_Unit_Name (Specification (Body_To_Inline),
1773 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
1774 Set_Corresponding_Spec (Body_To_Inline, Empty);
1775 end Generate_Subprogram_Body;
1777 -- Local variables
1779 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1780 Original_Body : Node_Id;
1781 Body_To_Analyze : Node_Id;
1783 begin
1784 pragma Assert (Current_Scope = Spec_Id);
1786 -- Within an instance, the body to inline must be treated as a nested
1787 -- generic, so that the proper global references are preserved. We
1788 -- do not do this at the library level, because it is not needed, and
1789 -- furthermore this causes trouble if front-end inlining is activated
1790 -- (-gnatN).
1792 if In_Instance
1793 and then Scope (Current_Scope) /= Standard_Standard
1794 then
1795 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1796 end if;
1798 -- Capture references to formals in order to substitute the actuals
1799 -- at the point of inlining or instantiation. To treat the formals
1800 -- as globals to the body to inline, nest the body within a dummy
1801 -- parameterless subprogram, declared within the real one.
1803 Generate_Subprogram_Body (N, Original_Body);
1804 Body_To_Analyze :=
1805 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1807 -- Set return type of function, which is also global and does not
1808 -- need to be resolved.
1810 if Ekind (Spec_Id) = E_Function then
1811 Set_Result_Definition (Specification (Body_To_Analyze),
1812 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1813 end if;
1815 if No (Declarations (N)) then
1816 Set_Declarations (N, New_List (Body_To_Analyze));
1817 else
1818 Append_To (Declarations (N), Body_To_Analyze);
1819 end if;
1821 Preanalyze (Body_To_Analyze);
1823 Push_Scope (Defining_Entity (Body_To_Analyze));
1824 Save_Global_References (Original_Body);
1825 End_Scope;
1826 Remove (Body_To_Analyze);
1828 -- Restore environment if previously saved
1830 if In_Instance
1831 and then Scope (Current_Scope) /= Standard_Standard
1832 then
1833 Restore_Env;
1834 end if;
1836 pragma Assert (No (Body_To_Inline (Decl)));
1837 Set_Body_To_Inline (Decl, Original_Body);
1838 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1839 end Build_Body_To_Inline;
1841 --------------------------------------
1842 -- Can_Split_Unconstrained_Function --
1843 --------------------------------------
1845 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
1846 Ret_Node : constant Node_Id :=
1847 First (Statements (Handled_Statement_Sequence (N)));
1848 D : Node_Id;
1850 begin
1851 -- No user defined declarations allowed in the function except inside
1852 -- the unique return statement; implicit labels are the only allowed
1853 -- declarations.
1855 if not Is_Empty_List (Declarations (N)) then
1856 D := First (Declarations (N));
1857 while Present (D) loop
1858 if Nkind (D) /= N_Implicit_Label_Declaration then
1859 return False;
1860 end if;
1862 Next (D);
1863 end loop;
1864 end if;
1866 -- We only split the inlined function when we are generating the code
1867 -- of its body; otherwise we leave duplicated split subprograms in
1868 -- the tree which (if referenced) generate wrong references at link
1869 -- time.
1871 return In_Extended_Main_Code_Unit (N)
1872 and then Present (Ret_Node)
1873 and then Nkind (Ret_Node) = N_Extended_Return_Statement
1874 and then No (Next (Ret_Node))
1875 and then Present (Handled_Statement_Sequence (Ret_Node));
1876 end Can_Split_Unconstrained_Function;
1878 ----------------------------------
1879 -- Split_Unconstrained_Function --
1880 ----------------------------------
1882 procedure Split_Unconstrained_Function
1883 (N : Node_Id;
1884 Spec_Id : Entity_Id)
1886 Loc : constant Source_Ptr := Sloc (N);
1887 Ret_Node : constant Node_Id :=
1888 First (Statements (Handled_Statement_Sequence (N)));
1889 Ret_Obj : constant Node_Id :=
1890 First (Return_Object_Declarations (Ret_Node));
1892 procedure Build_Procedure
1893 (Proc_Id : out Entity_Id;
1894 Decl_List : out List_Id);
1895 -- Build a procedure containing the statements found in the extended
1896 -- return statement of the unconstrained function body N.
1898 ---------------------
1899 -- Build_Procedure --
1900 ---------------------
1902 procedure Build_Procedure
1903 (Proc_Id : out Entity_Id;
1904 Decl_List : out List_Id)
1906 Formal : Entity_Id;
1907 Formal_List : constant List_Id := New_List;
1908 Proc_Spec : Node_Id;
1909 Proc_Body : Node_Id;
1910 Subp_Name : constant Name_Id := New_Internal_Name ('F');
1911 Body_Decl_List : List_Id := No_List;
1912 Param_Type : Node_Id;
1914 begin
1915 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
1916 Param_Type :=
1917 New_Copy (Object_Definition (Ret_Obj));
1918 else
1919 Param_Type :=
1920 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
1921 end if;
1923 Append_To (Formal_List,
1924 Make_Parameter_Specification (Loc,
1925 Defining_Identifier =>
1926 Make_Defining_Identifier (Loc,
1927 Chars => Chars (Defining_Identifier (Ret_Obj))),
1928 In_Present => False,
1929 Out_Present => True,
1930 Null_Exclusion_Present => False,
1931 Parameter_Type => Param_Type));
1933 Formal := First_Formal (Spec_Id);
1935 -- Note that we copy the parameter type rather than creating
1936 -- a reference to it, because it may be a class-wide entity
1937 -- that will not be retrieved by name.
1939 while Present (Formal) loop
1940 Append_To (Formal_List,
1941 Make_Parameter_Specification (Loc,
1942 Defining_Identifier =>
1943 Make_Defining_Identifier (Sloc (Formal),
1944 Chars => Chars (Formal)),
1945 In_Present => In_Present (Parent (Formal)),
1946 Out_Present => Out_Present (Parent (Formal)),
1947 Null_Exclusion_Present =>
1948 Null_Exclusion_Present (Parent (Formal)),
1949 Parameter_Type =>
1950 New_Copy_Tree (Parameter_Type (Parent (Formal))),
1951 Expression =>
1952 Copy_Separate_Tree (Expression (Parent (Formal)))));
1954 Next_Formal (Formal);
1955 end loop;
1957 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
1959 Proc_Spec :=
1960 Make_Procedure_Specification (Loc,
1961 Defining_Unit_Name => Proc_Id,
1962 Parameter_Specifications => Formal_List);
1964 Decl_List := New_List;
1966 Append_To (Decl_List,
1967 Make_Subprogram_Declaration (Loc, Proc_Spec));
1969 -- Can_Convert_Unconstrained_Function checked that the function
1970 -- has no local declarations except implicit label declarations.
1971 -- Copy these declarations to the built procedure.
1973 if Present (Declarations (N)) then
1974 Body_Decl_List := New_List;
1976 declare
1977 D : Node_Id;
1978 New_D : Node_Id;
1980 begin
1981 D := First (Declarations (N));
1982 while Present (D) loop
1983 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
1985 New_D :=
1986 Make_Implicit_Label_Declaration (Loc,
1987 Make_Defining_Identifier (Loc,
1988 Chars => Chars (Defining_Identifier (D))),
1989 Label_Construct => Empty);
1990 Append_To (Body_Decl_List, New_D);
1992 Next (D);
1993 end loop;
1994 end;
1995 end if;
1997 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
1999 Proc_Body :=
2000 Make_Subprogram_Body (Loc,
2001 Specification => Copy_Separate_Tree (Proc_Spec),
2002 Declarations => Body_Decl_List,
2003 Handled_Statement_Sequence =>
2004 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
2006 Set_Defining_Unit_Name (Specification (Proc_Body),
2007 Make_Defining_Identifier (Loc, Subp_Name));
2009 Append_To (Decl_List, Proc_Body);
2010 end Build_Procedure;
2012 -- Local variables
2014 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
2015 Blk_Stmt : Node_Id;
2016 Proc_Id : Entity_Id;
2017 Proc_Call : Node_Id;
2019 -- Start of processing for Split_Unconstrained_Function
2021 begin
2022 -- Build the associated procedure, analyze it and insert it before
2023 -- the function body N.
2025 declare
2026 Scope : constant Entity_Id := Current_Scope;
2027 Decl_List : List_Id;
2028 begin
2029 Pop_Scope;
2030 Build_Procedure (Proc_Id, Decl_List);
2031 Insert_Actions (N, Decl_List);
2032 Set_Is_Inlined (Proc_Id);
2033 Push_Scope (Scope);
2034 end;
2036 -- Build the call to the generated procedure
2038 declare
2039 Actual_List : constant List_Id := New_List;
2040 Formal : Entity_Id;
2042 begin
2043 Append_To (Actual_List,
2044 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
2046 Formal := First_Formal (Spec_Id);
2047 while Present (Formal) loop
2048 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
2050 -- Avoid spurious warning on unreferenced formals
2052 Set_Referenced (Formal);
2053 Next_Formal (Formal);
2054 end loop;
2056 Proc_Call :=
2057 Make_Procedure_Call_Statement (Loc,
2058 Name => New_Occurrence_Of (Proc_Id, Loc),
2059 Parameter_Associations => Actual_List);
2060 end;
2062 -- Generate:
2064 -- declare
2065 -- New_Obj : ...
2066 -- begin
2067 -- Proc (New_Obj, ...);
2068 -- return New_Obj;
2069 -- end;
2071 Blk_Stmt :=
2072 Make_Block_Statement (Loc,
2073 Declarations => New_List (New_Obj),
2074 Handled_Statement_Sequence =>
2075 Make_Handled_Sequence_Of_Statements (Loc,
2076 Statements => New_List (
2078 Proc_Call,
2080 Make_Simple_Return_Statement (Loc,
2081 Expression =>
2082 New_Occurrence_Of
2083 (Defining_Identifier (New_Obj), Loc)))));
2085 Rewrite (Ret_Node, Blk_Stmt);
2086 end Split_Unconstrained_Function;
2088 -- Local variables
2090 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2092 -- Start of processing for Check_And_Split_Unconstrained_Function
2094 begin
2095 pragma Assert (Back_End_Inlining
2096 and then Ekind (Spec_Id) = E_Function
2097 and then Returns_Unconstrained_Type (Spec_Id)
2098 and then Comes_From_Source (Body_Id)
2099 and then (Has_Pragma_Inline_Always (Spec_Id)
2100 or else Optimization_Level > 0));
2102 -- This routine must not be used in GNATprove mode since GNATprove
2103 -- relies on frontend inlining
2105 pragma Assert (not GNATprove_Mode);
2107 -- No need to split the function if we cannot generate the code
2109 if Serious_Errors_Detected /= 0 then
2110 return;
2111 end if;
2113 -- No action needed in stubs since the attribute Body_To_Inline
2114 -- is not available
2116 if Nkind (Decl) = N_Subprogram_Body_Stub then
2117 return;
2119 -- Cannot build the body to inline if the attribute is already set.
2120 -- This attribute may have been set if this is a subprogram renaming
2121 -- declarations (see Freeze.Build_Renamed_Body).
2123 elsif Present (Body_To_Inline (Decl)) then
2124 return;
2126 -- Check excluded declarations
2128 elsif Present (Declarations (N))
2129 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
2130 then
2131 return;
2133 -- Check excluded statements. There is no need to protect us against
2134 -- exception handlers since they are supported by the GCC backend.
2136 elsif Present (Handled_Statement_Sequence (N))
2137 and then Has_Excluded_Statement
2138 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2139 then
2140 return;
2141 end if;
2143 -- Build the body to inline only if really needed
2145 if Can_Split_Unconstrained_Function (N) then
2146 Split_Unconstrained_Function (N, Spec_Id);
2147 Build_Body_To_Inline (N, Spec_Id);
2148 Set_Is_Inlined (Spec_Id);
2149 end if;
2150 end Check_And_Split_Unconstrained_Function;
2152 -------------------------------------
2153 -- Check_Package_Body_For_Inlining --
2154 -------------------------------------
2156 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2157 Bname : Unit_Name_Type;
2158 E : Entity_Id;
2159 OK : Boolean;
2161 begin
2162 -- Legacy implementation (relying on frontend inlining)
2164 if not Back_End_Inlining
2165 and then Is_Compilation_Unit (P)
2166 and then not Is_Generic_Instance (P)
2167 then
2168 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2170 E := First_Entity (P);
2171 while Present (E) loop
2172 if Has_Pragma_Inline_Always (E)
2173 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2174 then
2175 if not Is_Loaded (Bname) then
2176 Load_Needed_Body (N, OK);
2178 if OK then
2180 -- Check we are not trying to inline a parent whose body
2181 -- depends on a child, when we are compiling the body of
2182 -- the child. Otherwise we have a potential elaboration
2183 -- circularity with inlined subprograms and with
2184 -- Taft-Amendment types.
2186 declare
2187 Comp : Node_Id; -- Body just compiled
2188 Child_Spec : Entity_Id; -- Spec of main unit
2189 Ent : Entity_Id; -- For iteration
2190 With_Clause : Node_Id; -- Context of body.
2192 begin
2193 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2194 and then Present (Body_Entity (P))
2195 then
2196 Child_Spec :=
2197 Defining_Entity
2198 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2200 Comp :=
2201 Parent (Unit_Declaration_Node (Body_Entity (P)));
2203 -- Check whether the context of the body just
2204 -- compiled includes a child of itself, and that
2205 -- child is the spec of the main compilation.
2207 With_Clause := First (Context_Items (Comp));
2208 while Present (With_Clause) loop
2209 if Nkind (With_Clause) = N_With_Clause
2210 and then
2211 Scope (Entity (Name (With_Clause))) = P
2212 and then
2213 Entity (Name (With_Clause)) = Child_Spec
2214 then
2215 Error_Msg_Node_2 := Child_Spec;
2216 Error_Msg_NE
2217 ("body of & depends on child unit&??",
2218 With_Clause, P);
2219 Error_Msg_N
2220 ("\subprograms in body cannot be inlined??",
2221 With_Clause);
2223 -- Disable further inlining from this unit,
2224 -- and keep Taft-amendment types incomplete.
2226 Ent := First_Entity (P);
2227 while Present (Ent) loop
2228 if Is_Type (Ent)
2229 and then Has_Completion_In_Body (Ent)
2230 then
2231 Set_Full_View (Ent, Empty);
2233 elsif Is_Subprogram (Ent) then
2234 Set_Is_Inlined (Ent, False);
2235 end if;
2237 Next_Entity (Ent);
2238 end loop;
2240 return;
2241 end if;
2243 Next (With_Clause);
2244 end loop;
2245 end if;
2246 end;
2248 elsif Ineffective_Inline_Warnings then
2249 Error_Msg_Unit_1 := Bname;
2250 Error_Msg_N
2251 ("unable to inline subprograms defined in $??", P);
2252 Error_Msg_N ("\body not found??", P);
2253 return;
2254 end if;
2255 end if;
2257 return;
2258 end if;
2260 Next_Entity (E);
2261 end loop;
2262 end if;
2263 end Check_Package_Body_For_Inlining;
2265 --------------------
2266 -- Cleanup_Scopes --
2267 --------------------
2269 procedure Cleanup_Scopes is
2270 Elmt : Elmt_Id;
2271 Decl : Node_Id;
2272 Scop : Entity_Id;
2274 begin
2275 Elmt := First_Elmt (To_Clean);
2276 while Present (Elmt) loop
2277 Scop := Node (Elmt);
2279 if Ekind (Scop) = E_Entry then
2280 Scop := Protected_Body_Subprogram (Scop);
2282 elsif Is_Subprogram (Scop)
2283 and then Is_Protected_Type (Scope (Scop))
2284 and then Present (Protected_Body_Subprogram (Scop))
2285 then
2286 -- If a protected operation contains an instance, its cleanup
2287 -- operations have been delayed, and the subprogram has been
2288 -- rewritten in the expansion of the enclosing protected body. It
2289 -- is the corresponding subprogram that may require the cleanup
2290 -- operations, so propagate the information that triggers cleanup
2291 -- activity.
2293 Set_Uses_Sec_Stack
2294 (Protected_Body_Subprogram (Scop),
2295 Uses_Sec_Stack (Scop));
2297 Scop := Protected_Body_Subprogram (Scop);
2298 end if;
2300 if Ekind (Scop) = E_Block then
2301 Decl := Parent (Block_Node (Scop));
2303 else
2304 Decl := Unit_Declaration_Node (Scop);
2306 if Nkind_In (Decl, N_Subprogram_Declaration,
2307 N_Task_Type_Declaration,
2308 N_Subprogram_Body_Stub)
2309 then
2310 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2311 end if;
2312 end if;
2314 Push_Scope (Scop);
2315 Expand_Cleanup_Actions (Decl);
2316 End_Scope;
2318 Elmt := Next_Elmt (Elmt);
2319 end loop;
2320 end Cleanup_Scopes;
2322 -------------------------
2323 -- Expand_Inlined_Call --
2324 -------------------------
2326 procedure Expand_Inlined_Call
2327 (N : Node_Id;
2328 Subp : Entity_Id;
2329 Orig_Subp : Entity_Id)
2331 Decls : constant List_Id := New_List;
2332 Is_Predef : constant Boolean :=
2333 Is_Predefined_Unit (Get_Source_Unit (Subp));
2334 Loc : constant Source_Ptr := Sloc (N);
2335 Orig_Bod : constant Node_Id :=
2336 Body_To_Inline (Unit_Declaration_Node (Subp));
2338 Uses_Back_End : constant Boolean :=
2339 Back_End_Inlining and then Optimization_Level > 0;
2340 -- The back-end expansion is used if the target supports back-end
2341 -- inlining and some level of optimixation is required; otherwise
2342 -- the inlining takes place fully as a tree expansion.
2344 Blk : Node_Id;
2345 Decl : Node_Id;
2346 Exit_Lab : Entity_Id := Empty;
2347 F : Entity_Id;
2348 A : Node_Id;
2349 Lab_Decl : Node_Id := Empty;
2350 Lab_Id : Node_Id;
2351 New_A : Node_Id;
2352 Num_Ret : Nat := 0;
2353 Ret_Type : Entity_Id;
2354 Temp : Entity_Id;
2355 Temp_Typ : Entity_Id;
2357 Is_Unc : Boolean;
2358 Is_Unc_Decl : Boolean;
2359 -- If the type returned by the function is unconstrained and the call
2360 -- can be inlined, special processing is required.
2362 Return_Object : Entity_Id := Empty;
2363 -- Entity in declaration in an extended_return_statement
2365 Targ : Node_Id := Empty;
2366 -- The target of the call. If context is an assignment statement then
2367 -- this is the left-hand side of the assignment, else it is a temporary
2368 -- to which the return value is assigned prior to rewriting the call.
2370 Targ1 : Node_Id := Empty;
2371 -- A separate target used when the return type is unconstrained
2373 procedure Declare_Postconditions_Result;
2374 -- When generating C code, declare _Result, which may be used in the
2375 -- inlined _Postconditions procedure to verify the return value.
2377 procedure Make_Exit_Label;
2378 -- Build declaration for exit label to be used in Return statements,
2379 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2380 -- declaration). Does nothing if Exit_Lab already set.
2382 function Process_Formals (N : Node_Id) return Traverse_Result;
2383 -- Replace occurrence of a formal with the corresponding actual, or the
2384 -- thunk generated for it. Replace a return statement with an assignment
2385 -- to the target of the call, with appropriate conversions if needed.
2387 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2388 -- If the call being expanded is that of an internal subprogram, set the
2389 -- sloc of the generated block to that of the call itself, so that the
2390 -- expansion is skipped by the "next" command in gdb. Same processing
2391 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2392 -- Debug_Generated_Code is true, suppress this change to simplify our
2393 -- own development. Same in GNATprove mode, to ensure that warnings and
2394 -- diagnostics point to the proper location.
2396 procedure Reset_Dispatching_Calls (N : Node_Id);
2397 -- In subtree N search for occurrences of dispatching calls that use the
2398 -- Ada 2005 Object.Operation notation and the object is a formal of the
2399 -- inlined subprogram. Reset the entity associated with Operation in all
2400 -- the found occurrences.
2402 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2403 -- If the function body is a single expression, replace call with
2404 -- expression, else insert block appropriately.
2406 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2407 -- If procedure body has no local variables, inline body without
2408 -- creating block, otherwise rewrite call with block.
2410 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2411 -- Determine whether a formal parameter is used only once in Orig_Bod
2413 -----------------------------------
2414 -- Declare_Postconditions_Result --
2415 -----------------------------------
2417 procedure Declare_Postconditions_Result is
2418 Enclosing_Subp : constant Entity_Id := Scope (Subp);
2420 begin
2421 pragma Assert
2422 (Modify_Tree_For_C
2423 and then Is_Subprogram (Enclosing_Subp)
2424 and then Present (Postconditions_Proc (Enclosing_Subp)));
2426 if Ekind (Enclosing_Subp) = E_Function then
2427 if Nkind (First (Parameter_Associations (N))) in
2428 N_Numeric_Or_String_Literal
2429 then
2430 Append_To (Declarations (Blk),
2431 Make_Object_Declaration (Loc,
2432 Defining_Identifier =>
2433 Make_Defining_Identifier (Loc, Name_uResult),
2434 Constant_Present => True,
2435 Object_Definition =>
2436 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2437 Expression =>
2438 New_Copy_Tree (First (Parameter_Associations (N)))));
2439 else
2440 Append_To (Declarations (Blk),
2441 Make_Object_Renaming_Declaration (Loc,
2442 Defining_Identifier =>
2443 Make_Defining_Identifier (Loc, Name_uResult),
2444 Subtype_Mark =>
2445 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2446 Name =>
2447 New_Copy_Tree (First (Parameter_Associations (N)))));
2448 end if;
2449 end if;
2450 end Declare_Postconditions_Result;
2452 ---------------------
2453 -- Make_Exit_Label --
2454 ---------------------
2456 procedure Make_Exit_Label is
2457 Lab_Ent : Entity_Id;
2458 begin
2459 if No (Exit_Lab) then
2460 Lab_Ent := Make_Temporary (Loc, 'L');
2461 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
2462 Exit_Lab := Make_Label (Loc, Lab_Id);
2463 Lab_Decl :=
2464 Make_Implicit_Label_Declaration (Loc,
2465 Defining_Identifier => Lab_Ent,
2466 Label_Construct => Exit_Lab);
2467 end if;
2468 end Make_Exit_Label;
2470 ---------------------
2471 -- Process_Formals --
2472 ---------------------
2474 function Process_Formals (N : Node_Id) return Traverse_Result is
2475 A : Entity_Id;
2476 E : Entity_Id;
2477 Ret : Node_Id;
2479 begin
2480 if Is_Entity_Name (N) and then Present (Entity (N)) then
2481 E := Entity (N);
2483 if Is_Formal (E) and then Scope (E) = Subp then
2484 A := Renamed_Object (E);
2486 -- Rewrite the occurrence of the formal into an occurrence of
2487 -- the actual. Also establish visibility on the proper view of
2488 -- the actual's subtype for the body's context (if the actual's
2489 -- subtype is private at the call point but its full view is
2490 -- visible to the body, then the inlined tree here must be
2491 -- analyzed with the full view).
2493 if Is_Entity_Name (A) then
2494 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
2495 Check_Private_View (N);
2497 elsif Nkind (A) = N_Defining_Identifier then
2498 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
2499 Check_Private_View (N);
2501 -- Numeric literal
2503 else
2504 Rewrite (N, New_Copy (A));
2505 end if;
2506 end if;
2508 return Skip;
2510 elsif Is_Entity_Name (N)
2511 and then Present (Return_Object)
2512 and then Chars (N) = Chars (Return_Object)
2513 then
2514 -- Occurrence within an extended return statement. The return
2515 -- object is local to the body been inlined, and thus the generic
2516 -- copy is not analyzed yet, so we match by name, and replace it
2517 -- with target of call.
2519 if Nkind (Targ) = N_Defining_Identifier then
2520 Rewrite (N, New_Occurrence_Of (Targ, Loc));
2521 else
2522 Rewrite (N, New_Copy_Tree (Targ));
2523 end if;
2525 return Skip;
2527 elsif Nkind (N) = N_Simple_Return_Statement then
2528 if No (Expression (N)) then
2529 Num_Ret := Num_Ret + 1;
2530 Make_Exit_Label;
2531 Rewrite (N,
2532 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2534 else
2535 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2536 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2537 then
2538 -- Function body is a single expression. No need for
2539 -- exit label.
2541 null;
2543 else
2544 Num_Ret := Num_Ret + 1;
2545 Make_Exit_Label;
2546 end if;
2548 -- Because of the presence of private types, the views of the
2549 -- expression and the context may be different, so place
2550 -- a type conversion to the context type to avoid spurious
2551 -- errors, e.g. when the expression is a numeric literal and
2552 -- the context is private. If the expression is an aggregate,
2553 -- use a qualified expression, because an aggregate is not a
2554 -- legal argument of a conversion. Ditto for numeric, character
2555 -- and string literals, and attributes that yield a universal
2556 -- type, because those must be resolved to a specific type.
2558 if Nkind_In (Expression (N), N_Aggregate,
2559 N_Character_Literal,
2560 N_Null,
2561 N_String_Literal)
2562 or else Yields_Universal_Type (Expression (N))
2563 then
2564 Ret :=
2565 Make_Qualified_Expression (Sloc (N),
2566 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2567 Expression => Relocate_Node (Expression (N)));
2569 -- Use an unchecked type conversion between access types, for
2570 -- which a type conversion would not always be valid, as no
2571 -- check may result from the conversion.
2573 elsif Is_Access_Type (Ret_Type) then
2574 Ret :=
2575 Unchecked_Convert_To
2576 (Ret_Type, Relocate_Node (Expression (N)));
2578 -- Otherwise use a type conversion, which may trigger a check
2580 else
2581 Ret :=
2582 Make_Type_Conversion (Sloc (N),
2583 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2584 Expression => Relocate_Node (Expression (N)));
2585 end if;
2587 if Nkind (Targ) = N_Defining_Identifier then
2588 Rewrite (N,
2589 Make_Assignment_Statement (Loc,
2590 Name => New_Occurrence_Of (Targ, Loc),
2591 Expression => Ret));
2592 else
2593 Rewrite (N,
2594 Make_Assignment_Statement (Loc,
2595 Name => New_Copy (Targ),
2596 Expression => Ret));
2597 end if;
2599 Set_Assignment_OK (Name (N));
2601 if Present (Exit_Lab) then
2602 Insert_After (N,
2603 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2604 end if;
2605 end if;
2607 return OK;
2609 -- An extended return becomes a block whose first statement is the
2610 -- assignment of the initial expression of the return object to the
2611 -- target of the call itself.
2613 elsif Nkind (N) = N_Extended_Return_Statement then
2614 declare
2615 Return_Decl : constant Entity_Id :=
2616 First (Return_Object_Declarations (N));
2617 Assign : Node_Id;
2619 begin
2620 Return_Object := Defining_Identifier (Return_Decl);
2622 if Present (Expression (Return_Decl)) then
2623 if Nkind (Targ) = N_Defining_Identifier then
2624 Assign :=
2625 Make_Assignment_Statement (Loc,
2626 Name => New_Occurrence_Of (Targ, Loc),
2627 Expression => Expression (Return_Decl));
2628 else
2629 Assign :=
2630 Make_Assignment_Statement (Loc,
2631 Name => New_Copy (Targ),
2632 Expression => Expression (Return_Decl));
2633 end if;
2635 Set_Assignment_OK (Name (Assign));
2637 if No (Handled_Statement_Sequence (N)) then
2638 Set_Handled_Statement_Sequence (N,
2639 Make_Handled_Sequence_Of_Statements (Loc,
2640 Statements => New_List));
2641 end if;
2643 Prepend (Assign,
2644 Statements (Handled_Statement_Sequence (N)));
2645 end if;
2647 Rewrite (N,
2648 Make_Block_Statement (Loc,
2649 Handled_Statement_Sequence =>
2650 Handled_Statement_Sequence (N)));
2652 return OK;
2653 end;
2655 -- Remove pragma Unreferenced since it may refer to formals that
2656 -- are not visible in the inlined body, and in any case we will
2657 -- not be posting warnings on the inlined body so it is unneeded.
2659 elsif Nkind (N) = N_Pragma
2660 and then Pragma_Name (N) = Name_Unreferenced
2661 then
2662 Rewrite (N, Make_Null_Statement (Sloc (N)));
2663 return OK;
2665 else
2666 return OK;
2667 end if;
2668 end Process_Formals;
2670 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2672 ------------------
2673 -- Process_Sloc --
2674 ------------------
2676 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2677 begin
2678 if not Debug_Generated_Code then
2679 Set_Sloc (Nod, Sloc (N));
2680 Set_Comes_From_Source (Nod, False);
2681 end if;
2683 return OK;
2684 end Process_Sloc;
2686 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2688 ------------------------------
2689 -- Reset_Dispatching_Calls --
2690 ------------------------------
2692 procedure Reset_Dispatching_Calls (N : Node_Id) is
2694 function Do_Reset (N : Node_Id) return Traverse_Result;
2695 -- Comment required ???
2697 --------------
2698 -- Do_Reset --
2699 --------------
2701 function Do_Reset (N : Node_Id) return Traverse_Result is
2702 begin
2703 if Nkind (N) = N_Procedure_Call_Statement
2704 and then Nkind (Name (N)) = N_Selected_Component
2705 and then Nkind (Prefix (Name (N))) = N_Identifier
2706 and then Is_Formal (Entity (Prefix (Name (N))))
2707 and then Is_Dispatching_Operation
2708 (Entity (Selector_Name (Name (N))))
2709 then
2710 Set_Entity (Selector_Name (Name (N)), Empty);
2711 end if;
2713 return OK;
2714 end Do_Reset;
2716 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
2718 -- Local variables
2720 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
2721 pragma Unreferenced (Dummy);
2723 -- Start of processing for Reset_Dispatching_Calls
2725 begin
2726 null;
2727 end Reset_Dispatching_Calls;
2729 ---------------------------
2730 -- Rewrite_Function_Call --
2731 ---------------------------
2733 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2734 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2735 Fst : constant Node_Id := First (Statements (HSS));
2737 begin
2738 -- Optimize simple case: function body is a single return statement,
2739 -- which has been expanded into an assignment.
2741 if Is_Empty_List (Declarations (Blk))
2742 and then Nkind (Fst) = N_Assignment_Statement
2743 and then No (Next (Fst))
2744 then
2745 -- The function call may have been rewritten as the temporary
2746 -- that holds the result of the call, in which case remove the
2747 -- now useless declaration.
2749 if Nkind (N) = N_Identifier
2750 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2751 then
2752 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2753 end if;
2755 Rewrite (N, Expression (Fst));
2757 elsif Nkind (N) = N_Identifier
2758 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2759 then
2760 -- The block assigns the result of the call to the temporary
2762 Insert_After (Parent (Entity (N)), Blk);
2764 -- If the context is an assignment, and the left-hand side is free of
2765 -- side-effects, the replacement is also safe.
2766 -- Can this be generalized further???
2768 elsif Nkind (Parent (N)) = N_Assignment_Statement
2769 and then
2770 (Is_Entity_Name (Name (Parent (N)))
2771 or else
2772 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
2773 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
2775 or else
2776 (Nkind (Name (Parent (N))) = N_Selected_Component
2777 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
2778 then
2779 -- Replace assignment with the block
2781 declare
2782 Original_Assignment : constant Node_Id := Parent (N);
2784 begin
2785 -- Preserve the original assignment node to keep the complete
2786 -- assignment subtree consistent enough for Analyze_Assignment
2787 -- to proceed (specifically, the original Lhs node must still
2788 -- have an assignment statement as its parent).
2790 -- We cannot rely on Original_Node to go back from the block
2791 -- node to the assignment node, because the assignment might
2792 -- already be a rewrite substitution.
2794 Discard_Node (Relocate_Node (Original_Assignment));
2795 Rewrite (Original_Assignment, Blk);
2796 end;
2798 elsif Nkind (Parent (N)) = N_Object_Declaration then
2800 -- A call to a function which returns an unconstrained type
2801 -- found in the expression initializing an object-declaration is
2802 -- expanded into a procedure call which must be added after the
2803 -- object declaration.
2805 if Is_Unc_Decl and Back_End_Inlining then
2806 Insert_Action_After (Parent (N), Blk);
2807 else
2808 Set_Expression (Parent (N), Empty);
2809 Insert_After (Parent (N), Blk);
2810 end if;
2812 elsif Is_Unc and then not Back_End_Inlining then
2813 Insert_Before (Parent (N), Blk);
2814 end if;
2815 end Rewrite_Function_Call;
2817 ----------------------------
2818 -- Rewrite_Procedure_Call --
2819 ----------------------------
2821 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2822 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2824 begin
2825 -- If there is a transient scope for N, this will be the scope of the
2826 -- actions for N, and the statements in Blk need to be within this
2827 -- scope. For example, they need to have visibility on the constant
2828 -- declarations created for the formals.
2830 -- If N needs no transient scope, and if there are no declarations in
2831 -- the inlined body, we can do a little optimization and insert the
2832 -- statements for the body directly after N, and rewrite N to a
2833 -- null statement, instead of rewriting N into a full-blown block
2834 -- statement.
2836 if not Scope_Is_Transient
2837 and then Is_Empty_List (Declarations (Blk))
2838 then
2839 Insert_List_After (N, Statements (HSS));
2840 Rewrite (N, Make_Null_Statement (Loc));
2841 else
2842 Rewrite (N, Blk);
2843 end if;
2844 end Rewrite_Procedure_Call;
2846 -------------------------
2847 -- Formal_Is_Used_Once --
2848 -------------------------
2850 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2851 Use_Counter : Int := 0;
2853 function Count_Uses (N : Node_Id) return Traverse_Result;
2854 -- Traverse the tree and count the uses of the formal parameter.
2855 -- In this case, for optimization purposes, we do not need to
2856 -- continue the traversal once more than one use is encountered.
2858 ----------------
2859 -- Count_Uses --
2860 ----------------
2862 function Count_Uses (N : Node_Id) return Traverse_Result is
2863 begin
2864 -- The original node is an identifier
2866 if Nkind (N) = N_Identifier
2867 and then Present (Entity (N))
2869 -- Original node's entity points to the one in the copied body
2871 and then Nkind (Entity (N)) = N_Identifier
2872 and then Present (Entity (Entity (N)))
2874 -- The entity of the copied node is the formal parameter
2876 and then Entity (Entity (N)) = Formal
2877 then
2878 Use_Counter := Use_Counter + 1;
2880 if Use_Counter > 1 then
2882 -- Denote more than one use and abandon the traversal
2884 Use_Counter := 2;
2885 return Abandon;
2887 end if;
2888 end if;
2890 return OK;
2891 end Count_Uses;
2893 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2895 -- Start of processing for Formal_Is_Used_Once
2897 begin
2898 Count_Formal_Uses (Orig_Bod);
2899 return Use_Counter = 1;
2900 end Formal_Is_Used_Once;
2902 -- Start of processing for Expand_Inlined_Call
2904 begin
2905 -- Initializations for old/new semantics
2907 if not Uses_Back_End then
2908 Is_Unc := Is_Array_Type (Etype (Subp))
2909 and then not Is_Constrained (Etype (Subp));
2910 Is_Unc_Decl := False;
2911 else
2912 Is_Unc := Returns_Unconstrained_Type (Subp)
2913 and then Optimization_Level > 0;
2914 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
2915 and then Is_Unc;
2916 end if;
2918 -- Check for an illegal attempt to inline a recursive procedure. If the
2919 -- subprogram has parameters this is detected when trying to supply a
2920 -- binding for parameters that already have one. For parameterless
2921 -- subprograms this must be done explicitly.
2923 if In_Open_Scopes (Subp) then
2924 Cannot_Inline
2925 ("cannot inline call to recursive subprogram?", N, Subp);
2926 Set_Is_Inlined (Subp, False);
2927 return;
2929 -- Skip inlining if this is not a true inlining since the attribute
2930 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
2931 -- true inlining, Orig_Bod has code rather than being an entity.
2933 elsif Nkind (Orig_Bod) in N_Entity then
2934 return;
2935 end if;
2937 if Nkind (Orig_Bod) = N_Defining_Identifier
2938 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2939 then
2940 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2941 -- can be replaced with calls to the renamed entity directly, because
2942 -- the subprograms are subtype conformant. If the renamed subprogram
2943 -- is an inherited operation, we must redo the expansion because
2944 -- implicit conversions may be needed. Similarly, if the renamed
2945 -- entity is inlined, expand the call for further optimizations.
2947 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2949 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
2950 Expand_Call (N);
2951 end if;
2953 return;
2954 end if;
2956 -- Register the call in the list of inlined calls
2958 Append_New_Elmt (N, To => Inlined_Calls);
2960 -- Use generic machinery to copy body of inlined subprogram, as if it
2961 -- were an instantiation, resetting source locations appropriately, so
2962 -- that nested inlined calls appear in the main unit.
2964 Save_Env (Subp, Empty);
2965 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2967 -- Old semantics
2969 if not Uses_Back_End then
2970 declare
2971 Bod : Node_Id;
2973 begin
2974 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2975 Blk :=
2976 Make_Block_Statement (Loc,
2977 Declarations => Declarations (Bod),
2978 Handled_Statement_Sequence =>
2979 Handled_Statement_Sequence (Bod));
2981 if No (Declarations (Bod)) then
2982 Set_Declarations (Blk, New_List);
2983 end if;
2985 -- When generating C code, declare _Result, which may be used to
2986 -- verify the return value.
2988 if Modify_Tree_For_C
2989 and then Nkind (N) = N_Procedure_Call_Statement
2990 and then Chars (Name (N)) = Name_uPostconditions
2991 then
2992 Declare_Postconditions_Result;
2993 end if;
2995 -- For the unconstrained case, capture the name of the local
2996 -- variable that holds the result. This must be the first
2997 -- declaration in the block, because its bounds cannot depend
2998 -- on local variables. Otherwise there is no way to declare the
2999 -- result outside of the block. Needless to say, in general the
3000 -- bounds will depend on the actuals in the call.
3002 -- If the context is an assignment statement, as is the case
3003 -- for the expansion of an extended return, the left-hand side
3004 -- provides bounds even if the return type is unconstrained.
3006 if Is_Unc then
3007 declare
3008 First_Decl : Node_Id;
3010 begin
3011 First_Decl := First (Declarations (Blk));
3013 -- If the body is a single extended return statement,the
3014 -- resulting block is a nested block.
3016 if No (First_Decl) then
3017 First_Decl :=
3018 First (Statements (Handled_Statement_Sequence (Blk)));
3020 if Nkind (First_Decl) = N_Block_Statement then
3021 First_Decl := First (Declarations (First_Decl));
3022 end if;
3023 end if;
3025 -- No front-end inlining possible
3027 if Nkind (First_Decl) /= N_Object_Declaration then
3028 return;
3029 end if;
3031 if Nkind (Parent (N)) /= N_Assignment_Statement then
3032 Targ1 := Defining_Identifier (First_Decl);
3033 else
3034 Targ1 := Name (Parent (N));
3035 end if;
3036 end;
3037 end if;
3038 end;
3040 -- New semantics
3042 else
3043 declare
3044 Bod : Node_Id;
3046 begin
3047 -- General case
3049 if not Is_Unc then
3050 Bod :=
3051 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3052 Blk :=
3053 Make_Block_Statement (Loc,
3054 Declarations => Declarations (Bod),
3055 Handled_Statement_Sequence =>
3056 Handled_Statement_Sequence (Bod));
3058 -- Inline a call to a function that returns an unconstrained type.
3059 -- The semantic analyzer checked that frontend-inlined functions
3060 -- returning unconstrained types have no declarations and have
3061 -- a single extended return statement. As part of its processing
3062 -- the function was split into two subprograms: a procedure P' and
3063 -- a function F' that has a block with a call to procedure P' (see
3064 -- Split_Unconstrained_Function).
3066 else
3067 pragma Assert
3068 (Nkind
3069 (First
3070 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3071 N_Block_Statement);
3073 declare
3074 Blk_Stmt : constant Node_Id :=
3075 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
3076 First_Stmt : constant Node_Id :=
3077 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
3078 Second_Stmt : constant Node_Id := Next (First_Stmt);
3080 begin
3081 pragma Assert
3082 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3083 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3084 and then No (Next (Second_Stmt)));
3086 Bod :=
3087 Copy_Generic_Node
3088 (First
3089 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3090 Empty, Instantiating => True);
3091 Blk := Bod;
3093 -- Capture the name of the local variable that holds the
3094 -- result. This must be the first declaration in the block,
3095 -- because its bounds cannot depend on local variables.
3096 -- Otherwise there is no way to declare the result outside
3097 -- of the block. Needless to say, in general the bounds will
3098 -- depend on the actuals in the call.
3100 if Nkind (Parent (N)) /= N_Assignment_Statement then
3101 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3103 -- If the context is an assignment statement, as is the case
3104 -- for the expansion of an extended return, the left-hand
3105 -- side provides bounds even if the return type is
3106 -- unconstrained.
3108 else
3109 Targ1 := Name (Parent (N));
3110 end if;
3111 end;
3112 end if;
3114 if No (Declarations (Bod)) then
3115 Set_Declarations (Blk, New_List);
3116 end if;
3117 end;
3118 end if;
3120 -- If this is a derived function, establish the proper return type
3122 if Present (Orig_Subp) and then Orig_Subp /= Subp then
3123 Ret_Type := Etype (Orig_Subp);
3124 else
3125 Ret_Type := Etype (Subp);
3126 end if;
3128 -- Create temporaries for the actuals that are expressions, or that are
3129 -- scalars and require copying to preserve semantics.
3131 F := First_Formal (Subp);
3132 A := First_Actual (N);
3133 while Present (F) loop
3134 if Present (Renamed_Object (F)) then
3136 -- If expander is active, it is an error to try to inline a
3137 -- recursive program. In GNATprove mode, just indicate that the
3138 -- inlining will not happen, and mark the subprogram as not always
3139 -- inlined.
3141 if GNATprove_Mode then
3142 Cannot_Inline
3143 ("cannot inline call to recursive subprogram?", N, Subp);
3144 Set_Is_Inlined_Always (Subp, False);
3145 else
3146 Error_Msg_N
3147 ("cannot inline call to recursive subprogram", N);
3148 end if;
3150 return;
3151 end if;
3153 -- Reset Last_Assignment for any parameters of mode out or in out, to
3154 -- prevent spurious warnings about overwriting for assignments to the
3155 -- formal in the inlined code.
3157 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
3158 Set_Last_Assignment (Entity (A), Empty);
3159 end if;
3161 -- If the argument may be a controlling argument in a call within
3162 -- the inlined body, we must preserve its classwide nature to insure
3163 -- that dynamic dispatching take place subsequently. If the formal
3164 -- has a constraint it must be preserved to retain the semantics of
3165 -- the body.
3167 if Is_Class_Wide_Type (Etype (F))
3168 or else (Is_Access_Type (Etype (F))
3169 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3170 then
3171 Temp_Typ := Etype (F);
3173 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3174 and then Etype (F) /= Base_Type (Etype (F))
3175 and then Is_Constrained (Etype (F))
3176 then
3177 Temp_Typ := Etype (F);
3179 else
3180 Temp_Typ := Etype (A);
3181 end if;
3183 -- If the actual is a simple name or a literal, no need to
3184 -- create a temporary, object can be used directly.
3186 -- If the actual is a literal and the formal has its address taken,
3187 -- we cannot pass the literal itself as an argument, so its value
3188 -- must be captured in a temporary. Skip this optimization in
3189 -- GNATprove mode, to make sure any check on a type conversion
3190 -- will be issued.
3192 if (Is_Entity_Name (A)
3193 and then
3194 (not Is_Scalar_Type (Etype (A))
3195 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3196 and then not GNATprove_Mode)
3198 -- When the actual is an identifier and the corresponding formal is
3199 -- used only once in the original body, the formal can be substituted
3200 -- directly with the actual parameter. Skip this optimization in
3201 -- GNATprove mode, to make sure any check on a type conversion
3202 -- will be issued.
3204 or else
3205 (Nkind (A) = N_Identifier
3206 and then Formal_Is_Used_Once (F)
3207 and then not GNATprove_Mode)
3209 or else
3210 (Nkind_In (A, N_Real_Literal,
3211 N_Integer_Literal,
3212 N_Character_Literal)
3213 and then not Address_Taken (F))
3214 then
3215 if Etype (F) /= Etype (A) then
3216 Set_Renamed_Object
3217 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3218 else
3219 Set_Renamed_Object (F, A);
3220 end if;
3222 else
3223 Temp := Make_Temporary (Loc, 'C');
3225 -- If the actual for an in/in-out parameter is a view conversion,
3226 -- make it into an unchecked conversion, given that an untagged
3227 -- type conversion is not a proper object for a renaming.
3229 -- In-out conversions that involve real conversions have already
3230 -- been transformed in Expand_Actuals.
3232 if Nkind (A) = N_Type_Conversion
3233 and then Ekind (F) /= E_In_Parameter
3234 then
3235 New_A :=
3236 Make_Unchecked_Type_Conversion (Loc,
3237 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3238 Expression => Relocate_Node (Expression (A)));
3240 -- In GNATprove mode, keep the most precise type of the actual for
3241 -- the temporary variable, when the formal type is unconstrained.
3242 -- Otherwise, the AST may contain unexpected assignment statements
3243 -- to a temporary variable of unconstrained type renaming a local
3244 -- variable of constrained type, which is not expected by
3245 -- GNATprove.
3247 elsif Etype (F) /= Etype (A)
3248 and then (not GNATprove_Mode or else Is_Constrained (Etype (F)))
3249 then
3250 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3251 Temp_Typ := Etype (F);
3253 else
3254 New_A := Relocate_Node (A);
3255 end if;
3257 Set_Sloc (New_A, Sloc (N));
3259 -- If the actual has a by-reference type, it cannot be copied,
3260 -- so its value is captured in a renaming declaration. Otherwise
3261 -- declare a local constant initialized with the actual.
3263 -- We also use a renaming declaration for expressions of an array
3264 -- type that is not bit-packed, both for efficiency reasons and to
3265 -- respect the semantics of the call: in most cases the original
3266 -- call will pass the parameter by reference, and thus the inlined
3267 -- code will have the same semantics.
3269 -- Finally, we need a renaming declaration in the case of limited
3270 -- types for which initialization cannot be by copy either.
3272 if Ekind (F) = E_In_Parameter
3273 and then not Is_By_Reference_Type (Etype (A))
3274 and then not Is_Limited_Type (Etype (A))
3275 and then
3276 (not Is_Array_Type (Etype (A))
3277 or else not Is_Object_Reference (A)
3278 or else Is_Bit_Packed_Array (Etype (A)))
3279 then
3280 Decl :=
3281 Make_Object_Declaration (Loc,
3282 Defining_Identifier => Temp,
3283 Constant_Present => True,
3284 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3285 Expression => New_A);
3287 else
3288 -- In GNATprove mode, make an explicit copy of input
3289 -- parameters when formal and actual types differ, to make
3290 -- sure any check on the type conversion will be issued.
3291 -- The legality of the copy is ensured by calling first
3292 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3294 if GNATprove_Mode
3295 and then Ekind (F) /= E_Out_Parameter
3296 and then not Same_Type (Etype (F), Etype (A))
3297 then
3298 pragma Assert (not Is_By_Reference_Type (Etype (A)));
3299 pragma Assert (not Is_Limited_Type (Etype (A)));
3301 Append_To (Decls,
3302 Make_Object_Declaration (Loc,
3303 Defining_Identifier => Make_Temporary (Loc, 'C'),
3304 Constant_Present => True,
3305 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3306 Expression => New_Copy_Tree (New_A)));
3307 end if;
3309 Decl :=
3310 Make_Object_Renaming_Declaration (Loc,
3311 Defining_Identifier => Temp,
3312 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3313 Name => New_A);
3314 end if;
3316 Append (Decl, Decls);
3317 Set_Renamed_Object (F, Temp);
3318 end if;
3320 Next_Formal (F);
3321 Next_Actual (A);
3322 end loop;
3324 -- Establish target of function call. If context is not assignment or
3325 -- declaration, create a temporary as a target. The declaration for the
3326 -- temporary may be subsequently optimized away if the body is a single
3327 -- expression, or if the left-hand side of the assignment is simple
3328 -- enough, i.e. an entity or an explicit dereference of one.
3330 if Ekind (Subp) = E_Function then
3331 if Nkind (Parent (N)) = N_Assignment_Statement
3332 and then Is_Entity_Name (Name (Parent (N)))
3333 then
3334 Targ := Name (Parent (N));
3336 elsif Nkind (Parent (N)) = N_Assignment_Statement
3337 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3338 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3339 then
3340 Targ := Name (Parent (N));
3342 elsif Nkind (Parent (N)) = N_Assignment_Statement
3343 and then Nkind (Name (Parent (N))) = N_Selected_Component
3344 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3345 then
3346 Targ := New_Copy_Tree (Name (Parent (N)));
3348 elsif Nkind (Parent (N)) = N_Object_Declaration
3349 and then Is_Limited_Type (Etype (Subp))
3350 then
3351 Targ := Defining_Identifier (Parent (N));
3353 -- New semantics: In an object declaration avoid an extra copy
3354 -- of the result of a call to an inlined function that returns
3355 -- an unconstrained type
3357 elsif Uses_Back_End
3358 and then Nkind (Parent (N)) = N_Object_Declaration
3359 and then Is_Unc
3360 then
3361 Targ := Defining_Identifier (Parent (N));
3363 else
3364 -- Replace call with temporary and create its declaration
3366 Temp := Make_Temporary (Loc, 'C');
3367 Set_Is_Internal (Temp);
3369 -- For the unconstrained case, the generated temporary has the
3370 -- same constrained declaration as the result variable. It may
3371 -- eventually be possible to remove that temporary and use the
3372 -- result variable directly.
3374 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
3375 then
3376 Decl :=
3377 Make_Object_Declaration (Loc,
3378 Defining_Identifier => Temp,
3379 Object_Definition =>
3380 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3382 Replace_Formals (Decl);
3384 else
3385 Decl :=
3386 Make_Object_Declaration (Loc,
3387 Defining_Identifier => Temp,
3388 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
3390 Set_Etype (Temp, Ret_Type);
3391 end if;
3393 Set_No_Initialization (Decl);
3394 Append (Decl, Decls);
3395 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3396 Targ := Temp;
3397 end if;
3398 end if;
3400 Insert_Actions (N, Decls);
3402 if Is_Unc_Decl then
3404 -- Special management for inlining a call to a function that returns
3405 -- an unconstrained type and initializes an object declaration: we
3406 -- avoid generating undesired extra calls and goto statements.
3408 -- Given:
3409 -- function Func (...) return String is
3410 -- begin
3411 -- declare
3412 -- Result : String (1 .. 4);
3413 -- begin
3414 -- Proc (Result, ...);
3415 -- return Result;
3416 -- end;
3417 -- end Func;
3419 -- Result : String := Func (...);
3421 -- Replace this object declaration by:
3423 -- Result : String (1 .. 4);
3424 -- Proc (Result, ...);
3426 Remove_Homonym (Targ);
3428 Decl :=
3429 Make_Object_Declaration
3430 (Loc,
3431 Defining_Identifier => Targ,
3432 Object_Definition =>
3433 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3434 Replace_Formals (Decl);
3435 Rewrite (Parent (N), Decl);
3436 Analyze (Parent (N));
3438 -- Avoid spurious warnings since we know that this declaration is
3439 -- referenced by the procedure call.
3441 Set_Never_Set_In_Source (Targ, False);
3443 -- Remove the local declaration of the extended return stmt from the
3444 -- inlined code
3446 Remove (Parent (Targ1));
3448 -- Update the reference to the result (since we have rewriten the
3449 -- object declaration)
3451 declare
3452 Blk_Call_Stmt : Node_Id;
3454 begin
3455 -- Capture the call to the procedure
3457 Blk_Call_Stmt :=
3458 First (Statements (Handled_Statement_Sequence (Blk)));
3459 pragma Assert
3460 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
3462 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
3463 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
3464 New_Occurrence_Of (Targ, Loc));
3465 end;
3467 -- Remove the return statement
3469 pragma Assert
3470 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3471 N_Simple_Return_Statement);
3473 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3474 end if;
3476 -- Traverse the tree and replace formals with actuals or their thunks.
3477 -- Attach block to tree before analysis and rewriting.
3479 Replace_Formals (Blk);
3480 Set_Parent (Blk, N);
3482 if GNATprove_Mode then
3483 null;
3485 elsif not Comes_From_Source (Subp) or else Is_Predef then
3486 Reset_Slocs (Blk);
3487 end if;
3489 if Is_Unc_Decl then
3491 -- No action needed since return statement has been already removed
3493 null;
3495 elsif Present (Exit_Lab) then
3497 -- If there's a single return statement at the end of the subprogram,
3498 -- the corresponding goto statement and the corresponding label are
3499 -- useless.
3501 if Num_Ret = 1
3502 and then
3503 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3504 N_Goto_Statement
3505 then
3506 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3507 else
3508 Append (Lab_Decl, (Declarations (Blk)));
3509 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3510 end if;
3511 end if;
3513 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3514 -- on conflicting private views that Gigi would ignore. If this is a
3515 -- predefined unit, analyze with checks off, as is done in the non-
3516 -- inlined run-time units.
3518 declare
3519 I_Flag : constant Boolean := In_Inlined_Body;
3521 begin
3522 In_Inlined_Body := True;
3524 if Is_Predef then
3525 declare
3526 Style : constant Boolean := Style_Check;
3528 begin
3529 Style_Check := False;
3531 -- Search for dispatching calls that use the Object.Operation
3532 -- notation using an Object that is a parameter of the inlined
3533 -- function. We reset the decoration of Operation to force
3534 -- the reanalysis of the inlined dispatching call because
3535 -- the actual object has been inlined.
3537 Reset_Dispatching_Calls (Blk);
3539 Analyze (Blk, Suppress => All_Checks);
3540 Style_Check := Style;
3541 end;
3543 else
3544 Analyze (Blk);
3545 end if;
3547 In_Inlined_Body := I_Flag;
3548 end;
3550 if Ekind (Subp) = E_Procedure then
3551 Rewrite_Procedure_Call (N, Blk);
3553 else
3554 Rewrite_Function_Call (N, Blk);
3556 if Is_Unc_Decl then
3557 null;
3559 -- For the unconstrained case, the replacement of the call has been
3560 -- made prior to the complete analysis of the generated declarations.
3561 -- Propagate the proper type now.
3563 elsif Is_Unc then
3564 if Nkind (N) = N_Identifier then
3565 Set_Etype (N, Etype (Entity (N)));
3566 else
3567 Set_Etype (N, Etype (Targ1));
3568 end if;
3569 end if;
3570 end if;
3572 Restore_Env;
3574 -- Cleanup mapping between formals and actuals for other expansions
3576 F := First_Formal (Subp);
3577 while Present (F) loop
3578 Set_Renamed_Object (F, Empty);
3579 Next_Formal (F);
3580 end loop;
3581 end Expand_Inlined_Call;
3583 --------------------------
3584 -- Get_Code_Unit_Entity --
3585 --------------------------
3587 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
3588 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
3590 begin
3591 if Ekind (Unit) = E_Package_Body then
3592 Unit := Spec_Entity (Unit);
3593 end if;
3595 return Unit;
3596 end Get_Code_Unit_Entity;
3598 ------------------------------
3599 -- Has_Excluded_Declaration --
3600 ------------------------------
3602 function Has_Excluded_Declaration
3603 (Subp : Entity_Id;
3604 Decls : List_Id) return Boolean
3606 D : Node_Id;
3608 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3609 -- Nested subprograms make a given body ineligible for inlining, but
3610 -- we make an exception for instantiations of unchecked conversion.
3611 -- The body has not been analyzed yet, so check the name, and verify
3612 -- that the visible entity with that name is the predefined unit.
3614 -----------------------------
3615 -- Is_Unchecked_Conversion --
3616 -----------------------------
3618 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3619 Id : constant Node_Id := Name (D);
3620 Conv : Entity_Id;
3622 begin
3623 if Nkind (Id) = N_Identifier
3624 and then Chars (Id) = Name_Unchecked_Conversion
3625 then
3626 Conv := Current_Entity (Id);
3628 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3629 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3630 then
3631 Conv := Current_Entity (Selector_Name (Id));
3632 else
3633 return False;
3634 end if;
3636 return Present (Conv)
3637 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
3638 and then Is_Intrinsic_Subprogram (Conv);
3639 end Is_Unchecked_Conversion;
3641 -- Start of processing for Has_Excluded_Declaration
3643 begin
3644 -- No action needed if the check is not needed
3646 if not Check_Inlining_Restrictions then
3647 return False;
3648 end if;
3650 D := First (Decls);
3651 while Present (D) loop
3653 -- First declarations universally excluded
3655 if Nkind (D) = N_Package_Declaration then
3656 Cannot_Inline
3657 ("cannot inline & (nested package declaration)?", D, Subp);
3658 return True;
3660 elsif Nkind (D) = N_Package_Instantiation then
3661 Cannot_Inline
3662 ("cannot inline & (nested package instantiation)?", D, Subp);
3663 return True;
3664 end if;
3666 -- Then declarations excluded only for front-end inlining
3668 if Back_End_Inlining then
3669 null;
3671 elsif Nkind (D) = N_Task_Type_Declaration
3672 or else Nkind (D) = N_Single_Task_Declaration
3673 then
3674 Cannot_Inline
3675 ("cannot inline & (nested task type declaration)?", D, Subp);
3676 return True;
3678 elsif Nkind (D) = N_Protected_Type_Declaration
3679 or else Nkind (D) = N_Single_Protected_Declaration
3680 then
3681 Cannot_Inline
3682 ("cannot inline & (nested protected type declaration)?",
3683 D, Subp);
3684 return True;
3686 elsif Nkind (D) = N_Subprogram_Body then
3687 Cannot_Inline
3688 ("cannot inline & (nested subprogram)?", D, Subp);
3689 return True;
3691 elsif Nkind (D) = N_Function_Instantiation
3692 and then not Is_Unchecked_Conversion (D)
3693 then
3694 Cannot_Inline
3695 ("cannot inline & (nested function instantiation)?", D, Subp);
3696 return True;
3698 elsif Nkind (D) = N_Procedure_Instantiation then
3699 Cannot_Inline
3700 ("cannot inline & (nested procedure instantiation)?", D, Subp);
3701 return True;
3703 -- Subtype declarations with predicates will generate predicate
3704 -- functions, i.e. nested subprogram bodies, so inlining is not
3705 -- possible.
3707 elsif Nkind (D) = N_Subtype_Declaration
3708 and then Present (Aspect_Specifications (D))
3709 then
3710 declare
3711 A : Node_Id;
3712 A_Id : Aspect_Id;
3714 begin
3715 A := First (Aspect_Specifications (D));
3716 while Present (A) loop
3717 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3719 if A_Id = Aspect_Predicate
3720 or else A_Id = Aspect_Static_Predicate
3721 or else A_Id = Aspect_Dynamic_Predicate
3722 then
3723 Cannot_Inline
3724 ("cannot inline & (subtype declaration with "
3725 & "predicate)?", D, Subp);
3726 return True;
3727 end if;
3729 Next (A);
3730 end loop;
3731 end;
3732 end if;
3734 Next (D);
3735 end loop;
3737 return False;
3738 end Has_Excluded_Declaration;
3740 ----------------------------
3741 -- Has_Excluded_Statement --
3742 ----------------------------
3744 function Has_Excluded_Statement
3745 (Subp : Entity_Id;
3746 Stats : List_Id) return Boolean
3748 S : Node_Id;
3749 E : Node_Id;
3751 begin
3752 -- No action needed if the check is not needed
3754 if not Check_Inlining_Restrictions then
3755 return False;
3756 end if;
3758 S := First (Stats);
3759 while Present (S) loop
3760 if Nkind_In (S, N_Abort_Statement,
3761 N_Asynchronous_Select,
3762 N_Conditional_Entry_Call,
3763 N_Delay_Relative_Statement,
3764 N_Delay_Until_Statement,
3765 N_Selective_Accept,
3766 N_Timed_Entry_Call)
3767 then
3768 Cannot_Inline
3769 ("cannot inline & (non-allowed statement)?", S, Subp);
3770 return True;
3772 elsif Nkind (S) = N_Block_Statement then
3773 if Present (Declarations (S))
3774 and then Has_Excluded_Declaration (Subp, Declarations (S))
3775 then
3776 return True;
3778 elsif Present (Handled_Statement_Sequence (S)) then
3779 if not Back_End_Inlining
3780 and then
3781 Present
3782 (Exception_Handlers (Handled_Statement_Sequence (S)))
3783 then
3784 Cannot_Inline
3785 ("cannot inline& (exception handler)?",
3786 First (Exception_Handlers
3787 (Handled_Statement_Sequence (S))),
3788 Subp);
3789 return True;
3791 elsif Has_Excluded_Statement
3792 (Subp, Statements (Handled_Statement_Sequence (S)))
3793 then
3794 return True;
3795 end if;
3796 end if;
3798 elsif Nkind (S) = N_Case_Statement then
3799 E := First (Alternatives (S));
3800 while Present (E) loop
3801 if Has_Excluded_Statement (Subp, Statements (E)) then
3802 return True;
3803 end if;
3805 Next (E);
3806 end loop;
3808 elsif Nkind (S) = N_If_Statement then
3809 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
3810 return True;
3811 end if;
3813 if Present (Elsif_Parts (S)) then
3814 E := First (Elsif_Parts (S));
3815 while Present (E) loop
3816 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
3817 return True;
3818 end if;
3820 Next (E);
3821 end loop;
3822 end if;
3824 if Present (Else_Statements (S))
3825 and then Has_Excluded_Statement (Subp, Else_Statements (S))
3826 then
3827 return True;
3828 end if;
3830 elsif Nkind (S) = N_Loop_Statement
3831 and then Has_Excluded_Statement (Subp, Statements (S))
3832 then
3833 return True;
3835 elsif Nkind (S) = N_Extended_Return_Statement then
3836 if Present (Handled_Statement_Sequence (S))
3837 and then
3838 Has_Excluded_Statement
3839 (Subp, Statements (Handled_Statement_Sequence (S)))
3840 then
3841 return True;
3843 elsif not Back_End_Inlining
3844 and then Present (Handled_Statement_Sequence (S))
3845 and then
3846 Present (Exception_Handlers
3847 (Handled_Statement_Sequence (S)))
3848 then
3849 Cannot_Inline
3850 ("cannot inline& (exception handler)?",
3851 First (Exception_Handlers (Handled_Statement_Sequence (S))),
3852 Subp);
3853 return True;
3854 end if;
3855 end if;
3857 Next (S);
3858 end loop;
3860 return False;
3861 end Has_Excluded_Statement;
3863 --------------------------
3864 -- Has_Initialized_Type --
3865 --------------------------
3867 function Has_Initialized_Type (E : Entity_Id) return Boolean is
3868 E_Body : constant Node_Id := Subprogram_Body (E);
3869 Decl : Node_Id;
3871 begin
3872 if No (E_Body) then -- imported subprogram
3873 return False;
3875 else
3876 Decl := First (Declarations (E_Body));
3877 while Present (Decl) loop
3878 if Nkind (Decl) = N_Full_Type_Declaration
3879 and then Present (Init_Proc (Defining_Identifier (Decl)))
3880 then
3881 return True;
3882 end if;
3884 Next (Decl);
3885 end loop;
3886 end if;
3888 return False;
3889 end Has_Initialized_Type;
3891 -----------------------
3892 -- Has_Single_Return --
3893 -----------------------
3895 function Has_Single_Return (N : Node_Id) return Boolean is
3896 Return_Statement : Node_Id := Empty;
3898 function Check_Return (N : Node_Id) return Traverse_Result;
3900 ------------------
3901 -- Check_Return --
3902 ------------------
3904 function Check_Return (N : Node_Id) return Traverse_Result is
3905 begin
3906 if Nkind (N) = N_Simple_Return_Statement then
3907 if Present (Expression (N))
3908 and then Is_Entity_Name (Expression (N))
3909 then
3910 pragma Assert (Present (Entity (Expression (N))));
3912 if No (Return_Statement) then
3913 Return_Statement := N;
3914 return OK;
3916 else
3917 pragma Assert
3918 (Present (Entity (Expression (Return_Statement))));
3920 if Entity (Expression (N)) =
3921 Entity (Expression (Return_Statement))
3922 then
3923 return OK;
3924 else
3925 return Abandon;
3926 end if;
3927 end if;
3929 -- A return statement within an extended return is a noop after
3930 -- inlining.
3932 elsif No (Expression (N))
3933 and then Nkind (Parent (Parent (N))) =
3934 N_Extended_Return_Statement
3935 then
3936 return OK;
3938 else
3939 -- Expression has wrong form
3941 return Abandon;
3942 end if;
3944 -- We can only inline a build-in-place function if it has a single
3945 -- extended return.
3947 elsif Nkind (N) = N_Extended_Return_Statement then
3948 if No (Return_Statement) then
3949 Return_Statement := N;
3950 return OK;
3952 else
3953 return Abandon;
3954 end if;
3956 else
3957 return OK;
3958 end if;
3959 end Check_Return;
3961 function Check_All_Returns is new Traverse_Func (Check_Return);
3963 -- Start of processing for Has_Single_Return
3965 begin
3966 if Check_All_Returns (N) /= OK then
3967 return False;
3969 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3970 return True;
3972 else
3973 return
3974 Present (Declarations (N))
3975 and then Present (First (Declarations (N)))
3976 and then Entity (Expression (Return_Statement)) =
3977 Defining_Identifier (First (Declarations (N)));
3978 end if;
3979 end Has_Single_Return;
3981 -----------------------------
3982 -- In_Main_Unit_Or_Subunit --
3983 -----------------------------
3985 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
3986 Comp : Node_Id := Cunit (Get_Code_Unit (E));
3988 begin
3989 -- Check whether the subprogram or package to inline is within the main
3990 -- unit or its spec or within a subunit. In either case there are no
3991 -- additional bodies to process. If the subprogram appears in a parent
3992 -- of the current unit, the check on whether inlining is possible is
3993 -- done in Analyze_Inlined_Bodies.
3995 while Nkind (Unit (Comp)) = N_Subunit loop
3996 Comp := Library_Unit (Comp);
3997 end loop;
3999 return Comp = Cunit (Main_Unit)
4000 or else Comp = Library_Unit (Cunit (Main_Unit));
4001 end In_Main_Unit_Or_Subunit;
4003 ----------------
4004 -- Initialize --
4005 ----------------
4007 procedure Initialize is
4008 begin
4009 Pending_Descriptor.Init;
4010 Pending_Instantiations.Init;
4011 Inlined_Bodies.Init;
4012 Successors.Init;
4013 Inlined.Init;
4015 for J in Hash_Headers'Range loop
4016 Hash_Headers (J) := No_Subp;
4017 end loop;
4019 Inlined_Calls := No_Elist;
4020 Backend_Calls := No_Elist;
4021 Backend_Inlined_Subps := No_Elist;
4022 Backend_Not_Inlined_Subps := No_Elist;
4023 end Initialize;
4025 ------------------------
4026 -- Instantiate_Bodies --
4027 ------------------------
4029 -- Generic bodies contain all the non-local references, so an
4030 -- instantiation does not need any more context than Standard
4031 -- itself, even if the instantiation appears in an inner scope.
4032 -- Generic associations have verified that the contract model is
4033 -- satisfied, so that any error that may occur in the analysis of
4034 -- the body is an internal error.
4036 procedure Instantiate_Bodies is
4037 J : Nat;
4038 Info : Pending_Body_Info;
4040 begin
4041 if Serious_Errors_Detected = 0 then
4042 Expander_Active := (Operating_Mode = Opt.Generate_Code);
4043 Push_Scope (Standard_Standard);
4044 To_Clean := New_Elmt_List;
4046 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4047 Start_Generic;
4048 end if;
4050 -- A body instantiation may generate additional instantiations, so
4051 -- the following loop must scan to the end of a possibly expanding
4052 -- set (that's why we can't simply use a FOR loop here).
4054 J := 0;
4055 while J <= Pending_Instantiations.Last
4056 and then Serious_Errors_Detected = 0
4057 loop
4058 Info := Pending_Instantiations.Table (J);
4060 -- If the instantiation node is absent, it has been removed
4061 -- as part of unreachable code.
4063 if No (Info.Inst_Node) then
4064 null;
4066 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
4067 Instantiate_Package_Body (Info);
4068 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
4070 else
4071 Instantiate_Subprogram_Body (Info);
4072 end if;
4074 J := J + 1;
4075 end loop;
4077 -- Reset the table of instantiations. Additional instantiations
4078 -- may be added through inlining, when additional bodies are
4079 -- analyzed.
4081 Pending_Instantiations.Init;
4083 -- We can now complete the cleanup actions of scopes that contain
4084 -- pending instantiations (skipped for generic units, since we
4085 -- never need any cleanups in generic units).
4087 if Expander_Active
4088 and then not Is_Generic_Unit (Main_Unit_Entity)
4089 then
4090 Cleanup_Scopes;
4091 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4092 End_Generic;
4093 end if;
4095 Pop_Scope;
4096 end if;
4097 end Instantiate_Bodies;
4099 ---------------
4100 -- Is_Nested --
4101 ---------------
4103 function Is_Nested (E : Entity_Id) return Boolean is
4104 Scop : Entity_Id;
4106 begin
4107 Scop := Scope (E);
4108 while Scop /= Standard_Standard loop
4109 if Ekind (Scop) in Subprogram_Kind then
4110 return True;
4112 elsif Ekind (Scop) = E_Task_Type
4113 or else Ekind (Scop) = E_Entry
4114 or else Ekind (Scop) = E_Entry_Family
4115 then
4116 return True;
4117 end if;
4119 Scop := Scope (Scop);
4120 end loop;
4122 return False;
4123 end Is_Nested;
4125 ------------------------
4126 -- List_Inlining_Info --
4127 ------------------------
4129 procedure List_Inlining_Info is
4130 Elmt : Elmt_Id;
4131 Nod : Node_Id;
4132 Count : Nat;
4134 begin
4135 if not Debug_Flag_Dot_J then
4136 return;
4137 end if;
4139 -- Generate listing of calls inlined by the frontend
4141 if Present (Inlined_Calls) then
4142 Count := 0;
4143 Elmt := First_Elmt (Inlined_Calls);
4144 while Present (Elmt) loop
4145 Nod := Node (Elmt);
4147 if In_Extended_Main_Code_Unit (Nod) then
4148 Count := Count + 1;
4150 if Count = 1 then
4151 Write_Str ("List of calls inlined by the frontend");
4152 Write_Eol;
4153 end if;
4155 Write_Str (" ");
4156 Write_Int (Count);
4157 Write_Str (":");
4158 Write_Location (Sloc (Nod));
4159 Write_Str (":");
4160 Output.Write_Eol;
4161 end if;
4163 Next_Elmt (Elmt);
4164 end loop;
4165 end if;
4167 -- Generate listing of calls passed to the backend
4169 if Present (Backend_Calls) then
4170 Count := 0;
4172 Elmt := First_Elmt (Backend_Calls);
4173 while Present (Elmt) loop
4174 Nod := Node (Elmt);
4176 if In_Extended_Main_Code_Unit (Nod) then
4177 Count := Count + 1;
4179 if Count = 1 then
4180 Write_Str ("List of inlined calls passed to the backend");
4181 Write_Eol;
4182 end if;
4184 Write_Str (" ");
4185 Write_Int (Count);
4186 Write_Str (":");
4187 Write_Location (Sloc (Nod));
4188 Output.Write_Eol;
4189 end if;
4191 Next_Elmt (Elmt);
4192 end loop;
4193 end if;
4195 -- Generate listing of subprograms passed to the backend
4197 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
4198 Count := 0;
4200 Elmt := First_Elmt (Backend_Inlined_Subps);
4201 while Present (Elmt) loop
4202 Nod := Node (Elmt);
4204 Count := Count + 1;
4206 if Count = 1 then
4207 Write_Str
4208 ("List of inlined subprograms passed to the backend");
4209 Write_Eol;
4210 end if;
4212 Write_Str (" ");
4213 Write_Int (Count);
4214 Write_Str (":");
4215 Write_Name (Chars (Nod));
4216 Write_Str (" (");
4217 Write_Location (Sloc (Nod));
4218 Write_Str (")");
4219 Output.Write_Eol;
4221 Next_Elmt (Elmt);
4222 end loop;
4223 end if;
4225 -- Generate listing of subprograms that cannot be inlined by the backend
4227 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
4228 Count := 0;
4230 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
4231 while Present (Elmt) loop
4232 Nod := Node (Elmt);
4234 Count := Count + 1;
4236 if Count = 1 then
4237 Write_Str
4238 ("List of subprograms that cannot be inlined by the backend");
4239 Write_Eol;
4240 end if;
4242 Write_Str (" ");
4243 Write_Int (Count);
4244 Write_Str (":");
4245 Write_Name (Chars (Nod));
4246 Write_Str (" (");
4247 Write_Location (Sloc (Nod));
4248 Write_Str (")");
4249 Output.Write_Eol;
4251 Next_Elmt (Elmt);
4252 end loop;
4253 end if;
4254 end List_Inlining_Info;
4256 ----------
4257 -- Lock --
4258 ----------
4260 procedure Lock is
4261 begin
4262 Pending_Instantiations.Release;
4263 Pending_Instantiations.Locked := True;
4264 Inlined_Bodies.Release;
4265 Inlined_Bodies.Locked := True;
4266 Successors.Release;
4267 Successors.Locked := True;
4268 Inlined.Release;
4269 Inlined.Locked := True;
4270 end Lock;
4272 --------------------------------
4273 -- Remove_Aspects_And_Pragmas --
4274 --------------------------------
4276 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
4277 procedure Remove_Items (List : List_Id);
4278 -- Remove all useless aspects/pragmas from a particular list
4280 ------------------
4281 -- Remove_Items --
4282 ------------------
4284 procedure Remove_Items (List : List_Id) is
4285 Item : Node_Id;
4286 Item_Id : Node_Id;
4287 Next_Item : Node_Id;
4289 begin
4290 -- Traverse the list looking for an aspect specification or a pragma
4292 Item := First (List);
4293 while Present (Item) loop
4294 Next_Item := Next (Item);
4296 if Nkind (Item) = N_Aspect_Specification then
4297 Item_Id := Identifier (Item);
4298 elsif Nkind (Item) = N_Pragma then
4299 Item_Id := Pragma_Identifier (Item);
4300 else
4301 Item_Id := Empty;
4302 end if;
4304 if Present (Item_Id)
4305 and then Nam_In (Chars (Item_Id), Name_Contract_Cases,
4306 Name_Global,
4307 Name_Depends,
4308 Name_Postcondition,
4309 Name_Precondition,
4310 Name_Refined_Global,
4311 Name_Refined_Depends,
4312 Name_Refined_Post,
4313 Name_Test_Case,
4314 Name_Unmodified,
4315 Name_Unreferenced,
4316 Name_Unused)
4317 then
4318 Remove (Item);
4319 end if;
4321 Item := Next_Item;
4322 end loop;
4323 end Remove_Items;
4325 -- Start of processing for Remove_Aspects_And_Pragmas
4327 begin
4328 Remove_Items (Aspect_Specifications (Body_Decl));
4329 Remove_Items (Declarations (Body_Decl));
4331 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
4332 -- in the body of the subprogram.
4334 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
4335 end Remove_Aspects_And_Pragmas;
4337 --------------------------
4338 -- Remove_Dead_Instance --
4339 --------------------------
4341 procedure Remove_Dead_Instance (N : Node_Id) is
4342 J : Int;
4344 begin
4345 J := 0;
4346 while J <= Pending_Instantiations.Last loop
4347 if Pending_Instantiations.Table (J).Inst_Node = N then
4348 Pending_Instantiations.Table (J).Inst_Node := Empty;
4349 return;
4350 end if;
4352 J := J + 1;
4353 end loop;
4354 end Remove_Dead_Instance;
4356 end Inline;