1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 ------------------------------------------------------------------------------
27 with Aspects
; use Aspects
;
28 with Atree
; use Atree
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Elists
; use Elists
;
32 with Errout
; use Errout
;
33 with Expander
; use Expander
;
34 with Exp_Ch6
; use Exp_Ch6
;
35 with Exp_Ch7
; use Exp_Ch7
;
36 with Exp_Tss
; use Exp_Tss
;
37 with Exp_Util
; use Exp_Util
;
38 with Fname
; use Fname
;
39 with Fname
.UF
; use Fname
.UF
;
41 with Namet
; use Namet
;
42 with Nmake
; use Nmake
;
43 with Nlists
; use Nlists
;
44 with Output
; use Output
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Ch8
; use Sem_Ch8
;
47 with Sem_Ch10
; use Sem_Ch10
;
48 with Sem_Ch12
; use Sem_Ch12
;
49 with Sem_Prag
; use Sem_Prag
;
50 with Sem_Util
; use Sem_Util
;
51 with Sinfo
; use Sinfo
;
52 with Sinput
; use Sinput
;
53 with Snames
; use Snames
;
54 with Stand
; use Stand
;
56 with Tbuild
; use Tbuild
;
57 with Uintp
; use Uintp
;
58 with Uname
; use Uname
;
62 package body Inline
is
64 Check_Inlining_Restrictions
: constant Boolean := True;
65 -- In the following cases the frontend rejects inlining because they
66 -- are not handled well by the backend. This variable facilitates
67 -- disabling these restrictions to evaluate future versions of the
68 -- GCC backend in which some of the restrictions may be supported.
70 -- - subprograms that have:
71 -- - nested subprograms
73 -- - package declarations
74 -- - task or protected object declarations
75 -- - some of the following statements:
77 -- - asynchronous-select
78 -- - conditional-entry-call
84 Inlined_Calls
: Elist_Id
;
85 -- List of frontend inlined calls
87 Backend_Calls
: Elist_Id
;
88 -- List of inline calls passed to the backend
90 Backend_Instances
: Elist_Id
;
91 -- List of instances inlined for the backend
93 Backend_Inlined_Subps
: Elist_Id
;
94 -- List of subprograms inlined by the backend
96 Backend_Not_Inlined_Subps
: Elist_Id
;
97 -- List of subprograms that cannot be inlined by the backend
99 -----------------------------
100 -- Pending_Instantiations --
101 -----------------------------
103 -- We make entries in this table for the pending instantiations of generic
104 -- bodies that are created during semantic analysis. After the analysis is
105 -- complete, calling Instantiate_Bodies performs the actual instantiations.
107 package Pending_Instantiations
is new Table
.Table
(
108 Table_Component_Type
=> Pending_Body_Info
,
109 Table_Index_Type
=> Int
,
110 Table_Low_Bound
=> 0,
111 Table_Initial
=> Alloc
.Pending_Instantiations_Initial
,
112 Table_Increment
=> Alloc
.Pending_Instantiations_Increment
,
113 Table_Name
=> "Pending_Instantiations");
115 -------------------------------------
116 -- Called_Pending_Instantiations --
117 -------------------------------------
119 -- With back-end inlining, the pending instantiations that are not in the
120 -- main unit or subunit are performed only after a call to the subprogram
121 -- instance, or to a subprogram within the package instance, is inlined.
122 -- Since such a call can be within a subsequent pending instantiation,
123 -- we make entries in this table that stores the index of these "called"
124 -- pending instantiations and perform them when the table is populated.
126 package Called_Pending_Instantiations
is new Table
.Table
(
127 Table_Component_Type
=> Int
,
128 Table_Index_Type
=> Int
,
129 Table_Low_Bound
=> 0,
130 Table_Initial
=> Alloc
.Pending_Instantiations_Initial
,
131 Table_Increment
=> Alloc
.Pending_Instantiations_Increment
,
132 Table_Name
=> "Called_Pending_Instantiations");
134 ---------------------------------
135 -- To_Pending_Instantiations --
136 ---------------------------------
138 -- With back-end inlining, we also need to have a map from the pending
139 -- instantiations to their index in the Pending_Instantiations table.
141 Node_Table_Size
: constant := 257;
142 -- Number of headers in hash table
144 subtype Node_Header_Num
is Integer range 0 .. Node_Table_Size
- 1;
145 -- Range of headers in hash table
147 function Node_Hash
(Id
: Node_Id
) return Node_Header_Num
;
148 -- Simple hash function for Node_Ids
150 package To_Pending_Instantiations
is new GNAT
.Htable
.Simple_HTable
151 (Header_Num
=> Node_Header_Num
,
162 function Node_Hash
(Id
: Node_Id
) return Node_Header_Num
is
164 return Node_Header_Num
(Id
mod Node_Table_Size
);
171 -- Inlined functions are actually placed in line by the backend if the
172 -- corresponding bodies are available (i.e. compiled). Whenever we find
173 -- a call to an inlined subprogram, we add the name of the enclosing
174 -- compilation unit to a worklist. After all compilation, and after
175 -- expansion of generic bodies, we traverse the list of pending bodies
176 -- and compile them as well.
178 package Inlined_Bodies
is new Table
.Table
(
179 Table_Component_Type
=> Entity_Id
,
180 Table_Index_Type
=> Int
,
181 Table_Low_Bound
=> 0,
182 Table_Initial
=> Alloc
.Inlined_Bodies_Initial
,
183 Table_Increment
=> Alloc
.Inlined_Bodies_Increment
,
184 Table_Name
=> "Inlined_Bodies");
186 -----------------------
187 -- Inline Processing --
188 -----------------------
190 -- For each call to an inlined subprogram, we make entries in a table
191 -- that stores caller and callee, and indicates the call direction from
192 -- one to the other. We also record the compilation unit that contains
193 -- the callee. After analyzing the bodies of all such compilation units,
194 -- we compute the transitive closure of inlined subprograms called from
195 -- the main compilation unit and make it available to the code generator
196 -- in no particular order, thus allowing cycles in the call graph.
198 Last_Inlined
: Entity_Id
:= Empty
;
200 -- For each entry in the table we keep a list of successors in topological
201 -- order, i.e. callers of the current subprogram.
203 type Subp_Index
is new Nat
;
204 No_Subp
: constant Subp_Index
:= 0;
206 -- The subprogram entities are hashed into the Inlined table
208 Num_Hash_Headers
: constant := 512;
210 Hash_Headers
: array (Subp_Index
range 0 .. Num_Hash_Headers
- 1)
213 type Succ_Index
is new Nat
;
214 No_Succ
: constant Succ_Index
:= 0;
216 type Succ_Info
is record
221 -- The following table stores list elements for the successor lists. These
222 -- lists cannot be chained directly through entries in the Inlined table,
223 -- because a given subprogram can appear in several such lists.
225 package Successors
is new Table
.Table
(
226 Table_Component_Type
=> Succ_Info
,
227 Table_Index_Type
=> Succ_Index
,
228 Table_Low_Bound
=> 1,
229 Table_Initial
=> Alloc
.Successors_Initial
,
230 Table_Increment
=> Alloc
.Successors_Increment
,
231 Table_Name
=> "Successors");
233 type Subp_Info
is record
234 Name
: Entity_Id
:= Empty
;
235 Next
: Subp_Index
:= No_Subp
;
236 First_Succ
: Succ_Index
:= No_Succ
;
237 Main_Call
: Boolean := False;
238 Processed
: Boolean := False;
241 package Inlined
is new Table
.Table
(
242 Table_Component_Type
=> Subp_Info
,
243 Table_Index_Type
=> Subp_Index
,
244 Table_Low_Bound
=> 1,
245 Table_Initial
=> Alloc
.Inlined_Initial
,
246 Table_Increment
=> Alloc
.Inlined_Increment
,
247 Table_Name
=> "Inlined");
249 -----------------------
250 -- Local Subprograms --
251 -----------------------
253 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
);
254 -- Make two entries in Inlined table, for an inlined subprogram being
255 -- called, and for the inlined subprogram that contains the call. If
256 -- the call is in the main compilation unit, Caller is Empty.
258 procedure Add_Inlined_Instance
(E
: Entity_Id
);
259 -- Add instance E to the list of of inlined instances for the unit
261 procedure Add_Inlined_Subprogram
(E
: Entity_Id
);
262 -- Add subprogram E to the list of inlined subprograms for the unit
264 function Add_Subp
(E
: Entity_Id
) return Subp_Index
;
265 -- Make entry in Inlined table for subprogram E, or return table index
266 -- that already holds E.
268 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
;
269 pragma Inline
(Get_Code_Unit_Entity
);
270 -- Return the entity node for the unit containing E. Always return the spec
273 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean;
274 -- If a candidate for inlining contains type declarations for types with
275 -- nontrivial initialization procedures, they are not worth inlining.
277 function Has_Single_Return
(N
: Node_Id
) return Boolean;
278 -- In general we cannot inline functions that return unconstrained type.
279 -- However, we can handle such functions if all return statements return
280 -- a local variable that is the first declaration in the body of the
281 -- function. In that case the call can be replaced by that local
282 -- variable as is done for other inlined calls.
284 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean;
285 -- Return True if E is in the main unit or its spec or in a subunit
287 function Is_Nested
(E
: Entity_Id
) return Boolean;
288 -- If the function is nested inside some other function, it will always
289 -- be compiled if that function is, so don't add it to the inline list.
290 -- We cannot compile a nested function outside the scope of the containing
291 -- function anyway. This is also the case if the function is defined in a
292 -- task body or within an entry (for example, an initialization procedure).
294 procedure Remove_Aspects_And_Pragmas
(Body_Decl
: Node_Id
);
295 -- Remove all aspects and/or pragmas that have no meaning in inlined body
296 -- Body_Decl. The analysis of these items is performed on the non-inlined
297 -- body. The items currently removed are:
310 ------------------------------
311 -- Deferred Cleanup Actions --
312 ------------------------------
314 -- The cleanup actions for scopes that contain instantiations is delayed
315 -- until after expansion of those instantiations, because they may contain
316 -- finalizable objects or tasks that affect the cleanup code. A scope
317 -- that contains instantiations only needs to be finalized once, even
318 -- if it contains more than one instance. We keep a list of scopes
319 -- that must still be finalized, and call cleanup_actions after all
320 -- the instantiations have been completed.
324 procedure Add_Scope_To_Clean
(Inst
: Entity_Id
);
325 -- Build set of scopes on which cleanup actions must be performed
327 procedure Cleanup_Scopes
;
328 -- Complete cleanup actions on scopes that need it
334 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
) is
335 P1
: constant Subp_Index
:= Add_Subp
(Called
);
340 if Present
(Caller
) then
341 P2
:= Add_Subp
(Caller
);
343 -- Add P1 to the list of successors of P2, if not already there.
344 -- Note that P2 may contain more than one call to P1, and only
345 -- one needs to be recorded.
347 J
:= Inlined
.Table
(P2
).First_Succ
;
348 while J
/= No_Succ
loop
349 if Successors
.Table
(J
).Subp
= P1
then
353 J
:= Successors
.Table
(J
).Next
;
356 -- On exit, make a successor entry for P1
358 Successors
.Increment_Last
;
359 Successors
.Table
(Successors
.Last
).Subp
:= P1
;
360 Successors
.Table
(Successors
.Last
).Next
:=
361 Inlined
.Table
(P2
).First_Succ
;
362 Inlined
.Table
(P2
).First_Succ
:= Successors
.Last
;
364 Inlined
.Table
(P1
).Main_Call
:= True;
368 ----------------------
369 -- Add_Inlined_Body --
370 ----------------------
372 procedure Add_Inlined_Body
(E
: Entity_Id
; N
: Node_Id
) is
374 type Inline_Level_Type
is (Dont_Inline
, Inline_Call
, Inline_Package
);
375 -- Level of inlining for the call: Dont_Inline means no inlining,
376 -- Inline_Call means that only the call is considered for inlining,
377 -- Inline_Package means that the call is considered for inlining and
378 -- its package compiled and scanned for more inlining opportunities.
380 function Is_Non_Loading_Expression_Function
381 (Id
: Entity_Id
) return Boolean;
382 -- Determine whether arbitrary entity Id denotes a subprogram which is
385 -- * An expression function
387 -- * A function completed by an expression function where both the
388 -- spec and body are in the same context.
390 function Must_Inline
return Inline_Level_Type
;
391 -- Inlining is only done if the call statement N is in the main unit,
392 -- or within the body of another inlined subprogram.
394 ----------------------------------------
395 -- Is_Non_Loading_Expression_Function --
396 ----------------------------------------
398 function Is_Non_Loading_Expression_Function
399 (Id
: Entity_Id
) return Boolean
406 -- A stand-alone expression function is transformed into a spec-body
407 -- pair in-place. Since both the spec and body are in the same list,
408 -- the inlining of such an expression function does not need to load
411 if Is_Expression_Function
(Id
) then
414 -- A function may be completed by an expression function
416 elsif Ekind
(Id
) = E_Function
then
417 Spec_Decl
:= Unit_Declaration_Node
(Id
);
419 if Nkind
(Spec_Decl
) = N_Subprogram_Declaration
then
420 Body_Id
:= Corresponding_Body
(Spec_Decl
);
422 if Present
(Body_Id
) then
423 Body_Decl
:= Unit_Declaration_Node
(Body_Id
);
425 -- The inlining of a completing expression function does
426 -- not need to load anything extra when both the spec and
427 -- body are in the same context.
430 Was_Expression_Function
(Body_Decl
)
431 and then Parent
(Spec_Decl
) = Parent
(Body_Decl
);
437 end Is_Non_Loading_Expression_Function
;
443 function Must_Inline
return Inline_Level_Type
is
448 -- Check if call is in main unit
450 Scop
:= Current_Scope
;
452 -- Do not try to inline if scope is standard. This could happen, for
453 -- example, for a call to Add_Global_Declaration, and it causes
454 -- trouble to try to inline at this level.
456 if Scop
= Standard_Standard
then
460 -- Otherwise lookup scope stack to outer scope
462 while Scope
(Scop
) /= Standard_Standard
463 and then not Is_Child_Unit
(Scop
)
465 Scop
:= Scope
(Scop
);
468 Comp
:= Parent
(Scop
);
469 while Nkind
(Comp
) /= N_Compilation_Unit
loop
470 Comp
:= Parent
(Comp
);
473 -- If the call is in the main unit, inline the call and compile the
474 -- package of the subprogram to find more calls to be inlined.
476 if Comp
= Cunit
(Main_Unit
)
477 or else Comp
= Library_Unit
(Cunit
(Main_Unit
))
480 return Inline_Package
;
483 -- The call is not in the main unit. See if it is in some subprogram
484 -- that can be inlined outside its unit. If so, inline the call and,
485 -- if the inlining level is set to 1, stop there; otherwise also
486 -- compile the package as above.
488 Scop
:= Current_Scope
;
489 while Scope
(Scop
) /= Standard_Standard
490 and then not Is_Child_Unit
(Scop
)
492 if Is_Overloadable
(Scop
)
493 and then Is_Inlined
(Scop
)
494 and then not Is_Nested
(Scop
)
498 if Inline_Level
= 1 then
501 return Inline_Package
;
505 Scop
:= Scope
(Scop
);
514 Level
: Inline_Level_Type
;
516 -- Start of processing for Add_Inlined_Body
519 Append_New_Elmt
(N
, To
=> Backend_Calls
);
521 -- Skip subprograms that cannot or need not be inlined outside their
522 -- unit or parent subprogram.
524 if Is_Abstract_Subprogram
(E
)
525 or else Convention
(E
) = Convention_Protected
526 or else In_Main_Unit_Or_Subunit
(E
)
527 or else Is_Nested
(E
)
532 -- Find out whether the call must be inlined. Unless the result is
533 -- Dont_Inline, Must_Inline also creates an edge for the call in the
534 -- callgraph; however, it will not be activated until after Is_Called
535 -- is set on the subprogram.
537 Level
:= Must_Inline
;
539 if Level
= Dont_Inline
then
543 -- If a previous call to the subprogram has been inlined, nothing to do
545 if Is_Called
(E
) then
549 -- If the subprogram is an instance, then inline the instance
551 if Is_Generic_Instance
(E
) then
552 Add_Inlined_Instance
(E
);
555 -- Mark the subprogram as called
559 -- If the call was generated by the compiler and is to a subprogram in
560 -- a run-time unit, we need to suppress debugging information for it,
561 -- so that the code that is eventually inlined will not affect the
562 -- debugging of the program. We do not do it if the call comes from
563 -- source because, even if the call is inlined, the user may expect it
564 -- to be present in the debugging information.
566 if not Comes_From_Source
(N
)
567 and then In_Extended_Main_Source_Unit
(N
)
568 and then Is_Predefined_Unit
(Get_Source_Unit
(E
))
570 Set_Needs_Debug_Info
(E
, False);
573 -- If the subprogram is an expression function, or is completed by one
574 -- where both the spec and body are in the same context, then there is
575 -- no need to load any package body since the body of the function is
578 if Is_Non_Loading_Expression_Function
(E
) then
582 -- Find unit containing E, and add to list of inlined bodies if needed.
583 -- Library-level functions must be handled specially, because there is
584 -- no enclosing package to retrieve. In this case, it is the body of
585 -- the function that will have to be loaded.
588 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
592 Inlined_Bodies
.Increment_Last
;
593 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := E
;
596 pragma Assert
(Ekind
(Pack
) = E_Package
);
598 -- If the subprogram is within an instance, inline the instance
600 if Comes_From_Source
(E
) then
603 while Present
(Inst
) and then Inst
/= Standard_Standard
loop
604 exit when Is_Generic_Instance
(Inst
);
605 Inst
:= Scope
(Inst
);
609 and then Is_Generic_Instance
(Inst
)
610 and then not Is_Called
(Inst
)
612 -- Do not add a pending instantiation if the body exits
613 -- already, or if the instance is a compilation unit, or
614 -- the instance node is missing.
616 Inst_Decl
:= Unit_Declaration_Node
(Inst
);
617 if Present
(Corresponding_Body
(Inst_Decl
))
618 or else Nkind
(Parent
(Inst_Decl
)) = N_Compilation_Unit
619 or else No
(Next
(Inst_Decl
))
621 Set_Is_Called
(Inst
);
624 -- If the inlined call itself appears within an instance,
625 -- ensure that the enclosing instance body is available.
626 -- This is necessary because Sem_Ch12.Might_Inline_Subp
627 -- does not recurse into nested instantiations.
629 if not Is_Inlined
(Inst
) and then In_Instance
then
630 Set_Is_Inlined
(Inst
);
632 -- The instantiation node usually follows the package
633 -- declaration for the instance. If the generic unit
634 -- has aspect specifications, they are transformed
635 -- into pragmas in the instance, and the instance node
636 -- appears after them.
638 Inst_Node
:= Next
(Inst_Decl
);
640 while Nkind
(Inst_Node
) /= N_Package_Instantiation
loop
641 Inst_Node
:= Next
(Inst_Node
);
644 Add_Pending_Instantiation
(Inst_Node
, Inst_Decl
);
647 Add_Inlined_Instance
(Inst
);
652 -- If the unit containing E is an instance, then the instance body
653 -- will be analyzed in any case, see Sem_Ch12.Might_Inline_Subp.
655 if Is_Generic_Instance
(Pack
) then
658 -- Do not inline the package if the subprogram is an init proc
659 -- or other internally generated subprogram, because in that
660 -- case the subprogram body appears in the same unit that
661 -- declares the type, and that body is visible to the back end.
662 -- Do not inline it either if it is in the main unit.
663 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
664 -- calls if the back-end takes care of inlining the call.
665 -- Note that Level is in Inline_Call | Inline_Packag here.
667 elsif ((Level
= Inline_Call
668 and then Has_Pragma_Inline_Always
(E
)
669 and then Back_End_Inlining
)
670 or else Level
= Inline_Package
)
671 and then not Is_Inlined
(Pack
)
672 and then not Is_Internal
(E
)
673 and then not In_Main_Unit_Or_Subunit
(Pack
)
675 Set_Is_Inlined
(Pack
);
676 Inlined_Bodies
.Increment_Last
;
677 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := Pack
;
681 -- Ensure that Analyze_Inlined_Bodies will be invoked after
682 -- completing the analysis of the current unit.
684 Inline_Processing_Required
:= True;
686 end Add_Inlined_Body
;
688 --------------------------
689 -- Add_Inlined_Instance --
690 --------------------------
692 procedure Add_Inlined_Instance
(E
: Entity_Id
) is
693 Decl_Node
: constant Node_Id
:= Unit_Declaration_Node
(E
);
697 -- This machinery is only used with back-end inlining
699 if not Back_End_Inlining
then
703 -- Register the instance in the list
705 Append_New_Elmt
(Decl_Node
, To
=> Backend_Instances
);
707 -- Retrieve the index of its corresponding pending instantiation
708 -- and mark this corresponding pending instantiation as needed.
710 Index
:= To_Pending_Instantiations
.Get
(Decl_Node
);
712 Called_Pending_Instantiations
.Append
(Index
);
714 pragma Assert
(False);
719 end Add_Inlined_Instance
;
721 ----------------------------
722 -- Add_Inlined_Subprogram --
723 ----------------------------
725 procedure Add_Inlined_Subprogram
(E
: Entity_Id
) is
726 Decl
: constant Node_Id
:= Parent
(Declaration_Node
(E
));
727 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
729 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
);
730 -- Append Subp to the list of subprograms inlined by the backend
732 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
);
733 -- Append Subp to the list of subprograms that cannot be inlined by
736 -----------------------------------------
737 -- Register_Backend_Inlined_Subprogram --
738 -----------------------------------------
740 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
) is
742 Append_New_Elmt
(Subp
, To
=> Backend_Inlined_Subps
);
743 end Register_Backend_Inlined_Subprogram
;
745 ---------------------------------------------
746 -- Register_Backend_Not_Inlined_Subprogram --
747 ---------------------------------------------
749 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
) is
751 Append_New_Elmt
(Subp
, To
=> Backend_Not_Inlined_Subps
);
752 end Register_Backend_Not_Inlined_Subprogram
;
754 -- Start of processing for Add_Inlined_Subprogram
757 -- We can inline the subprogram if its unit is known to be inlined or is
758 -- an instance whose body will be analyzed anyway or the subprogram was
759 -- generated as a body by the compiler (for example an initialization
760 -- procedure) or its declaration was provided along with the body (for
761 -- example an expression function) and it does not declare types with
762 -- nontrivial initialization procedures.
764 if (Is_Inlined
(Pack
)
765 or else Is_Generic_Instance
(Pack
)
766 or else Nkind
(Decl
) = N_Subprogram_Body
767 or else Present
(Corresponding_Body
(Decl
)))
768 and then not Has_Initialized_Type
(E
)
770 Register_Backend_Inlined_Subprogram
(E
);
772 if No
(Last_Inlined
) then
773 Set_First_Inlined_Subprogram
(Cunit
(Main_Unit
), E
);
775 Set_Next_Inlined_Subprogram
(Last_Inlined
, E
);
781 Register_Backend_Not_Inlined_Subprogram
(E
);
783 end Add_Inlined_Subprogram
;
785 --------------------------------
786 -- Add_Pending_Instantiation --
787 --------------------------------
789 procedure Add_Pending_Instantiation
(Inst
: Node_Id
; Act_Decl
: Node_Id
) is
790 Act_Decl_Id
: Entity_Id
;
794 -- Here is a defense against a ludicrous number of instantiations
795 -- caused by a circular set of instantiation attempts.
797 if Pending_Instantiations
.Last
+ 1 >= Maximum_Instantiations
then
798 Error_Msg_Uint_1
:= UI_From_Int
(Maximum_Instantiations
);
799 Error_Msg_N
("too many instantiations, exceeds max of^", Inst
);
800 Error_Msg_N
("\limit can be changed using -gnateinn switch", Inst
);
801 raise Unrecoverable_Error
;
804 -- Capture the body of the generic instantiation along with its context
805 -- for later processing by Instantiate_Bodies.
807 Pending_Instantiations
.Append
808 ((Act_Decl
=> Act_Decl
,
809 Config_Switches
=> Save_Config_Switches
,
810 Current_Sem_Unit
=> Current_Sem_Unit
,
811 Expander_Status
=> Expander_Active
,
813 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
814 Scope_Suppress
=> Scope_Suppress
,
815 Warnings
=> Save_Warnings
));
817 -- With back-end inlining, also associate the index to the instantiation
819 if Back_End_Inlining
then
820 Act_Decl_Id
:= Defining_Entity
(Act_Decl
);
821 Index
:= Pending_Instantiations
.Last
;
823 To_Pending_Instantiations
.Set
(Act_Decl
, Index
);
825 -- If an instantiation is either a compilation unit or is in the main
826 -- unit or subunit or is a nested subprogram, then its body is needed
827 -- as per the analysis already done in Analyze_Package_Instantiation
828 -- and Analyze_Subprogram_Instantiation.
830 if Nkind
(Parent
(Inst
)) = N_Compilation_Unit
831 or else In_Main_Unit_Or_Subunit
(Act_Decl_Id
)
832 or else (Is_Subprogram
(Act_Decl_Id
)
833 and then Is_Nested
(Act_Decl_Id
))
835 Called_Pending_Instantiations
.Append
(Index
);
837 Set_Is_Called
(Act_Decl_Id
);
840 end Add_Pending_Instantiation
;
842 ------------------------
843 -- Add_Scope_To_Clean --
844 ------------------------
846 procedure Add_Scope_To_Clean
(Inst
: Entity_Id
) is
847 Scop
: constant Entity_Id
:= Enclosing_Dynamic_Scope
(Inst
);
851 -- If the instance appears in a library-level package declaration,
852 -- all finalization is global, and nothing needs doing here.
854 if Scop
= Standard_Standard
then
858 -- If the instance is within a generic unit, no finalization code
859 -- can be generated. Note that at this point all bodies have been
860 -- analyzed, and the scope stack itself is not present, and the flag
861 -- Inside_A_Generic is not set.
868 while Present
(S
) and then S
/= Standard_Standard
loop
869 if Is_Generic_Unit
(S
) then
877 Elmt
:= First_Elmt
(To_Clean
);
878 while Present
(Elmt
) loop
879 if Node
(Elmt
) = Scop
then
883 Elmt
:= Next_Elmt
(Elmt
);
886 Append_Elmt
(Scop
, To_Clean
);
887 end Add_Scope_To_Clean
;
893 function Add_Subp
(E
: Entity_Id
) return Subp_Index
is
894 Index
: Subp_Index
:= Subp_Index
(E
) mod Num_Hash_Headers
;
898 -- Initialize entry in Inlined table
900 procedure New_Entry
is
902 Inlined
.Increment_Last
;
903 Inlined
.Table
(Inlined
.Last
).Name
:= E
;
904 Inlined
.Table
(Inlined
.Last
).Next
:= No_Subp
;
905 Inlined
.Table
(Inlined
.Last
).First_Succ
:= No_Succ
;
906 Inlined
.Table
(Inlined
.Last
).Main_Call
:= False;
907 Inlined
.Table
(Inlined
.Last
).Processed
:= False;
910 -- Start of processing for Add_Subp
913 if Hash_Headers
(Index
) = No_Subp
then
915 Hash_Headers
(Index
) := Inlined
.Last
;
919 J
:= Hash_Headers
(Index
);
920 while J
/= No_Subp
loop
921 if Inlined
.Table
(J
).Name
= E
then
925 J
:= Inlined
.Table
(J
).Next
;
929 -- On exit, subprogram was not found. Enter in table. Index is
930 -- the current last entry on the hash chain.
933 Inlined
.Table
(Index
).Next
:= Inlined
.Last
;
938 ----------------------------
939 -- Analyze_Inlined_Bodies --
940 ----------------------------
942 procedure Analyze_Inlined_Bodies
is
949 type Pending_Index
is new Nat
;
951 package Pending_Inlined
is new Table
.Table
(
952 Table_Component_Type
=> Subp_Index
,
953 Table_Index_Type
=> Pending_Index
,
954 Table_Low_Bound
=> 1,
955 Table_Initial
=> Alloc
.Inlined_Initial
,
956 Table_Increment
=> Alloc
.Inlined_Increment
,
957 Table_Name
=> "Pending_Inlined");
958 -- The workpile used to compute the transitive closure
960 -- Start of processing for Analyze_Inlined_Bodies
963 if Serious_Errors_Detected
= 0 then
964 Push_Scope
(Standard_Standard
);
967 while J
<= Inlined_Bodies
.Last
968 and then Serious_Errors_Detected
= 0
970 Pack
:= Inlined_Bodies
.Table
(J
);
972 and then Scope
(Pack
) /= Standard_Standard
973 and then not Is_Child_Unit
(Pack
)
975 Pack
:= Scope
(Pack
);
978 Comp_Unit
:= Parent
(Pack
);
979 while Present
(Comp_Unit
)
980 and then Nkind
(Comp_Unit
) /= N_Compilation_Unit
982 Comp_Unit
:= Parent
(Comp_Unit
);
985 -- Load the body if it exists and contains inlineable entities,
986 -- unless it is the main unit, or is an instance whose body has
987 -- already been analyzed.
989 if Present
(Comp_Unit
)
990 and then Comp_Unit
/= Cunit
(Main_Unit
)
991 and then Body_Required
(Comp_Unit
)
993 (Nkind
(Unit
(Comp_Unit
)) /= N_Package_Declaration
995 (No
(Corresponding_Body
(Unit
(Comp_Unit
)))
996 and then Body_Needed_For_Inlining
997 (Defining_Entity
(Unit
(Comp_Unit
)))))
1000 Bname
: constant Unit_Name_Type
:=
1001 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
1006 if not Is_Loaded
(Bname
) then
1007 Style_Check
:= False;
1008 Load_Needed_Body
(Comp_Unit
, OK
);
1012 -- Warn that a body was not available for inlining
1015 Error_Msg_Unit_1
:= Bname
;
1017 ("one or more inlined subprograms accessed in $!??",
1020 Get_File_Name
(Bname
, Subunit
=> False);
1021 Error_Msg_N
("\but file{ was not found!??", Comp_Unit
);
1029 if J
> Inlined_Bodies
.Last
then
1031 -- The analysis of required bodies may have produced additional
1032 -- generic instantiations. To obtain further inlining, we need
1033 -- to perform another round of generic body instantiations.
1037 -- Symmetrically, the instantiation of required generic bodies
1038 -- may have caused additional bodies to be inlined. To obtain
1039 -- further inlining, we keep looping over the inlined bodies.
1043 -- The list of inlined subprograms is an overestimate, because it
1044 -- includes inlined functions called from functions that are compiled
1045 -- as part of an inlined package, but are not themselves called. An
1046 -- accurate computation of just those subprograms that are needed
1047 -- requires that we perform a transitive closure over the call graph,
1048 -- starting from calls in the main compilation unit.
1050 for Index
in Inlined
.First
.. Inlined
.Last
loop
1051 if not Is_Called
(Inlined
.Table
(Index
).Name
) then
1053 -- This means that Add_Inlined_Body added the subprogram to the
1054 -- table but wasn't able to handle its code unit. Do nothing.
1056 Inlined
.Table
(Index
).Processed
:= True;
1058 elsif Inlined
.Table
(Index
).Main_Call
then
1059 Pending_Inlined
.Increment_Last
;
1060 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Index
;
1061 Inlined
.Table
(Index
).Processed
:= True;
1064 Set_Is_Called
(Inlined
.Table
(Index
).Name
, False);
1068 -- Iterate over the workpile until it is emptied, propagating the
1069 -- Is_Called flag to the successors of the processed subprogram.
1071 while Pending_Inlined
.Last
>= Pending_Inlined
.First
loop
1072 Subp
:= Pending_Inlined
.Table
(Pending_Inlined
.Last
);
1073 Pending_Inlined
.Decrement_Last
;
1075 S
:= Inlined
.Table
(Subp
).First_Succ
;
1077 while S
/= No_Succ
loop
1078 Subp
:= Successors
.Table
(S
).Subp
;
1080 if not Inlined
.Table
(Subp
).Processed
then
1081 Set_Is_Called
(Inlined
.Table
(Subp
).Name
);
1082 Pending_Inlined
.Increment_Last
;
1083 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Subp
;
1084 Inlined
.Table
(Subp
).Processed
:= True;
1087 S
:= Successors
.Table
(S
).Next
;
1091 -- Finally add the called subprograms to the list of inlined
1092 -- subprograms for the unit.
1094 for Index
in Inlined
.First
.. Inlined
.Last
loop
1095 if Is_Called
(Inlined
.Table
(Index
).Name
) then
1096 Add_Inlined_Subprogram
(Inlined
.Table
(Index
).Name
);
1102 end Analyze_Inlined_Bodies
;
1104 --------------------------
1105 -- Build_Body_To_Inline --
1106 --------------------------
1108 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
1109 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1110 Analysis_Status
: constant Boolean := Full_Analysis
;
1111 Original_Body
: Node_Id
;
1112 Body_To_Analyze
: Node_Id
;
1113 Max_Size
: constant := 10;
1115 function Has_Extended_Return
return Boolean;
1116 -- This function returns True if the subprogram has an extended return
1119 function Has_Pending_Instantiation
return Boolean;
1120 -- If some enclosing body contains instantiations that appear before
1121 -- the corresponding generic body, the enclosing body has a freeze node
1122 -- so that it can be elaborated after the generic itself. This might
1123 -- conflict with subsequent inlinings, so that it is unsafe to try to
1124 -- inline in such a case.
1126 function Has_Single_Return_In_GNATprove_Mode
return Boolean;
1127 -- This function is called only in GNATprove mode, and it returns
1128 -- True if the subprogram has no return statement or a single return
1129 -- statement as last statement. It returns False for subprogram with
1130 -- a single return as last statement inside one or more blocks, as
1131 -- inlining would generate gotos in that case as well (although the
1132 -- goto is useless in that case).
1134 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
1135 -- If the body of the subprogram includes a call that returns an
1136 -- unconstrained type, the secondary stack is involved, and it is
1137 -- not worth inlining.
1139 -------------------------
1140 -- Has_Extended_Return --
1141 -------------------------
1143 function Has_Extended_Return
return Boolean is
1144 Body_To_Inline
: constant Node_Id
:= N
;
1146 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
1147 -- Returns OK on node N if this is not an extended return statement
1153 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
1156 when N_Extended_Return_Statement
=>
1159 -- Skip locally declared subprogram bodies inside the body to
1160 -- inline, as the return statements inside those do not count.
1162 when N_Subprogram_Body
=>
1163 if N
= Body_To_Inline
then
1174 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
1176 -- Start of processing for Has_Extended_Return
1179 return Check_All_Returns
(N
) /= OK
;
1180 end Has_Extended_Return
;
1182 -------------------------------
1183 -- Has_Pending_Instantiation --
1184 -------------------------------
1186 function Has_Pending_Instantiation
return Boolean is
1191 while Present
(S
) loop
1192 if Is_Compilation_Unit
(S
)
1193 or else Is_Child_Unit
(S
)
1197 elsif Ekind
(S
) = E_Package
1198 and then Has_Forward_Instantiation
(S
)
1207 end Has_Pending_Instantiation
;
1209 -----------------------------------------
1210 -- Has_Single_Return_In_GNATprove_Mode --
1211 -----------------------------------------
1213 function Has_Single_Return_In_GNATprove_Mode
return Boolean is
1214 Body_To_Inline
: constant Node_Id
:= N
;
1215 Last_Statement
: Node_Id
:= Empty
;
1217 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
1218 -- Returns OK on node N if this is not a return statement different
1219 -- from the last statement in the subprogram.
1225 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
1228 when N_Extended_Return_Statement
1229 | N_Simple_Return_Statement
1231 if N
= Last_Statement
then
1237 -- Skip locally declared subprogram bodies inside the body to
1238 -- inline, as the return statements inside those do not count.
1240 when N_Subprogram_Body
=>
1241 if N
= Body_To_Inline
then
1252 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
1254 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1257 -- Retrieve the last statement
1259 Last_Statement
:= Last
(Statements
(Handled_Statement_Sequence
(N
)));
1261 -- Check that the last statement is the only possible return
1262 -- statement in the subprogram.
1264 return Check_All_Returns
(N
) = OK
;
1265 end Has_Single_Return_In_GNATprove_Mode
;
1267 --------------------------
1268 -- Uses_Secondary_Stack --
1269 --------------------------
1271 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
1272 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
1273 -- Look for function calls that return an unconstrained type
1279 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
1281 if Nkind
(N
) = N_Function_Call
1282 and then Is_Entity_Name
(Name
(N
))
1283 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
1284 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
1287 ("cannot inline & (call returns unconstrained type)?",
1295 function Check_Calls
is new Traverse_Func
(Check_Call
);
1298 return Check_Calls
(Bod
) = Abandon
;
1299 end Uses_Secondary_Stack
;
1301 -- Start of processing for Build_Body_To_Inline
1304 -- Return immediately if done already
1306 if Nkind
(Decl
) = N_Subprogram_Declaration
1307 and then Present
(Body_To_Inline
(Decl
))
1311 -- Subprograms that have return statements in the middle of the body are
1312 -- inlined with gotos. GNATprove does not currently support gotos, so
1313 -- we prevent such inlining.
1315 elsif GNATprove_Mode
1316 and then not Has_Single_Return_In_GNATprove_Mode
1318 Cannot_Inline
("cannot inline & (multiple returns)?", N
, Spec_Id
);
1321 -- Functions that return controlled types cannot currently be inlined
1322 -- because they require secondary stack handling; controlled actions
1323 -- may also interfere in complex ways with inlining.
1325 elsif Ekind
(Spec_Id
) = E_Function
1326 and then Needs_Finalization
(Etype
(Spec_Id
))
1329 ("cannot inline & (controlled return type)?", N
, Spec_Id
);
1333 if Present
(Declarations
(N
))
1334 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
1339 if Present
(Handled_Statement_Sequence
(N
)) then
1340 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
1342 ("cannot inline& (exception handler)?",
1343 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
1347 elsif Has_Excluded_Statement
1348 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
1354 -- We do not inline a subprogram that is too large, unless it is marked
1355 -- Inline_Always or we are in GNATprove mode. This pragma does not
1356 -- suppress the other checks on inlining (forbidden declarations,
1359 if not (Has_Pragma_Inline_Always
(Spec_Id
) or else GNATprove_Mode
)
1360 and then List_Length
1361 (Statements
(Handled_Statement_Sequence
(N
))) > Max_Size
1363 Cannot_Inline
("cannot inline& (body too large)?", N
, Spec_Id
);
1367 if Has_Pending_Instantiation
then
1369 ("cannot inline& (forward instance within enclosing body)?",
1374 -- Within an instance, the body to inline must be treated as a nested
1375 -- generic, so that the proper global references are preserved.
1377 -- Note that we do not do this at the library level, because it is not
1378 -- needed, and furthermore this causes trouble if front-end inlining
1379 -- is activated (-gnatN).
1381 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1382 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1383 Original_Body
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> True);
1385 Original_Body
:= Copy_Separate_Tree
(N
);
1388 -- We need to capture references to the formals in order to substitute
1389 -- the actuals at the point of inlining, i.e. instantiation. To treat
1390 -- the formals as globals to the body to inline, we nest it within a
1391 -- dummy parameterless subprogram, declared within the real one. To
1392 -- avoid generating an internal name (which is never public, and which
1393 -- affects serial numbers of other generated names), we use an internal
1394 -- symbol that cannot conflict with user declarations.
1396 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
1397 Set_Defining_Unit_Name
1398 (Specification
(Original_Body
),
1399 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
1400 Set_Corresponding_Spec
(Original_Body
, Empty
);
1402 -- Remove all aspects/pragmas that have no meaning in an inlined body
1404 Remove_Aspects_And_Pragmas
(Original_Body
);
1407 Copy_Generic_Node
(Original_Body
, Empty
, Instantiating
=> False);
1409 -- Set return type of function, which is also global and does not need
1412 if Ekind
(Spec_Id
) = E_Function
then
1413 Set_Result_Definition
1414 (Specification
(Body_To_Analyze
),
1415 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1418 if No
(Declarations
(N
)) then
1419 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1421 Append
(Body_To_Analyze
, Declarations
(N
));
1424 -- The body to inline is preanalyzed. In GNATprove mode we must disable
1425 -- full analysis as well so that light expansion does not take place
1426 -- either, and name resolution is unaffected.
1428 Expander_Mode_Save_And_Set
(False);
1429 Full_Analysis
:= False;
1431 Analyze
(Body_To_Analyze
);
1432 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1433 Save_Global_References
(Original_Body
);
1435 Remove
(Body_To_Analyze
);
1437 Expander_Mode_Restore
;
1438 Full_Analysis
:= Analysis_Status
;
1440 -- Restore environment if previously saved
1442 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1446 -- Functions that return unconstrained composite types require
1447 -- secondary stack handling, and cannot currently be inlined, unless
1448 -- all return statements return a local variable that is the first
1449 -- local declaration in the body. We had to delay this check until
1450 -- the body of the function is analyzed since Has_Single_Return()
1451 -- requires a minimum decoration.
1453 if Ekind
(Spec_Id
) = E_Function
1454 and then not Is_Scalar_Type
(Etype
(Spec_Id
))
1455 and then not Is_Access_Type
(Etype
(Spec_Id
))
1456 and then not Is_Constrained
(Etype
(Spec_Id
))
1458 if not Has_Single_Return
(Body_To_Analyze
)
1460 -- Skip inlining if the function returns an unconstrained type
1461 -- using an extended return statement, since this part of the
1462 -- new inlining model is not yet supported by the current
1463 -- implementation. ???
1465 or else (Returns_Unconstrained_Type
(Spec_Id
)
1466 and then Has_Extended_Return
)
1469 ("cannot inline & (unconstrained return type)?", N
, Spec_Id
);
1473 -- If secondary stack is used, there is no point in inlining. We have
1474 -- already issued the warning in this case, so nothing to do.
1476 elsif Uses_Secondary_Stack
(Body_To_Analyze
) then
1480 Set_Body_To_Inline
(Decl
, Original_Body
);
1481 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1482 Set_Is_Inlined
(Spec_Id
);
1483 end Build_Body_To_Inline
;
1485 -------------------------------------------
1486 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1487 -------------------------------------------
1489 function Call_Can_Be_Inlined_In_GNATprove_Mode
1491 Subp
: Entity_Id
) return Boolean
1497 F
:= First_Formal
(Subp
);
1498 A
:= First_Actual
(N
);
1499 while Present
(F
) loop
1500 if Ekind
(F
) /= E_Out_Parameter
1501 and then not Same_Type
(Etype
(F
), Etype
(A
))
1503 (Is_By_Reference_Type
(Etype
(A
))
1504 or else Is_Limited_Type
(Etype
(A
)))
1514 end Call_Can_Be_Inlined_In_GNATprove_Mode
;
1516 --------------------------------------
1517 -- Can_Be_Inlined_In_GNATprove_Mode --
1518 --------------------------------------
1520 function Can_Be_Inlined_In_GNATprove_Mode
1521 (Spec_Id
: Entity_Id
;
1522 Body_Id
: Entity_Id
) return Boolean
1524 function Has_Formal_With_Discriminant_Dependent_Fields
1525 (Id
: Entity_Id
) return Boolean;
1526 -- Returns true if the subprogram has at least one formal parameter of
1527 -- an unconstrained record type with per-object constraints on component
1530 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean;
1531 -- Return True if subprogram Id has any contract. The presence of
1532 -- Extensions_Visible or Volatile_Function is also considered as a
1535 function Is_Unit_Subprogram
(Id
: Entity_Id
) return Boolean;
1536 -- Return True if subprogram Id defines a compilation unit
1537 -- Shouldn't this be in Sem_Aux???
1539 function In_Package_Spec
(Id
: Entity_Id
) return Boolean;
1540 -- Return True if subprogram Id is defined in the package specification,
1541 -- either its visible or private part.
1543 ---------------------------------------------------
1544 -- Has_Formal_With_Discriminant_Dependent_Fields --
1545 ---------------------------------------------------
1547 function Has_Formal_With_Discriminant_Dependent_Fields
1548 (Id
: Entity_Id
) return Boolean
1550 function Has_Discriminant_Dependent_Component
1551 (Typ
: Entity_Id
) return Boolean;
1552 -- Determine whether unconstrained record type Typ has at least one
1553 -- component that depends on a discriminant.
1555 ------------------------------------------
1556 -- Has_Discriminant_Dependent_Component --
1557 ------------------------------------------
1559 function Has_Discriminant_Dependent_Component
1560 (Typ
: Entity_Id
) return Boolean
1565 -- Inspect all components of the record type looking for one that
1566 -- depends on a discriminant.
1568 Comp
:= First_Component
(Typ
);
1569 while Present
(Comp
) loop
1570 if Has_Discriminant_Dependent_Constraint
(Comp
) then
1574 Next_Component
(Comp
);
1578 end Has_Discriminant_Dependent_Component
;
1582 Subp_Id
: constant Entity_Id
:= Ultimate_Alias
(Id
);
1584 Formal_Typ
: Entity_Id
;
1586 -- Start of processing for
1587 -- Has_Formal_With_Discriminant_Dependent_Fields
1590 -- Inspect all parameters of the subprogram looking for a formal
1591 -- of an unconstrained record type with at least one discriminant
1592 -- dependent component.
1594 Formal
:= First_Formal
(Subp_Id
);
1595 while Present
(Formal
) loop
1596 Formal_Typ
:= Etype
(Formal
);
1598 if Is_Record_Type
(Formal_Typ
)
1599 and then not Is_Constrained
(Formal_Typ
)
1600 and then Has_Discriminant_Dependent_Component
(Formal_Typ
)
1605 Next_Formal
(Formal
);
1609 end Has_Formal_With_Discriminant_Dependent_Fields
;
1611 -----------------------
1612 -- Has_Some_Contract --
1613 -----------------------
1615 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean is
1619 -- A call to an expression function may precede the actual body which
1620 -- is inserted at the end of the enclosing declarations. Ensure that
1621 -- the related entity is decorated before inspecting the contract.
1623 if Is_Subprogram_Or_Generic_Subprogram
(Id
) then
1624 Items
:= Contract
(Id
);
1626 -- Note that Classifications is not Empty when Extensions_Visible
1627 -- or Volatile_Function is present, which causes such subprograms
1628 -- to be considered to have a contract here. This is fine as we
1629 -- want to avoid inlining these too.
1631 return Present
(Items
)
1632 and then (Present
(Pre_Post_Conditions
(Items
)) or else
1633 Present
(Contract_Test_Cases
(Items
)) or else
1634 Present
(Classifications
(Items
)));
1638 end Has_Some_Contract
;
1640 ---------------------
1641 -- In_Package_Spec --
1642 ---------------------
1644 function In_Package_Spec
(Id
: Entity_Id
) return Boolean is
1645 P
: constant Node_Id
:= Parent
(Subprogram_Spec
(Id
));
1646 -- Parent of the subprogram's declaration
1649 return Nkind
(Enclosing_Declaration
(P
)) = N_Package_Declaration
;
1650 end In_Package_Spec
;
1652 ------------------------
1653 -- Is_Unit_Subprogram --
1654 ------------------------
1656 function Is_Unit_Subprogram
(Id
: Entity_Id
) return Boolean is
1657 Decl
: Node_Id
:= Parent
(Parent
(Id
));
1659 if Nkind
(Parent
(Id
)) = N_Defining_Program_Unit_Name
then
1660 Decl
:= Parent
(Decl
);
1663 return Nkind
(Parent
(Decl
)) = N_Compilation_Unit
;
1664 end Is_Unit_Subprogram
;
1666 -- Local declarations
1669 -- Procedure or function entity for the subprogram
1671 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1674 pragma Assert
(Present
(Spec_Id
) or else Present
(Body_Id
));
1676 if Present
(Spec_Id
) then
1682 -- Only local subprograms without contracts are inlined in GNATprove
1683 -- mode, as these are the subprograms which a user is not interested in
1684 -- analyzing in isolation, but rather in the context of their call. This
1685 -- is a convenient convention, that could be changed for an explicit
1686 -- pragma/aspect one day.
1688 -- In a number of special cases, inlining is not desirable or not
1689 -- possible, see below.
1691 -- Do not inline unit-level subprograms
1693 if Is_Unit_Subprogram
(Id
) then
1696 -- Do not inline subprograms declared in package specs, because they are
1697 -- not local, i.e. can be called either from anywhere (if declared in
1698 -- visible part) or from the child units (if declared in private part).
1700 elsif In_Package_Spec
(Id
) then
1703 -- Do not inline subprograms declared in other units. This is important
1704 -- in particular for subprograms defined in the private part of a
1705 -- package spec, when analyzing one of its child packages, as otherwise
1706 -- we issue spurious messages about the impossibility to inline such
1709 elsif not In_Extended_Main_Code_Unit
(Id
) then
1712 -- Do not inline subprograms marked No_Return, possibly used for
1713 -- signaling errors, which GNATprove handles specially.
1715 elsif No_Return
(Id
) then
1718 -- Do not inline subprograms that have a contract on the spec or the
1719 -- body. Use the contract(s) instead in GNATprove. This also prevents
1720 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1722 elsif (Present
(Spec_Id
) and then Has_Some_Contract
(Spec_Id
))
1724 (Present
(Body_Id
) and then Has_Some_Contract
(Body_Id
))
1728 -- Do not inline expression functions, which are directly inlined at the
1731 elsif (Present
(Spec_Id
) and then Is_Expression_Function
(Spec_Id
))
1733 (Present
(Body_Id
) and then Is_Expression_Function
(Body_Id
))
1737 -- Do not inline generic subprogram instances. The visibility rules of
1738 -- generic instances plays badly with inlining.
1740 elsif Is_Generic_Instance
(Spec_Id
) then
1743 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1744 -- the subprogram body, a similar check is performed after the body
1745 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1747 elsif Present
(Spec_Id
)
1749 (No
(SPARK_Pragma
(Spec_Id
))
1751 Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Spec_Id
)) /= On
)
1755 -- Subprograms in generic instances are currently not inlined, to avoid
1756 -- problems with inlining of standard library subprograms.
1758 elsif Instantiation_Location
(Sloc
(Id
)) /= No_Location
then
1761 -- Do not inline subprograms and entries defined inside protected types,
1762 -- which typically are not helper subprograms, which also avoids getting
1763 -- spurious messages on calls that cannot be inlined.
1765 elsif Within_Protected_Type
(Id
) then
1768 -- Do not inline predicate functions (treated specially by GNATprove)
1770 elsif Is_Predicate_Function
(Id
) then
1773 -- Do not inline subprograms with a parameter of an unconstrained
1774 -- record type if it has discrimiant dependent fields. Indeed, with
1775 -- such parameters, the frontend cannot always ensure type compliance
1776 -- in record component accesses (in particular with records containing
1779 elsif Has_Formal_With_Discriminant_Dependent_Fields
(Id
) then
1782 -- Otherwise, this is a subprogram declared inside the private part of a
1783 -- package, or inside a package body, or locally in a subprogram, and it
1784 -- does not have any contract. Inline it.
1789 end Can_Be_Inlined_In_GNATprove_Mode
;
1795 procedure Cannot_Inline
1799 Is_Serious
: Boolean := False)
1802 -- In GNATprove mode, inlining is the technical means by which the
1803 -- higher-level goal of contextual analysis is reached, so issue
1804 -- messages about failure to apply contextual analysis to a
1805 -- subprogram, rather than failure to inline it.
1808 and then Msg
(Msg
'First .. Msg
'First + 12) = "cannot inline"
1811 Len1
: constant Positive :=
1812 String (String'("cannot inline"))'Length;
1813 Len2 : constant Positive :=
1814 String (String'("info: no contextual analysis of"))'Length;
1816 New_Msg
: String (1 .. Msg
'Length + Len2
- Len1
);
1819 New_Msg
(1 .. Len2
) := "info: no contextual analysis of";
1820 New_Msg
(Len2
+ 1 .. Msg
'Length + Len2
- Len1
) :=
1821 Msg
(Msg
'First + Len1
.. Msg
'Last);
1822 Cannot_Inline
(New_Msg
, N
, Subp
, Is_Serious
);
1827 pragma Assert
(Msg
(Msg
'Last) = '?');
1829 -- Legacy front-end inlining model
1831 if not Back_End_Inlining
then
1833 -- Do not emit warning if this is a predefined unit which is not
1834 -- the main unit. With validity checks enabled, some predefined
1835 -- subprograms may contain nested subprograms and become ineligible
1838 if Is_Predefined_Unit
(Get_Source_Unit
(Subp
))
1839 and then not In_Extended_Main_Source_Unit
(Subp
)
1843 -- In GNATprove mode, issue a warning when -gnatd_f is set, and
1844 -- indicate that the subprogram is not always inlined by setting
1845 -- flag Is_Inlined_Always to False.
1847 elsif GNATprove_Mode
then
1848 Set_Is_Inlined_Always
(Subp
, False);
1850 if Debug_Flag_Underscore_F
then
1851 Error_Msg_NE
(Msg
, N
, Subp
);
1854 elsif Has_Pragma_Inline_Always
(Subp
) then
1856 -- Remove last character (question mark) to make this into an
1857 -- error, because the Inline_Always pragma cannot be obeyed.
1859 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1861 elsif Ineffective_Inline_Warnings
then
1862 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1865 -- New semantics relying on back-end inlining
1867 elsif Is_Serious
then
1869 -- Remove last character (question mark) to make this into an error.
1871 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1873 -- In GNATprove mode, issue a warning when -gnatd_f is set, and
1874 -- indicate that the subprogram is not always inlined by setting
1875 -- flag Is_Inlined_Always to False.
1877 elsif GNATprove_Mode
then
1878 Set_Is_Inlined_Always
(Subp
, False);
1880 if Debug_Flag_Underscore_F
then
1881 Error_Msg_NE
(Msg
, N
, Subp
);
1886 -- Do not emit warning if this is a predefined unit which is not
1887 -- the main unit. This behavior is currently provided for backward
1888 -- compatibility but it will be removed when we enforce the
1889 -- strictness of the new rules.
1891 if Is_Predefined_Unit
(Get_Source_Unit
(Subp
))
1892 and then not In_Extended_Main_Source_Unit
(Subp
)
1896 elsif Has_Pragma_Inline_Always
(Subp
) then
1898 -- Emit a warning if this is a call to a runtime subprogram
1899 -- which is located inside a generic. Previously this call
1900 -- was silently skipped.
1902 if Is_Generic_Instance
(Subp
) then
1904 Gen_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Subp
));
1906 if Is_Predefined_Unit
(Get_Source_Unit
(Gen_P
)) then
1907 Set_Is_Inlined
(Subp
, False);
1908 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1914 -- Remove last character (question mark) to make this into an
1915 -- error, because the Inline_Always pragma cannot be obeyed.
1917 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
1920 Set_Is_Inlined
(Subp
, False);
1922 if Ineffective_Inline_Warnings
then
1923 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
1929 --------------------------------------------
1930 -- Check_And_Split_Unconstrained_Function --
1931 --------------------------------------------
1933 procedure Check_And_Split_Unconstrained_Function
1935 Spec_Id
: Entity_Id
;
1936 Body_Id
: Entity_Id
)
1938 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
1939 -- Use generic machinery to build an unexpanded body for the subprogram.
1940 -- This body is subsequently used for inline expansions at call sites.
1942 procedure Build_Return_Object_Formal
1946 -- Create a formal parameter for return object declaration Obj_Decl of
1947 -- an extended return statement and add it to list Formals.
1949 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean;
1950 -- Return true if we generate code for the function body N, the function
1951 -- body N has no local declarations and its unique statement is a single
1952 -- extended return statement with a handled statements sequence.
1954 procedure Copy_Formals
1956 Subp_Id
: Entity_Id
;
1958 -- Create new formal parameters from the formal parameters of subprogram
1959 -- Subp_Id and add them to list Formals.
1961 function Copy_Return_Object
(Obj_Decl
: Node_Id
) return Node_Id
;
1962 -- Create a copy of return object declaration Obj_Decl of an extended
1963 -- return statement.
1965 procedure Split_Unconstrained_Function
1967 Spec_Id
: Entity_Id
);
1968 -- N is an inlined function body that returns an unconstrained type and
1969 -- has a single extended return statement. Split N in two subprograms:
1970 -- a procedure P' and a function F'. The formals of P' duplicate the
1971 -- formals of N plus an extra formal which is used to return a value;
1972 -- its body is composed by the declarations and list of statements
1973 -- of the extended return statement of N.
1975 --------------------------
1976 -- Build_Body_To_Inline --
1977 --------------------------
1979 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
1980 procedure Generate_Subprogram_Body
1982 Body_To_Inline
: out Node_Id
);
1983 -- Generate a parameterless duplicate of subprogram body N. Note that
1984 -- occurrences of pragmas referencing the formals are removed since
1985 -- they have no meaning when the body is inlined and the formals are
1986 -- rewritten (the analysis of the non-inlined body will handle these
1987 -- pragmas). A new internal name is associated with Body_To_Inline.
1989 ------------------------------
1990 -- Generate_Subprogram_Body --
1991 ------------------------------
1993 procedure Generate_Subprogram_Body
1995 Body_To_Inline
: out Node_Id
)
1998 -- Within an instance, the body to inline must be treated as a
1999 -- nested generic so that proper global references are preserved.
2001 -- Note that we do not do this at the library level, because it
2002 -- is not needed, and furthermore this causes trouble if front
2003 -- end inlining is activated (-gnatN).
2006 and then Scope
(Current_Scope
) /= Standard_Standard
2009 Copy_Generic_Node
(N
, Empty
, Instantiating
=> True);
2011 -- ??? Shouldn't this use New_Copy_Tree? What about global
2012 -- references captured in the body to inline?
2014 Body_To_Inline
:= Copy_Separate_Tree
(N
);
2017 -- Remove aspects/pragmas that have no meaning in an inlined body
2019 Remove_Aspects_And_Pragmas
(Body_To_Inline
);
2021 -- We need to capture references to the formals in order
2022 -- to substitute the actuals at the point of inlining, i.e.
2023 -- instantiation. To treat the formals as globals to the body to
2024 -- inline, we nest it within a dummy parameterless subprogram,
2025 -- declared within the real one.
2027 Set_Parameter_Specifications
2028 (Specification
(Body_To_Inline
), No_List
);
2030 -- A new internal name is associated with Body_To_Inline to avoid
2031 -- conflicts when the non-inlined body N is analyzed.
2033 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
2034 Make_Defining_Identifier
(Sloc
(N
), New_Internal_Name
('P')));
2035 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
2036 end Generate_Subprogram_Body
;
2040 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
2041 Original_Body
: Node_Id
;
2042 Body_To_Analyze
: Node_Id
;
2044 -- Start of processing for Build_Body_To_Inline
2047 pragma Assert
(Current_Scope
= Spec_Id
);
2049 -- Within an instance, the body to inline must be treated as a nested
2050 -- generic, so that the proper global references are preserved. We
2051 -- do not do this at the library level, because it is not needed, and
2052 -- furthermore this causes trouble if front-end inlining is activated
2056 and then Scope
(Current_Scope
) /= Standard_Standard
2058 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
2061 -- Capture references to formals in order to substitute the actuals
2062 -- at the point of inlining or instantiation. To treat the formals
2063 -- as globals to the body to inline, nest the body within a dummy
2064 -- parameterless subprogram, declared within the real one.
2066 Generate_Subprogram_Body
(N
, Original_Body
);
2068 Copy_Generic_Node
(Original_Body
, Empty
, Instantiating
=> False);
2070 -- Set return type of function, which is also global and does not
2071 -- need to be resolved.
2073 if Ekind
(Spec_Id
) = E_Function
then
2074 Set_Result_Definition
(Specification
(Body_To_Analyze
),
2075 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
2078 if No
(Declarations
(N
)) then
2079 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
2081 Append_To
(Declarations
(N
), Body_To_Analyze
);
2084 Preanalyze
(Body_To_Analyze
);
2086 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
2087 Save_Global_References
(Original_Body
);
2089 Remove
(Body_To_Analyze
);
2091 -- Restore environment if previously saved
2094 and then Scope
(Current_Scope
) /= Standard_Standard
2099 pragma Assert
(No
(Body_To_Inline
(Decl
)));
2100 Set_Body_To_Inline
(Decl
, Original_Body
);
2101 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
2102 end Build_Body_To_Inline
;
2104 --------------------------------
2105 -- Build_Return_Object_Formal --
2106 --------------------------------
2108 procedure Build_Return_Object_Formal
2113 Obj_Def
: constant Node_Id
:= Object_Definition
(Obj_Decl
);
2114 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
2118 -- Build the type definition of the formal parameter. The use of
2119 -- New_Copy_Tree ensures that global references preserved in the
2120 -- case of generics.
2122 if Is_Entity_Name
(Obj_Def
) then
2123 Typ_Def
:= New_Copy_Tree
(Obj_Def
);
2125 Typ_Def
:= New_Copy_Tree
(Subtype_Mark
(Obj_Def
));
2130 -- Obj_Id : [out] Typ_Def
2132 -- Mode OUT should not be used when the return object is declared as
2133 -- a constant. Check the definition of the object declaration because
2134 -- the object has not been analyzed yet.
2137 Make_Parameter_Specification
(Loc
,
2138 Defining_Identifier
=>
2139 Make_Defining_Identifier
(Loc
, Chars
(Obj_Id
)),
2140 In_Present
=> False,
2141 Out_Present
=> not Constant_Present
(Obj_Decl
),
2142 Null_Exclusion_Present
=> False,
2143 Parameter_Type
=> Typ_Def
));
2144 end Build_Return_Object_Formal
;
2146 --------------------------------------
2147 -- Can_Split_Unconstrained_Function --
2148 --------------------------------------
2150 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean is
2151 Stmt
: constant Node_Id
:=
2152 First
(Statements
(Handled_Statement_Sequence
(N
)));
2156 -- No user defined declarations allowed in the function except inside
2157 -- the unique return statement; implicit labels are the only allowed
2160 Decl
:= First
(Declarations
(N
));
2161 while Present
(Decl
) loop
2162 if Nkind
(Decl
) /= N_Implicit_Label_Declaration
then
2169 -- We only split the inlined function when we are generating the code
2170 -- of its body; otherwise we leave duplicated split subprograms in
2171 -- the tree which (if referenced) generate wrong references at link
2174 return In_Extended_Main_Code_Unit
(N
)
2175 and then Present
(Stmt
)
2176 and then Nkind
(Stmt
) = N_Extended_Return_Statement
2177 and then No
(Next
(Stmt
))
2178 and then Present
(Handled_Statement_Sequence
(Stmt
));
2179 end Can_Split_Unconstrained_Function
;
2185 procedure Copy_Formals
2187 Subp_Id
: Entity_Id
;
2194 Formal
:= First_Formal
(Subp_Id
);
2195 while Present
(Formal
) loop
2196 Spec
:= Parent
(Formal
);
2198 -- Create an exact copy of the formal parameter. The use of
2199 -- New_Copy_Tree ensures that global references are preserved
2200 -- in case of generics.
2203 Make_Parameter_Specification
(Loc
,
2204 Defining_Identifier
=>
2205 Make_Defining_Identifier
(Sloc
(Formal
), Chars
(Formal
)),
2206 In_Present
=> In_Present
(Spec
),
2207 Out_Present
=> Out_Present
(Spec
),
2208 Null_Exclusion_Present
=> Null_Exclusion_Present
(Spec
),
2210 New_Copy_Tree
(Parameter_Type
(Spec
)),
2211 Expression
=> New_Copy_Tree
(Expression
(Spec
))));
2213 Next_Formal
(Formal
);
2217 ------------------------
2218 -- Copy_Return_Object --
2219 ------------------------
2221 function Copy_Return_Object
(Obj_Decl
: Node_Id
) return Node_Id
is
2222 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
2225 -- The use of New_Copy_Tree ensures that global references are
2226 -- preserved in case of generics.
2229 Make_Object_Declaration
(Sloc
(Obj_Decl
),
2230 Defining_Identifier
=>
2231 Make_Defining_Identifier
(Sloc
(Obj_Id
), Chars
(Obj_Id
)),
2232 Aliased_Present
=> Aliased_Present
(Obj_Decl
),
2233 Constant_Present
=> Constant_Present
(Obj_Decl
),
2234 Null_Exclusion_Present
=> Null_Exclusion_Present
(Obj_Decl
),
2235 Object_Definition
=>
2236 New_Copy_Tree
(Object_Definition
(Obj_Decl
)),
2237 Expression
=> New_Copy_Tree
(Expression
(Obj_Decl
)));
2238 end Copy_Return_Object
;
2240 ----------------------------------
2241 -- Split_Unconstrained_Function --
2242 ----------------------------------
2244 procedure Split_Unconstrained_Function
2246 Spec_Id
: Entity_Id
)
2248 Loc
: constant Source_Ptr
:= Sloc
(N
);
2249 Ret_Stmt
: constant Node_Id
:=
2250 First
(Statements
(Handled_Statement_Sequence
(N
)));
2251 Ret_Obj
: constant Node_Id
:=
2252 First
(Return_Object_Declarations
(Ret_Stmt
));
2254 procedure Build_Procedure
2255 (Proc_Id
: out Entity_Id
;
2256 Decl_List
: out List_Id
);
2257 -- Build a procedure containing the statements found in the extended
2258 -- return statement of the unconstrained function body N.
2260 ---------------------
2261 -- Build_Procedure --
2262 ---------------------
2264 procedure Build_Procedure
2265 (Proc_Id
: out Entity_Id
;
2266 Decl_List
: out List_Id
)
2268 Formals
: constant List_Id
:= New_List
;
2269 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
2271 Body_Decls
: List_Id
:= No_List
;
2273 Proc_Body
: Node_Id
;
2274 Proc_Spec
: Node_Id
;
2277 -- Create formal parameters for the return object and all formals
2278 -- of the unconstrained function in order to pass their values to
2281 Build_Return_Object_Formal
2283 Obj_Decl
=> Ret_Obj
,
2284 Formals
=> Formals
);
2289 Formals
=> Formals
);
2291 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
2294 Make_Procedure_Specification
(Loc
,
2295 Defining_Unit_Name
=> Proc_Id
,
2296 Parameter_Specifications
=> Formals
);
2298 Decl_List
:= New_List
;
2300 Append_To
(Decl_List
,
2301 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
2303 -- Can_Convert_Unconstrained_Function checked that the function
2304 -- has no local declarations except implicit label declarations.
2305 -- Copy these declarations to the built procedure.
2307 if Present
(Declarations
(N
)) then
2308 Body_Decls
:= New_List
;
2310 Decl
:= First
(Declarations
(N
));
2311 while Present
(Decl
) loop
2312 pragma Assert
(Nkind
(Decl
) = N_Implicit_Label_Declaration
);
2314 Append_To
(Body_Decls
,
2315 Make_Implicit_Label_Declaration
(Loc
,
2316 Make_Defining_Identifier
(Loc
,
2317 Chars
=> Chars
(Defining_Identifier
(Decl
))),
2318 Label_Construct
=> Empty
));
2324 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Stmt
)));
2327 Make_Subprogram_Body
(Loc
,
2328 Specification
=> Copy_Subprogram_Spec
(Proc_Spec
),
2329 Declarations
=> Body_Decls
,
2330 Handled_Statement_Sequence
=>
2331 New_Copy_Tree
(Handled_Statement_Sequence
(Ret_Stmt
)));
2333 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
2334 Make_Defining_Identifier
(Loc
, Subp_Name
));
2336 Append_To
(Decl_List
, Proc_Body
);
2337 end Build_Procedure
;
2341 New_Obj
: constant Node_Id
:= Copy_Return_Object
(Ret_Obj
);
2343 Proc_Call
: Node_Id
;
2344 Proc_Id
: Entity_Id
;
2346 -- Start of processing for Split_Unconstrained_Function
2349 -- Build the associated procedure, analyze it and insert it before
2350 -- the function body N.
2353 Scope
: constant Entity_Id
:= Current_Scope
;
2354 Decl_List
: List_Id
;
2357 Build_Procedure
(Proc_Id
, Decl_List
);
2358 Insert_Actions
(N
, Decl_List
);
2359 Set_Is_Inlined
(Proc_Id
);
2363 -- Build the call to the generated procedure
2366 Actual_List
: constant List_Id
:= New_List
;
2370 Append_To
(Actual_List
,
2371 New_Occurrence_Of
(Defining_Identifier
(New_Obj
), Loc
));
2373 Formal
:= First_Formal
(Spec_Id
);
2374 while Present
(Formal
) loop
2375 Append_To
(Actual_List
, New_Occurrence_Of
(Formal
, Loc
));
2377 -- Avoid spurious warning on unreferenced formals
2379 Set_Referenced
(Formal
);
2380 Next_Formal
(Formal
);
2384 Make_Procedure_Call_Statement
(Loc
,
2385 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
2386 Parameter_Associations
=> Actual_List
);
2394 -- Proc (New_Obj, ...);
2399 Make_Block_Statement
(Loc
,
2400 Declarations
=> New_List
(New_Obj
),
2401 Handled_Statement_Sequence
=>
2402 Make_Handled_Sequence_Of_Statements
(Loc
,
2403 Statements
=> New_List
(
2407 Make_Simple_Return_Statement
(Loc
,
2410 (Defining_Identifier
(New_Obj
), Loc
)))));
2412 Rewrite
(Ret_Stmt
, Blk_Stmt
);
2413 end Split_Unconstrained_Function
;
2417 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
2419 -- Start of processing for Check_And_Split_Unconstrained_Function
2422 pragma Assert
(Back_End_Inlining
2423 and then Ekind
(Spec_Id
) = E_Function
2424 and then Returns_Unconstrained_Type
(Spec_Id
)
2425 and then Comes_From_Source
(Body_Id
)
2426 and then (Has_Pragma_Inline_Always
(Spec_Id
)
2427 or else Optimization_Level
> 0));
2429 -- This routine must not be used in GNATprove mode since GNATprove
2430 -- relies on frontend inlining
2432 pragma Assert
(not GNATprove_Mode
);
2434 -- No need to split the function if we cannot generate the code
2436 if Serious_Errors_Detected
/= 0 then
2440 -- No action needed in stubs since the attribute Body_To_Inline
2443 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
2446 -- Cannot build the body to inline if the attribute is already set.
2447 -- This attribute may have been set if this is a subprogram renaming
2448 -- declarations (see Freeze.Build_Renamed_Body).
2450 elsif Present
(Body_To_Inline
(Decl
)) then
2453 -- Do not generate a body to inline for protected functions, because the
2454 -- transformation generates a call to a protected procedure, causing
2455 -- spurious errors. We don't inline protected operations anyway, so
2456 -- this is no loss. We might as well ignore intrinsics and foreign
2457 -- conventions as well -- just allow Ada conventions.
2459 elsif not (Convention
(Spec_Id
) = Convention_Ada
2460 or else Convention
(Spec_Id
) = Convention_Ada_Pass_By_Copy
2461 or else Convention
(Spec_Id
) = Convention_Ada_Pass_By_Reference
)
2465 -- Check excluded declarations
2467 elsif Present
(Declarations
(N
))
2468 and then Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
))
2472 -- Check excluded statements. There is no need to protect us against
2473 -- exception handlers since they are supported by the GCC backend.
2475 elsif Present
(Handled_Statement_Sequence
(N
))
2476 and then Has_Excluded_Statement
2477 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
2482 -- Build the body to inline only if really needed
2484 if Can_Split_Unconstrained_Function
(N
) then
2485 Split_Unconstrained_Function
(N
, Spec_Id
);
2486 Build_Body_To_Inline
(N
, Spec_Id
);
2487 Set_Is_Inlined
(Spec_Id
);
2489 end Check_And_Split_Unconstrained_Function
;
2491 -------------------------------------
2492 -- Check_Package_Body_For_Inlining --
2493 -------------------------------------
2495 procedure Check_Package_Body_For_Inlining
(N
: Node_Id
; P
: Entity_Id
) is
2496 Bname
: Unit_Name_Type
;
2501 -- Legacy implementation (relying on frontend inlining)
2503 if not Back_End_Inlining
2504 and then Is_Compilation_Unit
(P
)
2505 and then not Is_Generic_Instance
(P
)
2507 Bname
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
2509 E
:= First_Entity
(P
);
2510 while Present
(E
) loop
2511 if Has_Pragma_Inline_Always
(E
)
2512 or else (Has_Pragma_Inline
(E
) and Front_End_Inlining
)
2514 if not Is_Loaded
(Bname
) then
2515 Load_Needed_Body
(N
, OK
);
2519 -- Check we are not trying to inline a parent whose body
2520 -- depends on a child, when we are compiling the body of
2521 -- the child. Otherwise we have a potential elaboration
2522 -- circularity with inlined subprograms and with
2523 -- Taft-Amendment types.
2526 Comp
: Node_Id
; -- Body just compiled
2527 Child_Spec
: Entity_Id
; -- Spec of main unit
2528 Ent
: Entity_Id
; -- For iteration
2529 With_Clause
: Node_Id
; -- Context of body.
2532 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Package_Body
2533 and then Present
(Body_Entity
(P
))
2537 ((Unit
(Library_Unit
(Cunit
(Main_Unit
)))));
2540 Parent
(Unit_Declaration_Node
(Body_Entity
(P
)));
2542 -- Check whether the context of the body just
2543 -- compiled includes a child of itself, and that
2544 -- child is the spec of the main compilation.
2546 With_Clause
:= First
(Context_Items
(Comp
));
2547 while Present
(With_Clause
) loop
2548 if Nkind
(With_Clause
) = N_With_Clause
2550 Scope
(Entity
(Name
(With_Clause
))) = P
2552 Entity
(Name
(With_Clause
)) = Child_Spec
2554 Error_Msg_Node_2
:= Child_Spec
;
2556 ("body of & depends on child unit&??",
2559 ("\subprograms in body cannot be inlined??",
2562 -- Disable further inlining from this unit,
2563 -- and keep Taft-amendment types incomplete.
2565 Ent
:= First_Entity
(P
);
2566 while Present
(Ent
) loop
2568 and then Has_Completion_In_Body
(Ent
)
2570 Set_Full_View
(Ent
, Empty
);
2572 elsif Is_Subprogram
(Ent
) then
2573 Set_Is_Inlined
(Ent
, False);
2587 elsif Ineffective_Inline_Warnings
then
2588 Error_Msg_Unit_1
:= Bname
;
2590 ("unable to inline subprograms defined in $??", P
);
2591 Error_Msg_N
("\body not found??", P
);
2602 end Check_Package_Body_For_Inlining
;
2604 --------------------
2605 -- Cleanup_Scopes --
2606 --------------------
2608 procedure Cleanup_Scopes
is
2614 Elmt
:= First_Elmt
(To_Clean
);
2615 while Present
(Elmt
) loop
2616 Scop
:= Node
(Elmt
);
2618 if Ekind
(Scop
) = E_Entry
then
2619 Scop
:= Protected_Body_Subprogram
(Scop
);
2621 elsif Is_Subprogram
(Scop
)
2622 and then Is_Protected_Type
(Scope
(Scop
))
2623 and then Present
(Protected_Body_Subprogram
(Scop
))
2625 -- If a protected operation contains an instance, its cleanup
2626 -- operations have been delayed, and the subprogram has been
2627 -- rewritten in the expansion of the enclosing protected body. It
2628 -- is the corresponding subprogram that may require the cleanup
2629 -- operations, so propagate the information that triggers cleanup
2633 (Protected_Body_Subprogram
(Scop
),
2634 Uses_Sec_Stack
(Scop
));
2636 Scop
:= Protected_Body_Subprogram
(Scop
);
2639 if Ekind
(Scop
) = E_Block
then
2640 Decl
:= Parent
(Block_Node
(Scop
));
2643 Decl
:= Unit_Declaration_Node
(Scop
);
2645 if Nkind_In
(Decl
, N_Subprogram_Declaration
,
2646 N_Task_Type_Declaration
,
2647 N_Subprogram_Body_Stub
)
2649 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
2654 Expand_Cleanup_Actions
(Decl
);
2657 Elmt
:= Next_Elmt
(Elmt
);
2661 -------------------------
2662 -- Expand_Inlined_Call --
2663 -------------------------
2665 procedure Expand_Inlined_Call
2668 Orig_Subp
: Entity_Id
)
2670 Decls
: constant List_Id
:= New_List
;
2671 Is_Predef
: constant Boolean :=
2672 Is_Predefined_Unit
(Get_Source_Unit
(Subp
));
2673 Loc
: constant Source_Ptr
:= Sloc
(N
);
2674 Orig_Bod
: constant Node_Id
:=
2675 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
2677 Uses_Back_End
: constant Boolean :=
2678 Back_End_Inlining
and then Optimization_Level
> 0;
2679 -- The back-end expansion is used if the target supports back-end
2680 -- inlining and some level of optimixation is required; otherwise
2681 -- the inlining takes place fully as a tree expansion.
2685 Exit_Lab
: Entity_Id
:= Empty
;
2688 Lab_Decl
: Node_Id
:= Empty
;
2692 Ret_Type
: Entity_Id
;
2694 Temp_Typ
: Entity_Id
;
2697 Is_Unc_Decl
: Boolean;
2698 -- If the type returned by the function is unconstrained and the call
2699 -- can be inlined, special processing is required.
2701 Return_Object
: Entity_Id
:= Empty
;
2702 -- Entity in declaration in an extended_return_statement
2704 Targ
: Node_Id
:= Empty
;
2705 -- The target of the call. If context is an assignment statement then
2706 -- this is the left-hand side of the assignment, else it is a temporary
2707 -- to which the return value is assigned prior to rewriting the call.
2709 Targ1
: Node_Id
:= Empty
;
2710 -- A separate target used when the return type is unconstrained
2712 procedure Declare_Postconditions_Result
;
2713 -- When generating C code, declare _Result, which may be used in the
2714 -- inlined _Postconditions procedure to verify the return value.
2716 procedure Make_Exit_Label
;
2717 -- Build declaration for exit label to be used in Return statements,
2718 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2719 -- declaration). Does nothing if Exit_Lab already set.
2721 procedure Make_Loop_Labels_Unique
(HSS
: Node_Id
);
2722 -- When compiling for CCG and performing front-end inlining, replace
2723 -- loop names and references to them so that they do not conflict with
2724 -- homographs in the current subprogram.
2726 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
2727 -- Replace occurrence of a formal with the corresponding actual, or the
2728 -- thunk generated for it. Replace a return statement with an assignment
2729 -- to the target of the call, with appropriate conversions if needed.
2731 function Process_Formals_In_Aspects
(N
: Node_Id
) return Traverse_Result
;
2732 -- Because aspects are linked indirectly to the rest of the tree,
2733 -- replacement of formals appearing in aspect specifications must
2734 -- be performed in a separate pass, using an instantiation of the
2735 -- previous subprogram over aspect specifications reachable from N.
2737 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
2738 -- If the call being expanded is that of an internal subprogram, set the
2739 -- sloc of the generated block to that of the call itself, so that the
2740 -- expansion is skipped by the "next" command in gdb. Same processing
2741 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2742 -- Debug_Generated_Code is true, suppress this change to simplify our
2743 -- own development. Same in GNATprove mode, to ensure that warnings and
2744 -- diagnostics point to the proper location.
2746 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
2747 -- In subtree N search for occurrences of dispatching calls that use the
2748 -- Ada 2005 Object.Operation notation and the object is a formal of the
2749 -- inlined subprogram. Reset the entity associated with Operation in all
2750 -- the found occurrences.
2752 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
2753 -- If the function body is a single expression, replace call with
2754 -- expression, else insert block appropriately.
2756 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
2757 -- If procedure body has no local variables, inline body without
2758 -- creating block, otherwise rewrite call with block.
2760 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
2761 -- Determine whether a formal parameter is used only once in Orig_Bod
2763 -----------------------------------
2764 -- Declare_Postconditions_Result --
2765 -----------------------------------
2767 procedure Declare_Postconditions_Result
is
2768 Enclosing_Subp
: constant Entity_Id
:= Scope
(Subp
);
2773 and then Is_Subprogram
(Enclosing_Subp
)
2774 and then Present
(Postconditions_Proc
(Enclosing_Subp
)));
2776 if Ekind
(Enclosing_Subp
) = E_Function
then
2777 if Nkind
(First
(Parameter_Associations
(N
))) in
2778 N_Numeric_Or_String_Literal
2780 Append_To
(Declarations
(Blk
),
2781 Make_Object_Declaration
(Loc
,
2782 Defining_Identifier
=>
2783 Make_Defining_Identifier
(Loc
, Name_uResult
),
2784 Constant_Present
=> True,
2785 Object_Definition
=>
2786 New_Occurrence_Of
(Etype
(Enclosing_Subp
), Loc
),
2788 New_Copy_Tree
(First
(Parameter_Associations
(N
)))));
2790 Append_To
(Declarations
(Blk
),
2791 Make_Object_Renaming_Declaration
(Loc
,
2792 Defining_Identifier
=>
2793 Make_Defining_Identifier
(Loc
, Name_uResult
),
2795 New_Occurrence_Of
(Etype
(Enclosing_Subp
), Loc
),
2797 New_Copy_Tree
(First
(Parameter_Associations
(N
)))));
2800 end Declare_Postconditions_Result
;
2802 ---------------------
2803 -- Make_Exit_Label --
2804 ---------------------
2806 procedure Make_Exit_Label
is
2807 Lab_Ent
: Entity_Id
;
2809 if No
(Exit_Lab
) then
2810 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
2811 Lab_Id
:= New_Occurrence_Of
(Lab_Ent
, Loc
);
2812 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
2814 Make_Implicit_Label_Declaration
(Loc
,
2815 Defining_Identifier
=> Lab_Ent
,
2816 Label_Construct
=> Exit_Lab
);
2818 end Make_Exit_Label
;
2820 -----------------------------
2821 -- Make_Loop_Labels_Unique --
2822 -----------------------------
2824 procedure Make_Loop_Labels_Unique
(HSS
: Node_Id
) is
2825 function Process_Loop
(N
: Node_Id
) return Traverse_Result
;
2831 function Process_Loop
(N
: Node_Id
) return Traverse_Result
is
2835 if Nkind
(N
) = N_Loop_Statement
2836 and then Present
(Identifier
(N
))
2838 -- Create new external name for loop and update the
2839 -- corresponding entity.
2841 Id
:= Entity
(Identifier
(N
));
2842 Set_Chars
(Id
, New_External_Name
(Chars
(Id
), 'L', -1));
2843 Set_Chars
(Identifier
(N
), Chars
(Id
));
2845 elsif Nkind
(N
) = N_Exit_Statement
2846 and then Present
(Name
(N
))
2848 -- The exit statement must name an enclosing loop, whose name
2849 -- has already been updated.
2851 Set_Chars
(Name
(N
), Chars
(Entity
(Name
(N
))));
2857 procedure Update_Loop_Names
is new Traverse_Proc
(Process_Loop
);
2863 -- Start of processing for Make_Loop_Labels_Unique
2866 if Modify_Tree_For_C
then
2867 Stmt
:= First
(Statements
(HSS
));
2868 while Present
(Stmt
) loop
2869 Update_Loop_Names
(Stmt
);
2873 end Make_Loop_Labels_Unique
;
2875 ---------------------
2876 -- Process_Formals --
2877 ---------------------
2879 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
2885 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
2888 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
2889 A
:= Renamed_Object
(E
);
2891 -- Rewrite the occurrence of the formal into an occurrence of
2892 -- the actual. Also establish visibility on the proper view of
2893 -- the actual's subtype for the body's context (if the actual's
2894 -- subtype is private at the call point but its full view is
2895 -- visible to the body, then the inlined tree here must be
2896 -- analyzed with the full view).
2898 if Is_Entity_Name
(A
) then
2899 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Sloc
(N
)));
2900 Check_Private_View
(N
);
2902 elsif Nkind
(A
) = N_Defining_Identifier
then
2903 Rewrite
(N
, New_Occurrence_Of
(A
, Sloc
(N
)));
2904 Check_Private_View
(N
);
2909 Rewrite
(N
, New_Copy
(A
));
2915 elsif Is_Entity_Name
(N
)
2916 and then Present
(Return_Object
)
2917 and then Chars
(N
) = Chars
(Return_Object
)
2919 -- Occurrence within an extended return statement. The return
2920 -- object is local to the body been inlined, and thus the generic
2921 -- copy is not analyzed yet, so we match by name, and replace it
2922 -- with target of call.
2924 if Nkind
(Targ
) = N_Defining_Identifier
then
2925 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
2927 Rewrite
(N
, New_Copy_Tree
(Targ
));
2932 elsif Nkind
(N
) = N_Simple_Return_Statement
then
2933 if No
(Expression
(N
)) then
2934 Num_Ret
:= Num_Ret
+ 1;
2937 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
2940 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
2941 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
2943 -- Function body is a single expression. No need for
2949 Num_Ret
:= Num_Ret
+ 1;
2953 -- Because of the presence of private types, the views of the
2954 -- expression and the context may be different, so place
2955 -- a type conversion to the context type to avoid spurious
2956 -- errors, e.g. when the expression is a numeric literal and
2957 -- the context is private. If the expression is an aggregate,
2958 -- use a qualified expression, because an aggregate is not a
2959 -- legal argument of a conversion. Ditto for numeric, character
2960 -- and string literals, and attributes that yield a universal
2961 -- type, because those must be resolved to a specific type.
2963 if Nkind_In
(Expression
(N
), N_Aggregate
,
2964 N_Character_Literal
,
2967 or else Yields_Universal_Type
(Expression
(N
))
2970 Make_Qualified_Expression
(Sloc
(N
),
2971 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
2972 Expression
=> Relocate_Node
(Expression
(N
)));
2974 -- Use an unchecked type conversion between access types, for
2975 -- which a type conversion would not always be valid, as no
2976 -- check may result from the conversion.
2978 elsif Is_Access_Type
(Ret_Type
) then
2980 Unchecked_Convert_To
2981 (Ret_Type
, Relocate_Node
(Expression
(N
)));
2983 -- Otherwise use a type conversion, which may trigger a check
2987 Make_Type_Conversion
(Sloc
(N
),
2988 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
2989 Expression
=> Relocate_Node
(Expression
(N
)));
2992 if Nkind
(Targ
) = N_Defining_Identifier
then
2994 Make_Assignment_Statement
(Loc
,
2995 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2996 Expression
=> Ret
));
2999 Make_Assignment_Statement
(Loc
,
3000 Name
=> New_Copy
(Targ
),
3001 Expression
=> Ret
));
3004 Set_Assignment_OK
(Name
(N
));
3006 if Present
(Exit_Lab
) then
3008 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
3014 -- An extended return becomes a block whose first statement is the
3015 -- assignment of the initial expression of the return object to the
3016 -- target of the call itself.
3018 elsif Nkind
(N
) = N_Extended_Return_Statement
then
3020 Return_Decl
: constant Entity_Id
:=
3021 First
(Return_Object_Declarations
(N
));
3025 Return_Object
:= Defining_Identifier
(Return_Decl
);
3027 if Present
(Expression
(Return_Decl
)) then
3028 if Nkind
(Targ
) = N_Defining_Identifier
then
3030 Make_Assignment_Statement
(Loc
,
3031 Name
=> New_Occurrence_Of
(Targ
, Loc
),
3032 Expression
=> Expression
(Return_Decl
));
3035 Make_Assignment_Statement
(Loc
,
3036 Name
=> New_Copy
(Targ
),
3037 Expression
=> Expression
(Return_Decl
));
3040 Set_Assignment_OK
(Name
(Assign
));
3042 if No
(Handled_Statement_Sequence
(N
)) then
3043 Set_Handled_Statement_Sequence
(N
,
3044 Make_Handled_Sequence_Of_Statements
(Loc
,
3045 Statements
=> New_List
));
3049 Statements
(Handled_Statement_Sequence
(N
)));
3053 Make_Block_Statement
(Loc
,
3054 Handled_Statement_Sequence
=>
3055 Handled_Statement_Sequence
(N
)));
3060 -- Remove pragma Unreferenced since it may refer to formals that
3061 -- are not visible in the inlined body, and in any case we will
3062 -- not be posting warnings on the inlined body so it is unneeded.
3064 elsif Nkind
(N
) = N_Pragma
3065 and then Pragma_Name
(N
) = Name_Unreferenced
3067 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
3073 end Process_Formals
;
3075 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
3077 --------------------------------
3078 -- Process_Formals_In_Aspects --
3079 --------------------------------
3081 function Process_Formals_In_Aspects
3082 (N
: Node_Id
) return Traverse_Result
3087 if Has_Aspects
(N
) then
3088 A
:= First
(Aspect_Specifications
(N
));
3089 while Present
(A
) loop
3090 Replace_Formals
(Expression
(A
));
3096 end Process_Formals_In_Aspects
;
3098 procedure Replace_Formals_In_Aspects
is
3099 new Traverse_Proc
(Process_Formals_In_Aspects
);
3105 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
3107 if not Debug_Generated_Code
then
3108 Set_Sloc
(Nod
, Sloc
(N
));
3109 Set_Comes_From_Source
(Nod
, False);
3115 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
3117 ------------------------------
3118 -- Reset_Dispatching_Calls --
3119 ------------------------------
3121 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
3123 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
3124 -- Comment required ???
3130 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
3132 if Nkind
(N
) = N_Procedure_Call_Statement
3133 and then Nkind
(Name
(N
)) = N_Selected_Component
3134 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
3135 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
3136 and then Is_Dispatching_Operation
3137 (Entity
(Selector_Name
(Name
(N
))))
3139 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
3145 function Do_Reset_Calls
is new Traverse_Func
(Do_Reset
);
3149 Dummy
: constant Traverse_Result
:= Do_Reset_Calls
(N
);
3150 pragma Unreferenced
(Dummy
);
3152 -- Start of processing for Reset_Dispatching_Calls
3156 end Reset_Dispatching_Calls
;
3158 ---------------------------
3159 -- Rewrite_Function_Call --
3160 ---------------------------
3162 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
3163 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
3164 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
3167 Make_Loop_Labels_Unique
(HSS
);
3169 -- Optimize simple case: function body is a single return statement,
3170 -- which has been expanded into an assignment.
3172 if Is_Empty_List
(Declarations
(Blk
))
3173 and then Nkind
(Fst
) = N_Assignment_Statement
3174 and then No
(Next
(Fst
))
3176 -- The function call may have been rewritten as the temporary
3177 -- that holds the result of the call, in which case remove the
3178 -- now useless declaration.
3180 if Nkind
(N
) = N_Identifier
3181 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
3183 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
3186 Rewrite
(N
, Expression
(Fst
));
3188 elsif Nkind
(N
) = N_Identifier
3189 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
3191 -- The block assigns the result of the call to the temporary
3193 Insert_After
(Parent
(Entity
(N
)), Blk
);
3195 -- If the context is an assignment, and the left-hand side is free of
3196 -- side-effects, the replacement is also safe.
3197 -- Can this be generalized further???
3199 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3201 (Is_Entity_Name
(Name
(Parent
(N
)))
3203 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3204 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
3207 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
3208 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
3210 -- Replace assignment with the block
3213 Original_Assignment
: constant Node_Id
:= Parent
(N
);
3216 -- Preserve the original assignment node to keep the complete
3217 -- assignment subtree consistent enough for Analyze_Assignment
3218 -- to proceed (specifically, the original Lhs node must still
3219 -- have an assignment statement as its parent).
3221 -- We cannot rely on Original_Node to go back from the block
3222 -- node to the assignment node, because the assignment might
3223 -- already be a rewrite substitution.
3225 Discard_Node
(Relocate_Node
(Original_Assignment
));
3226 Rewrite
(Original_Assignment
, Blk
);
3229 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
3231 -- A call to a function which returns an unconstrained type
3232 -- found in the expression initializing an object-declaration is
3233 -- expanded into a procedure call which must be added after the
3234 -- object declaration.
3236 if Is_Unc_Decl
and Back_End_Inlining
then
3237 Insert_Action_After
(Parent
(N
), Blk
);
3239 Set_Expression
(Parent
(N
), Empty
);
3240 Insert_After
(Parent
(N
), Blk
);
3243 elsif Is_Unc
and then not Back_End_Inlining
then
3244 Insert_Before
(Parent
(N
), Blk
);
3246 end Rewrite_Function_Call
;
3248 ----------------------------
3249 -- Rewrite_Procedure_Call --
3250 ----------------------------
3252 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
3253 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
3256 Make_Loop_Labels_Unique
(HSS
);
3258 -- If there is a transient scope for N, this will be the scope of the
3259 -- actions for N, and the statements in Blk need to be within this
3260 -- scope. For example, they need to have visibility on the constant
3261 -- declarations created for the formals.
3263 -- If N needs no transient scope, and if there are no declarations in
3264 -- the inlined body, we can do a little optimization and insert the
3265 -- statements for the body directly after N, and rewrite N to a
3266 -- null statement, instead of rewriting N into a full-blown block
3269 if not Scope_Is_Transient
3270 and then Is_Empty_List
(Declarations
(Blk
))
3272 Insert_List_After
(N
, Statements
(HSS
));
3273 Rewrite
(N
, Make_Null_Statement
(Loc
));
3277 end Rewrite_Procedure_Call
;
3279 -------------------------
3280 -- Formal_Is_Used_Once --
3281 -------------------------
3283 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
3284 Use_Counter
: Int
:= 0;
3286 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
3287 -- Traverse the tree and count the uses of the formal parameter.
3288 -- In this case, for optimization purposes, we do not need to
3289 -- continue the traversal once more than one use is encountered.
3295 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
3297 -- The original node is an identifier
3299 if Nkind
(N
) = N_Identifier
3300 and then Present
(Entity
(N
))
3302 -- Original node's entity points to the one in the copied body
3304 and then Nkind
(Entity
(N
)) = N_Identifier
3305 and then Present
(Entity
(Entity
(N
)))
3307 -- The entity of the copied node is the formal parameter
3309 and then Entity
(Entity
(N
)) = Formal
3311 Use_Counter
:= Use_Counter
+ 1;
3313 if Use_Counter
> 1 then
3315 -- Denote more than one use and abandon the traversal
3326 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
3328 -- Start of processing for Formal_Is_Used_Once
3331 Count_Formal_Uses
(Orig_Bod
);
3332 return Use_Counter
= 1;
3333 end Formal_Is_Used_Once
;
3335 -- Start of processing for Expand_Inlined_Call
3338 -- Initializations for old/new semantics
3340 if not Uses_Back_End
then
3341 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
3342 and then not Is_Constrained
(Etype
(Subp
));
3343 Is_Unc_Decl
:= False;
3345 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
3346 and then Optimization_Level
> 0;
3347 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
3351 -- Check for an illegal attempt to inline a recursive procedure. If the
3352 -- subprogram has parameters this is detected when trying to supply a
3353 -- binding for parameters that already have one. For parameterless
3354 -- subprograms this must be done explicitly.
3356 if In_Open_Scopes
(Subp
) then
3358 ("cannot inline call to recursive subprogram?", N
, Subp
);
3359 Set_Is_Inlined
(Subp
, False);
3362 -- Skip inlining if this is not a true inlining since the attribute
3363 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3364 -- true inlining, Orig_Bod has code rather than being an entity.
3366 elsif Nkind
(Orig_Bod
) in N_Entity
then
3370 if Nkind
(Orig_Bod
) = N_Defining_Identifier
3371 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
3373 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3374 -- can be replaced with calls to the renamed entity directly, because
3375 -- the subprograms are subtype conformant. If the renamed subprogram
3376 -- is an inherited operation, we must redo the expansion because
3377 -- implicit conversions may be needed. Similarly, if the renamed
3378 -- entity is inlined, expand the call for further optimizations.
3380 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
3382 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
3389 -- Register the call in the list of inlined calls
3391 Append_New_Elmt
(N
, To
=> Inlined_Calls
);
3393 -- Use generic machinery to copy body of inlined subprogram, as if it
3394 -- were an instantiation, resetting source locations appropriately, so
3395 -- that nested inlined calls appear in the main unit.
3397 Save_Env
(Subp
, Empty
);
3398 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
3402 if not Uses_Back_End
then
3407 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
3409 Make_Block_Statement
(Loc
,
3410 Declarations
=> Declarations
(Bod
),
3411 Handled_Statement_Sequence
=>
3412 Handled_Statement_Sequence
(Bod
));
3414 if No
(Declarations
(Bod
)) then
3415 Set_Declarations
(Blk
, New_List
);
3418 -- When generating C code, declare _Result, which may be used to
3419 -- verify the return value.
3421 if Modify_Tree_For_C
3422 and then Nkind
(N
) = N_Procedure_Call_Statement
3423 and then Chars
(Name
(N
)) = Name_uPostconditions
3425 Declare_Postconditions_Result
;
3428 -- For the unconstrained case, capture the name of the local
3429 -- variable that holds the result. This must be the first
3430 -- declaration in the block, because its bounds cannot depend
3431 -- on local variables. Otherwise there is no way to declare the
3432 -- result outside of the block. Needless to say, in general the
3433 -- bounds will depend on the actuals in the call.
3435 -- If the context is an assignment statement, as is the case
3436 -- for the expansion of an extended return, the left-hand side
3437 -- provides bounds even if the return type is unconstrained.
3441 First_Decl
: Node_Id
;
3444 First_Decl
:= First
(Declarations
(Blk
));
3446 -- If the body is a single extended return statement,the
3447 -- resulting block is a nested block.
3449 if No
(First_Decl
) then
3451 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
3453 if Nkind
(First_Decl
) = N_Block_Statement
then
3454 First_Decl
:= First
(Declarations
(First_Decl
));
3458 -- No front-end inlining possible
3460 if Nkind
(First_Decl
) /= N_Object_Declaration
then
3464 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
3465 Targ1
:= Defining_Identifier
(First_Decl
);
3467 Targ1
:= Name
(Parent
(N
));
3484 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
3486 Make_Block_Statement
(Loc
,
3487 Declarations
=> Declarations
(Bod
),
3488 Handled_Statement_Sequence
=>
3489 Handled_Statement_Sequence
(Bod
));
3491 -- Inline a call to a function that returns an unconstrained type.
3492 -- The semantic analyzer checked that frontend-inlined functions
3493 -- returning unconstrained types have no declarations and have
3494 -- a single extended return statement. As part of its processing
3495 -- the function was split into two subprograms: a procedure P' and
3496 -- a function F' that has a block with a call to procedure P' (see
3497 -- Split_Unconstrained_Function).
3503 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)))) =
3507 Blk_Stmt
: constant Node_Id
:=
3508 First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
3509 First_Stmt
: constant Node_Id
:=
3510 First
(Statements
(Handled_Statement_Sequence
(Blk_Stmt
)));
3511 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
3515 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
3516 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
3517 and then No
(Next
(Second_Stmt
)));
3522 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
3523 Empty
, Instantiating
=> True);
3526 -- Capture the name of the local variable that holds the
3527 -- result. This must be the first declaration in the block,
3528 -- because its bounds cannot depend on local variables.
3529 -- Otherwise there is no way to declare the result outside
3530 -- of the block. Needless to say, in general the bounds will
3531 -- depend on the actuals in the call.
3533 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
3534 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
3536 -- If the context is an assignment statement, as is the case
3537 -- for the expansion of an extended return, the left-hand
3538 -- side provides bounds even if the return type is
3542 Targ1
:= Name
(Parent
(N
));
3547 if No
(Declarations
(Bod
)) then
3548 Set_Declarations
(Blk
, New_List
);
3553 -- If this is a derived function, establish the proper return type
3555 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
3556 Ret_Type
:= Etype
(Orig_Subp
);
3558 Ret_Type
:= Etype
(Subp
);
3561 -- Create temporaries for the actuals that are expressions, or that are
3562 -- scalars and require copying to preserve semantics.
3564 F
:= First_Formal
(Subp
);
3565 A
:= First_Actual
(N
);
3566 while Present
(F
) loop
3567 if Present
(Renamed_Object
(F
)) then
3569 -- If expander is active, it is an error to try to inline a
3570 -- recursive program. In GNATprove mode, just indicate that the
3571 -- inlining will not happen, and mark the subprogram as not always
3574 if GNATprove_Mode
then
3576 ("cannot inline call to recursive subprogram?", N
, Subp
);
3577 Set_Is_Inlined_Always
(Subp
, False);
3580 ("cannot inline call to recursive subprogram", N
);
3586 -- Reset Last_Assignment for any parameters of mode out or in out, to
3587 -- prevent spurious warnings about overwriting for assignments to the
3588 -- formal in the inlined code.
3590 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
3591 Set_Last_Assignment
(Entity
(A
), Empty
);
3594 -- If the argument may be a controlling argument in a call within
3595 -- the inlined body, we must preserve its classwide nature to insure
3596 -- that dynamic dispatching take place subsequently. If the formal
3597 -- has a constraint it must be preserved to retain the semantics of
3600 if Is_Class_Wide_Type
(Etype
(F
))
3601 or else (Is_Access_Type
(Etype
(F
))
3602 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
3604 Temp_Typ
:= Etype
(F
);
3606 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
3607 and then Etype
(F
) /= Base_Type
(Etype
(F
))
3608 and then Is_Constrained
(Etype
(F
))
3610 Temp_Typ
:= Etype
(F
);
3613 Temp_Typ
:= Etype
(A
);
3616 -- If the actual is a simple name or a literal, no need to
3617 -- create a temporary, object can be used directly.
3619 -- If the actual is a literal and the formal has its address taken,
3620 -- we cannot pass the literal itself as an argument, so its value
3621 -- must be captured in a temporary. Skip this optimization in
3622 -- GNATprove mode, to make sure any check on a type conversion
3625 if (Is_Entity_Name
(A
)
3627 (not Is_Scalar_Type
(Etype
(A
))
3628 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
)
3629 and then not GNATprove_Mode
)
3631 -- When the actual is an identifier and the corresponding formal is
3632 -- used only once in the original body, the formal can be substituted
3633 -- directly with the actual parameter. Skip this optimization in
3634 -- GNATprove mode, to make sure any check on a type conversion
3638 (Nkind
(A
) = N_Identifier
3639 and then Formal_Is_Used_Once
(F
)
3640 and then not GNATprove_Mode
)
3643 (Nkind_In
(A
, N_Real_Literal
,
3645 N_Character_Literal
)
3646 and then not Address_Taken
(F
))
3648 if Etype
(F
) /= Etype
(A
) then
3650 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
3652 Set_Renamed_Object
(F
, A
);
3656 Temp
:= Make_Temporary
(Loc
, 'C');
3658 -- If the actual for an in/in-out parameter is a view conversion,
3659 -- make it into an unchecked conversion, given that an untagged
3660 -- type conversion is not a proper object for a renaming.
3662 -- In-out conversions that involve real conversions have already
3663 -- been transformed in Expand_Actuals.
3665 if Nkind
(A
) = N_Type_Conversion
3666 and then Ekind
(F
) /= E_In_Parameter
3669 Make_Unchecked_Type_Conversion
(Loc
,
3670 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
3671 Expression
=> Relocate_Node
(Expression
(A
)));
3673 -- In GNATprove mode, keep the most precise type of the actual for
3674 -- the temporary variable, when the formal type is unconstrained.
3675 -- Otherwise, the AST may contain unexpected assignment statements
3676 -- to a temporary variable of unconstrained type renaming a local
3677 -- variable of constrained type, which is not expected by
3680 elsif Etype
(F
) /= Etype
(A
)
3681 and then (not GNATprove_Mode
or else Is_Constrained
(Etype
(F
)))
3683 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
3684 Temp_Typ
:= Etype
(F
);
3687 New_A
:= Relocate_Node
(A
);
3690 Set_Sloc
(New_A
, Sloc
(N
));
3692 -- If the actual has a by-reference type, it cannot be copied,
3693 -- so its value is captured in a renaming declaration. Otherwise
3694 -- declare a local constant initialized with the actual.
3696 -- We also use a renaming declaration for expressions of an array
3697 -- type that is not bit-packed, both for efficiency reasons and to
3698 -- respect the semantics of the call: in most cases the original
3699 -- call will pass the parameter by reference, and thus the inlined
3700 -- code will have the same semantics.
3702 -- Finally, we need a renaming declaration in the case of limited
3703 -- types for which initialization cannot be by copy either.
3705 if Ekind
(F
) = E_In_Parameter
3706 and then not Is_By_Reference_Type
(Etype
(A
))
3707 and then not Is_Limited_Type
(Etype
(A
))
3709 (not Is_Array_Type
(Etype
(A
))
3710 or else not Is_Object_Reference
(A
)
3711 or else Is_Bit_Packed_Array
(Etype
(A
)))
3714 Make_Object_Declaration
(Loc
,
3715 Defining_Identifier
=> Temp
,
3716 Constant_Present
=> True,
3717 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3718 Expression
=> New_A
);
3721 -- In GNATprove mode, make an explicit copy of input
3722 -- parameters when formal and actual types differ, to make
3723 -- sure any check on the type conversion will be issued.
3724 -- The legality of the copy is ensured by calling first
3725 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3728 and then Ekind
(F
) /= E_Out_Parameter
3729 and then not Same_Type
(Etype
(F
), Etype
(A
))
3731 pragma Assert
(not Is_By_Reference_Type
(Etype
(A
)));
3732 pragma Assert
(not Is_Limited_Type
(Etype
(A
)));
3735 Make_Object_Declaration
(Loc
,
3736 Defining_Identifier
=> Make_Temporary
(Loc
, 'C'),
3737 Constant_Present
=> True,
3738 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3739 Expression
=> New_Copy_Tree
(New_A
)));
3743 Make_Object_Renaming_Declaration
(Loc
,
3744 Defining_Identifier
=> Temp
,
3745 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3749 Append
(Decl
, Decls
);
3750 Set_Renamed_Object
(F
, Temp
);
3757 -- Establish target of function call. If context is not assignment or
3758 -- declaration, create a temporary as a target. The declaration for the
3759 -- temporary may be subsequently optimized away if the body is a single
3760 -- expression, or if the left-hand side of the assignment is simple
3761 -- enough, i.e. an entity or an explicit dereference of one.
3763 if Ekind
(Subp
) = E_Function
then
3764 if Nkind
(Parent
(N
)) = N_Assignment_Statement
3765 and then Is_Entity_Name
(Name
(Parent
(N
)))
3767 Targ
:= Name
(Parent
(N
));
3769 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3770 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3771 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3773 Targ
:= Name
(Parent
(N
));
3775 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3776 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
3777 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3779 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
3781 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
3782 and then Is_Limited_Type
(Etype
(Subp
))
3784 Targ
:= Defining_Identifier
(Parent
(N
));
3786 -- New semantics: In an object declaration avoid an extra copy
3787 -- of the result of a call to an inlined function that returns
3788 -- an unconstrained type
3791 and then Nkind
(Parent
(N
)) = N_Object_Declaration
3794 Targ
:= Defining_Identifier
(Parent
(N
));
3797 -- Replace call with temporary and create its declaration
3799 Temp
:= Make_Temporary
(Loc
, 'C');
3800 Set_Is_Internal
(Temp
);
3802 -- For the unconstrained case, the generated temporary has the
3803 -- same constrained declaration as the result variable. It may
3804 -- eventually be possible to remove that temporary and use the
3805 -- result variable directly.
3807 if Is_Unc
and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
3810 Make_Object_Declaration
(Loc
,
3811 Defining_Identifier
=> Temp
,
3812 Object_Definition
=>
3813 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3815 Replace_Formals
(Decl
);
3819 Make_Object_Declaration
(Loc
,
3820 Defining_Identifier
=> Temp
,
3821 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
3823 Set_Etype
(Temp
, Ret_Type
);
3826 Set_No_Initialization
(Decl
);
3827 Append
(Decl
, Decls
);
3828 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3833 Insert_Actions
(N
, Decls
);
3837 -- Special management for inlining a call to a function that returns
3838 -- an unconstrained type and initializes an object declaration: we
3839 -- avoid generating undesired extra calls and goto statements.
3842 -- function Func (...) return String is
3845 -- Result : String (1 .. 4);
3847 -- Proc (Result, ...);
3852 -- Result : String := Func (...);
3854 -- Replace this object declaration by:
3856 -- Result : String (1 .. 4);
3857 -- Proc (Result, ...);
3859 Remove_Homonym
(Targ
);
3862 Make_Object_Declaration
3864 Defining_Identifier
=> Targ
,
3865 Object_Definition
=>
3866 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3867 Replace_Formals
(Decl
);
3868 Rewrite
(Parent
(N
), Decl
);
3869 Analyze
(Parent
(N
));
3871 -- Avoid spurious warnings since we know that this declaration is
3872 -- referenced by the procedure call.
3874 Set_Never_Set_In_Source
(Targ
, False);
3876 -- Remove the local declaration of the extended return stmt from the
3879 Remove
(Parent
(Targ1
));
3881 -- Update the reference to the result (since we have rewriten the
3882 -- object declaration)
3885 Blk_Call_Stmt
: Node_Id
;
3888 -- Capture the call to the procedure
3891 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
3893 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
3895 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
3896 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
3897 New_Occurrence_Of
(Targ
, Loc
));
3900 -- Remove the return statement
3903 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3904 N_Simple_Return_Statement
);
3906 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3909 -- Traverse the tree and replace formals with actuals or their thunks.
3910 -- Attach block to tree before analysis and rewriting.
3912 Replace_Formals
(Blk
);
3913 Replace_Formals_In_Aspects
(Blk
);
3914 Set_Parent
(Blk
, N
);
3916 if GNATprove_Mode
then
3919 elsif not Comes_From_Source
(Subp
) or else Is_Predef
then
3925 -- No action needed since return statement has been already removed
3929 elsif Present
(Exit_Lab
) then
3931 -- If there's a single return statement at the end of the subprogram,
3932 -- the corresponding goto statement and the corresponding label are
3937 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3940 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3942 Append
(Lab_Decl
, (Declarations
(Blk
)));
3943 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
3947 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3948 -- on conflicting private views that Gigi would ignore. If this is a
3949 -- predefined unit, analyze with checks off, as is done in the non-
3950 -- inlined run-time units.
3953 I_Flag
: constant Boolean := In_Inlined_Body
;
3956 In_Inlined_Body
:= True;
3960 Style
: constant Boolean := Style_Check
;
3963 Style_Check
:= False;
3965 -- Search for dispatching calls that use the Object.Operation
3966 -- notation using an Object that is a parameter of the inlined
3967 -- function. We reset the decoration of Operation to force
3968 -- the reanalysis of the inlined dispatching call because
3969 -- the actual object has been inlined.
3971 Reset_Dispatching_Calls
(Blk
);
3973 Analyze
(Blk
, Suppress
=> All_Checks
);
3974 Style_Check
:= Style
;
3981 In_Inlined_Body
:= I_Flag
;
3984 if Ekind
(Subp
) = E_Procedure
then
3985 Rewrite_Procedure_Call
(N
, Blk
);
3988 Rewrite_Function_Call
(N
, Blk
);
3993 -- For the unconstrained case, the replacement of the call has been
3994 -- made prior to the complete analysis of the generated declarations.
3995 -- Propagate the proper type now.
3998 if Nkind
(N
) = N_Identifier
then
3999 Set_Etype
(N
, Etype
(Entity
(N
)));
4001 Set_Etype
(N
, Etype
(Targ1
));
4008 -- Cleanup mapping between formals and actuals for other expansions
4010 F
:= First_Formal
(Subp
);
4011 while Present
(F
) loop
4012 Set_Renamed_Object
(F
, Empty
);
4015 end Expand_Inlined_Call
;
4017 --------------------------
4018 -- Get_Code_Unit_Entity --
4019 --------------------------
4021 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
is
4022 Unit
: Entity_Id
:= Cunit_Entity
(Get_Code_Unit
(E
));
4025 if Ekind
(Unit
) = E_Package_Body
then
4026 Unit
:= Spec_Entity
(Unit
);
4030 end Get_Code_Unit_Entity
;
4032 ------------------------------
4033 -- Has_Excluded_Declaration --
4034 ------------------------------
4036 function Has_Excluded_Declaration
4038 Decls
: List_Id
) return Boolean
4042 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4043 -- Nested subprograms make a given body ineligible for inlining, but
4044 -- we make an exception for instantiations of unchecked conversion.
4045 -- The body has not been analyzed yet, so check the name, and verify
4046 -- that the visible entity with that name is the predefined unit.
4048 -----------------------------
4049 -- Is_Unchecked_Conversion --
4050 -----------------------------
4052 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4053 Id
: constant Node_Id
:= Name
(D
);
4057 if Nkind
(Id
) = N_Identifier
4058 and then Chars
(Id
) = Name_Unchecked_Conversion
4060 Conv
:= Current_Entity
(Id
);
4062 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
4063 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4065 Conv
:= Current_Entity
(Selector_Name
(Id
));
4070 return Present
(Conv
)
4071 and then Is_Predefined_Unit
(Get_Source_Unit
(Conv
))
4072 and then Is_Intrinsic_Subprogram
(Conv
);
4073 end Is_Unchecked_Conversion
;
4075 -- Start of processing for Has_Excluded_Declaration
4078 -- No action needed if the check is not needed
4080 if not Check_Inlining_Restrictions
then
4085 while Present
(D
) loop
4087 -- First declarations universally excluded
4089 if Nkind
(D
) = N_Package_Declaration
then
4091 ("cannot inline & (nested package declaration)?", D
, Subp
);
4094 elsif Nkind
(D
) = N_Package_Instantiation
then
4096 ("cannot inline & (nested package instantiation)?", D
, Subp
);
4100 -- Then declarations excluded only for front-end inlining
4102 if Back_End_Inlining
then
4105 elsif Nkind
(D
) = N_Task_Type_Declaration
4106 or else Nkind
(D
) = N_Single_Task_Declaration
4109 ("cannot inline & (nested task type declaration)?", D
, Subp
);
4112 elsif Nkind
(D
) = N_Protected_Type_Declaration
4113 or else Nkind
(D
) = N_Single_Protected_Declaration
4116 ("cannot inline & (nested protected type declaration)?",
4120 elsif Nkind
(D
) = N_Subprogram_Body
then
4122 ("cannot inline & (nested subprogram)?", D
, Subp
);
4125 elsif Nkind
(D
) = N_Function_Instantiation
4126 and then not Is_Unchecked_Conversion
(D
)
4129 ("cannot inline & (nested function instantiation)?", D
, Subp
);
4132 elsif Nkind
(D
) = N_Procedure_Instantiation
then
4134 ("cannot inline & (nested procedure instantiation)?", D
, Subp
);
4137 -- Subtype declarations with predicates will generate predicate
4138 -- functions, i.e. nested subprogram bodies, so inlining is not
4141 elsif Nkind
(D
) = N_Subtype_Declaration
4142 and then Present
(Aspect_Specifications
(D
))
4149 A
:= First
(Aspect_Specifications
(D
));
4150 while Present
(A
) loop
4151 A_Id
:= Get_Aspect_Id
(Chars
(Identifier
(A
)));
4153 if A_Id
= Aspect_Predicate
4154 or else A_Id
= Aspect_Static_Predicate
4155 or else A_Id
= Aspect_Dynamic_Predicate
4158 ("cannot inline & (subtype declaration with "
4159 & "predicate)?", D
, Subp
);
4172 end Has_Excluded_Declaration
;
4174 ----------------------------
4175 -- Has_Excluded_Statement --
4176 ----------------------------
4178 function Has_Excluded_Statement
4180 Stats
: List_Id
) return Boolean
4186 -- No action needed if the check is not needed
4188 if not Check_Inlining_Restrictions
then
4193 while Present
(S
) loop
4194 if Nkind_In
(S
, N_Abort_Statement
,
4195 N_Asynchronous_Select
,
4196 N_Conditional_Entry_Call
,
4197 N_Delay_Relative_Statement
,
4198 N_Delay_Until_Statement
,
4203 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4206 elsif Nkind
(S
) = N_Block_Statement
then
4207 if Present
(Declarations
(S
))
4208 and then Has_Excluded_Declaration
(Subp
, Declarations
(S
))
4212 elsif Present
(Handled_Statement_Sequence
(S
)) then
4213 if not Back_End_Inlining
4216 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4219 ("cannot inline& (exception handler)?",
4220 First
(Exception_Handlers
4221 (Handled_Statement_Sequence
(S
))),
4225 elsif Has_Excluded_Statement
4226 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
4232 elsif Nkind
(S
) = N_Case_Statement
then
4233 E
:= First
(Alternatives
(S
));
4234 while Present
(E
) loop
4235 if Has_Excluded_Statement
(Subp
, Statements
(E
)) then
4242 elsif Nkind
(S
) = N_If_Statement
then
4243 if Has_Excluded_Statement
(Subp
, Then_Statements
(S
)) then
4247 if Present
(Elsif_Parts
(S
)) then
4248 E
:= First
(Elsif_Parts
(S
));
4249 while Present
(E
) loop
4250 if Has_Excluded_Statement
(Subp
, Then_Statements
(E
)) then
4258 if Present
(Else_Statements
(S
))
4259 and then Has_Excluded_Statement
(Subp
, Else_Statements
(S
))
4264 elsif Nkind
(S
) = N_Loop_Statement
4265 and then Has_Excluded_Statement
(Subp
, Statements
(S
))
4269 elsif Nkind
(S
) = N_Extended_Return_Statement
then
4270 if Present
(Handled_Statement_Sequence
(S
))
4272 Has_Excluded_Statement
4273 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
4277 elsif not Back_End_Inlining
4278 and then Present
(Handled_Statement_Sequence
(S
))
4280 Present
(Exception_Handlers
4281 (Handled_Statement_Sequence
(S
)))
4284 ("cannot inline& (exception handler)?",
4285 First
(Exception_Handlers
(Handled_Statement_Sequence
(S
))),
4295 end Has_Excluded_Statement
;
4297 --------------------------
4298 -- Has_Initialized_Type --
4299 --------------------------
4301 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean is
4302 E_Body
: constant Node_Id
:= Subprogram_Body
(E
);
4306 if No
(E_Body
) then -- imported subprogram
4310 Decl
:= First
(Declarations
(E_Body
));
4311 while Present
(Decl
) loop
4312 if Nkind
(Decl
) = N_Full_Type_Declaration
4313 and then Present
(Init_Proc
(Defining_Identifier
(Decl
)))
4323 end Has_Initialized_Type
;
4325 -----------------------
4326 -- Has_Single_Return --
4327 -----------------------
4329 function Has_Single_Return
(N
: Node_Id
) return Boolean is
4330 Return_Statement
: Node_Id
:= Empty
;
4332 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
4338 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
4340 if Nkind
(N
) = N_Simple_Return_Statement
then
4341 if Present
(Expression
(N
))
4342 and then Is_Entity_Name
(Expression
(N
))
4344 pragma Assert
(Present
(Entity
(Expression
(N
))));
4346 if No
(Return_Statement
) then
4347 Return_Statement
:= N
;
4352 (Present
(Entity
(Expression
(Return_Statement
))));
4354 if Entity
(Expression
(N
)) =
4355 Entity
(Expression
(Return_Statement
))
4363 -- A return statement within an extended return is a noop after
4366 elsif No
(Expression
(N
))
4367 and then Nkind
(Parent
(Parent
(N
))) =
4368 N_Extended_Return_Statement
4373 -- Expression has wrong form
4378 -- We can only inline a build-in-place function if it has a single
4381 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4382 if No
(Return_Statement
) then
4383 Return_Statement
:= N
;
4395 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
4397 -- Start of processing for Has_Single_Return
4400 if Check_All_Returns
(N
) /= OK
then
4403 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
4408 Present
(Declarations
(N
))
4409 and then Present
(First
(Declarations
(N
)))
4410 and then Entity
(Expression
(Return_Statement
)) =
4411 Defining_Identifier
(First
(Declarations
(N
)));
4413 end Has_Single_Return
;
4415 -----------------------------
4416 -- In_Main_Unit_Or_Subunit --
4417 -----------------------------
4419 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean is
4420 Comp
: Node_Id
:= Cunit
(Get_Code_Unit
(E
));
4423 -- Check whether the subprogram or package to inline is within the main
4424 -- unit or its spec or within a subunit. In either case there are no
4425 -- additional bodies to process. If the subprogram appears in a parent
4426 -- of the current unit, the check on whether inlining is possible is
4427 -- done in Analyze_Inlined_Bodies.
4429 while Nkind
(Unit
(Comp
)) = N_Subunit
loop
4430 Comp
:= Library_Unit
(Comp
);
4433 return Comp
= Cunit
(Main_Unit
)
4434 or else Comp
= Library_Unit
(Cunit
(Main_Unit
));
4435 end In_Main_Unit_Or_Subunit
;
4441 procedure Initialize
is
4443 Pending_Instantiations
.Init
;
4444 Inlined_Bodies
.Init
;
4448 for J
in Hash_Headers
'Range loop
4449 Hash_Headers
(J
) := No_Subp
;
4452 Inlined_Calls
:= No_Elist
;
4453 Backend_Calls
:= No_Elist
;
4454 Backend_Instances
:= No_Elist
;
4455 Backend_Inlined_Subps
:= No_Elist
;
4456 Backend_Not_Inlined_Subps
:= No_Elist
;
4459 ------------------------
4460 -- Instantiate_Bodies --
4461 ------------------------
4463 -- Generic bodies contain all the non-local references, so an
4464 -- instantiation does not need any more context than Standard
4465 -- itself, even if the instantiation appears in an inner scope.
4466 -- Generic associations have verified that the contract model is
4467 -- satisfied, so that any error that may occur in the analysis of
4468 -- the body is an internal error.
4470 procedure Instantiate_Bodies
is
4472 procedure Instantiate_Body
(Info
: Pending_Body_Info
);
4473 -- Instantiate a pending body
4475 ------------------------
4476 -- Instantiate_Body --
4477 ------------------------
4479 procedure Instantiate_Body
(Info
: Pending_Body_Info
) is
4481 -- If the instantiation node is absent, it has been removed as part
4482 -- of unreachable code.
4484 if No
(Info
.Inst_Node
) then
4487 elsif Nkind
(Info
.Act_Decl
) = N_Package_Declaration
then
4488 Instantiate_Package_Body
(Info
);
4489 Add_Scope_To_Clean
(Defining_Entity
(Info
.Act_Decl
));
4492 Instantiate_Subprogram_Body
(Info
);
4494 end Instantiate_Body
;
4497 Info
: Pending_Body_Info
;
4499 -- Start of processing for Instantiate_Bodies
4502 if Serious_Errors_Detected
= 0 then
4503 Expander_Active
:= (Operating_Mode
= Opt
.Generate_Code
);
4504 Push_Scope
(Standard_Standard
);
4505 To_Clean
:= New_Elmt_List
;
4507 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4511 -- A body instantiation may generate additional instantiations, so
4512 -- the following loop must scan to the end of a possibly expanding
4513 -- set (that's why we cannot simply use a FOR loop here). We must
4514 -- also capture the element lest the set be entirely reallocated.
4517 if Back_End_Inlining
then
4518 while J
<= Called_Pending_Instantiations
.Last
4519 and then Serious_Errors_Detected
= 0
4521 K
:= Called_Pending_Instantiations
.Table
(J
);
4522 Info
:= Pending_Instantiations
.Table
(K
);
4523 Instantiate_Body
(Info
);
4529 while J
<= Pending_Instantiations
.Last
4530 and then Serious_Errors_Detected
= 0
4532 Info
:= Pending_Instantiations
.Table
(J
);
4533 Instantiate_Body
(Info
);
4539 -- Reset the table of instantiations. Additional instantiations
4540 -- may be added through inlining, when additional bodies are
4543 if Back_End_Inlining
then
4544 Called_Pending_Instantiations
.Init
;
4546 Pending_Instantiations
.Init
;
4549 -- We can now complete the cleanup actions of scopes that contain
4550 -- pending instantiations (skipped for generic units, since we
4551 -- never need any cleanups in generic units).
4554 and then not Is_Generic_Unit
(Main_Unit_Entity
)
4557 elsif Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4563 end Instantiate_Bodies
;
4569 function Is_Nested
(E
: Entity_Id
) return Boolean is
4574 while Scop
/= Standard_Standard
loop
4575 if Is_Subprogram
(Scop
) then
4578 elsif Ekind
(Scop
) = E_Task_Type
4579 or else Ekind
(Scop
) = E_Entry
4580 or else Ekind
(Scop
) = E_Entry_Family
4585 Scop
:= Scope
(Scop
);
4591 ------------------------
4592 -- List_Inlining_Info --
4593 ------------------------
4595 procedure List_Inlining_Info
is
4601 if not Debug_Flag_Dot_J
then
4605 -- Generate listing of calls inlined by the frontend
4607 if Present
(Inlined_Calls
) then
4609 Elmt
:= First_Elmt
(Inlined_Calls
);
4610 while Present
(Elmt
) loop
4613 if not In_Internal_Unit
(Nod
) then
4617 Write_Str
("List of calls inlined by the frontend");
4624 Write_Location
(Sloc
(Nod
));
4633 -- Generate listing of calls passed to the backend
4635 if Present
(Backend_Calls
) then
4638 Elmt
:= First_Elmt
(Backend_Calls
);
4639 while Present
(Elmt
) loop
4642 if not In_Internal_Unit
(Nod
) then
4646 Write_Str
("List of inlined calls passed to the backend");
4653 Write_Location
(Sloc
(Nod
));
4661 -- Generate listing of instances inlined for the backend
4663 if Present
(Backend_Instances
) then
4666 Elmt
:= First_Elmt
(Backend_Instances
);
4667 while Present
(Elmt
) loop
4670 if not In_Internal_Unit
(Nod
) then
4674 Write_Str
("List of instances inlined for the backend");
4681 Write_Location
(Sloc
(Nod
));
4689 -- Generate listing of subprograms passed to the backend
4691 if Present
(Backend_Inlined_Subps
) and then Back_End_Inlining
then
4694 Elmt
:= First_Elmt
(Backend_Inlined_Subps
);
4695 while Present
(Elmt
) loop
4698 if not In_Internal_Unit
(Nod
) then
4703 ("List of inlined subprograms passed to the backend");
4710 Write_Name
(Chars
(Nod
));
4712 Write_Location
(Sloc
(Nod
));
4721 -- Generate listing of subprograms that cannot be inlined by the backend
4723 if Present
(Backend_Not_Inlined_Subps
) and then Back_End_Inlining
then
4726 Elmt
:= First_Elmt
(Backend_Not_Inlined_Subps
);
4727 while Present
(Elmt
) loop
4730 if not In_Internal_Unit
(Nod
) then
4735 ("List of subprograms that cannot be inlined by backend");
4742 Write_Name
(Chars
(Nod
));
4744 Write_Location
(Sloc
(Nod
));
4752 end List_Inlining_Info
;
4760 Pending_Instantiations
.Release
;
4761 Pending_Instantiations
.Locked
:= True;
4762 Inlined_Bodies
.Release
;
4763 Inlined_Bodies
.Locked
:= True;
4765 Successors
.Locked
:= True;
4767 Inlined
.Locked
:= True;
4770 --------------------------------
4771 -- Remove_Aspects_And_Pragmas --
4772 --------------------------------
4774 procedure Remove_Aspects_And_Pragmas
(Body_Decl
: Node_Id
) is
4775 procedure Remove_Items
(List
: List_Id
);
4776 -- Remove all useless aspects/pragmas from a particular list
4782 procedure Remove_Items
(List
: List_Id
) is
4785 Next_Item
: Node_Id
;
4788 -- Traverse the list looking for an aspect specification or a pragma
4790 Item
:= First
(List
);
4791 while Present
(Item
) loop
4792 Next_Item
:= Next
(Item
);
4794 if Nkind
(Item
) = N_Aspect_Specification
then
4795 Item_Id
:= Identifier
(Item
);
4796 elsif Nkind
(Item
) = N_Pragma
then
4797 Item_Id
:= Pragma_Identifier
(Item
);
4802 if Present
(Item_Id
)
4803 and then Nam_In
(Chars
(Item_Id
), Name_Contract_Cases
,
4808 Name_Refined_Global
,
4809 Name_Refined_Depends
,
4823 -- Start of processing for Remove_Aspects_And_Pragmas
4826 Remove_Items
(Aspect_Specifications
(Body_Decl
));
4827 Remove_Items
(Declarations
(Body_Decl
));
4829 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
4830 -- in the body of the subprogram.
4832 Remove_Items
(Statements
(Handled_Statement_Sequence
(Body_Decl
)));
4833 end Remove_Aspects_And_Pragmas
;
4835 --------------------------
4836 -- Remove_Dead_Instance --
4837 --------------------------
4839 procedure Remove_Dead_Instance
(N
: Node_Id
) is
4844 while J
<= Pending_Instantiations
.Last
loop
4845 if Pending_Instantiations
.Table
(J
).Inst_Node
= N
then
4846 Pending_Instantiations
.Table
(J
).Inst_Node
:= Empty
;
4852 end Remove_Dead_Instance
;