[testsuite] [i386] work around fails with --enable-frame-pointer
[official-gcc.git] / gcc / ada / inline.adb
blobf23100dbb1346bc5be6208bca3f657677f52338c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- I N L I N E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Alloc;
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;
41 with Lib; use Lib;
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;
59 with Table;
60 with Tbuild; use Tbuild;
61 with Uintp; use Uintp;
62 with Uname; use Uname;
64 with GNAT.HTable;
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
76 -- - instantiations
77 -- - package declarations
78 -- - task or protected object declarations
79 -- - some of the following statements:
80 -- - abort
81 -- - asynchronous-select
82 -- - conditional-entry-call
83 -- - delay-relative
84 -- - delay-until
85 -- - selective-accept
86 -- - timed-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,
156 Element => Int,
157 No_Element => -1,
158 Key => Node_Id,
159 Hash => Node_Hash,
160 Equal => "=");
162 -----------------
163 -- Node_Hash --
164 -----------------
166 function Node_Hash (Id : Node_Id) return Node_Header_Num is
167 begin
168 return Node_Header_Num (Id mod Node_Table_Size);
169 end Node_Hash;
171 --------------------
172 -- Inlined Bodies --
173 --------------------
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)
215 of Subp_Index;
217 type Succ_Index is new Nat;
218 No_Succ : constant Succ_Index := 0;
220 type Succ_Info is record
221 Subp : Subp_Index;
222 Next : Succ_Index;
223 end 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;
243 end record;
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
273 (N : Node_Id;
274 Subp : Entity_Id;
275 Decls : List_Id;
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
288 -- for a package.
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:
315 -- Always_Terminates
316 -- Contract_Cases
317 -- Global
318 -- Depends
319 -- Exceptional_Cases
320 -- Postcondition
321 -- Precondition
322 -- Refined_Global
323 -- Refined_Depends
324 -- Refined_Post
325 -- Subprogram_Variant
326 -- Test_Case
327 -- Unmodified
328 -- Unreferenced
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.
346 To_Clean : Elist_Id;
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
354 --------------
355 -- Add_Call --
356 --------------
358 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
359 P1 : constant Subp_Index := Add_Subp (Called);
360 P2 : Subp_Index;
361 J : Succ_Index;
363 begin
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
374 return;
375 end if;
377 J := Successors.Table (J).Next;
378 end loop;
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;
387 else
388 Inlined.Table (P1).Main_Call := True;
389 end if;
390 end Add_Call;
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
407 -- either
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
425 Body_Decl : Node_Id;
426 Body_Id : Entity_Id;
427 Spec_Decl : Node_Id;
429 begin
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
433 -- anything extra.
435 if Is_Expression_Function (Id) then
436 return True;
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.
453 return
454 Was_Expression_Function (Body_Decl)
455 and then Parent (Spec_Decl) = Parent (Body_Decl);
456 end if;
457 end if;
458 end if;
460 return False;
461 end Is_Non_Loading_Expression_Function;
463 -----------------
464 -- Must_Inline --
465 -----------------
467 function Must_Inline return Inline_Level_Type is
468 Scop : Entity_Id;
469 Comp : Node_Id;
471 begin
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
481 return Dont_Inline;
482 end if;
484 -- Otherwise lookup scope stack to outer scope
486 while Scope (Scop) /= Standard_Standard
487 and then not Is_Child_Unit (Scop)
488 loop
489 Scop := Scope (Scop);
490 end loop;
492 Comp := Parent (Scop);
493 while Nkind (Comp) /= N_Compilation_Unit loop
494 Comp := Parent (Comp);
495 end loop;
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))
502 then
503 Add_Call (E);
504 return Inline_Package;
505 end if;
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)
515 loop
516 if Is_Overloadable (Scop)
517 and then Is_Inlined (Scop)
518 and then not Is_Nested (Scop)
519 then
520 Add_Call (E, Scop);
522 if Inline_Level = 1 then
523 return Inline_Call;
524 else
525 return Inline_Package;
526 end if;
527 end if;
529 Scop := Scope (Scop);
530 end loop;
532 return Dont_Inline;
533 end Must_Inline;
535 Inst : Entity_Id;
536 Inst_Decl : Node_Id;
537 Level : Inline_Level_Type;
539 -- Start of processing for Add_Inlined_Body
541 begin
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)
551 then
552 return;
553 end if;
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
563 return;
564 end if;
566 -- If a previous call to the subprogram has been inlined, nothing to do
568 if Is_Called (E) then
569 return;
570 end if;
572 -- If the subprogram is an instance, then inline the instance
574 if Is_Generic_Instance (E) then
575 Add_Inlined_Instance (E);
576 end if;
578 -- Mark the subprogram as called
580 Set_Is_Called (E);
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))
592 then
593 Set_Needs_Debug_Info (E, False);
594 end if;
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
599 -- in the spec.
601 if Is_Non_Loading_Expression_Function (E) then
602 return;
603 end if;
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.
610 declare
611 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
613 begin
614 if Pack = E then
615 Inlined_Bodies.Increment_Last;
616 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
618 else
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
624 Inst := Scope (E);
626 while Present (Inst) and then Inst /= Standard_Standard loop
627 exit when Is_Generic_Instance (Inst);
628 Inst := Scope (Inst);
629 end loop;
631 if Present (Inst)
632 and then Is_Generic_Instance (Inst)
633 and then not Is_Called (Inst)
634 then
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)))
643 then
644 Set_Is_Called (Inst);
645 else
646 Add_Inlined_Instance (Inst);
647 end if;
648 end if;
649 end if;
651 -- If the unit containing E is an instance, nothing more to do
653 if Is_Generic_Instance (Pack) then
654 null;
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)
672 then
673 Set_Is_Inlined (Pack);
674 Inlined_Bodies.Increment_Last;
675 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
676 end if;
677 end if;
679 -- Ensure that Analyze_Inlined_Bodies will be invoked after
680 -- completing the analysis of the current unit.
682 Inline_Processing_Required := True;
683 end;
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);
692 Index : Int;
694 begin
695 -- This machinery is only used with back-end inlining
697 if not Back_End_Inlining then
698 return;
699 end if;
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);
709 if Index >= 0 then
710 Called_Pending_Instantiations.Append (Index);
711 else
712 pragma Assert (False);
713 null;
714 end if;
716 Set_Is_Called (E);
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
732 -- the backend.
734 -----------------------------------------
735 -- Register_Backend_Inlined_Subprogram --
736 -----------------------------------------
738 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
739 begin
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
748 begin
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
754 begin
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)
767 then
768 Register_Backend_Inlined_Subprogram (E);
770 if No (Last_Inlined) then
771 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
772 else
773 Set_Next_Inlined_Subprogram (Last_Inlined, E);
774 end if;
776 Last_Inlined := E;
778 else
779 Register_Backend_Not_Inlined_Subprogram (E);
780 end if;
781 end Add_Inlined_Subprogram;
783 --------------------------------
784 -- Add_Pending_Instantiation --
785 --------------------------------
787 procedure Add_Pending_Instantiation
788 (Inst : Node_Id;
789 Act_Decl : Node_Id;
790 Fin_Scop : Node_Id := Empty)
792 Act_Decl_Id : Entity_Id;
793 Index : Int;
795 begin
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;
804 end if;
806 -- Capture the body of the generic instantiation along with its context
807 -- for later processing by Instantiate_Bodies.
809 Pending_Instantiations.Append
810 ((Inst_Node => Inst,
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))
835 then
836 Called_Pending_Instantiations.Append (Index);
838 Set_Is_Called (Act_Decl_Id);
839 end if;
840 end if;
841 end Add_Pending_Instantiation;
843 ------------------------
844 -- Add_Scope_To_Clean --
845 ------------------------
847 procedure Add_Scope_To_Clean (Scop : Entity_Id) is
848 Elmt : Elmt_Id;
850 begin
851 Elmt := First_Elmt (To_Clean);
852 while Present (Elmt) loop
853 if Node (Elmt) = Scop then
854 return;
855 end if;
857 Next_Elmt (Elmt);
858 end loop;
860 Append_Elmt (Scop, To_Clean);
861 end Add_Scope_To_Clean;
863 --------------
864 -- Add_Subp --
865 --------------
867 function Add_Subp (E : Entity_Id) return Subp_Index is
868 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
869 J : Subp_Index;
871 procedure New_Entry;
872 -- Initialize entry in Inlined table
874 procedure New_Entry is
875 begin
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;
882 end New_Entry;
884 -- Start of processing for Add_Subp
886 begin
887 if Hash_Headers (Index) = No_Subp then
888 New_Entry;
889 Hash_Headers (Index) := Inlined.Last;
890 return Inlined.Last;
892 else
893 J := Hash_Headers (Index);
894 while J /= No_Subp loop
895 if Inlined.Table (J).Name = E then
896 return J;
897 else
898 Index := J;
899 J := Inlined.Table (J).Next;
900 end if;
901 end loop;
903 -- On exit, subprogram was not found. Enter in table. Index is
904 -- the current last entry on the hash chain.
906 New_Entry;
907 Inlined.Table (Index).Next := Inlined.Last;
908 return Inlined.Last;
909 end if;
910 end Add_Subp;
912 ----------------------------
913 -- Analyze_Inlined_Bodies --
914 ----------------------------
916 procedure Analyze_Inlined_Bodies is
917 Comp_Unit : Node_Id;
918 J : Int;
919 Pack : Entity_Id;
920 Subp : Subp_Index;
921 S : Succ_Index;
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
936 begin
937 if Serious_Errors_Detected = 0 then
938 Push_Scope (Standard_Standard);
940 J := 0;
941 while J <= Inlined_Bodies.Last
942 and then Serious_Errors_Detected = 0
943 loop
944 Pack := Inlined_Bodies.Table (J);
945 while Present (Pack)
946 and then Scope (Pack) /= Standard_Standard
947 and then not Is_Child_Unit (Pack)
948 loop
949 Pack := Scope (Pack);
950 end loop;
952 Comp_Unit := Parent (Pack);
953 while Present (Comp_Unit)
954 and then Nkind (Comp_Unit) /= N_Compilation_Unit
955 loop
956 Comp_Unit := Parent (Comp_Unit);
957 end loop;
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)
966 and then
967 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
968 or else
969 (No (Corresponding_Body (Unit (Comp_Unit)))
970 and then Body_Needed_For_Inlining
971 (Defining_Entity (Unit (Comp_Unit)))))
972 then
973 declare
974 Bname : constant Unit_Name_Type :=
975 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
977 OK : Boolean;
979 begin
980 if not Is_Loaded (Bname) then
981 Style_Check := False;
982 Load_Needed_Body (Comp_Unit, OK);
984 if not OK then
986 -- Warn that a body was not available for inlining
987 -- by the back-end.
989 Error_Msg_Unit_1 := Bname;
990 Error_Msg_N
991 ("one or more inlined subprograms accessed in $!??",
992 Comp_Unit);
993 Error_Msg_File_1 :=
994 Get_File_Name (Bname, Subunit => False);
995 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
996 end if;
997 end if;
998 end;
999 end if;
1001 J := J + 1;
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.
1009 Instantiate_Bodies;
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.
1014 end if;
1015 end loop;
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;
1037 else
1038 Set_Is_Called (Inlined.Table (Index).Name, False);
1039 end if;
1040 end loop;
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;
1059 end if;
1061 S := Successors.Table (S).Next;
1062 end loop;
1063 end loop;
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
1069 declare
1070 E : constant Subprogram_Kind_Id := Inlined.Table (Index).Name;
1072 begin
1073 if Is_Called (E) and then not Is_Ignored_Ghost_Entity (E) then
1074 Add_Inlined_Subprogram (E);
1075 end if;
1076 end;
1077 end loop;
1079 Pop_Scope;
1080 end if;
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
1095 -- statement.
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
1127 ------------------
1128 -- Check_Return --
1129 ------------------
1131 function Check_Return (N : Node_Id) return Traverse_Result is
1132 begin
1133 case Nkind (N) is
1134 when N_Extended_Return_Statement =>
1135 return Abandon;
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
1142 return OK;
1143 else
1144 return Skip;
1145 end if;
1147 when others =>
1148 return OK;
1149 end case;
1150 end Check_Return;
1152 function Check_All_Returns is new Traverse_Func (Check_Return);
1154 -- Start of processing for Has_Extended_Return
1156 begin
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
1165 S : Entity_Id;
1167 begin
1168 S := Current_Scope;
1169 while Present (S) loop
1170 if Is_Compilation_Unit (S)
1171 or else Is_Child_Unit (S)
1172 then
1173 return False;
1175 elsif Ekind (S) = E_Package
1176 and then Has_Forward_Instantiation (S)
1177 then
1178 return True;
1179 end if;
1181 S := Scope (S);
1182 end loop;
1184 return False;
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.
1199 ------------------
1200 -- Check_Return --
1201 ------------------
1203 function Check_Return (N : Node_Id) return Traverse_Result is
1204 begin
1205 case Nkind (N) is
1206 when N_Extended_Return_Statement
1207 | N_Simple_Return_Statement
1209 if N = Last_Statement then
1210 return OK;
1211 else
1212 return Abandon;
1213 end if;
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
1220 return OK;
1221 else
1222 return Skip;
1223 end if;
1225 when others =>
1226 return OK;
1227 end case;
1228 end Check_Return;
1230 function Check_All_Returns is new Traverse_Func (Check_Return);
1232 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1234 begin
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
1253 ----------------
1254 -- Check_Call --
1255 ----------------
1257 function Check_Call (N : Node_Id) return Traverse_Result is
1258 begin
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))))
1263 then
1264 Cannot_Inline
1265 ("cannot inline & (call returns unconstrained type)?",
1266 N, Spec_Id);
1267 return Abandon;
1268 else
1269 return OK;
1270 end if;
1271 end Check_Call;
1273 function Check_Calls is new Traverse_Func (Check_Call);
1275 begin
1276 return Check_Calls (Bod) = Abandon;
1277 end Uses_Secondary_Stack;
1279 -- Start of processing for Build_Body_To_Inline
1281 begin
1282 -- Return immediately if done already
1284 if Nkind (Decl) = N_Subprogram_Declaration
1285 and then Present (Body_To_Inline (Decl))
1286 then
1287 return;
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
1295 then
1296 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1297 return;
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))
1305 then
1306 Cannot_Inline
1307 ("cannot inline & (controlled return type)?", N, Spec_Id);
1308 return;
1309 end if;
1311 if Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
1312 return;
1313 end if;
1315 if Present (Handled_Statement_Sequence (N)) then
1316 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1317 Cannot_Inline
1318 ("cannot inline& (exception handler)?",
1319 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1320 Spec_Id);
1321 return;
1323 elsif Has_Excluded_Statement
1324 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1325 then
1326 return;
1327 end if;
1328 end if;
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,
1333 -- handlers, etc).
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
1338 then
1339 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1340 return;
1341 end if;
1343 if Has_Pending_Instantiation then
1344 Cannot_Inline
1345 ("cannot inline& (forward instance within enclosing body)?",
1346 N, Spec_Id);
1347 return;
1348 end if;
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);
1360 else
1361 Original_Body := Copy_Separate_Tree (N);
1362 end if;
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);
1382 Body_To_Analyze :=
1383 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1385 -- Set return type of function, which is also global and does not need
1386 -- to be resolved.
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)));
1392 end if;
1394 if No (Declarations (N)) then
1395 Set_Declarations (N, New_List (Body_To_Analyze));
1396 else
1397 Append (Body_To_Analyze, Declarations (N));
1398 end if;
1400 Start_Generic;
1402 Analyze (Body_To_Analyze);
1403 Push_Scope (Defining_Entity (Body_To_Analyze));
1404 Save_Global_References (Original_Body);
1405 End_Scope;
1406 Remove (Body_To_Analyze);
1408 End_Generic;
1410 -- Restore environment if previously saved
1412 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1413 Restore_Env;
1414 end if;
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))
1427 then
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
1433 -- implementation.
1435 or else (Returns_Unconstrained_Type (Spec_Id)
1436 and then Has_Extended_Return)
1437 then
1438 Cannot_Inline
1439 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1440 return;
1441 end if;
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
1447 return;
1448 end if;
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
1460 (N : Node_Id;
1461 Subp : Entity_Id) return Boolean
1463 F : Entity_Id;
1464 A : Node_Id;
1466 begin
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))
1472 and then
1473 (Is_By_Reference_Type (Etype (A))
1474 or else Is_Limited_Type (Etype (A)))
1475 then
1476 return False;
1477 end if;
1479 Next_Formal (F);
1480 Next_Actual (A);
1481 end loop;
1483 return True;
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
1504 -- types.
1506 function Has_Skip_Proof_Annotation (Id : Entity_Id) return Boolean;
1507 -- Returns True if subprogram Id has an annotation Skip_Proof or
1508 -- Skip_Flow_And_Proof.
1510 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1511 -- Return True if subprogram Id has any contract. The presence of
1512 -- Extensions_Visible or Volatile_Function is also considered as a
1513 -- contract here.
1515 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1516 -- Return True if subprogram Id defines a compilation unit
1518 function In_Package_Spec (Id : Entity_Id) return Boolean;
1519 -- Return True if subprogram Id is defined in the package specification,
1520 -- either its visible or private part.
1522 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean;
1523 -- Return True if subprogram Id could be a traversal function, as
1524 -- defined in SPARK RM 3.10. This is only a safe approximation, as the
1525 -- knowledge of the SPARK boundary is needed to determine exactly
1526 -- traversal functions.
1528 ---------------------------------------
1529 -- Has_Formal_Or_Result_Of_Deep_Type --
1530 ---------------------------------------
1532 function Has_Formal_Or_Result_Of_Deep_Type
1533 (Id : Entity_Id) return Boolean
1535 function Is_Deep (Typ : Entity_Id) return Boolean;
1536 -- Return True if Typ is deep: either an access type or a composite
1537 -- type containing an access type.
1539 -------------
1540 -- Is_Deep --
1541 -------------
1543 function Is_Deep (Typ : Entity_Id) return Boolean is
1544 begin
1545 case Type_Kind'(Ekind (Typ)) is
1546 when Access_Kind =>
1547 return True;
1549 when E_Array_Type
1550 | E_Array_Subtype
1552 return Is_Deep (Component_Type (Typ));
1554 when Record_Kind =>
1555 declare
1556 Comp : Entity_Id := First_Component_Or_Discriminant (Typ);
1557 begin
1558 while Present (Comp) loop
1559 if Is_Deep (Etype (Comp)) then
1560 return True;
1561 end if;
1562 Next_Component_Or_Discriminant (Comp);
1563 end loop;
1564 end;
1565 return False;
1567 when Scalar_Kind
1568 | E_String_Literal_Subtype
1569 | Concurrent_Kind
1570 | Incomplete_Kind
1571 | E_Exception_Type
1572 | E_Subprogram_Type
1574 return False;
1576 when E_Private_Type
1577 | E_Private_Subtype
1578 | E_Limited_Private_Type
1579 | E_Limited_Private_Subtype
1581 -- Conservatively consider that the type might be deep if
1582 -- its completion has not been seen yet.
1584 if No (Underlying_Type (Typ)) then
1585 return True;
1587 -- Do not peek under a private type if its completion has
1588 -- SPARK_Mode Off. In such a case, a deep type is considered
1589 -- by GNATprove to be not deep.
1591 elsif Present (Full_View (Typ))
1592 and then Present (SPARK_Pragma (Full_View (Typ)))
1593 and then Get_SPARK_Mode_From_Annotation
1594 (SPARK_Pragma (Full_View (Typ))) = Off
1595 then
1596 return False;
1598 -- Otherwise peek under the private type.
1600 else
1601 return Is_Deep (Underlying_Type (Typ));
1602 end if;
1603 end case;
1604 end Is_Deep;
1606 -- Local variables
1608 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1609 Formal : Entity_Id;
1610 Formal_Typ : Entity_Id;
1612 -- Start of processing for Has_Formal_Or_Result_Of_Deep_Type
1614 begin
1615 -- Inspect all parameters of the subprogram looking for a formal
1616 -- of a deep type.
1618 Formal := First_Formal (Subp_Id);
1619 while Present (Formal) loop
1620 Formal_Typ := Etype (Formal);
1622 if Is_Deep (Formal_Typ) then
1623 return True;
1624 end if;
1626 Next_Formal (Formal);
1627 end loop;
1629 -- Check whether this is a function whose return type is deep
1631 if Ekind (Subp_Id) = E_Function
1632 and then Is_Deep (Etype (Subp_Id))
1633 then
1634 return True;
1635 end if;
1637 return False;
1638 end Has_Formal_Or_Result_Of_Deep_Type;
1640 ---------------------------------------------------
1641 -- Has_Formal_With_Discriminant_Dependent_Fields --
1642 ---------------------------------------------------
1644 function Has_Formal_With_Discriminant_Dependent_Fields
1645 (Id : Entity_Id) return Boolean
1647 function Has_Discriminant_Dependent_Component
1648 (Typ : Entity_Id) return Boolean;
1649 -- Determine whether unconstrained record type Typ has at least one
1650 -- component that depends on a discriminant.
1652 ------------------------------------------
1653 -- Has_Discriminant_Dependent_Component --
1654 ------------------------------------------
1656 function Has_Discriminant_Dependent_Component
1657 (Typ : Entity_Id) return Boolean
1659 Comp : Entity_Id;
1661 begin
1662 -- Inspect all components of the record type looking for one that
1663 -- depends on a discriminant.
1665 Comp := First_Component (Typ);
1666 while Present (Comp) loop
1667 if Has_Discriminant_Dependent_Constraint (Comp) then
1668 return True;
1669 end if;
1671 Next_Component (Comp);
1672 end loop;
1674 return False;
1675 end Has_Discriminant_Dependent_Component;
1677 -- Local variables
1679 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1680 Formal : Entity_Id;
1681 Formal_Typ : Entity_Id;
1683 -- Start of processing for
1684 -- Has_Formal_With_Discriminant_Dependent_Fields
1686 begin
1687 -- Inspect all parameters of the subprogram looking for a formal
1688 -- of an unconstrained record type with at least one discriminant
1689 -- dependent component.
1691 Formal := First_Formal (Subp_Id);
1692 while Present (Formal) loop
1693 Formal_Typ := Etype (Formal);
1695 if Is_Record_Type (Formal_Typ)
1696 and then not Is_Constrained (Formal_Typ)
1697 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1698 then
1699 return True;
1700 end if;
1702 Next_Formal (Formal);
1703 end loop;
1705 return False;
1706 end Has_Formal_With_Discriminant_Dependent_Fields;
1708 -------------------------------
1709 -- Has_Skip_Proof_Annotation --
1710 -------------------------------
1712 function Has_Skip_Proof_Annotation (Id : Entity_Id) return Boolean is
1713 Decl : Node_Id := Unit_Declaration_Node (Id);
1715 begin
1716 Next (Decl);
1718 while Present (Decl)
1719 and then Nkind (Decl) = N_Pragma
1720 loop
1721 if Get_Pragma_Id (Decl) = Pragma_Annotate
1722 and then List_Length (Pragma_Argument_Associations (Decl)) = 3
1723 then
1724 declare
1725 Arg1 : constant Node_Id :=
1726 First (Pragma_Argument_Associations (Decl));
1727 Arg2 : constant Node_Id := Next (Arg1);
1728 Arg1_Name : constant String :=
1729 Get_Name_String (Chars (Get_Pragma_Arg (Arg1)));
1730 Arg2_Name : constant String :=
1731 Get_Name_String (Chars (Get_Pragma_Arg (Arg2)));
1732 begin
1733 if Arg1_Name = "gnatprove"
1734 and then Arg2_Name in "skip_proof" | "skip_flow_and_proof"
1735 then
1736 return True;
1737 end if;
1738 end;
1739 end if;
1741 Next (Decl);
1742 end loop;
1744 return False;
1745 end Has_Skip_Proof_Annotation;
1747 -----------------------
1748 -- Has_Some_Contract --
1749 -----------------------
1751 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1752 Items : Node_Id;
1754 begin
1755 -- A call to an expression function may precede the actual body which
1756 -- is inserted at the end of the enclosing declarations. Ensure that
1757 -- the related entity is decorated before inspecting the contract.
1759 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1760 Items := Contract (Id);
1762 -- Note that Classifications is not Empty when Extensions_Visible
1763 -- or Volatile_Function is present, which causes such subprograms
1764 -- to be considered to have a contract here. This is fine as we
1765 -- want to avoid inlining these too.
1767 return Present (Items)
1768 and then (Present (Pre_Post_Conditions (Items)) or else
1769 Present (Contract_Test_Cases (Items)) or else
1770 Present (Classifications (Items)));
1771 end if;
1773 return False;
1774 end Has_Some_Contract;
1776 ---------------------
1777 -- In_Package_Spec --
1778 ---------------------
1780 function In_Package_Spec (Id : Entity_Id) return Boolean is
1781 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1782 -- Parent of the subprogram's declaration
1784 begin
1785 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1786 end In_Package_Spec;
1788 ------------------------
1789 -- Is_Unit_Subprogram --
1790 ------------------------
1792 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1793 Decl : Node_Id := Parent (Parent (Id));
1794 begin
1795 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1796 Decl := Parent (Decl);
1797 end if;
1799 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1800 end Is_Unit_Subprogram;
1802 ------------------------------
1803 -- Maybe_Traversal_Function --
1804 ------------------------------
1806 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean is
1807 begin
1808 return Ekind (Id) = E_Function
1810 -- Only traversal functions return an anonymous access-to-object
1811 -- type in SPARK.
1813 and then Is_Anonymous_Access_Type (Etype (Id));
1814 end Maybe_Traversal_Function;
1816 -- Local declarations
1818 Id : Entity_Id;
1819 -- Procedure or function entity for the subprogram
1821 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1823 begin
1824 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1826 if Present (Spec_Id) then
1827 Id := Spec_Id;
1828 else
1829 Id := Body_Id;
1830 end if;
1832 -- Only local subprograms without contracts are inlined in GNATprove
1833 -- mode, as these are the subprograms which a user is not interested in
1834 -- analyzing in isolation, but rather in the context of their call. This
1835 -- is a convenient convention, that could be changed for an explicit
1836 -- pragma/aspect one day.
1838 -- In a number of special cases, inlining is not desirable or not
1839 -- possible, see below.
1841 -- Do not inline unit-level subprograms
1843 if Is_Unit_Subprogram (Id) then
1844 return False;
1846 -- Do not inline subprograms declared in package specs, because they are
1847 -- not local, i.e. can be called either from anywhere (if declared in
1848 -- visible part) or from the child units (if declared in private part).
1850 elsif In_Package_Spec (Id) then
1851 return False;
1853 -- Do not inline subprograms declared in other units. This is important
1854 -- in particular for subprograms defined in the private part of a
1855 -- package spec, when analyzing one of its child packages, as otherwise
1856 -- we issue spurious messages about the impossibility to inline such
1857 -- calls.
1859 elsif not In_Extended_Main_Code_Unit (Id) then
1860 return False;
1862 -- Do not inline dispatching operations, as only their static calls
1863 -- can be analyzed in context, and not their dispatching calls.
1865 elsif Is_Dispatching_Operation (Id) then
1866 return False;
1868 -- Do not inline subprograms marked No_Return, possibly used for
1869 -- signaling errors, which GNATprove handles specially.
1871 elsif No_Return (Id) then
1872 return False;
1874 -- Do not inline subprograms that have a contract on the spec or the
1875 -- body. Use the contract(s) instead in GNATprove. This also prevents
1876 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1878 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1879 or else
1880 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1881 then
1882 return False;
1884 -- Do not inline expression functions, which are directly inlined at the
1885 -- prover level.
1887 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1888 or else
1889 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1890 then
1891 return False;
1893 -- Do not inline generic subprogram instances. The visibility rules of
1894 -- generic instances plays badly with inlining.
1896 elsif Is_Generic_Instance (Spec_Id) then
1897 return False;
1899 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1900 -- the subprogram body, a similar check is performed after the body
1901 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1903 elsif Present (Spec_Id)
1904 and then
1905 (No (SPARK_Pragma (Spec_Id))
1906 or else
1907 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1908 then
1909 return False;
1911 -- Do not inline subprograms and entries defined inside protected types,
1912 -- which typically are not helper subprograms, which also avoids getting
1913 -- spurious messages on calls that cannot be inlined.
1915 elsif Within_Protected_Type (Id) then
1916 return False;
1918 -- Do not inline predicate functions (treated specially by GNATprove)
1920 elsif Is_Predicate_Function (Id) then
1921 return False;
1923 -- Do not inline subprograms with a parameter of an unconstrained
1924 -- record type if it has discrimiant dependent fields. Indeed, with
1925 -- such parameters, the frontend cannot always ensure type compliance
1926 -- in record component accesses (in particular with records containing
1927 -- packed arrays).
1929 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1930 return False;
1932 -- Do not inline subprograms with a formal parameter or return type of
1933 -- a deep type, as in that case inlining might generate code that
1934 -- violates borrow-checking rules of SPARK 3.10 even if the original
1935 -- code did not.
1937 elsif Has_Formal_Or_Result_Of_Deep_Type (Id) then
1938 return False;
1940 -- Do not inline subprograms which may be traversal functions. Such
1941 -- inlining introduces temporary variables of named access type for
1942 -- which assignments are move instead of borrow/observe, possibly
1943 -- leading to spurious errors when checking SPARK rules related to
1944 -- pointer usage.
1946 elsif Maybe_Traversal_Function (Id) then
1947 return False;
1949 -- Do not inline subprograms with the Skip_Proof or Skip_Flow_And_Proof
1950 -- annotation, which should be handled separately.
1952 elsif Has_Skip_Proof_Annotation (Id) then
1953 return False;
1955 -- Otherwise, this is a subprogram declared inside the private part of a
1956 -- package, or inside a package body, or locally in a subprogram, and it
1957 -- does not have any contract. Inline it.
1959 else
1960 return True;
1961 end if;
1962 end Can_Be_Inlined_In_GNATprove_Mode;
1964 -------------------
1965 -- Cannot_Inline --
1966 -------------------
1968 procedure Cannot_Inline
1969 (Msg : String;
1970 N : Node_Id;
1971 Subp : Entity_Id;
1972 Is_Serious : Boolean := False;
1973 Suppress_Info : Boolean := False)
1975 begin
1976 -- In GNATprove mode, inlining is the technical means by which the
1977 -- higher-level goal of contextual analysis is reached, so issue
1978 -- messages about failure to apply contextual analysis to a
1979 -- subprogram, rather than failure to inline it.
1981 if GNATprove_Mode
1982 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1983 then
1984 declare
1985 Len1 : constant Positive :=
1986 String'("cannot inline")'Length;
1987 Len2 : constant Positive :=
1988 String'("info: no contextual analysis of")'Length;
1990 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1992 begin
1993 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1994 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1995 Msg (Msg'First + Len1 .. Msg'Last);
1996 Cannot_Inline (New_Msg, N, Subp, Is_Serious, Suppress_Info);
1997 return;
1998 end;
1999 end if;
2001 pragma Assert (Msg (Msg'Last) = '?');
2003 -- Legacy front-end inlining model
2005 if not Back_End_Inlining then
2007 -- Do not emit warning if this is a predefined unit which is not
2008 -- the main unit. With validity checks enabled, some predefined
2009 -- subprograms may contain nested subprograms and become ineligible
2010 -- for inlining.
2012 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2013 and then not In_Extended_Main_Source_Unit (Subp)
2014 then
2015 null;
2017 -- In GNATprove mode, issue an info message when -gnatd_f is set and
2018 -- Suppress_Info is False, and indicate that the subprogram is not
2019 -- always inlined by setting flag Is_Inlined_Always to False.
2021 elsif GNATprove_Mode then
2022 Set_Is_Inlined_Always (Subp, False);
2024 if Debug_Flag_Underscore_F and not Suppress_Info then
2025 Error_Msg_NE (Msg, N, Subp);
2026 end if;
2028 elsif Has_Pragma_Inline_Always (Subp) then
2030 -- Remove last character (question mark) to make this into an
2031 -- error, because the Inline_Always pragma cannot be obeyed.
2033 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2035 elsif Ineffective_Inline_Warnings then
2036 Error_Msg_NE (Msg & "p?", N, Subp);
2037 end if;
2039 -- New semantics relying on back-end inlining
2041 elsif Is_Serious then
2043 -- Remove last character (question mark) to make this into an error.
2045 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2047 else
2049 -- Do not emit warning if this is a predefined unit which is not
2050 -- the main unit. This behavior is currently provided for backward
2051 -- compatibility but it will be removed when we enforce the
2052 -- strictness of the new rules.
2054 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2055 and then not In_Extended_Main_Source_Unit (Subp)
2056 then
2057 null;
2059 elsif Has_Pragma_Inline_Always (Subp) then
2061 -- Emit a warning if this is a call to a runtime subprogram
2062 -- which is located inside a generic. Previously this call
2063 -- was silently skipped.
2065 if Is_Generic_Instance (Subp) then
2066 declare
2067 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
2068 begin
2069 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
2070 Set_Is_Inlined (Subp, False);
2071 Error_Msg_NE (Msg & "p?", N, Subp);
2072 return;
2073 end if;
2074 end;
2075 end if;
2077 -- Remove last character (question mark) to make this into an
2078 -- error, because the Inline_Always pragma cannot be obeyed.
2080 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2082 else
2083 Set_Is_Inlined (Subp, False);
2085 if Ineffective_Inline_Warnings then
2086 Error_Msg_NE (Msg & "p?", N, Subp);
2087 end if;
2088 end if;
2089 end if;
2090 end Cannot_Inline;
2092 --------------------------------------------
2093 -- Check_And_Split_Unconstrained_Function --
2094 --------------------------------------------
2096 procedure Check_And_Split_Unconstrained_Function
2097 (N : Node_Id;
2098 Spec_Id : Entity_Id;
2099 Body_Id : Entity_Id)
2101 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
2102 -- Use generic machinery to build an unexpanded body for the subprogram.
2103 -- This body is subsequently used for inline expansions at call sites.
2105 procedure Build_Return_Object_Formal
2106 (Loc : Source_Ptr;
2107 Obj_Decl : Node_Id;
2108 Formals : List_Id);
2109 -- Create a formal parameter for return object declaration Obj_Decl of
2110 -- an extended return statement and add it to list Formals.
2112 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
2113 -- Return true if we generate code for the function body N, the function
2114 -- body N has no local declarations and its unique statement is a single
2115 -- extended return statement with a handled statements sequence.
2117 procedure Copy_Formals
2118 (Loc : Source_Ptr;
2119 Subp_Id : Entity_Id;
2120 Formals : List_Id);
2121 -- Create new formal parameters from the formal parameters of subprogram
2122 -- Subp_Id and add them to list Formals.
2124 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id;
2125 -- Create a copy of return object declaration Obj_Decl of an extended
2126 -- return statement.
2128 procedure Split_Unconstrained_Function
2129 (N : Node_Id;
2130 Spec_Id : Entity_Id);
2131 -- N is an inlined function body that returns an unconstrained type and
2132 -- has a single extended return statement. Split N in two subprograms:
2133 -- a procedure P' and a function F'. The formals of P' duplicate the
2134 -- formals of N plus an extra formal which is used to return a value;
2135 -- its body is composed by the declarations and list of statements
2136 -- of the extended return statement of N.
2138 --------------------------
2139 -- Build_Body_To_Inline --
2140 --------------------------
2142 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
2143 procedure Generate_Subprogram_Body
2144 (N : Node_Id;
2145 Body_To_Inline : out Node_Id);
2146 -- Generate a parameterless duplicate of subprogram body N. Note that
2147 -- occurrences of pragmas referencing the formals are removed since
2148 -- they have no meaning when the body is inlined and the formals are
2149 -- rewritten (the analysis of the non-inlined body will handle these
2150 -- pragmas). A new internal name is associated with Body_To_Inline.
2152 ------------------------------
2153 -- Generate_Subprogram_Body --
2154 ------------------------------
2156 procedure Generate_Subprogram_Body
2157 (N : Node_Id;
2158 Body_To_Inline : out Node_Id)
2160 begin
2161 -- Within an instance, the body to inline must be treated as a
2162 -- nested generic so that proper global references are preserved.
2164 -- Note that we do not do this at the library level, because it
2165 -- is not needed, and furthermore this causes trouble if front
2166 -- end inlining is activated (-gnatN).
2168 if In_Instance
2169 and then Scope (Current_Scope) /= Standard_Standard
2170 then
2171 Body_To_Inline :=
2172 Copy_Generic_Node (N, Empty, Instantiating => True);
2173 else
2174 Body_To_Inline := New_Copy_Tree (N);
2175 end if;
2177 -- Remove aspects/pragmas that have no meaning in an inlined body
2179 Remove_Aspects_And_Pragmas (Body_To_Inline);
2181 -- We need to capture references to the formals in order
2182 -- to substitute the actuals at the point of inlining, i.e.
2183 -- instantiation. To treat the formals as globals to the body to
2184 -- inline, we nest it within a dummy parameterless subprogram,
2185 -- declared within the real one.
2187 Set_Parameter_Specifications
2188 (Specification (Body_To_Inline), No_List);
2190 -- A new internal name is associated with Body_To_Inline to avoid
2191 -- conflicts when the non-inlined body N is analyzed.
2193 Set_Defining_Unit_Name (Specification (Body_To_Inline),
2194 Make_Temporary (Sloc (N), 'P'));
2195 Set_Corresponding_Spec (Body_To_Inline, Empty);
2196 end Generate_Subprogram_Body;
2198 -- Local variables
2200 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2201 Original_Body : Node_Id;
2202 Body_To_Analyze : Node_Id;
2204 -- Start of processing for Build_Body_To_Inline
2206 begin
2207 pragma Assert (Current_Scope = Spec_Id);
2209 -- Within an instance, the body to inline must be treated as a nested
2210 -- generic, so that the proper global references are preserved. We
2211 -- do not do this at the library level, because it is not needed, and
2212 -- furthermore this causes trouble if front-end inlining is activated
2213 -- (-gnatN).
2215 if In_Instance
2216 and then Scope (Current_Scope) /= Standard_Standard
2217 then
2218 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2219 end if;
2221 -- Capture references to formals in order to substitute the actuals
2222 -- at the point of inlining or instantiation. To treat the formals
2223 -- as globals to the body to inline, nest the body within a dummy
2224 -- parameterless subprogram, declared within the real one.
2226 Generate_Subprogram_Body (N, Original_Body);
2227 Body_To_Analyze :=
2228 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
2230 -- Set return type of function, which is also global and does not
2231 -- need to be resolved.
2233 if Ekind (Spec_Id) = E_Function then
2234 Set_Result_Definition (Specification (Body_To_Analyze),
2235 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
2236 end if;
2238 if No (Declarations (N)) then
2239 Set_Declarations (N, New_List (Body_To_Analyze));
2240 else
2241 Append_To (Declarations (N), Body_To_Analyze);
2242 end if;
2244 Preanalyze (Body_To_Analyze);
2246 Push_Scope (Defining_Entity (Body_To_Analyze));
2247 Save_Global_References (Original_Body);
2248 End_Scope;
2249 Remove (Body_To_Analyze);
2251 -- Restore environment if previously saved
2253 if In_Instance
2254 and then Scope (Current_Scope) /= Standard_Standard
2255 then
2256 Restore_Env;
2257 end if;
2259 pragma Assert (No (Body_To_Inline (Decl)));
2260 Set_Body_To_Inline (Decl, Original_Body);
2261 Mutate_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
2262 end Build_Body_To_Inline;
2264 --------------------------------
2265 -- Build_Return_Object_Formal --
2266 --------------------------------
2268 procedure Build_Return_Object_Formal
2269 (Loc : Source_Ptr;
2270 Obj_Decl : Node_Id;
2271 Formals : List_Id)
2273 Obj_Def : constant Node_Id := Object_Definition (Obj_Decl);
2274 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2275 Typ_Def : Node_Id;
2277 begin
2278 -- Build the type definition of the formal parameter. The use of
2279 -- New_Copy_Tree ensures that global references preserved in the
2280 -- case of generics.
2282 if Is_Entity_Name (Obj_Def) then
2283 Typ_Def := New_Copy_Tree (Obj_Def);
2284 else
2285 Typ_Def := New_Copy_Tree (Subtype_Mark (Obj_Def));
2286 end if;
2288 -- Generate:
2290 -- Obj_Id : [out] Typ_Def
2292 -- Mode OUT should not be used when the return object is declared as
2293 -- a constant. Check the definition of the object declaration because
2294 -- the object has not been analyzed yet.
2296 Append_To (Formals,
2297 Make_Parameter_Specification (Loc,
2298 Defining_Identifier =>
2299 Make_Defining_Identifier (Loc, Chars (Obj_Id)),
2300 In_Present => False,
2301 Out_Present => not Constant_Present (Obj_Decl),
2302 Null_Exclusion_Present => False,
2303 Parameter_Type => Typ_Def));
2304 end Build_Return_Object_Formal;
2306 --------------------------------------
2307 -- Can_Split_Unconstrained_Function --
2308 --------------------------------------
2310 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
2311 Stmt : constant Node_Id :=
2312 First (Statements (Handled_Statement_Sequence (N)));
2313 Decl : Node_Id;
2315 begin
2316 -- No user defined declarations allowed in the function except inside
2317 -- the unique return statement; implicit labels are the only allowed
2318 -- declarations.
2320 Decl := First (Declarations (N));
2321 while Present (Decl) loop
2322 if Nkind (Decl) /= N_Implicit_Label_Declaration then
2323 return False;
2324 end if;
2326 Next (Decl);
2327 end loop;
2329 -- We only split the inlined function when we are generating the code
2330 -- of its body; otherwise we leave duplicated split subprograms in
2331 -- the tree which (if referenced) generate wrong references at link
2332 -- time.
2334 return In_Extended_Main_Code_Unit (N)
2335 and then Present (Stmt)
2336 and then Nkind (Stmt) = N_Extended_Return_Statement
2337 and then No (Next (Stmt))
2338 and then Present (Handled_Statement_Sequence (Stmt));
2339 end Can_Split_Unconstrained_Function;
2341 ------------------
2342 -- Copy_Formals --
2343 ------------------
2345 procedure Copy_Formals
2346 (Loc : Source_Ptr;
2347 Subp_Id : Entity_Id;
2348 Formals : List_Id)
2350 Formal : Entity_Id;
2351 Spec : Node_Id;
2353 begin
2354 Formal := First_Formal (Subp_Id);
2355 while Present (Formal) loop
2356 Spec := Parent (Formal);
2358 -- Create an exact copy of the formal parameter. The use of
2359 -- New_Copy_Tree ensures that global references are preserved
2360 -- in case of generics.
2362 Append_To (Formals,
2363 Make_Parameter_Specification (Loc,
2364 Defining_Identifier =>
2365 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
2366 In_Present => In_Present (Spec),
2367 Out_Present => Out_Present (Spec),
2368 Null_Exclusion_Present => Null_Exclusion_Present (Spec),
2369 Parameter_Type =>
2370 New_Copy_Tree (Parameter_Type (Spec)),
2371 Expression => New_Copy_Tree (Expression (Spec))));
2373 Next_Formal (Formal);
2374 end loop;
2375 end Copy_Formals;
2377 ------------------------
2378 -- Copy_Return_Object --
2379 ------------------------
2381 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id is
2382 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2384 begin
2385 -- The use of New_Copy_Tree ensures that global references are
2386 -- preserved in case of generics.
2388 return
2389 Make_Object_Declaration (Sloc (Obj_Decl),
2390 Defining_Identifier =>
2391 Make_Defining_Identifier (Sloc (Obj_Id), Chars (Obj_Id)),
2392 Aliased_Present => Aliased_Present (Obj_Decl),
2393 Constant_Present => Constant_Present (Obj_Decl),
2394 Null_Exclusion_Present => Null_Exclusion_Present (Obj_Decl),
2395 Object_Definition =>
2396 New_Copy_Tree (Object_Definition (Obj_Decl)),
2397 Expression => New_Copy_Tree (Expression (Obj_Decl)));
2398 end Copy_Return_Object;
2400 ----------------------------------
2401 -- Split_Unconstrained_Function --
2402 ----------------------------------
2404 procedure Split_Unconstrained_Function
2405 (N : Node_Id;
2406 Spec_Id : Entity_Id)
2408 Loc : constant Source_Ptr := Sloc (N);
2409 Ret_Stmt : constant Node_Id :=
2410 First (Statements (Handled_Statement_Sequence (N)));
2411 Ret_Obj : constant Node_Id :=
2412 First (Return_Object_Declarations (Ret_Stmt));
2414 procedure Build_Procedure
2415 (Proc_Id : out Entity_Id;
2416 Decl_List : out List_Id);
2417 -- Build a procedure containing the statements found in the extended
2418 -- return statement of the unconstrained function body N.
2420 ---------------------
2421 -- Build_Procedure --
2422 ---------------------
2424 procedure Build_Procedure
2425 (Proc_Id : out Entity_Id;
2426 Decl_List : out List_Id)
2428 Formals : constant List_Id := New_List;
2429 Subp_Name : constant Name_Id := New_Internal_Name ('F');
2431 Body_Decls : List_Id := No_List;
2432 Decl : Node_Id;
2433 Proc_Body : Node_Id;
2434 Proc_Spec : Node_Id;
2436 begin
2437 -- Create formal parameters for the return object and all formals
2438 -- of the unconstrained function in order to pass their values to
2439 -- the procedure.
2441 Build_Return_Object_Formal
2442 (Loc => Loc,
2443 Obj_Decl => Ret_Obj,
2444 Formals => Formals);
2446 Copy_Formals
2447 (Loc => Loc,
2448 Subp_Id => Spec_Id,
2449 Formals => Formals);
2451 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
2453 Proc_Spec :=
2454 Make_Procedure_Specification (Loc,
2455 Defining_Unit_Name => Proc_Id,
2456 Parameter_Specifications => Formals);
2458 Decl_List := New_List;
2460 Append_To (Decl_List,
2461 Make_Subprogram_Declaration (Loc, Proc_Spec));
2463 -- Can_Convert_Unconstrained_Function checked that the function
2464 -- has no local declarations except implicit label declarations.
2465 -- Copy these declarations to the built procedure.
2467 if Present (Declarations (N)) then
2468 Body_Decls := New_List;
2470 Decl := First (Declarations (N));
2471 while Present (Decl) loop
2472 pragma Assert (Nkind (Decl) = N_Implicit_Label_Declaration);
2474 Append_To (Body_Decls,
2475 Make_Implicit_Label_Declaration (Loc,
2476 Make_Defining_Identifier (Loc,
2477 Chars => Chars (Defining_Identifier (Decl))),
2478 Label_Construct => Empty));
2480 Next (Decl);
2481 end loop;
2482 end if;
2484 pragma Assert (Present (Handled_Statement_Sequence (Ret_Stmt)));
2486 Proc_Body :=
2487 Make_Subprogram_Body (Loc,
2488 Specification => Copy_Subprogram_Spec (Proc_Spec),
2489 Declarations => Body_Decls,
2490 Handled_Statement_Sequence =>
2491 New_Copy_Tree (Handled_Statement_Sequence (Ret_Stmt)));
2493 Set_Defining_Unit_Name (Specification (Proc_Body),
2494 Make_Defining_Identifier (Loc, Subp_Name));
2496 Append_To (Decl_List, Proc_Body);
2497 end Build_Procedure;
2499 -- Local variables
2501 New_Obj : constant Node_Id := Copy_Return_Object (Ret_Obj);
2502 Blk_Stmt : Node_Id;
2503 Proc_Call : Node_Id;
2504 Proc_Id : Entity_Id;
2506 -- Start of processing for Split_Unconstrained_Function
2508 begin
2509 -- Build the associated procedure, analyze it and insert it before
2510 -- the function body N.
2512 declare
2513 Scope : constant Entity_Id := Current_Scope;
2514 Decl_List : List_Id;
2515 begin
2516 Pop_Scope;
2517 Build_Procedure (Proc_Id, Decl_List);
2518 Insert_Actions (N, Decl_List);
2519 Set_Is_Inlined (Proc_Id);
2520 Push_Scope (Scope);
2521 end;
2523 -- Build the call to the generated procedure
2525 declare
2526 Actual_List : constant List_Id := New_List;
2527 Formal : Entity_Id;
2529 begin
2530 Append_To (Actual_List,
2531 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
2533 Formal := First_Formal (Spec_Id);
2534 while Present (Formal) loop
2535 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
2537 -- Avoid spurious warning on unreferenced formals
2539 Set_Referenced (Formal);
2540 Next_Formal (Formal);
2541 end loop;
2543 Proc_Call :=
2544 Make_Procedure_Call_Statement (Loc,
2545 Name => New_Occurrence_Of (Proc_Id, Loc),
2546 Parameter_Associations => Actual_List);
2547 end;
2549 -- Generate:
2551 -- declare
2552 -- New_Obj : ...
2553 -- begin
2554 -- Proc (New_Obj, ...);
2555 -- return New_Obj;
2556 -- end;
2558 Blk_Stmt :=
2559 Make_Block_Statement (Loc,
2560 Declarations => New_List (New_Obj),
2561 Handled_Statement_Sequence =>
2562 Make_Handled_Sequence_Of_Statements (Loc,
2563 Statements => New_List (
2565 Proc_Call,
2567 Make_Simple_Return_Statement (Loc,
2568 Expression =>
2569 New_Occurrence_Of
2570 (Defining_Identifier (New_Obj), Loc)))));
2572 Rewrite (Ret_Stmt, Blk_Stmt);
2573 end Split_Unconstrained_Function;
2575 -- Local variables
2577 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2579 -- Start of processing for Check_And_Split_Unconstrained_Function
2581 begin
2582 pragma Assert (Back_End_Inlining
2583 and then Ekind (Spec_Id) = E_Function
2584 and then Returns_Unconstrained_Type (Spec_Id)
2585 and then Comes_From_Source (Body_Id)
2586 and then (Has_Pragma_Inline_Always (Spec_Id)
2587 or else Optimization_Level > 0));
2589 -- This routine must not be used in GNATprove mode since GNATprove
2590 -- relies on frontend inlining
2592 pragma Assert (not GNATprove_Mode);
2594 -- No need to split the function if we cannot generate the code
2596 if Serious_Errors_Detected /= 0 then
2597 return;
2598 end if;
2600 -- No action needed in stubs since the attribute Body_To_Inline
2601 -- is not available
2603 if Nkind (Decl) = N_Subprogram_Body_Stub then
2604 return;
2606 -- Cannot build the body to inline if the attribute is already set.
2607 -- This attribute may have been set if this is a subprogram renaming
2608 -- declarations (see Freeze.Build_Renamed_Body).
2610 elsif Present (Body_To_Inline (Decl)) then
2611 return;
2613 -- Do not generate a body to inline for protected functions, because the
2614 -- transformation generates a call to a protected procedure, causing
2615 -- spurious errors. We don't inline protected operations anyway, so
2616 -- this is no loss. We might as well ignore intrinsics and foreign
2617 -- conventions as well -- just allow Ada conventions.
2619 elsif not (Convention (Spec_Id) = Convention_Ada
2620 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Copy
2621 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Reference)
2622 then
2623 return;
2625 -- Check excluded declarations
2627 elsif Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
2628 return;
2630 -- Check excluded statements. There is no need to protect us against
2631 -- exception handlers since they are supported by the GCC backend.
2633 elsif Present (Handled_Statement_Sequence (N))
2634 and then Has_Excluded_Statement
2635 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2636 then
2637 return;
2638 end if;
2640 -- Build the body to inline only if really needed
2642 if Can_Split_Unconstrained_Function (N) then
2643 Split_Unconstrained_Function (N, Spec_Id);
2644 Build_Body_To_Inline (N, Spec_Id);
2645 Set_Is_Inlined (Spec_Id);
2646 end if;
2647 end Check_And_Split_Unconstrained_Function;
2649 ---------------------------------------------
2650 -- Check_Object_Renaming_In_GNATprove_Mode --
2651 ---------------------------------------------
2653 procedure Check_Object_Renaming_In_GNATprove_Mode (Spec_Id : Entity_Id) is
2654 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2655 Body_Decl : constant Node_Id :=
2656 Unit_Declaration_Node (Corresponding_Body (Decl));
2658 function Check_Object_Renaming (N : Node_Id) return Traverse_Result;
2659 -- Returns Abandon on node N if this is a reference to an object
2660 -- renaming, which will be expanded into the renamed object in
2661 -- GNATprove mode.
2663 ---------------------------
2664 -- Check_Object_Renaming --
2665 ---------------------------
2667 function Check_Object_Renaming (N : Node_Id) return Traverse_Result is
2668 begin
2669 case Nkind (Original_Node (N)) is
2670 when N_Expanded_Name
2671 | N_Identifier
2673 declare
2674 Obj_Id : constant Entity_Id := Entity (Original_Node (N));
2675 begin
2676 -- Recognize the case when SPARK expansion rewrites a
2677 -- reference to an object renaming.
2679 if Present (Obj_Id)
2680 and then Is_Object (Obj_Id)
2681 and then Present (Renamed_Object (Obj_Id))
2682 and then Nkind (Renamed_Object (Obj_Id)) not in N_Entity
2684 -- Copy_Generic_Node called for inlining expects the
2685 -- references to global entities to have the same kind
2686 -- in the "generic" code and its "instantiation".
2688 and then Nkind (Original_Node (N)) /=
2689 Nkind (Renamed_Object (Obj_Id))
2690 then
2691 return Abandon;
2692 else
2693 return OK;
2694 end if;
2695 end;
2697 when others =>
2698 return OK;
2699 end case;
2700 end Check_Object_Renaming;
2702 function Check_All_Object_Renamings is new
2703 Traverse_Func (Check_Object_Renaming);
2705 -- Start of processing for Check_Object_Renaming_In_GNATprove_Mode
2707 begin
2708 -- Subprograms with object renamings replaced by the special SPARK
2709 -- expansion cannot be inlined.
2711 if Check_All_Object_Renamings (Body_Decl) /= OK then
2712 Cannot_Inline ("cannot inline & (object renaming)?",
2713 Body_Decl, Spec_Id);
2714 Set_Body_To_Inline (Decl, Empty);
2715 end if;
2716 end Check_Object_Renaming_In_GNATprove_Mode;
2718 -------------------------------------
2719 -- Check_Package_Body_For_Inlining --
2720 -------------------------------------
2722 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2723 Bname : Unit_Name_Type;
2724 E : Entity_Id;
2725 OK : Boolean;
2727 begin
2728 -- Legacy implementation (relying on frontend inlining)
2730 if not Back_End_Inlining
2731 and then Is_Compilation_Unit (P)
2732 and then not Is_Generic_Instance (P)
2733 then
2734 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2736 E := First_Entity (P);
2737 while Present (E) loop
2738 if Has_Pragma_Inline_Always (E)
2739 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2740 then
2741 if not Is_Loaded (Bname) then
2742 Load_Needed_Body (N, OK);
2744 if OK then
2746 -- Check we are not trying to inline a parent whose body
2747 -- depends on a child, when we are compiling the body of
2748 -- the child. Otherwise we have a potential elaboration
2749 -- circularity with inlined subprograms and with
2750 -- Taft-Amendment types.
2752 declare
2753 Comp : Node_Id; -- Body just compiled
2754 Child_Spec : Entity_Id; -- Spec of main unit
2755 Ent : Entity_Id; -- For iteration
2756 With_Clause : Node_Id; -- Context of body.
2758 begin
2759 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2760 and then Present (Body_Entity (P))
2761 then
2762 Child_Spec :=
2763 Defining_Entity
2764 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2766 Comp :=
2767 Parent (Unit_Declaration_Node (Body_Entity (P)));
2769 -- Check whether the context of the body just
2770 -- compiled includes a child of itself, and that
2771 -- child is the spec of the main compilation.
2773 With_Clause := First (Context_Items (Comp));
2774 while Present (With_Clause) loop
2775 if Nkind (With_Clause) = N_With_Clause
2776 and then
2777 Scope (Entity (Name (With_Clause))) = P
2778 and then
2779 Entity (Name (With_Clause)) = Child_Spec
2780 then
2781 Error_Msg_Node_2 := Child_Spec;
2782 Error_Msg_NE
2783 ("body of & depends on child unit&??",
2784 With_Clause, P);
2785 Error_Msg_N
2786 ("\subprograms in body cannot be inlined??",
2787 With_Clause);
2789 -- Disable further inlining from this unit,
2790 -- and keep Taft-amendment types incomplete.
2792 Ent := First_Entity (P);
2793 while Present (Ent) loop
2794 if Is_Type (Ent)
2795 and then Has_Completion_In_Body (Ent)
2796 then
2797 Set_Full_View (Ent, Empty);
2799 elsif Is_Subprogram (Ent) then
2800 Set_Is_Inlined (Ent, False);
2801 end if;
2803 Next_Entity (Ent);
2804 end loop;
2806 return;
2807 end if;
2809 Next (With_Clause);
2810 end loop;
2811 end if;
2812 end;
2814 elsif Ineffective_Inline_Warnings then
2815 Error_Msg_Unit_1 := Bname;
2816 Error_Msg_N
2817 ("unable to inline subprograms defined in $?p?", P);
2818 Error_Msg_N ("\body not found?p?", P);
2819 return;
2820 end if;
2821 end if;
2823 return;
2824 end if;
2826 Next_Entity (E);
2827 end loop;
2828 end if;
2829 end Check_Package_Body_For_Inlining;
2831 --------------------
2832 -- Cleanup_Scopes --
2833 --------------------
2835 procedure Cleanup_Scopes is
2836 Decl : Node_Id;
2837 Elmt : Elmt_Id;
2838 Fin : Entity_Id;
2839 Kind : Entity_Kind;
2840 Scop : Entity_Id;
2842 begin
2843 Elmt := First_Elmt (To_Clean);
2844 while Present (Elmt) loop
2845 Scop := Node (Elmt);
2846 Kind := Ekind (Scop);
2848 if Kind = E_Block then
2849 Decl := Parent (Block_Node (Scop));
2851 else
2852 Decl := Unit_Declaration_Node (Scop);
2854 if Nkind (Decl) in N_Subprogram_Declaration
2855 | N_Task_Type_Declaration
2856 | N_Subprogram_Body_Stub
2857 then
2858 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2859 end if;
2860 end if;
2862 -- Finalizers are built only for package specs and bodies that are
2863 -- compilation units, so check that we do not have anything else.
2864 -- Moreover, they must be built at most once for each entity during
2865 -- the compilation of the main unit. However, if other units are
2866 -- later compiled for inlining purposes, they may also contain body
2867 -- instances and, therefore, appear again here, so we need to make
2868 -- sure that we do not build two finalizers for them (note that the
2869 -- contents of the finalizer for these units is irrelevant since it
2870 -- is not output in the generated code).
2872 if Kind in E_Package | E_Package_Body then
2873 declare
2874 Unit_Entity : constant Entity_Id :=
2875 (if Kind = E_Package then Scop else Spec_Entity (Scop));
2877 begin
2878 pragma Assert (Is_Compilation_Unit (Unit_Entity)
2879 and then (No (Finalizer (Scop))
2880 or else Unit_Entity /= Main_Unit_Entity));
2882 if No (Finalizer (Scop)) then
2883 Build_Finalizer
2884 (N => Decl,
2885 Clean_Stmts => No_List,
2886 Mark_Id => Empty,
2887 Top_Decls => No_List,
2888 Defer_Abort => False,
2889 Fin_Id => Fin);
2891 if Present (Fin) then
2892 Set_Finalizer (Scop, Fin);
2893 end if;
2894 end if;
2895 end;
2897 else
2898 Push_Scope (Scop);
2899 Expand_Cleanup_Actions (Decl);
2900 Pop_Scope;
2901 end if;
2903 Next_Elmt (Elmt);
2904 end loop;
2905 end Cleanup_Scopes;
2907 -----------------------------------------------
2908 -- Establish_Actual_Mapping_For_Inlined_Call --
2909 -----------------------------------------------
2911 procedure Establish_Actual_Mapping_For_Inlined_Call
2912 (N : Node_Id;
2913 Subp : Entity_Id;
2914 Decls : List_Id;
2915 Body_Or_Expr_To_Check : Node_Id)
2918 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2919 -- Determine whether a formal parameter is used only once in
2920 -- Body_Or_Expr_To_Check.
2922 -------------------------
2923 -- Formal_Is_Used_Once --
2924 -------------------------
2926 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2927 Use_Counter : Nat := 0;
2929 function Count_Uses (N : Node_Id) return Traverse_Result;
2930 -- Traverse the tree and count the uses of the formal parameter.
2931 -- In this case, for optimization purposes, we do not need to
2932 -- continue the traversal once more than one use is encountered.
2934 ----------------
2935 -- Count_Uses --
2936 ----------------
2938 function Count_Uses (N : Node_Id) return Traverse_Result is
2939 begin
2940 -- The original node is an identifier
2942 if Nkind (N) = N_Identifier
2943 and then Present (Entity (N))
2945 -- Original node's entity points to the one in the copied body
2947 and then Nkind (Entity (N)) = N_Identifier
2948 and then Present (Entity (Entity (N)))
2950 -- The entity of the copied node is the formal parameter
2952 and then Entity (Entity (N)) = Formal
2953 then
2954 Use_Counter := Use_Counter + 1;
2956 -- If this is a second use then abandon the traversal
2958 if Use_Counter > 1 then
2959 return Abandon;
2960 end if;
2961 end if;
2963 return OK;
2964 end Count_Uses;
2966 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2968 -- Start of processing for Formal_Is_Used_Once
2970 begin
2971 Count_Formal_Uses (Body_Or_Expr_To_Check);
2972 return Use_Counter = 1;
2973 end Formal_Is_Used_Once;
2975 -- Local Data --
2977 F : Entity_Id;
2978 A : Node_Id;
2979 Decl : Node_Id;
2980 Loc : constant Source_Ptr := Sloc (N);
2981 New_A : Node_Id;
2982 Temp : Entity_Id;
2983 Temp_Typ : Entity_Id;
2985 -- Start of processing for Establish_Actual_Mapping_For_Inlined_Call
2987 begin
2988 F := First_Formal (Subp);
2989 A := First_Actual (N);
2990 while Present (F) loop
2991 -- Reset Last_Assignment for any parameters of mode out or in out, to
2992 -- prevent spurious warnings about overwriting for assignments to the
2993 -- formal in the inlined code.
2995 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
2997 -- In GNATprove mode a protected component acting as an actual
2998 -- subprogram parameter will appear as inlined-for-proof. However,
2999 -- its E_Component entity is not an assignable object, so the
3000 -- assertion in Set_Last_Assignment will fail. We just omit the
3001 -- call to Set_Last_Assignment, because GNATprove flags useless
3002 -- assignments with its own flow analysis.
3004 -- In GNAT mode such a problem does not occur, because protected
3005 -- components are inlined via object renamings whose entity kind
3006 -- E_Variable is assignable.
3008 if Is_Assignable (Entity (A)) then
3009 Set_Last_Assignment (Entity (A), Empty);
3010 else
3011 pragma Assert
3012 (GNATprove_Mode and then Is_Protected_Component (Entity (A)));
3013 end if;
3014 end if;
3016 -- If the argument may be a controlling argument in a call within
3017 -- the inlined body, we must preserve its class-wide nature to ensure
3018 -- that dynamic dispatching will take place subsequently. If the
3019 -- formal has a constraint, then it must be preserved to retain the
3020 -- semantics of the body.
3022 if Is_Class_Wide_Type (Etype (F))
3023 or else (Is_Access_Type (Etype (F))
3024 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3025 then
3026 Temp_Typ := Etype (F);
3028 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3029 and then Etype (F) /= Base_Type (Etype (F))
3030 and then Is_Constrained (Etype (F))
3031 then
3032 Temp_Typ := Etype (F);
3034 else
3035 Temp_Typ := Etype (A);
3036 end if;
3038 -- If the actual is a simple name or a literal, no need to create a
3039 -- temporary, object can be used directly. Skip this optimization in
3040 -- GNATprove mode, to make sure any check on a type conversion will
3041 -- be issued.
3043 if (Is_Entity_Name (A)
3044 and then
3045 (not Is_Scalar_Type (Etype (A))
3046 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3047 and then not GNATprove_Mode)
3049 -- When the actual is an identifier and the corresponding formal is
3050 -- used only once in the original body, the formal can be substituted
3051 -- directly with the actual parameter. Skip this optimization in
3052 -- GNATprove mode, to make sure any check on a type conversion
3053 -- will be issued.
3055 or else
3056 (Nkind (A) = N_Identifier
3057 and then Formal_Is_Used_Once (F)
3058 and then not GNATprove_Mode)
3060 -- If the actual is a literal and the formal has its address taken,
3061 -- we cannot pass the literal itself as an argument, so its value
3062 -- must be captured in a temporary.
3064 or else
3065 (Nkind (A) in
3066 N_Real_Literal | N_Integer_Literal | N_Character_Literal
3067 and then not Address_Taken (F))
3068 then
3069 if Etype (F) /= Etype (A) then
3070 Set_Renamed_Object
3071 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3072 else
3073 Set_Renamed_Object (F, A);
3074 end if;
3076 else
3077 Temp := Make_Temporary (Loc, 'C');
3079 -- If the actual for an in/in-out parameter is a view conversion,
3080 -- make it into an unchecked conversion, given that an untagged
3081 -- type conversion is not a proper object for a renaming.
3083 -- In-out conversions that involve real conversions have already
3084 -- been transformed in Expand_Actuals.
3086 if Nkind (A) = N_Type_Conversion
3087 and then Ekind (F) /= E_In_Parameter
3088 then
3089 New_A := Unchecked_Convert_To (Etype (F), Expression (A));
3091 -- In GNATprove mode, keep the most precise type of the actual for
3092 -- the temporary variable, when the formal type is unconstrained.
3093 -- Otherwise, the AST may contain unexpected assignment statements
3094 -- to a temporary variable of unconstrained type renaming a local
3095 -- variable of constrained type, which is not expected by
3096 -- GNATprove.
3098 elsif Etype (F) /= Etype (A)
3099 and then (not GNATprove_Mode or else Is_Constrained (Etype (F)))
3100 then
3101 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3102 Temp_Typ := Etype (F);
3104 else
3105 New_A := Relocate_Node (A);
3106 end if;
3108 Set_Sloc (New_A, Sloc (N));
3110 -- If the actual has a by-reference type, it cannot be copied,
3111 -- so its value is captured in a renaming declaration. Otherwise
3112 -- declare a local constant initialized with the actual.
3114 -- We also use a renaming declaration for expressions of an array
3115 -- type that is not bit-packed, both for efficiency reasons and to
3116 -- respect the semantics of the call: in most cases the original
3117 -- call will pass the parameter by reference, and thus the inlined
3118 -- code will have the same semantics.
3120 -- Finally, we need a renaming declaration in the case of limited
3121 -- types for which initialization cannot be by copy either.
3123 if Ekind (F) = E_In_Parameter
3124 and then not Is_By_Reference_Type (Etype (A))
3125 and then not Is_Limited_Type (Etype (A))
3126 and then
3127 (not Is_Array_Type (Etype (A))
3128 or else not Is_Object_Reference (A)
3129 or else Is_Bit_Packed_Array (Etype (A)))
3130 then
3131 Decl :=
3132 Make_Object_Declaration (Loc,
3133 Defining_Identifier => Temp,
3134 Constant_Present => True,
3135 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3136 Expression => New_A);
3138 else
3139 -- In GNATprove mode, make an explicit copy of input
3140 -- parameters when formal and actual types differ, to make
3141 -- sure any check on the type conversion will be issued.
3142 -- The legality of the copy is ensured by calling first
3143 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3145 if GNATprove_Mode
3146 and then Ekind (F) /= E_Out_Parameter
3147 and then not Same_Type (Etype (F), Etype (A))
3148 then
3149 pragma Assert (not Is_By_Reference_Type (Etype (A)));
3150 pragma Assert (not Is_Limited_Type (Etype (A)));
3152 Append_To (Decls,
3153 Make_Object_Declaration (Loc,
3154 Defining_Identifier => Make_Temporary (Loc, 'C'),
3155 Constant_Present => True,
3156 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3157 Expression => New_Copy_Tree (New_A)));
3158 end if;
3160 Decl :=
3161 Make_Object_Renaming_Declaration (Loc,
3162 Defining_Identifier => Temp,
3163 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3164 Name => New_A);
3165 end if;
3167 Append (Decl, Decls);
3168 Set_Renamed_Object (F, Temp);
3169 end if;
3171 Next_Formal (F);
3172 Next_Actual (A);
3173 end loop;
3174 end Establish_Actual_Mapping_For_Inlined_Call;
3176 -------------------------
3177 -- Expand_Inlined_Call --
3178 -------------------------
3180 procedure Expand_Inlined_Call
3181 (N : Node_Id;
3182 Subp : Entity_Id;
3183 Orig_Subp : Entity_Id)
3185 Decls : constant List_Id := New_List;
3186 Is_Predef : constant Boolean :=
3187 Is_Predefined_Unit (Get_Source_Unit (Subp));
3188 Loc : constant Source_Ptr := Sloc (N);
3189 Orig_Bod : constant Node_Id :=
3190 Body_To_Inline (Unit_Declaration_Node (Subp));
3192 Uses_Back_End : constant Boolean :=
3193 Back_End_Inlining and then Optimization_Level > 0;
3194 -- The back-end expansion is used if the target supports back-end
3195 -- inlining and some level of optimixation is required; otherwise
3196 -- the inlining takes place fully as a tree expansion.
3198 Blk : Node_Id;
3199 Decl : Node_Id;
3200 Exit_Lab : Entity_Id := Empty;
3201 Lab_Decl : Node_Id := Empty;
3202 Lab_Id : Node_Id;
3203 Num_Ret : Nat := 0;
3204 Ret_Type : Entity_Id;
3205 Temp : Entity_Id;
3207 Is_Unc : Boolean;
3208 Is_Unc_Decl : Boolean;
3209 -- If the type returned by the function is unconstrained and the call
3210 -- can be inlined, special processing is required.
3212 Return_Object : Entity_Id := Empty;
3213 -- Entity in declaration in an extended_return_statement
3215 Targ : Node_Id := Empty;
3216 -- The target of the call. If context is an assignment statement then
3217 -- this is the left-hand side of the assignment, else it is a temporary
3218 -- to which the return value is assigned prior to rewriting the call.
3220 Targ1 : Node_Id := Empty;
3221 -- A separate target used when the return type is unconstrained
3223 procedure Declare_Postconditions_Result;
3224 -- When generating C code, declare _Result, which may be used in the
3225 -- inlined _Postconditions procedure to verify the return value.
3227 procedure Make_Exit_Label;
3228 -- Build declaration for exit label to be used in Return statements,
3229 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3230 -- declaration). Does nothing if Exit_Lab already set.
3232 procedure Make_Loop_Labels_Unique (HSS : Node_Id);
3233 -- When compiling for CCG and performing front-end inlining, replace
3234 -- loop names and references to them so that they do not conflict with
3235 -- homographs in the current subprogram.
3237 function Process_Formals (N : Node_Id) return Traverse_Result;
3238 -- Replace occurrence of a formal with the corresponding actual, or the
3239 -- thunk generated for it. Replace a return statement with an assignment
3240 -- to the target of the call, with appropriate conversions if needed.
3242 function Process_Formals_In_Aspects (N : Node_Id) return Traverse_Result;
3243 -- Because aspects are linked indirectly to the rest of the tree,
3244 -- replacement of formals appearing in aspect specifications must
3245 -- be performed in a separate pass, using an instantiation of the
3246 -- previous subprogram over aspect specifications reachable from N.
3248 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3249 -- If the call being expanded is that of an internal subprogram, set the
3250 -- sloc of the generated block to that of the call itself, so that the
3251 -- expansion is skipped by the "next" command in gdb. Same processing
3252 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
3253 -- Debug_Generated_Code is true, suppress this change to simplify our
3254 -- own development. Same in GNATprove mode, to ensure that warnings and
3255 -- diagnostics point to the proper location.
3257 procedure Reset_Dispatching_Calls (N : Node_Id);
3258 -- In subtree N search for occurrences of dispatching calls that use the
3259 -- Ada 2005 Object.Operation notation and the object is a formal of the
3260 -- inlined subprogram. Reset the entity associated with Operation in all
3261 -- the found occurrences.
3263 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3264 -- If the function body is a single expression, replace call with
3265 -- expression, else insert block appropriately.
3267 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3268 -- If procedure body has no local variables, inline body without
3269 -- creating block, otherwise rewrite call with block.
3271 -----------------------------------
3272 -- Declare_Postconditions_Result --
3273 -----------------------------------
3275 procedure Declare_Postconditions_Result is
3276 Enclosing_Subp : constant Entity_Id := Scope (Subp);
3278 begin
3279 pragma Assert
3280 (Modify_Tree_For_C
3281 and then Is_Subprogram (Enclosing_Subp)
3282 and then Present (Wrapped_Statements (Enclosing_Subp)));
3284 if Ekind (Enclosing_Subp) = E_Function then
3285 if Nkind (First (Parameter_Associations (N))) in
3286 N_Numeric_Or_String_Literal
3287 then
3288 Append_To (Declarations (Blk),
3289 Make_Object_Declaration (Loc,
3290 Defining_Identifier =>
3291 Make_Defining_Identifier (Loc, Name_uResult),
3292 Constant_Present => True,
3293 Object_Definition =>
3294 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
3295 Expression =>
3296 New_Copy_Tree (First (Parameter_Associations (N)))));
3297 else
3298 Append_To (Declarations (Blk),
3299 Make_Object_Renaming_Declaration (Loc,
3300 Defining_Identifier =>
3301 Make_Defining_Identifier (Loc, Name_uResult),
3302 Subtype_Mark =>
3303 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
3304 Name =>
3305 New_Copy_Tree (First (Parameter_Associations (N)))));
3306 end if;
3307 end if;
3308 end Declare_Postconditions_Result;
3310 ---------------------
3311 -- Make_Exit_Label --
3312 ---------------------
3314 procedure Make_Exit_Label is
3315 Lab_Ent : Entity_Id;
3316 begin
3317 if No (Exit_Lab) then
3318 Lab_Ent := Make_Temporary (Loc, 'L');
3319 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
3320 Exit_Lab := Make_Label (Loc, Lab_Id);
3321 Lab_Decl :=
3322 Make_Implicit_Label_Declaration (Loc,
3323 Defining_Identifier => Lab_Ent,
3324 Label_Construct => Exit_Lab);
3325 end if;
3326 end Make_Exit_Label;
3328 -----------------------------
3329 -- Make_Loop_Labels_Unique --
3330 -----------------------------
3332 procedure Make_Loop_Labels_Unique (HSS : Node_Id) is
3333 function Process_Loop (N : Node_Id) return Traverse_Result;
3335 ------------------
3336 -- Process_Loop --
3337 ------------------
3339 function Process_Loop (N : Node_Id) return Traverse_Result is
3340 Id : Entity_Id;
3342 begin
3343 if Nkind (N) = N_Loop_Statement
3344 and then Present (Identifier (N))
3345 then
3346 -- Create new external name for loop and update the
3347 -- corresponding entity.
3349 Id := Entity (Identifier (N));
3350 Set_Chars (Id, New_External_Name (Chars (Id), 'L', -1));
3351 Set_Chars (Identifier (N), Chars (Id));
3353 elsif Nkind (N) = N_Exit_Statement
3354 and then Present (Name (N))
3355 then
3356 -- The exit statement must name an enclosing loop, whose name
3357 -- has already been updated.
3359 Set_Chars (Name (N), Chars (Entity (Name (N))));
3360 end if;
3362 return OK;
3363 end Process_Loop;
3365 procedure Update_Loop_Names is new Traverse_Proc (Process_Loop);
3367 -- Local variables
3369 Stmt : Node_Id;
3371 -- Start of processing for Make_Loop_Labels_Unique
3373 begin
3374 if Modify_Tree_For_C then
3375 Stmt := First (Statements (HSS));
3376 while Present (Stmt) loop
3377 Update_Loop_Names (Stmt);
3378 Next (Stmt);
3379 end loop;
3380 end if;
3381 end Make_Loop_Labels_Unique;
3383 ---------------------
3384 -- Process_Formals --
3385 ---------------------
3387 function Process_Formals (N : Node_Id) return Traverse_Result is
3388 A : Entity_Id;
3389 E : Entity_Id;
3390 Ret : Node_Id;
3392 Had_Private_View : Boolean;
3394 begin
3395 if Is_Entity_Name (N) and then Present (Entity (N)) then
3396 E := Entity (N);
3398 if Is_Formal (E) and then Scope (E) = Subp then
3399 A := Renamed_Object (E);
3401 -- Rewrite the occurrence of the formal into an occurrence of
3402 -- the actual. Also establish visibility on the proper view of
3403 -- the actual's subtype for the body's context (if the actual's
3404 -- subtype is private at the call point but its full view is
3405 -- visible to the body, then the inlined tree here must be
3406 -- analyzed with the full view).
3408 -- The Has_Private_View flag is cleared by rewriting, so it
3409 -- must be explicitly saved and restored, just like when
3410 -- instantiating the body to inline.
3412 if Is_Entity_Name (A) then
3413 Had_Private_View := Has_Private_View (N);
3414 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
3415 Set_Has_Private_View (N, Had_Private_View);
3416 Check_Private_View (N);
3418 elsif Nkind (A) = N_Defining_Identifier then
3419 Had_Private_View := Has_Private_View (N);
3420 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
3421 Set_Has_Private_View (N, Had_Private_View);
3422 Check_Private_View (N);
3424 -- Numeric literal
3426 else
3427 Rewrite (N, New_Copy (A));
3428 end if;
3429 end if;
3431 return Skip;
3433 elsif Is_Entity_Name (N)
3434 and then Present (Return_Object)
3435 and then Chars (N) = Chars (Return_Object)
3436 then
3437 -- Occurrence within an extended return statement. The return
3438 -- object is local to the body been inlined, and thus the generic
3439 -- copy is not analyzed yet, so we match by name, and replace it
3440 -- with target of call.
3442 if Nkind (Targ) = N_Defining_Identifier then
3443 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3444 else
3445 Rewrite (N, New_Copy_Tree (Targ));
3446 end if;
3448 return Skip;
3450 elsif Nkind (N) = N_Simple_Return_Statement then
3451 if No (Expression (N)) then
3452 Num_Ret := Num_Ret + 1;
3453 Make_Exit_Label;
3454 Rewrite (N,
3455 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3457 else
3458 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3459 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3460 then
3461 -- Function body is a single expression. No need for
3462 -- exit label.
3464 null;
3466 else
3467 Num_Ret := Num_Ret + 1;
3468 Make_Exit_Label;
3469 end if;
3471 -- Because of the presence of private types, the views of the
3472 -- expression and the context may be different, so place
3473 -- a type conversion to the context type to avoid spurious
3474 -- errors, e.g. when the expression is a numeric literal and
3475 -- the context is private. If the expression is an aggregate,
3476 -- use a qualified expression, because an aggregate is not a
3477 -- legal argument of a conversion. Ditto for numeric, character
3478 -- and string literals, and attributes that yield a universal
3479 -- type, because those must be resolved to a specific type.
3481 if Nkind (Expression (N)) in N_Aggregate
3482 | N_Character_Literal
3483 | N_Null
3484 | N_String_Literal
3485 or else Yields_Universal_Type (Expression (N))
3486 then
3487 Ret :=
3488 Make_Qualified_Expression (Sloc (N),
3489 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3490 Expression => Relocate_Node (Expression (N)));
3492 -- Use an unchecked type conversion between access types, for
3493 -- which a type conversion would not always be valid, as no
3494 -- check may result from the conversion.
3496 elsif Is_Access_Type (Ret_Type) then
3497 Ret :=
3498 Unchecked_Convert_To
3499 (Ret_Type, Relocate_Node (Expression (N)));
3501 -- Otherwise use a type conversion, which may trigger a check
3503 else
3504 Ret :=
3505 Make_Type_Conversion (Sloc (N),
3506 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3507 Expression => Relocate_Node (Expression (N)));
3508 end if;
3510 if Nkind (Targ) = N_Defining_Identifier then
3511 Rewrite (N,
3512 Make_Assignment_Statement (Loc,
3513 Name => New_Occurrence_Of (Targ, Loc),
3514 Expression => Ret));
3515 else
3516 Rewrite (N,
3517 Make_Assignment_Statement (Loc,
3518 Name => New_Copy (Targ),
3519 Expression => Ret));
3520 end if;
3522 Set_Assignment_OK (Name (N));
3524 if Present (Exit_Lab) then
3525 Insert_After (N,
3526 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3527 end if;
3528 end if;
3530 return OK;
3532 -- An extended return becomes a block whose first statement is the
3533 -- assignment of the initial expression of the return object to the
3534 -- target of the call itself.
3536 elsif Nkind (N) = N_Extended_Return_Statement then
3537 declare
3538 Return_Decl : constant Entity_Id :=
3539 First (Return_Object_Declarations (N));
3540 Assign : Node_Id;
3542 begin
3543 Return_Object := Defining_Identifier (Return_Decl);
3545 if Present (Expression (Return_Decl)) then
3546 if Nkind (Targ) = N_Defining_Identifier then
3547 Assign :=
3548 Make_Assignment_Statement (Loc,
3549 Name => New_Occurrence_Of (Targ, Loc),
3550 Expression => Expression (Return_Decl));
3551 else
3552 Assign :=
3553 Make_Assignment_Statement (Loc,
3554 Name => New_Copy (Targ),
3555 Expression => Expression (Return_Decl));
3556 end if;
3558 Set_Assignment_OK (Name (Assign));
3560 if No (Handled_Statement_Sequence (N)) then
3561 Set_Handled_Statement_Sequence (N,
3562 Make_Handled_Sequence_Of_Statements (Loc,
3563 Statements => New_List));
3564 end if;
3566 Prepend (Assign,
3567 Statements (Handled_Statement_Sequence (N)));
3568 end if;
3570 Rewrite (N,
3571 Make_Block_Statement (Loc,
3572 Handled_Statement_Sequence =>
3573 Handled_Statement_Sequence (N)));
3575 return OK;
3576 end;
3578 -- Remove pragma Unreferenced since it may refer to formals that
3579 -- are not visible in the inlined body, and in any case we will
3580 -- not be posting warnings on the inlined body so it is unneeded.
3582 elsif Nkind (N) = N_Pragma
3583 and then Pragma_Name (N) = Name_Unreferenced
3584 then
3585 Rewrite (N, Make_Null_Statement (Sloc (N)));
3586 return OK;
3588 else
3589 return OK;
3590 end if;
3591 end Process_Formals;
3593 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3595 --------------------------------
3596 -- Process_Formals_In_Aspects --
3597 --------------------------------
3599 function Process_Formals_In_Aspects
3600 (N : Node_Id) return Traverse_Result
3602 begin
3603 if Nkind (N) = N_Aspect_Specification then
3604 Replace_Formals (Expression (N));
3605 end if;
3606 return OK;
3607 end Process_Formals_In_Aspects;
3609 procedure Replace_Formals_In_Aspects is
3610 new Traverse_Proc (Process_Formals_In_Aspects);
3612 ------------------
3613 -- Process_Sloc --
3614 ------------------
3616 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3617 begin
3618 if not Debug_Generated_Code then
3619 Set_Sloc (Nod, Sloc (N));
3620 Set_Comes_From_Source (Nod, False);
3621 end if;
3623 return OK;
3624 end Process_Sloc;
3626 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3628 ------------------------------
3629 -- Reset_Dispatching_Calls --
3630 ------------------------------
3632 procedure Reset_Dispatching_Calls (N : Node_Id) is
3634 function Do_Reset (N : Node_Id) return Traverse_Result;
3636 --------------
3637 -- Do_Reset --
3638 --------------
3640 function Do_Reset (N : Node_Id) return Traverse_Result is
3641 begin
3642 if Nkind (N) = N_Procedure_Call_Statement
3643 and then Nkind (Name (N)) = N_Selected_Component
3644 and then Nkind (Prefix (Name (N))) = N_Identifier
3645 and then Is_Formal (Entity (Prefix (Name (N))))
3646 and then Is_Dispatching_Operation
3647 (Entity (Selector_Name (Name (N))))
3648 then
3649 Set_Entity (Selector_Name (Name (N)), Empty);
3650 end if;
3652 return OK;
3653 end Do_Reset;
3655 procedure Do_Reset_Calls is new Traverse_Proc (Do_Reset);
3657 begin
3658 Do_Reset_Calls (N);
3659 end Reset_Dispatching_Calls;
3661 ---------------------------
3662 -- Rewrite_Function_Call --
3663 ---------------------------
3665 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3666 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3667 Fst : constant Node_Id := First (Statements (HSS));
3669 begin
3670 Make_Loop_Labels_Unique (HSS);
3672 -- Optimize simple case: function body is a single return statement,
3673 -- which has been expanded into an assignment.
3675 if Is_Empty_List (Declarations (Blk))
3676 and then Nkind (Fst) = N_Assignment_Statement
3677 and then No (Next (Fst))
3678 then
3679 -- The function call may have been rewritten as the temporary
3680 -- that holds the result of the call, in which case remove the
3681 -- now useless declaration.
3683 if Nkind (N) = N_Identifier
3684 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3685 then
3686 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3687 end if;
3689 Rewrite (N, Expression (Fst));
3691 elsif Nkind (N) = N_Identifier
3692 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3693 then
3694 -- The block assigns the result of the call to the temporary
3696 Insert_After (Parent (Entity (N)), Blk);
3698 -- If the context is an assignment, and the left-hand side is free of
3699 -- side effects, the replacement is also safe.
3701 elsif Nkind (Parent (N)) = N_Assignment_Statement
3702 and then
3703 (Is_Entity_Name (Name (Parent (N)))
3704 or else
3705 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3706 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
3708 or else
3709 (Nkind (Name (Parent (N))) = N_Selected_Component
3710 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3711 then
3712 -- Replace assignment with the block
3714 declare
3715 Original_Assignment : constant Node_Id := Parent (N);
3717 begin
3718 -- Preserve the original assignment node to keep the complete
3719 -- assignment subtree consistent enough for Analyze_Assignment
3720 -- to proceed (specifically, the original Lhs node must still
3721 -- have an assignment statement as its parent).
3723 -- We cannot rely on Original_Node to go back from the block
3724 -- node to the assignment node, because the assignment might
3725 -- already be a rewrite substitution.
3727 Discard_Node (Relocate_Node (Original_Assignment));
3728 Rewrite (Original_Assignment, Blk);
3729 end;
3731 elsif Nkind (Parent (N)) = N_Object_Declaration then
3733 -- A call to a function which returns an unconstrained type
3734 -- found in the expression initializing an object-declaration is
3735 -- expanded into a procedure call which must be added after the
3736 -- object declaration.
3738 if Is_Unc_Decl and Back_End_Inlining then
3739 Insert_Action_After (Parent (N), Blk);
3740 else
3741 Set_Expression (Parent (N), Empty);
3742 Insert_After (Parent (N), Blk);
3743 end if;
3745 elsif Is_Unc and then not Back_End_Inlining then
3746 Insert_Before (Parent (N), Blk);
3747 end if;
3748 end Rewrite_Function_Call;
3750 ----------------------------
3751 -- Rewrite_Procedure_Call --
3752 ----------------------------
3754 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3755 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3757 begin
3758 Make_Loop_Labels_Unique (HSS);
3760 -- If there is a transient scope for N, this will be the scope of the
3761 -- actions for N, and the statements in Blk need to be within this
3762 -- scope. For example, they need to have visibility on the constant
3763 -- declarations created for the formals.
3765 -- If N needs no transient scope, and if there are no declarations in
3766 -- the inlined body, we can do a little optimization and insert the
3767 -- statements for the body directly after N, and rewrite N to a
3768 -- null statement, instead of rewriting N into a full-blown block
3769 -- statement.
3771 if not Scope_Is_Transient
3772 and then Is_Empty_List (Declarations (Blk))
3773 then
3774 Insert_List_After (N, Statements (HSS));
3775 Rewrite (N, Make_Null_Statement (Loc));
3776 else
3777 Rewrite (N, Blk);
3778 end if;
3779 end Rewrite_Procedure_Call;
3781 -- Start of processing for Expand_Inlined_Call
3783 begin
3784 -- Initializations for old/new semantics
3786 if not Uses_Back_End then
3787 Is_Unc := Is_Array_Type (Etype (Subp))
3788 and then not Is_Constrained (Etype (Subp));
3789 Is_Unc_Decl := False;
3790 else
3791 Is_Unc := Returns_Unconstrained_Type (Subp)
3792 and then Optimization_Level > 0;
3793 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
3794 and then Is_Unc;
3795 end if;
3797 -- Check for an illegal attempt to inline a recursive procedure. If the
3798 -- subprogram has parameters this is detected when trying to supply a
3799 -- binding for parameters that already have one. For parameterless
3800 -- subprograms this must be done explicitly.
3802 if In_Open_Scopes (Subp) then
3803 Cannot_Inline
3804 ("cannot inline call to recursive subprogram?", N, Subp);
3805 Set_Is_Inlined (Subp, False);
3806 return;
3808 -- Skip inlining if this is not a true inlining since the attribute
3809 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3810 -- true inlining, Orig_Bod has code rather than being an entity.
3812 elsif Nkind (Orig_Bod) in N_Entity then
3813 return;
3814 end if;
3816 if Nkind (Orig_Bod) in N_Defining_Identifier
3817 | N_Defining_Operator_Symbol
3818 then
3819 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3820 -- can be replaced with calls to the renamed entity directly, because
3821 -- the subprograms are subtype conformant. If the renamed subprogram
3822 -- is an inherited operation, we must redo the expansion because
3823 -- implicit conversions may be needed. Similarly, if the renamed
3824 -- entity is inlined, expand the call for further optimizations.
3826 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3828 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3829 Expand_Call (N);
3830 end if;
3832 return;
3833 end if;
3835 -- Register the call in the list of inlined calls
3837 Append_New_Elmt (N, To => Inlined_Calls);
3839 -- Use generic machinery to copy body of inlined subprogram, as if it
3840 -- were an instantiation, resetting source locations appropriately, so
3841 -- that nested inlined calls appear in the main unit.
3843 Save_Env (Subp, Empty);
3844 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3846 -- Old semantics
3848 if not Uses_Back_End then
3849 declare
3850 Bod : Node_Id;
3852 begin
3853 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3854 Blk :=
3855 Make_Block_Statement (Loc,
3856 Declarations => Declarations (Bod),
3857 Handled_Statement_Sequence =>
3858 Handled_Statement_Sequence (Bod));
3860 if No (Declarations (Bod)) then
3861 Set_Declarations (Blk, New_List);
3862 end if;
3864 -- When generating C code, declare _Result, which may be used to
3865 -- verify the return value.
3867 if Modify_Tree_For_C
3868 and then Nkind (N) = N_Procedure_Call_Statement
3869 and then Chars (Name (N)) = Name_uWrapped_Statements
3870 then
3871 Declare_Postconditions_Result;
3872 end if;
3874 -- For the unconstrained case, capture the name of the local
3875 -- variable that holds the result. This must be the first
3876 -- declaration in the block, because its bounds cannot depend
3877 -- on local variables. Otherwise there is no way to declare the
3878 -- result outside of the block. Needless to say, in general the
3879 -- bounds will depend on the actuals in the call.
3881 -- If the context is an assignment statement, as is the case
3882 -- for the expansion of an extended return, the left-hand side
3883 -- provides bounds even if the return type is unconstrained.
3885 if Is_Unc then
3886 declare
3887 First_Decl : Node_Id;
3889 begin
3890 First_Decl := First (Declarations (Blk));
3892 -- If the body is a single extended return statement,the
3893 -- resulting block is a nested block.
3895 if No (First_Decl) then
3896 First_Decl :=
3897 First (Statements (Handled_Statement_Sequence (Blk)));
3899 if Nkind (First_Decl) = N_Block_Statement then
3900 First_Decl := First (Declarations (First_Decl));
3901 end if;
3902 end if;
3904 -- No front-end inlining possible
3906 if Nkind (First_Decl) /= N_Object_Declaration then
3907 return;
3908 end if;
3910 if Nkind (Parent (N)) /= N_Assignment_Statement then
3911 Targ1 := Defining_Identifier (First_Decl);
3912 else
3913 Targ1 := Name (Parent (N));
3914 end if;
3915 end;
3916 end if;
3917 end;
3919 -- New semantics
3921 else
3922 declare
3923 Bod : Node_Id;
3925 begin
3926 -- General case
3928 if not Is_Unc then
3929 Bod :=
3930 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3931 Blk :=
3932 Make_Block_Statement (Loc,
3933 Declarations => Declarations (Bod),
3934 Handled_Statement_Sequence =>
3935 Handled_Statement_Sequence (Bod));
3937 -- Inline a call to a function that returns an unconstrained type.
3938 -- The semantic analyzer checked that frontend-inlined functions
3939 -- returning unconstrained types have no declarations and have
3940 -- a single extended return statement. As part of its processing
3941 -- the function was split into two subprograms: a procedure P' and
3942 -- a function F' that has a block with a call to procedure P' (see
3943 -- Split_Unconstrained_Function).
3945 else
3946 pragma Assert
3947 (Nkind
3948 (First
3949 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3950 N_Block_Statement);
3952 declare
3953 Blk_Stmt : constant Node_Id :=
3954 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
3955 First_Stmt : constant Node_Id :=
3956 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
3957 Second_Stmt : constant Node_Id := Next (First_Stmt);
3959 begin
3960 pragma Assert
3961 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3962 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3963 and then No (Next (Second_Stmt)));
3965 Bod :=
3966 Copy_Generic_Node
3967 (First
3968 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3969 Empty, Instantiating => True);
3970 Blk := Bod;
3972 -- Capture the name of the local variable that holds the
3973 -- result. This must be the first declaration in the block,
3974 -- because its bounds cannot depend on local variables.
3975 -- Otherwise there is no way to declare the result outside
3976 -- of the block. Needless to say, in general the bounds will
3977 -- depend on the actuals in the call.
3979 if Nkind (Parent (N)) /= N_Assignment_Statement then
3980 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3982 -- If the context is an assignment statement, as is the case
3983 -- for the expansion of an extended return, the left-hand
3984 -- side provides bounds even if the return type is
3985 -- unconstrained.
3987 else
3988 Targ1 := Name (Parent (N));
3989 end if;
3990 end;
3991 end if;
3993 if No (Declarations (Bod)) then
3994 Set_Declarations (Blk, New_List);
3995 end if;
3996 end;
3997 end if;
3999 -- If this is a derived function, establish the proper return type
4001 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4002 Ret_Type := Etype (Orig_Subp);
4003 else
4004 Ret_Type := Etype (Subp);
4005 end if;
4007 -- Create temporaries for the actuals that are expressions, or that are
4008 -- scalars and require copying to preserve semantics.
4010 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Orig_Bod);
4012 -- Establish target of function call. If context is not assignment or
4013 -- declaration, create a temporary as a target. The declaration for the
4014 -- temporary may be subsequently optimized away if the body is a single
4015 -- expression, or if the left-hand side of the assignment is simple
4016 -- enough, i.e. an entity or an explicit dereference of one.
4018 if Ekind (Subp) = E_Function then
4019 if Nkind (Parent (N)) = N_Assignment_Statement
4020 and then Is_Entity_Name (Name (Parent (N)))
4021 then
4022 Targ := Name (Parent (N));
4024 elsif Nkind (Parent (N)) = N_Assignment_Statement
4025 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4026 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4027 then
4028 Targ := Name (Parent (N));
4030 elsif Nkind (Parent (N)) = N_Assignment_Statement
4031 and then Nkind (Name (Parent (N))) = N_Selected_Component
4032 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4033 then
4034 Targ := New_Copy_Tree (Name (Parent (N)));
4036 elsif Nkind (Parent (N)) = N_Object_Declaration
4037 and then Is_Limited_Type (Etype (Subp))
4038 then
4039 Targ := Defining_Identifier (Parent (N));
4041 -- New semantics: In an object declaration avoid an extra copy
4042 -- of the result of a call to an inlined function that returns
4043 -- an unconstrained type
4045 elsif Uses_Back_End
4046 and then Nkind (Parent (N)) = N_Object_Declaration
4047 and then Is_Unc
4048 then
4049 Targ := Defining_Identifier (Parent (N));
4051 else
4052 -- Replace call with temporary and create its declaration
4054 Temp := Make_Temporary (Loc, 'C');
4055 Set_Is_Internal (Temp);
4057 -- For the unconstrained case, the generated temporary has the
4058 -- same constrained declaration as the result variable. It may
4059 -- eventually be possible to remove that temporary and use the
4060 -- result variable directly.
4062 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
4063 then
4064 Decl :=
4065 Make_Object_Declaration (Loc,
4066 Defining_Identifier => Temp,
4067 Object_Definition =>
4068 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4070 Replace_Formals (Decl);
4072 else
4073 Decl :=
4074 Make_Object_Declaration (Loc,
4075 Defining_Identifier => Temp,
4076 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
4078 Set_Etype (Temp, Ret_Type);
4079 end if;
4081 Set_No_Initialization (Decl);
4082 Append (Decl, Decls);
4083 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4084 Targ := Temp;
4085 end if;
4086 end if;
4088 Insert_Actions (N, Decls);
4090 if Is_Unc_Decl then
4092 -- Special management for inlining a call to a function that returns
4093 -- an unconstrained type and initializes an object declaration: we
4094 -- avoid generating undesired extra calls and goto statements.
4096 -- Given:
4097 -- function Func (...) return String is
4098 -- begin
4099 -- declare
4100 -- Result : String (1 .. 4);
4101 -- begin
4102 -- Proc (Result, ...);
4103 -- return Result;
4104 -- end;
4105 -- end Func;
4107 -- Result : String := Func (...);
4109 -- Replace this object declaration by:
4111 -- Result : String (1 .. 4);
4112 -- Proc (Result, ...);
4114 Remove_Homonym (Targ);
4116 Decl :=
4117 Make_Object_Declaration
4118 (Loc,
4119 Defining_Identifier => Targ,
4120 Object_Definition =>
4121 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4122 Replace_Formals (Decl);
4123 Set_No_Initialization (Decl);
4124 Rewrite (Parent (N), Decl);
4125 Analyze (Parent (N));
4127 -- Avoid spurious warnings since we know that this declaration is
4128 -- referenced by the procedure call.
4130 Set_Never_Set_In_Source (Targ, False);
4132 -- Remove the local declaration of the extended return stmt from the
4133 -- inlined code
4135 Remove (Parent (Targ1));
4137 -- Update the reference to the result (since we have rewriten the
4138 -- object declaration)
4140 declare
4141 Blk_Call_Stmt : Node_Id;
4143 begin
4144 -- Capture the call to the procedure
4146 Blk_Call_Stmt :=
4147 First (Statements (Handled_Statement_Sequence (Blk)));
4148 pragma Assert
4149 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
4151 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
4152 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
4153 New_Occurrence_Of (Targ, Loc));
4154 end;
4156 -- Remove the return statement
4158 pragma Assert
4159 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4160 N_Simple_Return_Statement);
4162 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4163 end if;
4165 -- Traverse the tree and replace formals with actuals or their thunks.
4166 -- Attach block to tree before analysis and rewriting.
4168 Replace_Formals (Blk);
4169 Replace_Formals_In_Aspects (Blk);
4170 Set_Parent (Blk, N);
4172 if GNATprove_Mode then
4173 null;
4175 elsif not Comes_From_Source (Subp) or else Is_Predef then
4176 Reset_Slocs (Blk);
4177 end if;
4179 if Is_Unc_Decl then
4181 -- No action needed since return statement has been already removed
4183 null;
4185 elsif Present (Exit_Lab) then
4187 -- If there's a single return statement at the end of the subprogram,
4188 -- the corresponding goto statement and the corresponding label are
4189 -- useless.
4191 if Num_Ret = 1
4192 and then
4193 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4194 N_Goto_Statement
4195 then
4196 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4197 else
4198 Append (Lab_Decl, (Declarations (Blk)));
4199 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4200 end if;
4201 end if;
4203 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
4204 -- on conflicting private views that Gigi would ignore. If this is a
4205 -- predefined unit, analyze with checks off, as is done in the non-
4206 -- inlined run-time units.
4208 declare
4209 I_Flag : constant Boolean := In_Inlined_Body;
4211 begin
4212 In_Inlined_Body := True;
4214 if Is_Predef then
4215 declare
4216 Style : constant Boolean := Style_Check;
4218 begin
4219 Style_Check := False;
4221 -- Search for dispatching calls that use the Object.Operation
4222 -- notation using an Object that is a parameter of the inlined
4223 -- function. We reset the decoration of Operation to force
4224 -- the reanalysis of the inlined dispatching call because
4225 -- the actual object has been inlined.
4227 Reset_Dispatching_Calls (Blk);
4229 -- In GNATprove mode, always consider checks on, even for
4230 -- predefined units.
4232 if GNATprove_Mode then
4233 Analyze (Blk);
4234 else
4235 Analyze (Blk, Suppress => All_Checks);
4236 end if;
4238 Style_Check := Style;
4239 end;
4241 else
4242 Analyze (Blk);
4243 end if;
4245 In_Inlined_Body := I_Flag;
4246 end;
4248 if Ekind (Subp) = E_Procedure then
4249 Rewrite_Procedure_Call (N, Blk);
4251 else
4252 Rewrite_Function_Call (N, Blk);
4254 if Is_Unc_Decl then
4255 null;
4257 -- For the unconstrained case, the replacement of the call has been
4258 -- made prior to the complete analysis of the generated declarations.
4259 -- Propagate the proper type now.
4261 elsif Is_Unc then
4262 if Nkind (N) = N_Identifier then
4263 Set_Etype (N, Etype (Entity (N)));
4264 else
4265 Set_Etype (N, Etype (Targ1));
4266 end if;
4267 end if;
4268 end if;
4270 Restore_Env;
4272 -- Cleanup mapping between formals and actuals for other expansions
4274 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4275 end Expand_Inlined_Call;
4277 --------------------------
4278 -- Get_Code_Unit_Entity --
4279 --------------------------
4281 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
4282 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
4284 begin
4285 if Ekind (Unit) = E_Package_Body then
4286 Unit := Spec_Entity (Unit);
4287 end if;
4289 return Unit;
4290 end Get_Code_Unit_Entity;
4292 ------------------------------
4293 -- Has_Excluded_Declaration --
4294 ------------------------------
4296 function Has_Excluded_Declaration
4297 (Subp : Entity_Id;
4298 Decls : List_Id) return Boolean
4300 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4301 -- Nested subprograms make a given body ineligible for inlining, but
4302 -- we make an exception for instantiations of unchecked conversion.
4303 -- The body has not been analyzed yet, so check the name, and verify
4304 -- that the visible entity with that name is the predefined unit.
4306 -----------------------------
4307 -- Is_Unchecked_Conversion --
4308 -----------------------------
4310 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4311 Id : constant Node_Id := Name (D);
4312 Conv : Entity_Id;
4314 begin
4315 if Nkind (Id) = N_Identifier
4316 and then Chars (Id) = Name_Unchecked_Conversion
4317 then
4318 Conv := Current_Entity (Id);
4320 elsif Nkind (Id) in N_Selected_Component | N_Expanded_Name
4321 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4322 then
4323 Conv := Current_Entity (Selector_Name (Id));
4324 else
4325 return False;
4326 end if;
4328 return Present (Conv)
4329 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
4330 and then Is_Intrinsic_Subprogram (Conv);
4331 end Is_Unchecked_Conversion;
4333 -- Local variables
4335 Decl : Node_Id;
4337 -- Start of processing for Has_Excluded_Declaration
4339 begin
4340 -- No action needed if the check is not needed
4342 if not Check_Inlining_Restrictions then
4343 return False;
4344 end if;
4346 Decl := First (Decls);
4347 while Present (Decl) loop
4349 -- First declarations universally excluded
4351 if Nkind (Decl) = N_Package_Declaration then
4352 Cannot_Inline
4353 ("cannot inline & (nested package declaration)?", Decl, Subp);
4354 return True;
4356 elsif Nkind (Decl) = N_Package_Instantiation then
4357 Cannot_Inline
4358 ("cannot inline & (nested package instantiation)?", Decl, Subp);
4359 return True;
4360 end if;
4362 -- Then declarations excluded only for front-end inlining
4364 if Back_End_Inlining then
4365 null;
4367 elsif Nkind (Decl) = N_Task_Type_Declaration
4368 or else Nkind (Decl) = N_Single_Task_Declaration
4369 then
4370 Cannot_Inline
4371 ("cannot inline & (nested task type declaration)?", Decl, Subp);
4372 return True;
4374 elsif Nkind (Decl) in N_Protected_Type_Declaration
4375 | N_Single_Protected_Declaration
4376 then
4377 Cannot_Inline
4378 ("cannot inline & (nested protected type declaration)?",
4379 Decl, Subp);
4380 return True;
4382 elsif Nkind (Decl) = N_Subprogram_Body then
4383 Cannot_Inline
4384 ("cannot inline & (nested subprogram)?", Decl, Subp);
4385 return True;
4387 elsif Nkind (Decl) = N_Function_Instantiation
4388 and then not Is_Unchecked_Conversion (Decl)
4389 then
4390 Cannot_Inline
4391 ("cannot inline & (nested function instantiation)?", Decl, Subp);
4392 return True;
4394 elsif Nkind (Decl) = N_Procedure_Instantiation then
4395 Cannot_Inline
4396 ("cannot inline & (nested procedure instantiation)?",
4397 Decl, Subp);
4398 return True;
4400 -- Subtype declarations with predicates will generate predicate
4401 -- functions, i.e. nested subprogram bodies, so inlining is not
4402 -- possible.
4404 elsif Nkind (Decl) = N_Subtype_Declaration then
4405 declare
4406 A : Node_Id;
4407 A_Id : Aspect_Id;
4409 begin
4410 A := First (Aspect_Specifications (Decl));
4411 while Present (A) loop
4412 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4414 if A_Id = Aspect_Predicate
4415 or else A_Id = Aspect_Static_Predicate
4416 or else A_Id = Aspect_Dynamic_Predicate
4417 then
4418 Cannot_Inline
4419 ("cannot inline & (subtype declaration with "
4420 & "predicate)?", Decl, Subp);
4421 return True;
4422 end if;
4424 Next (A);
4425 end loop;
4426 end;
4427 end if;
4429 Next (Decl);
4430 end loop;
4432 return False;
4433 end Has_Excluded_Declaration;
4435 ----------------------------
4436 -- Has_Excluded_Statement --
4437 ----------------------------
4439 function Has_Excluded_Statement
4440 (Subp : Entity_Id;
4441 Stats : List_Id) return Boolean
4443 S : Node_Id;
4444 E : Node_Id;
4446 begin
4447 -- No action needed if the check is not needed
4449 if not Check_Inlining_Restrictions then
4450 return False;
4451 end if;
4453 S := First (Stats);
4454 while Present (S) loop
4455 if Nkind (S) in N_Abort_Statement
4456 | N_Asynchronous_Select
4457 | N_Conditional_Entry_Call
4458 | N_Delay_Relative_Statement
4459 | N_Delay_Until_Statement
4460 | N_Selective_Accept
4461 | N_Timed_Entry_Call
4462 then
4463 Cannot_Inline
4464 ("cannot inline & (non-allowed statement)?", S, Subp);
4465 return True;
4467 elsif Nkind (S) = N_Block_Statement then
4468 if Has_Excluded_Declaration (Subp, Declarations (S)) then
4469 return True;
4471 elsif Present (Handled_Statement_Sequence (S)) then
4472 if not Back_End_Inlining
4473 and then
4474 Present
4475 (Exception_Handlers (Handled_Statement_Sequence (S)))
4476 then
4477 Cannot_Inline
4478 ("cannot inline& (exception handler)?",
4479 First (Exception_Handlers
4480 (Handled_Statement_Sequence (S))),
4481 Subp);
4482 return True;
4484 elsif Has_Excluded_Statement
4485 (Subp, Statements (Handled_Statement_Sequence (S)))
4486 then
4487 return True;
4488 end if;
4489 end if;
4491 elsif Nkind (S) = N_Case_Statement then
4492 E := First (Alternatives (S));
4493 while Present (E) loop
4494 if Has_Excluded_Statement (Subp, Statements (E)) then
4495 return True;
4496 end if;
4498 Next (E);
4499 end loop;
4501 elsif Nkind (S) = N_If_Statement then
4502 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
4503 return True;
4504 end if;
4506 if Present (Elsif_Parts (S)) then
4507 E := First (Elsif_Parts (S));
4508 while Present (E) loop
4509 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
4510 return True;
4511 end if;
4513 Next (E);
4514 end loop;
4515 end if;
4517 if Present (Else_Statements (S))
4518 and then Has_Excluded_Statement (Subp, Else_Statements (S))
4519 then
4520 return True;
4521 end if;
4523 elsif Nkind (S) = N_Loop_Statement
4524 and then Has_Excluded_Statement (Subp, Statements (S))
4525 then
4526 return True;
4528 elsif Nkind (S) = N_Extended_Return_Statement then
4529 if Present (Handled_Statement_Sequence (S))
4530 and then
4531 Has_Excluded_Statement
4532 (Subp, Statements (Handled_Statement_Sequence (S)))
4533 then
4534 return True;
4536 elsif not Back_End_Inlining
4537 and then Present (Handled_Statement_Sequence (S))
4538 and then
4539 Present (Exception_Handlers
4540 (Handled_Statement_Sequence (S)))
4541 then
4542 Cannot_Inline
4543 ("cannot inline& (exception handler)?",
4544 First (Exception_Handlers (Handled_Statement_Sequence (S))),
4545 Subp);
4546 return True;
4547 end if;
4548 end if;
4550 Next (S);
4551 end loop;
4553 return False;
4554 end Has_Excluded_Statement;
4556 --------------------------
4557 -- Has_Initialized_Type --
4558 --------------------------
4560 function Has_Initialized_Type (E : Entity_Id) return Boolean is
4561 E_Body : constant Node_Id := Subprogram_Body (E);
4562 Decl : Node_Id;
4564 begin
4565 if No (E_Body) then -- imported subprogram
4566 return False;
4568 else
4569 Decl := First (Declarations (E_Body));
4570 while Present (Decl) loop
4571 if Nkind (Decl) = N_Full_Type_Declaration
4572 and then Comes_From_Source (Decl)
4573 and then Present (Init_Proc (Defining_Identifier (Decl)))
4574 then
4575 return True;
4576 end if;
4578 Next (Decl);
4579 end loop;
4580 end if;
4582 return False;
4583 end Has_Initialized_Type;
4585 -----------------------
4586 -- Has_Single_Return --
4587 -----------------------
4589 function Has_Single_Return (N : Node_Id) return Boolean is
4590 Return_Statement : Node_Id := Empty;
4592 function Check_Return (N : Node_Id) return Traverse_Result;
4594 ------------------
4595 -- Check_Return --
4596 ------------------
4598 function Check_Return (N : Node_Id) return Traverse_Result is
4599 begin
4600 if Nkind (N) = N_Simple_Return_Statement then
4601 if Present (Expression (N))
4602 and then Is_Entity_Name (Expression (N))
4603 then
4604 pragma Assert (Present (Entity (Expression (N))));
4606 if No (Return_Statement) then
4607 Return_Statement := N;
4608 return OK;
4610 else
4611 pragma Assert
4612 (Present (Entity (Expression (Return_Statement))));
4614 if Entity (Expression (N)) =
4615 Entity (Expression (Return_Statement))
4616 then
4617 return OK;
4618 else
4619 return Abandon;
4620 end if;
4621 end if;
4623 -- A return statement within an extended return is a noop after
4624 -- inlining.
4626 elsif No (Expression (N))
4627 and then Nkind (Parent (Parent (N))) =
4628 N_Extended_Return_Statement
4629 then
4630 return OK;
4632 else
4633 -- Expression has wrong form
4635 return Abandon;
4636 end if;
4638 -- We can only inline a build-in-place function if it has a single
4639 -- extended return.
4641 elsif Nkind (N) = N_Extended_Return_Statement then
4642 if No (Return_Statement) then
4643 Return_Statement := N;
4644 return OK;
4646 else
4647 return Abandon;
4648 end if;
4650 else
4651 return OK;
4652 end if;
4653 end Check_Return;
4655 function Check_All_Returns is new Traverse_Func (Check_Return);
4657 -- Start of processing for Has_Single_Return
4659 begin
4660 if Check_All_Returns (N) /= OK then
4661 return False;
4663 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4664 return True;
4666 else
4667 return
4668 Present (Declarations (N))
4669 and then Present (First (Declarations (N)))
4670 and then Nkind (First (Declarations (N))) = N_Object_Declaration
4671 and then Entity (Expression (Return_Statement)) =
4672 Defining_Identifier (First (Declarations (N)));
4673 end if;
4674 end Has_Single_Return;
4676 -----------------------------
4677 -- In_Main_Unit_Or_Subunit --
4678 -----------------------------
4680 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
4681 Comp : Node_Id := Cunit (Get_Code_Unit (E));
4683 begin
4684 -- Check whether the subprogram or package to inline is within the main
4685 -- unit or its spec or within a subunit. In either case there are no
4686 -- additional bodies to process. If the subprogram appears in a parent
4687 -- of the current unit, the check on whether inlining is possible is
4688 -- done in Analyze_Inlined_Bodies.
4690 while Nkind (Unit (Comp)) = N_Subunit loop
4691 Comp := Library_Unit (Comp);
4692 end loop;
4694 return Comp = Cunit (Main_Unit)
4695 or else Comp = Library_Unit (Cunit (Main_Unit));
4696 end In_Main_Unit_Or_Subunit;
4698 ----------------
4699 -- Initialize --
4700 ----------------
4702 procedure Initialize is
4703 begin
4704 Pending_Instantiations.Init;
4705 Called_Pending_Instantiations.Init;
4706 Inlined_Bodies.Init;
4707 Successors.Init;
4708 Inlined.Init;
4710 for J in Hash_Headers'Range loop
4711 Hash_Headers (J) := No_Subp;
4712 end loop;
4714 Inlined_Calls := No_Elist;
4715 Backend_Calls := No_Elist;
4716 Backend_Instances := No_Elist;
4717 Backend_Inlined_Subps := No_Elist;
4718 Backend_Not_Inlined_Subps := No_Elist;
4719 end Initialize;
4721 ---------------------------------
4722 -- Inline_Static_Function_Call --
4723 ---------------------------------
4725 procedure Inline_Static_Function_Call (N : Node_Id; Subp : Entity_Id) is
4727 function Replace_Formal (N : Node_Id) return Traverse_Result;
4728 -- Replace each occurrence of a formal with the
4729 -- corresponding actual, using the mapping created
4730 -- by Establish_Actual_Mapping_For_Inlined_Call.
4732 function Reset_Sloc (Nod : Node_Id) return Traverse_Result;
4733 -- Reset the Sloc of a node to that of the call itself, so that errors
4734 -- will be flagged on the call to the static expression function itself
4735 -- rather than on the expression of the function's declaration.
4737 --------------------
4738 -- Replace_Formal --
4739 --------------------
4741 function Replace_Formal (N : Node_Id) return Traverse_Result is
4742 A : Entity_Id;
4743 E : Entity_Id;
4745 begin
4746 if Is_Entity_Name (N) and then Present (Entity (N)) then
4747 E := Entity (N);
4749 if Is_Formal (E) and then Scope (E) = Subp then
4750 A := Renamed_Object (E);
4752 if Nkind (A) = N_Defining_Identifier then
4753 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
4755 -- Literal cases
4757 else
4758 Rewrite (N, New_Copy (A));
4759 end if;
4760 end if;
4762 return Skip;
4764 else
4765 return OK;
4766 end if;
4767 end Replace_Formal;
4769 procedure Replace_Formals is new Traverse_Proc (Replace_Formal);
4771 ------------------
4772 -- Process_Sloc --
4773 ------------------
4775 function Reset_Sloc (Nod : Node_Id) return Traverse_Result is
4776 begin
4777 Set_Sloc (Nod, Sloc (N));
4778 Set_Comes_From_Source (Nod, False);
4780 return OK;
4781 end Reset_Sloc;
4783 procedure Reset_Slocs is new Traverse_Proc (Reset_Sloc);
4785 -- Start of processing for Inline_Static_Function_Call
4787 begin
4788 pragma Assert (Is_Static_Function_Call (N));
4790 declare
4791 Decls : constant List_Id := New_List;
4792 Func_Expr : constant Node_Id :=
4793 Expression_Of_Expression_Function (Subp);
4794 Expr_Copy : constant Node_Id := New_Copy_Tree (Func_Expr);
4796 begin
4797 -- Create a mapping from formals to actuals, also creating temps in
4798 -- Decls, when needed, to hold the actuals.
4800 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Func_Expr);
4802 -- Ensure that the copy has the same parent as the call (this seems
4803 -- to matter when GNATprove_Mode is set and there are nested static
4804 -- calls; prevents blowups in Insert_Actions, though it's not clear
4805 -- exactly why this is needed???).
4807 Set_Parent (Expr_Copy, Parent (N));
4809 Insert_Actions (N, Decls);
4811 -- Now substitute actuals for their corresponding formal references
4812 -- within the expression.
4814 Replace_Formals (Expr_Copy);
4816 Reset_Slocs (Expr_Copy);
4818 -- Apply a qualified expression with the function's result subtype,
4819 -- to ensure that we check the expression against any constraint
4820 -- or predicate, which will cause the call to be illegal if the
4821 -- folded expression doesn't satisfy them. (The predicate case
4822 -- might not get checked if the subtype hasn't been frozen yet,
4823 -- which can happen if this static expression happens to be what
4824 -- causes the freezing, because Has_Static_Predicate doesn't get
4825 -- set on the subtype until it's frozen and Build_Predicates is
4826 -- called. It's not clear how to address this case. ???)
4828 Rewrite (Expr_Copy,
4829 Make_Qualified_Expression (Sloc (Expr_Copy),
4830 Subtype_Mark =>
4831 New_Occurrence_Of (Etype (N), Sloc (Expr_Copy)),
4832 Expression =>
4833 Relocate_Node (Expr_Copy)));
4835 Set_Etype (Expr_Copy, Etype (N));
4837 Analyze_And_Resolve (Expr_Copy, Etype (N));
4839 -- Finally rewrite the function call as the folded static result
4841 Rewrite (N, Expr_Copy);
4843 -- Cleanup mapping between formals and actuals for other expansions
4845 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4846 end;
4847 end Inline_Static_Function_Call;
4849 ------------------------
4850 -- Instantiate_Bodies --
4851 ------------------------
4853 -- Generic bodies contain all the non-local references, so an
4854 -- instantiation does not need any more context than Standard
4855 -- itself, even if the instantiation appears in an inner scope.
4856 -- Generic associations have verified that the contract model is
4857 -- satisfied, so that any error that may occur in the analysis of
4858 -- the body is an internal error.
4860 procedure Instantiate_Bodies is
4862 procedure Instantiate_Body (Info : Pending_Body_Info);
4863 -- Instantiate a pending body
4865 ------------------------
4866 -- Instantiate_Body --
4867 ------------------------
4869 procedure Instantiate_Body (Info : Pending_Body_Info) is
4870 Scop : Entity_Id;
4872 begin
4873 -- If the instantiation node is absent, it has been removed as part
4874 -- of unreachable code.
4876 if No (Info.Inst_Node) then
4877 null;
4879 -- If the instantiation node is a package body, this means that the
4880 -- instance is a compilation unit and the instantiation has already
4881 -- been performed by Build_Instance_Compilation_Unit_Nodes.
4883 elsif Nkind (Info.Inst_Node) = N_Package_Body then
4884 null;
4886 -- For other package instances, instantiate the body and register the
4887 -- finalization scope, if any, for subsequent generation of cleanups.
4889 elsif Nkind (Info.Inst_Node) = N_Package_Instantiation then
4891 -- If the enclosing finalization scope is a package body, set the
4892 -- In_Package_Body flag on its spec. This is required, in the case
4893 -- where the body contains other package instantiations that have
4894 -- a body, for Analyze_Package_Instantiation to compute a correct
4895 -- finalization scope.
4897 if Present (Info.Fin_Scop)
4898 and then Ekind (Info.Fin_Scop) = E_Package_Body
4899 then
4900 Set_In_Package_Body (Spec_Entity (Info.Fin_Scop), True);
4901 end if;
4903 Instantiate_Package_Body (Info);
4905 if Present (Info.Fin_Scop) then
4906 Scop := Info.Fin_Scop;
4908 -- If the enclosing finalization scope is dynamic, the instance
4909 -- may have been relocated, for example if it was declared in a
4910 -- protected entry, protected subprogram, or task body.
4912 if Is_Dynamic_Scope (Scop) then
4913 Scop :=
4914 Enclosing_Dynamic_Scope (Defining_Entity (Info.Act_Decl));
4915 end if;
4917 Add_Scope_To_Clean (Scop);
4919 -- Reset the In_Package_Body flag if it was set above
4921 if Ekind (Info.Fin_Scop) = E_Package_Body then
4922 Set_In_Package_Body (Spec_Entity (Info.Fin_Scop), False);
4923 end if;
4924 end if;
4926 -- For subprogram instances, always instantiate the body
4928 else
4929 Instantiate_Subprogram_Body (Info);
4930 end if;
4931 end Instantiate_Body;
4933 J, K : Nat;
4934 Info : Pending_Body_Info;
4936 -- Start of processing for Instantiate_Bodies
4938 begin
4939 if Serious_Errors_Detected = 0 then
4940 Expander_Active := (Operating_Mode = Opt.Generate_Code);
4941 Push_Scope (Standard_Standard);
4942 To_Clean := New_Elmt_List;
4944 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4945 Start_Generic;
4946 end if;
4948 -- A body instantiation may generate additional instantiations, so
4949 -- the following loop must scan to the end of a possibly expanding
4950 -- set (that's why we cannot simply use a FOR loop here). We must
4951 -- also capture the element lest the set be entirely reallocated.
4953 J := 0;
4954 if Back_End_Inlining then
4955 while J <= Called_Pending_Instantiations.Last
4956 and then Serious_Errors_Detected = 0
4957 loop
4958 K := Called_Pending_Instantiations.Table (J);
4959 Info := Pending_Instantiations.Table (K);
4960 Instantiate_Body (Info);
4962 J := J + 1;
4963 end loop;
4965 else
4966 while J <= Pending_Instantiations.Last
4967 and then Serious_Errors_Detected = 0
4968 loop
4969 Info := Pending_Instantiations.Table (J);
4970 Instantiate_Body (Info);
4972 J := J + 1;
4973 end loop;
4974 end if;
4976 -- Reset the table of instantiations. Additional instantiations
4977 -- may be added through inlining, when additional bodies are
4978 -- analyzed.
4980 if Back_End_Inlining then
4981 Called_Pending_Instantiations.Init;
4982 else
4983 Pending_Instantiations.Init;
4984 end if;
4986 -- We can now complete the cleanup actions of scopes that contain
4987 -- pending instantiations (skipped for generic units, since we
4988 -- never need any cleanups in generic units).
4990 if Expander_Active
4991 and then not Is_Generic_Unit (Main_Unit_Entity)
4992 then
4993 Cleanup_Scopes;
4994 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4995 End_Generic;
4996 end if;
4998 Pop_Scope;
4999 end if;
5000 end Instantiate_Bodies;
5002 ---------------
5003 -- Is_Nested --
5004 ---------------
5006 function Is_Nested (E : Entity_Id) return Boolean is
5007 Scop : Entity_Id;
5009 begin
5010 Scop := Scope (E);
5011 while Scop /= Standard_Standard loop
5012 if Is_Subprogram (Scop) then
5013 return True;
5015 elsif Ekind (Scop) = E_Task_Type
5016 or else Ekind (Scop) = E_Entry
5017 or else Ekind (Scop) = E_Entry_Family
5018 then
5019 return True;
5020 end if;
5022 Scop := Scope (Scop);
5023 end loop;
5025 return False;
5026 end Is_Nested;
5028 ------------------------
5029 -- List_Inlining_Info --
5030 ------------------------
5032 procedure List_Inlining_Info is
5033 Elmt : Elmt_Id;
5034 Nod : Node_Id;
5035 Count : Nat;
5037 begin
5038 if not Debug_Flag_Dot_J then
5039 return;
5040 end if;
5042 -- Generate listing of calls inlined by the frontend
5044 if Present (Inlined_Calls) then
5045 Count := 0;
5046 Elmt := First_Elmt (Inlined_Calls);
5047 while Present (Elmt) loop
5048 Nod := Node (Elmt);
5050 if not In_Internal_Unit (Nod) then
5051 Count := Count + 1;
5053 if Count = 1 then
5054 Write_Str ("List of calls inlined by the frontend");
5055 Write_Eol;
5056 end if;
5058 Write_Str (" ");
5059 Write_Int (Count);
5060 Write_Str (":");
5061 Write_Location (Sloc (Nod));
5062 Write_Str (":");
5063 Output.Write_Eol;
5064 end if;
5066 Next_Elmt (Elmt);
5067 end loop;
5068 end if;
5070 -- Generate listing of calls passed to the backend
5072 if Present (Backend_Calls) then
5073 Count := 0;
5075 Elmt := First_Elmt (Backend_Calls);
5076 while Present (Elmt) loop
5077 Nod := Node (Elmt);
5079 if not In_Internal_Unit (Nod) then
5080 Count := Count + 1;
5082 if Count = 1 then
5083 Write_Str ("List of inlined calls passed to the backend");
5084 Write_Eol;
5085 end if;
5087 Write_Str (" ");
5088 Write_Int (Count);
5089 Write_Str (":");
5090 Write_Location (Sloc (Nod));
5091 Output.Write_Eol;
5092 end if;
5094 Next_Elmt (Elmt);
5095 end loop;
5096 end if;
5098 -- Generate listing of instances inlined for the backend
5100 if Present (Backend_Instances) then
5101 Count := 0;
5103 Elmt := First_Elmt (Backend_Instances);
5104 while Present (Elmt) loop
5105 Nod := Node (Elmt);
5107 if not In_Internal_Unit (Nod) then
5108 Count := Count + 1;
5110 if Count = 1 then
5111 Write_Str ("List of instances inlined for the backend");
5112 Write_Eol;
5113 end if;
5115 Write_Str (" ");
5116 Write_Int (Count);
5117 Write_Str (":");
5118 Write_Location (Sloc (Nod));
5119 Output.Write_Eol;
5120 end if;
5122 Next_Elmt (Elmt);
5123 end loop;
5124 end if;
5126 -- Generate listing of subprograms passed to the backend
5128 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
5129 Count := 0;
5131 Elmt := First_Elmt (Backend_Inlined_Subps);
5132 while Present (Elmt) loop
5133 Nod := Node (Elmt);
5135 if not In_Internal_Unit (Nod) then
5136 Count := Count + 1;
5138 if Count = 1 then
5139 Write_Str
5140 ("List of inlined subprograms passed to the backend");
5141 Write_Eol;
5142 end if;
5144 Write_Str (" ");
5145 Write_Int (Count);
5146 Write_Str (":");
5147 Write_Name (Chars (Nod));
5148 Write_Str (" (");
5149 Write_Location (Sloc (Nod));
5150 Write_Str (")");
5151 Output.Write_Eol;
5152 end if;
5154 Next_Elmt (Elmt);
5155 end loop;
5156 end if;
5158 -- Generate listing of subprograms that cannot be inlined by the backend
5160 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
5161 Count := 0;
5163 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
5164 while Present (Elmt) loop
5165 Nod := Node (Elmt);
5167 if not In_Internal_Unit (Nod) then
5168 Count := Count + 1;
5170 if Count = 1 then
5171 Write_Str
5172 ("List of subprograms that cannot be inlined by backend");
5173 Write_Eol;
5174 end if;
5176 Write_Str (" ");
5177 Write_Int (Count);
5178 Write_Str (":");
5179 Write_Name (Chars (Nod));
5180 Write_Str (" (");
5181 Write_Location (Sloc (Nod));
5182 Write_Str (")");
5183 Output.Write_Eol;
5184 end if;
5186 Next_Elmt (Elmt);
5187 end loop;
5188 end if;
5189 end List_Inlining_Info;
5191 ----------
5192 -- Lock --
5193 ----------
5195 procedure Lock is
5196 begin
5197 Pending_Instantiations.Release;
5198 Pending_Instantiations.Locked := True;
5199 Called_Pending_Instantiations.Release;
5200 Called_Pending_Instantiations.Locked := True;
5201 Inlined_Bodies.Release;
5202 Inlined_Bodies.Locked := True;
5203 Successors.Release;
5204 Successors.Locked := True;
5205 Inlined.Release;
5206 Inlined.Locked := True;
5207 end Lock;
5209 --------------------------------
5210 -- Remove_Aspects_And_Pragmas --
5211 --------------------------------
5213 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
5214 procedure Remove_Items (List : List_Id);
5215 -- Remove all useless aspects/pragmas from a particular list
5217 ------------------
5218 -- Remove_Items --
5219 ------------------
5221 procedure Remove_Items (List : List_Id) is
5222 Item : Node_Id;
5223 Item_Id : Node_Id;
5224 Next_Item : Node_Id;
5226 begin
5227 -- Traverse the list looking for an aspect specification or a pragma
5229 Item := First (List);
5230 while Present (Item) loop
5231 Next_Item := Next (Item);
5233 if Nkind (Item) = N_Aspect_Specification then
5234 Item_Id := Identifier (Item);
5235 elsif Nkind (Item) = N_Pragma then
5236 Item_Id := Pragma_Identifier (Item);
5237 else
5238 Item_Id := Empty;
5239 end if;
5241 if Present (Item_Id)
5242 and then Chars (Item_Id) in Name_Always_Terminates
5243 | Name_Contract_Cases
5244 | Name_Global
5245 | Name_Depends
5246 | Name_Exceptional_Cases
5247 | Name_Postcondition
5248 | Name_Precondition
5249 | Name_Refined_Global
5250 | Name_Refined_Depends
5251 | Name_Refined_Post
5252 | Name_Subprogram_Variant
5253 | Name_Test_Case
5254 | Name_Unmodified
5255 | Name_Unreferenced
5256 | Name_Unused
5257 then
5258 Remove (Item);
5259 end if;
5261 Item := Next_Item;
5262 end loop;
5263 end Remove_Items;
5265 -- Start of processing for Remove_Aspects_And_Pragmas
5267 begin
5268 Remove_Items (Aspect_Specifications (Body_Decl));
5269 Remove_Items (Declarations (Body_Decl));
5271 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
5272 -- in the body of the subprogram.
5274 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
5275 end Remove_Aspects_And_Pragmas;
5277 --------------------------
5278 -- Remove_Dead_Instance --
5279 --------------------------
5281 procedure Remove_Dead_Instance (N : Node_Id) is
5282 begin
5283 for J in 0 .. Pending_Instantiations.Last loop
5284 if Pending_Instantiations.Table (J).Inst_Node = N then
5285 Pending_Instantiations.Table (J).Inst_Node := Empty;
5286 return;
5287 end if;
5288 end loop;
5289 end Remove_Dead_Instance;
5291 -------------------------------------------
5292 -- Reset_Actual_Mapping_For_Inlined_Call --
5293 -------------------------------------------
5295 procedure Reset_Actual_Mapping_For_Inlined_Call (Subp : Entity_Id) is
5296 F : Entity_Id := First_Formal (Subp);
5298 begin
5299 while Present (F) loop
5300 Set_Renamed_Object (F, Empty);
5301 Next_Formal (F);
5302 end loop;
5303 end Reset_Actual_Mapping_For_Inlined_Call;
5305 end Inline;