1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
30 with Errout
; use Errout
;
31 with Expander
; use Expander
;
32 with Exp_Ch6
; use Exp_Ch6
;
33 with Exp_Ch7
; use Exp_Ch7
;
34 with Exp_Tss
; use Exp_Tss
;
35 with Exp_Util
; use Exp_Util
;
36 with Fname
; use Fname
;
37 with Fname
.UF
; use Fname
.UF
;
39 with Namet
; use Namet
;
40 with Nmake
; use Nmake
;
41 with Nlists
; use Nlists
;
42 with Output
; use Output
;
43 with Sem_Aux
; use Sem_Aux
;
44 with Sem_Ch8
; use Sem_Ch8
;
45 with Sem_Ch10
; use Sem_Ch10
;
46 with Sem_Ch12
; use Sem_Ch12
;
47 with Sem_Prag
; use Sem_Prag
;
48 with Sem_Util
; use Sem_Util
;
49 with Sinfo
; use Sinfo
;
50 with Sinput
; use Sinput
;
51 with Snames
; use Snames
;
52 with Stand
; use Stand
;
53 with Uname
; use Uname
;
54 with Tbuild
; use Tbuild
;
56 package body Inline
is
58 Check_Inlining_Restrictions
: constant Boolean := True;
59 -- In the following cases the frontend rejects inlining because they
60 -- are not handled well by the backend. This variable facilitates
61 -- disabling these restrictions to evaluate future versions of the
62 -- GCC backend in which some of the restrictions may be supported.
64 -- - subprograms that have:
65 -- - nested subprograms
67 -- - package declarations
68 -- - task or protected object declarations
69 -- - some of the following statements:
71 -- - asynchronous-select
72 -- - conditional-entry-call
78 Inlined_Calls
: Elist_Id
;
79 -- List of frontend inlined calls
81 Backend_Calls
: Elist_Id
;
82 -- List of inline calls passed to the backend
84 Backend_Inlined_Subps
: Elist_Id
;
85 -- List of subprograms inlined by the backend
87 Backend_Not_Inlined_Subps
: Elist_Id
;
88 -- List of subprograms that cannot be inlined by the backend
94 -- Inlined functions are actually placed in line by the backend if the
95 -- corresponding bodies are available (i.e. compiled). Whenever we find
96 -- a call to an inlined subprogram, we add the name of the enclosing
97 -- compilation unit to a worklist. After all compilation, and after
98 -- expansion of generic bodies, we traverse the list of pending bodies
99 -- and compile them as well.
101 package Inlined_Bodies
is new Table
.Table
(
102 Table_Component_Type
=> Entity_Id
,
103 Table_Index_Type
=> Int
,
104 Table_Low_Bound
=> 0,
105 Table_Initial
=> Alloc
.Inlined_Bodies_Initial
,
106 Table_Increment
=> Alloc
.Inlined_Bodies_Increment
,
107 Table_Name
=> "Inlined_Bodies");
109 -----------------------
110 -- Inline Processing --
111 -----------------------
113 -- For each call to an inlined subprogram, we make entries in a table
114 -- that stores caller and callee, and indicates the call direction from
115 -- one to the other. We also record the compilation unit that contains
116 -- the callee. After analyzing the bodies of all such compilation units,
117 -- we compute the transitive closure of inlined subprograms called from
118 -- the main compilation unit and make it available to the code generator
119 -- in no particular order, thus allowing cycles in the call graph.
121 Last_Inlined
: Entity_Id
:= Empty
;
123 -- For each entry in the table we keep a list of successors in topological
124 -- order, i.e. callers of the current subprogram.
126 type Subp_Index
is new Nat
;
127 No_Subp
: constant Subp_Index
:= 0;
129 -- The subprogram entities are hashed into the Inlined table
131 Num_Hash_Headers
: constant := 512;
133 Hash_Headers
: array (Subp_Index
range 0 .. Num_Hash_Headers
- 1)
136 type Succ_Index
is new Nat
;
137 No_Succ
: constant Succ_Index
:= 0;
139 type Succ_Info
is record
144 -- The following table stores list elements for the successor lists. These
145 -- lists cannot be chained directly through entries in the Inlined table,
146 -- because a given subprogram can appear in several such lists.
148 package Successors
is new Table
.Table
(
149 Table_Component_Type
=> Succ_Info
,
150 Table_Index_Type
=> Succ_Index
,
151 Table_Low_Bound
=> 1,
152 Table_Initial
=> Alloc
.Successors_Initial
,
153 Table_Increment
=> Alloc
.Successors_Increment
,
154 Table_Name
=> "Successors");
156 type Subp_Info
is record
157 Name
: Entity_Id
:= Empty
;
158 Next
: Subp_Index
:= No_Subp
;
159 First_Succ
: Succ_Index
:= No_Succ
;
160 Listed
: Boolean := False;
161 Main_Call
: Boolean := False;
162 Processed
: Boolean := False;
165 package Inlined
is new Table
.Table
(
166 Table_Component_Type
=> Subp_Info
,
167 Table_Index_Type
=> Subp_Index
,
168 Table_Low_Bound
=> 1,
169 Table_Initial
=> Alloc
.Inlined_Initial
,
170 Table_Increment
=> Alloc
.Inlined_Increment
,
171 Table_Name
=> "Inlined");
173 -----------------------
174 -- Local Subprograms --
175 -----------------------
177 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
);
178 -- Make two entries in Inlined table, for an inlined subprogram being
179 -- called, and for the inlined subprogram that contains the call. If
180 -- the call is in the main compilation unit, Caller is Empty.
182 procedure Add_Inlined_Subprogram
(Index
: Subp_Index
);
183 -- Add the subprogram to the list of inlined subprogram for the unit
185 function Add_Subp
(E
: Entity_Id
) return Subp_Index
;
186 -- Make entry in Inlined table for subprogram E, or return table index
187 -- that already holds E.
189 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
;
190 pragma Inline
(Get_Code_Unit_Entity
);
191 -- Return the entity node for the unit containing E. Always return the spec
194 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean;
195 -- If a candidate for inlining contains type declarations for types with
196 -- non-trivial initialization procedures, they are not worth inlining.
198 function Has_Single_Return
(N
: Node_Id
) return Boolean;
199 -- In general we cannot inline functions that return unconstrained type.
200 -- However, we can handle such functions if all return statements return a
201 -- local variable that is the only declaration in the body of the function.
202 -- In that case the call can be replaced by that local variable as is done
203 -- for other inlined calls.
205 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean;
206 -- Return True if E is in the main unit or its spec or in a subunit
208 function Is_Nested
(E
: Entity_Id
) return Boolean;
209 -- If the function is nested inside some other function, it will always
210 -- be compiled if that function is, so don't add it to the inline list.
211 -- We cannot compile a nested function outside the scope of the containing
212 -- function anyway. This is also the case if the function is defined in a
213 -- task body or within an entry (for example, an initialization procedure).
215 procedure Remove_Pragmas
(Bod
: Node_Id
);
216 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
217 -- parameter has no meaning when the body is inlined and the formals
218 -- are rewritten. Remove it from body to inline. The analysis of the
219 -- non-inlined body will handle the pragma properly.
221 ------------------------------
222 -- Deferred Cleanup Actions --
223 ------------------------------
225 -- The cleanup actions for scopes that contain instantiations is delayed
226 -- until after expansion of those instantiations, because they may contain
227 -- finalizable objects or tasks that affect the cleanup code. A scope
228 -- that contains instantiations only needs to be finalized once, even
229 -- if it contains more than one instance. We keep a list of scopes
230 -- that must still be finalized, and call cleanup_actions after all
231 -- the instantiations have been completed.
235 procedure Add_Scope_To_Clean
(Inst
: Entity_Id
);
236 -- Build set of scopes on which cleanup actions must be performed
238 procedure Cleanup_Scopes
;
239 -- Complete cleanup actions on scopes that need it
245 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
) is
246 P1
: constant Subp_Index
:= Add_Subp
(Called
);
251 if Present
(Caller
) then
252 P2
:= Add_Subp
(Caller
);
254 -- Add P1 to the list of successors of P2, if not already there.
255 -- Note that P2 may contain more than one call to P1, and only
256 -- one needs to be recorded.
258 J
:= Inlined
.Table
(P2
).First_Succ
;
259 while J
/= No_Succ
loop
260 if Successors
.Table
(J
).Subp
= P1
then
264 J
:= Successors
.Table
(J
).Next
;
267 -- On exit, make a successor entry for P1
269 Successors
.Increment_Last
;
270 Successors
.Table
(Successors
.Last
).Subp
:= P1
;
271 Successors
.Table
(Successors
.Last
).Next
:=
272 Inlined
.Table
(P2
).First_Succ
;
273 Inlined
.Table
(P2
).First_Succ
:= Successors
.Last
;
275 Inlined
.Table
(P1
).Main_Call
:= True;
279 ----------------------
280 -- Add_Inlined_Body --
281 ----------------------
283 procedure Add_Inlined_Body
(E
: Entity_Id
) is
285 type Inline_Level_Type
is (Dont_Inline
, Inline_Call
, Inline_Package
);
286 -- Level of inlining for the call: Dont_Inline means no inlining,
287 -- Inline_Call means that only the call is considered for inlining,
288 -- Inline_Package means that the call is considered for inlining and
289 -- its package compiled and scanned for more inlining opportunities.
291 function Must_Inline
return Inline_Level_Type
;
292 -- Inlining is only done if the call statement N is in the main unit,
293 -- or within the body of another inlined subprogram.
299 function Must_Inline
return Inline_Level_Type
is
304 -- Check if call is in main unit
306 Scop
:= Current_Scope
;
308 -- Do not try to inline if scope is standard. This could happen, for
309 -- example, for a call to Add_Global_Declaration, and it causes
310 -- trouble to try to inline at this level.
312 if Scop
= Standard_Standard
then
316 -- Otherwise lookup scope stack to outer scope
318 while Scope
(Scop
) /= Standard_Standard
319 and then not Is_Child_Unit
(Scop
)
321 Scop
:= Scope
(Scop
);
324 Comp
:= Parent
(Scop
);
325 while Nkind
(Comp
) /= N_Compilation_Unit
loop
326 Comp
:= Parent
(Comp
);
329 -- If the call is in the main unit, inline the call and compile the
330 -- package of the subprogram to find more calls to be inlined.
332 if Comp
= Cunit
(Main_Unit
)
333 or else Comp
= Library_Unit
(Cunit
(Main_Unit
))
336 return Inline_Package
;
339 -- The call is not in the main unit. See if it is in some inlined
340 -- subprogram. If so, inline the call and, if the inlining level is
341 -- set to 1, stop there; otherwise also compile the package as above.
343 Scop
:= Current_Scope
;
344 while Scope
(Scop
) /= Standard_Standard
345 and then not Is_Child_Unit
(Scop
)
347 if Is_Overloadable
(Scop
) and then Is_Inlined
(Scop
) then
350 if Inline_Level
= 1 then
353 return Inline_Package
;
357 Scop
:= Scope
(Scop
);
363 Level
: Inline_Level_Type
;
365 -- Start of processing for Add_Inlined_Body
368 -- Find unit containing E, and add to list of inlined bodies if needed.
369 -- If the body is already present, no need to load any other unit. This
370 -- is the case for an initialization procedure, which appears in the
371 -- package declaration that contains the type. It is also the case if
372 -- the body has already been analyzed. Finally, if the unit enclosing
373 -- E is an instance, the instance body will be analyzed in any case,
374 -- and there is no need to add the enclosing unit (whose body might not
377 -- Library-level functions must be handled specially, because there is
378 -- no enclosing package to retrieve. In this case, it is the body of
379 -- the function that will have to be loaded.
381 if Is_Abstract_Subprogram
(E
)
382 or else Is_Nested
(E
)
383 or else Convention
(E
) = Convention_Protected
388 Level
:= Must_Inline
;
389 if Level
/= Dont_Inline
then
391 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
396 -- Library-level inlined function. Add function itself to
397 -- list of needed units.
400 Inlined_Bodies
.Increment_Last
;
401 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := E
;
403 elsif Ekind
(Pack
) = E_Package
then
406 if Is_Generic_Instance
(Pack
) then
409 -- Do not inline the package if the subprogram is an init proc
410 -- or other internally generated subprogram, because in that
411 -- case the subprogram body appears in the same unit that
412 -- declares the type, and that body is visible to the back end.
413 -- Do not inline it either if it is in the main unit.
415 elsif Level
= Inline_Package
416 and then not Is_Inlined
(Pack
)
417 and then not Is_Internal
(E
)
418 and then not In_Main_Unit_Or_Subunit
(Pack
)
420 Set_Is_Inlined
(Pack
);
421 Inlined_Bodies
.Increment_Last
;
422 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := Pack
;
424 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
425 -- calls if the back-end takes care of inlining the call.
427 elsif Level
= Inline_Call
428 and then Has_Pragma_Inline_Always
(E
)
429 and then Back_End_Inlining
431 Set_Is_Inlined
(Pack
);
432 Inlined_Bodies
.Increment_Last
;
433 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := Pack
;
438 end Add_Inlined_Body
;
440 ----------------------------
441 -- Add_Inlined_Subprogram --
442 ----------------------------
444 procedure Add_Inlined_Subprogram
(Index
: Subp_Index
) is
445 E
: constant Entity_Id
:= Inlined
.Table
(Index
).Name
;
446 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
448 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
);
449 -- Append Subp to the list of subprograms inlined by the backend
451 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
);
452 -- Append Subp to the list of subprograms that cannot be inlined by
455 -----------------------------------------
456 -- Register_Backend_Inlined_Subprogram --
457 -----------------------------------------
459 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
) is
461 Append_New_Elmt
(Subp
, To
=> Backend_Inlined_Subps
);
462 end Register_Backend_Inlined_Subprogram
;
464 ---------------------------------------------
465 -- Register_Backend_Not_Inlined_Subprogram --
466 ---------------------------------------------
468 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
) is
470 Append_New_Elmt
(Subp
, To
=> Backend_Not_Inlined_Subps
);
471 end Register_Backend_Not_Inlined_Subprogram
;
473 -- Start of processing for Add_Inlined_Subprogram
476 -- If the subprogram is to be inlined, and if its unit is known to be
477 -- inlined or is an instance whose body will be analyzed anyway or the
478 -- subprogram has been generated by the compiler, and if it is declared
479 -- at the library level not in the main unit, and if it can be inlined
480 -- by the back-end, then insert it in the list of inlined subprograms.
483 and then (Is_Inlined
(Pack
)
484 or else Is_Generic_Instance
(Pack
)
485 or else Is_Internal
(E
))
486 and then not In_Main_Unit_Or_Subunit
(E
)
487 and then not Is_Nested
(E
)
488 and then not Has_Initialized_Type
(E
)
490 Register_Backend_Inlined_Subprogram
(E
);
492 if No
(Last_Inlined
) then
493 Set_First_Inlined_Subprogram
(Cunit
(Main_Unit
), E
);
495 Set_Next_Inlined_Subprogram
(Last_Inlined
, E
);
501 Register_Backend_Not_Inlined_Subprogram
(E
);
504 Inlined
.Table
(Index
).Listed
:= True;
505 end Add_Inlined_Subprogram
;
507 ------------------------
508 -- Add_Scope_To_Clean --
509 ------------------------
511 procedure Add_Scope_To_Clean
(Inst
: Entity_Id
) is
512 Scop
: constant Entity_Id
:= Enclosing_Dynamic_Scope
(Inst
);
516 -- If the instance appears in a library-level package declaration,
517 -- all finalization is global, and nothing needs doing here.
519 if Scop
= Standard_Standard
then
523 -- If the instance is within a generic unit, no finalization code
524 -- can be generated. Note that at this point all bodies have been
525 -- analyzed, and the scope stack itself is not present, and the flag
526 -- Inside_A_Generic is not set.
533 while Present
(S
) and then S
/= Standard_Standard
loop
534 if Is_Generic_Unit
(S
) then
542 Elmt
:= First_Elmt
(To_Clean
);
543 while Present
(Elmt
) loop
544 if Node
(Elmt
) = Scop
then
548 Elmt
:= Next_Elmt
(Elmt
);
551 Append_Elmt
(Scop
, To_Clean
);
552 end Add_Scope_To_Clean
;
558 function Add_Subp
(E
: Entity_Id
) return Subp_Index
is
559 Index
: Subp_Index
:= Subp_Index
(E
) mod Num_Hash_Headers
;
563 -- Initialize entry in Inlined table
565 procedure New_Entry
is
567 Inlined
.Increment_Last
;
568 Inlined
.Table
(Inlined
.Last
).Name
:= E
;
569 Inlined
.Table
(Inlined
.Last
).Next
:= No_Subp
;
570 Inlined
.Table
(Inlined
.Last
).First_Succ
:= No_Succ
;
571 Inlined
.Table
(Inlined
.Last
).Listed
:= False;
572 Inlined
.Table
(Inlined
.Last
).Main_Call
:= False;
573 Inlined
.Table
(Inlined
.Last
).Processed
:= False;
576 -- Start of processing for Add_Subp
579 if Hash_Headers
(Index
) = No_Subp
then
581 Hash_Headers
(Index
) := Inlined
.Last
;
585 J
:= Hash_Headers
(Index
);
586 while J
/= No_Subp
loop
587 if Inlined
.Table
(J
).Name
= E
then
591 J
:= Inlined
.Table
(J
).Next
;
595 -- On exit, subprogram was not found. Enter in table. Index is
596 -- the current last entry on the hash chain.
599 Inlined
.Table
(Index
).Next
:= Inlined
.Last
;
604 ----------------------------
605 -- Analyze_Inlined_Bodies --
606 ----------------------------
608 procedure Analyze_Inlined_Bodies
is
615 type Pending_Index
is new Nat
;
617 package Pending_Inlined
is new Table
.Table
(
618 Table_Component_Type
=> Subp_Index
,
619 Table_Index_Type
=> Pending_Index
,
620 Table_Low_Bound
=> 1,
621 Table_Initial
=> Alloc
.Inlined_Initial
,
622 Table_Increment
=> Alloc
.Inlined_Increment
,
623 Table_Name
=> "Pending_Inlined");
624 -- The workpile used to compute the transitive closure
626 function Is_Ancestor_Of_Main
628 Nam
: Node_Id
) return Boolean;
629 -- Determine whether the unit whose body is loaded is an ancestor of
630 -- the main unit, and has a with_clause on it. The body is not
631 -- analyzed yet, so the check is purely lexical: the name of the with
632 -- clause is a selected component, and names of ancestors must match.
634 -------------------------
635 -- Is_Ancestor_Of_Main --
636 -------------------------
638 function Is_Ancestor_Of_Main
640 Nam
: Node_Id
) return Boolean
645 if Nkind
(Nam
) /= N_Selected_Component
then
649 if Chars
(Selector_Name
(Nam
)) /=
650 Chars
(Cunit_Entity
(Main_Unit
))
655 Pref
:= Prefix
(Nam
);
656 if Nkind
(Pref
) = N_Identifier
then
658 -- Par is an ancestor of Par.Child.
660 return Chars
(Pref
) = Chars
(U_Name
);
662 elsif Nkind
(Pref
) = N_Selected_Component
663 and then Chars
(Selector_Name
(Pref
)) = Chars
(U_Name
)
665 -- Par.Child is an ancestor of Par.Child.Grand.
667 return True; -- should check that ancestor match
670 -- A is an ancestor of A.B.C if it is an ancestor of A.B
672 return Is_Ancestor_Of_Main
(U_Name
, Pref
);
675 end Is_Ancestor_Of_Main
;
677 -- Start of processing for Analyze_Inlined_Bodies
680 if Serious_Errors_Detected
= 0 then
681 Push_Scope
(Standard_Standard
);
684 while J
<= Inlined_Bodies
.Last
685 and then Serious_Errors_Detected
= 0
687 Pack
:= Inlined_Bodies
.Table
(J
);
689 and then Scope
(Pack
) /= Standard_Standard
690 and then not Is_Child_Unit
(Pack
)
692 Pack
:= Scope
(Pack
);
695 Comp_Unit
:= Parent
(Pack
);
696 while Present
(Comp_Unit
)
697 and then Nkind
(Comp_Unit
) /= N_Compilation_Unit
699 Comp_Unit
:= Parent
(Comp_Unit
);
702 -- Load the body, unless it is the main unit, or is an instance
703 -- whose body has already been analyzed.
705 if Present
(Comp_Unit
)
706 and then Comp_Unit
/= Cunit
(Main_Unit
)
707 and then Body_Required
(Comp_Unit
)
708 and then (Nkind
(Unit
(Comp_Unit
)) /= N_Package_Declaration
709 or else No
(Corresponding_Body
(Unit
(Comp_Unit
))))
712 Bname
: constant Unit_Name_Type
:=
713 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
718 if not Is_Loaded
(Bname
) then
719 Style_Check
:= False;
720 Load_Needed_Body
(Comp_Unit
, OK
, Do_Analyze
=> False);
724 -- Warn that a body was not available for inlining
727 Error_Msg_Unit_1
:= Bname
;
729 ("one or more inlined subprograms accessed in $!??",
732 Get_File_Name
(Bname
, Subunit
=> False);
733 Error_Msg_N
("\but file{ was not found!??", Comp_Unit
);
736 -- If the package to be inlined is an ancestor unit of
737 -- the main unit, and it has a semantic dependence on
738 -- it, the inlining cannot take place to prevent an
739 -- elaboration circularity. The desired body is not
740 -- analyzed yet, to prevent the completion of Taft
741 -- amendment types that would lead to elaboration
742 -- circularities in gigi.
745 U_Id
: constant Entity_Id
:=
746 Defining_Entity
(Unit
(Comp_Unit
));
747 Body_Unit
: constant Node_Id
:=
748 Library_Unit
(Comp_Unit
);
752 Item
:= First
(Context_Items
(Body_Unit
));
753 while Present
(Item
) loop
754 if Nkind
(Item
) = N_With_Clause
756 Is_Ancestor_Of_Main
(U_Id
, Name
(Item
))
758 Set_Is_Inlined
(U_Id
, False);
765 -- If no suspicious with_clauses, analyze the body.
767 if Is_Inlined
(U_Id
) then
768 Semantics
(Body_Unit
);
779 -- The analysis of required bodies may have produced additional
780 -- generic instantiations. To obtain further inlining, we perform
781 -- another round of generic body instantiations. Establishing a
782 -- fully recursive loop between inlining and generic instantiations
783 -- is unlikely to yield more than this one additional pass.
787 -- The list of inlined subprograms is an overestimate, because it
788 -- includes inlined functions called from functions that are compiled
789 -- as part of an inlined package, but are not themselves called. An
790 -- accurate computation of just those subprograms that are needed
791 -- requires that we perform a transitive closure over the call graph,
792 -- starting from calls in the main program.
794 for Index
in Inlined
.First
.. Inlined
.Last
loop
795 if not Is_Called
(Inlined
.Table
(Index
).Name
) then
797 -- This means that Add_Inlined_Body added the subprogram to the
798 -- table but wasn't able to handle its code unit. Do nothing.
800 Inlined
.Table
(Index
).Processed
:= True;
802 elsif Inlined
.Table
(Index
).Main_Call
then
803 Pending_Inlined
.Increment_Last
;
804 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Index
;
805 Inlined
.Table
(Index
).Processed
:= True;
808 Set_Is_Called
(Inlined
.Table
(Index
).Name
, False);
812 -- Iterate over the workpile until it is emptied, propagating the
813 -- Is_Called flag to the successors of the processed subprogram.
815 while Pending_Inlined
.Last
>= Pending_Inlined
.First
loop
816 Subp
:= Pending_Inlined
.Table
(Pending_Inlined
.Last
);
817 Pending_Inlined
.Decrement_Last
;
819 S
:= Inlined
.Table
(Subp
).First_Succ
;
821 while S
/= No_Succ
loop
822 Subp
:= Successors
.Table
(S
).Subp
;
824 if not Inlined
.Table
(Subp
).Processed
then
825 Set_Is_Called
(Inlined
.Table
(Subp
).Name
);
826 Pending_Inlined
.Increment_Last
;
827 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Subp
;
828 Inlined
.Table
(Subp
).Processed
:= True;
831 S
:= Successors
.Table
(S
).Next
;
835 -- Finally add the called subprograms to the list of inlined
836 -- subprograms for the unit.
838 for Index
in Inlined
.First
.. Inlined
.Last
loop
839 if Is_Called
(Inlined
.Table
(Index
).Name
)
840 and then not Inlined
.Table
(Index
).Listed
842 Add_Inlined_Subprogram
(Index
);
848 end Analyze_Inlined_Bodies
;
850 --------------------------
851 -- Build_Body_To_Inline --
852 --------------------------
854 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
855 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
856 Analysis_Status
: constant Boolean := Full_Analysis
;
857 Original_Body
: Node_Id
;
858 Body_To_Analyze
: Node_Id
;
859 Max_Size
: constant := 10;
861 function Has_Pending_Instantiation
return Boolean;
862 -- If some enclosing body contains instantiations that appear before
863 -- the corresponding generic body, the enclosing body has a freeze node
864 -- so that it can be elaborated after the generic itself. This might
865 -- conflict with subsequent inlinings, so that it is unsafe to try to
866 -- inline in such a case.
868 function Has_Single_Return_In_GNATprove_Mode
return Boolean;
869 -- This function is called only in GNATprove mode, and it returns
870 -- True if the subprogram has no return statement or a single return
871 -- statement as last statement. It returns False for subprogram with
872 -- a single return as last statement inside one or more blocks, as
873 -- inlining would generate gotos in that case as well (although the
874 -- goto is useless in that case).
876 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
877 -- If the body of the subprogram includes a call that returns an
878 -- unconstrained type, the secondary stack is involved, and it
879 -- is not worth inlining.
881 -------------------------------
882 -- Has_Pending_Instantiation --
883 -------------------------------
885 function Has_Pending_Instantiation
return Boolean is
890 while Present
(S
) loop
891 if Is_Compilation_Unit
(S
)
892 or else Is_Child_Unit
(S
)
896 elsif Ekind
(S
) = E_Package
897 and then Has_Forward_Instantiation
(S
)
906 end Has_Pending_Instantiation
;
908 -----------------------------------------
909 -- Has_Single_Return_In_GNATprove_Mode --
910 -----------------------------------------
912 function Has_Single_Return_In_GNATprove_Mode
return Boolean is
913 Last_Statement
: Node_Id
:= Empty
;
915 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
916 -- Returns OK on node N if this is not a return statement different
917 -- from the last statement in the subprogram.
923 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
925 if Nkind_In
(N
, N_Simple_Return_Statement
,
926 N_Extended_Return_Statement
)
928 if N
= Last_Statement
then
939 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
941 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
944 -- Retrieve the last statement
946 Last_Statement
:= Last
(Statements
(Handled_Statement_Sequence
(N
)));
948 -- Check that the last statement is the only possible return
949 -- statement in the subprogram.
951 return Check_All_Returns
(N
) = OK
;
952 end Has_Single_Return_In_GNATprove_Mode
;
954 --------------------------
955 -- Uses_Secondary_Stack --
956 --------------------------
958 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
959 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
960 -- Look for function calls that return an unconstrained type
966 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
968 if Nkind
(N
) = N_Function_Call
969 and then Is_Entity_Name
(Name
(N
))
970 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
971 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
974 ("cannot inline & (call returns unconstrained type)?",
982 function Check_Calls
is new Traverse_Func
(Check_Call
);
985 return Check_Calls
(Bod
) = Abandon
;
986 end Uses_Secondary_Stack
;
988 -- Start of processing for Build_Body_To_Inline
991 -- Return immediately if done already
993 if Nkind
(Decl
) = N_Subprogram_Declaration
994 and then Present
(Body_To_Inline
(Decl
))
998 -- Subprograms that have return statements in the middle of the body are
999 -- inlined with gotos. GNATprove does not currently support gotos, so
1000 -- we prevent such inlining.
1002 elsif GNATprove_Mode
1003 and then not Has_Single_Return_In_GNATprove_Mode
1005 Cannot_Inline
("cannot inline & (multiple returns)?", N
, Spec_Id
);
1008 -- Functions that return unconstrained composite types require
1009 -- secondary stack handling, and cannot currently be inlined, unless
1010 -- all return statements return a local variable that is the first
1011 -- local declaration in the body.
1013 elsif Ekind
(Spec_Id
) = E_Function
1014 and then not Is_Scalar_Type
(Etype
(Spec_Id
))
1015 and then not Is_Access_Type
(Etype
(Spec_Id
))
1016 and then not Is_Constrained
(Etype
(Spec_Id
))
1018 if not Has_Single_Return
(N
) then
1020 ("cannot inline & (unconstrained return type)?", N
, Spec_Id
);
1024 -- Ditto for functions that return controlled types, where controlled
1025 -- actions interfere in complex ways with inlining.
1027 elsif Ekind
(Spec_Id
) = E_Function
1028 and then Needs_Finalization
(Etype
(Spec_Id
))
1031 ("cannot inline & (controlled return type)?", N
, Spec_Id
);
1035 if Present
(Declarations
(N
))
1036 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
1041 if Present
(Handled_Statement_Sequence
(N
)) then
1042 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
1044 ("cannot inline& (exception handler)?",
1045 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
1049 elsif Has_Excluded_Statement
1050 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
1056 -- We do not inline a subprogram that is too large, unless it is marked
1057 -- Inline_Always or we are in GNATprove mode. This pragma does not
1058 -- suppress the other checks on inlining (forbidden declarations,
1061 if not (Has_Pragma_Inline_Always
(Spec_Id
) or else GNATprove_Mode
)
1062 and then List_Length
1063 (Statements
(Handled_Statement_Sequence
(N
))) > Max_Size
1065 Cannot_Inline
("cannot inline& (body too large)?", N
, Spec_Id
);
1069 if Has_Pending_Instantiation
then
1071 ("cannot inline& (forward instance within enclosing body)?",
1076 -- Within an instance, the body to inline must be treated as a nested
1077 -- generic, so that the proper global references are preserved.
1079 -- Note that we do not do this at the library level, because it is not
1080 -- needed, and furthermore this causes trouble if front end inlining
1081 -- is activated (-gnatN).
1083 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1084 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1085 Original_Body
:= Copy_Generic_Node
(N
, Empty
, True);
1087 Original_Body
:= Copy_Separate_Tree
(N
);
1090 -- We need to capture references to the formals in order to substitute
1091 -- the actuals at the point of inlining, i.e. instantiation. To treat
1092 -- the formals as globals to the body to inline, we nest it within a
1093 -- dummy parameterless subprogram, declared within the real one. To
1094 -- avoid generating an internal name (which is never public, and which
1095 -- affects serial numbers of other generated names), we use an internal
1096 -- symbol that cannot conflict with user declarations.
1098 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
1099 Set_Defining_Unit_Name
1100 (Specification
(Original_Body
),
1101 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
1102 Set_Corresponding_Spec
(Original_Body
, Empty
);
1104 -- Remove those pragmas that have no meaining in an inlined body.
1106 Remove_Pragmas
(Original_Body
);
1108 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
1110 -- Set return type of function, which is also global and does not need
1113 if Ekind
(Spec_Id
) = E_Function
then
1114 Set_Result_Definition
(Specification
(Body_To_Analyze
),
1115 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1118 if No
(Declarations
(N
)) then
1119 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1121 Append
(Body_To_Analyze
, Declarations
(N
));
1124 -- The body to inline is pre-analyzed. In GNATprove mode we must
1125 -- disable full analysis as well so that light expansion does not
1126 -- take place either, and name resolution is unaffected.
1128 Expander_Mode_Save_And_Set
(False);
1129 Full_Analysis
:= False;
1131 Analyze
(Body_To_Analyze
);
1132 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1133 Save_Global_References
(Original_Body
);
1135 Remove
(Body_To_Analyze
);
1137 Expander_Mode_Restore
;
1138 Full_Analysis
:= Analysis_Status
;
1140 -- Restore environment if previously saved
1142 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1146 -- If secondary stack is used, there is no point in inlining. We have
1147 -- already issued the warning in this case, so nothing to do.
1149 if Uses_Secondary_Stack
(Body_To_Analyze
) then
1153 Set_Body_To_Inline
(Decl
, Original_Body
);
1154 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1155 Set_Is_Inlined
(Spec_Id
);
1156 end Build_Body_To_Inline
;
1162 procedure Cannot_Inline
1166 Is_Serious
: Boolean := False)
1169 -- In GNATprove mode, inlining is the technical means by which the
1170 -- higher-level goal of contextual analysis is reached, so issue
1171 -- messages about failure to apply contextual analysis to a
1172 -- subprogram, rather than failure to inline it.
1175 and then Msg
(Msg
'First .. Msg
'First + 12) = "cannot inline"
1178 Len1
: constant Positive :=
1179 String (String'("cannot inline"))'Length;
1180 Len2 : constant Positive :=
1181 String (String'("info: no contextual analysis of"))'Length;
1183 New_Msg
: String (1 .. Msg
'Length + Len2
- Len1
);
1186 New_Msg
(1 .. Len2
) := "info: no contextual analysis of";
1187 New_Msg
(Len2
+ 1 .. Msg
'Length + Len2
- Len1
) :=
1188 Msg
(Msg
'First + Len1
.. Msg
'Last);
1189 Cannot_Inline
(New_Msg
, N
, Subp
, Is_Serious
);
1194 pragma Assert
(Msg
(Msg
'Last) = '?');
1196 -- Legacy front end inlining model
1198 if not Back_End_Inlining
then
1200 -- Do not emit warning if this is a predefined unit which is not
1201 -- the main unit. With validity checks enabled, some predefined
1202 -- subprograms may contain nested subprograms and become ineligible
1205 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
1206 and then not In_Extended_Main_Source_Unit
(Subp
)
1210 -- In GNATprove mode, issue a warning, and indicate that the
1211 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1214 elsif GNATprove_Mode
then
1215 Set_Is_Inlined_Always
(Subp
, False);
1216 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1218 elsif Has_Pragma_Inline_Always
(Subp
) then
1220 -- Remove last character (question mark) to make this into an
1221 -- error, because the Inline_Always pragma cannot be obeyed.
1223 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1225 elsif Ineffective_Inline_Warnings
then
1226 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1229 -- New semantics relying on back end inlining
1231 elsif Is_Serious
then
1233 -- Remove last character (question mark) to make this into an error.
1235 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1237 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1238 -- is not always inlined by setting flag Is_Inlined_Always to False.
1240 elsif GNATprove_Mode
then
1241 Set_Is_Inlined_Always
(Subp
, False);
1242 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1246 -- Do not emit warning if this is a predefined unit which is not
1247 -- the main unit. This behavior is currently provided for backward
1248 -- compatibility but it will be removed when we enforce the
1249 -- strictness of the new rules.
1251 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
1252 and then not In_Extended_Main_Source_Unit
(Subp
)
1256 elsif Has_Pragma_Inline_Always
(Subp
) then
1258 -- Emit a warning if this is a call to a runtime subprogram
1259 -- which is located inside a generic. Previously this call
1260 -- was silently skipped.
1262 if Is_Generic_Instance
(Subp
) then
1264 Gen_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Subp
));
1266 if Is_Predefined_File_Name
1267 (Unit_File_Name
(Get_Source_Unit
(Gen_P
)))
1269 Set_Is_Inlined
(Subp
, False);
1270 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1276 -- Remove last character (question mark) to make this into an
1277 -- error, because the Inline_Always pragma cannot be obeyed.
1279 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1282 Set_Is_Inlined
(Subp
, False);
1284 if Ineffective_Inline_Warnings
then
1285 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1291 --------------------------------------
1292 -- Can_Be_Inlined_In_GNATprove_Mode --
1293 --------------------------------------
1295 function Can_Be_Inlined_In_GNATprove_Mode
1296 (Spec_Id
: Entity_Id
;
1297 Body_Id
: Entity_Id
) return Boolean
1299 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean;
1300 -- Returns True if subprogram Id has any contract (Pre, Post, Global,
1303 function Is_Unit_Subprogram
(Id
: Entity_Id
) return Boolean;
1304 -- Returns True if subprogram Id defines a compilation unit
1305 -- Shouldn't this be in Sem_Aux???
1307 function In_Package_Visible_Spec
(Id
: Node_Id
) return Boolean;
1308 -- Returns True if subprogram Id is defined in the visible part of a
1309 -- package specification.
1311 function Is_Expression_Function
(Id
: Entity_Id
) return Boolean;
1312 -- Returns True if subprogram Id was defined originally as an expression
1315 -----------------------
1316 -- Has_Some_Contract --
1317 -----------------------
1319 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean is
1323 -- A call to an expression function may precede the actual body which
1324 -- is inserted at the end of the enclosing declarations. Ensure that
1325 -- the related entity is analyzed before inspecting the contract.
1327 if Analyzed
(Id
) then
1328 Items
:= Contract
(Id
);
1330 return Present
(Items
)
1331 and then (Present
(Pre_Post_Conditions
(Items
)) or else
1332 Present
(Contract_Test_Cases
(Items
)) or else
1333 Present
(Classifications
(Items
)));
1337 end Has_Some_Contract
;
1339 -----------------------------
1340 -- In_Package_Visible_Spec --
1341 -----------------------------
1343 function In_Package_Visible_Spec
(Id
: Node_Id
) return Boolean is
1344 Decl
: Node_Id
:= Parent
(Parent
(Id
));
1348 if Nkind
(Parent
(Id
)) = N_Defining_Program_Unit_Name
then
1349 Decl
:= Parent
(Decl
);
1354 return Nkind
(P
) = N_Package_Specification
1355 and then List_Containing
(Decl
) = Visible_Declarations
(P
);
1356 end In_Package_Visible_Spec
;
1358 ----------------------------
1359 -- Is_Expression_Function --
1360 ----------------------------
1362 function Is_Expression_Function
(Id
: Entity_Id
) return Boolean is
1363 Decl
: Node_Id
:= Parent
(Parent
(Id
));
1365 if Nkind
(Parent
(Id
)) = N_Defining_Program_Unit_Name
then
1366 Decl
:= Parent
(Decl
);
1369 return Nkind
(Original_Node
(Decl
)) = N_Expression_Function
;
1370 end Is_Expression_Function
;
1372 ------------------------
1373 -- Is_Unit_Subprogram --
1374 ------------------------
1376 function Is_Unit_Subprogram
(Id
: Entity_Id
) return Boolean is
1377 Decl
: Node_Id
:= Parent
(Parent
(Id
));
1379 if Nkind
(Parent
(Id
)) = N_Defining_Program_Unit_Name
then
1380 Decl
:= Parent
(Decl
);
1383 return Nkind
(Parent
(Decl
)) = N_Compilation_Unit
;
1384 end Is_Unit_Subprogram
;
1386 -- Local declarations
1388 Id
: Entity_Id
; -- Procedure or function entity for the subprogram
1390 -- Start of Can_Be_Inlined_In_GNATprove_Mode
1393 pragma Assert
(Present
(Spec_Id
) or else Present
(Body_Id
));
1395 if Present
(Spec_Id
) then
1401 -- Only local subprograms without contracts are inlined in GNATprove
1402 -- mode, as these are the subprograms which a user is not interested in
1403 -- analyzing in isolation, but rather in the context of their call. This
1404 -- is a convenient convention, that could be changed for an explicit
1405 -- pragma/aspect one day.
1407 -- In a number of special cases, inlining is not desirable or not
1408 -- possible, see below.
1410 -- Do not inline unit-level subprograms
1412 if Is_Unit_Subprogram
(Id
) then
1415 -- Do not inline subprograms declared in the visible part of a package
1417 elsif In_Package_Visible_Spec
(Id
) then
1420 -- Do not inline subprograms that have a contract on the spec or the
1421 -- body. Use the contract(s) instead in GNATprove.
1423 elsif (Present
(Spec_Id
) and then Has_Some_Contract
(Spec_Id
))
1425 (Present
(Body_Id
) and then Has_Some_Contract
(Body_Id
))
1429 -- Do not inline expression functions, which are directly inlined at the
1432 elsif (Present
(Spec_Id
) and then Is_Expression_Function
(Spec_Id
))
1434 (Present
(Body_Id
) and then Is_Expression_Function
(Body_Id
))
1438 -- Do not inline generic subprogram instances. The visibility rules of
1439 -- generic instances plays badly with inlining.
1441 elsif Is_Generic_Instance
(Spec_Id
) then
1444 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1445 -- the subprogram body, a similar check is performed after the body
1446 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1448 elsif Present
(Spec_Id
)
1450 (No
(SPARK_Pragma
(Spec_Id
))
1451 or else Get_SPARK_Mode_From_Pragma
(SPARK_Pragma
(Spec_Id
)) /= On
)
1455 -- Subprograms in generic instances are currently not inlined, to avoid
1456 -- problems with inlining of standard library subprograms.
1458 elsif Instantiation_Location
(Sloc
(Id
)) /= No_Location
then
1461 -- Don't inline predicate functions (treated specially by GNATprove)
1463 elsif Is_Predicate_Function
(Id
) then
1466 -- Otherwise, this is a subprogram declared inside the private part of a
1467 -- package, or inside a package body, or locally in a subprogram, and it
1468 -- does not have any contract. Inline it.
1473 end Can_Be_Inlined_In_GNATprove_Mode
;
1475 --------------------------------------------
1476 -- Check_And_Split_Unconstrained_Function --
1477 --------------------------------------------
1479 procedure Check_And_Split_Unconstrained_Function
1481 Spec_Id
: Entity_Id
;
1482 Body_Id
: Entity_Id
)
1484 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
1485 -- Use generic machinery to build an unexpanded body for the subprogram.
1486 -- This body is subsequently used for inline expansions at call sites.
1488 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean;
1489 -- Return true if we generate code for the function body N, the function
1490 -- body N has no local declarations and its unique statement is a single
1491 -- extended return statement with a handled statements sequence.
1493 procedure Generate_Subprogram_Body
1495 Body_To_Inline
: out Node_Id
);
1496 -- Generate a parameterless duplicate of subprogram body N. Occurrences
1497 -- of pragmas referencing the formals are removed since they have no
1498 -- meaning when the body is inlined and the formals are rewritten (the
1499 -- analysis of the non-inlined body will handle these pragmas properly).
1500 -- A new internal name is associated with Body_To_Inline.
1502 procedure Split_Unconstrained_Function
1504 Spec_Id
: Entity_Id
);
1505 -- N is an inlined function body that returns an unconstrained type and
1506 -- has a single extended return statement. Split N in two subprograms:
1507 -- a procedure P' and a function F'. The formals of P' duplicate the
1508 -- formals of N plus an extra formal which is used return a value;
1509 -- its body is composed by the declarations and list of statements
1510 -- of the extended return statement of N.
1512 --------------------------
1513 -- Build_Body_To_Inline --
1514 --------------------------
1516 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
1517 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1518 Original_Body
: Node_Id
;
1519 Body_To_Analyze
: Node_Id
;
1522 pragma Assert
(Current_Scope
= Spec_Id
);
1524 -- Within an instance, the body to inline must be treated as a nested
1525 -- generic, so that the proper global references are preserved. We
1526 -- do not do this at the library level, because it is not needed, and
1527 -- furthermore this causes trouble if front end inlining is activated
1531 and then Scope
(Current_Scope
) /= Standard_Standard
1533 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1536 -- We need to capture references to the formals in order
1537 -- to substitute the actuals at the point of inlining, i.e.
1538 -- instantiation. To treat the formals as globals to the body to
1539 -- inline, we nest it within a dummy parameterless subprogram,
1540 -- declared within the real one.
1542 Generate_Subprogram_Body
(N
, Original_Body
);
1543 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
1545 -- Set return type of function, which is also global and does not
1546 -- need to be resolved.
1548 if Ekind
(Spec_Id
) = E_Function
then
1549 Set_Result_Definition
(Specification
(Body_To_Analyze
),
1550 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1553 if No
(Declarations
(N
)) then
1554 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1556 Append_To
(Declarations
(N
), Body_To_Analyze
);
1559 Preanalyze
(Body_To_Analyze
);
1561 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1562 Save_Global_References
(Original_Body
);
1564 Remove
(Body_To_Analyze
);
1566 -- Restore environment if previously saved
1569 and then Scope
(Current_Scope
) /= Standard_Standard
1574 pragma Assert
(No
(Body_To_Inline
(Decl
)));
1575 Set_Body_To_Inline
(Decl
, Original_Body
);
1576 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1577 end Build_Body_To_Inline
;
1579 --------------------------------------
1580 -- Can_Split_Unconstrained_Function --
1581 --------------------------------------
1583 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean
1585 Ret_Node
: constant Node_Id
:=
1586 First
(Statements
(Handled_Statement_Sequence
(N
)));
1590 -- No user defined declarations allowed in the function except inside
1591 -- the unique return statement; implicit labels are the only allowed
1594 if not Is_Empty_List
(Declarations
(N
)) then
1595 D
:= First
(Declarations
(N
));
1596 while Present
(D
) loop
1597 if Nkind
(D
) /= N_Implicit_Label_Declaration
then
1605 -- We only split the inlined function when we are generating the code
1606 -- of its body; otherwise we leave duplicated split subprograms in
1607 -- the tree which (if referenced) generate wrong references at link
1610 return In_Extended_Main_Code_Unit
(N
)
1611 and then Present
(Ret_Node
)
1612 and then Nkind
(Ret_Node
) = N_Extended_Return_Statement
1613 and then No
(Next
(Ret_Node
))
1614 and then Present
(Handled_Statement_Sequence
(Ret_Node
));
1615 end Can_Split_Unconstrained_Function
;
1617 -----------------------------
1618 -- Generate_Body_To_Inline --
1619 -----------------------------
1621 procedure Generate_Subprogram_Body
1623 Body_To_Inline
: out Node_Id
)
1626 -- Within an instance, the body to inline must be treated as a nested
1627 -- generic, so that the proper global references are preserved.
1629 -- Note that we do not do this at the library level, because it
1630 -- is not needed, and furthermore this causes trouble if front
1631 -- end inlining is activated (-gnatN).
1634 and then Scope
(Current_Scope
) /= Standard_Standard
1636 Body_To_Inline
:= Copy_Generic_Node
(N
, Empty
, True);
1638 Body_To_Inline
:= Copy_Separate_Tree
(N
);
1641 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
1642 -- parameter has no meaning when the body is inlined and the formals
1643 -- are rewritten. Remove it from body to inline. The analysis of the
1644 -- non-inlined body will handle the pragma properly.
1646 Remove_Pragmas
(Body_To_Inline
);
1648 -- We need to capture references to the formals in order
1649 -- to substitute the actuals at the point of inlining, i.e.
1650 -- instantiation. To treat the formals as globals to the body to
1651 -- inline, we nest it within a dummy parameterless subprogram,
1652 -- declared within the real one.
1654 Set_Parameter_Specifications
1655 (Specification
(Body_To_Inline
), No_List
);
1657 -- A new internal name is associated with Body_To_Inline to avoid
1658 -- conflicts when the non-inlined body N is analyzed.
1660 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
1661 Make_Defining_Identifier
(Sloc
(N
), New_Internal_Name
('P')));
1662 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
1663 end Generate_Subprogram_Body
;
1665 ----------------------------------
1666 -- Split_Unconstrained_Function --
1667 ----------------------------------
1669 procedure Split_Unconstrained_Function
1671 Spec_Id
: Entity_Id
)
1673 Loc
: constant Source_Ptr
:= Sloc
(N
);
1674 Ret_Node
: constant Node_Id
:=
1675 First
(Statements
(Handled_Statement_Sequence
(N
)));
1676 Ret_Obj
: constant Node_Id
:=
1677 First
(Return_Object_Declarations
(Ret_Node
));
1679 procedure Build_Procedure
1680 (Proc_Id
: out Entity_Id
;
1681 Decl_List
: out List_Id
);
1682 -- Build a procedure containing the statements found in the extended
1683 -- return statement of the unconstrained function body N.
1685 ---------------------
1686 -- Build_Procedure --
1687 ---------------------
1689 procedure Build_Procedure
1690 (Proc_Id
: out Entity_Id
;
1691 Decl_List
: out List_Id
)
1694 Formal_List
: constant List_Id
:= New_List
;
1695 Proc_Spec
: Node_Id
;
1696 Proc_Body
: Node_Id
;
1697 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
1698 Body_Decl_List
: List_Id
:= No_List
;
1699 Param_Type
: Node_Id
;
1702 if Nkind
(Object_Definition
(Ret_Obj
)) = N_Identifier
then
1704 New_Copy
(Object_Definition
(Ret_Obj
));
1707 New_Copy
(Subtype_Mark
(Object_Definition
(Ret_Obj
)));
1710 Append_To
(Formal_List
,
1711 Make_Parameter_Specification
(Loc
,
1712 Defining_Identifier
=>
1713 Make_Defining_Identifier
(Loc
,
1714 Chars
=> Chars
(Defining_Identifier
(Ret_Obj
))),
1715 In_Present
=> False,
1716 Out_Present
=> True,
1717 Null_Exclusion_Present
=> False,
1718 Parameter_Type
=> Param_Type
));
1720 Formal
:= First_Formal
(Spec_Id
);
1721 while Present
(Formal
) loop
1722 Append_To
(Formal_List
,
1723 Make_Parameter_Specification
(Loc
,
1724 Defining_Identifier
=>
1725 Make_Defining_Identifier
(Sloc
(Formal
),
1726 Chars
=> Chars
(Formal
)),
1727 In_Present
=> In_Present
(Parent
(Formal
)),
1728 Out_Present
=> Out_Present
(Parent
(Formal
)),
1729 Null_Exclusion_Present
=>
1730 Null_Exclusion_Present
(Parent
(Formal
)),
1732 New_Occurrence_Of
(Etype
(Formal
), Loc
),
1734 Copy_Separate_Tree
(Expression
(Parent
(Formal
)))));
1736 Next_Formal
(Formal
);
1739 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
1742 Make_Procedure_Specification
(Loc
,
1743 Defining_Unit_Name
=> Proc_Id
,
1744 Parameter_Specifications
=> Formal_List
);
1746 Decl_List
:= New_List
;
1748 Append_To
(Decl_List
,
1749 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
1751 -- Can_Convert_Unconstrained_Function checked that the function
1752 -- has no local declarations except implicit label declarations.
1753 -- Copy these declarations to the built procedure.
1755 if Present
(Declarations
(N
)) then
1756 Body_Decl_List
:= New_List
;
1763 D
:= First
(Declarations
(N
));
1764 while Present
(D
) loop
1765 pragma Assert
(Nkind
(D
) = N_Implicit_Label_Declaration
);
1768 Make_Implicit_Label_Declaration
(Loc
,
1769 Make_Defining_Identifier
(Loc
,
1770 Chars
=> Chars
(Defining_Identifier
(D
))),
1771 Label_Construct
=> Empty
);
1772 Append_To
(Body_Decl_List
, New_D
);
1779 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Node
)));
1782 Make_Subprogram_Body
(Loc
,
1783 Specification
=> Copy_Separate_Tree
(Proc_Spec
),
1784 Declarations
=> Body_Decl_List
,
1785 Handled_Statement_Sequence
=>
1786 Copy_Separate_Tree
(Handled_Statement_Sequence
(Ret_Node
)));
1788 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
1789 Make_Defining_Identifier
(Loc
, Subp_Name
));
1791 Append_To
(Decl_List
, Proc_Body
);
1792 end Build_Procedure
;
1796 New_Obj
: constant Node_Id
:= Copy_Separate_Tree
(Ret_Obj
);
1798 Proc_Id
: Entity_Id
;
1799 Proc_Call
: Node_Id
;
1801 -- Start of processing for Split_Unconstrained_Function
1804 -- Build the associated procedure, analyze it and insert it before
1805 -- the function body N.
1808 Scope
: constant Entity_Id
:= Current_Scope
;
1809 Decl_List
: List_Id
;
1812 Build_Procedure
(Proc_Id
, Decl_List
);
1813 Insert_Actions
(N
, Decl_List
);
1817 -- Build the call to the generated procedure
1820 Actual_List
: constant List_Id
:= New_List
;
1824 Append_To
(Actual_List
,
1825 New_Occurrence_Of
(Defining_Identifier
(New_Obj
), Loc
));
1827 Formal
:= First_Formal
(Spec_Id
);
1828 while Present
(Formal
) loop
1829 Append_To
(Actual_List
, New_Occurrence_Of
(Formal
, Loc
));
1831 -- Avoid spurious warning on unreferenced formals
1833 Set_Referenced
(Formal
);
1834 Next_Formal
(Formal
);
1838 Make_Procedure_Call_Statement
(Loc
,
1839 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
1840 Parameter_Associations
=> Actual_List
);
1848 -- main_1__F1b (New_Obj, ...);
1853 Make_Block_Statement
(Loc
,
1854 Declarations
=> New_List
(New_Obj
),
1855 Handled_Statement_Sequence
=>
1856 Make_Handled_Sequence_Of_Statements
(Loc
,
1857 Statements
=> New_List
(
1861 Make_Simple_Return_Statement
(Loc
,
1864 (Defining_Identifier
(New_Obj
), Loc
)))));
1866 Rewrite
(Ret_Node
, Blk_Stmt
);
1867 end Split_Unconstrained_Function
;
1871 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1873 -- Start of processing for Check_And_Split_Unconstrained_Function
1876 pragma Assert
(Back_End_Inlining
1877 and then Ekind
(Spec_Id
) = E_Function
1878 and then Returns_Unconstrained_Type
(Spec_Id
)
1879 and then Comes_From_Source
(Body_Id
)
1880 and then (Has_Pragma_Inline_Always
(Spec_Id
)
1881 or else Optimization_Level
> 0));
1883 -- This routine must not be used in GNATprove mode since GNATprove
1884 -- relies on frontend inlining
1886 pragma Assert
(not GNATprove_Mode
);
1888 -- No need to split the function if we cannot generate the code
1890 if Serious_Errors_Detected
/= 0 then
1894 -- No action needed in stubs since the attribute Body_To_Inline
1897 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
1900 -- Cannot build the body to inline if the attribute is already set.
1901 -- This attribute may have been set if this is a subprogram renaming
1902 -- declarations (see Freeze.Build_Renamed_Body).
1904 elsif Present
(Body_To_Inline
(Decl
)) then
1907 -- Check excluded declarations
1909 elsif Present
(Declarations
(N
))
1910 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
1914 -- Check excluded statements. There is no need to protect us against
1915 -- exception handlers since they are supported by the GCC backend.
1917 elsif Present
(Handled_Statement_Sequence
(N
))
1918 and then Has_Excluded_Statement
1919 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
1924 -- Build the body to inline only if really needed
1926 if Can_Split_Unconstrained_Function
(N
) then
1927 Split_Unconstrained_Function
(N
, Spec_Id
);
1928 Build_Body_To_Inline
(N
, Spec_Id
);
1929 Set_Is_Inlined
(Spec_Id
);
1931 end Check_And_Split_Unconstrained_Function
;
1933 -------------------------------------
1934 -- Check_Package_Body_For_Inlining --
1935 -------------------------------------
1937 procedure Check_Package_Body_For_Inlining
(N
: Node_Id
; P
: Entity_Id
) is
1938 Bname
: Unit_Name_Type
;
1943 -- Legacy implementation (relying on frontend inlining)
1945 if not Back_End_Inlining
1946 and then Is_Compilation_Unit
(P
)
1947 and then not Is_Generic_Instance
(P
)
1949 Bname
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
1951 E
:= First_Entity
(P
);
1952 while Present
(E
) loop
1953 if Has_Pragma_Inline_Always
(E
)
1954 or else (Has_Pragma_Inline
(E
) and Front_End_Inlining
)
1956 if not Is_Loaded
(Bname
) then
1957 Load_Needed_Body
(N
, OK
);
1961 -- Check we are not trying to inline a parent whose body
1962 -- depends on a child, when we are compiling the body of
1963 -- the child. Otherwise we have a potential elaboration
1964 -- circularity with inlined subprograms and with
1965 -- Taft-Amendment types.
1968 Comp
: Node_Id
; -- Body just compiled
1969 Child_Spec
: Entity_Id
; -- Spec of main unit
1970 Ent
: Entity_Id
; -- For iteration
1971 With_Clause
: Node_Id
; -- Context of body.
1974 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Package_Body
1975 and then Present
(Body_Entity
(P
))
1979 ((Unit
(Library_Unit
(Cunit
(Main_Unit
)))));
1982 Parent
(Unit_Declaration_Node
(Body_Entity
(P
)));
1984 -- Check whether the context of the body just
1985 -- compiled includes a child of itself, and that
1986 -- child is the spec of the main compilation.
1988 With_Clause
:= First
(Context_Items
(Comp
));
1989 while Present
(With_Clause
) loop
1990 if Nkind
(With_Clause
) = N_With_Clause
1992 Scope
(Entity
(Name
(With_Clause
))) = P
1994 Entity
(Name
(With_Clause
)) = Child_Spec
1996 Error_Msg_Node_2
:= Child_Spec
;
1998 ("body of & depends on child unit&??",
2001 ("\subprograms in body cannot be inlined??",
2004 -- Disable further inlining from this unit,
2005 -- and keep Taft-amendment types incomplete.
2007 Ent
:= First_Entity
(P
);
2008 while Present
(Ent
) loop
2010 and then Has_Completion_In_Body
(Ent
)
2012 Set_Full_View
(Ent
, Empty
);
2014 elsif Is_Subprogram
(Ent
) then
2015 Set_Is_Inlined
(Ent
, False);
2029 elsif Ineffective_Inline_Warnings
then
2030 Error_Msg_Unit_1
:= Bname
;
2032 ("unable to inline subprograms defined in $??", P
);
2033 Error_Msg_N
("\body not found??", P
);
2044 end Check_Package_Body_For_Inlining
;
2046 --------------------
2047 -- Cleanup_Scopes --
2048 --------------------
2050 procedure Cleanup_Scopes
is
2056 Elmt
:= First_Elmt
(To_Clean
);
2057 while Present
(Elmt
) loop
2058 Scop
:= Node
(Elmt
);
2060 if Ekind
(Scop
) = E_Entry
then
2061 Scop
:= Protected_Body_Subprogram
(Scop
);
2063 elsif Is_Subprogram
(Scop
)
2064 and then Is_Protected_Type
(Scope
(Scop
))
2065 and then Present
(Protected_Body_Subprogram
(Scop
))
2067 -- If a protected operation contains an instance, its cleanup
2068 -- operations have been delayed, and the subprogram has been
2069 -- rewritten in the expansion of the enclosing protected body. It
2070 -- is the corresponding subprogram that may require the cleanup
2071 -- operations, so propagate the information that triggers cleanup
2075 (Protected_Body_Subprogram
(Scop
),
2076 Uses_Sec_Stack
(Scop
));
2078 Scop
:= Protected_Body_Subprogram
(Scop
);
2081 if Ekind
(Scop
) = E_Block
then
2082 Decl
:= Parent
(Block_Node
(Scop
));
2085 Decl
:= Unit_Declaration_Node
(Scop
);
2087 if Nkind_In
(Decl
, N_Subprogram_Declaration
,
2088 N_Task_Type_Declaration
,
2089 N_Subprogram_Body_Stub
)
2091 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
2096 Expand_Cleanup_Actions
(Decl
);
2099 Elmt
:= Next_Elmt
(Elmt
);
2103 -------------------------
2104 -- Expand_Inlined_Call --
2105 -------------------------
2107 procedure Expand_Inlined_Call
2110 Orig_Subp
: Entity_Id
)
2112 Loc
: constant Source_Ptr
:= Sloc
(N
);
2113 Is_Predef
: constant Boolean :=
2114 Is_Predefined_File_Name
2115 (Unit_File_Name
(Get_Source_Unit
(Subp
)));
2116 Orig_Bod
: constant Node_Id
:=
2117 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
2121 Decls
: constant List_Id
:= New_List
;
2122 Exit_Lab
: Entity_Id
:= Empty
;
2129 Ret_Type
: Entity_Id
;
2132 -- The target of the call. If context is an assignment statement then
2133 -- this is the left-hand side of the assignment, else it is a temporary
2134 -- to which the return value is assigned prior to rewriting the call.
2137 -- A separate target used when the return type is unconstrained
2140 Temp_Typ
: Entity_Id
;
2142 Return_Object
: Entity_Id
:= Empty
;
2143 -- Entity in declaration in an extended_return_statement
2146 Is_Unc_Decl
: Boolean;
2147 -- If the type returned by the function is unconstrained and the call
2148 -- can be inlined, special processing is required.
2150 procedure Make_Exit_Label
;
2151 -- Build declaration for exit label to be used in Return statements,
2152 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2153 -- declaration). Does nothing if Exit_Lab already set.
2155 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
2156 -- Replace occurrence of a formal with the corresponding actual, or the
2157 -- thunk generated for it. Replace a return statement with an assignment
2158 -- to the target of the call, with appropriate conversions if needed.
2160 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
2161 -- If the call being expanded is that of an internal subprogram, set the
2162 -- sloc of the generated block to that of the call itself, so that the
2163 -- expansion is skipped by the "next" command in gdb. Same processing
2164 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2165 -- Debug_Generated_Code is true, suppress this change to simplify our
2166 -- own development. Same in GNATprove mode, to ensure that warnings and
2167 -- diagnostics point to the proper location.
2169 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
2170 -- In subtree N search for occurrences of dispatching calls that use the
2171 -- Ada 2005 Object.Operation notation and the object is a formal of the
2172 -- inlined subprogram. Reset the entity associated with Operation in all
2173 -- the found occurrences.
2175 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
2176 -- If the function body is a single expression, replace call with
2177 -- expression, else insert block appropriately.
2179 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
2180 -- If procedure body has no local variables, inline body without
2181 -- creating block, otherwise rewrite call with block.
2183 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
2184 -- Determine whether a formal parameter is used only once in Orig_Bod
2186 ---------------------
2187 -- Make_Exit_Label --
2188 ---------------------
2190 procedure Make_Exit_Label
is
2191 Lab_Ent
: Entity_Id
;
2193 if No
(Exit_Lab
) then
2194 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
2195 Lab_Id
:= New_Occurrence_Of
(Lab_Ent
, Loc
);
2196 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
2198 Make_Implicit_Label_Declaration
(Loc
,
2199 Defining_Identifier
=> Lab_Ent
,
2200 Label_Construct
=> Exit_Lab
);
2202 end Make_Exit_Label
;
2204 ---------------------
2205 -- Process_Formals --
2206 ---------------------
2208 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
2214 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
2217 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
2218 A
:= Renamed_Object
(E
);
2220 -- Rewrite the occurrence of the formal into an occurrence of
2221 -- the actual. Also establish visibility on the proper view of
2222 -- the actual's subtype for the body's context (if the actual's
2223 -- subtype is private at the call point but its full view is
2224 -- visible to the body, then the inlined tree here must be
2225 -- analyzed with the full view).
2227 if Is_Entity_Name
(A
) then
2228 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Loc
));
2229 Check_Private_View
(N
);
2231 elsif Nkind
(A
) = N_Defining_Identifier
then
2232 Rewrite
(N
, New_Occurrence_Of
(A
, Loc
));
2233 Check_Private_View
(N
);
2238 Rewrite
(N
, New_Copy
(A
));
2244 elsif Is_Entity_Name
(N
)
2245 and then Present
(Return_Object
)
2246 and then Chars
(N
) = Chars
(Return_Object
)
2248 -- Occurrence within an extended return statement. The return
2249 -- object is local to the body been inlined, and thus the generic
2250 -- copy is not analyzed yet, so we match by name, and replace it
2251 -- with target of call.
2253 if Nkind
(Targ
) = N_Defining_Identifier
then
2254 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
2256 Rewrite
(N
, New_Copy_Tree
(Targ
));
2261 elsif Nkind
(N
) = N_Simple_Return_Statement
then
2262 if No
(Expression
(N
)) then
2265 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
2268 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
2269 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
2271 -- Function body is a single expression. No need for
2277 Num_Ret
:= Num_Ret
+ 1;
2281 -- Because of the presence of private types, the views of the
2282 -- expression and the context may be different, so place an
2283 -- unchecked conversion to the context type to avoid spurious
2284 -- errors, e.g. when the expression is a numeric literal and
2285 -- the context is private. If the expression is an aggregate,
2286 -- use a qualified expression, because an aggregate is not a
2287 -- legal argument of a conversion. Ditto for numeric literals,
2288 -- which must be resolved to a specific type.
2290 if Nkind_In
(Expression
(N
), N_Aggregate
,
2296 Make_Qualified_Expression
(Sloc
(N
),
2297 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
2298 Expression
=> Relocate_Node
(Expression
(N
)));
2301 Unchecked_Convert_To
2302 (Ret_Type
, Relocate_Node
(Expression
(N
)));
2305 if Nkind
(Targ
) = N_Defining_Identifier
then
2307 Make_Assignment_Statement
(Loc
,
2308 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2309 Expression
=> Ret
));
2312 Make_Assignment_Statement
(Loc
,
2313 Name
=> New_Copy
(Targ
),
2314 Expression
=> Ret
));
2317 Set_Assignment_OK
(Name
(N
));
2319 if Present
(Exit_Lab
) then
2321 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
2327 -- An extended return becomes a block whose first statement is the
2328 -- assignment of the initial expression of the return object to the
2329 -- target of the call itself.
2331 elsif Nkind
(N
) = N_Extended_Return_Statement
then
2333 Return_Decl
: constant Entity_Id
:=
2334 First
(Return_Object_Declarations
(N
));
2338 Return_Object
:= Defining_Identifier
(Return_Decl
);
2340 if Present
(Expression
(Return_Decl
)) then
2341 if Nkind
(Targ
) = N_Defining_Identifier
then
2343 Make_Assignment_Statement
(Loc
,
2344 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2345 Expression
=> Expression
(Return_Decl
));
2348 Make_Assignment_Statement
(Loc
,
2349 Name
=> New_Copy
(Targ
),
2350 Expression
=> Expression
(Return_Decl
));
2353 Set_Assignment_OK
(Name
(Assign
));
2355 if No
(Handled_Statement_Sequence
(N
)) then
2356 Set_Handled_Statement_Sequence
(N
,
2357 Make_Handled_Sequence_Of_Statements
(Loc
,
2358 Statements
=> New_List
));
2362 Statements
(Handled_Statement_Sequence
(N
)));
2366 Make_Block_Statement
(Loc
,
2367 Handled_Statement_Sequence
=>
2368 Handled_Statement_Sequence
(N
)));
2373 -- Remove pragma Unreferenced since it may refer to formals that
2374 -- are not visible in the inlined body, and in any case we will
2375 -- not be posting warnings on the inlined body so it is unneeded.
2377 elsif Nkind
(N
) = N_Pragma
2378 and then Pragma_Name
(N
) = Name_Unreferenced
2380 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
2386 end Process_Formals
;
2388 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
2394 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
2396 if not Debug_Generated_Code
then
2397 Set_Sloc
(Nod
, Sloc
(N
));
2398 Set_Comes_From_Source
(Nod
, False);
2404 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
2406 ------------------------------
2407 -- Reset_Dispatching_Calls --
2408 ------------------------------
2410 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
2412 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
2413 -- Comment required ???
2419 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
2421 if Nkind
(N
) = N_Procedure_Call_Statement
2422 and then Nkind
(Name
(N
)) = N_Selected_Component
2423 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
2424 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
2425 and then Is_Dispatching_Operation
2426 (Entity
(Selector_Name
(Name
(N
))))
2428 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
2434 function Do_Reset_Calls
is new Traverse_Func
(Do_Reset
);
2438 Dummy
: constant Traverse_Result
:= Do_Reset_Calls
(N
);
2439 pragma Unreferenced
(Dummy
);
2441 -- Start of processing for Reset_Dispatching_Calls
2445 end Reset_Dispatching_Calls
;
2447 ---------------------------
2448 -- Rewrite_Function_Call --
2449 ---------------------------
2451 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2452 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2453 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
2456 -- Optimize simple case: function body is a single return statement,
2457 -- which has been expanded into an assignment.
2459 if Is_Empty_List
(Declarations
(Blk
))
2460 and then Nkind
(Fst
) = N_Assignment_Statement
2461 and then No
(Next
(Fst
))
2463 -- The function call may have been rewritten as the temporary
2464 -- that holds the result of the call, in which case remove the
2465 -- now useless declaration.
2467 if Nkind
(N
) = N_Identifier
2468 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2470 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
2473 Rewrite
(N
, Expression
(Fst
));
2475 elsif Nkind
(N
) = N_Identifier
2476 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2478 -- The block assigns the result of the call to the temporary
2480 Insert_After
(Parent
(Entity
(N
)), Blk
);
2482 -- If the context is an assignment, and the left-hand side is free of
2483 -- side-effects, the replacement is also safe.
2484 -- Can this be generalized further???
2486 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
2488 (Is_Entity_Name
(Name
(Parent
(N
)))
2490 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
2491 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
2494 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
2495 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
2497 -- Replace assignment with the block
2500 Original_Assignment
: constant Node_Id
:= Parent
(N
);
2503 -- Preserve the original assignment node to keep the complete
2504 -- assignment subtree consistent enough for Analyze_Assignment
2505 -- to proceed (specifically, the original Lhs node must still
2506 -- have an assignment statement as its parent).
2508 -- We cannot rely on Original_Node to go back from the block
2509 -- node to the assignment node, because the assignment might
2510 -- already be a rewrite substitution.
2512 Discard_Node
(Relocate_Node
(Original_Assignment
));
2513 Rewrite
(Original_Assignment
, Blk
);
2516 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
2518 -- A call to a function which returns an unconstrained type
2519 -- found in the expression initializing an object-declaration is
2520 -- expanded into a procedure call which must be added after the
2521 -- object declaration.
2523 if Is_Unc_Decl
and Back_End_Inlining
then
2524 Insert_Action_After
(Parent
(N
), Blk
);
2526 Set_Expression
(Parent
(N
), Empty
);
2527 Insert_After
(Parent
(N
), Blk
);
2530 elsif Is_Unc
and then not Back_End_Inlining
then
2531 Insert_Before
(Parent
(N
), Blk
);
2533 end Rewrite_Function_Call
;
2535 ----------------------------
2536 -- Rewrite_Procedure_Call --
2537 ----------------------------
2539 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2540 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2543 -- If there is a transient scope for N, this will be the scope of the
2544 -- actions for N, and the statements in Blk need to be within this
2545 -- scope. For example, they need to have visibility on the constant
2546 -- declarations created for the formals.
2548 -- If N needs no transient scope, and if there are no declarations in
2549 -- the inlined body, we can do a little optimization and insert the
2550 -- statements for the body directly after N, and rewrite N to a
2551 -- null statement, instead of rewriting N into a full-blown block
2554 if not Scope_Is_Transient
2555 and then Is_Empty_List
(Declarations
(Blk
))
2557 Insert_List_After
(N
, Statements
(HSS
));
2558 Rewrite
(N
, Make_Null_Statement
(Loc
));
2562 end Rewrite_Procedure_Call
;
2564 -------------------------
2565 -- Formal_Is_Used_Once --
2566 -------------------------
2568 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
2569 Use_Counter
: Int
:= 0;
2571 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
2572 -- Traverse the tree and count the uses of the formal parameter.
2573 -- In this case, for optimization purposes, we do not need to
2574 -- continue the traversal once more than one use is encountered.
2580 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
2582 -- The original node is an identifier
2584 if Nkind
(N
) = N_Identifier
2585 and then Present
(Entity
(N
))
2587 -- Original node's entity points to the one in the copied body
2589 and then Nkind
(Entity
(N
)) = N_Identifier
2590 and then Present
(Entity
(Entity
(N
)))
2592 -- The entity of the copied node is the formal parameter
2594 and then Entity
(Entity
(N
)) = Formal
2596 Use_Counter
:= Use_Counter
+ 1;
2598 if Use_Counter
> 1 then
2600 -- Denote more than one use and abandon the traversal
2611 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
2613 -- Start of processing for Formal_Is_Used_Once
2616 Count_Formal_Uses
(Orig_Bod
);
2617 return Use_Counter
= 1;
2618 end Formal_Is_Used_Once
;
2620 -- Start of processing for Expand_Inlined_Call
2623 -- Initializations for old/new semantics
2625 if not Back_End_Inlining
then
2626 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
2627 and then not Is_Constrained
(Etype
(Subp
));
2628 Is_Unc_Decl
:= False;
2630 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
2631 and then Optimization_Level
> 0;
2632 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
2636 -- Check for an illegal attempt to inline a recursive procedure. If the
2637 -- subprogram has parameters this is detected when trying to supply a
2638 -- binding for parameters that already have one. For parameterless
2639 -- subprograms this must be done explicitly.
2641 if In_Open_Scopes
(Subp
) then
2642 Error_Msg_N
("call to recursive subprogram cannot be inlined??", N
);
2643 Set_Is_Inlined
(Subp
, False);
2645 -- In GNATprove mode, issue a warning, and indicate that the
2646 -- subprogram is not always inlined by setting flag Is_Inlined_Always
2649 if GNATprove_Mode
then
2650 Set_Is_Inlined_Always
(Subp
, False);
2655 -- Skip inlining if this is not a true inlining since the attribute
2656 -- Body_To_Inline is also set for renamings (see sinfo.ads)
2658 elsif Nkind
(Orig_Bod
) in N_Entity
then
2659 if not Has_Pragma_Inline
(Subp
) then
2663 -- Skip inlining if the function returns an unconstrained type using
2664 -- an extended return statement since this part of the new inlining
2665 -- model which is not yet supported by the current implementation. ???
2669 Nkind
(First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
2670 = N_Extended_Return_Statement
2671 and then not Back_End_Inlining
2676 if Nkind
(Orig_Bod
) = N_Defining_Identifier
2677 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
2679 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2680 -- can be replaced with calls to the renamed entity directly, because
2681 -- the subprograms are subtype conformant. If the renamed subprogram
2682 -- is an inherited operation, we must redo the expansion because
2683 -- implicit conversions may be needed. Similarly, if the renamed
2684 -- entity is inlined, expand the call for further optimizations.
2686 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
2688 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
2695 -- Register the call in the list of inlined calls
2697 Append_New_Elmt
(N
, To
=> Inlined_Calls
);
2699 -- Use generic machinery to copy body of inlined subprogram, as if it
2700 -- were an instantiation, resetting source locations appropriately, so
2701 -- that nested inlined calls appear in the main unit.
2703 Save_Env
(Subp
, Empty
);
2704 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
2708 if not Back_End_Inlining
then
2713 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
2715 Make_Block_Statement
(Loc
,
2716 Declarations
=> Declarations
(Bod
),
2717 Handled_Statement_Sequence
=>
2718 Handled_Statement_Sequence
(Bod
));
2720 if No
(Declarations
(Bod
)) then
2721 Set_Declarations
(Blk
, New_List
);
2724 -- For the unconstrained case, capture the name of the local
2725 -- variable that holds the result. This must be the first
2726 -- declaration in the block, because its bounds cannot depend
2727 -- on local variables. Otherwise there is no way to declare the
2728 -- result outside of the block. Needless to say, in general the
2729 -- bounds will depend on the actuals in the call.
2731 -- If the context is an assignment statement, as is the case
2732 -- for the expansion of an extended return, the left-hand side
2733 -- provides bounds even if the return type is unconstrained.
2737 First_Decl
: Node_Id
;
2740 First_Decl
:= First
(Declarations
(Blk
));
2742 if Nkind
(First_Decl
) /= N_Object_Declaration
then
2746 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
2747 Targ1
:= Defining_Identifier
(First_Decl
);
2749 Targ1
:= Name
(Parent
(N
));
2766 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
2768 Make_Block_Statement
(Loc
,
2769 Declarations
=> Declarations
(Bod
),
2770 Handled_Statement_Sequence
=>
2771 Handled_Statement_Sequence
(Bod
));
2773 -- Inline a call to a function that returns an unconstrained type.
2774 -- The semantic analyzer checked that frontend-inlined functions
2775 -- returning unconstrained types have no declarations and have
2776 -- a single extended return statement. As part of its processing
2777 -- the function was split in two subprograms: a procedure P and
2778 -- a function F that has a block with a call to procedure P (see
2779 -- Split_Unconstrained_Function).
2785 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)))) =
2789 Blk_Stmt
: constant Node_Id
:=
2790 First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
2791 First_Stmt
: constant Node_Id
:=
2792 First
(Statements
(Handled_Statement_Sequence
(Blk_Stmt
)));
2793 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
2797 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
2798 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
2799 and then No
(Next
(Second_Stmt
)));
2804 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
2805 Empty
, Instantiating
=> True);
2808 -- Capture the name of the local variable that holds the
2809 -- result. This must be the first declaration in the block,
2810 -- because its bounds cannot depend on local variables.
2811 -- Otherwise there is no way to declare the result outside
2812 -- of the block. Needless to say, in general the bounds will
2813 -- depend on the actuals in the call.
2815 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
2816 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
2818 -- If the context is an assignment statement, as is the case
2819 -- for the expansion of an extended return, the left-hand
2820 -- side provides bounds even if the return type is
2824 Targ1
:= Name
(Parent
(N
));
2829 if No
(Declarations
(Bod
)) then
2830 Set_Declarations
(Blk
, New_List
);
2835 -- If this is a derived function, establish the proper return type
2837 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
2838 Ret_Type
:= Etype
(Orig_Subp
);
2840 Ret_Type
:= Etype
(Subp
);
2843 -- Create temporaries for the actuals that are expressions, or that are
2844 -- scalars and require copying to preserve semantics.
2846 F
:= First_Formal
(Subp
);
2847 A
:= First_Actual
(N
);
2848 while Present
(F
) loop
2849 if Present
(Renamed_Object
(F
)) then
2851 -- If expander is active, it is an error to try to inline a
2852 -- recursive program. In GNATprove mode, just indicate that the
2853 -- inlining will not happen, and mark the subprogram as not always
2856 if GNATprove_Mode
then
2858 ("cannot inline call to recursive subprogram?", N
, Subp
);
2859 Set_Is_Inlined_Always
(Subp
, False);
2862 ("cannot inline call to recursive subprogram", N
);
2868 -- Reset Last_Assignment for any parameters of mode out or in out, to
2869 -- prevent spurious warnings about overwriting for assignments to the
2870 -- formal in the inlined code.
2872 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
2873 Set_Last_Assignment
(Entity
(A
), Empty
);
2876 -- If the argument may be a controlling argument in a call within
2877 -- the inlined body, we must preserve its classwide nature to insure
2878 -- that dynamic dispatching take place subsequently. If the formal
2879 -- has a constraint it must be preserved to retain the semantics of
2882 if Is_Class_Wide_Type
(Etype
(F
))
2883 or else (Is_Access_Type
(Etype
(F
))
2884 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
2886 Temp_Typ
:= Etype
(F
);
2888 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
2889 and then Etype
(F
) /= Base_Type
(Etype
(F
))
2891 Temp_Typ
:= Etype
(F
);
2893 Temp_Typ
:= Etype
(A
);
2896 -- If the actual is a simple name or a literal, no need to
2897 -- create a temporary, object can be used directly.
2899 -- If the actual is a literal and the formal has its address taken,
2900 -- we cannot pass the literal itself as an argument, so its value
2901 -- must be captured in a temporary.
2903 if (Is_Entity_Name
(A
)
2905 (not Is_Scalar_Type
(Etype
(A
))
2906 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
))
2908 -- When the actual is an identifier and the corresponding formal is
2909 -- used only once in the original body, the formal can be substituted
2910 -- directly with the actual parameter.
2912 or else (Nkind
(A
) = N_Identifier
2913 and then Formal_Is_Used_Once
(F
))
2916 (Nkind_In
(A
, N_Real_Literal
,
2918 N_Character_Literal
)
2919 and then not Address_Taken
(F
))
2921 if Etype
(F
) /= Etype
(A
) then
2923 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
2925 Set_Renamed_Object
(F
, A
);
2929 Temp
:= Make_Temporary
(Loc
, 'C');
2931 -- If the actual for an in/in-out parameter is a view conversion,
2932 -- make it into an unchecked conversion, given that an untagged
2933 -- type conversion is not a proper object for a renaming.
2935 -- In-out conversions that involve real conversions have already
2936 -- been transformed in Expand_Actuals.
2938 if Nkind
(A
) = N_Type_Conversion
2939 and then Ekind
(F
) /= E_In_Parameter
2942 Make_Unchecked_Type_Conversion
(Loc
,
2943 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
2944 Expression
=> Relocate_Node
(Expression
(A
)));
2946 elsif Etype
(F
) /= Etype
(A
) then
2947 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
2948 Temp_Typ
:= Etype
(F
);
2951 New_A
:= Relocate_Node
(A
);
2954 Set_Sloc
(New_A
, Sloc
(N
));
2956 -- If the actual has a by-reference type, it cannot be copied,
2957 -- so its value is captured in a renaming declaration. Otherwise
2958 -- declare a local constant initialized with the actual.
2960 -- We also use a renaming declaration for expressions of an array
2961 -- type that is not bit-packed, both for efficiency reasons and to
2962 -- respect the semantics of the call: in most cases the original
2963 -- call will pass the parameter by reference, and thus the inlined
2964 -- code will have the same semantics.
2966 -- Finally, we need a renaming declaration in the case of limited
2967 -- types for which initialization cannot be by copy either.
2969 if Ekind
(F
) = E_In_Parameter
2970 and then not Is_By_Reference_Type
(Etype
(A
))
2971 and then not Is_Limited_Type
(Etype
(A
))
2973 (not Is_Array_Type
(Etype
(A
))
2974 or else not Is_Object_Reference
(A
)
2975 or else Is_Bit_Packed_Array
(Etype
(A
)))
2978 Make_Object_Declaration
(Loc
,
2979 Defining_Identifier
=> Temp
,
2980 Constant_Present
=> True,
2981 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
2982 Expression
=> New_A
);
2985 Make_Object_Renaming_Declaration
(Loc
,
2986 Defining_Identifier
=> Temp
,
2987 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
2991 Append
(Decl
, Decls
);
2992 Set_Renamed_Object
(F
, Temp
);
2999 -- Establish target of function call. If context is not assignment or
3000 -- declaration, create a temporary as a target. The declaration for the
3001 -- temporary may be subsequently optimized away if the body is a single
3002 -- expression, or if the left-hand side of the assignment is simple
3003 -- enough, i.e. an entity or an explicit dereference of one.
3005 if Ekind
(Subp
) = E_Function
then
3006 if Nkind
(Parent
(N
)) = N_Assignment_Statement
3007 and then Is_Entity_Name
(Name
(Parent
(N
)))
3009 Targ
:= Name
(Parent
(N
));
3011 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3012 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3013 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3015 Targ
:= Name
(Parent
(N
));
3017 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3018 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
3019 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3021 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
3023 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
3024 and then Is_Limited_Type
(Etype
(Subp
))
3026 Targ
:= Defining_Identifier
(Parent
(N
));
3028 -- New semantics: In an object declaration avoid an extra copy
3029 -- of the result of a call to an inlined function that returns
3030 -- an unconstrained type
3032 elsif Back_End_Inlining
3033 and then Nkind
(Parent
(N
)) = N_Object_Declaration
3036 Targ
:= Defining_Identifier
(Parent
(N
));
3039 -- Replace call with temporary and create its declaration
3041 Temp
:= Make_Temporary
(Loc
, 'C');
3042 Set_Is_Internal
(Temp
);
3044 -- For the unconstrained case, the generated temporary has the
3045 -- same constrained declaration as the result variable. It may
3046 -- eventually be possible to remove that temporary and use the
3047 -- result variable directly.
3049 if Is_Unc
and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
3052 Make_Object_Declaration
(Loc
,
3053 Defining_Identifier
=> Temp
,
3054 Object_Definition
=>
3055 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3057 Replace_Formals
(Decl
);
3061 Make_Object_Declaration
(Loc
,
3062 Defining_Identifier
=> Temp
,
3063 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
3065 Set_Etype
(Temp
, Ret_Type
);
3068 Set_No_Initialization
(Decl
);
3069 Append
(Decl
, Decls
);
3070 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3075 Insert_Actions
(N
, Decls
);
3079 -- Special management for inlining a call to a function that returns
3080 -- an unconstrained type and initializes an object declaration: we
3081 -- avoid generating undesired extra calls and goto statements.
3084 -- function Func (...) return ...
3087 -- Result : String (1 .. 4);
3089 -- Proc (Result, ...);
3094 -- Result : String := Func (...);
3096 -- Replace this object declaration by:
3098 -- Result : String (1 .. 4);
3099 -- Proc (Result, ...);
3101 Remove_Homonym
(Targ
);
3104 Make_Object_Declaration
3106 Defining_Identifier
=> Targ
,
3107 Object_Definition
=>
3108 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3109 Replace_Formals
(Decl
);
3110 Rewrite
(Parent
(N
), Decl
);
3111 Analyze
(Parent
(N
));
3113 -- Avoid spurious warnings since we know that this declaration is
3114 -- referenced by the procedure call.
3116 Set_Never_Set_In_Source
(Targ
, False);
3118 -- Remove the local declaration of the extended return stmt from the
3121 Remove
(Parent
(Targ1
));
3123 -- Update the reference to the result (since we have rewriten the
3124 -- object declaration)
3127 Blk_Call_Stmt
: Node_Id
;
3130 -- Capture the call to the procedure
3133 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
3135 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
3137 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
3138 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
3139 New_Occurrence_Of
(Targ
, Loc
));
3142 -- Remove the return statement
3145 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3146 N_Simple_Return_Statement
);
3148 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3151 -- Traverse the tree and replace formals with actuals or their thunks.
3152 -- Attach block to tree before analysis and rewriting.
3154 Replace_Formals
(Blk
);
3155 Set_Parent
(Blk
, N
);
3157 if GNATprove_Mode
then
3160 elsif not Comes_From_Source
(Subp
) or else Is_Predef
then
3166 -- No action needed since return statement has been already removed
3170 elsif Present
(Exit_Lab
) then
3172 -- If the body was a single expression, the single return statement
3173 -- and the corresponding label are useless.
3177 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3180 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3182 Append
(Lab_Decl
, (Declarations
(Blk
)));
3183 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
3187 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3188 -- on conflicting private views that Gigi would ignore. If this is a
3189 -- predefined unit, analyze with checks off, as is done in the non-
3190 -- inlined run-time units.
3193 I_Flag
: constant Boolean := In_Inlined_Body
;
3196 In_Inlined_Body
:= True;
3200 Style
: constant Boolean := Style_Check
;
3203 Style_Check
:= False;
3205 -- Search for dispatching calls that use the Object.Operation
3206 -- notation using an Object that is a parameter of the inlined
3207 -- function. We reset the decoration of Operation to force
3208 -- the reanalysis of the inlined dispatching call because
3209 -- the actual object has been inlined.
3211 Reset_Dispatching_Calls
(Blk
);
3213 Analyze
(Blk
, Suppress
=> All_Checks
);
3214 Style_Check
:= Style
;
3221 In_Inlined_Body
:= I_Flag
;
3224 if Ekind
(Subp
) = E_Procedure
then
3225 Rewrite_Procedure_Call
(N
, Blk
);
3228 Rewrite_Function_Call
(N
, Blk
);
3233 -- For the unconstrained case, the replacement of the call has been
3234 -- made prior to the complete analysis of the generated declarations.
3235 -- Propagate the proper type now.
3238 if Nkind
(N
) = N_Identifier
then
3239 Set_Etype
(N
, Etype
(Entity
(N
)));
3241 Set_Etype
(N
, Etype
(Targ1
));
3248 -- Cleanup mapping between formals and actuals for other expansions
3250 F
:= First_Formal
(Subp
);
3251 while Present
(F
) loop
3252 Set_Renamed_Object
(F
, Empty
);
3255 end Expand_Inlined_Call
;
3257 --------------------------
3258 -- Get_Code_Unit_Entity --
3259 --------------------------
3261 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
is
3262 Unit
: Entity_Id
:= Cunit_Entity
(Get_Code_Unit
(E
));
3265 if Ekind
(Unit
) = E_Package_Body
then
3266 Unit
:= Spec_Entity
(Unit
);
3270 end Get_Code_Unit_Entity
;
3272 ------------------------------
3273 -- Has_Excluded_Declaration --
3274 ------------------------------
3276 function Has_Excluded_Declaration
3278 Decls
: List_Id
) return Boolean
3282 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
3283 -- Nested subprograms make a given body ineligible for inlining, but
3284 -- we make an exception for instantiations of unchecked conversion.
3285 -- The body has not been analyzed yet, so check the name, and verify
3286 -- that the visible entity with that name is the predefined unit.
3288 -----------------------------
3289 -- Is_Unchecked_Conversion --
3290 -----------------------------
3292 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
3293 Id
: constant Node_Id
:= Name
(D
);
3297 if Nkind
(Id
) = N_Identifier
3298 and then Chars
(Id
) = Name_Unchecked_Conversion
3300 Conv
:= Current_Entity
(Id
);
3302 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
3303 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
3305 Conv
:= Current_Entity
(Selector_Name
(Id
));
3310 return Present
(Conv
)
3311 and then Is_Predefined_File_Name
3312 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
3313 and then Is_Intrinsic_Subprogram
(Conv
);
3314 end Is_Unchecked_Conversion
;
3316 -- Start of processing for Has_Excluded_Declaration
3319 -- No action needed if the check is not needed
3321 if not Check_Inlining_Restrictions
then
3326 while Present
(D
) loop
3328 -- First declarations universally excluded
3330 if Nkind
(D
) = N_Package_Declaration
then
3332 ("cannot inline & (nested package declaration)?",
3336 elsif Nkind
(D
) = N_Package_Instantiation
then
3338 ("cannot inline & (nested package instantiation)?",
3343 -- Then declarations excluded only for front end inlining
3345 if Back_End_Inlining
then
3348 elsif Nkind
(D
) = N_Task_Type_Declaration
3349 or else Nkind
(D
) = N_Single_Task_Declaration
3352 ("cannot inline & (nested task type declaration)?",
3356 elsif Nkind
(D
) = N_Protected_Type_Declaration
3357 or else Nkind
(D
) = N_Single_Protected_Declaration
3360 ("cannot inline & (nested protected type declaration)?",
3364 elsif Nkind
(D
) = N_Subprogram_Body
then
3366 ("cannot inline & (nested subprogram)?",
3370 elsif Nkind
(D
) = N_Function_Instantiation
3371 and then not Is_Unchecked_Conversion
(D
)
3374 ("cannot inline & (nested function instantiation)?",
3378 elsif Nkind
(D
) = N_Procedure_Instantiation
then
3380 ("cannot inline & (nested procedure instantiation)?",
3389 end Has_Excluded_Declaration
;
3391 ----------------------------
3392 -- Has_Excluded_Statement --
3393 ----------------------------
3395 function Has_Excluded_Statement
3397 Stats
: List_Id
) return Boolean
3403 -- No action needed if the check is not needed
3405 if not Check_Inlining_Restrictions
then
3410 while Present
(S
) loop
3411 if Nkind_In
(S
, N_Abort_Statement
,
3412 N_Asynchronous_Select
,
3413 N_Conditional_Entry_Call
,
3414 N_Delay_Relative_Statement
,
3415 N_Delay_Until_Statement
,
3420 ("cannot inline & (non-allowed statement)?", S
, Subp
);
3423 elsif Nkind
(S
) = N_Block_Statement
then
3424 if Present
(Declarations
(S
))
3425 and then Has_Excluded_Declaration
(Subp
, Declarations
(S
))
3429 elsif Present
(Handled_Statement_Sequence
(S
)) then
3430 if not Back_End_Inlining
3433 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
3436 ("cannot inline& (exception handler)?",
3437 First
(Exception_Handlers
3438 (Handled_Statement_Sequence
(S
))),
3442 elsif Has_Excluded_Statement
3443 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
3449 elsif Nkind
(S
) = N_Case_Statement
then
3450 E
:= First
(Alternatives
(S
));
3451 while Present
(E
) loop
3452 if Has_Excluded_Statement
(Subp
, Statements
(E
)) then
3459 elsif Nkind
(S
) = N_If_Statement
then
3460 if Has_Excluded_Statement
(Subp
, Then_Statements
(S
)) then
3464 if Present
(Elsif_Parts
(S
)) then
3465 E
:= First
(Elsif_Parts
(S
));
3466 while Present
(E
) loop
3467 if Has_Excluded_Statement
(Subp
, Then_Statements
(E
)) then
3475 if Present
(Else_Statements
(S
))
3476 and then Has_Excluded_Statement
(Subp
, Else_Statements
(S
))
3481 elsif Nkind
(S
) = N_Loop_Statement
3482 and then Has_Excluded_Statement
(Subp
, Statements
(S
))
3486 elsif Nkind
(S
) = N_Extended_Return_Statement
then
3487 if Present
(Handled_Statement_Sequence
(S
))
3489 Has_Excluded_Statement
3490 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
3494 elsif not Back_End_Inlining
3495 and then Present
(Handled_Statement_Sequence
(S
))
3497 Present
(Exception_Handlers
3498 (Handled_Statement_Sequence
(S
)))
3501 ("cannot inline& (exception handler)?",
3502 First
(Exception_Handlers
(Handled_Statement_Sequence
(S
))),
3512 end Has_Excluded_Statement
;
3514 --------------------------
3515 -- Has_Initialized_Type --
3516 --------------------------
3518 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean is
3519 E_Body
: constant Node_Id
:= Get_Subprogram_Body
(E
);
3523 if No
(E_Body
) then -- imported subprogram
3527 Decl
:= First
(Declarations
(E_Body
));
3528 while Present
(Decl
) loop
3529 if Nkind
(Decl
) = N_Full_Type_Declaration
3530 and then Present
(Init_Proc
(Defining_Identifier
(Decl
)))
3540 end Has_Initialized_Type
;
3542 -----------------------
3543 -- Has_Single_Return --
3544 -----------------------
3546 function Has_Single_Return
(N
: Node_Id
) return Boolean is
3547 Return_Statement
: Node_Id
:= Empty
;
3549 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
3555 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
3557 if Nkind
(N
) = N_Simple_Return_Statement
then
3558 if Present
(Expression
(N
))
3559 and then Is_Entity_Name
(Expression
(N
))
3561 if No
(Return_Statement
) then
3562 Return_Statement
:= N
;
3565 elsif Chars
(Expression
(N
)) =
3566 Chars
(Expression
(Return_Statement
))
3574 -- A return statement within an extended return is a noop
3577 elsif No
(Expression
(N
))
3579 Nkind
(Parent
(Parent
(N
))) = N_Extended_Return_Statement
3584 -- Expression has wrong form
3589 -- We can only inline a build-in-place function if it has a single
3592 elsif Nkind
(N
) = N_Extended_Return_Statement
then
3593 if No
(Return_Statement
) then
3594 Return_Statement
:= N
;
3606 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
3608 -- Start of processing for Has_Single_Return
3611 if Check_All_Returns
(N
) /= OK
then
3614 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
3618 return Present
(Declarations
(N
))
3619 and then Present
(First
(Declarations
(N
)))
3620 and then Chars
(Expression
(Return_Statement
)) =
3621 Chars
(Defining_Identifier
(First
(Declarations
(N
))));
3623 end Has_Single_Return
;
3625 -----------------------------
3626 -- In_Main_Unit_Or_Subunit --
3627 -----------------------------
3629 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean is
3630 Comp
: Node_Id
:= Cunit
(Get_Code_Unit
(E
));
3633 -- Check whether the subprogram or package to inline is within the main
3634 -- unit or its spec or within a subunit. In either case there are no
3635 -- additional bodies to process. If the subprogram appears in a parent
3636 -- of the current unit, the check on whether inlining is possible is
3637 -- done in Analyze_Inlined_Bodies.
3639 while Nkind
(Unit
(Comp
)) = N_Subunit
loop
3640 Comp
:= Library_Unit
(Comp
);
3643 return Comp
= Cunit
(Main_Unit
)
3644 or else Comp
= Library_Unit
(Cunit
(Main_Unit
));
3645 end In_Main_Unit_Or_Subunit
;
3651 procedure Initialize
is
3653 Pending_Descriptor
.Init
;
3654 Pending_Instantiations
.Init
;
3655 Inlined_Bodies
.Init
;
3659 for J
in Hash_Headers
'Range loop
3660 Hash_Headers
(J
) := No_Subp
;
3663 Inlined_Calls
:= No_Elist
;
3664 Backend_Calls
:= No_Elist
;
3665 Backend_Inlined_Subps
:= No_Elist
;
3666 Backend_Not_Inlined_Subps
:= No_Elist
;
3669 ------------------------
3670 -- Instantiate_Bodies --
3671 ------------------------
3673 -- Generic bodies contain all the non-local references, so an
3674 -- instantiation does not need any more context than Standard
3675 -- itself, even if the instantiation appears in an inner scope.
3676 -- Generic associations have verified that the contract model is
3677 -- satisfied, so that any error that may occur in the analysis of
3678 -- the body is an internal error.
3680 procedure Instantiate_Bodies
is
3682 Info
: Pending_Body_Info
;
3685 if Serious_Errors_Detected
= 0 then
3686 Expander_Active
:= (Operating_Mode
= Opt
.Generate_Code
);
3687 Push_Scope
(Standard_Standard
);
3688 To_Clean
:= New_Elmt_List
;
3690 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
3694 -- A body instantiation may generate additional instantiations, so
3695 -- the following loop must scan to the end of a possibly expanding
3696 -- set (that's why we can't simply use a FOR loop here).
3699 while J
<= Pending_Instantiations
.Last
3700 and then Serious_Errors_Detected
= 0
3702 Info
:= Pending_Instantiations
.Table
(J
);
3704 -- If the instantiation node is absent, it has been removed
3705 -- as part of unreachable code.
3707 if No
(Info
.Inst_Node
) then
3710 elsif Nkind
(Info
.Act_Decl
) = N_Package_Declaration
then
3711 Instantiate_Package_Body
(Info
);
3712 Add_Scope_To_Clean
(Defining_Entity
(Info
.Act_Decl
));
3715 Instantiate_Subprogram_Body
(Info
);
3721 -- Reset the table of instantiations. Additional instantiations
3722 -- may be added through inlining, when additional bodies are
3725 Pending_Instantiations
.Init
;
3727 -- We can now complete the cleanup actions of scopes that contain
3728 -- pending instantiations (skipped for generic units, since we
3729 -- never need any cleanups in generic units).
3730 -- pending instantiations.
3733 and then not Is_Generic_Unit
(Main_Unit_Entity
)
3736 elsif Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
3742 end Instantiate_Bodies
;
3748 function Is_Nested
(E
: Entity_Id
) return Boolean is
3753 while Scop
/= Standard_Standard
loop
3754 if Ekind
(Scop
) in Subprogram_Kind
then
3757 elsif Ekind
(Scop
) = E_Task_Type
3758 or else Ekind
(Scop
) = E_Entry
3759 or else Ekind
(Scop
) = E_Entry_Family
3764 Scop
:= Scope
(Scop
);
3770 ------------------------
3771 -- List_Inlining_Info --
3772 ------------------------
3774 procedure List_Inlining_Info
is
3780 if not Debug_Flag_Dot_J
then
3784 -- Generate listing of calls inlined by the frontend
3786 if Present
(Inlined_Calls
) then
3788 Elmt
:= First_Elmt
(Inlined_Calls
);
3789 while Present
(Elmt
) loop
3792 if In_Extended_Main_Code_Unit
(Nod
) then
3796 Write_Str
("List of calls inlined by the frontend");
3803 Write_Location
(Sloc
(Nod
));
3812 -- Generate listing of calls passed to the backend
3814 if Present
(Backend_Calls
) then
3817 Elmt
:= First_Elmt
(Backend_Calls
);
3818 while Present
(Elmt
) loop
3821 if In_Extended_Main_Code_Unit
(Nod
) then
3825 Write_Str
("List of inlined calls passed to the backend");
3832 Write_Location
(Sloc
(Nod
));
3840 -- Generate listing of subprograms passed to the backend
3842 if Present
(Backend_Inlined_Subps
) and then Back_End_Inlining
then
3845 Elmt
:= First_Elmt
(Backend_Inlined_Subps
);
3846 while Present
(Elmt
) loop
3853 ("List of inlined subprograms passed to the backend");
3860 Write_Name
(Chars
(Nod
));
3862 Write_Location
(Sloc
(Nod
));
3870 -- Generate listing of subprograms that cannot be inlined by the backend
3872 if Present
(Backend_Not_Inlined_Subps
) and then Back_End_Inlining
then
3875 Elmt
:= First_Elmt
(Backend_Not_Inlined_Subps
);
3876 while Present
(Elmt
) loop
3883 ("List of subprograms that cannot be inlined by the backend");
3890 Write_Name
(Chars
(Nod
));
3892 Write_Location
(Sloc
(Nod
));
3899 end List_Inlining_Info
;
3907 Pending_Instantiations
.Locked
:= True;
3908 Inlined_Bodies
.Locked
:= True;
3909 Successors
.Locked
:= True;
3910 Inlined
.Locked
:= True;
3911 Pending_Instantiations
.Release
;
3912 Inlined_Bodies
.Release
;
3917 ---------------------------
3918 -- Register_Backend_Call --
3919 ---------------------------
3921 procedure Register_Backend_Call
(N
: Node_Id
) is
3923 Append_New_Elmt
(N
, To
=> Backend_Calls
);
3924 end Register_Backend_Call
;
3926 --------------------------
3927 -- Remove_Dead_Instance --
3928 --------------------------
3930 procedure Remove_Dead_Instance
(N
: Node_Id
) is
3935 while J
<= Pending_Instantiations
.Last
loop
3936 if Pending_Instantiations
.Table
(J
).Inst_Node
= N
then
3937 Pending_Instantiations
.Table
(J
).Inst_Node
:= Empty
;
3943 end Remove_Dead_Instance
;
3945 --------------------
3946 -- Remove_Pragmas --
3947 --------------------
3949 procedure Remove_Pragmas
(Bod
: Node_Id
) is
3954 Decl
:= First
(Declarations
(Bod
));
3955 while Present
(Decl
) loop
3958 if Nkind
(Decl
) = N_Pragma
3959 and then Nam_In
(Pragma_Name
(Decl
), Name_Contract_Cases
,