1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Einfo
.Entities
; use Einfo
.Entities
;
32 with Einfo
.Utils
; use Einfo
.Utils
;
33 with Elists
; use Elists
;
34 with Errout
; use Errout
;
35 with Exp_Ch6
; use Exp_Ch6
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Tss
; use Exp_Tss
;
38 with Exp_Util
; use Exp_Util
;
39 with Fname
; use Fname
;
40 with Fname
.UF
; use Fname
.UF
;
42 with Namet
; use Namet
;
43 with Nmake
; use Nmake
;
44 with Nlists
; use Nlists
;
45 with Output
; use Output
;
46 with Sem_Aux
; use Sem_Aux
;
47 with Sem_Ch8
; use Sem_Ch8
;
48 with Sem_Ch10
; use Sem_Ch10
;
49 with Sem_Ch12
; use Sem_Ch12
;
50 with Sem_Prag
; use Sem_Prag
;
51 with Sem_Res
; use Sem_Res
;
52 with Sem_Util
; use Sem_Util
;
53 with Sinfo
; use Sinfo
;
54 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
55 with Sinfo
.Utils
; use Sinfo
.Utils
;
56 with Sinput
; use Sinput
;
57 with Snames
; use Snames
;
58 with Stand
; use Stand
;
60 with Tbuild
; use Tbuild
;
61 with Uintp
; use Uintp
;
62 with Uname
; use Uname
;
66 package body Inline
is
68 Check_Inlining_Restrictions
: constant Boolean := True;
69 -- In the following cases the frontend rejects inlining because they
70 -- are not handled well by the backend. This variable facilitates
71 -- disabling these restrictions to evaluate future versions of the
72 -- GCC backend in which some of the restrictions may be supported.
74 -- - subprograms that have:
75 -- - nested subprograms
77 -- - package declarations
78 -- - task or protected object declarations
79 -- - some of the following statements:
81 -- - asynchronous-select
82 -- - conditional-entry-call
88 Inlined_Calls
: Elist_Id
;
89 -- List of frontend inlined calls
91 Backend_Calls
: Elist_Id
;
92 -- List of inline calls passed to the backend
94 Backend_Instances
: Elist_Id
;
95 -- List of instances inlined for the backend
97 Backend_Inlined_Subps
: Elist_Id
;
98 -- List of subprograms inlined by the backend
100 Backend_Not_Inlined_Subps
: Elist_Id
;
101 -- List of subprograms that cannot be inlined by the backend
103 -----------------------------
104 -- Pending_Instantiations --
105 -----------------------------
107 -- We make entries in this table for the pending instantiations of generic
108 -- bodies that are created during semantic analysis. After the analysis is
109 -- complete, calling Instantiate_Bodies performs the actual instantiations.
111 package Pending_Instantiations
is new Table
.Table
(
112 Table_Component_Type
=> Pending_Body_Info
,
113 Table_Index_Type
=> Int
,
114 Table_Low_Bound
=> 0,
115 Table_Initial
=> Alloc
.Pending_Instantiations_Initial
,
116 Table_Increment
=> Alloc
.Pending_Instantiations_Increment
,
117 Table_Name
=> "Pending_Instantiations");
119 -------------------------------------
120 -- Called_Pending_Instantiations --
121 -------------------------------------
123 -- With back-end inlining, the pending instantiations that are not in the
124 -- main unit or subunit are performed only after a call to the subprogram
125 -- instance, or to a subprogram within the package instance, is inlined.
126 -- Since such a call can be within a subsequent pending instantiation,
127 -- we make entries in this table that stores the index of these "called"
128 -- pending instantiations and perform them when the table is populated.
130 package Called_Pending_Instantiations
is new Table
.Table
(
131 Table_Component_Type
=> Int
,
132 Table_Index_Type
=> Int
,
133 Table_Low_Bound
=> 0,
134 Table_Initial
=> Alloc
.Pending_Instantiations_Initial
,
135 Table_Increment
=> Alloc
.Pending_Instantiations_Increment
,
136 Table_Name
=> "Called_Pending_Instantiations");
138 ---------------------------------
139 -- To_Pending_Instantiations --
140 ---------------------------------
142 -- With back-end inlining, we also need to have a map from the pending
143 -- instantiations to their index in the Pending_Instantiations table.
145 Node_Table_Size
: constant := 257;
146 -- Number of headers in hash table
148 subtype Node_Header_Num
is Integer range 0 .. Node_Table_Size
- 1;
149 -- Range of headers in hash table
151 function Node_Hash
(Id
: Node_Id
) return Node_Header_Num
;
152 -- Simple hash function for Node_Ids
154 package To_Pending_Instantiations
is new GNAT
.Htable
.Simple_HTable
155 (Header_Num
=> Node_Header_Num
,
166 function Node_Hash
(Id
: Node_Id
) return Node_Header_Num
is
168 return Node_Header_Num
(Id
mod Node_Table_Size
);
175 -- Inlined functions are actually placed in line by the backend if the
176 -- corresponding bodies are available (i.e. compiled). Whenever we find
177 -- a call to an inlined subprogram, we add the name of the enclosing
178 -- compilation unit to a worklist. After all compilation, and after
179 -- expansion of generic bodies, we traverse the list of pending bodies
180 -- and compile them as well.
182 package Inlined_Bodies
is new Table
.Table
(
183 Table_Component_Type
=> Entity_Id
,
184 Table_Index_Type
=> Int
,
185 Table_Low_Bound
=> 0,
186 Table_Initial
=> Alloc
.Inlined_Bodies_Initial
,
187 Table_Increment
=> Alloc
.Inlined_Bodies_Increment
,
188 Table_Name
=> "Inlined_Bodies");
190 -----------------------
191 -- Inline Processing --
192 -----------------------
194 -- For each call to an inlined subprogram, we make entries in a table
195 -- that stores caller and callee, and indicates the call direction from
196 -- one to the other. We also record the compilation unit that contains
197 -- the callee. After analyzing the bodies of all such compilation units,
198 -- we compute the transitive closure of inlined subprograms called from
199 -- the main compilation unit and make it available to the code generator
200 -- in no particular order, thus allowing cycles in the call graph.
202 Last_Inlined
: Entity_Id
:= Empty
;
204 -- For each entry in the table we keep a list of successors in topological
205 -- order, i.e. callers of the current subprogram.
207 type Subp_Index
is new Nat
;
208 No_Subp
: constant Subp_Index
:= 0;
210 -- The subprogram entities are hashed into the Inlined table
212 Num_Hash_Headers
: constant := 512;
214 Hash_Headers
: array (Subp_Index
range 0 .. Num_Hash_Headers
- 1)
217 type Succ_Index
is new Nat
;
218 No_Succ
: constant Succ_Index
:= 0;
220 type Succ_Info
is record
225 -- The following table stores list elements for the successor lists. These
226 -- lists cannot be chained directly through entries in the Inlined table,
227 -- because a given subprogram can appear in several such lists.
229 package Successors
is new Table
.Table
(
230 Table_Component_Type
=> Succ_Info
,
231 Table_Index_Type
=> Succ_Index
,
232 Table_Low_Bound
=> 1,
233 Table_Initial
=> Alloc
.Successors_Initial
,
234 Table_Increment
=> Alloc
.Successors_Increment
,
235 Table_Name
=> "Successors");
237 type Subp_Info
is record
238 Name
: Entity_Id
:= Empty
;
239 Next
: Subp_Index
:= No_Subp
;
240 First_Succ
: Succ_Index
:= No_Succ
;
241 Main_Call
: Boolean := False;
242 Processed
: Boolean := False;
245 package Inlined
is new Table
.Table
(
246 Table_Component_Type
=> Subp_Info
,
247 Table_Index_Type
=> Subp_Index
,
248 Table_Low_Bound
=> 1,
249 Table_Initial
=> Alloc
.Inlined_Initial
,
250 Table_Increment
=> Alloc
.Inlined_Increment
,
251 Table_Name
=> "Inlined");
253 -----------------------
254 -- Local Subprograms --
255 -----------------------
257 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
);
258 -- Make two entries in Inlined table, for an inlined subprogram being
259 -- called, and for the inlined subprogram that contains the call. If
260 -- the call is in the main compilation unit, Caller is Empty.
262 procedure Add_Inlined_Instance
(E
: Entity_Id
);
263 -- Add instance E to the list of inlined instances for the unit
265 procedure Add_Inlined_Subprogram
(E
: Entity_Id
);
266 -- Add subprogram E to the list of inlined subprograms for the unit
268 function Add_Subp
(E
: Entity_Id
) return Subp_Index
;
269 -- Make entry in Inlined table for subprogram E, or return table index
270 -- that already holds E.
272 procedure Establish_Actual_Mapping_For_Inlined_Call
276 Body_Or_Expr_To_Check
: Node_Id
);
277 -- Establish a mapping from formals to actuals in the call N for the target
278 -- subprogram Subp, and create temporaries or renamings when needed for the
279 -- actuals that are expressions (except for actuals given by simple entity
280 -- names or literals) or that are scalars that require copying to preserve
281 -- semantics. Any temporary objects that are created are inserted in Decls.
282 -- Body_Or_Expr_To_Check indicates the target body (or possibly expression
283 -- of an expression function), which may be traversed to count formal uses.
285 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
;
286 pragma Inline
(Get_Code_Unit_Entity
);
287 -- Return the entity node for the unit containing E. Always return the spec
290 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean;
291 -- If a candidate for inlining contains type declarations for types with
292 -- nontrivial initialization procedures, they are not worth inlining.
294 function Has_Single_Return
(N
: Node_Id
) return Boolean;
295 -- In general we cannot inline functions that return unconstrained type.
296 -- However, we can handle such functions if all return statements return
297 -- a local variable that is the first declaration in the body of the
298 -- function. In that case the call can be replaced by that local
299 -- variable as is done for other inlined calls.
301 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean;
302 -- Return True if E is in the main unit or its spec or in a subunit
304 function Is_Nested
(E
: Entity_Id
) return Boolean;
305 -- If the function is nested inside some other function, it will always
306 -- be compiled if that function is, so don't add it to the inline list.
307 -- We cannot compile a nested function outside the scope of the containing
308 -- function anyway. This is also the case if the function is defined in a
309 -- task body or within an entry (for example, an initialization procedure).
311 procedure Remove_Aspects_And_Pragmas
(Body_Decl
: Node_Id
);
312 -- Remove all aspects and/or pragmas that have no meaning in inlined body
313 -- Body_Decl. The analysis of these items is performed on the non-inlined
314 -- body. The items currently removed are:
325 -- Subprogram_Variant
330 procedure Reset_Actual_Mapping_For_Inlined_Call
(Subp
: Entity_Id
);
331 -- Reset the Renamed_Object field to Empty on all formals of Subp, which
332 -- can be set by a call to Establish_Actual_Mapping_For_Inlined_Call.
334 ------------------------------
335 -- Deferred Cleanup Actions --
336 ------------------------------
338 -- The cleanup actions for scopes that contain package instantiations with
339 -- a body are delayed until after the package body is instantiated. because
340 -- the body may contain finalizable objects or other constructs that affect
341 -- the cleanup code. A scope that contains such instantiations only needs
342 -- to be finalized once, even though it may contain more than one instance.
343 -- We keep a list of scopes that must still be finalized and Cleanup_Scopes
344 -- will be invoked after all the body instantiations have been completed.
348 procedure Add_Scope_To_Clean
(Scop
: Entity_Id
);
349 -- Build set of scopes on which cleanup actions must be performed
351 procedure Cleanup_Scopes
;
352 -- Complete cleanup actions on scopes that need it
358 procedure Add_Call
(Called
: Entity_Id
; Caller
: Entity_Id
:= Empty
) is
359 P1
: constant Subp_Index
:= Add_Subp
(Called
);
364 if Present
(Caller
) then
365 P2
:= Add_Subp
(Caller
);
367 -- Add P1 to the list of successors of P2, if not already there.
368 -- Note that P2 may contain more than one call to P1, and only
369 -- one needs to be recorded.
371 J
:= Inlined
.Table
(P2
).First_Succ
;
372 while J
/= No_Succ
loop
373 if Successors
.Table
(J
).Subp
= P1
then
377 J
:= Successors
.Table
(J
).Next
;
380 -- On exit, make a successor entry for P1
382 Successors
.Increment_Last
;
383 Successors
.Table
(Successors
.Last
).Subp
:= P1
;
384 Successors
.Table
(Successors
.Last
).Next
:=
385 Inlined
.Table
(P2
).First_Succ
;
386 Inlined
.Table
(P2
).First_Succ
:= Successors
.Last
;
388 Inlined
.Table
(P1
).Main_Call
:= True;
392 ----------------------
393 -- Add_Inlined_Body --
394 ----------------------
396 procedure Add_Inlined_Body
(E
: Entity_Id
; N
: Node_Id
) is
398 type Inline_Level_Type
is (Dont_Inline
, Inline_Call
, Inline_Package
);
399 -- Level of inlining for the call: Dont_Inline means no inlining,
400 -- Inline_Call means that only the call is considered for inlining,
401 -- Inline_Package means that the call is considered for inlining and
402 -- its package compiled and scanned for more inlining opportunities.
404 function Is_Non_Loading_Expression_Function
405 (Id
: Entity_Id
) return Boolean;
406 -- Determine whether arbitrary entity Id denotes a subprogram which is
409 -- * An expression function
411 -- * A function completed by an expression function where both the
412 -- spec and body are in the same context.
414 function Must_Inline
return Inline_Level_Type
;
415 -- Inlining is only done if the call statement N is in the main unit,
416 -- or within the body of another inlined subprogram.
418 ----------------------------------------
419 -- Is_Non_Loading_Expression_Function --
420 ----------------------------------------
422 function Is_Non_Loading_Expression_Function
423 (Id
: Entity_Id
) return Boolean
430 -- A stand-alone expression function is transformed into a spec-body
431 -- pair in-place. Since both the spec and body are in the same list,
432 -- the inlining of such an expression function does not need to load
435 if Is_Expression_Function
(Id
) then
438 -- A function may be completed by an expression function
440 elsif Ekind
(Id
) = E_Function
then
441 Spec_Decl
:= Unit_Declaration_Node
(Id
);
443 if Nkind
(Spec_Decl
) = N_Subprogram_Declaration
then
444 Body_Id
:= Corresponding_Body
(Spec_Decl
);
446 if Present
(Body_Id
) then
447 Body_Decl
:= Unit_Declaration_Node
(Body_Id
);
449 -- The inlining of a completing expression function does
450 -- not need to load anything extra when both the spec and
451 -- body are in the same context.
454 Was_Expression_Function
(Body_Decl
)
455 and then Parent
(Spec_Decl
) = Parent
(Body_Decl
);
461 end Is_Non_Loading_Expression_Function
;
467 function Must_Inline
return Inline_Level_Type
is
472 -- Check if call is in main unit
474 Scop
:= Current_Scope
;
476 -- Do not try to inline if scope is standard. This could happen, for
477 -- example, for a call to Add_Global_Declaration, and it causes
478 -- trouble to try to inline at this level.
480 if Scop
= Standard_Standard
then
484 -- Otherwise lookup scope stack to outer scope
486 while Scope
(Scop
) /= Standard_Standard
487 and then not Is_Child_Unit
(Scop
)
489 Scop
:= Scope
(Scop
);
492 Comp
:= Parent
(Scop
);
493 while Nkind
(Comp
) /= N_Compilation_Unit
loop
494 Comp
:= Parent
(Comp
);
497 -- If the call is in the main unit, inline the call and compile the
498 -- package of the subprogram to find more calls to be inlined.
500 if Comp
= Cunit
(Main_Unit
)
501 or else Comp
= Library_Unit
(Cunit
(Main_Unit
))
504 return Inline_Package
;
507 -- The call is not in the main unit. See if it is in some subprogram
508 -- that can be inlined outside its unit. If so, inline the call and,
509 -- if the inlining level is set to 1, stop there; otherwise also
510 -- compile the package as above.
512 Scop
:= Current_Scope
;
513 while Scope
(Scop
) /= Standard_Standard
514 and then not Is_Child_Unit
(Scop
)
516 if Is_Overloadable
(Scop
)
517 and then Is_Inlined
(Scop
)
518 and then not Is_Nested
(Scop
)
522 if Inline_Level
= 1 then
525 return Inline_Package
;
529 Scop
:= Scope
(Scop
);
537 Level
: Inline_Level_Type
;
539 -- Start of processing for Add_Inlined_Body
542 Append_New_Elmt
(N
, To
=> Backend_Calls
);
544 -- Skip subprograms that cannot or need not be inlined outside their
545 -- unit or parent subprogram.
547 if Is_Abstract_Subprogram
(E
)
548 or else Convention
(E
) = Convention_Protected
549 or else In_Main_Unit_Or_Subunit
(E
)
550 or else Is_Nested
(E
)
555 -- Find out whether the call must be inlined. Unless the result is
556 -- Dont_Inline, Must_Inline also creates an edge for the call in the
557 -- callgraph; however, it will not be activated until after Is_Called
558 -- is set on the subprogram.
560 Level
:= Must_Inline
;
562 if Level
= Dont_Inline
then
566 -- If a previous call to the subprogram has been inlined, nothing to do
568 if Is_Called
(E
) then
572 -- If the subprogram is an instance, then inline the instance
574 if Is_Generic_Instance
(E
) then
575 Add_Inlined_Instance
(E
);
578 -- Mark the subprogram as called
582 -- If the call was generated by the compiler and is to a subprogram in
583 -- a run-time unit, we need to suppress debugging information for it,
584 -- so that the code that is eventually inlined will not affect the
585 -- debugging of the program. We do not do it if the call comes from
586 -- source because, even if the call is inlined, the user may expect it
587 -- to be present in the debugging information.
589 if not Comes_From_Source
(N
)
590 and then In_Extended_Main_Source_Unit
(N
)
591 and then Is_Predefined_Unit
(Get_Source_Unit
(E
))
593 Set_Needs_Debug_Info
(E
, False);
596 -- If the subprogram is an expression function, or is completed by one
597 -- where both the spec and body are in the same context, then there is
598 -- no need to load any package body since the body of the function is
601 if Is_Non_Loading_Expression_Function
(E
) then
605 -- Find unit containing E, and add to list of inlined bodies if needed.
606 -- Library-level functions must be handled specially, because there is
607 -- no enclosing package to retrieve. In this case, it is the body of
608 -- the function that will have to be loaded.
611 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
615 Inlined_Bodies
.Increment_Last
;
616 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := E
;
619 pragma Assert
(Ekind
(Pack
) = E_Package
);
621 -- If the subprogram is within an instance, inline the instance
623 if Comes_From_Source
(E
) then
626 while Present
(Inst
) and then Inst
/= Standard_Standard
loop
627 exit when Is_Generic_Instance
(Inst
);
628 Inst
:= Scope
(Inst
);
632 and then Is_Generic_Instance
(Inst
)
633 and then not Is_Called
(Inst
)
635 Inst_Decl
:= Unit_Declaration_Node
(Inst
);
637 -- Do not inline the instance if the body already exists,
638 -- or the instance node is simply missing.
640 if Present
(Corresponding_Body
(Inst_Decl
))
641 or else (Nkind
(Parent
(Inst_Decl
)) /= N_Compilation_Unit
642 and then No
(Next
(Inst_Decl
)))
644 Set_Is_Called
(Inst
);
646 Add_Inlined_Instance
(Inst
);
651 -- If the unit containing E is an instance, nothing more to do
653 if Is_Generic_Instance
(Pack
) then
656 -- Do not inline the package if the subprogram is an init proc
657 -- or other internally generated subprogram, because in that
658 -- case the subprogram body appears in the same unit that
659 -- declares the type, and that body is visible to the back end.
660 -- Do not inline it either if it is in the main unit.
661 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
662 -- calls if the back end takes care of inlining the call.
663 -- Note that Level is in Inline_Call | Inline_Package here.
665 elsif ((Level
= Inline_Call
666 and then Has_Pragma_Inline_Always
(E
)
667 and then Back_End_Inlining
)
668 or else Level
= Inline_Package
)
669 and then not Is_Inlined
(Pack
)
670 and then not Is_Internal
(E
)
671 and then not In_Main_Unit_Or_Subunit
(Pack
)
673 Set_Is_Inlined
(Pack
);
674 Inlined_Bodies
.Increment_Last
;
675 Inlined_Bodies
.Table
(Inlined_Bodies
.Last
) := Pack
;
679 -- Ensure that Analyze_Inlined_Bodies will be invoked after
680 -- completing the analysis of the current unit.
682 Inline_Processing_Required
:= True;
684 end Add_Inlined_Body
;
686 --------------------------
687 -- Add_Inlined_Instance --
688 --------------------------
690 procedure Add_Inlined_Instance
(E
: Entity_Id
) is
691 Decl_Node
: constant Node_Id
:= Unit_Declaration_Node
(E
);
695 -- This machinery is only used with back-end inlining
697 if not Back_End_Inlining
then
701 -- Register the instance in the list
703 Append_New_Elmt
(Decl_Node
, To
=> Backend_Instances
);
705 -- Retrieve the index of its corresponding pending instantiation
706 -- and mark this corresponding pending instantiation as needed.
708 Index
:= To_Pending_Instantiations
.Get
(Decl_Node
);
710 Called_Pending_Instantiations
.Append
(Index
);
712 pragma Assert
(False);
717 end Add_Inlined_Instance
;
719 ----------------------------
720 -- Add_Inlined_Subprogram --
721 ----------------------------
723 procedure Add_Inlined_Subprogram
(E
: Entity_Id
) is
724 Decl
: constant Node_Id
:= Parent
(Declaration_Node
(E
));
725 Pack
: constant Entity_Id
:= Get_Code_Unit_Entity
(E
);
727 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
);
728 -- Append Subp to the list of subprograms inlined by the backend
730 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
);
731 -- Append Subp to the list of subprograms that cannot be inlined by
734 -----------------------------------------
735 -- Register_Backend_Inlined_Subprogram --
736 -----------------------------------------
738 procedure Register_Backend_Inlined_Subprogram
(Subp
: Entity_Id
) is
740 Append_New_Elmt
(Subp
, To
=> Backend_Inlined_Subps
);
741 end Register_Backend_Inlined_Subprogram
;
743 ---------------------------------------------
744 -- Register_Backend_Not_Inlined_Subprogram --
745 ---------------------------------------------
747 procedure Register_Backend_Not_Inlined_Subprogram
(Subp
: Entity_Id
) is
749 Append_New_Elmt
(Subp
, To
=> Backend_Not_Inlined_Subps
);
750 end Register_Backend_Not_Inlined_Subprogram
;
752 -- Start of processing for Add_Inlined_Subprogram
755 -- We can inline the subprogram if its unit is known to be inlined or is
756 -- an instance whose body will be analyzed anyway or the subprogram was
757 -- generated as a body by the compiler (for example an initialization
758 -- procedure) or its declaration was provided along with the body (for
759 -- example an expression function) and it does not declare types with
760 -- nontrivial initialization procedures.
762 if (Is_Inlined
(Pack
)
763 or else Is_Generic_Instance
(Pack
)
764 or else Nkind
(Decl
) = N_Subprogram_Body
765 or else Present
(Corresponding_Body
(Decl
)))
766 and then not Has_Initialized_Type
(E
)
768 Register_Backend_Inlined_Subprogram
(E
);
770 if No
(Last_Inlined
) then
771 Set_First_Inlined_Subprogram
(Cunit
(Main_Unit
), E
);
773 Set_Next_Inlined_Subprogram
(Last_Inlined
, E
);
779 Register_Backend_Not_Inlined_Subprogram
(E
);
781 end Add_Inlined_Subprogram
;
783 --------------------------------
784 -- Add_Pending_Instantiation --
785 --------------------------------
787 procedure Add_Pending_Instantiation
790 Fin_Scop
: Node_Id
:= Empty
)
792 Act_Decl_Id
: Entity_Id
;
796 -- Here is a defense against a ludicrous number of instantiations
797 -- caused by a circular set of instantiation attempts.
799 if Pending_Instantiations
.Last
+ 1 >= Maximum_Instantiations
then
800 Error_Msg_Uint_1
:= UI_From_Int
(Maximum_Instantiations
);
801 Error_Msg_N
("too many instantiations, exceeds max of^", Inst
);
802 Error_Msg_N
("\limit can be changed using -gnateinn switch", Inst
);
803 raise Unrecoverable_Error
;
806 -- Capture the body of the generic instantiation along with its context
807 -- for later processing by Instantiate_Bodies.
809 Pending_Instantiations
.Append
811 Act_Decl
=> Act_Decl
,
812 Fin_Scop
=> Fin_Scop
,
813 Config_Switches
=> Save_Config_Switches
,
814 Current_Sem_Unit
=> Current_Sem_Unit
,
815 Expander_Status
=> Expander_Active
,
816 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
817 Scope_Suppress
=> Scope_Suppress
,
818 Warnings
=> Save_Warnings
));
820 -- With back-end inlining, also associate the index to the instantiation
822 if Back_End_Inlining
then
823 Act_Decl_Id
:= Defining_Entity
(Act_Decl
);
824 Index
:= Pending_Instantiations
.Last
;
826 To_Pending_Instantiations
.Set
(Act_Decl
, Index
);
828 -- If an instantiation is in the main unit or subunit, or is a nested
829 -- subprogram, then its body is needed as per the analysis done in
830 -- Analyze_Package_Instantiation & Analyze_Subprogram_Instantiation.
832 if In_Main_Unit_Or_Subunit
(Act_Decl_Id
)
833 or else (Is_Subprogram
(Act_Decl_Id
)
834 and then Is_Nested
(Act_Decl_Id
))
836 Called_Pending_Instantiations
.Append
(Index
);
838 Set_Is_Called
(Act_Decl_Id
);
841 end Add_Pending_Instantiation
;
843 ------------------------
844 -- Add_Scope_To_Clean --
845 ------------------------
847 procedure Add_Scope_To_Clean
(Scop
: Entity_Id
) is
851 Elmt
:= First_Elmt
(To_Clean
);
852 while Present
(Elmt
) loop
853 if Node
(Elmt
) = Scop
then
860 Append_Elmt
(Scop
, To_Clean
);
861 end Add_Scope_To_Clean
;
867 function Add_Subp
(E
: Entity_Id
) return Subp_Index
is
868 Index
: Subp_Index
:= Subp_Index
(E
) mod Num_Hash_Headers
;
872 -- Initialize entry in Inlined table
874 procedure New_Entry
is
876 Inlined
.Increment_Last
;
877 Inlined
.Table
(Inlined
.Last
).Name
:= E
;
878 Inlined
.Table
(Inlined
.Last
).Next
:= No_Subp
;
879 Inlined
.Table
(Inlined
.Last
).First_Succ
:= No_Succ
;
880 Inlined
.Table
(Inlined
.Last
).Main_Call
:= False;
881 Inlined
.Table
(Inlined
.Last
).Processed
:= False;
884 -- Start of processing for Add_Subp
887 if Hash_Headers
(Index
) = No_Subp
then
889 Hash_Headers
(Index
) := Inlined
.Last
;
893 J
:= Hash_Headers
(Index
);
894 while J
/= No_Subp
loop
895 if Inlined
.Table
(J
).Name
= E
then
899 J
:= Inlined
.Table
(J
).Next
;
903 -- On exit, subprogram was not found. Enter in table. Index is
904 -- the current last entry on the hash chain.
907 Inlined
.Table
(Index
).Next
:= Inlined
.Last
;
912 ----------------------------
913 -- Analyze_Inlined_Bodies --
914 ----------------------------
916 procedure Analyze_Inlined_Bodies
is
923 type Pending_Index
is new Nat
;
925 package Pending_Inlined
is new Table
.Table
(
926 Table_Component_Type
=> Subp_Index
,
927 Table_Index_Type
=> Pending_Index
,
928 Table_Low_Bound
=> 1,
929 Table_Initial
=> Alloc
.Inlined_Initial
,
930 Table_Increment
=> Alloc
.Inlined_Increment
,
931 Table_Name
=> "Pending_Inlined");
932 -- The workpile used to compute the transitive closure
934 -- Start of processing for Analyze_Inlined_Bodies
937 if Serious_Errors_Detected
= 0 then
938 Push_Scope
(Standard_Standard
);
941 while J
<= Inlined_Bodies
.Last
942 and then Serious_Errors_Detected
= 0
944 Pack
:= Inlined_Bodies
.Table
(J
);
946 and then Scope
(Pack
) /= Standard_Standard
947 and then not Is_Child_Unit
(Pack
)
949 Pack
:= Scope
(Pack
);
952 Comp_Unit
:= Parent
(Pack
);
953 while Present
(Comp_Unit
)
954 and then Nkind
(Comp_Unit
) /= N_Compilation_Unit
956 Comp_Unit
:= Parent
(Comp_Unit
);
959 -- Load the body if it exists and contains inlineable entities,
960 -- unless it is the main unit, or is an instance whose body has
961 -- already been analyzed.
963 if Present
(Comp_Unit
)
964 and then Comp_Unit
/= Cunit
(Main_Unit
)
965 and then Body_Required
(Comp_Unit
)
967 (Nkind
(Unit
(Comp_Unit
)) /= N_Package_Declaration
969 (No
(Corresponding_Body
(Unit
(Comp_Unit
)))
970 and then Body_Needed_For_Inlining
971 (Defining_Entity
(Unit
(Comp_Unit
)))))
974 Bname
: constant Unit_Name_Type
:=
975 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
980 if not Is_Loaded
(Bname
) then
981 Style_Check
:= False;
982 Load_Needed_Body
(Comp_Unit
, OK
);
986 -- Warn that a body was not available for inlining
989 Error_Msg_Unit_1
:= Bname
;
991 ("one or more inlined subprograms accessed in $!??",
994 Get_File_Name
(Bname
, Subunit
=> False);
995 Error_Msg_N
("\but file{ was not found!??", Comp_Unit
);
1003 if J
> Inlined_Bodies
.Last
then
1005 -- The analysis of required bodies may have produced additional
1006 -- generic instantiations. To obtain further inlining, we need
1007 -- to perform another round of generic body instantiations.
1011 -- Symmetrically, the instantiation of required generic bodies
1012 -- may have caused additional bodies to be inlined. To obtain
1013 -- further inlining, we keep looping over the inlined bodies.
1017 -- The list of inlined subprograms is an overestimate, because it
1018 -- includes inlined functions called from functions that are compiled
1019 -- as part of an inlined package, but are not themselves called. An
1020 -- accurate computation of just those subprograms that are needed
1021 -- requires that we perform a transitive closure over the call graph,
1022 -- starting from calls in the main compilation unit.
1024 for Index
in Inlined
.First
.. Inlined
.Last
loop
1025 if not Is_Called
(Inlined
.Table
(Index
).Name
) then
1027 -- This means that Add_Inlined_Body added the subprogram to the
1028 -- table but wasn't able to handle its code unit. Do nothing.
1030 Inlined
.Table
(Index
).Processed
:= True;
1032 elsif Inlined
.Table
(Index
).Main_Call
then
1033 Pending_Inlined
.Increment_Last
;
1034 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Index
;
1035 Inlined
.Table
(Index
).Processed
:= True;
1038 Set_Is_Called
(Inlined
.Table
(Index
).Name
, False);
1042 -- Iterate over the workpile until it is emptied, propagating the
1043 -- Is_Called flag to the successors of the processed subprogram.
1045 while Pending_Inlined
.Last
>= Pending_Inlined
.First
loop
1046 Subp
:= Pending_Inlined
.Table
(Pending_Inlined
.Last
);
1047 Pending_Inlined
.Decrement_Last
;
1049 S
:= Inlined
.Table
(Subp
).First_Succ
;
1051 while S
/= No_Succ
loop
1052 Subp
:= Successors
.Table
(S
).Subp
;
1054 if not Inlined
.Table
(Subp
).Processed
then
1055 Set_Is_Called
(Inlined
.Table
(Subp
).Name
);
1056 Pending_Inlined
.Increment_Last
;
1057 Pending_Inlined
.Table
(Pending_Inlined
.Last
) := Subp
;
1058 Inlined
.Table
(Subp
).Processed
:= True;
1061 S
:= Successors
.Table
(S
).Next
;
1065 -- Finally add the called subprograms to the list of inlined
1066 -- subprograms for the unit.
1068 for Index
in Inlined
.First
.. Inlined
.Last
loop
1070 E
: constant Subprogram_Kind_Id
:= Inlined
.Table
(Index
).Name
;
1073 if Is_Called
(E
) and then not Is_Ignored_Ghost_Entity
(E
) then
1074 Add_Inlined_Subprogram
(E
);
1081 end Analyze_Inlined_Bodies
;
1083 --------------------------
1084 -- Build_Body_To_Inline --
1085 --------------------------
1087 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
1088 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
1089 Original_Body
: Node_Id
;
1090 Body_To_Analyze
: Node_Id
;
1091 Max_Size
: constant := 10;
1093 function Has_Extended_Return
return Boolean;
1094 -- This function returns True if the subprogram has an extended return
1097 function Has_Pending_Instantiation
return Boolean;
1098 -- If some enclosing body contains instantiations that appear before
1099 -- the corresponding generic body, the enclosing body has a freeze node
1100 -- so that it can be elaborated after the generic itself. This might
1101 -- conflict with subsequent inlinings, so that it is unsafe to try to
1102 -- inline in such a case.
1104 function Has_Single_Return_In_GNATprove_Mode
return Boolean;
1105 -- This function is called only in GNATprove mode, and it returns
1106 -- True if the subprogram has no return statement or a single return
1107 -- statement as last statement. It returns False for subprogram with
1108 -- a single return as last statement inside one or more blocks, as
1109 -- inlining would generate gotos in that case as well (although the
1110 -- goto is useless in that case).
1112 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
1113 -- If the body of the subprogram includes a call that returns an
1114 -- unconstrained type, the secondary stack is involved, and it is
1115 -- not worth inlining.
1117 -------------------------
1118 -- Has_Extended_Return --
1119 -------------------------
1121 function Has_Extended_Return
return Boolean is
1122 Body_To_Inline
: constant Node_Id
:= N
;
1124 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
1125 -- Returns OK on node N if this is not an extended return statement
1131 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
1134 when N_Extended_Return_Statement
=>
1137 -- Skip locally declared subprogram bodies inside the body to
1138 -- inline, as the return statements inside those do not count.
1140 when N_Subprogram_Body
=>
1141 if N
= Body_To_Inline
then
1152 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
1154 -- Start of processing for Has_Extended_Return
1157 return Check_All_Returns
(N
) /= OK
;
1158 end Has_Extended_Return
;
1160 -------------------------------
1161 -- Has_Pending_Instantiation --
1162 -------------------------------
1164 function Has_Pending_Instantiation
return Boolean is
1169 while Present
(S
) loop
1170 if Is_Compilation_Unit
(S
)
1171 or else Is_Child_Unit
(S
)
1175 elsif Ekind
(S
) = E_Package
1176 and then Has_Forward_Instantiation
(S
)
1185 end Has_Pending_Instantiation
;
1187 -----------------------------------------
1188 -- Has_Single_Return_In_GNATprove_Mode --
1189 -----------------------------------------
1191 function Has_Single_Return_In_GNATprove_Mode
return Boolean is
1192 Body_To_Inline
: constant Node_Id
:= N
;
1193 Last_Statement
: Node_Id
:= Empty
;
1195 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
1196 -- Returns OK on node N if this is not a return statement different
1197 -- from the last statement in the subprogram.
1203 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
1206 when N_Extended_Return_Statement
1207 | N_Simple_Return_Statement
1209 if N
= Last_Statement
then
1215 -- Skip locally declared subprogram bodies inside the body to
1216 -- inline, as the return statements inside those do not count.
1218 when N_Subprogram_Body
=>
1219 if N
= Body_To_Inline
then
1230 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
1232 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1235 -- Retrieve the last statement
1237 Last_Statement
:= Last
(Statements
(Handled_Statement_Sequence
(N
)));
1239 -- Check that the last statement is the only possible return
1240 -- statement in the subprogram.
1242 return Check_All_Returns
(N
) = OK
;
1243 end Has_Single_Return_In_GNATprove_Mode
;
1245 --------------------------
1246 -- Uses_Secondary_Stack --
1247 --------------------------
1249 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
1250 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
1251 -- Look for function calls that return an unconstrained type
1257 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
1259 if Nkind
(N
) = N_Function_Call
1260 and then Is_Entity_Name
(Name
(N
))
1261 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
1262 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
1265 ("cannot inline & (call returns unconstrained type)?",
1273 function Check_Calls
is new Traverse_Func
(Check_Call
);
1276 return Check_Calls
(Bod
) = Abandon
;
1277 end Uses_Secondary_Stack
;
1279 -- Start of processing for Build_Body_To_Inline
1282 -- Return immediately if done already
1284 if Nkind
(Decl
) = N_Subprogram_Declaration
1285 and then Present
(Body_To_Inline
(Decl
))
1289 -- Subprograms that have return statements in the middle of the body are
1290 -- inlined with gotos. GNATprove does not currently support gotos, so
1291 -- we prevent such inlining.
1293 elsif GNATprove_Mode
1294 and then not Has_Single_Return_In_GNATprove_Mode
1296 Cannot_Inline
("cannot inline & (multiple returns)?", N
, Spec_Id
);
1299 -- Functions that return controlled types cannot currently be inlined
1300 -- because they require secondary stack handling; controlled actions
1301 -- may also interfere in complex ways with inlining.
1303 elsif Ekind
(Spec_Id
) = E_Function
1304 and then Needs_Finalization
(Etype
(Spec_Id
))
1307 ("cannot inline & (controlled return type)?", N
, Spec_Id
);
1311 if Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
)) then
1315 if Present
(Handled_Statement_Sequence
(N
)) then
1316 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
1318 ("cannot inline& (exception handler)?",
1319 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
1323 elsif Has_Excluded_Statement
1324 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
1330 -- We do not inline a subprogram that is too large, unless it is marked
1331 -- Inline_Always or we are in GNATprove mode. This pragma does not
1332 -- suppress the other checks on inlining (forbidden declarations,
1335 if not (Has_Pragma_Inline_Always
(Spec_Id
) or else GNATprove_Mode
)
1336 and then List_Length
1337 (Statements
(Handled_Statement_Sequence
(N
))) > Max_Size
1339 Cannot_Inline
("cannot inline& (body too large)?", N
, Spec_Id
);
1343 if Has_Pending_Instantiation
then
1345 ("cannot inline& (forward instance within enclosing body)?",
1350 -- Within an instance, the body to inline must be treated as a nested
1351 -- generic, so that the proper global references are preserved.
1353 -- Note that we do not do this at the library level, because it is not
1354 -- needed, and furthermore this causes trouble if front-end inlining
1355 -- is activated (-gnatN).
1357 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1358 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
1359 Original_Body
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> True);
1361 Original_Body
:= Copy_Separate_Tree
(N
);
1364 -- We need to capture references to the formals in order to substitute
1365 -- the actuals at the point of inlining, i.e. instantiation. To treat
1366 -- the formals as globals to the body to inline, we nest it within a
1367 -- dummy parameterless subprogram, declared within the real one. To
1368 -- avoid generating an internal name (which is never public, and which
1369 -- affects serial numbers of other generated names), we use an internal
1370 -- symbol that cannot conflict with user declarations.
1372 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
1373 Set_Defining_Unit_Name
1374 (Specification
(Original_Body
),
1375 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
1376 Set_Corresponding_Spec
(Original_Body
, Empty
);
1378 -- Remove all aspects/pragmas that have no meaning in an inlined body
1380 Remove_Aspects_And_Pragmas
(Original_Body
);
1383 Copy_Generic_Node
(Original_Body
, Empty
, Instantiating
=> False);
1385 -- Set return type of function, which is also global and does not need
1388 if Ekind
(Spec_Id
) = E_Function
then
1389 Set_Result_Definition
1390 (Specification
(Body_To_Analyze
),
1391 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
1394 if No
(Declarations
(N
)) then
1395 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
1397 Append
(Body_To_Analyze
, Declarations
(N
));
1402 Analyze
(Body_To_Analyze
);
1403 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
1404 Save_Global_References
(Original_Body
);
1406 Remove
(Body_To_Analyze
);
1410 -- Restore environment if previously saved
1412 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
1416 -- Functions that return unconstrained composite types require
1417 -- secondary stack handling, and cannot currently be inlined, unless
1418 -- all return statements return a local variable that is the first
1419 -- local declaration in the body. We had to delay this check until
1420 -- the body of the function is analyzed since Has_Single_Return()
1421 -- requires a minimum decoration.
1423 if Ekind
(Spec_Id
) = E_Function
1424 and then not Is_Scalar_Type
(Etype
(Spec_Id
))
1425 and then not Is_Access_Type
(Etype
(Spec_Id
))
1426 and then not Is_Constrained
(Etype
(Spec_Id
))
1428 if not Has_Single_Return
(Body_To_Analyze
)
1430 -- Skip inlining if the function returns an unconstrained type
1431 -- using an extended return statement, since this part of the
1432 -- new inlining model is not yet supported by the current
1435 or else (Returns_Unconstrained_Type
(Spec_Id
)
1436 and then Has_Extended_Return
)
1439 ("cannot inline & (unconstrained return type)?", N
, Spec_Id
);
1443 -- If secondary stack is used, there is no point in inlining. We have
1444 -- already issued the warning in this case, so nothing to do.
1446 elsif Uses_Secondary_Stack
(Body_To_Analyze
) then
1450 Set_Body_To_Inline
(Decl
, Original_Body
);
1451 Mutate_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
1452 Set_Is_Inlined
(Spec_Id
);
1453 end Build_Body_To_Inline
;
1455 -------------------------------------------
1456 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1457 -------------------------------------------
1459 function Call_Can_Be_Inlined_In_GNATprove_Mode
1461 Subp
: Entity_Id
) return Boolean
1467 F
:= First_Formal
(Subp
);
1468 A
:= First_Actual
(N
);
1469 while Present
(F
) loop
1470 if Ekind
(F
) /= E_Out_Parameter
1471 and then not Same_Type
(Etype
(F
), Etype
(A
))
1473 (Is_By_Reference_Type
(Etype
(A
))
1474 or else Is_Limited_Type
(Etype
(A
)))
1484 end Call_Can_Be_Inlined_In_GNATprove_Mode
;
1486 --------------------------------------
1487 -- Can_Be_Inlined_In_GNATprove_Mode --
1488 --------------------------------------
1490 function Can_Be_Inlined_In_GNATprove_Mode
1491 (Spec_Id
: Entity_Id
;
1492 Body_Id
: Entity_Id
) return Boolean
1494 function Has_Formal_Or_Result_Of_Deep_Type
1495 (Id
: Entity_Id
) return Boolean;
1496 -- Returns true if the subprogram has at least one formal parameter or
1497 -- a return type of a deep type: either an access type or a composite
1498 -- type containing an access type.
1500 function Has_Formal_With_Discriminant_Dependent_Fields
1501 (Id
: Entity_Id
) return Boolean;
1502 -- Returns true if the subprogram has at least one formal parameter of
1503 -- an unconstrained record type with per-object constraints on component
1506 function Has_Some_Contract
(Id
: Entity_Id
) return Boolean;
1507 -- Return True if subprogram Id has any contract. The presence of
1508 -- Extensions_Visible or Volatile_Function is also considered as a
1511 function Is_Unit_Subprogram
(Id
: Entity_Id
) return Boolean;
1512 -- Return True if subprogram Id defines a compilation unit
1514 function In_Package_Spec
(Id
: Entity_Id
) return Boolean;
1515 -- Return True if subprogram Id is defined in the package specification,
1516 -- either its visible or private part.
1518 function Maybe_Traversal_Function
(Id
: Entity_Id
) return Boolean;
1519 -- Return True if subprogram Id could be a traversal function, as
1520 -- defined in SPARK RM 3.10. This is only a safe approximation, as the
1521 -- knowledge of the SPARK boundary is needed to determine exactly
1522 -- traversal functions.
1524 ---------------------------------------
1525 -- Has_Formal_Or_Result_Of_Deep_Type --
1526 ---------------------------------------
1528 function Has_Formal_Or_Result_Of_Deep_Type
1529 (Id
: Entity_Id
) return Boolean
1531 function Is_Deep
(Typ
: Entity_Id
) return Boolean;
1532 -- Return True if Typ is deep: either an access type or a composite
1533 -- type containing an access type.
1539 function Is_Deep
(Typ
: Entity_Id
) return Boolean is
1541 case Type_Kind
'(Ekind (Typ)) is
1548 return Is_Deep (Component_Type (Typ));
1552 Comp : Entity_Id := First_Component_Or_Discriminant (Typ);
1554 while Present (Comp) loop
1555 if Is_Deep (Etype (Comp)) then
1558 Next_Component_Or_Discriminant (Comp);
1564 | E_String_Literal_Subtype
1574 | E_Limited_Private_Type
1575 | E_Limited_Private_Subtype
1577 -- Conservatively consider that the type might be deep if
1578 -- its completion has not been seen yet.
1580 if No (Underlying_Type (Typ)) then
1583 -- Do not peek under a private type if its completion has
1584 -- SPARK_Mode Off. In such a case, a deep type is considered
1585 -- by GNATprove to be not deep.
1587 elsif Present (Full_View (Typ))
1588 and then Present (SPARK_Pragma (Full_View (Typ)))
1589 and then Get_SPARK_Mode_From_Annotation
1590 (SPARK_Pragma (Full_View (Typ))) = Off
1594 -- Otherwise peek under the private type.
1597 return Is_Deep (Underlying_Type (Typ));
1604 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1606 Formal_Typ : Entity_Id;
1608 -- Start of processing for Has_Formal_Or_Result_Of_Deep_Type
1611 -- Inspect all parameters of the subprogram looking for a formal
1614 Formal := First_Formal (Subp_Id);
1615 while Present (Formal) loop
1616 Formal_Typ := Etype (Formal);
1618 if Is_Deep (Formal_Typ) then
1622 Next_Formal (Formal);
1625 -- Check whether this is a function whose return type is deep
1627 if Ekind (Subp_Id) = E_Function
1628 and then Is_Deep (Etype (Subp_Id))
1634 end Has_Formal_Or_Result_Of_Deep_Type;
1636 ---------------------------------------------------
1637 -- Has_Formal_With_Discriminant_Dependent_Fields --
1638 ---------------------------------------------------
1640 function Has_Formal_With_Discriminant_Dependent_Fields
1641 (Id : Entity_Id) return Boolean
1643 function Has_Discriminant_Dependent_Component
1644 (Typ : Entity_Id) return Boolean;
1645 -- Determine whether unconstrained record type Typ has at least one
1646 -- component that depends on a discriminant.
1648 ------------------------------------------
1649 -- Has_Discriminant_Dependent_Component --
1650 ------------------------------------------
1652 function Has_Discriminant_Dependent_Component
1653 (Typ : Entity_Id) return Boolean
1658 -- Inspect all components of the record type looking for one that
1659 -- depends on a discriminant.
1661 Comp := First_Component (Typ);
1662 while Present (Comp) loop
1663 if Has_Discriminant_Dependent_Constraint (Comp) then
1667 Next_Component (Comp);
1671 end Has_Discriminant_Dependent_Component;
1675 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1677 Formal_Typ : Entity_Id;
1679 -- Start of processing for
1680 -- Has_Formal_With_Discriminant_Dependent_Fields
1683 -- Inspect all parameters of the subprogram looking for a formal
1684 -- of an unconstrained record type with at least one discriminant
1685 -- dependent component.
1687 Formal := First_Formal (Subp_Id);
1688 while Present (Formal) loop
1689 Formal_Typ := Etype (Formal);
1691 if Is_Record_Type (Formal_Typ)
1692 and then not Is_Constrained (Formal_Typ)
1693 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1698 Next_Formal (Formal);
1702 end Has_Formal_With_Discriminant_Dependent_Fields;
1704 -----------------------
1705 -- Has_Some_Contract --
1706 -----------------------
1708 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1712 -- A call to an expression function may precede the actual body which
1713 -- is inserted at the end of the enclosing declarations. Ensure that
1714 -- the related entity is decorated before inspecting the contract.
1716 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1717 Items := Contract (Id);
1719 -- Note that Classifications is not Empty when Extensions_Visible
1720 -- or Volatile_Function is present, which causes such subprograms
1721 -- to be considered to have a contract here. This is fine as we
1722 -- want to avoid inlining these too.
1724 return Present (Items)
1725 and then (Present (Pre_Post_Conditions (Items)) or else
1726 Present (Contract_Test_Cases (Items)) or else
1727 Present (Classifications (Items)));
1731 end Has_Some_Contract;
1733 ---------------------
1734 -- In_Package_Spec --
1735 ---------------------
1737 function In_Package_Spec (Id : Entity_Id) return Boolean is
1738 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1739 -- Parent of the subprogram's declaration
1742 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1743 end In_Package_Spec;
1745 ------------------------
1746 -- Is_Unit_Subprogram --
1747 ------------------------
1749 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1750 Decl : Node_Id := Parent (Parent (Id));
1752 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1753 Decl := Parent (Decl);
1756 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1757 end Is_Unit_Subprogram;
1759 ------------------------------
1760 -- Maybe_Traversal_Function --
1761 ------------------------------
1763 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean is
1765 return Ekind (Id) = E_Function
1767 -- Only traversal functions return an anonymous access-to-object
1770 and then Is_Anonymous_Access_Type (Etype (Id));
1771 end Maybe_Traversal_Function;
1773 -- Local declarations
1776 -- Procedure or function entity for the subprogram
1778 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1781 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1783 if Present (Spec_Id) then
1789 -- Only local subprograms without contracts are inlined in GNATprove
1790 -- mode, as these are the subprograms which a user is not interested in
1791 -- analyzing in isolation, but rather in the context of their call. This
1792 -- is a convenient convention, that could be changed for an explicit
1793 -- pragma/aspect one day.
1795 -- In a number of special cases, inlining is not desirable or not
1796 -- possible, see below.
1798 -- Do not inline unit-level subprograms
1800 if Is_Unit_Subprogram (Id) then
1803 -- Do not inline subprograms declared in package specs, because they are
1804 -- not local, i.e. can be called either from anywhere (if declared in
1805 -- visible part) or from the child units (if declared in private part).
1807 elsif In_Package_Spec (Id) then
1810 -- Do not inline subprograms declared in other units. This is important
1811 -- in particular for subprograms defined in the private part of a
1812 -- package spec, when analyzing one of its child packages, as otherwise
1813 -- we issue spurious messages about the impossibility to inline such
1816 elsif not In_Extended_Main_Code_Unit (Id) then
1819 -- Do not inline dispatching operations, as only their static calls
1820 -- can be analyzed in context, and not their dispatching calls.
1822 elsif Is_Dispatching_Operation (Id) then
1825 -- Do not inline subprograms marked No_Return, possibly used for
1826 -- signaling errors, which GNATprove handles specially.
1828 elsif No_Return (Id) then
1831 -- Do not inline subprograms that have a contract on the spec or the
1832 -- body. Use the contract(s) instead in GNATprove. This also prevents
1833 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1835 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1837 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1841 -- Do not inline expression functions, which are directly inlined at the
1844 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1846 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1850 -- Do not inline generic subprogram instances. The visibility rules of
1851 -- generic instances plays badly with inlining.
1853 elsif Is_Generic_Instance (Spec_Id) then
1856 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1857 -- the subprogram body, a similar check is performed after the body
1858 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1860 elsif Present (Spec_Id)
1862 (No (SPARK_Pragma (Spec_Id))
1864 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1868 -- Do not inline subprograms and entries defined inside protected types,
1869 -- which typically are not helper subprograms, which also avoids getting
1870 -- spurious messages on calls that cannot be inlined.
1872 elsif Within_Protected_Type (Id) then
1875 -- Do not inline predicate functions (treated specially by GNATprove)
1877 elsif Is_Predicate_Function (Id) then
1880 -- Do not inline subprograms with a parameter of an unconstrained
1881 -- record type if it has discrimiant dependent fields. Indeed, with
1882 -- such parameters, the frontend cannot always ensure type compliance
1883 -- in record component accesses (in particular with records containing
1886 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1889 -- Do not inline subprograms with a formal parameter or return type of
1890 -- a deep type, as in that case inlining might generate code that
1891 -- violates borrow-checking rules of SPARK 3.10 even if the original
1894 elsif Has_Formal_Or_Result_Of_Deep_Type (Id) then
1897 -- Do not inline subprograms which may be traversal functions. Such
1898 -- inlining introduces temporary variables of named access type for
1899 -- which assignments are move instead of borrow/observe, possibly
1900 -- leading to spurious errors when checking SPARK rules related to
1903 elsif Maybe_Traversal_Function (Id) then
1906 -- Otherwise, this is a subprogram declared inside the private part of a
1907 -- package, or inside a package body, or locally in a subprogram, and it
1908 -- does not have any contract. Inline it.
1913 end Can_Be_Inlined_In_GNATprove_Mode;
1919 procedure Cannot_Inline
1923 Is_Serious : Boolean := False;
1924 Suppress_Info : Boolean := False)
1927 -- In GNATprove mode, inlining is the technical means by which the
1928 -- higher-level goal of contextual analysis is reached, so issue
1929 -- messages about failure to apply contextual analysis to a
1930 -- subprogram, rather than failure to inline it.
1933 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1936 Len1 : constant Positive :=
1937 String (String'("cannot inline"))'Length;
1938 Len2
: constant Positive :=
1939 String (String'("info: no contextual analysis of"))'Length;
1941 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1944 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1945 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1946 Msg (Msg'First + Len1 .. Msg'Last);
1947 Cannot_Inline (New_Msg, N, Subp, Is_Serious, Suppress_Info);
1952 pragma Assert (Msg (Msg'Last) = '?
');
1954 -- Legacy front-end inlining model
1956 if not Back_End_Inlining then
1958 -- Do not emit warning if this is a predefined unit which is not
1959 -- the main unit. With validity checks enabled, some predefined
1960 -- subprograms may contain nested subprograms and become ineligible
1963 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1964 and then not In_Extended_Main_Source_Unit (Subp)
1968 -- In GNATprove mode, issue an info message when -gnatd_f is set and
1969 -- Suppress_Info is False, and indicate that the subprogram is not
1970 -- always inlined by setting flag Is_Inlined_Always to False.
1972 elsif GNATprove_Mode then
1973 Set_Is_Inlined_Always (Subp, False);
1975 if Debug_Flag_Underscore_F and not Suppress_Info then
1976 Error_Msg_NE (Msg, N, Subp);
1979 elsif Has_Pragma_Inline_Always (Subp) then
1981 -- Remove last character (question mark) to make this into an
1982 -- error, because the Inline_Always pragma cannot be obeyed.
1984 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1986 elsif Ineffective_Inline_Warnings then
1987 Error_Msg_NE (Msg & "p?", N, Subp);
1990 -- New semantics relying on back-end inlining
1992 elsif Is_Serious then
1994 -- Remove last character (question mark) to make this into an error.
1996 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1998 -- In GNATprove mode, issue an info message when -gnatd_f is set and
1999 -- Suppress_Info is False, and indicate that the subprogram is not
2000 -- always inlined by setting flag Is_Inlined_Always to False.
2002 elsif GNATprove_Mode then
2003 Set_Is_Inlined_Always (Subp, False);
2005 if Debug_Flag_Underscore_F and not Suppress_Info then
2006 Error_Msg_NE (Msg, N, Subp);
2011 -- Do not emit warning if this is a predefined unit which is not
2012 -- the main unit. This behavior is currently provided for backward
2013 -- compatibility but it will be removed when we enforce the
2014 -- strictness of the new rules.
2016 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2017 and then not In_Extended_Main_Source_Unit (Subp)
2021 elsif Has_Pragma_Inline_Always (Subp) then
2023 -- Emit a warning if this is a call to a runtime subprogram
2024 -- which is located inside a generic. Previously this call
2025 -- was silently skipped.
2027 if Is_Generic_Instance (Subp) then
2029 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
2031 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
2032 Set_Is_Inlined (Subp, False);
2033 Error_Msg_NE (Msg & "p?", N, Subp);
2039 -- Remove last character (question mark) to make this into an
2040 -- error, because the Inline_Always pragma cannot be obeyed.
2042 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2045 Set_Is_Inlined (Subp, False);
2047 if Ineffective_Inline_Warnings then
2048 Error_Msg_NE (Msg & "p?", N, Subp);
2054 --------------------------------------------
2055 -- Check_And_Split_Unconstrained_Function --
2056 --------------------------------------------
2058 procedure Check_And_Split_Unconstrained_Function
2060 Spec_Id : Entity_Id;
2061 Body_Id : Entity_Id)
2063 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
2064 -- Use generic machinery to build an unexpanded body for the subprogram.
2065 -- This body is subsequently used for inline expansions at call sites.
2067 procedure Build_Return_Object_Formal
2071 -- Create a formal parameter for return object declaration Obj_Decl of
2072 -- an extended return statement and add it to list Formals.
2074 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
2075 -- Return true if we generate code for the function body N, the function
2076 -- body N has no local declarations and its unique statement is a single
2077 -- extended return statement with a handled statements sequence.
2079 procedure Copy_Formals
2081 Subp_Id : Entity_Id;
2083 -- Create new formal parameters from the formal parameters of subprogram
2084 -- Subp_Id and add them to list Formals.
2086 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id;
2087 -- Create a copy of return object declaration Obj_Decl of an extended
2088 -- return statement.
2090 procedure Split_Unconstrained_Function
2092 Spec_Id : Entity_Id);
2093 -- N is an inlined function body that returns an unconstrained type and
2094 -- has a single extended return statement. Split N in two subprograms:
2095 -- a procedure P' and a
function F
'. The formals of P' duplicate the
2096 -- formals of N plus an extra formal which is used to return a value;
2097 -- its body is composed by the declarations and list of statements
2098 -- of the extended return statement of N.
2100 --------------------------
2101 -- Build_Body_To_Inline --
2102 --------------------------
2104 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
2105 procedure Generate_Subprogram_Body
2107 Body_To_Inline
: out Node_Id
);
2108 -- Generate a parameterless duplicate of subprogram body N. Note that
2109 -- occurrences of pragmas referencing the formals are removed since
2110 -- they have no meaning when the body is inlined and the formals are
2111 -- rewritten (the analysis of the non-inlined body will handle these
2112 -- pragmas). A new internal name is associated with Body_To_Inline.
2114 ------------------------------
2115 -- Generate_Subprogram_Body --
2116 ------------------------------
2118 procedure Generate_Subprogram_Body
2120 Body_To_Inline
: out Node_Id
)
2123 -- Within an instance, the body to inline must be treated as a
2124 -- nested generic so that proper global references are preserved.
2126 -- Note that we do not do this at the library level, because it
2127 -- is not needed, and furthermore this causes trouble if front
2128 -- end inlining is activated (-gnatN).
2131 and then Scope
(Current_Scope
) /= Standard_Standard
2134 Copy_Generic_Node
(N
, Empty
, Instantiating
=> True);
2136 Body_To_Inline
:= New_Copy_Tree
(N
);
2139 -- Remove aspects/pragmas that have no meaning in an inlined body
2141 Remove_Aspects_And_Pragmas
(Body_To_Inline
);
2143 -- We need to capture references to the formals in order
2144 -- to substitute the actuals at the point of inlining, i.e.
2145 -- instantiation. To treat the formals as globals to the body to
2146 -- inline, we nest it within a dummy parameterless subprogram,
2147 -- declared within the real one.
2149 Set_Parameter_Specifications
2150 (Specification
(Body_To_Inline
), No_List
);
2152 -- A new internal name is associated with Body_To_Inline to avoid
2153 -- conflicts when the non-inlined body N is analyzed.
2155 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
2156 Make_Temporary
(Sloc
(N
), 'P'));
2157 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
2158 end Generate_Subprogram_Body
;
2162 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
2163 Original_Body
: Node_Id
;
2164 Body_To_Analyze
: Node_Id
;
2166 -- Start of processing for Build_Body_To_Inline
2169 pragma Assert
(Current_Scope
= Spec_Id
);
2171 -- Within an instance, the body to inline must be treated as a nested
2172 -- generic, so that the proper global references are preserved. We
2173 -- do not do this at the library level, because it is not needed, and
2174 -- furthermore this causes trouble if front-end inlining is activated
2178 and then Scope
(Current_Scope
) /= Standard_Standard
2180 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
2183 -- Capture references to formals in order to substitute the actuals
2184 -- at the point of inlining or instantiation. To treat the formals
2185 -- as globals to the body to inline, nest the body within a dummy
2186 -- parameterless subprogram, declared within the real one.
2188 Generate_Subprogram_Body
(N
, Original_Body
);
2190 Copy_Generic_Node
(Original_Body
, Empty
, Instantiating
=> False);
2192 -- Set return type of function, which is also global and does not
2193 -- need to be resolved.
2195 if Ekind
(Spec_Id
) = E_Function
then
2196 Set_Result_Definition
(Specification
(Body_To_Analyze
),
2197 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
2200 if No
(Declarations
(N
)) then
2201 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
2203 Append_To
(Declarations
(N
), Body_To_Analyze
);
2206 Preanalyze
(Body_To_Analyze
);
2208 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
2209 Save_Global_References
(Original_Body
);
2211 Remove
(Body_To_Analyze
);
2213 -- Restore environment if previously saved
2216 and then Scope
(Current_Scope
) /= Standard_Standard
2221 pragma Assert
(No
(Body_To_Inline
(Decl
)));
2222 Set_Body_To_Inline
(Decl
, Original_Body
);
2223 Mutate_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
2224 end Build_Body_To_Inline
;
2226 --------------------------------
2227 -- Build_Return_Object_Formal --
2228 --------------------------------
2230 procedure Build_Return_Object_Formal
2235 Obj_Def
: constant Node_Id
:= Object_Definition
(Obj_Decl
);
2236 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
2240 -- Build the type definition of the formal parameter. The use of
2241 -- New_Copy_Tree ensures that global references preserved in the
2242 -- case of generics.
2244 if Is_Entity_Name
(Obj_Def
) then
2245 Typ_Def
:= New_Copy_Tree
(Obj_Def
);
2247 Typ_Def
:= New_Copy_Tree
(Subtype_Mark
(Obj_Def
));
2252 -- Obj_Id : [out] Typ_Def
2254 -- Mode OUT should not be used when the return object is declared as
2255 -- a constant. Check the definition of the object declaration because
2256 -- the object has not been analyzed yet.
2259 Make_Parameter_Specification
(Loc
,
2260 Defining_Identifier
=>
2261 Make_Defining_Identifier
(Loc
, Chars
(Obj_Id
)),
2262 In_Present
=> False,
2263 Out_Present
=> not Constant_Present
(Obj_Decl
),
2264 Null_Exclusion_Present
=> False,
2265 Parameter_Type
=> Typ_Def
));
2266 end Build_Return_Object_Formal
;
2268 --------------------------------------
2269 -- Can_Split_Unconstrained_Function --
2270 --------------------------------------
2272 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean is
2273 Stmt
: constant Node_Id
:=
2274 First
(Statements
(Handled_Statement_Sequence
(N
)));
2278 -- No user defined declarations allowed in the function except inside
2279 -- the unique return statement; implicit labels are the only allowed
2282 Decl
:= First
(Declarations
(N
));
2283 while Present
(Decl
) loop
2284 if Nkind
(Decl
) /= N_Implicit_Label_Declaration
then
2291 -- We only split the inlined function when we are generating the code
2292 -- of its body; otherwise we leave duplicated split subprograms in
2293 -- the tree which (if referenced) generate wrong references at link
2296 return In_Extended_Main_Code_Unit
(N
)
2297 and then Present
(Stmt
)
2298 and then Nkind
(Stmt
) = N_Extended_Return_Statement
2299 and then No
(Next
(Stmt
))
2300 and then Present
(Handled_Statement_Sequence
(Stmt
));
2301 end Can_Split_Unconstrained_Function
;
2307 procedure Copy_Formals
2309 Subp_Id
: Entity_Id
;
2316 Formal
:= First_Formal
(Subp_Id
);
2317 while Present
(Formal
) loop
2318 Spec
:= Parent
(Formal
);
2320 -- Create an exact copy of the formal parameter. The use of
2321 -- New_Copy_Tree ensures that global references are preserved
2322 -- in case of generics.
2325 Make_Parameter_Specification
(Loc
,
2326 Defining_Identifier
=>
2327 Make_Defining_Identifier
(Sloc
(Formal
), Chars
(Formal
)),
2328 In_Present
=> In_Present
(Spec
),
2329 Out_Present
=> Out_Present
(Spec
),
2330 Null_Exclusion_Present
=> Null_Exclusion_Present
(Spec
),
2332 New_Copy_Tree
(Parameter_Type
(Spec
)),
2333 Expression
=> New_Copy_Tree
(Expression
(Spec
))));
2335 Next_Formal
(Formal
);
2339 ------------------------
2340 -- Copy_Return_Object --
2341 ------------------------
2343 function Copy_Return_Object
(Obj_Decl
: Node_Id
) return Node_Id
is
2344 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
2347 -- The use of New_Copy_Tree ensures that global references are
2348 -- preserved in case of generics.
2351 Make_Object_Declaration
(Sloc
(Obj_Decl
),
2352 Defining_Identifier
=>
2353 Make_Defining_Identifier
(Sloc
(Obj_Id
), Chars
(Obj_Id
)),
2354 Aliased_Present
=> Aliased_Present
(Obj_Decl
),
2355 Constant_Present
=> Constant_Present
(Obj_Decl
),
2356 Null_Exclusion_Present
=> Null_Exclusion_Present
(Obj_Decl
),
2357 Object_Definition
=>
2358 New_Copy_Tree
(Object_Definition
(Obj_Decl
)),
2359 Expression
=> New_Copy_Tree
(Expression
(Obj_Decl
)));
2360 end Copy_Return_Object
;
2362 ----------------------------------
2363 -- Split_Unconstrained_Function --
2364 ----------------------------------
2366 procedure Split_Unconstrained_Function
2368 Spec_Id
: Entity_Id
)
2370 Loc
: constant Source_Ptr
:= Sloc
(N
);
2371 Ret_Stmt
: constant Node_Id
:=
2372 First
(Statements
(Handled_Statement_Sequence
(N
)));
2373 Ret_Obj
: constant Node_Id
:=
2374 First
(Return_Object_Declarations
(Ret_Stmt
));
2376 procedure Build_Procedure
2377 (Proc_Id
: out Entity_Id
;
2378 Decl_List
: out List_Id
);
2379 -- Build a procedure containing the statements found in the extended
2380 -- return statement of the unconstrained function body N.
2382 ---------------------
2383 -- Build_Procedure --
2384 ---------------------
2386 procedure Build_Procedure
2387 (Proc_Id
: out Entity_Id
;
2388 Decl_List
: out List_Id
)
2390 Formals
: constant List_Id
:= New_List
;
2391 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
2393 Body_Decls
: List_Id
:= No_List
;
2395 Proc_Body
: Node_Id
;
2396 Proc_Spec
: Node_Id
;
2399 -- Create formal parameters for the return object and all formals
2400 -- of the unconstrained function in order to pass their values to
2403 Build_Return_Object_Formal
2405 Obj_Decl
=> Ret_Obj
,
2406 Formals
=> Formals
);
2411 Formals
=> Formals
);
2413 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
2416 Make_Procedure_Specification
(Loc
,
2417 Defining_Unit_Name
=> Proc_Id
,
2418 Parameter_Specifications
=> Formals
);
2420 Decl_List
:= New_List
;
2422 Append_To
(Decl_List
,
2423 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
2425 -- Can_Convert_Unconstrained_Function checked that the function
2426 -- has no local declarations except implicit label declarations.
2427 -- Copy these declarations to the built procedure.
2429 if Present
(Declarations
(N
)) then
2430 Body_Decls
:= New_List
;
2432 Decl
:= First
(Declarations
(N
));
2433 while Present
(Decl
) loop
2434 pragma Assert
(Nkind
(Decl
) = N_Implicit_Label_Declaration
);
2436 Append_To
(Body_Decls
,
2437 Make_Implicit_Label_Declaration
(Loc
,
2438 Make_Defining_Identifier
(Loc
,
2439 Chars
=> Chars
(Defining_Identifier
(Decl
))),
2440 Label_Construct
=> Empty
));
2446 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Stmt
)));
2449 Make_Subprogram_Body
(Loc
,
2450 Specification
=> Copy_Subprogram_Spec
(Proc_Spec
),
2451 Declarations
=> Body_Decls
,
2452 Handled_Statement_Sequence
=>
2453 New_Copy_Tree
(Handled_Statement_Sequence
(Ret_Stmt
)));
2455 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
2456 Make_Defining_Identifier
(Loc
, Subp_Name
));
2458 Append_To
(Decl_List
, Proc_Body
);
2459 end Build_Procedure
;
2463 New_Obj
: constant Node_Id
:= Copy_Return_Object
(Ret_Obj
);
2465 Proc_Call
: Node_Id
;
2466 Proc_Id
: Entity_Id
;
2468 -- Start of processing for Split_Unconstrained_Function
2471 -- Build the associated procedure, analyze it and insert it before
2472 -- the function body N.
2475 Scope
: constant Entity_Id
:= Current_Scope
;
2476 Decl_List
: List_Id
;
2479 Build_Procedure
(Proc_Id
, Decl_List
);
2480 Insert_Actions
(N
, Decl_List
);
2481 Set_Is_Inlined
(Proc_Id
);
2485 -- Build the call to the generated procedure
2488 Actual_List
: constant List_Id
:= New_List
;
2492 Append_To
(Actual_List
,
2493 New_Occurrence_Of
(Defining_Identifier
(New_Obj
), Loc
));
2495 Formal
:= First_Formal
(Spec_Id
);
2496 while Present
(Formal
) loop
2497 Append_To
(Actual_List
, New_Occurrence_Of
(Formal
, Loc
));
2499 -- Avoid spurious warning on unreferenced formals
2501 Set_Referenced
(Formal
);
2502 Next_Formal
(Formal
);
2506 Make_Procedure_Call_Statement
(Loc
,
2507 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
2508 Parameter_Associations
=> Actual_List
);
2516 -- Proc (New_Obj, ...);
2521 Make_Block_Statement
(Loc
,
2522 Declarations
=> New_List
(New_Obj
),
2523 Handled_Statement_Sequence
=>
2524 Make_Handled_Sequence_Of_Statements
(Loc
,
2525 Statements
=> New_List
(
2529 Make_Simple_Return_Statement
(Loc
,
2532 (Defining_Identifier
(New_Obj
), Loc
)))));
2534 Rewrite
(Ret_Stmt
, Blk_Stmt
);
2535 end Split_Unconstrained_Function
;
2539 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
2541 -- Start of processing for Check_And_Split_Unconstrained_Function
2544 pragma Assert
(Back_End_Inlining
2545 and then Ekind
(Spec_Id
) = E_Function
2546 and then Returns_Unconstrained_Type
(Spec_Id
)
2547 and then Comes_From_Source
(Body_Id
)
2548 and then (Has_Pragma_Inline_Always
(Spec_Id
)
2549 or else Optimization_Level
> 0));
2551 -- This routine must not be used in GNATprove mode since GNATprove
2552 -- relies on frontend inlining
2554 pragma Assert
(not GNATprove_Mode
);
2556 -- No need to split the function if we cannot generate the code
2558 if Serious_Errors_Detected
/= 0 then
2562 -- No action needed in stubs since the attribute Body_To_Inline
2565 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
2568 -- Cannot build the body to inline if the attribute is already set.
2569 -- This attribute may have been set if this is a subprogram renaming
2570 -- declarations (see Freeze.Build_Renamed_Body).
2572 elsif Present
(Body_To_Inline
(Decl
)) then
2575 -- Do not generate a body to inline for protected functions, because the
2576 -- transformation generates a call to a protected procedure, causing
2577 -- spurious errors. We don't inline protected operations anyway, so
2578 -- this is no loss. We might as well ignore intrinsics and foreign
2579 -- conventions as well -- just allow Ada conventions.
2581 elsif not (Convention
(Spec_Id
) = Convention_Ada
2582 or else Convention
(Spec_Id
) = Convention_Ada_Pass_By_Copy
2583 or else Convention
(Spec_Id
) = Convention_Ada_Pass_By_Reference
)
2587 -- Check excluded declarations
2589 elsif Has_Excluded_Declaration
(Spec_Id
, Declarations
(N
)) then
2592 -- Check excluded statements. There is no need to protect us against
2593 -- exception handlers since they are supported by the GCC backend.
2595 elsif Present
(Handled_Statement_Sequence
(N
))
2596 and then Has_Excluded_Statement
2597 (Spec_Id
, Statements
(Handled_Statement_Sequence
(N
)))
2602 -- Build the body to inline only if really needed
2604 if Can_Split_Unconstrained_Function
(N
) then
2605 Split_Unconstrained_Function
(N
, Spec_Id
);
2606 Build_Body_To_Inline
(N
, Spec_Id
);
2607 Set_Is_Inlined
(Spec_Id
);
2609 end Check_And_Split_Unconstrained_Function
;
2611 ---------------------------------------------
2612 -- Check_Object_Renaming_In_GNATprove_Mode --
2613 ---------------------------------------------
2615 procedure Check_Object_Renaming_In_GNATprove_Mode
(Spec_Id
: Entity_Id
) is
2616 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
2617 Body_Decl
: constant Node_Id
:=
2618 Unit_Declaration_Node
(Corresponding_Body
(Decl
));
2620 function Check_Object_Renaming
(N
: Node_Id
) return Traverse_Result
;
2621 -- Returns Abandon on node N if this is a reference to an object
2622 -- renaming, which will be expanded into the renamed object in
2625 ---------------------------
2626 -- Check_Object_Renaming --
2627 ---------------------------
2629 function Check_Object_Renaming
(N
: Node_Id
) return Traverse_Result
is
2631 case Nkind
(Original_Node
(N
)) is
2632 when N_Expanded_Name
2636 Obj_Id
: constant Entity_Id
:= Entity
(Original_Node
(N
));
2638 -- Recognize the case when SPARK expansion rewrites a
2639 -- reference to an object renaming.
2642 and then Is_Object
(Obj_Id
)
2643 and then Present
(Renamed_Object
(Obj_Id
))
2644 and then Nkind
(Renamed_Object
(Obj_Id
)) not in N_Entity
2646 -- Copy_Generic_Node called for inlining expects the
2647 -- references to global entities to have the same kind
2648 -- in the "generic" code and its "instantiation".
2650 and then Nkind
(Original_Node
(N
)) /=
2651 Nkind
(Renamed_Object
(Obj_Id
))
2662 end Check_Object_Renaming
;
2664 function Check_All_Object_Renamings
is new
2665 Traverse_Func
(Check_Object_Renaming
);
2667 -- Start of processing for Check_Object_Renaming_In_GNATprove_Mode
2670 -- Subprograms with object renamings replaced by the special SPARK
2671 -- expansion cannot be inlined.
2673 if Check_All_Object_Renamings
(Body_Decl
) /= OK
then
2674 Cannot_Inline
("cannot inline & (object renaming)?",
2675 Body_Decl
, Spec_Id
);
2676 Set_Body_To_Inline
(Decl
, Empty
);
2678 end Check_Object_Renaming_In_GNATprove_Mode
;
2680 -------------------------------------
2681 -- Check_Package_Body_For_Inlining --
2682 -------------------------------------
2684 procedure Check_Package_Body_For_Inlining
(N
: Node_Id
; P
: Entity_Id
) is
2685 Bname
: Unit_Name_Type
;
2690 -- Legacy implementation (relying on frontend inlining)
2692 if not Back_End_Inlining
2693 and then Is_Compilation_Unit
(P
)
2694 and then not Is_Generic_Instance
(P
)
2696 Bname
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
2698 E
:= First_Entity
(P
);
2699 while Present
(E
) loop
2700 if Has_Pragma_Inline_Always
(E
)
2701 or else (Has_Pragma_Inline
(E
) and Front_End_Inlining
)
2703 if not Is_Loaded
(Bname
) then
2704 Load_Needed_Body
(N
, OK
);
2708 -- Check we are not trying to inline a parent whose body
2709 -- depends on a child, when we are compiling the body of
2710 -- the child. Otherwise we have a potential elaboration
2711 -- circularity with inlined subprograms and with
2712 -- Taft-Amendment types.
2715 Comp
: Node_Id
; -- Body just compiled
2716 Child_Spec
: Entity_Id
; -- Spec of main unit
2717 Ent
: Entity_Id
; -- For iteration
2718 With_Clause
: Node_Id
; -- Context of body.
2721 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Package_Body
2722 and then Present
(Body_Entity
(P
))
2726 ((Unit
(Library_Unit
(Cunit
(Main_Unit
)))));
2729 Parent
(Unit_Declaration_Node
(Body_Entity
(P
)));
2731 -- Check whether the context of the body just
2732 -- compiled includes a child of itself, and that
2733 -- child is the spec of the main compilation.
2735 With_Clause
:= First
(Context_Items
(Comp
));
2736 while Present
(With_Clause
) loop
2737 if Nkind
(With_Clause
) = N_With_Clause
2739 Scope
(Entity
(Name
(With_Clause
))) = P
2741 Entity
(Name
(With_Clause
)) = Child_Spec
2743 Error_Msg_Node_2
:= Child_Spec
;
2745 ("body of & depends on child unit&??",
2748 ("\subprograms in body cannot be inlined??",
2751 -- Disable further inlining from this unit,
2752 -- and keep Taft-amendment types incomplete.
2754 Ent
:= First_Entity
(P
);
2755 while Present
(Ent
) loop
2757 and then Has_Completion_In_Body
(Ent
)
2759 Set_Full_View
(Ent
, Empty
);
2761 elsif Is_Subprogram
(Ent
) then
2762 Set_Is_Inlined
(Ent
, False);
2776 elsif Ineffective_Inline_Warnings
then
2777 Error_Msg_Unit_1
:= Bname
;
2779 ("unable to inline subprograms defined in $?p?", P
);
2780 Error_Msg_N
("\body not found?p?", P
);
2791 end Check_Package_Body_For_Inlining
;
2793 --------------------
2794 -- Cleanup_Scopes --
2795 --------------------
2797 procedure Cleanup_Scopes
is
2805 Elmt
:= First_Elmt
(To_Clean
);
2806 while Present
(Elmt
) loop
2807 Scop
:= Node
(Elmt
);
2808 Kind
:= Ekind
(Scop
);
2810 if Kind
= E_Block
then
2811 Decl
:= Parent
(Block_Node
(Scop
));
2814 Decl
:= Unit_Declaration_Node
(Scop
);
2816 if Nkind
(Decl
) in N_Subprogram_Declaration
2817 | N_Task_Type_Declaration
2818 | N_Subprogram_Body_Stub
2820 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
2824 -- Finalizers are built only for package specs and bodies that are
2825 -- compilation units, so check that we do not have anything else.
2826 -- Moreover, they must be built at most once for each entity during
2827 -- the compilation of the main unit. However, if other units are
2828 -- later compiled for inlining purposes, they may also contain body
2829 -- instances and, therefore, appear again here, so we need to make
2830 -- sure that we do not build two finalizers for them (note that the
2831 -- contents of the finalizer for these units is irrelevant since it
2832 -- is not output in the generated code).
2834 if Kind
in E_Package | E_Package_Body
then
2836 Unit_Entity
: constant Entity_Id
:=
2837 (if Kind
= E_Package
then Scop
else Spec_Entity
(Scop
));
2840 pragma Assert
(Is_Compilation_Unit
(Unit_Entity
)
2841 and then (No
(Finalizer
(Scop
))
2842 or else Unit_Entity
/= Main_Unit_Entity
));
2844 if No
(Finalizer
(Scop
)) then
2847 Clean_Stmts
=> No_List
,
2849 Top_Decls
=> No_List
,
2850 Defer_Abort
=> False,
2853 if Present
(Fin
) then
2854 Set_Finalizer
(Scop
, Fin
);
2861 Expand_Cleanup_Actions
(Decl
);
2869 -----------------------------------------------
2870 -- Establish_Actual_Mapping_For_Inlined_Call --
2871 -----------------------------------------------
2873 procedure Establish_Actual_Mapping_For_Inlined_Call
2877 Body_Or_Expr_To_Check
: Node_Id
)
2880 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
2881 -- Determine whether a formal parameter is used only once in
2882 -- Body_Or_Expr_To_Check.
2884 -------------------------
2885 -- Formal_Is_Used_Once --
2886 -------------------------
2888 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
2889 Use_Counter
: Nat
:= 0;
2891 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
2892 -- Traverse the tree and count the uses of the formal parameter.
2893 -- In this case, for optimization purposes, we do not need to
2894 -- continue the traversal once more than one use is encountered.
2900 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
2902 -- The original node is an identifier
2904 if Nkind
(N
) = N_Identifier
2905 and then Present
(Entity
(N
))
2907 -- Original node's entity points to the one in the copied body
2909 and then Nkind
(Entity
(N
)) = N_Identifier
2910 and then Present
(Entity
(Entity
(N
)))
2912 -- The entity of the copied node is the formal parameter
2914 and then Entity
(Entity
(N
)) = Formal
2916 Use_Counter
:= Use_Counter
+ 1;
2918 -- If this is a second use then abandon the traversal
2920 if Use_Counter
> 1 then
2928 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
2930 -- Start of processing for Formal_Is_Used_Once
2933 Count_Formal_Uses
(Body_Or_Expr_To_Check
);
2934 return Use_Counter
= 1;
2935 end Formal_Is_Used_Once
;
2942 Loc
: constant Source_Ptr
:= Sloc
(N
);
2945 Temp_Typ
: Entity_Id
;
2947 -- Start of processing for Establish_Actual_Mapping_For_Inlined_Call
2950 F
:= First_Formal
(Subp
);
2951 A
:= First_Actual
(N
);
2952 while Present
(F
) loop
2953 if Present
(Renamed_Object
(F
)) then
2955 -- If expander is active, it is an error to try to inline a
2956 -- recursive subprogram. In GNATprove mode, just indicate that the
2957 -- inlining will not happen, and mark the subprogram as not always
2960 if GNATprove_Mode
then
2962 ("cannot inline call to recursive subprogram?", N
, Subp
);
2963 Set_Is_Inlined_Always
(Subp
, False);
2966 ("cannot inline call to recursive subprogram", N
);
2972 -- Reset Last_Assignment for any parameters of mode out or in out, to
2973 -- prevent spurious warnings about overwriting for assignments to the
2974 -- formal in the inlined code.
2976 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
2978 -- In GNATprove mode a protected component acting as an actual
2979 -- subprogram parameter will appear as inlined-for-proof. However,
2980 -- its E_Component entity is not an assignable object, so the
2981 -- assertion in Set_Last_Assignment will fail. We just omit the
2982 -- call to Set_Last_Assignment, because GNATprove flags useless
2983 -- assignments with its own flow analysis.
2985 -- In GNAT mode such a problem does not occur, because protected
2986 -- components are inlined via object renamings whose entity kind
2987 -- E_Variable is assignable.
2989 if Is_Assignable
(Entity
(A
)) then
2990 Set_Last_Assignment
(Entity
(A
), Empty
);
2993 (GNATprove_Mode
and then Is_Protected_Component
(Entity
(A
)));
2997 -- If the argument may be a controlling argument in a call within
2998 -- the inlined body, we must preserve its class-wide nature to ensure
2999 -- that dynamic dispatching will take place subsequently. If the
3000 -- formal has a constraint, then it must be preserved to retain the
3001 -- semantics of the body.
3003 if Is_Class_Wide_Type
(Etype
(F
))
3004 or else (Is_Access_Type
(Etype
(F
))
3005 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
3007 Temp_Typ
:= Etype
(F
);
3009 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
3010 and then Etype
(F
) /= Base_Type
(Etype
(F
))
3011 and then Is_Constrained
(Etype
(F
))
3013 Temp_Typ
:= Etype
(F
);
3016 Temp_Typ
:= Etype
(A
);
3019 -- If the actual is a simple name or a literal, no need to create a
3020 -- temporary, object can be used directly. Skip this optimization in
3021 -- GNATprove mode, to make sure any check on a type conversion will
3024 if (Is_Entity_Name
(A
)
3026 (not Is_Scalar_Type
(Etype
(A
))
3027 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
)
3028 and then not GNATprove_Mode
)
3030 -- When the actual is an identifier and the corresponding formal is
3031 -- used only once in the original body, the formal can be substituted
3032 -- directly with the actual parameter. Skip this optimization in
3033 -- GNATprove mode, to make sure any check on a type conversion
3037 (Nkind
(A
) = N_Identifier
3038 and then Formal_Is_Used_Once
(F
)
3039 and then not GNATprove_Mode
)
3041 -- If the actual is a literal and the formal has its address taken,
3042 -- we cannot pass the literal itself as an argument, so its value
3043 -- must be captured in a temporary.
3047 N_Real_Literal | N_Integer_Literal | N_Character_Literal
3048 and then not Address_Taken
(F
))
3050 if Etype
(F
) /= Etype
(A
) then
3052 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
3054 Set_Renamed_Object
(F
, A
);
3058 Temp
:= Make_Temporary
(Loc
, 'C');
3060 -- If the actual for an in/in-out parameter is a view conversion,
3061 -- make it into an unchecked conversion, given that an untagged
3062 -- type conversion is not a proper object for a renaming.
3064 -- In-out conversions that involve real conversions have already
3065 -- been transformed in Expand_Actuals.
3067 if Nkind
(A
) = N_Type_Conversion
3068 and then Ekind
(F
) /= E_In_Parameter
3070 New_A
:= Unchecked_Convert_To
(Etype
(F
), Expression
(A
));
3072 -- In GNATprove mode, keep the most precise type of the actual for
3073 -- the temporary variable, when the formal type is unconstrained.
3074 -- Otherwise, the AST may contain unexpected assignment statements
3075 -- to a temporary variable of unconstrained type renaming a local
3076 -- variable of constrained type, which is not expected by
3079 elsif Etype
(F
) /= Etype
(A
)
3080 and then (not GNATprove_Mode
or else Is_Constrained
(Etype
(F
)))
3082 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
3083 Temp_Typ
:= Etype
(F
);
3086 New_A
:= Relocate_Node
(A
);
3089 Set_Sloc
(New_A
, Sloc
(N
));
3091 -- If the actual has a by-reference type, it cannot be copied,
3092 -- so its value is captured in a renaming declaration. Otherwise
3093 -- declare a local constant initialized with the actual.
3095 -- We also use a renaming declaration for expressions of an array
3096 -- type that is not bit-packed, both for efficiency reasons and to
3097 -- respect the semantics of the call: in most cases the original
3098 -- call will pass the parameter by reference, and thus the inlined
3099 -- code will have the same semantics.
3101 -- Finally, we need a renaming declaration in the case of limited
3102 -- types for which initialization cannot be by copy either.
3104 if Ekind
(F
) = E_In_Parameter
3105 and then not Is_By_Reference_Type
(Etype
(A
))
3106 and then not Is_Limited_Type
(Etype
(A
))
3108 (not Is_Array_Type
(Etype
(A
))
3109 or else not Is_Object_Reference
(A
)
3110 or else Is_Bit_Packed_Array
(Etype
(A
)))
3113 Make_Object_Declaration
(Loc
,
3114 Defining_Identifier
=> Temp
,
3115 Constant_Present
=> True,
3116 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3117 Expression
=> New_A
);
3120 -- In GNATprove mode, make an explicit copy of input
3121 -- parameters when formal and actual types differ, to make
3122 -- sure any check on the type conversion will be issued.
3123 -- The legality of the copy is ensured by calling first
3124 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3127 and then Ekind
(F
) /= E_Out_Parameter
3128 and then not Same_Type
(Etype
(F
), Etype
(A
))
3130 pragma Assert
(not Is_By_Reference_Type
(Etype
(A
)));
3131 pragma Assert
(not Is_Limited_Type
(Etype
(A
)));
3134 Make_Object_Declaration
(Loc
,
3135 Defining_Identifier
=> Make_Temporary
(Loc
, 'C'),
3136 Constant_Present
=> True,
3137 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3138 Expression
=> New_Copy_Tree
(New_A
)));
3142 Make_Object_Renaming_Declaration
(Loc
,
3143 Defining_Identifier
=> Temp
,
3144 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3148 Append
(Decl
, Decls
);
3149 Set_Renamed_Object
(F
, Temp
);
3155 end Establish_Actual_Mapping_For_Inlined_Call
;
3157 -------------------------
3158 -- Expand_Inlined_Call --
3159 -------------------------
3161 procedure Expand_Inlined_Call
3164 Orig_Subp
: Entity_Id
)
3166 Decls
: constant List_Id
:= New_List
;
3167 Is_Predef
: constant Boolean :=
3168 Is_Predefined_Unit
(Get_Source_Unit
(Subp
));
3169 Loc
: constant Source_Ptr
:= Sloc
(N
);
3170 Orig_Bod
: constant Node_Id
:=
3171 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
3173 Uses_Back_End
: constant Boolean :=
3174 Back_End_Inlining
and then Optimization_Level
> 0;
3175 -- The back-end expansion is used if the target supports back-end
3176 -- inlining and some level of optimixation is required; otherwise
3177 -- the inlining takes place fully as a tree expansion.
3181 Exit_Lab
: Entity_Id
:= Empty
;
3182 Lab_Decl
: Node_Id
:= Empty
;
3185 Ret_Type
: Entity_Id
;
3189 Is_Unc_Decl
: Boolean;
3190 -- If the type returned by the function is unconstrained and the call
3191 -- can be inlined, special processing is required.
3193 Return_Object
: Entity_Id
:= Empty
;
3194 -- Entity in declaration in an extended_return_statement
3196 Targ
: Node_Id
:= Empty
;
3197 -- The target of the call. If context is an assignment statement then
3198 -- this is the left-hand side of the assignment, else it is a temporary
3199 -- to which the return value is assigned prior to rewriting the call.
3201 Targ1
: Node_Id
:= Empty
;
3202 -- A separate target used when the return type is unconstrained
3204 procedure Declare_Postconditions_Result
;
3205 -- When generating C code, declare _Result, which may be used in the
3206 -- inlined _Postconditions procedure to verify the return value.
3208 procedure Make_Exit_Label
;
3209 -- Build declaration for exit label to be used in Return statements,
3210 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3211 -- declaration). Does nothing if Exit_Lab already set.
3213 procedure Make_Loop_Labels_Unique
(HSS
: Node_Id
);
3214 -- When compiling for CCG and performing front-end inlining, replace
3215 -- loop names and references to them so that they do not conflict with
3216 -- homographs in the current subprogram.
3218 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
3219 -- Replace occurrence of a formal with the corresponding actual, or the
3220 -- thunk generated for it. Replace a return statement with an assignment
3221 -- to the target of the call, with appropriate conversions if needed.
3223 function Process_Formals_In_Aspects
(N
: Node_Id
) return Traverse_Result
;
3224 -- Because aspects are linked indirectly to the rest of the tree,
3225 -- replacement of formals appearing in aspect specifications must
3226 -- be performed in a separate pass, using an instantiation of the
3227 -- previous subprogram over aspect specifications reachable from N.
3229 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
3230 -- If the call being expanded is that of an internal subprogram, set the
3231 -- sloc of the generated block to that of the call itself, so that the
3232 -- expansion is skipped by the "next" command in gdb. Same processing
3233 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
3234 -- Debug_Generated_Code is true, suppress this change to simplify our
3235 -- own development. Same in GNATprove mode, to ensure that warnings and
3236 -- diagnostics point to the proper location.
3238 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
3239 -- In subtree N search for occurrences of dispatching calls that use the
3240 -- Ada 2005 Object.Operation notation and the object is a formal of the
3241 -- inlined subprogram. Reset the entity associated with Operation in all
3242 -- the found occurrences.
3244 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
3245 -- If the function body is a single expression, replace call with
3246 -- expression, else insert block appropriately.
3248 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
3249 -- If procedure body has no local variables, inline body without
3250 -- creating block, otherwise rewrite call with block.
3252 -----------------------------------
3253 -- Declare_Postconditions_Result --
3254 -----------------------------------
3256 procedure Declare_Postconditions_Result
is
3257 Enclosing_Subp
: constant Entity_Id
:= Scope
(Subp
);
3262 and then Is_Subprogram
(Enclosing_Subp
)
3263 and then Present
(Wrapped_Statements
(Enclosing_Subp
)));
3265 if Ekind
(Enclosing_Subp
) = E_Function
then
3266 if Nkind
(First
(Parameter_Associations
(N
))) in
3267 N_Numeric_Or_String_Literal
3269 Append_To
(Declarations
(Blk
),
3270 Make_Object_Declaration
(Loc
,
3271 Defining_Identifier
=>
3272 Make_Defining_Identifier
(Loc
, Name_uResult
),
3273 Constant_Present
=> True,
3274 Object_Definition
=>
3275 New_Occurrence_Of
(Etype
(Enclosing_Subp
), Loc
),
3277 New_Copy_Tree
(First
(Parameter_Associations
(N
)))));
3279 Append_To
(Declarations
(Blk
),
3280 Make_Object_Renaming_Declaration
(Loc
,
3281 Defining_Identifier
=>
3282 Make_Defining_Identifier
(Loc
, Name_uResult
),
3284 New_Occurrence_Of
(Etype
(Enclosing_Subp
), Loc
),
3286 New_Copy_Tree
(First
(Parameter_Associations
(N
)))));
3289 end Declare_Postconditions_Result
;
3291 ---------------------
3292 -- Make_Exit_Label --
3293 ---------------------
3295 procedure Make_Exit_Label
is
3296 Lab_Ent
: Entity_Id
;
3298 if No
(Exit_Lab
) then
3299 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
3300 Lab_Id
:= New_Occurrence_Of
(Lab_Ent
, Loc
);
3301 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
3303 Make_Implicit_Label_Declaration
(Loc
,
3304 Defining_Identifier
=> Lab_Ent
,
3305 Label_Construct
=> Exit_Lab
);
3307 end Make_Exit_Label
;
3309 -----------------------------
3310 -- Make_Loop_Labels_Unique --
3311 -----------------------------
3313 procedure Make_Loop_Labels_Unique
(HSS
: Node_Id
) is
3314 function Process_Loop
(N
: Node_Id
) return Traverse_Result
;
3320 function Process_Loop
(N
: Node_Id
) return Traverse_Result
is
3324 if Nkind
(N
) = N_Loop_Statement
3325 and then Present
(Identifier
(N
))
3327 -- Create new external name for loop and update the
3328 -- corresponding entity.
3330 Id
:= Entity
(Identifier
(N
));
3331 Set_Chars
(Id
, New_External_Name
(Chars
(Id
), 'L', -1));
3332 Set_Chars
(Identifier
(N
), Chars
(Id
));
3334 elsif Nkind
(N
) = N_Exit_Statement
3335 and then Present
(Name
(N
))
3337 -- The exit statement must name an enclosing loop, whose name
3338 -- has already been updated.
3340 Set_Chars
(Name
(N
), Chars
(Entity
(Name
(N
))));
3346 procedure Update_Loop_Names
is new Traverse_Proc
(Process_Loop
);
3352 -- Start of processing for Make_Loop_Labels_Unique
3355 if Modify_Tree_For_C
then
3356 Stmt
:= First
(Statements
(HSS
));
3357 while Present
(Stmt
) loop
3358 Update_Loop_Names
(Stmt
);
3362 end Make_Loop_Labels_Unique
;
3364 ---------------------
3365 -- Process_Formals --
3366 ---------------------
3368 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
3373 Had_Private_View
: Boolean;
3376 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
3379 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
3380 A
:= Renamed_Object
(E
);
3382 -- Rewrite the occurrence of the formal into an occurrence of
3383 -- the actual. Also establish visibility on the proper view of
3384 -- the actual's subtype for the body's context (if the actual's
3385 -- subtype is private at the call point but its full view is
3386 -- visible to the body, then the inlined tree here must be
3387 -- analyzed with the full view).
3389 -- The Has_Private_View flag is cleared by rewriting, so it
3390 -- must be explicitly saved and restored, just like when
3391 -- instantiating the body to inline.
3393 if Is_Entity_Name
(A
) then
3394 Had_Private_View
:= Has_Private_View
(N
);
3395 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Sloc
(N
)));
3396 Set_Has_Private_View
(N
, Had_Private_View
);
3397 Check_Private_View
(N
);
3399 elsif Nkind
(A
) = N_Defining_Identifier
then
3400 Had_Private_View
:= Has_Private_View
(N
);
3401 Rewrite
(N
, New_Occurrence_Of
(A
, Sloc
(N
)));
3402 Set_Has_Private_View
(N
, Had_Private_View
);
3403 Check_Private_View
(N
);
3408 Rewrite
(N
, New_Copy
(A
));
3414 elsif Is_Entity_Name
(N
)
3415 and then Present
(Return_Object
)
3416 and then Chars
(N
) = Chars
(Return_Object
)
3418 -- Occurrence within an extended return statement. The return
3419 -- object is local to the body been inlined, and thus the generic
3420 -- copy is not analyzed yet, so we match by name, and replace it
3421 -- with target of call.
3423 if Nkind
(Targ
) = N_Defining_Identifier
then
3424 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
3426 Rewrite
(N
, New_Copy_Tree
(Targ
));
3431 elsif Nkind
(N
) = N_Simple_Return_Statement
then
3432 if No
(Expression
(N
)) then
3433 Num_Ret
:= Num_Ret
+ 1;
3436 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
3439 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
3440 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
3442 -- Function body is a single expression. No need for
3448 Num_Ret
:= Num_Ret
+ 1;
3452 -- Because of the presence of private types, the views of the
3453 -- expression and the context may be different, so place
3454 -- a type conversion to the context type to avoid spurious
3455 -- errors, e.g. when the expression is a numeric literal and
3456 -- the context is private. If the expression is an aggregate,
3457 -- use a qualified expression, because an aggregate is not a
3458 -- legal argument of a conversion. Ditto for numeric, character
3459 -- and string literals, and attributes that yield a universal
3460 -- type, because those must be resolved to a specific type.
3462 if Nkind
(Expression
(N
)) in N_Aggregate
3463 | N_Character_Literal
3466 or else Yields_Universal_Type
(Expression
(N
))
3469 Make_Qualified_Expression
(Sloc
(N
),
3470 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
3471 Expression
=> Relocate_Node
(Expression
(N
)));
3473 -- Use an unchecked type conversion between access types, for
3474 -- which a type conversion would not always be valid, as no
3475 -- check may result from the conversion.
3477 elsif Is_Access_Type
(Ret_Type
) then
3479 Unchecked_Convert_To
3480 (Ret_Type
, Relocate_Node
(Expression
(N
)));
3482 -- Otherwise use a type conversion, which may trigger a check
3486 Make_Type_Conversion
(Sloc
(N
),
3487 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
3488 Expression
=> Relocate_Node
(Expression
(N
)));
3491 if Nkind
(Targ
) = N_Defining_Identifier
then
3493 Make_Assignment_Statement
(Loc
,
3494 Name
=> New_Occurrence_Of
(Targ
, Loc
),
3495 Expression
=> Ret
));
3498 Make_Assignment_Statement
(Loc
,
3499 Name
=> New_Copy
(Targ
),
3500 Expression
=> Ret
));
3503 Set_Assignment_OK
(Name
(N
));
3505 if Present
(Exit_Lab
) then
3507 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
3513 -- An extended return becomes a block whose first statement is the
3514 -- assignment of the initial expression of the return object to the
3515 -- target of the call itself.
3517 elsif Nkind
(N
) = N_Extended_Return_Statement
then
3519 Return_Decl
: constant Entity_Id
:=
3520 First
(Return_Object_Declarations
(N
));
3524 Return_Object
:= Defining_Identifier
(Return_Decl
);
3526 if Present
(Expression
(Return_Decl
)) then
3527 if Nkind
(Targ
) = N_Defining_Identifier
then
3529 Make_Assignment_Statement
(Loc
,
3530 Name
=> New_Occurrence_Of
(Targ
, Loc
),
3531 Expression
=> Expression
(Return_Decl
));
3534 Make_Assignment_Statement
(Loc
,
3535 Name
=> New_Copy
(Targ
),
3536 Expression
=> Expression
(Return_Decl
));
3539 Set_Assignment_OK
(Name
(Assign
));
3541 if No
(Handled_Statement_Sequence
(N
)) then
3542 Set_Handled_Statement_Sequence
(N
,
3543 Make_Handled_Sequence_Of_Statements
(Loc
,
3544 Statements
=> New_List
));
3548 Statements
(Handled_Statement_Sequence
(N
)));
3552 Make_Block_Statement
(Loc
,
3553 Handled_Statement_Sequence
=>
3554 Handled_Statement_Sequence
(N
)));
3559 -- Remove pragma Unreferenced since it may refer to formals that
3560 -- are not visible in the inlined body, and in any case we will
3561 -- not be posting warnings on the inlined body so it is unneeded.
3563 elsif Nkind
(N
) = N_Pragma
3564 and then Pragma_Name
(N
) = Name_Unreferenced
3566 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
3572 end Process_Formals
;
3574 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
3576 --------------------------------
3577 -- Process_Formals_In_Aspects --
3578 --------------------------------
3580 function Process_Formals_In_Aspects
3581 (N
: Node_Id
) return Traverse_Result
3586 if Has_Aspects
(N
) then
3587 A
:= First
(Aspect_Specifications
(N
));
3588 while Present
(A
) loop
3589 Replace_Formals
(Expression
(A
));
3595 end Process_Formals_In_Aspects
;
3597 procedure Replace_Formals_In_Aspects
is
3598 new Traverse_Proc
(Process_Formals_In_Aspects
);
3604 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
3606 if not Debug_Generated_Code
then
3607 Set_Sloc
(Nod
, Sloc
(N
));
3608 Set_Comes_From_Source
(Nod
, False);
3614 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
3616 ------------------------------
3617 -- Reset_Dispatching_Calls --
3618 ------------------------------
3620 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
3622 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
3628 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
3630 if Nkind
(N
) = N_Procedure_Call_Statement
3631 and then Nkind
(Name
(N
)) = N_Selected_Component
3632 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
3633 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
3634 and then Is_Dispatching_Operation
3635 (Entity
(Selector_Name
(Name
(N
))))
3637 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
3643 procedure Do_Reset_Calls
is new Traverse_Proc
(Do_Reset
);
3647 end Reset_Dispatching_Calls
;
3649 ---------------------------
3650 -- Rewrite_Function_Call --
3651 ---------------------------
3653 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
3654 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
3655 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
3658 Make_Loop_Labels_Unique
(HSS
);
3660 -- Optimize simple case: function body is a single return statement,
3661 -- which has been expanded into an assignment.
3663 if Is_Empty_List
(Declarations
(Blk
))
3664 and then Nkind
(Fst
) = N_Assignment_Statement
3665 and then No
(Next
(Fst
))
3667 -- The function call may have been rewritten as the temporary
3668 -- that holds the result of the call, in which case remove the
3669 -- now useless declaration.
3671 if Nkind
(N
) = N_Identifier
3672 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
3674 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
3677 Rewrite
(N
, Expression
(Fst
));
3679 elsif Nkind
(N
) = N_Identifier
3680 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
3682 -- The block assigns the result of the call to the temporary
3684 Insert_After
(Parent
(Entity
(N
)), Blk
);
3686 -- If the context is an assignment, and the left-hand side is free of
3687 -- side-effects, the replacement is also safe.
3689 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3691 (Is_Entity_Name
(Name
(Parent
(N
)))
3693 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3694 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
3697 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
3698 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
3700 -- Replace assignment with the block
3703 Original_Assignment
: constant Node_Id
:= Parent
(N
);
3706 -- Preserve the original assignment node to keep the complete
3707 -- assignment subtree consistent enough for Analyze_Assignment
3708 -- to proceed (specifically, the original Lhs node must still
3709 -- have an assignment statement as its parent).
3711 -- We cannot rely on Original_Node to go back from the block
3712 -- node to the assignment node, because the assignment might
3713 -- already be a rewrite substitution.
3715 Discard_Node
(Relocate_Node
(Original_Assignment
));
3716 Rewrite
(Original_Assignment
, Blk
);
3719 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
3721 -- A call to a function which returns an unconstrained type
3722 -- found in the expression initializing an object-declaration is
3723 -- expanded into a procedure call which must be added after the
3724 -- object declaration.
3726 if Is_Unc_Decl
and Back_End_Inlining
then
3727 Insert_Action_After
(Parent
(N
), Blk
);
3729 Set_Expression
(Parent
(N
), Empty
);
3730 Insert_After
(Parent
(N
), Blk
);
3733 elsif Is_Unc
and then not Back_End_Inlining
then
3734 Insert_Before
(Parent
(N
), Blk
);
3736 end Rewrite_Function_Call
;
3738 ----------------------------
3739 -- Rewrite_Procedure_Call --
3740 ----------------------------
3742 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
3743 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
3746 Make_Loop_Labels_Unique
(HSS
);
3748 -- If there is a transient scope for N, this will be the scope of the
3749 -- actions for N, and the statements in Blk need to be within this
3750 -- scope. For example, they need to have visibility on the constant
3751 -- declarations created for the formals.
3753 -- If N needs no transient scope, and if there are no declarations in
3754 -- the inlined body, we can do a little optimization and insert the
3755 -- statements for the body directly after N, and rewrite N to a
3756 -- null statement, instead of rewriting N into a full-blown block
3759 if not Scope_Is_Transient
3760 and then Is_Empty_List
(Declarations
(Blk
))
3762 Insert_List_After
(N
, Statements
(HSS
));
3763 Rewrite
(N
, Make_Null_Statement
(Loc
));
3767 end Rewrite_Procedure_Call
;
3769 -- Start of processing for Expand_Inlined_Call
3772 -- Initializations for old/new semantics
3774 if not Uses_Back_End
then
3775 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
3776 and then not Is_Constrained
(Etype
(Subp
));
3777 Is_Unc_Decl
:= False;
3779 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
3780 and then Optimization_Level
> 0;
3781 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
3785 -- Check for an illegal attempt to inline a recursive procedure. If the
3786 -- subprogram has parameters this is detected when trying to supply a
3787 -- binding for parameters that already have one. For parameterless
3788 -- subprograms this must be done explicitly.
3790 if In_Open_Scopes
(Subp
) then
3792 ("cannot inline call to recursive subprogram?", N
, Subp
);
3793 Set_Is_Inlined
(Subp
, False);
3796 -- Skip inlining if this is not a true inlining since the attribute
3797 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3798 -- true inlining, Orig_Bod has code rather than being an entity.
3800 elsif Nkind
(Orig_Bod
) in N_Entity
then
3804 if Nkind
(Orig_Bod
) in N_Defining_Identifier
3805 | N_Defining_Operator_Symbol
3807 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3808 -- can be replaced with calls to the renamed entity directly, because
3809 -- the subprograms are subtype conformant. If the renamed subprogram
3810 -- is an inherited operation, we must redo the expansion because
3811 -- implicit conversions may be needed. Similarly, if the renamed
3812 -- entity is inlined, expand the call for further optimizations.
3814 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
3816 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
3823 -- Register the call in the list of inlined calls
3825 Append_New_Elmt
(N
, To
=> Inlined_Calls
);
3827 -- Use generic machinery to copy body of inlined subprogram, as if it
3828 -- were an instantiation, resetting source locations appropriately, so
3829 -- that nested inlined calls appear in the main unit.
3831 Save_Env
(Subp
, Empty
);
3832 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
3836 if not Uses_Back_End
then
3841 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
3843 Make_Block_Statement
(Loc
,
3844 Declarations
=> Declarations
(Bod
),
3845 Handled_Statement_Sequence
=>
3846 Handled_Statement_Sequence
(Bod
));
3848 if No
(Declarations
(Bod
)) then
3849 Set_Declarations
(Blk
, New_List
);
3852 -- When generating C code, declare _Result, which may be used to
3853 -- verify the return value.
3855 if Modify_Tree_For_C
3856 and then Nkind
(N
) = N_Procedure_Call_Statement
3857 and then Chars
(Name
(N
)) = Name_uWrapped_Statements
3859 Declare_Postconditions_Result
;
3862 -- For the unconstrained case, capture the name of the local
3863 -- variable that holds the result. This must be the first
3864 -- declaration in the block, because its bounds cannot depend
3865 -- on local variables. Otherwise there is no way to declare the
3866 -- result outside of the block. Needless to say, in general the
3867 -- bounds will depend on the actuals in the call.
3869 -- If the context is an assignment statement, as is the case
3870 -- for the expansion of an extended return, the left-hand side
3871 -- provides bounds even if the return type is unconstrained.
3875 First_Decl
: Node_Id
;
3878 First_Decl
:= First
(Declarations
(Blk
));
3880 -- If the body is a single extended return statement,the
3881 -- resulting block is a nested block.
3883 if No
(First_Decl
) then
3885 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
3887 if Nkind
(First_Decl
) = N_Block_Statement
then
3888 First_Decl
:= First
(Declarations
(First_Decl
));
3892 -- No front-end inlining possible
3894 if Nkind
(First_Decl
) /= N_Object_Declaration
then
3898 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
3899 Targ1
:= Defining_Identifier
(First_Decl
);
3901 Targ1
:= Name
(Parent
(N
));
3918 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
3920 Make_Block_Statement
(Loc
,
3921 Declarations
=> Declarations
(Bod
),
3922 Handled_Statement_Sequence
=>
3923 Handled_Statement_Sequence
(Bod
));
3925 -- Inline a call to a function that returns an unconstrained type.
3926 -- The semantic analyzer checked that frontend-inlined functions
3927 -- returning unconstrained types have no declarations and have
3928 -- a single extended return statement. As part of its processing
3929 -- the function was split into two subprograms: a procedure P' and
3930 -- a function F' that has a block with a call to procedure P' (see
3931 -- Split_Unconstrained_Function).
3937 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)))) =
3941 Blk_Stmt
: constant Node_Id
:=
3942 First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
3943 First_Stmt
: constant Node_Id
:=
3944 First
(Statements
(Handled_Statement_Sequence
(Blk_Stmt
)));
3945 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
3949 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
3950 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
3951 and then No
(Next
(Second_Stmt
)));
3956 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
3957 Empty
, Instantiating
=> True);
3960 -- Capture the name of the local variable that holds the
3961 -- result. This must be the first declaration in the block,
3962 -- because its bounds cannot depend on local variables.
3963 -- Otherwise there is no way to declare the result outside
3964 -- of the block. Needless to say, in general the bounds will
3965 -- depend on the actuals in the call.
3967 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
3968 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
3970 -- If the context is an assignment statement, as is the case
3971 -- for the expansion of an extended return, the left-hand
3972 -- side provides bounds even if the return type is
3976 Targ1
:= Name
(Parent
(N
));
3981 if No
(Declarations
(Bod
)) then
3982 Set_Declarations
(Blk
, New_List
);
3987 -- If this is a derived function, establish the proper return type
3989 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
3990 Ret_Type
:= Etype
(Orig_Subp
);
3992 Ret_Type
:= Etype
(Subp
);
3995 -- Create temporaries for the actuals that are expressions, or that are
3996 -- scalars and require copying to preserve semantics.
3998 Establish_Actual_Mapping_For_Inlined_Call
(N
, Subp
, Decls
, Orig_Bod
);
4000 -- Establish target of function call. If context is not assignment or
4001 -- declaration, create a temporary as a target. The declaration for the
4002 -- temporary may be subsequently optimized away if the body is a single
4003 -- expression, or if the left-hand side of the assignment is simple
4004 -- enough, i.e. an entity or an explicit dereference of one.
4006 if Ekind
(Subp
) = E_Function
then
4007 if Nkind
(Parent
(N
)) = N_Assignment_Statement
4008 and then Is_Entity_Name
(Name
(Parent
(N
)))
4010 Targ
:= Name
(Parent
(N
));
4012 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
4013 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
4014 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
4016 Targ
:= Name
(Parent
(N
));
4018 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
4019 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
4020 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
4022 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
4024 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
4025 and then Is_Limited_Type
(Etype
(Subp
))
4027 Targ
:= Defining_Identifier
(Parent
(N
));
4029 -- New semantics: In an object declaration avoid an extra copy
4030 -- of the result of a call to an inlined function that returns
4031 -- an unconstrained type
4034 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4037 Targ
:= Defining_Identifier
(Parent
(N
));
4040 -- Replace call with temporary and create its declaration
4042 Temp
:= Make_Temporary
(Loc
, 'C');
4043 Set_Is_Internal
(Temp
);
4045 -- For the unconstrained case, the generated temporary has the
4046 -- same constrained declaration as the result variable. It may
4047 -- eventually be possible to remove that temporary and use the
4048 -- result variable directly.
4050 if Is_Unc
and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
4053 Make_Object_Declaration
(Loc
,
4054 Defining_Identifier
=> Temp
,
4055 Object_Definition
=>
4056 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
4058 Replace_Formals
(Decl
);
4062 Make_Object_Declaration
(Loc
,
4063 Defining_Identifier
=> Temp
,
4064 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
4066 Set_Etype
(Temp
, Ret_Type
);
4069 Set_No_Initialization
(Decl
);
4070 Append
(Decl
, Decls
);
4071 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4076 Insert_Actions
(N
, Decls
);
4080 -- Special management for inlining a call to a function that returns
4081 -- an unconstrained type and initializes an object declaration: we
4082 -- avoid generating undesired extra calls and goto statements.
4085 -- function Func (...) return String is
4088 -- Result : String (1 .. 4);
4090 -- Proc (Result, ...);
4095 -- Result : String := Func (...);
4097 -- Replace this object declaration by:
4099 -- Result : String (1 .. 4);
4100 -- Proc (Result, ...);
4102 Remove_Homonym
(Targ
);
4105 Make_Object_Declaration
4107 Defining_Identifier
=> Targ
,
4108 Object_Definition
=>
4109 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
4110 Replace_Formals
(Decl
);
4111 Rewrite
(Parent
(N
), Decl
);
4112 Analyze
(Parent
(N
));
4114 -- Avoid spurious warnings since we know that this declaration is
4115 -- referenced by the procedure call.
4117 Set_Never_Set_In_Source
(Targ
, False);
4119 -- Remove the local declaration of the extended return stmt from the
4122 Remove
(Parent
(Targ1
));
4124 -- Update the reference to the result (since we have rewriten the
4125 -- object declaration)
4128 Blk_Call_Stmt
: Node_Id
;
4131 -- Capture the call to the procedure
4134 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
4136 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
4138 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
4139 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
4140 New_Occurrence_Of
(Targ
, Loc
));
4143 -- Remove the return statement
4146 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
4147 N_Simple_Return_Statement
);
4149 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
4152 -- Traverse the tree and replace formals with actuals or their thunks.
4153 -- Attach block to tree before analysis and rewriting.
4155 Replace_Formals
(Blk
);
4156 Replace_Formals_In_Aspects
(Blk
);
4157 Set_Parent
(Blk
, N
);
4159 if GNATprove_Mode
then
4162 elsif not Comes_From_Source
(Subp
) or else Is_Predef
then
4168 -- No action needed since return statement has been already removed
4172 elsif Present
(Exit_Lab
) then
4174 -- If there's a single return statement at the end of the subprogram,
4175 -- the corresponding goto statement and the corresponding label are
4180 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
4183 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
4185 Append
(Lab_Decl
, (Declarations
(Blk
)));
4186 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
4190 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
4191 -- on conflicting private views that Gigi would ignore. If this is a
4192 -- predefined unit, analyze with checks off, as is done in the non-
4193 -- inlined run-time units.
4196 I_Flag
: constant Boolean := In_Inlined_Body
;
4199 In_Inlined_Body
:= True;
4203 Style
: constant Boolean := Style_Check
;
4206 Style_Check
:= False;
4208 -- Search for dispatching calls that use the Object.Operation
4209 -- notation using an Object that is a parameter of the inlined
4210 -- function. We reset the decoration of Operation to force
4211 -- the reanalysis of the inlined dispatching call because
4212 -- the actual object has been inlined.
4214 Reset_Dispatching_Calls
(Blk
);
4216 -- In GNATprove mode, always consider checks on, even for
4217 -- predefined units.
4219 if GNATprove_Mode
then
4222 Analyze
(Blk
, Suppress
=> All_Checks
);
4225 Style_Check
:= Style
;
4232 In_Inlined_Body
:= I_Flag
;
4235 if Ekind
(Subp
) = E_Procedure
then
4236 Rewrite_Procedure_Call
(N
, Blk
);
4239 Rewrite_Function_Call
(N
, Blk
);
4244 -- For the unconstrained case, the replacement of the call has been
4245 -- made prior to the complete analysis of the generated declarations.
4246 -- Propagate the proper type now.
4249 if Nkind
(N
) = N_Identifier
then
4250 Set_Etype
(N
, Etype
(Entity
(N
)));
4252 Set_Etype
(N
, Etype
(Targ1
));
4259 -- Cleanup mapping between formals and actuals for other expansions
4261 Reset_Actual_Mapping_For_Inlined_Call
(Subp
);
4262 end Expand_Inlined_Call
;
4264 --------------------------
4265 -- Get_Code_Unit_Entity --
4266 --------------------------
4268 function Get_Code_Unit_Entity
(E
: Entity_Id
) return Entity_Id
is
4269 Unit
: Entity_Id
:= Cunit_Entity
(Get_Code_Unit
(E
));
4272 if Ekind
(Unit
) = E_Package_Body
then
4273 Unit
:= Spec_Entity
(Unit
);
4277 end Get_Code_Unit_Entity
;
4279 ------------------------------
4280 -- Has_Excluded_Declaration --
4281 ------------------------------
4283 function Has_Excluded_Declaration
4285 Decls
: List_Id
) return Boolean
4287 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4288 -- Nested subprograms make a given body ineligible for inlining, but
4289 -- we make an exception for instantiations of unchecked conversion.
4290 -- The body has not been analyzed yet, so check the name, and verify
4291 -- that the visible entity with that name is the predefined unit.
4293 -----------------------------
4294 -- Is_Unchecked_Conversion --
4295 -----------------------------
4297 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4298 Id
: constant Node_Id
:= Name
(D
);
4302 if Nkind
(Id
) = N_Identifier
4303 and then Chars
(Id
) = Name_Unchecked_Conversion
4305 Conv
:= Current_Entity
(Id
);
4307 elsif Nkind
(Id
) in N_Selected_Component | N_Expanded_Name
4308 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4310 Conv
:= Current_Entity
(Selector_Name
(Id
));
4315 return Present
(Conv
)
4316 and then Is_Predefined_Unit
(Get_Source_Unit
(Conv
))
4317 and then Is_Intrinsic_Subprogram
(Conv
);
4318 end Is_Unchecked_Conversion
;
4324 -- Start of processing for Has_Excluded_Declaration
4327 -- No action needed if the check is not needed
4329 if not Check_Inlining_Restrictions
then
4333 Decl
:= First
(Decls
);
4334 while Present
(Decl
) loop
4336 -- First declarations universally excluded
4338 if Nkind
(Decl
) = N_Package_Declaration
then
4340 ("cannot inline & (nested package declaration)?", Decl
, Subp
);
4343 elsif Nkind
(Decl
) = N_Package_Instantiation
then
4345 ("cannot inline & (nested package instantiation)?", Decl
, Subp
);
4349 -- Then declarations excluded only for front-end inlining
4351 if Back_End_Inlining
then
4354 elsif Nkind
(Decl
) = N_Task_Type_Declaration
4355 or else Nkind
(Decl
) = N_Single_Task_Declaration
4358 ("cannot inline & (nested task type declaration)?", Decl
, Subp
);
4361 elsif Nkind
(Decl
) in N_Protected_Type_Declaration
4362 | N_Single_Protected_Declaration
4365 ("cannot inline & (nested protected type declaration)?",
4369 elsif Nkind
(Decl
) = N_Subprogram_Body
then
4371 ("cannot inline & (nested subprogram)?", Decl
, Subp
);
4374 elsif Nkind
(Decl
) = N_Function_Instantiation
4375 and then not Is_Unchecked_Conversion
(Decl
)
4378 ("cannot inline & (nested function instantiation)?", Decl
, Subp
);
4381 elsif Nkind
(Decl
) = N_Procedure_Instantiation
then
4383 ("cannot inline & (nested procedure instantiation)?",
4387 -- Subtype declarations with predicates will generate predicate
4388 -- functions, i.e. nested subprogram bodies, so inlining is not
4391 elsif Nkind
(Decl
) = N_Subtype_Declaration
then
4397 A
:= First
(Aspect_Specifications
(Decl
));
4398 while Present
(A
) loop
4399 A_Id
:= Get_Aspect_Id
(Chars
(Identifier
(A
)));
4401 if A_Id
= Aspect_Predicate
4402 or else A_Id
= Aspect_Static_Predicate
4403 or else A_Id
= Aspect_Dynamic_Predicate
4406 ("cannot inline & (subtype declaration with "
4407 & "predicate)?", Decl
, Subp
);
4420 end Has_Excluded_Declaration
;
4422 ----------------------------
4423 -- Has_Excluded_Statement --
4424 ----------------------------
4426 function Has_Excluded_Statement
4428 Stats
: List_Id
) return Boolean
4434 -- No action needed if the check is not needed
4436 if not Check_Inlining_Restrictions
then
4441 while Present
(S
) loop
4442 if Nkind
(S
) in N_Abort_Statement
4443 | N_Asynchronous_Select
4444 | N_Conditional_Entry_Call
4445 | N_Delay_Relative_Statement
4446 | N_Delay_Until_Statement
4447 | N_Selective_Accept
4448 | N_Timed_Entry_Call
4451 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4454 elsif Nkind
(S
) = N_Block_Statement
then
4455 if Has_Excluded_Declaration
(Subp
, Declarations
(S
)) then
4458 elsif Present
(Handled_Statement_Sequence
(S
)) then
4459 if not Back_End_Inlining
4462 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4465 ("cannot inline& (exception handler)?",
4466 First
(Exception_Handlers
4467 (Handled_Statement_Sequence
(S
))),
4471 elsif Has_Excluded_Statement
4472 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
4478 elsif Nkind
(S
) = N_Case_Statement
then
4479 E
:= First
(Alternatives
(S
));
4480 while Present
(E
) loop
4481 if Has_Excluded_Statement
(Subp
, Statements
(E
)) then
4488 elsif Nkind
(S
) = N_If_Statement
then
4489 if Has_Excluded_Statement
(Subp
, Then_Statements
(S
)) then
4493 if Present
(Elsif_Parts
(S
)) then
4494 E
:= First
(Elsif_Parts
(S
));
4495 while Present
(E
) loop
4496 if Has_Excluded_Statement
(Subp
, Then_Statements
(E
)) then
4504 if Present
(Else_Statements
(S
))
4505 and then Has_Excluded_Statement
(Subp
, Else_Statements
(S
))
4510 elsif Nkind
(S
) = N_Loop_Statement
4511 and then Has_Excluded_Statement
(Subp
, Statements
(S
))
4515 elsif Nkind
(S
) = N_Extended_Return_Statement
then
4516 if Present
(Handled_Statement_Sequence
(S
))
4518 Has_Excluded_Statement
4519 (Subp
, Statements
(Handled_Statement_Sequence
(S
)))
4523 elsif not Back_End_Inlining
4524 and then Present
(Handled_Statement_Sequence
(S
))
4526 Present
(Exception_Handlers
4527 (Handled_Statement_Sequence
(S
)))
4530 ("cannot inline& (exception handler)?",
4531 First
(Exception_Handlers
(Handled_Statement_Sequence
(S
))),
4541 end Has_Excluded_Statement
;
4543 --------------------------
4544 -- Has_Initialized_Type --
4545 --------------------------
4547 function Has_Initialized_Type
(E
: Entity_Id
) return Boolean is
4548 E_Body
: constant Node_Id
:= Subprogram_Body
(E
);
4552 if No
(E_Body
) then -- imported subprogram
4556 Decl
:= First
(Declarations
(E_Body
));
4557 while Present
(Decl
) loop
4558 if Nkind
(Decl
) = N_Full_Type_Declaration
4559 and then Comes_From_Source
(Decl
)
4560 and then Present
(Init_Proc
(Defining_Identifier
(Decl
)))
4570 end Has_Initialized_Type
;
4572 -----------------------
4573 -- Has_Single_Return --
4574 -----------------------
4576 function Has_Single_Return
(N
: Node_Id
) return Boolean is
4577 Return_Statement
: Node_Id
:= Empty
;
4579 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
4585 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
4587 if Nkind
(N
) = N_Simple_Return_Statement
then
4588 if Present
(Expression
(N
))
4589 and then Is_Entity_Name
(Expression
(N
))
4591 pragma Assert
(Present
(Entity
(Expression
(N
))));
4593 if No
(Return_Statement
) then
4594 Return_Statement
:= N
;
4599 (Present
(Entity
(Expression
(Return_Statement
))));
4601 if Entity
(Expression
(N
)) =
4602 Entity
(Expression
(Return_Statement
))
4610 -- A return statement within an extended return is a noop after
4613 elsif No
(Expression
(N
))
4614 and then Nkind
(Parent
(Parent
(N
))) =
4615 N_Extended_Return_Statement
4620 -- Expression has wrong form
4625 -- We can only inline a build-in-place function if it has a single
4628 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4629 if No
(Return_Statement
) then
4630 Return_Statement
:= N
;
4642 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
4644 -- Start of processing for Has_Single_Return
4647 if Check_All_Returns
(N
) /= OK
then
4650 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
4655 Present
(Declarations
(N
))
4656 and then Present
(First
(Declarations
(N
)))
4657 and then Nkind
(First
(Declarations
(N
))) = N_Object_Declaration
4658 and then Entity
(Expression
(Return_Statement
)) =
4659 Defining_Identifier
(First
(Declarations
(N
)));
4661 end Has_Single_Return
;
4663 -----------------------------
4664 -- In_Main_Unit_Or_Subunit --
4665 -----------------------------
4667 function In_Main_Unit_Or_Subunit
(E
: Entity_Id
) return Boolean is
4668 Comp
: Node_Id
:= Cunit
(Get_Code_Unit
(E
));
4671 -- Check whether the subprogram or package to inline is within the main
4672 -- unit or its spec or within a subunit. In either case there are no
4673 -- additional bodies to process. If the subprogram appears in a parent
4674 -- of the current unit, the check on whether inlining is possible is
4675 -- done in Analyze_Inlined_Bodies.
4677 while Nkind
(Unit
(Comp
)) = N_Subunit
loop
4678 Comp
:= Library_Unit
(Comp
);
4681 return Comp
= Cunit
(Main_Unit
)
4682 or else Comp
= Library_Unit
(Cunit
(Main_Unit
));
4683 end In_Main_Unit_Or_Subunit
;
4689 procedure Initialize
is
4691 Pending_Instantiations
.Init
;
4692 Called_Pending_Instantiations
.Init
;
4693 Inlined_Bodies
.Init
;
4697 for J
in Hash_Headers
'Range loop
4698 Hash_Headers
(J
) := No_Subp
;
4701 Inlined_Calls
:= No_Elist
;
4702 Backend_Calls
:= No_Elist
;
4703 Backend_Instances
:= No_Elist
;
4704 Backend_Inlined_Subps
:= No_Elist
;
4705 Backend_Not_Inlined_Subps
:= No_Elist
;
4708 ---------------------------------
4709 -- Inline_Static_Function_Call --
4710 ---------------------------------
4712 procedure Inline_Static_Function_Call
(N
: Node_Id
; Subp
: Entity_Id
) is
4714 function Replace_Formal
(N
: Node_Id
) return Traverse_Result
;
4715 -- Replace each occurrence of a formal with the
4716 -- corresponding actual, using the mapping created
4717 -- by Establish_Actual_Mapping_For_Inlined_Call.
4719 function Reset_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
4720 -- Reset the Sloc of a node to that of the call itself, so that errors
4721 -- will be flagged on the call to the static expression function itself
4722 -- rather than on the expression of the function's declaration.
4724 --------------------
4725 -- Replace_Formal --
4726 --------------------
4728 function Replace_Formal
(N
: Node_Id
) return Traverse_Result
is
4733 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
4736 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
4737 A
:= Renamed_Object
(E
);
4739 if Nkind
(A
) = N_Defining_Identifier
then
4740 Rewrite
(N
, New_Occurrence_Of
(A
, Sloc
(N
)));
4745 Rewrite
(N
, New_Copy
(A
));
4756 procedure Replace_Formals
is new Traverse_Proc
(Replace_Formal
);
4762 function Reset_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
4764 Set_Sloc
(Nod
, Sloc
(N
));
4765 Set_Comes_From_Source
(Nod
, False);
4770 procedure Reset_Slocs
is new Traverse_Proc
(Reset_Sloc
);
4772 -- Start of processing for Inline_Static_Function_Call
4775 pragma Assert
(Is_Static_Function_Call
(N
));
4778 Decls
: constant List_Id
:= New_List
;
4779 Func_Expr
: constant Node_Id
:=
4780 Expression_Of_Expression_Function
(Subp
);
4781 Expr_Copy
: constant Node_Id
:= New_Copy_Tree
(Func_Expr
);
4784 -- Create a mapping from formals to actuals, also creating temps in
4785 -- Decls, when needed, to hold the actuals.
4787 Establish_Actual_Mapping_For_Inlined_Call
(N
, Subp
, Decls
, Func_Expr
);
4789 -- Ensure that the copy has the same parent as the call (this seems
4790 -- to matter when GNATprove_Mode is set and there are nested static
4791 -- calls; prevents blowups in Insert_Actions, though it's not clear
4792 -- exactly why this is needed???).
4794 Set_Parent
(Expr_Copy
, Parent
(N
));
4796 Insert_Actions
(N
, Decls
);
4798 -- Now substitute actuals for their corresponding formal references
4799 -- within the expression.
4801 Replace_Formals
(Expr_Copy
);
4803 Reset_Slocs
(Expr_Copy
);
4805 -- Apply a qualified expression with the function's result subtype,
4806 -- to ensure that we check the expression against any constraint
4807 -- or predicate, which will cause the call to be illegal if the
4808 -- folded expression doesn't satisfy them. (The predicate case
4809 -- might not get checked if the subtype hasn't been frozen yet,
4810 -- which can happen if this static expression happens to be what
4811 -- causes the freezing, because Has_Static_Predicate doesn't get
4812 -- set on the subtype until it's frozen and Build_Predicates is
4813 -- called. It's not clear how to address this case. ???)
4816 Make_Qualified_Expression
(Sloc
(Expr_Copy
),
4818 New_Occurrence_Of
(Etype
(N
), Sloc
(Expr_Copy
)),
4820 Relocate_Node
(Expr_Copy
)));
4822 Set_Etype
(Expr_Copy
, Etype
(N
));
4824 Analyze_And_Resolve
(Expr_Copy
, Etype
(N
));
4826 -- Finally rewrite the function call as the folded static result
4828 Rewrite
(N
, Expr_Copy
);
4830 -- Cleanup mapping between formals and actuals for other expansions
4832 Reset_Actual_Mapping_For_Inlined_Call
(Subp
);
4834 end Inline_Static_Function_Call
;
4836 ------------------------
4837 -- Instantiate_Bodies --
4838 ------------------------
4840 -- Generic bodies contain all the non-local references, so an
4841 -- instantiation does not need any more context than Standard
4842 -- itself, even if the instantiation appears in an inner scope.
4843 -- Generic associations have verified that the contract model is
4844 -- satisfied, so that any error that may occur in the analysis of
4845 -- the body is an internal error.
4847 procedure Instantiate_Bodies
is
4849 procedure Instantiate_Body
(Info
: Pending_Body_Info
);
4850 -- Instantiate a pending body
4852 ------------------------
4853 -- Instantiate_Body --
4854 ------------------------
4856 procedure Instantiate_Body
(Info
: Pending_Body_Info
) is
4860 -- If the instantiation node is absent, it has been removed as part
4861 -- of unreachable code.
4863 if No
(Info
.Inst_Node
) then
4866 -- If the instantiation node is a package body, this means that the
4867 -- instance is a compilation unit and the instantiation has already
4868 -- been performed by Build_Instance_Compilation_Unit_Nodes.
4870 elsif Nkind
(Info
.Inst_Node
) = N_Package_Body
then
4873 -- For other package instances, instantiate the body and register the
4874 -- finalization scope, if any, for subsequent generation of cleanups.
4876 elsif Nkind
(Info
.Inst_Node
) = N_Package_Instantiation
then
4878 -- If the enclosing finalization scope is a package body, set the
4879 -- In_Package_Body flag on its spec. This is required, in the case
4880 -- where the body contains other package instantiations that have
4881 -- a body, for Analyze_Package_Instantiation to compute a correct
4882 -- finalization scope.
4884 if Present
(Info
.Fin_Scop
)
4885 and then Ekind
(Info
.Fin_Scop
) = E_Package_Body
4887 Set_In_Package_Body
(Spec_Entity
(Info
.Fin_Scop
), True);
4890 Instantiate_Package_Body
(Info
);
4892 if Present
(Info
.Fin_Scop
) then
4893 Scop
:= Info
.Fin_Scop
;
4895 -- If the enclosing finalization scope is dynamic, the instance
4896 -- may have been relocated, for example if it was declared in a
4897 -- protected entry, protected subprogram, or task body.
4899 if Is_Dynamic_Scope
(Scop
) then
4901 Enclosing_Dynamic_Scope
(Defining_Entity
(Info
.Act_Decl
));
4904 Add_Scope_To_Clean
(Scop
);
4906 -- Reset the In_Package_Body flag if it was set above
4908 if Ekind
(Info
.Fin_Scop
) = E_Package_Body
then
4909 Set_In_Package_Body
(Spec_Entity
(Info
.Fin_Scop
), False);
4913 -- For subprogram instances, always instantiate the body
4916 Instantiate_Subprogram_Body
(Info
);
4918 end Instantiate_Body
;
4921 Info
: Pending_Body_Info
;
4923 -- Start of processing for Instantiate_Bodies
4926 if Serious_Errors_Detected
= 0 then
4927 Expander_Active
:= (Operating_Mode
= Opt
.Generate_Code
);
4928 Push_Scope
(Standard_Standard
);
4929 To_Clean
:= New_Elmt_List
;
4931 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4935 -- A body instantiation may generate additional instantiations, so
4936 -- the following loop must scan to the end of a possibly expanding
4937 -- set (that's why we cannot simply use a FOR loop here). We must
4938 -- also capture the element lest the set be entirely reallocated.
4941 if Back_End_Inlining
then
4942 while J
<= Called_Pending_Instantiations
.Last
4943 and then Serious_Errors_Detected
= 0
4945 K
:= Called_Pending_Instantiations
.Table
(J
);
4946 Info
:= Pending_Instantiations
.Table
(K
);
4947 Instantiate_Body
(Info
);
4953 while J
<= Pending_Instantiations
.Last
4954 and then Serious_Errors_Detected
= 0
4956 Info
:= Pending_Instantiations
.Table
(J
);
4957 Instantiate_Body
(Info
);
4963 -- Reset the table of instantiations. Additional instantiations
4964 -- may be added through inlining, when additional bodies are
4967 if Back_End_Inlining
then
4968 Called_Pending_Instantiations
.Init
;
4970 Pending_Instantiations
.Init
;
4973 -- We can now complete the cleanup actions of scopes that contain
4974 -- pending instantiations (skipped for generic units, since we
4975 -- never need any cleanups in generic units).
4978 and then not Is_Generic_Unit
(Main_Unit_Entity
)
4981 elsif Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4987 end Instantiate_Bodies
;
4993 function Is_Nested
(E
: Entity_Id
) return Boolean is
4998 while Scop
/= Standard_Standard
loop
4999 if Is_Subprogram
(Scop
) then
5002 elsif Ekind
(Scop
) = E_Task_Type
5003 or else Ekind
(Scop
) = E_Entry
5004 or else Ekind
(Scop
) = E_Entry_Family
5009 Scop
:= Scope
(Scop
);
5015 ------------------------
5016 -- List_Inlining_Info --
5017 ------------------------
5019 procedure List_Inlining_Info
is
5025 if not Debug_Flag_Dot_J
then
5029 -- Generate listing of calls inlined by the frontend
5031 if Present
(Inlined_Calls
) then
5033 Elmt
:= First_Elmt
(Inlined_Calls
);
5034 while Present
(Elmt
) loop
5037 if not In_Internal_Unit
(Nod
) then
5041 Write_Str
("List of calls inlined by the frontend");
5048 Write_Location
(Sloc
(Nod
));
5057 -- Generate listing of calls passed to the backend
5059 if Present
(Backend_Calls
) then
5062 Elmt
:= First_Elmt
(Backend_Calls
);
5063 while Present
(Elmt
) loop
5066 if not In_Internal_Unit
(Nod
) then
5070 Write_Str
("List of inlined calls passed to the backend");
5077 Write_Location
(Sloc
(Nod
));
5085 -- Generate listing of instances inlined for the backend
5087 if Present
(Backend_Instances
) then
5090 Elmt
:= First_Elmt
(Backend_Instances
);
5091 while Present
(Elmt
) loop
5094 if not In_Internal_Unit
(Nod
) then
5098 Write_Str
("List of instances inlined for the backend");
5105 Write_Location
(Sloc
(Nod
));
5113 -- Generate listing of subprograms passed to the backend
5115 if Present
(Backend_Inlined_Subps
) and then Back_End_Inlining
then
5118 Elmt
:= First_Elmt
(Backend_Inlined_Subps
);
5119 while Present
(Elmt
) loop
5122 if not In_Internal_Unit
(Nod
) then
5127 ("List of inlined subprograms passed to the backend");
5134 Write_Name
(Chars
(Nod
));
5136 Write_Location
(Sloc
(Nod
));
5145 -- Generate listing of subprograms that cannot be inlined by the backend
5147 if Present
(Backend_Not_Inlined_Subps
) and then Back_End_Inlining
then
5150 Elmt
:= First_Elmt
(Backend_Not_Inlined_Subps
);
5151 while Present
(Elmt
) loop
5154 if not In_Internal_Unit
(Nod
) then
5159 ("List of subprograms that cannot be inlined by backend");
5166 Write_Name
(Chars
(Nod
));
5168 Write_Location
(Sloc
(Nod
));
5176 end List_Inlining_Info
;
5184 Pending_Instantiations
.Release
;
5185 Pending_Instantiations
.Locked
:= True;
5186 Called_Pending_Instantiations
.Release
;
5187 Called_Pending_Instantiations
.Locked
:= True;
5188 Inlined_Bodies
.Release
;
5189 Inlined_Bodies
.Locked
:= True;
5191 Successors
.Locked
:= True;
5193 Inlined
.Locked
:= True;
5196 --------------------------------
5197 -- Remove_Aspects_And_Pragmas --
5198 --------------------------------
5200 procedure Remove_Aspects_And_Pragmas
(Body_Decl
: Node_Id
) is
5201 procedure Remove_Items
(List
: List_Id
);
5202 -- Remove all useless aspects/pragmas from a particular list
5208 procedure Remove_Items
(List
: List_Id
) is
5211 Next_Item
: Node_Id
;
5214 -- Traverse the list looking for an aspect specification or a pragma
5216 Item
:= First
(List
);
5217 while Present
(Item
) loop
5218 Next_Item
:= Next
(Item
);
5220 if Nkind
(Item
) = N_Aspect_Specification
then
5221 Item_Id
:= Identifier
(Item
);
5222 elsif Nkind
(Item
) = N_Pragma
then
5223 Item_Id
:= Pragma_Identifier
(Item
);
5228 if Present
(Item_Id
)
5229 and then Chars
(Item_Id
) in Name_Always_Terminates
5230 | Name_Contract_Cases
5233 | Name_Exceptional_Cases
5234 | Name_Postcondition
5236 | Name_Refined_Global
5237 | Name_Refined_Depends
5239 | Name_Subprogram_Variant
5252 -- Start of processing for Remove_Aspects_And_Pragmas
5255 Remove_Items
(Aspect_Specifications
(Body_Decl
));
5256 Remove_Items
(Declarations
(Body_Decl
));
5258 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
5259 -- in the body of the subprogram.
5261 Remove_Items
(Statements
(Handled_Statement_Sequence
(Body_Decl
)));
5262 end Remove_Aspects_And_Pragmas
;
5264 --------------------------
5265 -- Remove_Dead_Instance --
5266 --------------------------
5268 procedure Remove_Dead_Instance
(N
: Node_Id
) is
5270 for J
in 0 .. Pending_Instantiations
.Last
loop
5271 if Pending_Instantiations
.Table
(J
).Inst_Node
= N
then
5272 Pending_Instantiations
.Table
(J
).Inst_Node
:= Empty
;
5276 end Remove_Dead_Instance
;
5278 -------------------------------------------
5279 -- Reset_Actual_Mapping_For_Inlined_Call --
5280 -------------------------------------------
5282 procedure Reset_Actual_Mapping_For_Inlined_Call
(Subp
: Entity_Id
) is
5283 F
: Entity_Id
:= First_Formal
(Subp
);
5286 while Present
(F
) loop
5287 Set_Renamed_Object
(F
, Empty
);
5290 end Reset_Actual_Mapping_For_Inlined_Call
;