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 Comes_From_Source
(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 function Back_End_Cannot_Inline
(Subp
: Entity_Id
) return Boolean;
449 -- There are various conditions under which back-end inlining cannot
452 -- a) If a body has handlers, it must not be inlined, because this
453 -- may violate program semantics, and because in zero-cost exception
454 -- mode it will lead to undefined symbols at link time.
456 -- b) If a body contains inlined function instances, it cannot be
457 -- inlined under ZCX because the numeric suffix generated by gigi
458 -- will be different in the body and the place of the inlined call.
460 -- This procedure must be carefully coordinated with the back end.
462 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
);
463 -- Append Subp to the list of subprograms inlined by the backend
465 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
);
466 -- Append Subp to the list of subprograms that cannot be inlined by
469 ----------------------------
470 -- Back_End_Cannot_Inline --
471 ----------------------------
473 function Back_End_Cannot_Inline
(Subp
: Entity_Id
) return Boolean is
474 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
475 Body_Ent
: Entity_Id
;
479 if Nkind
(Decl
) = N_Subprogram_Declaration
480 and then Present
(Corresponding_Body
(Decl
))
482 Body_Ent
:= Corresponding_Body
(Decl
);
487 -- If subprogram is marked Inline_Always, inlining is mandatory
489 if Has_Pragma_Inline_Always
(Subp
) then
495 (Handled_Statement_Sequence
496 (Unit_Declaration_Node
(Corresponding_Body
(Decl
)))))
501 Ent
:= First_Entity
(Body_Ent
);
502 while Present
(Ent
) loop
503 if Is_Subprogram
(Ent
)
504 and then Is_Generic_Instance
(Ent
)
513 end Back_End_Cannot_Inline
;
515 -----------------------------------------
516 -- Register_Backend_Inlined_Subprogram --
517 -----------------------------------------
519 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
) is
521 Append_New_Elmt
(Subp
, To
=> Backend_Inlined_Subps
);
522 end Register_Backend_Inlined_Subprogram
;
524 ---------------------------------------------
525 -- Register_Backend_Not_Inlined_Subprogram --
526 ---------------------------------------------
528 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
) is
530 Append_New_Elmt
(Subp
, To
=> Backend_Not_Inlined_Subps
);
531 end Register_Backend_Not_Inlined_Subprogram
;
533 -- Start of processing for Add_Inlined_Subprogram
536 -- If the subprogram is to be inlined, and if its unit is known to be
537 -- inlined or is an instance whose body will be analyzed anyway or the
538 -- subprogram has been generated by the compiler, and if it is declared
539 -- at the library level not in the main unit, and if it can be inlined
540 -- by the back-end, then insert it in the list of inlined subprograms.
543 and then (Is_Inlined
(Pack
)
544 or else Is_Generic_Instance
(Pack
)
545 or else Is_Internal
(E
))
546 and then not In_Main_Unit_Or_Subunit
(E
)
547 and then not Is_Nested
(E
)
548 and then not Has_Initialized_Type
(E
)
550 if Back_End_Cannot_Inline
(E
) then
551 Set_Is_Inlined
(E
, False);
552 Register_Backend_Not_Inlined_Subprogram
(E
);
555 Register_Backend_Inlined_Subprogram
(E
);
557 if No
(Last_Inlined
) then
558 Set_First_Inlined_Subprogram
(Cunit
(Main_Unit
), E
);
560 Set_Next_Inlined_Subprogram
(Last_Inlined
, E
);
566 Register_Backend_Not_Inlined_Subprogram
(E
);
569 Inlined
.Table
(Index
).Listed
:= True;
570 end Add_Inlined_Subprogram
;
572 ------------------------
573 -- Add_Scope_To_Clean --
574 ------------------------
576 procedure Add_Scope_To_Clean
(Inst
: Entity_Id
) is
577 Scop
: constant Entity_Id
:= Enclosing_Dynamic_Scope
(Inst
);
581 -- If the instance appears in a library-level package declaration,
582 -- all finalization is global, and nothing needs doing here.
584 if Scop
= Standard_Standard
then
588 -- If the instance is within a generic unit, no finalization code
589 -- can be generated. Note that at this point all bodies have been
590 -- analyzed, and the scope stack itself is not present, and the flag
591 -- Inside_A_Generic is not set.
598 while Present
(S
) and then S
/= Standard_Standard
loop
599 if Is_Generic_Unit
(S
) then
607 Elmt
:= First_Elmt
(To_Clean
);
608 while Present
(Elmt
) loop
609 if Node
(Elmt
) = Scop
then
613 Elmt
:= Next_Elmt
(Elmt
);
616 Append_Elmt
(Scop
, To_Clean
);
617 end Add_Scope_To_Clean
;
623 function Add_Subp
(E
: Entity_Id
) return Subp_Index
is
624 Index
: Subp_Index
:= Subp_Index
(E
) mod Num_Hash_Headers
;
628 -- Initialize entry in Inlined table
630 procedure New_Entry
is
632 Inlined
.Increment_Last
;
633 Inlined
.Table
(Inlined
.Last
).Name
:= E
;
634 Inlined
.Table
(Inlined
.Last
).Next
:= No_Subp
;
635 Inlined
.Table
(Inlined
.Last
).First_Succ
:= No_Succ
;
636 Inlined
.Table
(Inlined
.Last
).Listed
:= False;
637 Inlined
.Table
(Inlined
.Last
).Main_Call
:= False;
638 Inlined
.Table
(Inlined
.Last
).Processed
:= False;
641 -- Start of processing for Add_Subp
644 if Hash_Headers
(Index
) = No_Subp
then
646 Hash_Headers
(Index
) := Inlined
.Last
;
650 J
:= Hash_Headers
(Index
);
651 while J
/= No_Subp
loop
652 if Inlined
.Table
(J
).Name
= E
then
656 J
:= Inlined
.Table
(J
).Next
;
660 -- On exit, subprogram was not found. Enter in table. Index is
661 -- the current last entry on the hash chain.
664 Inlined
.Table
(Index
).Next
:= Inlined
.Last
;
669 ----------------------------
670 -- Analyze_Inlined_Bodies --
671 ----------------------------
673 procedure Analyze_Inlined_Bodies
is
680 type Pending_Index
is new Nat
;
682 package Pending_Inlined
is new Table
.Table
(
683 Table_Component_Type
=> Subp_Index
,
684 Table_Index_Type
=> Pending_Index
,
685 Table_Low_Bound
=> 1,
686 Table_Initial
=> Alloc
.Inlined_Initial
,
687 Table_Increment
=> Alloc
.Inlined_Increment
,
688 Table_Name
=> "Pending_Inlined");
689 -- The workpile used to compute the transitive closure
691 function Is_Ancestor_Of_Main
693 Nam
: Node_Id
) return Boolean;
694 -- Determine whether the unit whose body is loaded is an ancestor of
695 -- the main unit, and has a with_clause on it. The body is not
696 -- analyzed yet, so the check is purely lexical: the name of the with
697 -- clause is a selected component, and names of ancestors must match.
699 -------------------------
700 -- Is_Ancestor_Of_Main --
701 -------------------------
703 function Is_Ancestor_Of_Main
705 Nam
: Node_Id
) return Boolean
710 if Nkind
(Nam
) /= N_Selected_Component
then
714 if Chars
(Selector_Name
(Nam
)) /=
715 Chars
(Cunit_Entity
(Main_Unit
))
720 Pref
:= Prefix
(Nam
);
721 if Nkind
(Pref
) = N_Identifier
then
723 -- Par is an ancestor of Par.Child.
725 return Chars
(Pref
) = Chars
(U_Name
);
727 elsif Nkind
(Pref
) = N_Selected_Component
728 and then Chars
(Selector_Name
(Pref
)) = Chars
(U_Name
)
730 -- Par.Child is an ancestor of Par.Child.Grand.
732 return True; -- should check that ancestor match
735 -- A is an ancestor of A.B.C if it is an ancestor of A.B
737 return Is_Ancestor_Of_Main
(U_Name
, Pref
);
740 end Is_Ancestor_Of_Main
;
742 -- Start of processing for Analyze_Inlined_Bodies
745 if Serious_Errors_Detected
= 0 then
746 Push_Scope
(Standard_Standard
);
749 while J
<= Inlined_Bodies
.Last
750 and then Serious_Errors_Detected
= 0
752 Pack
:= Inlined_Bodies
.Table
(J
);
754 and then Scope
(Pack
) /= Standard_Standard
755 and then not Is_Child_Unit
(Pack
)
757 Pack
:= Scope
(Pack
);
760 Comp_Unit
:= Parent
(Pack
);
761 while Present
(Comp_Unit
)
762 and then Nkind
(Comp_Unit
) /= N_Compilation_Unit
764 Comp_Unit
:= Parent
(Comp_Unit
);
767 -- Load the body, unless it is the main unit, or is an instance
768 -- whose body has already been analyzed.
770 if Present
(Comp_Unit
)
771 and then Comp_Unit
/= Cunit
(Main_Unit
)
772 and then Body_Required
(Comp_Unit
)
773 and then (Nkind
(Unit
(Comp_Unit
)) /= N_Package_Declaration
774 or else No
(Corresponding_Body
(Unit
(Comp_Unit
))))
777 Bname
: constant Unit_Name_Type
:=
778 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
783 if not Is_Loaded
(Bname
) then
784 Style_Check
:= False;
785 Load_Needed_Body
(Comp_Unit
, OK
, Do_Analyze
=> False);
789 -- Warn that a body was not available for inlining
792 Error_Msg_Unit_1
:= Bname
;
794 ("one or more inlined subprograms accessed in $!??",
797 Get_File_Name
(Bname
, Subunit
=> False);
798 Error_Msg_N
("\but file{ was not found!??", Comp_Unit
);
801 -- If the package to be inlined is an ancestor unit of
802 -- the main unit, and it has a semantic dependence on
803 -- it, the inlining cannot take place to prevent an
804 -- elaboration circularity. The desired body is not
805 -- analyzed yet, to prevent the completion of Taft
806 -- amendment types that would lead to elaboration
807 -- circularities in gigi.
810 U_Id
: constant Entity_Id
:=
811 Defining_Entity
(Unit
(Comp_Unit
));
812 Body_Unit
: constant Node_Id
:=
813 Library_Unit
(Comp_Unit
);
817 Item
:= First
(Context_Items
(Body_Unit
));
818 while Present
(Item
) loop
819 if Nkind
(Item
) = N_With_Clause
821 Is_Ancestor_Of_Main
(U_Id
, Name
(Item
))
823 Set_Is_Inlined
(U_Id
, False);
830 -- If no suspicious with_clauses, analyze the body.
832 if Is_Inlined
(U_Id
) then
833 Semantics
(Body_Unit
);
844 -- The analysis of required bodies may have produced additional
845 -- generic instantiations. To obtain further inlining, we perform
846 -- another round of generic body instantiations. Establishing a
847 -- fully recursive loop between inlining and generic instantiations
848 -- is unlikely to yield more than this one additional pass.
852 -- The list of inlined subprograms is an overestimate, because it
853 -- includes inlined functions called from functions that are compiled
854 -- as part of an inlined package, but are not themselves called. An
855 -- accurate computation of just those subprograms that are needed
856 -- requires that we perform a transitive closure over the call graph,
857 -- starting from calls in the main program.
859 for Index
in Inlined
.First
.. Inlined
.Last
loop
860 if not Is_Called
(Inlined
.Table
(Index
).Name
) then
862 -- This means that Add_Inlined_Body added the subprogram to the
863 -- table but wasn't able to handle its code unit. Do nothing.
865 Inlined
.Table
(Index
).Processed
:= True;
867 elsif Inlined
.Table
(Index
).Main_Call
then
868 Pending_Inlined
.Increment_Last
;
869 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Index
;
870 Inlined
.Table
(Index
).Processed
:= True;
873 Set_Is_Called
(Inlined
.Table
(Index
).Name
, False);
877 -- Iterate over the workpile until it is emptied, propagating the
878 -- Is_Called flag to the successors of the processed subprogram.
880 while Pending_Inlined
.Last
>= Pending_Inlined
.First
loop
881 Subp
:= Pending_Inlined
.Table
(Pending_Inlined
.Last
);
882 Pending_Inlined
.Decrement_Last
;
884 S
:= Inlined
.Table
(Subp
).First_Succ
;
886 while S
/= No_Succ
loop
887 Subp
:= Successors
.Table
(S
).Subp
;
889 if not Inlined
.Table
(Subp
).Processed
then
890 Set_Is_Called
(Inlined
.Table
(Subp
).Name
);
891 Pending_Inlined
.Increment_Last
;
892 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Subp
;
893 Inlined
.Table
(Subp
).Processed
:= True;
896 S
:= Successors
.Table
(S
).Next
;
900 -- Finally add the called subprograms to the list of inlined
901 -- subprograms for the unit.
903 for Index
in Inlined
.First
.. Inlined
.Last
loop
904 if Is_Called
(Inlined
.Table
(Index
).Name
)
905 and then not Inlined
.Table
(Index
).Listed
907 Add_Inlined_Subprogram
(Index
);
913 end Analyze_Inlined_Bodies
;
915 --------------------------
916 -- Build_Body_To_Inline --
917 --------------------------
919 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
920 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
921 Analysis_Status
: constant Boolean := Full_Analysis
;
922 Original_Body
: Node_Id
;
923 Body_To_Analyze
: Node_Id
;
924 Max_Size
: constant := 10;
926 function Has_Pending_Instantiation
return Boolean;
927 -- If some enclosing body contains instantiations that appear before
928 -- the corresponding generic body, the enclosing body has a freeze node
929 -- so that it can be elaborated after the generic itself. This might
930 -- conflict with subsequent inlinings, so that it is unsafe to try to
931 -- inline in such a case.
933 function Has_Single_Return_In_GNATprove_Mode
return Boolean;
934 -- This function is called only in GNATprove mode, and it returns
935 -- True if the subprogram has no return statement or a single return
936 -- statement as last statement.
938 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
939 -- If the body of the subprogram includes a call that returns an
940 -- unconstrained type, the secondary stack is involved, and it
941 -- is not worth inlining.
943 -------------------------------
944 -- Has_Pending_Instantiation --
945 -------------------------------
947 function Has_Pending_Instantiation
return Boolean is
952 while Present
(S
) loop
953 if Is_Compilation_Unit
(S
)
954 or else Is_Child_Unit
(S
)
958 elsif Ekind
(S
) = E_Package
959 and then Has_Forward_Instantiation
(S
)
968 end Has_Pending_Instantiation
;
970 -----------------------------------------
971 -- Has_Single_Return_In_GNATprove_Mode --
972 -----------------------------------------
974 function Has_Single_Return_In_GNATprove_Mode
return Boolean is
975 Last_Statement
: Node_Id
:= Empty
;
977 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
978 -- Returns OK on node N if this is not a return statement different
979 -- from the last statement in the subprogram.
985 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
987 if Nkind_In
(N
, N_Simple_Return_Statement
,
988 N_Extended_Return_Statement
)
990 if N
= Last_Statement
then
1001 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
1003 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1006 -- Retrieve last statement inside possible block statements
1008 Last_Statement
:= Last
(Statements
(Handled_Statement_Sequence
(N
)));
1010 while Nkind
(Last_Statement
) = N_Block_Statement
loop
1012 Last
(Statements
(Handled_Statement_Sequence
(Last_Statement
)));
1015 -- Check that the last statement is the only possible return
1016 -- statement in the subprogram.
1018 return Check_All_Returns
(N
) = OK
;
1019 end Has_Single_Return_In_GNATprove_Mode
;
1021 --------------------------
1022 -- Uses_Secondary_Stack --
1023 --------------------------
1025 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
1026 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
1027 -- Look for function calls that return an unconstrained type
1033 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
1035 if Nkind
(N
) = N_Function_Call
1036 and then Is_Entity_Name
(Name
(N
))
1037 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
1038 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
1041 ("cannot inline & (call returns unconstrained type)?",
1049 function Check_Calls
is new Traverse_Func
(Check_Call
);
1052 return Check_Calls
(Bod
) = Abandon
;
1053 end Uses_Secondary_Stack
;
1055 -- Start of processing for Build_Body_To_Inline
1058 -- Return immediately if done already
1060 if Nkind
(Decl
) = N_Subprogram_Declaration
1061 and then Present
(Body_To_Inline
(Decl
))
1065 -- Subprograms that have return statements in the middle of the body are
1066 -- inlined with gotos. GNATprove does not currently support gotos, so
1067 -- we prevent such inlining.
1069 elsif GNATprove_Mode
1070 and then not Has_Single_Return_In_GNATprove_Mode
1072 Cannot_Inline
("cannot inline & (multiple returns)?", N
, Spec_Id
);
1075 -- Functions that return unconstrained composite types require
1076 -- secondary stack handling, and cannot currently be inlined, unless
1077 -- all return statements return a local variable that is the first
1078 -- local declaration in the body.
1080 elsif Ekind
(Spec_Id
) = E_Function
1081 and then not Is_Scalar_Type
(Etype
(Spec_Id
))
1082 and then not Is_Access_Type
(Etype
(Spec_Id
))
1083 and then not Is_Constrained
(Etype
(Spec_Id
))
1085 if not Has_Single_Return
(N
) then
1087 ("cannot inline & (unconstrained return type)?", N
, Spec_Id
);
1091 -- Ditto for functions that return controlled types, where controlled
1092 -- actions interfere in complex ways with inlining.
1094 elsif Ekind
(Spec_Id
) = E_Function
1095 and then Needs_Finalization
(Etype
(Spec_Id
))
1098 ("cannot inline & (controlled return type)?", N
, Spec_Id
);
1102 if Present
(Declarations
(N
))
1103 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
1108 if Present
(Handled_Statement_Sequence
(N
)) then
1109 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
1111 ("cannot inline& (exception handler)?",
1112 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
1116 elsif Has_Excluded_Statement
1117 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
1123 -- We do not inline a subprogram that is too large, unless it is marked
1124 -- Inline_Always or we are in GNATprove mode. This pragma does not
1125 -- suppress the other checks on inlining (forbidden declarations,
1128 if not (Has_Pragma_Inline_Always
(Spec_Id
) or else GNATprove_Mode
)
1129 and then List_Length
1130 (Statements
(Handled_Statement_Sequence
(N
))) > Max_Size
1132 Cannot_Inline
("cannot inline& (body too large)?", N
, Spec_Id
);
1136 if Has_Pending_Instantiation
then
1138 ("cannot inline& (forward instance within enclosing body)?",
1143 -- Within an instance, the body to inline must be treated as a nested
1144 -- generic, so that the proper global references are preserved.
1146 -- Note that we do not do this at the library level, because it is not
1147 -- needed, and furthermore this causes trouble if front end inlining
1148 -- is activated (-gnatN).
1150 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1151 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1152 Original_Body
:= Copy_Generic_Node
(N
, Empty
, True);
1154 Original_Body
:= Copy_Separate_Tree
(N
);
1157 -- We need to capture references to the formals in order to substitute
1158 -- the actuals at the point of inlining, i.e. instantiation. To treat
1159 -- the formals as globals to the body to inline, we nest it within a
1160 -- dummy parameterless subprogram, declared within the real one. To
1161 -- avoid generating an internal name (which is never public, and which
1162 -- affects serial numbers of other generated names), we use an internal
1163 -- symbol that cannot conflict with user declarations.
1165 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
1166 Set_Defining_Unit_Name
1167 (Specification
(Original_Body
),
1168 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
1169 Set_Corresponding_Spec
(Original_Body
, Empty
);
1171 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
1173 -- Set return type of function, which is also global and does not need
1176 if Ekind
(Spec_Id
) = E_Function
then
1177 Set_Result_Definition
(Specification
(Body_To_Analyze
),
1178 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1181 if No
(Declarations
(N
)) then
1182 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1184 Append
(Body_To_Analyze
, Declarations
(N
));
1187 -- The body to inline is pre-analyzed. In GNATprove mode we must
1188 -- disable full analysis as well so that light expansion does not
1189 -- take place either, and name resolution is unaffected.
1191 Expander_Mode_Save_And_Set
(False);
1192 Full_Analysis
:= False;
1193 Remove_Pragmas
(Body_To_Analyze
);
1195 Analyze
(Body_To_Analyze
);
1196 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1197 Save_Global_References
(Original_Body
);
1199 Remove
(Body_To_Analyze
);
1201 Expander_Mode_Restore
;
1202 Full_Analysis
:= Analysis_Status
;
1204 -- Restore environment if previously saved
1206 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1210 -- If secondary stack is used, there is no point in inlining. We have
1211 -- already issued the warning in this case, so nothing to do.
1213 if Uses_Secondary_Stack
(Body_To_Analyze
) then
1217 Set_Body_To_Inline
(Decl
, Original_Body
);
1218 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1219 Set_Is_Inlined
(Spec_Id
);
1220 end Build_Body_To_Inline
;
1226 procedure Cannot_Inline
1230 Is_Serious
: Boolean := False)
1233 -- In GNATprove mode, inlining is the technical means by which the
1234 -- higher-level goal of contextual analysis is reached, so issue
1235 -- messages about failure to apply contextual analysis to a
1236 -- subprogram, rather than failure to inline it.
1239 and then Msg
(Msg
'First .. Msg
'First + 12) = "cannot inline"
1242 Len1
: constant Positive :=
1243 String (String'("cannot inline"))'Length;
1244 Len2 : constant Positive :=
1245 String (String'("info: no contextual analysis of"))'Length;
1247 New_Msg
: String (1 .. Msg
'Length + Len2
- Len1
);
1250 New_Msg
(1 .. Len2
) := "info: no contextual analysis of";
1251 New_Msg
(Len2
+ 1 .. Msg
'Length + Len2
- Len1
) :=
1252 Msg
(Msg
'First + Len1
.. Msg
'Last);
1253 Cannot_Inline
(New_Msg
, N
, Subp
, Is_Serious
);
1258 pragma Assert
(Msg
(Msg
'Last) = '?');
1260 -- Legacy front end inlining model
1262 if not Back_End_Inlining
then
1264 -- Do not emit warning if this is a predefined unit which is not
1265 -- the main unit. With validity checks enabled, some predefined
1266 -- subprograms may contain nested subprograms and become ineligible
1269 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
1270 and then not In_Extended_Main_Source_Unit
(Subp
)
1274 -- In GNATprove mode, issue a warning, and indicate that the
1275 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1278 elsif GNATprove_Mode
then
1279 Set_Is_Inlined_Always
(Subp
, False);
1280 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1282 elsif Has_Pragma_Inline_Always
(Subp
) then
1284 -- Remove last character (question mark) to make this into an
1285 -- error, because the Inline_Always pragma cannot be obeyed.
1287 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1289 elsif Ineffective_Inline_Warnings
then
1290 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1297 elsif Is_Serious
then
1299 -- Remove last character (question mark) to make this into an error.
1301 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1303 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1304 -- is not always inlined by setting flag Is_Inlined_Always to False.
1306 elsif GNATprove_Mode
then
1307 Set_Is_Inlined_Always
(Subp
, False);
1308 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1310 -- Do not issue errors/warnings when compiling with optimizations
1312 elsif Optimization_Level
= 0 then
1314 -- Do not emit warning if this is a predefined unit which is not
1315 -- the main unit. This behavior is currently provided for backward
1316 -- compatibility but it will be removed when we enforce the
1317 -- strictness of the new rules.
1319 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
1320 and then not In_Extended_Main_Source_Unit
(Subp
)
1324 elsif Has_Pragma_Inline_Always
(Subp
) then
1326 -- Emit a warning if this is a call to a runtime subprogram
1327 -- which is located inside a generic. Previously this call
1328 -- was silently skipped.
1330 if Is_Generic_Instance
(Subp
) then
1332 Gen_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Subp
));
1334 if Is_Predefined_File_Name
1335 (Unit_File_Name
(Get_Source_Unit
(Gen_P
)))
1337 Set_Is_Inlined
(Subp
, False);
1338 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1344 -- Remove last character (question mark) to make this into an
1345 -- error, because the Inline_Always pragma cannot be obeyed.
1347 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1349 else pragma Assert
(Front_End_Inlining
);
1350 Set_Is_Inlined
(Subp
, False);
1352 -- When inlining cannot take place we must issue an error.
1353 -- For backward compatibility we still report a warning.
1355 if Ineffective_Inline_Warnings
then
1356 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1360 -- Compiling with optimizations enabled it is too early to report
1361 -- problems since the backend may still perform inlining. In order
1362 -- to report unhandled inlinings the program must be compiled with
1363 -- -Winline and the error is reported by the backend.
1370 --------------------------------------
1371 -- Can_Be_Inlined_In_GNATprove_Mode --
1372 --------------------------------------
1374 function Can_Be_Inlined_In_GNATprove_Mode
1375 (Spec_Id
: Entity_Id
;
1376 Body_Id
: Entity_Id
) return Boolean
1378 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean;
1379 -- Returns True if subprogram Id has any contract (Pre, Post, Global,
1382 function In_Some_Private_Part
(N
: Node_Id
) return Boolean;
1383 -- Returns True if node N is defined in the private part of a package
1385 function In_Unit_Body
(N
: Node_Id
) return Boolean;
1386 -- Returns True if node N is defined in the body of a unit
1388 function Is_Expression_Function
(Id
: Entity_Id
) return Boolean;
1389 -- Returns True if subprogram Id was defined originally as an expression
1392 -----------------------
1393 -- Has_Some_Contract --
1394 -----------------------
1396 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean is
1397 Items
: constant Node_Id
:= Contract
(Id
);
1399 return Present
(Items
)
1400 and then (Present
(Pre_Post_Conditions
(Items
)) or else
1401 Present
(Contract_Test_Cases
(Items
)) or else
1402 Present
(Classifications
(Items
)));
1403 end Has_Some_Contract
;
1405 --------------------------
1406 -- In_Some_Private_Part --
1407 --------------------------
1409 function In_Some_Private_Part
(N
: Node_Id
) return Boolean is
1415 while Present
(P
) and then Present
(Parent
(P
)) loop
1418 if Nkind
(PP
) = N_Package_Specification
1419 and then List_Containing
(P
) = Private_Declarations
(PP
)
1428 end In_Some_Private_Part
;
1434 function In_Unit_Body
(N
: Node_Id
) return Boolean is
1435 CU
: constant Node_Id
:= Enclosing_Comp_Unit_Node
(N
);
1438 and then Nkind_In
(Unit
(CU
), N_Package_Body
,
1443 ----------------------------
1444 -- Is_Expression_Function --
1445 ----------------------------
1447 function Is_Expression_Function
(Id
: Entity_Id
) return Boolean is
1448 Decl
: constant Node_Id
:= Parent
(Parent
(Id
));
1450 return Nkind
(Original_Node
(Decl
)) = N_Expression_Function
;
1451 end Is_Expression_Function
;
1453 -- Local declarations
1455 Id
: Entity_Id
; -- Procedure or function entity for the subprogram
1457 -- Start of Can_Be_Inlined_In_GNATprove_Mode
1460 pragma Assert
(Present
(Spec_Id
) or else Present
(Body_Id
));
1462 if Present
(Spec_Id
) then
1468 -- Only local subprograms without contracts are inlined in GNATprove
1469 -- mode, as these are the subprograms which a user is not interested in
1470 -- analyzing in isolation, but rather in the context of their call. This
1471 -- is a convenient convention, that could be changed for an explicit
1472 -- pragma/aspect one day.
1474 -- In a number of special cases, inlining is not desirable or not
1475 -- possible, see below.
1477 -- Do not inline unit-level subprograms
1479 if Nkind
(Parent
(Id
)) = N_Defining_Program_Unit_Name
then
1482 -- Do not inline subprograms declared in the visible part of a library
1485 elsif Is_Library_Level_Entity
(Id
)
1486 and then not In_Unit_Body
(Id
)
1487 and then not In_Some_Private_Part
(Id
)
1491 -- Do not inline subprograms that have a contract on the spec or the
1492 -- body. Use the contract(s) instead in GNATprove.
1494 elsif (Present
(Spec_Id
) and then Has_Some_Contract
(Spec_Id
))
1496 (Present
(Body_Id
) and then Has_Some_Contract
(Body_Id
))
1500 -- Do not inline expression functions, which are directly inlined at the
1503 elsif (Present
(Spec_Id
) and then Is_Expression_Function
(Spec_Id
))
1505 (Present
(Body_Id
) and then Is_Expression_Function
(Body_Id
))
1509 -- Do not inline generic subprogram instances. The visibility rules of
1510 -- generic instances plays badly with inlining.
1512 elsif Is_Generic_Instance
(Spec_Id
) then
1515 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1516 -- the subprogram body, a similar check is performed after the body
1517 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1519 elsif Present
(Spec_Id
)
1521 (No
(SPARK_Pragma
(Spec_Id
))
1522 or else Get_SPARK_Mode_From_Pragma
(SPARK_Pragma
(Spec_Id
)) /= On
)
1526 -- Subprograms in generic instances are currently not inlined, to avoid
1527 -- problems with inlining of standard library subprograms.
1529 elsif Instantiation_Location
(Sloc
(Id
)) /= No_Location
then
1532 -- Don't inline predicate functions (treated specially by GNATprove)
1534 elsif Is_Predicate_Function
(Id
) then
1537 -- Otherwise, this is a subprogram declared inside the private part of a
1538 -- package, or inside a package body, or locally in a subprogram, and it
1539 -- does not have any contract. Inline it.
1544 end Can_Be_Inlined_In_GNATprove_Mode
;
1546 --------------------------------------------
1547 -- Check_And_Split_Unconstrained_Function --
1548 --------------------------------------------
1550 procedure Check_And_Split_Unconstrained_Function
1552 Spec_Id
: Entity_Id
;
1553 Body_Id
: Entity_Id
)
1555 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
1556 -- Use generic machinery to build an unexpanded body for the subprogram.
1557 -- This body is subsequently used for inline expansions at call sites.
1559 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean;
1560 -- Return true if we generate code for the function body N, the function
1561 -- body N has no local declarations and its unique statement is a single
1562 -- extended return statement with a handled statements sequence.
1564 procedure Generate_Subprogram_Body
1566 Body_To_Inline
: out Node_Id
);
1567 -- Generate a parameterless duplicate of subprogram body N. Occurrences
1568 -- of pragmas referencing the formals are removed since they have no
1569 -- meaning when the body is inlined and the formals are rewritten (the
1570 -- analysis of the non-inlined body will handle these pragmas properly).
1571 -- A new internal name is associated with Body_To_Inline.
1573 procedure Split_Unconstrained_Function
1575 Spec_Id
: Entity_Id
);
1576 -- N is an inlined function body that returns an unconstrained type and
1577 -- has a single extended return statement. Split N in two subprograms:
1578 -- a procedure P' and a function F'. The formals of P' duplicate the
1579 -- formals of N plus an extra formal which is used return a value;
1580 -- its body is composed by the declarations and list of statements
1581 -- of the extended return statement of N.
1583 --------------------------
1584 -- Build_Body_To_Inline --
1585 --------------------------
1587 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
1588 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1589 Original_Body
: Node_Id
;
1590 Body_To_Analyze
: Node_Id
;
1593 pragma Assert
(Current_Scope
= Spec_Id
);
1595 -- Within an instance, the body to inline must be treated as a nested
1596 -- generic, so that the proper global references are preserved. We
1597 -- do not do this at the library level, because it is not needed, and
1598 -- furthermore this causes trouble if front end inlining is activated
1602 and then Scope
(Current_Scope
) /= Standard_Standard
1604 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1607 -- We need to capture references to the formals in order
1608 -- to substitute the actuals at the point of inlining, i.e.
1609 -- instantiation. To treat the formals as globals to the body to
1610 -- inline, we nest it within a dummy parameterless subprogram,
1611 -- declared within the real one.
1613 Generate_Subprogram_Body
(N
, Original_Body
);
1614 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
1616 -- Set return type of function, which is also global and does not
1617 -- need to be resolved.
1619 if Ekind
(Spec_Id
) = E_Function
then
1620 Set_Result_Definition
(Specification
(Body_To_Analyze
),
1621 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1624 if No
(Declarations
(N
)) then
1625 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1627 Append_To
(Declarations
(N
), Body_To_Analyze
);
1630 Preanalyze
(Body_To_Analyze
);
1632 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1633 Save_Global_References
(Original_Body
);
1635 Remove
(Body_To_Analyze
);
1637 -- Restore environment if previously saved
1640 and then Scope
(Current_Scope
) /= Standard_Standard
1645 pragma Assert
(No
(Body_To_Inline
(Decl
)));
1646 Set_Body_To_Inline
(Decl
, Original_Body
);
1647 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1648 end Build_Body_To_Inline
;
1650 --------------------------------------
1651 -- Can_Split_Unconstrained_Function --
1652 --------------------------------------
1654 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean
1656 Ret_Node
: constant Node_Id
:=
1657 First
(Statements
(Handled_Statement_Sequence
(N
)));
1661 -- No user defined declarations allowed in the function except inside
1662 -- the unique return statement; implicit labels are the only allowed
1665 if not Is_Empty_List
(Declarations
(N
)) then
1666 D
:= First
(Declarations
(N
));
1667 while Present
(D
) loop
1668 if Nkind
(D
) /= N_Implicit_Label_Declaration
then
1676 -- We only split the inlined function when we are generating the code
1677 -- of its body; otherwise we leave duplicated split subprograms in
1678 -- the tree which (if referenced) generate wrong references at link
1681 return In_Extended_Main_Code_Unit
(N
)
1682 and then Present
(Ret_Node
)
1683 and then Nkind
(Ret_Node
) = N_Extended_Return_Statement
1684 and then No
(Next
(Ret_Node
))
1685 and then Present
(Handled_Statement_Sequence
(Ret_Node
));
1686 end Can_Split_Unconstrained_Function
;
1688 -----------------------------
1689 -- Generate_Body_To_Inline --
1690 -----------------------------
1692 procedure Generate_Subprogram_Body
1694 Body_To_Inline
: out Node_Id
)
1697 -- Within an instance, the body to inline must be treated as a nested
1698 -- generic, so that the proper global references are preserved.
1700 -- Note that we do not do this at the library level, because it
1701 -- is not needed, and furthermore this causes trouble if front
1702 -- end inlining is activated (-gnatN).
1705 and then Scope
(Current_Scope
) /= Standard_Standard
1707 Body_To_Inline
:= Copy_Generic_Node
(N
, Empty
, True);
1709 Body_To_Inline
:= Copy_Separate_Tree
(N
);
1712 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
1713 -- parameter has no meaning when the body is inlined and the formals
1714 -- are rewritten. Remove it from body to inline. The analysis of the
1715 -- non-inlined body will handle the pragma properly.
1717 Remove_Pragmas
(Body_To_Inline
);
1719 -- We need to capture references to the formals in order
1720 -- to substitute the actuals at the point of inlining, i.e.
1721 -- instantiation. To treat the formals as globals to the body to
1722 -- inline, we nest it within a dummy parameterless subprogram,
1723 -- declared within the real one.
1725 Set_Parameter_Specifications
1726 (Specification
(Body_To_Inline
), No_List
);
1728 -- A new internal name is associated with Body_To_Inline to avoid
1729 -- conflicts when the non-inlined body N is analyzed.
1731 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
1732 Make_Defining_Identifier
(Sloc
(N
), New_Internal_Name
('P')));
1733 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
1734 end Generate_Subprogram_Body
;
1736 ----------------------------------
1737 -- Split_Unconstrained_Function --
1738 ----------------------------------
1740 procedure Split_Unconstrained_Function
1742 Spec_Id
: Entity_Id
)
1744 Loc
: constant Source_Ptr
:= Sloc
(N
);
1745 Ret_Node
: constant Node_Id
:=
1746 First
(Statements
(Handled_Statement_Sequence
(N
)));
1747 Ret_Obj
: constant Node_Id
:=
1748 First
(Return_Object_Declarations
(Ret_Node
));
1750 procedure Build_Procedure
1751 (Proc_Id
: out Entity_Id
;
1752 Decl_List
: out List_Id
);
1753 -- Build a procedure containing the statements found in the extended
1754 -- return statement of the unconstrained function body N.
1756 ---------------------
1757 -- Build_Procedure --
1758 ---------------------
1760 procedure Build_Procedure
1761 (Proc_Id
: out Entity_Id
;
1762 Decl_List
: out List_Id
)
1765 Formal_List
: constant List_Id
:= New_List
;
1766 Proc_Spec
: Node_Id
;
1767 Proc_Body
: Node_Id
;
1768 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
1769 Body_Decl_List
: List_Id
:= No_List
;
1770 Param_Type
: Node_Id
;
1773 if Nkind
(Object_Definition
(Ret_Obj
)) = N_Identifier
then
1775 New_Copy
(Object_Definition
(Ret_Obj
));
1778 New_Copy
(Subtype_Mark
(Object_Definition
(Ret_Obj
)));
1781 Append_To
(Formal_List
,
1782 Make_Parameter_Specification
(Loc
,
1783 Defining_Identifier
=>
1784 Make_Defining_Identifier
(Loc
,
1785 Chars
=> Chars
(Defining_Identifier
(Ret_Obj
))),
1786 In_Present
=> False,
1787 Out_Present
=> True,
1788 Null_Exclusion_Present
=> False,
1789 Parameter_Type
=> Param_Type
));
1791 Formal
:= First_Formal
(Spec_Id
);
1792 while Present
(Formal
) loop
1793 Append_To
(Formal_List
,
1794 Make_Parameter_Specification
(Loc
,
1795 Defining_Identifier
=>
1796 Make_Defining_Identifier
(Sloc
(Formal
),
1797 Chars
=> Chars
(Formal
)),
1798 In_Present
=> In_Present
(Parent
(Formal
)),
1799 Out_Present
=> Out_Present
(Parent
(Formal
)),
1800 Null_Exclusion_Present
=>
1801 Null_Exclusion_Present
(Parent
(Formal
)),
1803 New_Occurrence_Of
(Etype
(Formal
), Loc
),
1805 Copy_Separate_Tree
(Expression
(Parent
(Formal
)))));
1807 Next_Formal
(Formal
);
1810 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
1813 Make_Procedure_Specification
(Loc
,
1814 Defining_Unit_Name
=> Proc_Id
,
1815 Parameter_Specifications
=> Formal_List
);
1817 Decl_List
:= New_List
;
1819 Append_To
(Decl_List
,
1820 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
1822 -- Can_Convert_Unconstrained_Function checked that the function
1823 -- has no local declarations except implicit label declarations.
1824 -- Copy these declarations to the built procedure.
1826 if Present
(Declarations
(N
)) then
1827 Body_Decl_List
:= New_List
;
1834 D
:= First
(Declarations
(N
));
1835 while Present
(D
) loop
1836 pragma Assert
(Nkind
(D
) = N_Implicit_Label_Declaration
);
1839 Make_Implicit_Label_Declaration
(Loc
,
1840 Make_Defining_Identifier
(Loc
,
1841 Chars
=> Chars
(Defining_Identifier
(D
))),
1842 Label_Construct
=> Empty
);
1843 Append_To
(Body_Decl_List
, New_D
);
1850 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Node
)));
1853 Make_Subprogram_Body
(Loc
,
1854 Specification
=> Copy_Separate_Tree
(Proc_Spec
),
1855 Declarations
=> Body_Decl_List
,
1856 Handled_Statement_Sequence
=>
1857 Copy_Separate_Tree
(Handled_Statement_Sequence
(Ret_Node
)));
1859 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
1860 Make_Defining_Identifier
(Loc
, Subp_Name
));
1862 Append_To
(Decl_List
, Proc_Body
);
1863 end Build_Procedure
;
1867 New_Obj
: constant Node_Id
:= Copy_Separate_Tree
(Ret_Obj
);
1869 Proc_Id
: Entity_Id
;
1870 Proc_Call
: Node_Id
;
1872 -- Start of processing for Split_Unconstrained_Function
1875 -- Build the associated procedure, analyze it and insert it before
1876 -- the function body N.
1879 Scope
: constant Entity_Id
:= Current_Scope
;
1880 Decl_List
: List_Id
;
1883 Build_Procedure
(Proc_Id
, Decl_List
);
1884 Insert_Actions
(N
, Decl_List
);
1888 -- Build the call to the generated procedure
1891 Actual_List
: constant List_Id
:= New_List
;
1895 Append_To
(Actual_List
,
1896 New_Occurrence_Of
(Defining_Identifier
(New_Obj
), Loc
));
1898 Formal
:= First_Formal
(Spec_Id
);
1899 while Present
(Formal
) loop
1900 Append_To
(Actual_List
, New_Occurrence_Of
(Formal
, Loc
));
1902 -- Avoid spurious warning on unreferenced formals
1904 Set_Referenced
(Formal
);
1905 Next_Formal
(Formal
);
1909 Make_Procedure_Call_Statement
(Loc
,
1910 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
1911 Parameter_Associations
=> Actual_List
);
1919 -- main_1__F1b (New_Obj, ...);
1924 Make_Block_Statement
(Loc
,
1925 Declarations
=> New_List
(New_Obj
),
1926 Handled_Statement_Sequence
=>
1927 Make_Handled_Sequence_Of_Statements
(Loc
,
1928 Statements
=> New_List
(
1932 Make_Simple_Return_Statement
(Loc
,
1935 (Defining_Identifier
(New_Obj
), Loc
)))));
1937 Rewrite
(Ret_Node
, Blk_Stmt
);
1938 end Split_Unconstrained_Function
;
1942 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1944 -- Start of processing for Check_And_Split_Unconstrained_Function
1947 pragma Assert
(Back_End_Inlining
1948 and then Ekind
(Spec_Id
) = E_Function
1949 and then Returns_Unconstrained_Type
(Spec_Id
)
1950 and then Comes_From_Source
(Body_Id
)
1951 and then (Has_Pragma_Inline_Always
(Spec_Id
)
1952 or else Optimization_Level
> 0));
1954 -- This routine must not be used in GNATprove mode since GNATprove
1955 -- relies on frontend inlining
1957 pragma Assert
(not GNATprove_Mode
);
1959 -- No need to split the function if we cannot generate the code
1961 if Serious_Errors_Detected
/= 0 then
1965 -- Do not inline any subprogram that contains nested subprograms,
1966 -- since the backend inlining circuit seems to generate uninitialized
1967 -- references in this case. We know this happens in the case of front
1968 -- end ZCX support, but it also appears it can happen in other cases
1969 -- as well. The backend often rejects attempts to inline in the case
1970 -- of nested procedures anyway, so little if anything is lost by this.
1971 -- Note that this is test is for the benefit of the back-end. There
1972 -- is a separate test for front-end inlining that also rejects nested
1975 -- Do not do this test if errors have been detected, because in some
1976 -- error cases, this code blows up, and we don't need it anyway if
1977 -- there have been errors, since we won't get to the linker anyway.
1985 P_Ent
:= Scope
(P_Ent
);
1986 exit when No
(P_Ent
) or else P_Ent
= Standard_Standard
;
1988 if Is_Subprogram
(P_Ent
) then
1989 Set_Is_Inlined
(P_Ent
, False);
1991 if Comes_From_Source
(P_Ent
)
1992 and then (Has_Pragma_Inline
(P_Ent
))
1995 ("cannot inline& (nested subprogram)?", N
, P_Ent
,
1996 Is_Serious
=> True);
2003 -- No action needed in stubs since the attribute Body_To_Inline
2006 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
2009 -- Cannot build the body to inline if the attribute is already set.
2010 -- This attribute may have been set if this is a subprogram renaming
2011 -- declarations (see Freeze.Build_Renamed_Body).
2013 elsif Present
(Body_To_Inline
(Decl
)) then
2016 -- Check excluded declarations
2018 elsif Present
(Declarations
(N
))
2019 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
2023 -- Check excluded statements. There is no need to protect us against
2024 -- exception handlers since they are supported by the GCC backend.
2026 elsif Present
(Handled_Statement_Sequence
(N
))
2027 and then Has_Excluded_Statement
2028 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
2033 -- Build the body to inline only if really needed
2035 if Can_Split_Unconstrained_Function
(N
) then
2036 Split_Unconstrained_Function
(N
, Spec_Id
);
2037 Build_Body_To_Inline
(N
, Spec_Id
);
2038 Set_Is_Inlined
(Spec_Id
);
2040 end Check_And_Split_Unconstrained_Function
;
2042 -------------------------------------
2043 -- Check_Package_Body_For_Inlining --
2044 -------------------------------------
2046 procedure Check_Package_Body_For_Inlining
(N
: Node_Id
; P
: Entity_Id
) is
2047 Bname
: Unit_Name_Type
;
2052 if Is_Compilation_Unit
(P
)
2053 and then not Is_Generic_Instance
(P
)
2055 Bname
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
2057 E
:= First_Entity
(P
);
2058 while Present
(E
) loop
2059 if Has_Pragma_Inline_Always
(E
)
2060 or else (Front_End_Inlining
and then Has_Pragma_Inline
(E
))
2062 if not Is_Loaded
(Bname
) then
2063 Load_Needed_Body
(N
, OK
);
2067 -- Check we are not trying to inline a parent whose body
2068 -- depends on a child, when we are compiling the body of
2069 -- the child. Otherwise we have a potential elaboration
2070 -- circularity with inlined subprograms and with
2071 -- Taft-Amendment types.
2074 Comp
: Node_Id
; -- Body just compiled
2075 Child_Spec
: Entity_Id
; -- Spec of main unit
2076 Ent
: Entity_Id
; -- For iteration
2077 With_Clause
: Node_Id
; -- Context of body.
2080 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Package_Body
2081 and then Present
(Body_Entity
(P
))
2085 ((Unit
(Library_Unit
(Cunit
(Main_Unit
)))));
2088 Parent
(Unit_Declaration_Node
(Body_Entity
(P
)));
2090 -- Check whether the context of the body just
2091 -- compiled includes a child of itself, and that
2092 -- child is the spec of the main compilation.
2094 With_Clause
:= First
(Context_Items
(Comp
));
2095 while Present
(With_Clause
) loop
2096 if Nkind
(With_Clause
) = N_With_Clause
2098 Scope
(Entity
(Name
(With_Clause
))) = P
2100 Entity
(Name
(With_Clause
)) = Child_Spec
2102 Error_Msg_Node_2
:= Child_Spec
;
2104 ("body of & depends on child unit&??",
2107 ("\subprograms in body cannot be inlined??",
2110 -- Disable further inlining from this unit,
2111 -- and keep Taft-amendment types incomplete.
2113 Ent
:= First_Entity
(P
);
2114 while Present
(Ent
) loop
2116 and then Has_Completion_In_Body
(Ent
)
2118 Set_Full_View
(Ent
, Empty
);
2120 elsif Is_Subprogram
(Ent
) then
2121 Set_Is_Inlined
(Ent
, False);
2135 elsif Ineffective_Inline_Warnings
then
2136 Error_Msg_Unit_1
:= Bname
;
2138 ("unable to inline subprograms defined in $??", P
);
2139 Error_Msg_N
("\body not found??", P
);
2150 end Check_Package_Body_For_Inlining
;
2152 --------------------
2153 -- Cleanup_Scopes --
2154 --------------------
2156 procedure Cleanup_Scopes
is
2162 Elmt
:= First_Elmt
(To_Clean
);
2163 while Present
(Elmt
) loop
2164 Scop
:= Node
(Elmt
);
2166 if Ekind
(Scop
) = E_Entry
then
2167 Scop
:= Protected_Body_Subprogram
(Scop
);
2169 elsif Is_Subprogram
(Scop
)
2170 and then Is_Protected_Type
(Scope
(Scop
))
2171 and then Present
(Protected_Body_Subprogram
(Scop
))
2173 -- If a protected operation contains an instance, its cleanup
2174 -- operations have been delayed, and the subprogram has been
2175 -- rewritten in the expansion of the enclosing protected body. It
2176 -- is the corresponding subprogram that may require the cleanup
2177 -- operations, so propagate the information that triggers cleanup
2181 (Protected_Body_Subprogram
(Scop
),
2182 Uses_Sec_Stack
(Scop
));
2184 Scop
:= Protected_Body_Subprogram
(Scop
);
2187 if Ekind
(Scop
) = E_Block
then
2188 Decl
:= Parent
(Block_Node
(Scop
));
2191 Decl
:= Unit_Declaration_Node
(Scop
);
2193 if Nkind_In
(Decl
, N_Subprogram_Declaration
,
2194 N_Task_Type_Declaration
,
2195 N_Subprogram_Body_Stub
)
2197 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
2202 Expand_Cleanup_Actions
(Decl
);
2205 Elmt
:= Next_Elmt
(Elmt
);
2209 -------------------------
2210 -- Expand_Inlined_Call --
2211 -------------------------
2213 procedure Expand_Inlined_Call
2216 Orig_Subp
: Entity_Id
)
2218 Loc
: constant Source_Ptr
:= Sloc
(N
);
2219 Is_Predef
: constant Boolean :=
2220 Is_Predefined_File_Name
2221 (Unit_File_Name
(Get_Source_Unit
(Subp
)));
2222 Orig_Bod
: constant Node_Id
:=
2223 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
2227 Decls
: constant List_Id
:= New_List
;
2228 Exit_Lab
: Entity_Id
:= Empty
;
2235 Ret_Type
: Entity_Id
;
2238 -- The target of the call. If context is an assignment statement then
2239 -- this is the left-hand side of the assignment, else it is a temporary
2240 -- to which the return value is assigned prior to rewriting the call.
2243 -- A separate target used when the return type is unconstrained
2246 Temp_Typ
: Entity_Id
;
2248 Return_Object
: Entity_Id
:= Empty
;
2249 -- Entity in declaration in an extended_return_statement
2252 Is_Unc_Decl
: Boolean;
2253 -- If the type returned by the function is unconstrained and the call
2254 -- can be inlined, special processing is required.
2256 procedure Make_Exit_Label
;
2257 -- Build declaration for exit label to be used in Return statements,
2258 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2259 -- declaration). Does nothing if Exit_Lab already set.
2261 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
2262 -- Replace occurrence of a formal with the corresponding actual, or the
2263 -- thunk generated for it. Replace a return statement with an assignment
2264 -- to the target of the call, with appropriate conversions if needed.
2266 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
2267 -- If the call being expanded is that of an internal subprogram, set the
2268 -- sloc of the generated block to that of the call itself, so that the
2269 -- expansion is skipped by the "next" command in gdb. Same processing
2270 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2271 -- Debug_Generated_Code is true, suppress this change to simplify our
2272 -- own development. Same in GNATprove mode, to ensure that warnings and
2273 -- diagnostics point to the proper location.
2275 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
2276 -- In subtree N search for occurrences of dispatching calls that use the
2277 -- Ada 2005 Object.Operation notation and the object is a formal of the
2278 -- inlined subprogram. Reset the entity associated with Operation in all
2279 -- the found occurrences.
2281 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
2282 -- If the function body is a single expression, replace call with
2283 -- expression, else insert block appropriately.
2285 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
2286 -- If procedure body has no local variables, inline body without
2287 -- creating block, otherwise rewrite call with block.
2289 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
2290 -- Determine whether a formal parameter is used only once in Orig_Bod
2292 ---------------------
2293 -- Make_Exit_Label --
2294 ---------------------
2296 procedure Make_Exit_Label
is
2297 Lab_Ent
: Entity_Id
;
2299 if No
(Exit_Lab
) then
2300 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
2301 Lab_Id
:= New_Occurrence_Of
(Lab_Ent
, Loc
);
2302 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
2304 Make_Implicit_Label_Declaration
(Loc
,
2305 Defining_Identifier
=> Lab_Ent
,
2306 Label_Construct
=> Exit_Lab
);
2308 end Make_Exit_Label
;
2310 ---------------------
2311 -- Process_Formals --
2312 ---------------------
2314 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
2320 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
2323 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
2324 A
:= Renamed_Object
(E
);
2326 -- Rewrite the occurrence of the formal into an occurrence of
2327 -- the actual. Also establish visibility on the proper view of
2328 -- the actual's subtype for the body's context (if the actual's
2329 -- subtype is private at the call point but its full view is
2330 -- visible to the body, then the inlined tree here must be
2331 -- analyzed with the full view).
2333 if Is_Entity_Name
(A
) then
2334 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Loc
));
2335 Check_Private_View
(N
);
2337 elsif Nkind
(A
) = N_Defining_Identifier
then
2338 Rewrite
(N
, New_Occurrence_Of
(A
, Loc
));
2339 Check_Private_View
(N
);
2344 Rewrite
(N
, New_Copy
(A
));
2350 elsif Is_Entity_Name
(N
)
2351 and then Present
(Return_Object
)
2352 and then Chars
(N
) = Chars
(Return_Object
)
2354 -- Occurrence within an extended return statement. The return
2355 -- object is local to the body been inlined, and thus the generic
2356 -- copy is not analyzed yet, so we match by name, and replace it
2357 -- with target of call.
2359 if Nkind
(Targ
) = N_Defining_Identifier
then
2360 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
2362 Rewrite
(N
, New_Copy_Tree
(Targ
));
2367 elsif Nkind
(N
) = N_Simple_Return_Statement
then
2368 if No
(Expression
(N
)) then
2371 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
2374 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
2375 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
2377 -- Function body is a single expression. No need for
2383 Num_Ret
:= Num_Ret
+ 1;
2387 -- Because of the presence of private types, the views of the
2388 -- expression and the context may be different, so place an
2389 -- unchecked conversion to the context type to avoid spurious
2390 -- errors, e.g. when the expression is a numeric literal and
2391 -- the context is private. If the expression is an aggregate,
2392 -- use a qualified expression, because an aggregate is not a
2393 -- legal argument of a conversion. Ditto for numeric literals,
2394 -- which must be resolved to a specific type.
2396 if Nkind_In
(Expression
(N
), N_Aggregate
,
2402 Make_Qualified_Expression
(Sloc
(N
),
2403 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
2404 Expression
=> Relocate_Node
(Expression
(N
)));
2407 Unchecked_Convert_To
2408 (Ret_Type
, Relocate_Node
(Expression
(N
)));
2411 if Nkind
(Targ
) = N_Defining_Identifier
then
2413 Make_Assignment_Statement
(Loc
,
2414 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2415 Expression
=> Ret
));
2418 Make_Assignment_Statement
(Loc
,
2419 Name
=> New_Copy
(Targ
),
2420 Expression
=> Ret
));
2423 Set_Assignment_OK
(Name
(N
));
2425 if Present
(Exit_Lab
) then
2427 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
2433 -- An extended return becomes a block whose first statement is the
2434 -- assignment of the initial expression of the return object to the
2435 -- target of the call itself.
2437 elsif Nkind
(N
) = N_Extended_Return_Statement
then
2439 Return_Decl
: constant Entity_Id
:=
2440 First
(Return_Object_Declarations
(N
));
2444 Return_Object
:= Defining_Identifier
(Return_Decl
);
2446 if Present
(Expression
(Return_Decl
)) then
2447 if Nkind
(Targ
) = N_Defining_Identifier
then
2449 Make_Assignment_Statement
(Loc
,
2450 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2451 Expression
=> Expression
(Return_Decl
));
2454 Make_Assignment_Statement
(Loc
,
2455 Name
=> New_Copy
(Targ
),
2456 Expression
=> Expression
(Return_Decl
));
2459 Set_Assignment_OK
(Name
(Assign
));
2461 if No
(Handled_Statement_Sequence
(N
)) then
2462 Set_Handled_Statement_Sequence
(N
,
2463 Make_Handled_Sequence_Of_Statements
(Loc
,
2464 Statements
=> New_List
));
2468 Statements
(Handled_Statement_Sequence
(N
)));
2472 Make_Block_Statement
(Loc
,
2473 Handled_Statement_Sequence
=>
2474 Handled_Statement_Sequence
(N
)));
2479 -- Remove pragma Unreferenced since it may refer to formals that
2480 -- are not visible in the inlined body, and in any case we will
2481 -- not be posting warnings on the inlined body so it is unneeded.
2483 elsif Nkind
(N
) = N_Pragma
2484 and then Pragma_Name
(N
) = Name_Unreferenced
2486 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
2492 end Process_Formals
;
2494 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
2500 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
2502 if not Debug_Generated_Code
then
2503 Set_Sloc
(Nod
, Sloc
(N
));
2504 Set_Comes_From_Source
(Nod
, False);
2510 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
2512 ------------------------------
2513 -- Reset_Dispatching_Calls --
2514 ------------------------------
2516 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
2518 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
2519 -- Comment required ???
2525 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
2527 if Nkind
(N
) = N_Procedure_Call_Statement
2528 and then Nkind
(Name
(N
)) = N_Selected_Component
2529 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
2530 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
2531 and then Is_Dispatching_Operation
2532 (Entity
(Selector_Name
(Name
(N
))))
2534 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
2540 function Do_Reset_Calls
is new Traverse_Func
(Do_Reset
);
2544 Dummy
: constant Traverse_Result
:= Do_Reset_Calls
(N
);
2545 pragma Unreferenced
(Dummy
);
2547 -- Start of processing for Reset_Dispatching_Calls
2551 end Reset_Dispatching_Calls
;
2553 ---------------------------
2554 -- Rewrite_Function_Call --
2555 ---------------------------
2557 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2558 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2559 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
2562 -- Optimize simple case: function body is a single return statement,
2563 -- which has been expanded into an assignment.
2565 if Is_Empty_List
(Declarations
(Blk
))
2566 and then Nkind
(Fst
) = N_Assignment_Statement
2567 and then No
(Next
(Fst
))
2569 -- The function call may have been rewritten as the temporary
2570 -- that holds the result of the call, in which case remove the
2571 -- now useless declaration.
2573 if Nkind
(N
) = N_Identifier
2574 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2576 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
2579 Rewrite
(N
, Expression
(Fst
));
2581 elsif Nkind
(N
) = N_Identifier
2582 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2584 -- The block assigns the result of the call to the temporary
2586 Insert_After
(Parent
(Entity
(N
)), Blk
);
2588 -- If the context is an assignment, and the left-hand side is free of
2589 -- side-effects, the replacement is also safe.
2590 -- Can this be generalized further???
2592 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
2594 (Is_Entity_Name
(Name
(Parent
(N
)))
2596 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
2597 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
2600 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
2601 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
2603 -- Replace assignment with the block
2606 Original_Assignment
: constant Node_Id
:= Parent
(N
);
2609 -- Preserve the original assignment node to keep the complete
2610 -- assignment subtree consistent enough for Analyze_Assignment
2611 -- to proceed (specifically, the original Lhs node must still
2612 -- have an assignment statement as its parent).
2614 -- We cannot rely on Original_Node to go back from the block
2615 -- node to the assignment node, because the assignment might
2616 -- already be a rewrite substitution.
2618 Discard_Node
(Relocate_Node
(Original_Assignment
));
2619 Rewrite
(Original_Assignment
, Blk
);
2622 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
2624 -- A call to a function which returns an unconstrained type
2625 -- found in the expression initializing an object-declaration is
2626 -- expanded into a procedure call which must be added after the
2627 -- object declaration.
2629 if Is_Unc_Decl
and Back_End_Inlining
then
2630 Insert_Action_After
(Parent
(N
), Blk
);
2632 Set_Expression
(Parent
(N
), Empty
);
2633 Insert_After
(Parent
(N
), Blk
);
2636 elsif Is_Unc
and then not Back_End_Inlining
then
2637 Insert_Before
(Parent
(N
), Blk
);
2639 end Rewrite_Function_Call
;
2641 ----------------------------
2642 -- Rewrite_Procedure_Call --
2643 ----------------------------
2645 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2646 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2649 -- If there is a transient scope for N, this will be the scope of the
2650 -- actions for N, and the statements in Blk need to be within this
2651 -- scope. For example, they need to have visibility on the constant
2652 -- declarations created for the formals.
2654 -- If N needs no transient scope, and if there are no declarations in
2655 -- the inlined body, we can do a little optimization and insert the
2656 -- statements for the body directly after N, and rewrite N to a
2657 -- null statement, instead of rewriting N into a full-blown block
2660 if not Scope_Is_Transient
2661 and then Is_Empty_List
(Declarations
(Blk
))
2663 Insert_List_After
(N
, Statements
(HSS
));
2664 Rewrite
(N
, Make_Null_Statement
(Loc
));
2668 end Rewrite_Procedure_Call
;
2670 -------------------------
2671 -- Formal_Is_Used_Once --
2672 -------------------------
2674 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
2675 Use_Counter
: Int
:= 0;
2677 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
2678 -- Traverse the tree and count the uses of the formal parameter.
2679 -- In this case, for optimization purposes, we do not need to
2680 -- continue the traversal once more than one use is encountered.
2686 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
2688 -- The original node is an identifier
2690 if Nkind
(N
) = N_Identifier
2691 and then Present
(Entity
(N
))
2693 -- Original node's entity points to the one in the copied body
2695 and then Nkind
(Entity
(N
)) = N_Identifier
2696 and then Present
(Entity
(Entity
(N
)))
2698 -- The entity of the copied node is the formal parameter
2700 and then Entity
(Entity
(N
)) = Formal
2702 Use_Counter
:= Use_Counter
+ 1;
2704 if Use_Counter
> 1 then
2706 -- Denote more than one use and abandon the traversal
2717 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
2719 -- Start of processing for Formal_Is_Used_Once
2722 Count_Formal_Uses
(Orig_Bod
);
2723 return Use_Counter
= 1;
2724 end Formal_Is_Used_Once
;
2726 -- Start of processing for Expand_Inlined_Call
2729 -- Initializations for old/new semantics
2731 if not Back_End_Inlining
then
2732 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
2733 and then not Is_Constrained
(Etype
(Subp
));
2734 Is_Unc_Decl
:= False;
2736 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
2737 and then Optimization_Level
> 0;
2738 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
2742 -- Check for an illegal attempt to inline a recursive procedure. If the
2743 -- subprogram has parameters this is detected when trying to supply a
2744 -- binding for parameters that already have one. For parameterless
2745 -- subprograms this must be done explicitly.
2747 if In_Open_Scopes
(Subp
) then
2748 Error_Msg_N
("call to recursive subprogram cannot be inlined??", N
);
2749 Set_Is_Inlined
(Subp
, False);
2751 -- In GNATprove mode, issue a warning, and indicate that the
2752 -- subprogram is not always inlined by setting flag Is_Inlined_Always
2755 if GNATprove_Mode
then
2756 Set_Is_Inlined_Always
(Subp
, False);
2761 -- Skip inlining if this is not a true inlining since the attribute
2762 -- Body_To_Inline is also set for renamings (see sinfo.ads)
2764 elsif Nkind
(Orig_Bod
) in N_Entity
then
2767 -- Skip inlining if the function returns an unconstrained type using
2768 -- an extended return statement since this part of the new inlining
2769 -- model which is not yet supported by the current implementation. ???
2773 Nkind
(First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
2774 = N_Extended_Return_Statement
2775 and then not Back_End_Inlining
2780 if Nkind
(Orig_Bod
) = N_Defining_Identifier
2781 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
2783 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2784 -- can be replaced with calls to the renamed entity directly, because
2785 -- the subprograms are subtype conformant. If the renamed subprogram
2786 -- is an inherited operation, we must redo the expansion because
2787 -- implicit conversions may be needed. Similarly, if the renamed
2788 -- entity is inlined, expand the call for further optimizations.
2790 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
2792 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
2799 -- Register the call in the list of inlined calls
2801 Append_New_Elmt
(N
, To
=> Inlined_Calls
);
2803 -- Use generic machinery to copy body of inlined subprogram, as if it
2804 -- were an instantiation, resetting source locations appropriately, so
2805 -- that nested inlined calls appear in the main unit.
2807 Save_Env
(Subp
, Empty
);
2808 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
2812 if not Back_End_Inlining
then
2817 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
2819 Make_Block_Statement
(Loc
,
2820 Declarations
=> Declarations
(Bod
),
2821 Handled_Statement_Sequence
=>
2822 Handled_Statement_Sequence
(Bod
));
2824 if No
(Declarations
(Bod
)) then
2825 Set_Declarations
(Blk
, New_List
);
2828 -- For the unconstrained case, capture the name of the local
2829 -- variable that holds the result. This must be the first
2830 -- declaration in the block, because its bounds cannot depend
2831 -- on local variables. Otherwise there is no way to declare the
2832 -- result outside of the block. Needless to say, in general the
2833 -- bounds will depend on the actuals in the call.
2835 -- If the context is an assignment statement, as is the case
2836 -- for the expansion of an extended return, the left-hand side
2837 -- provides bounds even if the return type is unconstrained.
2841 First_Decl
: Node_Id
;
2844 First_Decl
:= First
(Declarations
(Blk
));
2846 if Nkind
(First_Decl
) /= N_Object_Declaration
then
2850 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
2851 Targ1
:= Defining_Identifier
(First_Decl
);
2853 Targ1
:= Name
(Parent
(N
));
2870 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
2872 Make_Block_Statement
(Loc
,
2873 Declarations
=> Declarations
(Bod
),
2874 Handled_Statement_Sequence
=>
2875 Handled_Statement_Sequence
(Bod
));
2877 -- Inline a call to a function that returns an unconstrained type.
2878 -- The semantic analyzer checked that frontend-inlined functions
2879 -- returning unconstrained types have no declarations and have
2880 -- a single extended return statement. As part of its processing
2881 -- the function was split in two subprograms: a procedure P and
2882 -- a function F that has a block with a call to procedure P (see
2883 -- Split_Unconstrained_Function).
2889 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)))) =
2893 Blk_Stmt
: constant Node_Id
:=
2894 First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
2895 First_Stmt
: constant Node_Id
:=
2896 First
(Statements
(Handled_Statement_Sequence
(Blk_Stmt
)));
2897 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
2901 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
2902 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
2903 and then No
(Next
(Second_Stmt
)));
2908 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
2909 Empty
, Instantiating
=> True);
2912 -- Capture the name of the local variable that holds the
2913 -- result. This must be the first declaration in the block,
2914 -- because its bounds cannot depend on local variables.
2915 -- Otherwise there is no way to declare the result outside
2916 -- of the block. Needless to say, in general the bounds will
2917 -- depend on the actuals in the call.
2919 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
2920 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
2922 -- If the context is an assignment statement, as is the case
2923 -- for the expansion of an extended return, the left-hand
2924 -- side provides bounds even if the return type is
2928 Targ1
:= Name
(Parent
(N
));
2933 if No
(Declarations
(Bod
)) then
2934 Set_Declarations
(Blk
, New_List
);
2939 -- If this is a derived function, establish the proper return type
2941 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
2942 Ret_Type
:= Etype
(Orig_Subp
);
2944 Ret_Type
:= Etype
(Subp
);
2947 -- Create temporaries for the actuals that are expressions, or that are
2948 -- scalars and require copying to preserve semantics.
2950 F
:= First_Formal
(Subp
);
2951 A
:= First_Actual
(N
);
2952 while Present
(F
) loop
2953 if Present
(Renamed_Object
(F
)) then
2955 -- If expander is active, it is an error to try to inline a
2956 -- recursive program. In GNATprove mode, just indicate that the
2957 -- inlining will not happen, and mark the subprogram as not always
2960 if GNATprove_Mode
then
2962 ("cannot inline call to recursive subprogram?", N
, Subp
);
2963 Set_Is_Inlined_Always
(Subp
, False);
2966 ("cannot inline call to recursive subprogram", N
);
2972 -- Reset Last_Assignment for any parameters of mode out or in out, to
2973 -- prevent spurious warnings about overwriting for assignments to the
2974 -- formal in the inlined code.
2976 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
2977 Set_Last_Assignment
(Entity
(A
), Empty
);
2980 -- If the argument may be a controlling argument in a call within
2981 -- the inlined body, we must preserve its classwide nature to insure
2982 -- that dynamic dispatching take place subsequently. If the formal
2983 -- has a constraint it must be preserved to retain the semantics of
2986 if Is_Class_Wide_Type
(Etype
(F
))
2987 or else (Is_Access_Type
(Etype
(F
))
2988 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
2990 Temp_Typ
:= Etype
(F
);
2992 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
2993 and then Etype
(F
) /= Base_Type
(Etype
(F
))
2995 Temp_Typ
:= Etype
(F
);
2997 Temp_Typ
:= Etype
(A
);
3000 -- If the actual is a simple name or a literal, no need to
3001 -- create a temporary, object can be used directly.
3003 -- If the actual is a literal and the formal has its address taken,
3004 -- we cannot pass the literal itself as an argument, so its value
3005 -- must be captured in a temporary.
3007 if (Is_Entity_Name
(A
)
3009 (not Is_Scalar_Type
(Etype
(A
))
3010 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
))
3012 -- When the actual is an identifier and the corresponding formal is
3013 -- used only once in the original body, the formal can be substituted
3014 -- directly with the actual parameter.
3016 or else (Nkind
(A
) = N_Identifier
3017 and then Formal_Is_Used_Once
(F
))
3020 (Nkind_In
(A
, N_Real_Literal
,
3022 N_Character_Literal
)
3023 and then not Address_Taken
(F
))
3025 if Etype
(F
) /= Etype
(A
) then
3027 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
3029 Set_Renamed_Object
(F
, A
);
3033 Temp
:= Make_Temporary
(Loc
, 'C');
3035 -- If the actual for an in/in-out parameter is a view conversion,
3036 -- make it into an unchecked conversion, given that an untagged
3037 -- type conversion is not a proper object for a renaming.
3039 -- In-out conversions that involve real conversions have already
3040 -- been transformed in Expand_Actuals.
3042 if Nkind
(A
) = N_Type_Conversion
3043 and then Ekind
(F
) /= E_In_Parameter
3046 Make_Unchecked_Type_Conversion
(Loc
,
3047 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
3048 Expression
=> Relocate_Node
(Expression
(A
)));
3050 elsif Etype
(F
) /= Etype
(A
) then
3051 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
3052 Temp_Typ
:= Etype
(F
);
3055 New_A
:= Relocate_Node
(A
);
3058 Set_Sloc
(New_A
, Sloc
(N
));
3060 -- If the actual has a by-reference type, it cannot be copied,
3061 -- so its value is captured in a renaming declaration. Otherwise
3062 -- declare a local constant initialized with the actual.
3064 -- We also use a renaming declaration for expressions of an array
3065 -- type that is not bit-packed, both for efficiency reasons and to
3066 -- respect the semantics of the call: in most cases the original
3067 -- call will pass the parameter by reference, and thus the inlined
3068 -- code will have the same semantics.
3070 -- Finally, we need a renaming declaration in the case of limited
3071 -- types for which initialization cannot be by copy either.
3073 if Ekind
(F
) = E_In_Parameter
3074 and then not Is_By_Reference_Type
(Etype
(A
))
3075 and then not Is_Limited_Type
(Etype
(A
))
3077 (not Is_Array_Type
(Etype
(A
))
3078 or else not Is_Object_Reference
(A
)
3079 or else Is_Bit_Packed_Array
(Etype
(A
)))
3082 Make_Object_Declaration
(Loc
,
3083 Defining_Identifier
=> Temp
,
3084 Constant_Present
=> True,
3085 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3086 Expression
=> New_A
);
3089 Make_Object_Renaming_Declaration
(Loc
,
3090 Defining_Identifier
=> Temp
,
3091 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3095 Append
(Decl
, Decls
);
3096 Set_Renamed_Object
(F
, Temp
);
3103 -- Establish target of function call. If context is not assignment or
3104 -- declaration, create a temporary as a target. The declaration for the
3105 -- temporary may be subsequently optimized away if the body is a single
3106 -- expression, or if the left-hand side of the assignment is simple
3107 -- enough, i.e. an entity or an explicit dereference of one.
3109 if Ekind
(Subp
) = E_Function
then
3110 if Nkind
(Parent
(N
)) = N_Assignment_Statement
3111 and then Is_Entity_Name
(Name
(Parent
(N
)))
3113 Targ
:= Name
(Parent
(N
));
3115 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3116 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3117 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3119 Targ
:= Name
(Parent
(N
));
3121 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3122 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
3123 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3125 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
3127 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
3128 and then Is_Limited_Type
(Etype
(Subp
))
3130 Targ
:= Defining_Identifier
(Parent
(N
));
3132 -- New semantics: In an object declaration avoid an extra copy
3133 -- of the result of a call to an inlined function that returns
3134 -- an unconstrained type
3136 elsif Back_End_Inlining
3137 and then Nkind
(Parent
(N
)) = N_Object_Declaration
3140 Targ
:= Defining_Identifier
(Parent
(N
));
3143 -- Replace call with temporary and create its declaration
3145 Temp
:= Make_Temporary
(Loc
, 'C');
3146 Set_Is_Internal
(Temp
);
3148 -- For the unconstrained case, the generated temporary has the
3149 -- same constrained declaration as the result variable. It may
3150 -- eventually be possible to remove that temporary and use the
3151 -- result variable directly.
3153 if Is_Unc
and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
3156 Make_Object_Declaration
(Loc
,
3157 Defining_Identifier
=> Temp
,
3158 Object_Definition
=>
3159 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3161 Replace_Formals
(Decl
);
3165 Make_Object_Declaration
(Loc
,
3166 Defining_Identifier
=> Temp
,
3167 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
3169 Set_Etype
(Temp
, Ret_Type
);
3172 Set_No_Initialization
(Decl
);
3173 Append
(Decl
, Decls
);
3174 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3179 Insert_Actions
(N
, Decls
);
3183 -- Special management for inlining a call to a function that returns
3184 -- an unconstrained type and initializes an object declaration: we
3185 -- avoid generating undesired extra calls and goto statements.
3188 -- function Func (...) return ...
3191 -- Result : String (1 .. 4);
3193 -- Proc (Result, ...);
3198 -- Result : String := Func (...);
3200 -- Replace this object declaration by:
3202 -- Result : String (1 .. 4);
3203 -- Proc (Result, ...);
3205 Remove_Homonym
(Targ
);
3208 Make_Object_Declaration
3210 Defining_Identifier
=> Targ
,
3211 Object_Definition
=>
3212 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3213 Replace_Formals
(Decl
);
3214 Rewrite
(Parent
(N
), Decl
);
3215 Analyze
(Parent
(N
));
3217 -- Avoid spurious warnings since we know that this declaration is
3218 -- referenced by the procedure call.
3220 Set_Never_Set_In_Source
(Targ
, False);
3222 -- Remove the local declaration of the extended return stmt from the
3225 Remove
(Parent
(Targ1
));
3227 -- Update the reference to the result (since we have rewriten the
3228 -- object declaration)
3231 Blk_Call_Stmt
: Node_Id
;
3234 -- Capture the call to the procedure
3237 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
3239 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
3241 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
3242 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
3243 New_Occurrence_Of
(Targ
, Loc
));
3246 -- Remove the return statement
3249 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3250 N_Simple_Return_Statement
);
3252 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3255 -- Traverse the tree and replace formals with actuals or their thunks.
3256 -- Attach block to tree before analysis and rewriting.
3258 Replace_Formals
(Blk
);
3259 Set_Parent
(Blk
, N
);
3261 if GNATprove_Mode
then
3264 elsif not Comes_From_Source
(Subp
) or else Is_Predef
then
3270 -- No action needed since return statement has been already removed
3274 elsif Present
(Exit_Lab
) then
3276 -- If the body was a single expression, the single return statement
3277 -- and the corresponding label are useless.
3281 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3284 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3286 Append
(Lab_Decl
, (Declarations
(Blk
)));
3287 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
3291 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3292 -- on conflicting private views that Gigi would ignore. If this is a
3293 -- predefined unit, analyze with checks off, as is done in the non-
3294 -- inlined run-time units.
3297 I_Flag
: constant Boolean := In_Inlined_Body
;
3300 In_Inlined_Body
:= True;
3304 Style
: constant Boolean := Style_Check
;
3307 Style_Check
:= False;
3309 -- Search for dispatching calls that use the Object.Operation
3310 -- notation using an Object that is a parameter of the inlined
3311 -- function. We reset the decoration of Operation to force
3312 -- the reanalysis of the inlined dispatching call because
3313 -- the actual object has been inlined.
3315 Reset_Dispatching_Calls
(Blk
);
3317 Analyze
(Blk
, Suppress
=> All_Checks
);
3318 Style_Check
:= Style
;
3325 In_Inlined_Body
:= I_Flag
;
3328 if Ekind
(Subp
) = E_Procedure
then
3329 Rewrite_Procedure_Call
(N
, Blk
);
3332 Rewrite_Function_Call
(N
, Blk
);
3337 -- For the unconstrained case, the replacement of the call has been
3338 -- made prior to the complete analysis of the generated declarations.
3339 -- Propagate the proper type now.
3342 if Nkind
(N
) = N_Identifier
then
3343 Set_Etype
(N
, Etype
(Entity
(N
)));
3345 Set_Etype
(N
, Etype
(Targ1
));
3352 -- Cleanup mapping between formals and actuals for other expansions
3354 F
:= First_Formal
(Subp
);
3355 while Present
(F
) loop
3356 Set_Renamed_Object
(F
, Empty
);
3359 end Expand_Inlined_Call
;
3361 --------------------------
3362 -- Get_Code_Unit_Entity --
3363 --------------------------
3365 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
is
3366 Unit
: Entity_Id
:= Cunit_Entity
(Get_Code_Unit
(E
));
3369 if Ekind
(Unit
) = E_Package_Body
then
3370 Unit
:= Spec_Entity
(Unit
);
3374 end Get_Code_Unit_Entity
;
3376 ------------------------------
3377 -- Has_Excluded_Declaration --
3378 ------------------------------
3380 function Has_Excluded_Declaration
3382 Decls
: List_Id
) return Boolean
3386 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
3387 -- Nested subprograms make a given body ineligible for inlining, but
3388 -- we make an exception for instantiations of unchecked conversion.
3389 -- The body has not been analyzed yet, so check the name, and verify
3390 -- that the visible entity with that name is the predefined unit.
3392 -----------------------------
3393 -- Is_Unchecked_Conversion --
3394 -----------------------------
3396 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
3397 Id
: constant Node_Id
:= Name
(D
);
3401 if Nkind
(Id
) = N_Identifier
3402 and then Chars
(Id
) = Name_Unchecked_Conversion
3404 Conv
:= Current_Entity
(Id
);
3406 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
3407 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
3409 Conv
:= Current_Entity
(Selector_Name
(Id
));
3414 return Present
(Conv
)
3415 and then Is_Predefined_File_Name
3416 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
3417 and then Is_Intrinsic_Subprogram
(Conv
);
3418 end Is_Unchecked_Conversion
;
3420 -- Start of processing for Has_Excluded_Declaration
3423 -- No action needed if the check is not needed
3425 if not Check_Inlining_Restrictions
then
3430 while Present
(D
) loop
3431 if Nkind
(D
) = N_Subprogram_Body
then
3433 ("cannot inline & (nested subprogram)?",
3437 elsif Nkind
(D
) = N_Task_Type_Declaration
3438 or else Nkind
(D
) = N_Single_Task_Declaration
3441 ("cannot inline & (nested task type declaration)?",
3445 elsif Nkind
(D
) = N_Protected_Type_Declaration
3446 or else Nkind
(D
) = N_Single_Protected_Declaration
3449 ("cannot inline & (nested protected type declaration)?",
3453 elsif Nkind
(D
) = N_Package_Declaration
then
3455 ("cannot inline & (nested package declaration)?",
3459 elsif Nkind
(D
) = N_Function_Instantiation
3460 and then not Is_Unchecked_Conversion
(D
)
3463 ("cannot inline & (nested function instantiation)?",
3467 elsif Nkind
(D
) = N_Procedure_Instantiation
then
3469 ("cannot inline & (nested procedure instantiation)?",
3473 elsif Nkind
(D
) = N_Package_Instantiation
then
3475 ("cannot inline & (nested package instantiation)?",
3484 end Has_Excluded_Declaration
;
3486 ----------------------------
3487 -- Has_Excluded_Statement --
3488 ----------------------------
3490 function Has_Excluded_Statement
3492 Stats
: List_Id
) return Boolean
3498 -- No action needed if the check is not needed
3500 if not Check_Inlining_Restrictions
then
3505 while Present
(S
) loop
3506 if Nkind_In
(S
, N_Abort_Statement
,
3507 N_Asynchronous_Select
,
3508 N_Conditional_Entry_Call
,
3509 N_Delay_Relative_Statement
,
3510 N_Delay_Until_Statement
,
3515 ("cannot inline & (non-allowed statement)?", S
, Subp
);
3518 elsif Nkind
(S
) = N_Block_Statement
then
3519 if Present
(Declarations
(S
))
3520 and then Has_Excluded_Declaration
(Subp
, Declarations
(S
))
3524 elsif Present
(Handled_Statement_Sequence
(S
)) then
3525 if not Back_End_Inlining
3528 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
3531 ("cannot inline& (exception handler)?",
3532 First
(Exception_Handlers
3533 (Handled_Statement_Sequence
(S
))),
3537 elsif Has_Excluded_Statement
3538 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
3544 elsif Nkind
(S
) = N_Case_Statement
then
3545 E
:= First
(Alternatives
(S
));
3546 while Present
(E
) loop
3547 if Has_Excluded_Statement
(Subp
, Statements
(E
)) then
3554 elsif Nkind
(S
) = N_If_Statement
then
3555 if Has_Excluded_Statement
(Subp
, Then_Statements
(S
)) then
3559 if Present
(Elsif_Parts
(S
)) then
3560 E
:= First
(Elsif_Parts
(S
));
3561 while Present
(E
) loop
3562 if Has_Excluded_Statement
(Subp
, Then_Statements
(E
)) then
3570 if Present
(Else_Statements
(S
))
3571 and then Has_Excluded_Statement
(Subp
, Else_Statements
(S
))
3576 elsif Nkind
(S
) = N_Loop_Statement
3577 and then Has_Excluded_Statement
(Subp
, Statements
(S
))
3581 elsif Nkind
(S
) = N_Extended_Return_Statement
then
3582 if Present
(Handled_Statement_Sequence
(S
))
3584 Has_Excluded_Statement
3585 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
3589 elsif not Back_End_Inlining
3590 and then Present
(Handled_Statement_Sequence
(S
))
3592 Present
(Exception_Handlers
3593 (Handled_Statement_Sequence
(S
)))
3596 ("cannot inline& (exception handler)?",
3597 First
(Exception_Handlers
(Handled_Statement_Sequence
(S
))),
3607 end Has_Excluded_Statement
;
3609 --------------------------
3610 -- Has_Initialized_Type --
3611 --------------------------
3613 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean is
3614 E_Body
: constant Node_Id
:= Get_Subprogram_Body
(E
);
3618 if No
(E_Body
) then -- imported subprogram
3622 Decl
:= First
(Declarations
(E_Body
));
3623 while Present
(Decl
) loop
3624 if Nkind
(Decl
) = N_Full_Type_Declaration
3625 and then Present
(Init_Proc
(Defining_Identifier
(Decl
)))
3635 end Has_Initialized_Type
;
3637 -----------------------
3638 -- Has_Single_Return --
3639 -----------------------
3641 function Has_Single_Return
(N
: Node_Id
) return Boolean is
3642 Return_Statement
: Node_Id
:= Empty
;
3644 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
3650 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
3652 if Nkind
(N
) = N_Simple_Return_Statement
then
3653 if Present
(Expression
(N
))
3654 and then Is_Entity_Name
(Expression
(N
))
3656 if No
(Return_Statement
) then
3657 Return_Statement
:= N
;
3660 elsif Chars
(Expression
(N
)) =
3661 Chars
(Expression
(Return_Statement
))
3669 -- A return statement within an extended return is a noop
3672 elsif No
(Expression
(N
))
3674 Nkind
(Parent
(Parent
(N
))) = N_Extended_Return_Statement
3679 -- Expression has wrong form
3684 -- We can only inline a build-in-place function if it has a single
3687 elsif Nkind
(N
) = N_Extended_Return_Statement
then
3688 if No
(Return_Statement
) then
3689 Return_Statement
:= N
;
3701 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
3703 -- Start of processing for Has_Single_Return
3706 if Check_All_Returns
(N
) /= OK
then
3709 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
3713 return Present
(Declarations
(N
))
3714 and then Present
(First
(Declarations
(N
)))
3715 and then Chars
(Expression
(Return_Statement
)) =
3716 Chars
(Defining_Identifier
(First
(Declarations
(N
))));
3718 end Has_Single_Return
;
3720 -----------------------------
3721 -- In_Main_Unit_Or_Subunit --
3722 -----------------------------
3724 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean is
3725 Comp
: Node_Id
:= Cunit
(Get_Code_Unit
(E
));
3728 -- Check whether the subprogram or package to inline is within the main
3729 -- unit or its spec or within a subunit. In either case there are no
3730 -- additional bodies to process. If the subprogram appears in a parent
3731 -- of the current unit, the check on whether inlining is possible is
3732 -- done in Analyze_Inlined_Bodies.
3734 while Nkind
(Unit
(Comp
)) = N_Subunit
loop
3735 Comp
:= Library_Unit
(Comp
);
3738 return Comp
= Cunit
(Main_Unit
)
3739 or else Comp
= Library_Unit
(Cunit
(Main_Unit
));
3740 end In_Main_Unit_Or_Subunit
;
3746 procedure Initialize
is
3748 Pending_Descriptor
.Init
;
3749 Pending_Instantiations
.Init
;
3750 Inlined_Bodies
.Init
;
3754 for J
in Hash_Headers
'Range loop
3755 Hash_Headers
(J
) := No_Subp
;
3758 Inlined_Calls
:= No_Elist
;
3759 Backend_Calls
:= No_Elist
;
3760 Backend_Inlined_Subps
:= No_Elist
;
3761 Backend_Not_Inlined_Subps
:= No_Elist
;
3764 ------------------------
3765 -- Instantiate_Bodies --
3766 ------------------------
3768 -- Generic bodies contain all the non-local references, so an
3769 -- instantiation does not need any more context than Standard
3770 -- itself, even if the instantiation appears in an inner scope.
3771 -- Generic associations have verified that the contract model is
3772 -- satisfied, so that any error that may occur in the analysis of
3773 -- the body is an internal error.
3775 procedure Instantiate_Bodies
is
3777 Info
: Pending_Body_Info
;
3780 if Serious_Errors_Detected
= 0 then
3781 Expander_Active
:= (Operating_Mode
= Opt
.Generate_Code
);
3782 Push_Scope
(Standard_Standard
);
3783 To_Clean
:= New_Elmt_List
;
3785 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
3789 -- A body instantiation may generate additional instantiations, so
3790 -- the following loop must scan to the end of a possibly expanding
3791 -- set (that's why we can't simply use a FOR loop here).
3794 while J
<= Pending_Instantiations
.Last
3795 and then Serious_Errors_Detected
= 0
3797 Info
:= Pending_Instantiations
.Table
(J
);
3799 -- If the instantiation node is absent, it has been removed
3800 -- as part of unreachable code.
3802 if No
(Info
.Inst_Node
) then
3805 elsif Nkind
(Info
.Act_Decl
) = N_Package_Declaration
then
3806 Instantiate_Package_Body
(Info
);
3807 Add_Scope_To_Clean
(Defining_Entity
(Info
.Act_Decl
));
3810 Instantiate_Subprogram_Body
(Info
);
3816 -- Reset the table of instantiations. Additional instantiations
3817 -- may be added through inlining, when additional bodies are
3820 Pending_Instantiations
.Init
;
3822 -- We can now complete the cleanup actions of scopes that contain
3823 -- pending instantiations (skipped for generic units, since we
3824 -- never need any cleanups in generic units).
3825 -- pending instantiations.
3828 and then not Is_Generic_Unit
(Main_Unit_Entity
)
3831 elsif Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
3837 end Instantiate_Bodies
;
3843 function Is_Nested
(E
: Entity_Id
) return Boolean is
3848 while Scop
/= Standard_Standard
loop
3849 if Ekind
(Scop
) in Subprogram_Kind
then
3852 elsif Ekind
(Scop
) = E_Task_Type
3853 or else Ekind
(Scop
) = E_Entry
3854 or else Ekind
(Scop
) = E_Entry_Family
3859 Scop
:= Scope
(Scop
);
3865 ------------------------
3866 -- List_Inlining_Info --
3867 ------------------------
3869 procedure List_Inlining_Info
is
3875 if not Debug_Flag_Dot_J
then
3879 -- Generate listing of calls inlined by the frontend
3881 if Present
(Inlined_Calls
) then
3883 Elmt
:= First_Elmt
(Inlined_Calls
);
3884 while Present
(Elmt
) loop
3887 if In_Extended_Main_Code_Unit
(Nod
) then
3891 Write_Str
("Listing of frontend inlined calls");
3898 Write_Location
(Sloc
(Nod
));
3907 -- Generate listing of calls passed to the backend
3909 if Present
(Backend_Calls
) then
3912 Elmt
:= First_Elmt
(Backend_Calls
);
3913 while Present
(Elmt
) loop
3916 if In_Extended_Main_Code_Unit
(Nod
) then
3920 Write_Str
("Listing of inlined calls passed to the backend");
3927 Write_Location
(Sloc
(Nod
));
3935 -- Generate listing of subprograms passed to the backend
3937 if Present
(Backend_Inlined_Subps
)
3938 and then Back_End_Inlining
3942 Elmt
:= First_Elmt
(Backend_Inlined_Subps
);
3943 while Present
(Elmt
) loop
3950 ("Listing of inlined subprograms passed to the backend");
3957 Write_Name
(Chars
(Nod
));
3959 Write_Location
(Sloc
(Nod
));
3967 -- Generate listing of subprogram that cannot be inlined by the backend
3969 if Present
(Backend_Not_Inlined_Subps
)
3970 and then Back_End_Inlining
3974 Elmt
:= First_Elmt
(Backend_Not_Inlined_Subps
);
3975 while Present
(Elmt
) loop
3982 ("Listing of subprograms that cannot inline the backend");
3989 Write_Name
(Chars
(Nod
));
3991 Write_Location
(Sloc
(Nod
));
3998 end List_Inlining_Info
;
4006 Pending_Instantiations
.Locked
:= True;
4007 Inlined_Bodies
.Locked
:= True;
4008 Successors
.Locked
:= True;
4009 Inlined
.Locked
:= True;
4010 Pending_Instantiations
.Release
;
4011 Inlined_Bodies
.Release
;
4016 ---------------------------
4017 -- Register_Backend_Call --
4018 ---------------------------
4020 procedure Register_Backend_Call
(N
: Node_Id
) is
4022 Append_New_Elmt
(N
, To
=> Backend_Calls
);
4023 end Register_Backend_Call
;
4025 --------------------------
4026 -- Remove_Dead_Instance --
4027 --------------------------
4029 procedure Remove_Dead_Instance
(N
: Node_Id
) is
4034 while J
<= Pending_Instantiations
.Last
loop
4035 if Pending_Instantiations
.Table
(J
).Inst_Node
= N
then
4036 Pending_Instantiations
.Table
(J
).Inst_Node
:= Empty
;
4042 end Remove_Dead_Instance
;
4044 --------------------
4045 -- Remove_Pragmas --
4046 --------------------
4048 procedure Remove_Pragmas
(Bod
: Node_Id
) is
4053 Decl
:= First
(Declarations
(Bod
));
4054 while Present
(Decl
) loop
4057 if Nkind
(Decl
) = N_Pragma
4058 and then Nam_In
(Pragma_Name
(Decl
), Name_Contract_Cases
,