[Ada] Further cleanup in inlining machinery
[official-gcc.git] / gcc / ada / inline.adb
blob84ad1abe9181bc480b4cfaed5e8baf37289c32c7
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-2019, 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 Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Fname.UF; use Fname.UF;
40 with Lib; use Lib;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Output; use Output;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Ch10; use Sem_Ch10;
48 with Sem_Ch12; use Sem_Ch12;
49 with Sem_Prag; use Sem_Prag;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinput; use Sinput;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Table;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Uname; use Uname;
60 with GNAT.HTable;
62 package body Inline is
64 Check_Inlining_Restrictions : constant Boolean := True;
65 -- In the following cases the frontend rejects inlining because they
66 -- are not handled well by the backend. This variable facilitates
67 -- disabling these restrictions to evaluate future versions of the
68 -- GCC backend in which some of the restrictions may be supported.
70 -- - subprograms that have:
71 -- - nested subprograms
72 -- - instantiations
73 -- - package declarations
74 -- - task or protected object declarations
75 -- - some of the following statements:
76 -- - abort
77 -- - asynchronous-select
78 -- - conditional-entry-call
79 -- - delay-relative
80 -- - delay-until
81 -- - selective-accept
82 -- - timed-entry-call
84 Inlined_Calls : Elist_Id;
85 -- List of frontend inlined calls
87 Backend_Calls : Elist_Id;
88 -- List of inline calls passed to the backend
90 Backend_Instances : Elist_Id;
91 -- List of instances inlined for the backend
93 Backend_Inlined_Subps : Elist_Id;
94 -- List of subprograms inlined by the backend
96 Backend_Not_Inlined_Subps : Elist_Id;
97 -- List of subprograms that cannot be inlined by the backend
99 -----------------------------
100 -- Pending_Instantiations --
101 -----------------------------
103 -- We make entries in this table for the pending instantiations of generic
104 -- bodies that are created during semantic analysis. After the analysis is
105 -- complete, calling Instantiate_Bodies performs the actual instantiations.
107 package Pending_Instantiations is new Table.Table (
108 Table_Component_Type => Pending_Body_Info,
109 Table_Index_Type => Int,
110 Table_Low_Bound => 0,
111 Table_Initial => Alloc.Pending_Instantiations_Initial,
112 Table_Increment => Alloc.Pending_Instantiations_Increment,
113 Table_Name => "Pending_Instantiations");
115 -------------------------------------
116 -- Called_Pending_Instantiations --
117 -------------------------------------
119 -- With back-end inlining, the pending instantiations that are not in the
120 -- main unit or subunit are performed only after a call to the subprogram
121 -- instance, or to a subprogram within the package instance, is inlined.
122 -- Since such a call can be within a subsequent pending instantiation,
123 -- we make entries in this table that stores the index of these "called"
124 -- pending instantiations and perform them when the table is populated.
126 package Called_Pending_Instantiations is new Table.Table (
127 Table_Component_Type => Int,
128 Table_Index_Type => Int,
129 Table_Low_Bound => 0,
130 Table_Initial => Alloc.Pending_Instantiations_Initial,
131 Table_Increment => Alloc.Pending_Instantiations_Increment,
132 Table_Name => "Called_Pending_Instantiations");
134 ---------------------------------
135 -- To_Pending_Instantiations --
136 ---------------------------------
138 -- With back-end inlining, we also need to have a map from the pending
139 -- instantiations to their index in the Pending_Instantiations table.
141 Node_Table_Size : constant := 257;
142 -- Number of headers in hash table
144 subtype Node_Header_Num is Integer range 0 .. Node_Table_Size - 1;
145 -- Range of headers in hash table
147 function Node_Hash (Id : Node_Id) return Node_Header_Num;
148 -- Simple hash function for Node_Ids
150 package To_Pending_Instantiations is new GNAT.Htable.Simple_HTable
151 (Header_Num => Node_Header_Num,
152 Element => Int,
153 No_Element => -1,
154 Key => Node_Id,
155 Hash => Node_Hash,
156 Equal => "=");
158 -----------------
159 -- Node_Hash --
160 -----------------
162 function Node_Hash (Id : Node_Id) return Node_Header_Num is
163 begin
164 return Node_Header_Num (Id mod Node_Table_Size);
165 end Node_Hash;
167 --------------------
168 -- Inlined Bodies --
169 --------------------
171 -- Inlined functions are actually placed in line by the backend if the
172 -- corresponding bodies are available (i.e. compiled). Whenever we find
173 -- a call to an inlined subprogram, we add the name of the enclosing
174 -- compilation unit to a worklist. After all compilation, and after
175 -- expansion of generic bodies, we traverse the list of pending bodies
176 -- and compile them as well.
178 package Inlined_Bodies is new Table.Table (
179 Table_Component_Type => Entity_Id,
180 Table_Index_Type => Int,
181 Table_Low_Bound => 0,
182 Table_Initial => Alloc.Inlined_Bodies_Initial,
183 Table_Increment => Alloc.Inlined_Bodies_Increment,
184 Table_Name => "Inlined_Bodies");
186 -----------------------
187 -- Inline Processing --
188 -----------------------
190 -- For each call to an inlined subprogram, we make entries in a table
191 -- that stores caller and callee, and indicates the call direction from
192 -- one to the other. We also record the compilation unit that contains
193 -- the callee. After analyzing the bodies of all such compilation units,
194 -- we compute the transitive closure of inlined subprograms called from
195 -- the main compilation unit and make it available to the code generator
196 -- in no particular order, thus allowing cycles in the call graph.
198 Last_Inlined : Entity_Id := Empty;
200 -- For each entry in the table we keep a list of successors in topological
201 -- order, i.e. callers of the current subprogram.
203 type Subp_Index is new Nat;
204 No_Subp : constant Subp_Index := 0;
206 -- The subprogram entities are hashed into the Inlined table
208 Num_Hash_Headers : constant := 512;
210 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
211 of Subp_Index;
213 type Succ_Index is new Nat;
214 No_Succ : constant Succ_Index := 0;
216 type Succ_Info is record
217 Subp : Subp_Index;
218 Next : Succ_Index;
219 end record;
221 -- The following table stores list elements for the successor lists. These
222 -- lists cannot be chained directly through entries in the Inlined table,
223 -- because a given subprogram can appear in several such lists.
225 package Successors is new Table.Table (
226 Table_Component_Type => Succ_Info,
227 Table_Index_Type => Succ_Index,
228 Table_Low_Bound => 1,
229 Table_Initial => Alloc.Successors_Initial,
230 Table_Increment => Alloc.Successors_Increment,
231 Table_Name => "Successors");
233 type Subp_Info is record
234 Name : Entity_Id := Empty;
235 Next : Subp_Index := No_Subp;
236 First_Succ : Succ_Index := No_Succ;
237 Main_Call : Boolean := False;
238 Processed : Boolean := False;
239 end record;
241 package Inlined is new Table.Table (
242 Table_Component_Type => Subp_Info,
243 Table_Index_Type => Subp_Index,
244 Table_Low_Bound => 1,
245 Table_Initial => Alloc.Inlined_Initial,
246 Table_Increment => Alloc.Inlined_Increment,
247 Table_Name => "Inlined");
249 -----------------------
250 -- Local Subprograms --
251 -----------------------
253 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
254 -- Make two entries in Inlined table, for an inlined subprogram being
255 -- called, and for the inlined subprogram that contains the call. If
256 -- the call is in the main compilation unit, Caller is Empty.
258 procedure Add_Inlined_Instance (E : Entity_Id);
259 -- Add instance E to the list of of inlined instances for the unit
261 procedure Add_Inlined_Subprogram (E : Entity_Id);
262 -- Add subprogram E to the list of inlined subprograms for the unit
264 function Add_Subp (E : Entity_Id) return Subp_Index;
265 -- Make entry in Inlined table for subprogram E, or return table index
266 -- that already holds E.
268 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id;
269 pragma Inline (Get_Code_Unit_Entity);
270 -- Return the entity node for the unit containing E. Always return the spec
271 -- for a package.
273 function Has_Initialized_Type (E : Entity_Id) return Boolean;
274 -- If a candidate for inlining contains type declarations for types with
275 -- nontrivial initialization procedures, they are not worth inlining.
277 function Has_Single_Return (N : Node_Id) return Boolean;
278 -- In general we cannot inline functions that return unconstrained type.
279 -- However, we can handle such functions if all return statements return
280 -- a local variable that is the first declaration in the body of the
281 -- function. In that case the call can be replaced by that local
282 -- variable as is done for other inlined calls.
284 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean;
285 -- Return True if E is in the main unit or its spec or in a subunit
287 function Is_Nested (E : Entity_Id) return Boolean;
288 -- If the function is nested inside some other function, it will always
289 -- be compiled if that function is, so don't add it to the inline list.
290 -- We cannot compile a nested function outside the scope of the containing
291 -- function anyway. This is also the case if the function is defined in a
292 -- task body or within an entry (for example, an initialization procedure).
294 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id);
295 -- Remove all aspects and/or pragmas that have no meaning in inlined body
296 -- Body_Decl. The analysis of these items is performed on the non-inlined
297 -- body. The items currently removed are:
298 -- Contract_Cases
299 -- Global
300 -- Depends
301 -- Postcondition
302 -- Precondition
303 -- Refined_Global
304 -- Refined_Depends
305 -- Refined_Post
306 -- Test_Case
307 -- Unmodified
308 -- Unreferenced
310 ------------------------------
311 -- Deferred Cleanup Actions --
312 ------------------------------
314 -- The cleanup actions for scopes that contain instantiations is delayed
315 -- until after expansion of those instantiations, because they may contain
316 -- finalizable objects or tasks that affect the cleanup code. A scope
317 -- that contains instantiations only needs to be finalized once, even
318 -- if it contains more than one instance. We keep a list of scopes
319 -- that must still be finalized, and call cleanup_actions after all
320 -- the instantiations have been completed.
322 To_Clean : Elist_Id;
324 procedure Add_Scope_To_Clean (Inst : Entity_Id);
325 -- Build set of scopes on which cleanup actions must be performed
327 procedure Cleanup_Scopes;
328 -- Complete cleanup actions on scopes that need it
330 --------------
331 -- Add_Call --
332 --------------
334 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
335 P1 : constant Subp_Index := Add_Subp (Called);
336 P2 : Subp_Index;
337 J : Succ_Index;
339 begin
340 if Present (Caller) then
341 P2 := Add_Subp (Caller);
343 -- Add P1 to the list of successors of P2, if not already there.
344 -- Note that P2 may contain more than one call to P1, and only
345 -- one needs to be recorded.
347 J := Inlined.Table (P2).First_Succ;
348 while J /= No_Succ loop
349 if Successors.Table (J).Subp = P1 then
350 return;
351 end if;
353 J := Successors.Table (J).Next;
354 end loop;
356 -- On exit, make a successor entry for P1
358 Successors.Increment_Last;
359 Successors.Table (Successors.Last).Subp := P1;
360 Successors.Table (Successors.Last).Next :=
361 Inlined.Table (P2).First_Succ;
362 Inlined.Table (P2).First_Succ := Successors.Last;
363 else
364 Inlined.Table (P1).Main_Call := True;
365 end if;
366 end Add_Call;
368 ----------------------
369 -- Add_Inlined_Body --
370 ----------------------
372 procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id) is
374 type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package);
375 -- Level of inlining for the call: Dont_Inline means no inlining,
376 -- Inline_Call means that only the call is considered for inlining,
377 -- Inline_Package means that the call is considered for inlining and
378 -- its package compiled and scanned for more inlining opportunities.
380 function Is_Non_Loading_Expression_Function
381 (Id : Entity_Id) return Boolean;
382 -- Determine whether arbitrary entity Id denotes a subprogram which is
383 -- either
385 -- * An expression function
387 -- * A function completed by an expression function where both the
388 -- spec and body are in the same context.
390 function Must_Inline return Inline_Level_Type;
391 -- Inlining is only done if the call statement N is in the main unit,
392 -- or within the body of another inlined subprogram.
394 ----------------------------------------
395 -- Is_Non_Loading_Expression_Function --
396 ----------------------------------------
398 function Is_Non_Loading_Expression_Function
399 (Id : Entity_Id) return Boolean
401 Body_Decl : Node_Id;
402 Body_Id : Entity_Id;
403 Spec_Decl : Node_Id;
405 begin
406 -- A stand-alone expression function is transformed into a spec-body
407 -- pair in-place. Since both the spec and body are in the same list,
408 -- the inlining of such an expression function does not need to load
409 -- anything extra.
411 if Is_Expression_Function (Id) then
412 return True;
414 -- A function may be completed by an expression function
416 elsif Ekind (Id) = E_Function then
417 Spec_Decl := Unit_Declaration_Node (Id);
419 if Nkind (Spec_Decl) = N_Subprogram_Declaration then
420 Body_Id := Corresponding_Body (Spec_Decl);
422 if Present (Body_Id) then
423 Body_Decl := Unit_Declaration_Node (Body_Id);
425 -- The inlining of a completing expression function does
426 -- not need to load anything extra when both the spec and
427 -- body are in the same context.
429 return
430 Was_Expression_Function (Body_Decl)
431 and then Parent (Spec_Decl) = Parent (Body_Decl);
432 end if;
433 end if;
434 end if;
436 return False;
437 end Is_Non_Loading_Expression_Function;
439 -----------------
440 -- Must_Inline --
441 -----------------
443 function Must_Inline return Inline_Level_Type is
444 Scop : Entity_Id;
445 Comp : Node_Id;
447 begin
448 -- Check if call is in main unit
450 Scop := Current_Scope;
452 -- Do not try to inline if scope is standard. This could happen, for
453 -- example, for a call to Add_Global_Declaration, and it causes
454 -- trouble to try to inline at this level.
456 if Scop = Standard_Standard then
457 return Dont_Inline;
458 end if;
460 -- Otherwise lookup scope stack to outer scope
462 while Scope (Scop) /= Standard_Standard
463 and then not Is_Child_Unit (Scop)
464 loop
465 Scop := Scope (Scop);
466 end loop;
468 Comp := Parent (Scop);
469 while Nkind (Comp) /= N_Compilation_Unit loop
470 Comp := Parent (Comp);
471 end loop;
473 -- If the call is in the main unit, inline the call and compile the
474 -- package of the subprogram to find more calls to be inlined.
476 if Comp = Cunit (Main_Unit)
477 or else Comp = Library_Unit (Cunit (Main_Unit))
478 then
479 Add_Call (E);
480 return Inline_Package;
481 end if;
483 -- The call is not in the main unit. See if it is in some subprogram
484 -- that can be inlined outside its unit. If so, inline the call and,
485 -- if the inlining level is set to 1, stop there; otherwise also
486 -- compile the package as above.
488 Scop := Current_Scope;
489 while Scope (Scop) /= Standard_Standard
490 and then not Is_Child_Unit (Scop)
491 loop
492 if Is_Overloadable (Scop)
493 and then Is_Inlined (Scop)
494 and then not Is_Nested (Scop)
495 then
496 Add_Call (E, Scop);
498 if Inline_Level = 1 then
499 return Inline_Call;
500 else
501 return Inline_Package;
502 end if;
503 end if;
505 Scop := Scope (Scop);
506 end loop;
508 return Dont_Inline;
509 end Must_Inline;
511 Inst : Entity_Id;
512 Inst_Decl : Node_Id;
513 Inst_Node : Node_Id;
514 Level : Inline_Level_Type;
516 -- Start of processing for Add_Inlined_Body
518 begin
519 Append_New_Elmt (N, To => Backend_Calls);
521 -- Skip subprograms that cannot or need not be inlined outside their
522 -- unit or parent subprogram.
524 if Is_Abstract_Subprogram (E)
525 or else Convention (E) = Convention_Protected
526 or else In_Main_Unit_Or_Subunit (E)
527 or else Is_Nested (E)
528 then
529 return;
530 end if;
532 -- Find out whether the call must be inlined. Unless the result is
533 -- Dont_Inline, Must_Inline also creates an edge for the call in the
534 -- callgraph; however, it will not be activated until after Is_Called
535 -- is set on the subprogram.
537 Level := Must_Inline;
539 if Level = Dont_Inline then
540 return;
541 end if;
543 -- If a previous call to the subprogram has been inlined, nothing to do
545 if Is_Called (E) then
546 return;
547 end if;
549 -- If the subprogram is an instance, then inline the instance
551 if Is_Generic_Instance (E) then
552 Add_Inlined_Instance (E);
553 end if;
555 -- Mark the subprogram as called
557 Set_Is_Called (E);
559 -- If the call was generated by the compiler and is to a subprogram in
560 -- a run-time unit, we need to suppress debugging information for it,
561 -- so that the code that is eventually inlined will not affect the
562 -- debugging of the program. We do not do it if the call comes from
563 -- source because, even if the call is inlined, the user may expect it
564 -- to be present in the debugging information.
566 if not Comes_From_Source (N)
567 and then In_Extended_Main_Source_Unit (N)
568 and then Is_Predefined_Unit (Get_Source_Unit (E))
569 then
570 Set_Needs_Debug_Info (E, False);
571 end if;
573 -- If the subprogram is an expression function, or is completed by one
574 -- where both the spec and body are in the same context, then there is
575 -- no need to load any package body since the body of the function is
576 -- in the spec.
578 if Is_Non_Loading_Expression_Function (E) then
579 return;
580 end if;
582 -- Find unit containing E, and add to list of inlined bodies if needed.
583 -- Library-level functions must be handled specially, because there is
584 -- no enclosing package to retrieve. In this case, it is the body of
585 -- the function that will have to be loaded.
587 declare
588 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
590 begin
591 if Pack = E then
592 Inlined_Bodies.Increment_Last;
593 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
595 else
596 pragma Assert (Ekind (Pack) = E_Package);
598 -- If the subprogram is within an instance, inline the instance
600 if Comes_From_Source (E) then
601 Inst := Scope (E);
603 while Present (Inst) and then Inst /= Standard_Standard loop
604 exit when Is_Generic_Instance (Inst);
605 Inst := Scope (Inst);
606 end loop;
608 if Present (Inst)
609 and then Is_Generic_Instance (Inst)
610 and then not Is_Called (Inst)
611 then
612 -- Do not add a pending instantiation if the body exits
613 -- already, or if the instance is a compilation unit, or
614 -- the instance node is missing.
616 Inst_Decl := Unit_Declaration_Node (Inst);
617 if Present (Corresponding_Body (Inst_Decl))
618 or else Nkind (Parent (Inst_Decl)) = N_Compilation_Unit
619 or else No (Next (Inst_Decl))
620 then
621 Set_Is_Called (Inst);
623 else
624 -- If the inlined call itself appears within an instance,
625 -- ensure that the enclosing instance body is available.
626 -- This is necessary because Sem_Ch12.Might_Inline_Subp
627 -- does not recurse into nested instantiations.
629 if not Is_Inlined (Inst) and then In_Instance then
630 Set_Is_Inlined (Inst);
632 -- The instantiation node usually follows the package
633 -- declaration for the instance. If the generic unit
634 -- has aspect specifications, they are transformed
635 -- into pragmas in the instance, and the instance node
636 -- appears after them.
638 Inst_Node := Next (Inst_Decl);
640 while Nkind (Inst_Node) /= N_Package_Instantiation loop
641 Inst_Node := Next (Inst_Node);
642 end loop;
644 Add_Pending_Instantiation (Inst_Node, Inst_Decl);
645 end if;
647 Add_Inlined_Instance (Inst);
648 end if;
649 end if;
650 end if;
652 -- If the unit containing E is an instance, then the instance body
653 -- will be analyzed in any case, see Sem_Ch12.Might_Inline_Subp.
655 if Is_Generic_Instance (Pack) then
656 null;
658 -- Do not inline the package if the subprogram is an init proc
659 -- or other internally generated subprogram, because in that
660 -- case the subprogram body appears in the same unit that
661 -- declares the type, and that body is visible to the back end.
662 -- Do not inline it either if it is in the main unit.
663 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
664 -- calls if the back-end takes care of inlining the call.
665 -- Note that Level is in Inline_Call | Inline_Packag here.
667 elsif ((Level = Inline_Call
668 and then Has_Pragma_Inline_Always (E)
669 and then Back_End_Inlining)
670 or else Level = Inline_Package)
671 and then not Is_Inlined (Pack)
672 and then not Is_Internal (E)
673 and then not In_Main_Unit_Or_Subunit (Pack)
674 then
675 Set_Is_Inlined (Pack);
676 Inlined_Bodies.Increment_Last;
677 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
678 end if;
679 end if;
681 -- Ensure that Analyze_Inlined_Bodies will be invoked after
682 -- completing the analysis of the current unit.
684 Inline_Processing_Required := True;
685 end;
686 end Add_Inlined_Body;
688 --------------------------
689 -- Add_Inlined_Instance --
690 --------------------------
692 procedure Add_Inlined_Instance (E : Entity_Id) is
693 Decl_Node : constant Node_Id := Unit_Declaration_Node (E);
694 Index : Int;
696 begin
697 -- This machinery is only used with back-end inlining
699 if not Back_End_Inlining then
700 return;
701 end if;
703 -- Register the instance in the list
705 Append_New_Elmt (Decl_Node, To => Backend_Instances);
707 -- Retrieve the index of its corresponding pending instantiation
708 -- and mark this corresponding pending instantiation as needed.
710 Index := To_Pending_Instantiations.Get (Decl_Node);
711 if Index >= 0 then
712 Called_Pending_Instantiations.Append (Index);
713 else
714 pragma Assert (False);
715 null;
716 end if;
718 Set_Is_Called (E);
719 end Add_Inlined_Instance;
721 ----------------------------
722 -- Add_Inlined_Subprogram --
723 ----------------------------
725 procedure Add_Inlined_Subprogram (E : Entity_Id) is
726 Decl : constant Node_Id := Parent (Declaration_Node (E));
727 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
729 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
730 -- Append Subp to the list of subprograms inlined by the backend
732 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
733 -- Append Subp to the list of subprograms that cannot be inlined by
734 -- the backend.
736 -----------------------------------------
737 -- Register_Backend_Inlined_Subprogram --
738 -----------------------------------------
740 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
741 begin
742 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
743 end Register_Backend_Inlined_Subprogram;
745 ---------------------------------------------
746 -- Register_Backend_Not_Inlined_Subprogram --
747 ---------------------------------------------
749 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
750 begin
751 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
752 end Register_Backend_Not_Inlined_Subprogram;
754 -- Start of processing for Add_Inlined_Subprogram
756 begin
757 -- We can inline the subprogram if its unit is known to be inlined or is
758 -- an instance whose body will be analyzed anyway or the subprogram was
759 -- generated as a body by the compiler (for example an initialization
760 -- procedure) or its declaration was provided along with the body (for
761 -- example an expression function) and it does not declare types with
762 -- nontrivial initialization procedures.
764 if (Is_Inlined (Pack)
765 or else Is_Generic_Instance (Pack)
766 or else Nkind (Decl) = N_Subprogram_Body
767 or else Present (Corresponding_Body (Decl)))
768 and then not Has_Initialized_Type (E)
769 then
770 Register_Backend_Inlined_Subprogram (E);
772 if No (Last_Inlined) then
773 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
774 else
775 Set_Next_Inlined_Subprogram (Last_Inlined, E);
776 end if;
778 Last_Inlined := E;
780 else
781 Register_Backend_Not_Inlined_Subprogram (E);
782 end if;
783 end Add_Inlined_Subprogram;
785 --------------------------------
786 -- Add_Pending_Instantiation --
787 --------------------------------
789 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
790 Act_Decl_Id : Entity_Id;
791 Index : Int;
793 begin
794 -- Here is a defense against a ludicrous number of instantiations
795 -- caused by a circular set of instantiation attempts.
797 if Pending_Instantiations.Last + 1 >= Maximum_Instantiations then
798 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
799 Error_Msg_N ("too many instantiations, exceeds max of^", Inst);
800 Error_Msg_N ("\limit can be changed using -gnateinn switch", Inst);
801 raise Unrecoverable_Error;
802 end if;
804 -- Capture the body of the generic instantiation along with its context
805 -- for later processing by Instantiate_Bodies.
807 Pending_Instantiations.Append
808 ((Act_Decl => Act_Decl,
809 Config_Switches => Save_Config_Switches,
810 Current_Sem_Unit => Current_Sem_Unit,
811 Expander_Status => Expander_Active,
812 Inst_Node => Inst,
813 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
814 Scope_Suppress => Scope_Suppress,
815 Warnings => Save_Warnings));
817 -- With back-end inlining, also associate the index to the instantiation
819 if Back_End_Inlining then
820 Act_Decl_Id := Defining_Entity (Act_Decl);
821 Index := Pending_Instantiations.Last;
823 To_Pending_Instantiations.Set (Act_Decl, Index);
825 -- If an instantiation is either a compilation unit or is in the main
826 -- unit or subunit or is a nested subprogram, then its body is needed
827 -- as per the analysis already done in Analyze_Package_Instantiation
828 -- and Analyze_Subprogram_Instantiation.
830 if Nkind (Parent (Inst)) = N_Compilation_Unit
831 or else In_Main_Unit_Or_Subunit (Act_Decl_Id)
832 or else (Is_Subprogram (Act_Decl_Id)
833 and then Is_Nested (Act_Decl_Id))
834 then
835 Called_Pending_Instantiations.Append (Index);
837 Set_Is_Called (Act_Decl_Id);
838 end if;
839 end if;
840 end Add_Pending_Instantiation;
842 ------------------------
843 -- Add_Scope_To_Clean --
844 ------------------------
846 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
847 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
848 Elmt : Elmt_Id;
850 begin
851 -- If the instance appears in a library-level package declaration,
852 -- all finalization is global, and nothing needs doing here.
854 if Scop = Standard_Standard then
855 return;
856 end if;
858 -- If the instance is within a generic unit, no finalization code
859 -- can be generated. Note that at this point all bodies have been
860 -- analyzed, and the scope stack itself is not present, and the flag
861 -- Inside_A_Generic is not set.
863 declare
864 S : Entity_Id;
866 begin
867 S := Scope (Inst);
868 while Present (S) and then S /= Standard_Standard loop
869 if Is_Generic_Unit (S) then
870 return;
871 end if;
873 S := Scope (S);
874 end loop;
875 end;
877 Elmt := First_Elmt (To_Clean);
878 while Present (Elmt) loop
879 if Node (Elmt) = Scop then
880 return;
881 end if;
883 Elmt := Next_Elmt (Elmt);
884 end loop;
886 Append_Elmt (Scop, To_Clean);
887 end Add_Scope_To_Clean;
889 --------------
890 -- Add_Subp --
891 --------------
893 function Add_Subp (E : Entity_Id) return Subp_Index is
894 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
895 J : Subp_Index;
897 procedure New_Entry;
898 -- Initialize entry in Inlined table
900 procedure New_Entry is
901 begin
902 Inlined.Increment_Last;
903 Inlined.Table (Inlined.Last).Name := E;
904 Inlined.Table (Inlined.Last).Next := No_Subp;
905 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
906 Inlined.Table (Inlined.Last).Main_Call := False;
907 Inlined.Table (Inlined.Last).Processed := False;
908 end New_Entry;
910 -- Start of processing for Add_Subp
912 begin
913 if Hash_Headers (Index) = No_Subp then
914 New_Entry;
915 Hash_Headers (Index) := Inlined.Last;
916 return Inlined.Last;
918 else
919 J := Hash_Headers (Index);
920 while J /= No_Subp loop
921 if Inlined.Table (J).Name = E then
922 return J;
923 else
924 Index := J;
925 J := Inlined.Table (J).Next;
926 end if;
927 end loop;
929 -- On exit, subprogram was not found. Enter in table. Index is
930 -- the current last entry on the hash chain.
932 New_Entry;
933 Inlined.Table (Index).Next := Inlined.Last;
934 return Inlined.Last;
935 end if;
936 end Add_Subp;
938 ----------------------------
939 -- Analyze_Inlined_Bodies --
940 ----------------------------
942 procedure Analyze_Inlined_Bodies is
943 Comp_Unit : Node_Id;
944 J : Int;
945 Pack : Entity_Id;
946 Subp : Subp_Index;
947 S : Succ_Index;
949 type Pending_Index is new Nat;
951 package Pending_Inlined is new Table.Table (
952 Table_Component_Type => Subp_Index,
953 Table_Index_Type => Pending_Index,
954 Table_Low_Bound => 1,
955 Table_Initial => Alloc.Inlined_Initial,
956 Table_Increment => Alloc.Inlined_Increment,
957 Table_Name => "Pending_Inlined");
958 -- The workpile used to compute the transitive closure
960 -- Start of processing for Analyze_Inlined_Bodies
962 begin
963 if Serious_Errors_Detected = 0 then
964 Push_Scope (Standard_Standard);
966 J := 0;
967 while J <= Inlined_Bodies.Last
968 and then Serious_Errors_Detected = 0
969 loop
970 Pack := Inlined_Bodies.Table (J);
971 while Present (Pack)
972 and then Scope (Pack) /= Standard_Standard
973 and then not Is_Child_Unit (Pack)
974 loop
975 Pack := Scope (Pack);
976 end loop;
978 Comp_Unit := Parent (Pack);
979 while Present (Comp_Unit)
980 and then Nkind (Comp_Unit) /= N_Compilation_Unit
981 loop
982 Comp_Unit := Parent (Comp_Unit);
983 end loop;
985 -- Load the body if it exists and contains inlineable entities,
986 -- unless it is the main unit, or is an instance whose body has
987 -- already been analyzed.
989 if Present (Comp_Unit)
990 and then Comp_Unit /= Cunit (Main_Unit)
991 and then Body_Required (Comp_Unit)
992 and then
993 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
994 or else
995 (No (Corresponding_Body (Unit (Comp_Unit)))
996 and then Body_Needed_For_Inlining
997 (Defining_Entity (Unit (Comp_Unit)))))
998 then
999 declare
1000 Bname : constant Unit_Name_Type :=
1001 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
1003 OK : Boolean;
1005 begin
1006 if not Is_Loaded (Bname) then
1007 Style_Check := False;
1008 Load_Needed_Body (Comp_Unit, OK);
1010 if not OK then
1012 -- Warn that a body was not available for inlining
1013 -- by the back-end.
1015 Error_Msg_Unit_1 := Bname;
1016 Error_Msg_N
1017 ("one or more inlined subprograms accessed in $!??",
1018 Comp_Unit);
1019 Error_Msg_File_1 :=
1020 Get_File_Name (Bname, Subunit => False);
1021 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
1022 end if;
1023 end if;
1024 end;
1025 end if;
1027 J := J + 1;
1029 if J > Inlined_Bodies.Last then
1031 -- The analysis of required bodies may have produced additional
1032 -- generic instantiations. To obtain further inlining, we need
1033 -- to perform another round of generic body instantiations.
1035 Instantiate_Bodies;
1037 -- Symmetrically, the instantiation of required generic bodies
1038 -- may have caused additional bodies to be inlined. To obtain
1039 -- further inlining, we keep looping over the inlined bodies.
1040 end if;
1041 end loop;
1043 -- The list of inlined subprograms is an overestimate, because it
1044 -- includes inlined functions called from functions that are compiled
1045 -- as part of an inlined package, but are not themselves called. An
1046 -- accurate computation of just those subprograms that are needed
1047 -- requires that we perform a transitive closure over the call graph,
1048 -- starting from calls in the main compilation unit.
1050 for Index in Inlined.First .. Inlined.Last loop
1051 if not Is_Called (Inlined.Table (Index).Name) then
1053 -- This means that Add_Inlined_Body added the subprogram to the
1054 -- table but wasn't able to handle its code unit. Do nothing.
1056 Inlined.Table (Index).Processed := True;
1058 elsif Inlined.Table (Index).Main_Call then
1059 Pending_Inlined.Increment_Last;
1060 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
1061 Inlined.Table (Index).Processed := True;
1063 else
1064 Set_Is_Called (Inlined.Table (Index).Name, False);
1065 end if;
1066 end loop;
1068 -- Iterate over the workpile until it is emptied, propagating the
1069 -- Is_Called flag to the successors of the processed subprogram.
1071 while Pending_Inlined.Last >= Pending_Inlined.First loop
1072 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
1073 Pending_Inlined.Decrement_Last;
1075 S := Inlined.Table (Subp).First_Succ;
1077 while S /= No_Succ loop
1078 Subp := Successors.Table (S).Subp;
1080 if not Inlined.Table (Subp).Processed then
1081 Set_Is_Called (Inlined.Table (Subp).Name);
1082 Pending_Inlined.Increment_Last;
1083 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
1084 Inlined.Table (Subp).Processed := True;
1085 end if;
1087 S := Successors.Table (S).Next;
1088 end loop;
1089 end loop;
1091 -- Finally add the called subprograms to the list of inlined
1092 -- subprograms for the unit.
1094 for Index in Inlined.First .. Inlined.Last loop
1095 if Is_Called (Inlined.Table (Index).Name) then
1096 Add_Inlined_Subprogram (Inlined.Table (Index).Name);
1097 end if;
1098 end loop;
1100 Pop_Scope;
1101 end if;
1102 end Analyze_Inlined_Bodies;
1104 --------------------------
1105 -- Build_Body_To_Inline --
1106 --------------------------
1108 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1109 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1110 Analysis_Status : constant Boolean := Full_Analysis;
1111 Original_Body : Node_Id;
1112 Body_To_Analyze : Node_Id;
1113 Max_Size : constant := 10;
1115 function Has_Extended_Return return Boolean;
1116 -- This function returns True if the subprogram has an extended return
1117 -- statement.
1119 function Has_Pending_Instantiation return Boolean;
1120 -- If some enclosing body contains instantiations that appear before
1121 -- the corresponding generic body, the enclosing body has a freeze node
1122 -- so that it can be elaborated after the generic itself. This might
1123 -- conflict with subsequent inlinings, so that it is unsafe to try to
1124 -- inline in such a case.
1126 function Has_Single_Return_In_GNATprove_Mode return Boolean;
1127 -- This function is called only in GNATprove mode, and it returns
1128 -- True if the subprogram has no return statement or a single return
1129 -- statement as last statement. It returns False for subprogram with
1130 -- a single return as last statement inside one or more blocks, as
1131 -- inlining would generate gotos in that case as well (although the
1132 -- goto is useless in that case).
1134 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1135 -- If the body of the subprogram includes a call that returns an
1136 -- unconstrained type, the secondary stack is involved, and it is
1137 -- not worth inlining.
1139 -------------------------
1140 -- Has_Extended_Return --
1141 -------------------------
1143 function Has_Extended_Return return Boolean is
1144 Body_To_Inline : constant Node_Id := N;
1146 function Check_Return (N : Node_Id) return Traverse_Result;
1147 -- Returns OK on node N if this is not an extended return statement
1149 ------------------
1150 -- Check_Return --
1151 ------------------
1153 function Check_Return (N : Node_Id) return Traverse_Result is
1154 begin
1155 case Nkind (N) is
1156 when N_Extended_Return_Statement =>
1157 return Abandon;
1159 -- Skip locally declared subprogram bodies inside the body to
1160 -- inline, as the return statements inside those do not count.
1162 when N_Subprogram_Body =>
1163 if N = Body_To_Inline then
1164 return OK;
1165 else
1166 return Skip;
1167 end if;
1169 when others =>
1170 return OK;
1171 end case;
1172 end Check_Return;
1174 function Check_All_Returns is new Traverse_Func (Check_Return);
1176 -- Start of processing for Has_Extended_Return
1178 begin
1179 return Check_All_Returns (N) /= OK;
1180 end Has_Extended_Return;
1182 -------------------------------
1183 -- Has_Pending_Instantiation --
1184 -------------------------------
1186 function Has_Pending_Instantiation return Boolean is
1187 S : Entity_Id;
1189 begin
1190 S := Current_Scope;
1191 while Present (S) loop
1192 if Is_Compilation_Unit (S)
1193 or else Is_Child_Unit (S)
1194 then
1195 return False;
1197 elsif Ekind (S) = E_Package
1198 and then Has_Forward_Instantiation (S)
1199 then
1200 return True;
1201 end if;
1203 S := Scope (S);
1204 end loop;
1206 return False;
1207 end Has_Pending_Instantiation;
1209 -----------------------------------------
1210 -- Has_Single_Return_In_GNATprove_Mode --
1211 -----------------------------------------
1213 function Has_Single_Return_In_GNATprove_Mode return Boolean is
1214 Body_To_Inline : constant Node_Id := N;
1215 Last_Statement : Node_Id := Empty;
1217 function Check_Return (N : Node_Id) return Traverse_Result;
1218 -- Returns OK on node N if this is not a return statement different
1219 -- from the last statement in the subprogram.
1221 ------------------
1222 -- Check_Return --
1223 ------------------
1225 function Check_Return (N : Node_Id) return Traverse_Result is
1226 begin
1227 case Nkind (N) is
1228 when N_Extended_Return_Statement
1229 | N_Simple_Return_Statement
1231 if N = Last_Statement then
1232 return OK;
1233 else
1234 return Abandon;
1235 end if;
1237 -- Skip locally declared subprogram bodies inside the body to
1238 -- inline, as the return statements inside those do not count.
1240 when N_Subprogram_Body =>
1241 if N = Body_To_Inline then
1242 return OK;
1243 else
1244 return Skip;
1245 end if;
1247 when others =>
1248 return OK;
1249 end case;
1250 end Check_Return;
1252 function Check_All_Returns is new Traverse_Func (Check_Return);
1254 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1256 begin
1257 -- Retrieve the last statement
1259 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
1261 -- Check that the last statement is the only possible return
1262 -- statement in the subprogram.
1264 return Check_All_Returns (N) = OK;
1265 end Has_Single_Return_In_GNATprove_Mode;
1267 --------------------------
1268 -- Uses_Secondary_Stack --
1269 --------------------------
1271 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1272 function Check_Call (N : Node_Id) return Traverse_Result;
1273 -- Look for function calls that return an unconstrained type
1275 ----------------
1276 -- Check_Call --
1277 ----------------
1279 function Check_Call (N : Node_Id) return Traverse_Result is
1280 begin
1281 if Nkind (N) = N_Function_Call
1282 and then Is_Entity_Name (Name (N))
1283 and then Is_Composite_Type (Etype (Entity (Name (N))))
1284 and then not Is_Constrained (Etype (Entity (Name (N))))
1285 then
1286 Cannot_Inline
1287 ("cannot inline & (call returns unconstrained type)?",
1288 N, Spec_Id);
1289 return Abandon;
1290 else
1291 return OK;
1292 end if;
1293 end Check_Call;
1295 function Check_Calls is new Traverse_Func (Check_Call);
1297 begin
1298 return Check_Calls (Bod) = Abandon;
1299 end Uses_Secondary_Stack;
1301 -- Start of processing for Build_Body_To_Inline
1303 begin
1304 -- Return immediately if done already
1306 if Nkind (Decl) = N_Subprogram_Declaration
1307 and then Present (Body_To_Inline (Decl))
1308 then
1309 return;
1311 -- Subprograms that have return statements in the middle of the body are
1312 -- inlined with gotos. GNATprove does not currently support gotos, so
1313 -- we prevent such inlining.
1315 elsif GNATprove_Mode
1316 and then not Has_Single_Return_In_GNATprove_Mode
1317 then
1318 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1319 return;
1321 -- Functions that return controlled types cannot currently be inlined
1322 -- because they require secondary stack handling; controlled actions
1323 -- may also interfere in complex ways with inlining.
1325 elsif Ekind (Spec_Id) = E_Function
1326 and then Needs_Finalization (Etype (Spec_Id))
1327 then
1328 Cannot_Inline
1329 ("cannot inline & (controlled return type)?", N, Spec_Id);
1330 return;
1331 end if;
1333 if Present (Declarations (N))
1334 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
1335 then
1336 return;
1337 end if;
1339 if Present (Handled_Statement_Sequence (N)) then
1340 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1341 Cannot_Inline
1342 ("cannot inline& (exception handler)?",
1343 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1344 Spec_Id);
1345 return;
1347 elsif Has_Excluded_Statement
1348 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1349 then
1350 return;
1351 end if;
1352 end if;
1354 -- We do not inline a subprogram that is too large, unless it is marked
1355 -- Inline_Always or we are in GNATprove mode. This pragma does not
1356 -- suppress the other checks on inlining (forbidden declarations,
1357 -- handlers, etc).
1359 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1360 and then List_Length
1361 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1362 then
1363 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1364 return;
1365 end if;
1367 if Has_Pending_Instantiation then
1368 Cannot_Inline
1369 ("cannot inline& (forward instance within enclosing body)?",
1370 N, Spec_Id);
1371 return;
1372 end if;
1374 -- Within an instance, the body to inline must be treated as a nested
1375 -- generic, so that the proper global references are preserved.
1377 -- Note that we do not do this at the library level, because it is not
1378 -- needed, and furthermore this causes trouble if front-end inlining
1379 -- is activated (-gnatN).
1381 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1382 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1383 Original_Body := Copy_Generic_Node (N, Empty, Instantiating => True);
1384 else
1385 Original_Body := Copy_Separate_Tree (N);
1386 end if;
1388 -- We need to capture references to the formals in order to substitute
1389 -- the actuals at the point of inlining, i.e. instantiation. To treat
1390 -- the formals as globals to the body to inline, we nest it within a
1391 -- dummy parameterless subprogram, declared within the real one. To
1392 -- avoid generating an internal name (which is never public, and which
1393 -- affects serial numbers of other generated names), we use an internal
1394 -- symbol that cannot conflict with user declarations.
1396 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1397 Set_Defining_Unit_Name
1398 (Specification (Original_Body),
1399 Make_Defining_Identifier (Sloc (N), Name_uParent));
1400 Set_Corresponding_Spec (Original_Body, Empty);
1402 -- Remove all aspects/pragmas that have no meaning in an inlined body
1404 Remove_Aspects_And_Pragmas (Original_Body);
1406 Body_To_Analyze :=
1407 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1409 -- Set return type of function, which is also global and does not need
1410 -- to be resolved.
1412 if Ekind (Spec_Id) = E_Function then
1413 Set_Result_Definition
1414 (Specification (Body_To_Analyze),
1415 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1416 end if;
1418 if No (Declarations (N)) then
1419 Set_Declarations (N, New_List (Body_To_Analyze));
1420 else
1421 Append (Body_To_Analyze, Declarations (N));
1422 end if;
1424 -- The body to inline is preanalyzed. In GNATprove mode we must disable
1425 -- full analysis as well so that light expansion does not take place
1426 -- either, and name resolution is unaffected.
1428 Expander_Mode_Save_And_Set (False);
1429 Full_Analysis := False;
1431 Analyze (Body_To_Analyze);
1432 Push_Scope (Defining_Entity (Body_To_Analyze));
1433 Save_Global_References (Original_Body);
1434 End_Scope;
1435 Remove (Body_To_Analyze);
1437 Expander_Mode_Restore;
1438 Full_Analysis := Analysis_Status;
1440 -- Restore environment if previously saved
1442 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1443 Restore_Env;
1444 end if;
1446 -- Functions that return unconstrained composite types require
1447 -- secondary stack handling, and cannot currently be inlined, unless
1448 -- all return statements return a local variable that is the first
1449 -- local declaration in the body. We had to delay this check until
1450 -- the body of the function is analyzed since Has_Single_Return()
1451 -- requires a minimum decoration.
1453 if Ekind (Spec_Id) = E_Function
1454 and then not Is_Scalar_Type (Etype (Spec_Id))
1455 and then not Is_Access_Type (Etype (Spec_Id))
1456 and then not Is_Constrained (Etype (Spec_Id))
1457 then
1458 if not Has_Single_Return (Body_To_Analyze)
1460 -- Skip inlining if the function returns an unconstrained type
1461 -- using an extended return statement, since this part of the
1462 -- new inlining model is not yet supported by the current
1463 -- implementation. ???
1465 or else (Returns_Unconstrained_Type (Spec_Id)
1466 and then Has_Extended_Return)
1467 then
1468 Cannot_Inline
1469 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1470 return;
1471 end if;
1473 -- If secondary stack is used, there is no point in inlining. We have
1474 -- already issued the warning in this case, so nothing to do.
1476 elsif Uses_Secondary_Stack (Body_To_Analyze) then
1477 return;
1478 end if;
1480 Set_Body_To_Inline (Decl, Original_Body);
1481 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1482 Set_Is_Inlined (Spec_Id);
1483 end Build_Body_To_Inline;
1485 -------------------------------------------
1486 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1487 -------------------------------------------
1489 function Call_Can_Be_Inlined_In_GNATprove_Mode
1490 (N : Node_Id;
1491 Subp : Entity_Id) return Boolean
1493 F : Entity_Id;
1494 A : Node_Id;
1496 begin
1497 F := First_Formal (Subp);
1498 A := First_Actual (N);
1499 while Present (F) loop
1500 if Ekind (F) /= E_Out_Parameter
1501 and then not Same_Type (Etype (F), Etype (A))
1502 and then
1503 (Is_By_Reference_Type (Etype (A))
1504 or else Is_Limited_Type (Etype (A)))
1505 then
1506 return False;
1507 end if;
1509 Next_Formal (F);
1510 Next_Actual (A);
1511 end loop;
1513 return True;
1514 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1516 --------------------------------------
1517 -- Can_Be_Inlined_In_GNATprove_Mode --
1518 --------------------------------------
1520 function Can_Be_Inlined_In_GNATprove_Mode
1521 (Spec_Id : Entity_Id;
1522 Body_Id : Entity_Id) return Boolean
1524 function Has_Formal_With_Discriminant_Dependent_Fields
1525 (Id : Entity_Id) return Boolean;
1526 -- Returns true if the subprogram has at least one formal parameter of
1527 -- an unconstrained record type with per-object constraints on component
1528 -- types.
1530 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1531 -- Return True if subprogram Id has any contract. The presence of
1532 -- Extensions_Visible or Volatile_Function is also considered as a
1533 -- contract here.
1535 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1536 -- Return True if subprogram Id defines a compilation unit
1537 -- Shouldn't this be in Sem_Aux???
1539 function In_Package_Spec (Id : Entity_Id) return Boolean;
1540 -- Return True if subprogram Id is defined in the package specification,
1541 -- either its visible or private part.
1543 ---------------------------------------------------
1544 -- Has_Formal_With_Discriminant_Dependent_Fields --
1545 ---------------------------------------------------
1547 function Has_Formal_With_Discriminant_Dependent_Fields
1548 (Id : Entity_Id) return Boolean
1550 function Has_Discriminant_Dependent_Component
1551 (Typ : Entity_Id) return Boolean;
1552 -- Determine whether unconstrained record type Typ has at least one
1553 -- component that depends on a discriminant.
1555 ------------------------------------------
1556 -- Has_Discriminant_Dependent_Component --
1557 ------------------------------------------
1559 function Has_Discriminant_Dependent_Component
1560 (Typ : Entity_Id) return Boolean
1562 Comp : Entity_Id;
1564 begin
1565 -- Inspect all components of the record type looking for one that
1566 -- depends on a discriminant.
1568 Comp := First_Component (Typ);
1569 while Present (Comp) loop
1570 if Has_Discriminant_Dependent_Constraint (Comp) then
1571 return True;
1572 end if;
1574 Next_Component (Comp);
1575 end loop;
1577 return False;
1578 end Has_Discriminant_Dependent_Component;
1580 -- Local variables
1582 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1583 Formal : Entity_Id;
1584 Formal_Typ : Entity_Id;
1586 -- Start of processing for
1587 -- Has_Formal_With_Discriminant_Dependent_Fields
1589 begin
1590 -- Inspect all parameters of the subprogram looking for a formal
1591 -- of an unconstrained record type with at least one discriminant
1592 -- dependent component.
1594 Formal := First_Formal (Subp_Id);
1595 while Present (Formal) loop
1596 Formal_Typ := Etype (Formal);
1598 if Is_Record_Type (Formal_Typ)
1599 and then not Is_Constrained (Formal_Typ)
1600 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1601 then
1602 return True;
1603 end if;
1605 Next_Formal (Formal);
1606 end loop;
1608 return False;
1609 end Has_Formal_With_Discriminant_Dependent_Fields;
1611 -----------------------
1612 -- Has_Some_Contract --
1613 -----------------------
1615 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1616 Items : Node_Id;
1618 begin
1619 -- A call to an expression function may precede the actual body which
1620 -- is inserted at the end of the enclosing declarations. Ensure that
1621 -- the related entity is decorated before inspecting the contract.
1623 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1624 Items := Contract (Id);
1626 -- Note that Classifications is not Empty when Extensions_Visible
1627 -- or Volatile_Function is present, which causes such subprograms
1628 -- to be considered to have a contract here. This is fine as we
1629 -- want to avoid inlining these too.
1631 return Present (Items)
1632 and then (Present (Pre_Post_Conditions (Items)) or else
1633 Present (Contract_Test_Cases (Items)) or else
1634 Present (Classifications (Items)));
1635 end if;
1637 return False;
1638 end Has_Some_Contract;
1640 ---------------------
1641 -- In_Package_Spec --
1642 ---------------------
1644 function In_Package_Spec (Id : Entity_Id) return Boolean is
1645 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1646 -- Parent of the subprogram's declaration
1648 begin
1649 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1650 end In_Package_Spec;
1652 ------------------------
1653 -- Is_Unit_Subprogram --
1654 ------------------------
1656 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1657 Decl : Node_Id := Parent (Parent (Id));
1658 begin
1659 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1660 Decl := Parent (Decl);
1661 end if;
1663 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1664 end Is_Unit_Subprogram;
1666 -- Local declarations
1668 Id : Entity_Id;
1669 -- Procedure or function entity for the subprogram
1671 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1673 begin
1674 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1676 if Present (Spec_Id) then
1677 Id := Spec_Id;
1678 else
1679 Id := Body_Id;
1680 end if;
1682 -- Only local subprograms without contracts are inlined in GNATprove
1683 -- mode, as these are the subprograms which a user is not interested in
1684 -- analyzing in isolation, but rather in the context of their call. This
1685 -- is a convenient convention, that could be changed for an explicit
1686 -- pragma/aspect one day.
1688 -- In a number of special cases, inlining is not desirable or not
1689 -- possible, see below.
1691 -- Do not inline unit-level subprograms
1693 if Is_Unit_Subprogram (Id) then
1694 return False;
1696 -- Do not inline subprograms declared in package specs, because they are
1697 -- not local, i.e. can be called either from anywhere (if declared in
1698 -- visible part) or from the child units (if declared in private part).
1700 elsif In_Package_Spec (Id) then
1701 return False;
1703 -- Do not inline subprograms declared in other units. This is important
1704 -- in particular for subprograms defined in the private part of a
1705 -- package spec, when analyzing one of its child packages, as otherwise
1706 -- we issue spurious messages about the impossibility to inline such
1707 -- calls.
1709 elsif not In_Extended_Main_Code_Unit (Id) then
1710 return False;
1712 -- Do not inline subprograms marked No_Return, possibly used for
1713 -- signaling errors, which GNATprove handles specially.
1715 elsif No_Return (Id) then
1716 return False;
1718 -- Do not inline subprograms that have a contract on the spec or the
1719 -- body. Use the contract(s) instead in GNATprove. This also prevents
1720 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1722 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1723 or else
1724 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1725 then
1726 return False;
1728 -- Do not inline expression functions, which are directly inlined at the
1729 -- prover level.
1731 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1732 or else
1733 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1734 then
1735 return False;
1737 -- Do not inline generic subprogram instances. The visibility rules of
1738 -- generic instances plays badly with inlining.
1740 elsif Is_Generic_Instance (Spec_Id) then
1741 return False;
1743 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1744 -- the subprogram body, a similar check is performed after the body
1745 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1747 elsif Present (Spec_Id)
1748 and then
1749 (No (SPARK_Pragma (Spec_Id))
1750 or else
1751 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1752 then
1753 return False;
1755 -- Subprograms in generic instances are currently not inlined, to avoid
1756 -- problems with inlining of standard library subprograms.
1758 elsif Instantiation_Location (Sloc (Id)) /= No_Location then
1759 return False;
1761 -- Do not inline subprograms and entries defined inside protected types,
1762 -- which typically are not helper subprograms, which also avoids getting
1763 -- spurious messages on calls that cannot be inlined.
1765 elsif Within_Protected_Type (Id) then
1766 return False;
1768 -- Do not inline predicate functions (treated specially by GNATprove)
1770 elsif Is_Predicate_Function (Id) then
1771 return False;
1773 -- Do not inline subprograms with a parameter of an unconstrained
1774 -- record type if it has discrimiant dependent fields. Indeed, with
1775 -- such parameters, the frontend cannot always ensure type compliance
1776 -- in record component accesses (in particular with records containing
1777 -- packed arrays).
1779 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1780 return False;
1782 -- Otherwise, this is a subprogram declared inside the private part of a
1783 -- package, or inside a package body, or locally in a subprogram, and it
1784 -- does not have any contract. Inline it.
1786 else
1787 return True;
1788 end if;
1789 end Can_Be_Inlined_In_GNATprove_Mode;
1791 -------------------
1792 -- Cannot_Inline --
1793 -------------------
1795 procedure Cannot_Inline
1796 (Msg : String;
1797 N : Node_Id;
1798 Subp : Entity_Id;
1799 Is_Serious : Boolean := False)
1801 begin
1802 -- In GNATprove mode, inlining is the technical means by which the
1803 -- higher-level goal of contextual analysis is reached, so issue
1804 -- messages about failure to apply contextual analysis to a
1805 -- subprogram, rather than failure to inline it.
1807 if GNATprove_Mode
1808 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1809 then
1810 declare
1811 Len1 : constant Positive :=
1812 String (String'("cannot inline"))'Length;
1813 Len2 : constant Positive :=
1814 String (String'("info: no contextual analysis of"))'Length;
1816 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1818 begin
1819 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1820 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1821 Msg (Msg'First + Len1 .. Msg'Last);
1822 Cannot_Inline (New_Msg, N, Subp, Is_Serious);
1823 return;
1824 end;
1825 end if;
1827 pragma Assert (Msg (Msg'Last) = '?');
1829 -- Legacy front-end inlining model
1831 if not Back_End_Inlining then
1833 -- Do not emit warning if this is a predefined unit which is not
1834 -- the main unit. With validity checks enabled, some predefined
1835 -- subprograms may contain nested subprograms and become ineligible
1836 -- for inlining.
1838 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1839 and then not In_Extended_Main_Source_Unit (Subp)
1840 then
1841 null;
1843 -- In GNATprove mode, issue a warning when -gnatd_f is set, and
1844 -- indicate that the subprogram is not always inlined by setting
1845 -- flag Is_Inlined_Always to False.
1847 elsif GNATprove_Mode then
1848 Set_Is_Inlined_Always (Subp, False);
1850 if Debug_Flag_Underscore_F then
1851 Error_Msg_NE (Msg, N, Subp);
1852 end if;
1854 elsif Has_Pragma_Inline_Always (Subp) then
1856 -- Remove last character (question mark) to make this into an
1857 -- error, because the Inline_Always pragma cannot be obeyed.
1859 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1861 elsif Ineffective_Inline_Warnings then
1862 Error_Msg_NE (Msg & "p?", N, Subp);
1863 end if;
1865 -- New semantics relying on back-end inlining
1867 elsif Is_Serious then
1869 -- Remove last character (question mark) to make this into an error.
1871 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1873 -- In GNATprove mode, issue a warning when -gnatd_f is set, and
1874 -- indicate that the subprogram is not always inlined by setting
1875 -- flag Is_Inlined_Always to False.
1877 elsif GNATprove_Mode then
1878 Set_Is_Inlined_Always (Subp, False);
1880 if Debug_Flag_Underscore_F then
1881 Error_Msg_NE (Msg, N, Subp);
1882 end if;
1884 else
1886 -- Do not emit warning if this is a predefined unit which is not
1887 -- the main unit. This behavior is currently provided for backward
1888 -- compatibility but it will be removed when we enforce the
1889 -- strictness of the new rules.
1891 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1892 and then not In_Extended_Main_Source_Unit (Subp)
1893 then
1894 null;
1896 elsif Has_Pragma_Inline_Always (Subp) then
1898 -- Emit a warning if this is a call to a runtime subprogram
1899 -- which is located inside a generic. Previously this call
1900 -- was silently skipped.
1902 if Is_Generic_Instance (Subp) then
1903 declare
1904 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
1905 begin
1906 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
1907 Set_Is_Inlined (Subp, False);
1908 Error_Msg_NE (Msg & "p?", N, Subp);
1909 return;
1910 end if;
1911 end;
1912 end if;
1914 -- Remove last character (question mark) to make this into an
1915 -- error, because the Inline_Always pragma cannot be obeyed.
1917 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1919 else
1920 Set_Is_Inlined (Subp, False);
1922 if Ineffective_Inline_Warnings then
1923 Error_Msg_NE (Msg & "p?", N, Subp);
1924 end if;
1925 end if;
1926 end if;
1927 end Cannot_Inline;
1929 --------------------------------------------
1930 -- Check_And_Split_Unconstrained_Function --
1931 --------------------------------------------
1933 procedure Check_And_Split_Unconstrained_Function
1934 (N : Node_Id;
1935 Spec_Id : Entity_Id;
1936 Body_Id : Entity_Id)
1938 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
1939 -- Use generic machinery to build an unexpanded body for the subprogram.
1940 -- This body is subsequently used for inline expansions at call sites.
1942 procedure Build_Return_Object_Formal
1943 (Loc : Source_Ptr;
1944 Obj_Decl : Node_Id;
1945 Formals : List_Id);
1946 -- Create a formal parameter for return object declaration Obj_Decl of
1947 -- an extended return statement and add it to list Formals.
1949 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
1950 -- Return true if we generate code for the function body N, the function
1951 -- body N has no local declarations and its unique statement is a single
1952 -- extended return statement with a handled statements sequence.
1954 procedure Copy_Formals
1955 (Loc : Source_Ptr;
1956 Subp_Id : Entity_Id;
1957 Formals : List_Id);
1958 -- Create new formal parameters from the formal parameters of subprogram
1959 -- Subp_Id and add them to list Formals.
1961 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id;
1962 -- Create a copy of return object declaration Obj_Decl of an extended
1963 -- return statement.
1965 procedure Split_Unconstrained_Function
1966 (N : Node_Id;
1967 Spec_Id : Entity_Id);
1968 -- N is an inlined function body that returns an unconstrained type and
1969 -- has a single extended return statement. Split N in two subprograms:
1970 -- a procedure P' and a function F'. The formals of P' duplicate the
1971 -- formals of N plus an extra formal which is used to return a value;
1972 -- its body is composed by the declarations and list of statements
1973 -- of the extended return statement of N.
1975 --------------------------
1976 -- Build_Body_To_Inline --
1977 --------------------------
1979 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1980 procedure Generate_Subprogram_Body
1981 (N : Node_Id;
1982 Body_To_Inline : out Node_Id);
1983 -- Generate a parameterless duplicate of subprogram body N. Note that
1984 -- occurrences of pragmas referencing the formals are removed since
1985 -- they have no meaning when the body is inlined and the formals are
1986 -- rewritten (the analysis of the non-inlined body will handle these
1987 -- pragmas). A new internal name is associated with Body_To_Inline.
1989 ------------------------------
1990 -- Generate_Subprogram_Body --
1991 ------------------------------
1993 procedure Generate_Subprogram_Body
1994 (N : Node_Id;
1995 Body_To_Inline : out Node_Id)
1997 begin
1998 -- Within an instance, the body to inline must be treated as a
1999 -- nested generic so that proper global references are preserved.
2001 -- Note that we do not do this at the library level, because it
2002 -- is not needed, and furthermore this causes trouble if front
2003 -- end inlining is activated (-gnatN).
2005 if In_Instance
2006 and then Scope (Current_Scope) /= Standard_Standard
2007 then
2008 Body_To_Inline :=
2009 Copy_Generic_Node (N, Empty, Instantiating => True);
2010 else
2011 -- ??? Shouldn't this use New_Copy_Tree? What about global
2012 -- references captured in the body to inline?
2014 Body_To_Inline := Copy_Separate_Tree (N);
2015 end if;
2017 -- Remove aspects/pragmas that have no meaning in an inlined body
2019 Remove_Aspects_And_Pragmas (Body_To_Inline);
2021 -- We need to capture references to the formals in order
2022 -- to substitute the actuals at the point of inlining, i.e.
2023 -- instantiation. To treat the formals as globals to the body to
2024 -- inline, we nest it within a dummy parameterless subprogram,
2025 -- declared within the real one.
2027 Set_Parameter_Specifications
2028 (Specification (Body_To_Inline), No_List);
2030 -- A new internal name is associated with Body_To_Inline to avoid
2031 -- conflicts when the non-inlined body N is analyzed.
2033 Set_Defining_Unit_Name (Specification (Body_To_Inline),
2034 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
2035 Set_Corresponding_Spec (Body_To_Inline, Empty);
2036 end Generate_Subprogram_Body;
2038 -- Local variables
2040 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2041 Original_Body : Node_Id;
2042 Body_To_Analyze : Node_Id;
2044 -- Start of processing for Build_Body_To_Inline
2046 begin
2047 pragma Assert (Current_Scope = Spec_Id);
2049 -- Within an instance, the body to inline must be treated as a nested
2050 -- generic, so that the proper global references are preserved. We
2051 -- do not do this at the library level, because it is not needed, and
2052 -- furthermore this causes trouble if front-end inlining is activated
2053 -- (-gnatN).
2055 if In_Instance
2056 and then Scope (Current_Scope) /= Standard_Standard
2057 then
2058 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2059 end if;
2061 -- Capture references to formals in order to substitute the actuals
2062 -- at the point of inlining or instantiation. To treat the formals
2063 -- as globals to the body to inline, nest the body within a dummy
2064 -- parameterless subprogram, declared within the real one.
2066 Generate_Subprogram_Body (N, Original_Body);
2067 Body_To_Analyze :=
2068 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
2070 -- Set return type of function, which is also global and does not
2071 -- need to be resolved.
2073 if Ekind (Spec_Id) = E_Function then
2074 Set_Result_Definition (Specification (Body_To_Analyze),
2075 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
2076 end if;
2078 if No (Declarations (N)) then
2079 Set_Declarations (N, New_List (Body_To_Analyze));
2080 else
2081 Append_To (Declarations (N), Body_To_Analyze);
2082 end if;
2084 Preanalyze (Body_To_Analyze);
2086 Push_Scope (Defining_Entity (Body_To_Analyze));
2087 Save_Global_References (Original_Body);
2088 End_Scope;
2089 Remove (Body_To_Analyze);
2091 -- Restore environment if previously saved
2093 if In_Instance
2094 and then Scope (Current_Scope) /= Standard_Standard
2095 then
2096 Restore_Env;
2097 end if;
2099 pragma Assert (No (Body_To_Inline (Decl)));
2100 Set_Body_To_Inline (Decl, Original_Body);
2101 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
2102 end Build_Body_To_Inline;
2104 --------------------------------
2105 -- Build_Return_Object_Formal --
2106 --------------------------------
2108 procedure Build_Return_Object_Formal
2109 (Loc : Source_Ptr;
2110 Obj_Decl : Node_Id;
2111 Formals : List_Id)
2113 Obj_Def : constant Node_Id := Object_Definition (Obj_Decl);
2114 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2115 Typ_Def : Node_Id;
2117 begin
2118 -- Build the type definition of the formal parameter. The use of
2119 -- New_Copy_Tree ensures that global references preserved in the
2120 -- case of generics.
2122 if Is_Entity_Name (Obj_Def) then
2123 Typ_Def := New_Copy_Tree (Obj_Def);
2124 else
2125 Typ_Def := New_Copy_Tree (Subtype_Mark (Obj_Def));
2126 end if;
2128 -- Generate:
2130 -- Obj_Id : [out] Typ_Def
2132 -- Mode OUT should not be used when the return object is declared as
2133 -- a constant. Check the definition of the object declaration because
2134 -- the object has not been analyzed yet.
2136 Append_To (Formals,
2137 Make_Parameter_Specification (Loc,
2138 Defining_Identifier =>
2139 Make_Defining_Identifier (Loc, Chars (Obj_Id)),
2140 In_Present => False,
2141 Out_Present => not Constant_Present (Obj_Decl),
2142 Null_Exclusion_Present => False,
2143 Parameter_Type => Typ_Def));
2144 end Build_Return_Object_Formal;
2146 --------------------------------------
2147 -- Can_Split_Unconstrained_Function --
2148 --------------------------------------
2150 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
2151 Stmt : constant Node_Id :=
2152 First (Statements (Handled_Statement_Sequence (N)));
2153 Decl : Node_Id;
2155 begin
2156 -- No user defined declarations allowed in the function except inside
2157 -- the unique return statement; implicit labels are the only allowed
2158 -- declarations.
2160 Decl := First (Declarations (N));
2161 while Present (Decl) loop
2162 if Nkind (Decl) /= N_Implicit_Label_Declaration then
2163 return False;
2164 end if;
2166 Next (Decl);
2167 end loop;
2169 -- We only split the inlined function when we are generating the code
2170 -- of its body; otherwise we leave duplicated split subprograms in
2171 -- the tree which (if referenced) generate wrong references at link
2172 -- time.
2174 return In_Extended_Main_Code_Unit (N)
2175 and then Present (Stmt)
2176 and then Nkind (Stmt) = N_Extended_Return_Statement
2177 and then No (Next (Stmt))
2178 and then Present (Handled_Statement_Sequence (Stmt));
2179 end Can_Split_Unconstrained_Function;
2181 ------------------
2182 -- Copy_Formals --
2183 ------------------
2185 procedure Copy_Formals
2186 (Loc : Source_Ptr;
2187 Subp_Id : Entity_Id;
2188 Formals : List_Id)
2190 Formal : Entity_Id;
2191 Spec : Node_Id;
2193 begin
2194 Formal := First_Formal (Subp_Id);
2195 while Present (Formal) loop
2196 Spec := Parent (Formal);
2198 -- Create an exact copy of the formal parameter. The use of
2199 -- New_Copy_Tree ensures that global references are preserved
2200 -- in case of generics.
2202 Append_To (Formals,
2203 Make_Parameter_Specification (Loc,
2204 Defining_Identifier =>
2205 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
2206 In_Present => In_Present (Spec),
2207 Out_Present => Out_Present (Spec),
2208 Null_Exclusion_Present => Null_Exclusion_Present (Spec),
2209 Parameter_Type =>
2210 New_Copy_Tree (Parameter_Type (Spec)),
2211 Expression => New_Copy_Tree (Expression (Spec))));
2213 Next_Formal (Formal);
2214 end loop;
2215 end Copy_Formals;
2217 ------------------------
2218 -- Copy_Return_Object --
2219 ------------------------
2221 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id is
2222 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2224 begin
2225 -- The use of New_Copy_Tree ensures that global references are
2226 -- preserved in case of generics.
2228 return
2229 Make_Object_Declaration (Sloc (Obj_Decl),
2230 Defining_Identifier =>
2231 Make_Defining_Identifier (Sloc (Obj_Id), Chars (Obj_Id)),
2232 Aliased_Present => Aliased_Present (Obj_Decl),
2233 Constant_Present => Constant_Present (Obj_Decl),
2234 Null_Exclusion_Present => Null_Exclusion_Present (Obj_Decl),
2235 Object_Definition =>
2236 New_Copy_Tree (Object_Definition (Obj_Decl)),
2237 Expression => New_Copy_Tree (Expression (Obj_Decl)));
2238 end Copy_Return_Object;
2240 ----------------------------------
2241 -- Split_Unconstrained_Function --
2242 ----------------------------------
2244 procedure Split_Unconstrained_Function
2245 (N : Node_Id;
2246 Spec_Id : Entity_Id)
2248 Loc : constant Source_Ptr := Sloc (N);
2249 Ret_Stmt : constant Node_Id :=
2250 First (Statements (Handled_Statement_Sequence (N)));
2251 Ret_Obj : constant Node_Id :=
2252 First (Return_Object_Declarations (Ret_Stmt));
2254 procedure Build_Procedure
2255 (Proc_Id : out Entity_Id;
2256 Decl_List : out List_Id);
2257 -- Build a procedure containing the statements found in the extended
2258 -- return statement of the unconstrained function body N.
2260 ---------------------
2261 -- Build_Procedure --
2262 ---------------------
2264 procedure Build_Procedure
2265 (Proc_Id : out Entity_Id;
2266 Decl_List : out List_Id)
2268 Formals : constant List_Id := New_List;
2269 Subp_Name : constant Name_Id := New_Internal_Name ('F');
2271 Body_Decls : List_Id := No_List;
2272 Decl : Node_Id;
2273 Proc_Body : Node_Id;
2274 Proc_Spec : Node_Id;
2276 begin
2277 -- Create formal parameters for the return object and all formals
2278 -- of the unconstrained function in order to pass their values to
2279 -- the procedure.
2281 Build_Return_Object_Formal
2282 (Loc => Loc,
2283 Obj_Decl => Ret_Obj,
2284 Formals => Formals);
2286 Copy_Formals
2287 (Loc => Loc,
2288 Subp_Id => Spec_Id,
2289 Formals => Formals);
2291 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
2293 Proc_Spec :=
2294 Make_Procedure_Specification (Loc,
2295 Defining_Unit_Name => Proc_Id,
2296 Parameter_Specifications => Formals);
2298 Decl_List := New_List;
2300 Append_To (Decl_List,
2301 Make_Subprogram_Declaration (Loc, Proc_Spec));
2303 -- Can_Convert_Unconstrained_Function checked that the function
2304 -- has no local declarations except implicit label declarations.
2305 -- Copy these declarations to the built procedure.
2307 if Present (Declarations (N)) then
2308 Body_Decls := New_List;
2310 Decl := First (Declarations (N));
2311 while Present (Decl) loop
2312 pragma Assert (Nkind (Decl) = N_Implicit_Label_Declaration);
2314 Append_To (Body_Decls,
2315 Make_Implicit_Label_Declaration (Loc,
2316 Make_Defining_Identifier (Loc,
2317 Chars => Chars (Defining_Identifier (Decl))),
2318 Label_Construct => Empty));
2320 Next (Decl);
2321 end loop;
2322 end if;
2324 pragma Assert (Present (Handled_Statement_Sequence (Ret_Stmt)));
2326 Proc_Body :=
2327 Make_Subprogram_Body (Loc,
2328 Specification => Copy_Subprogram_Spec (Proc_Spec),
2329 Declarations => Body_Decls,
2330 Handled_Statement_Sequence =>
2331 New_Copy_Tree (Handled_Statement_Sequence (Ret_Stmt)));
2333 Set_Defining_Unit_Name (Specification (Proc_Body),
2334 Make_Defining_Identifier (Loc, Subp_Name));
2336 Append_To (Decl_List, Proc_Body);
2337 end Build_Procedure;
2339 -- Local variables
2341 New_Obj : constant Node_Id := Copy_Return_Object (Ret_Obj);
2342 Blk_Stmt : Node_Id;
2343 Proc_Call : Node_Id;
2344 Proc_Id : Entity_Id;
2346 -- Start of processing for Split_Unconstrained_Function
2348 begin
2349 -- Build the associated procedure, analyze it and insert it before
2350 -- the function body N.
2352 declare
2353 Scope : constant Entity_Id := Current_Scope;
2354 Decl_List : List_Id;
2355 begin
2356 Pop_Scope;
2357 Build_Procedure (Proc_Id, Decl_List);
2358 Insert_Actions (N, Decl_List);
2359 Set_Is_Inlined (Proc_Id);
2360 Push_Scope (Scope);
2361 end;
2363 -- Build the call to the generated procedure
2365 declare
2366 Actual_List : constant List_Id := New_List;
2367 Formal : Entity_Id;
2369 begin
2370 Append_To (Actual_List,
2371 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
2373 Formal := First_Formal (Spec_Id);
2374 while Present (Formal) loop
2375 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
2377 -- Avoid spurious warning on unreferenced formals
2379 Set_Referenced (Formal);
2380 Next_Formal (Formal);
2381 end loop;
2383 Proc_Call :=
2384 Make_Procedure_Call_Statement (Loc,
2385 Name => New_Occurrence_Of (Proc_Id, Loc),
2386 Parameter_Associations => Actual_List);
2387 end;
2389 -- Generate:
2391 -- declare
2392 -- New_Obj : ...
2393 -- begin
2394 -- Proc (New_Obj, ...);
2395 -- return New_Obj;
2396 -- end;
2398 Blk_Stmt :=
2399 Make_Block_Statement (Loc,
2400 Declarations => New_List (New_Obj),
2401 Handled_Statement_Sequence =>
2402 Make_Handled_Sequence_Of_Statements (Loc,
2403 Statements => New_List (
2405 Proc_Call,
2407 Make_Simple_Return_Statement (Loc,
2408 Expression =>
2409 New_Occurrence_Of
2410 (Defining_Identifier (New_Obj), Loc)))));
2412 Rewrite (Ret_Stmt, Blk_Stmt);
2413 end Split_Unconstrained_Function;
2415 -- Local variables
2417 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2419 -- Start of processing for Check_And_Split_Unconstrained_Function
2421 begin
2422 pragma Assert (Back_End_Inlining
2423 and then Ekind (Spec_Id) = E_Function
2424 and then Returns_Unconstrained_Type (Spec_Id)
2425 and then Comes_From_Source (Body_Id)
2426 and then (Has_Pragma_Inline_Always (Spec_Id)
2427 or else Optimization_Level > 0));
2429 -- This routine must not be used in GNATprove mode since GNATprove
2430 -- relies on frontend inlining
2432 pragma Assert (not GNATprove_Mode);
2434 -- No need to split the function if we cannot generate the code
2436 if Serious_Errors_Detected /= 0 then
2437 return;
2438 end if;
2440 -- No action needed in stubs since the attribute Body_To_Inline
2441 -- is not available
2443 if Nkind (Decl) = N_Subprogram_Body_Stub then
2444 return;
2446 -- Cannot build the body to inline if the attribute is already set.
2447 -- This attribute may have been set if this is a subprogram renaming
2448 -- declarations (see Freeze.Build_Renamed_Body).
2450 elsif Present (Body_To_Inline (Decl)) then
2451 return;
2453 -- Do not generate a body to inline for protected functions, because the
2454 -- transformation generates a call to a protected procedure, causing
2455 -- spurious errors. We don't inline protected operations anyway, so
2456 -- this is no loss. We might as well ignore intrinsics and foreign
2457 -- conventions as well -- just allow Ada conventions.
2459 elsif not (Convention (Spec_Id) = Convention_Ada
2460 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Copy
2461 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Reference)
2462 then
2463 return;
2465 -- Check excluded declarations
2467 elsif Present (Declarations (N))
2468 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
2469 then
2470 return;
2472 -- Check excluded statements. There is no need to protect us against
2473 -- exception handlers since they are supported by the GCC backend.
2475 elsif Present (Handled_Statement_Sequence (N))
2476 and then Has_Excluded_Statement
2477 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2478 then
2479 return;
2480 end if;
2482 -- Build the body to inline only if really needed
2484 if Can_Split_Unconstrained_Function (N) then
2485 Split_Unconstrained_Function (N, Spec_Id);
2486 Build_Body_To_Inline (N, Spec_Id);
2487 Set_Is_Inlined (Spec_Id);
2488 end if;
2489 end Check_And_Split_Unconstrained_Function;
2491 -------------------------------------
2492 -- Check_Package_Body_For_Inlining --
2493 -------------------------------------
2495 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2496 Bname : Unit_Name_Type;
2497 E : Entity_Id;
2498 OK : Boolean;
2500 begin
2501 -- Legacy implementation (relying on frontend inlining)
2503 if not Back_End_Inlining
2504 and then Is_Compilation_Unit (P)
2505 and then not Is_Generic_Instance (P)
2506 then
2507 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2509 E := First_Entity (P);
2510 while Present (E) loop
2511 if Has_Pragma_Inline_Always (E)
2512 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2513 then
2514 if not Is_Loaded (Bname) then
2515 Load_Needed_Body (N, OK);
2517 if OK then
2519 -- Check we are not trying to inline a parent whose body
2520 -- depends on a child, when we are compiling the body of
2521 -- the child. Otherwise we have a potential elaboration
2522 -- circularity with inlined subprograms and with
2523 -- Taft-Amendment types.
2525 declare
2526 Comp : Node_Id; -- Body just compiled
2527 Child_Spec : Entity_Id; -- Spec of main unit
2528 Ent : Entity_Id; -- For iteration
2529 With_Clause : Node_Id; -- Context of body.
2531 begin
2532 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2533 and then Present (Body_Entity (P))
2534 then
2535 Child_Spec :=
2536 Defining_Entity
2537 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2539 Comp :=
2540 Parent (Unit_Declaration_Node (Body_Entity (P)));
2542 -- Check whether the context of the body just
2543 -- compiled includes a child of itself, and that
2544 -- child is the spec of the main compilation.
2546 With_Clause := First (Context_Items (Comp));
2547 while Present (With_Clause) loop
2548 if Nkind (With_Clause) = N_With_Clause
2549 and then
2550 Scope (Entity (Name (With_Clause))) = P
2551 and then
2552 Entity (Name (With_Clause)) = Child_Spec
2553 then
2554 Error_Msg_Node_2 := Child_Spec;
2555 Error_Msg_NE
2556 ("body of & depends on child unit&??",
2557 With_Clause, P);
2558 Error_Msg_N
2559 ("\subprograms in body cannot be inlined??",
2560 With_Clause);
2562 -- Disable further inlining from this unit,
2563 -- and keep Taft-amendment types incomplete.
2565 Ent := First_Entity (P);
2566 while Present (Ent) loop
2567 if Is_Type (Ent)
2568 and then Has_Completion_In_Body (Ent)
2569 then
2570 Set_Full_View (Ent, Empty);
2572 elsif Is_Subprogram (Ent) then
2573 Set_Is_Inlined (Ent, False);
2574 end if;
2576 Next_Entity (Ent);
2577 end loop;
2579 return;
2580 end if;
2582 Next (With_Clause);
2583 end loop;
2584 end if;
2585 end;
2587 elsif Ineffective_Inline_Warnings then
2588 Error_Msg_Unit_1 := Bname;
2589 Error_Msg_N
2590 ("unable to inline subprograms defined in $??", P);
2591 Error_Msg_N ("\body not found??", P);
2592 return;
2593 end if;
2594 end if;
2596 return;
2597 end if;
2599 Next_Entity (E);
2600 end loop;
2601 end if;
2602 end Check_Package_Body_For_Inlining;
2604 --------------------
2605 -- Cleanup_Scopes --
2606 --------------------
2608 procedure Cleanup_Scopes is
2609 Elmt : Elmt_Id;
2610 Decl : Node_Id;
2611 Scop : Entity_Id;
2613 begin
2614 Elmt := First_Elmt (To_Clean);
2615 while Present (Elmt) loop
2616 Scop := Node (Elmt);
2618 if Ekind (Scop) = E_Entry then
2619 Scop := Protected_Body_Subprogram (Scop);
2621 elsif Is_Subprogram (Scop)
2622 and then Is_Protected_Type (Scope (Scop))
2623 and then Present (Protected_Body_Subprogram (Scop))
2624 then
2625 -- If a protected operation contains an instance, its cleanup
2626 -- operations have been delayed, and the subprogram has been
2627 -- rewritten in the expansion of the enclosing protected body. It
2628 -- is the corresponding subprogram that may require the cleanup
2629 -- operations, so propagate the information that triggers cleanup
2630 -- activity.
2632 Set_Uses_Sec_Stack
2633 (Protected_Body_Subprogram (Scop),
2634 Uses_Sec_Stack (Scop));
2636 Scop := Protected_Body_Subprogram (Scop);
2637 end if;
2639 if Ekind (Scop) = E_Block then
2640 Decl := Parent (Block_Node (Scop));
2642 else
2643 Decl := Unit_Declaration_Node (Scop);
2645 if Nkind_In (Decl, N_Subprogram_Declaration,
2646 N_Task_Type_Declaration,
2647 N_Subprogram_Body_Stub)
2648 then
2649 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2650 end if;
2651 end if;
2653 Push_Scope (Scop);
2654 Expand_Cleanup_Actions (Decl);
2655 End_Scope;
2657 Elmt := Next_Elmt (Elmt);
2658 end loop;
2659 end Cleanup_Scopes;
2661 -------------------------
2662 -- Expand_Inlined_Call --
2663 -------------------------
2665 procedure Expand_Inlined_Call
2666 (N : Node_Id;
2667 Subp : Entity_Id;
2668 Orig_Subp : Entity_Id)
2670 Decls : constant List_Id := New_List;
2671 Is_Predef : constant Boolean :=
2672 Is_Predefined_Unit (Get_Source_Unit (Subp));
2673 Loc : constant Source_Ptr := Sloc (N);
2674 Orig_Bod : constant Node_Id :=
2675 Body_To_Inline (Unit_Declaration_Node (Subp));
2677 Uses_Back_End : constant Boolean :=
2678 Back_End_Inlining and then Optimization_Level > 0;
2679 -- The back-end expansion is used if the target supports back-end
2680 -- inlining and some level of optimixation is required; otherwise
2681 -- the inlining takes place fully as a tree expansion.
2683 Blk : Node_Id;
2684 Decl : Node_Id;
2685 Exit_Lab : Entity_Id := Empty;
2686 F : Entity_Id;
2687 A : Node_Id;
2688 Lab_Decl : Node_Id := Empty;
2689 Lab_Id : Node_Id;
2690 New_A : Node_Id;
2691 Num_Ret : Nat := 0;
2692 Ret_Type : Entity_Id;
2693 Temp : Entity_Id;
2694 Temp_Typ : Entity_Id;
2696 Is_Unc : Boolean;
2697 Is_Unc_Decl : Boolean;
2698 -- If the type returned by the function is unconstrained and the call
2699 -- can be inlined, special processing is required.
2701 Return_Object : Entity_Id := Empty;
2702 -- Entity in declaration in an extended_return_statement
2704 Targ : Node_Id := Empty;
2705 -- The target of the call. If context is an assignment statement then
2706 -- this is the left-hand side of the assignment, else it is a temporary
2707 -- to which the return value is assigned prior to rewriting the call.
2709 Targ1 : Node_Id := Empty;
2710 -- A separate target used when the return type is unconstrained
2712 procedure Declare_Postconditions_Result;
2713 -- When generating C code, declare _Result, which may be used in the
2714 -- inlined _Postconditions procedure to verify the return value.
2716 procedure Make_Exit_Label;
2717 -- Build declaration for exit label to be used in Return statements,
2718 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2719 -- declaration). Does nothing if Exit_Lab already set.
2721 procedure Make_Loop_Labels_Unique (HSS : Node_Id);
2722 -- When compiling for CCG and performing front-end inlining, replace
2723 -- loop names and references to them so that they do not conflict with
2724 -- homographs in the current subprogram.
2726 function Process_Formals (N : Node_Id) return Traverse_Result;
2727 -- Replace occurrence of a formal with the corresponding actual, or the
2728 -- thunk generated for it. Replace a return statement with an assignment
2729 -- to the target of the call, with appropriate conversions if needed.
2731 function Process_Formals_In_Aspects (N : Node_Id) return Traverse_Result;
2732 -- Because aspects are linked indirectly to the rest of the tree,
2733 -- replacement of formals appearing in aspect specifications must
2734 -- be performed in a separate pass, using an instantiation of the
2735 -- previous subprogram over aspect specifications reachable from N.
2737 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2738 -- If the call being expanded is that of an internal subprogram, set the
2739 -- sloc of the generated block to that of the call itself, so that the
2740 -- expansion is skipped by the "next" command in gdb. Same processing
2741 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2742 -- Debug_Generated_Code is true, suppress this change to simplify our
2743 -- own development. Same in GNATprove mode, to ensure that warnings and
2744 -- diagnostics point to the proper location.
2746 procedure Reset_Dispatching_Calls (N : Node_Id);
2747 -- In subtree N search for occurrences of dispatching calls that use the
2748 -- Ada 2005 Object.Operation notation and the object is a formal of the
2749 -- inlined subprogram. Reset the entity associated with Operation in all
2750 -- the found occurrences.
2752 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2753 -- If the function body is a single expression, replace call with
2754 -- expression, else insert block appropriately.
2756 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2757 -- If procedure body has no local variables, inline body without
2758 -- creating block, otherwise rewrite call with block.
2760 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2761 -- Determine whether a formal parameter is used only once in Orig_Bod
2763 -----------------------------------
2764 -- Declare_Postconditions_Result --
2765 -----------------------------------
2767 procedure Declare_Postconditions_Result is
2768 Enclosing_Subp : constant Entity_Id := Scope (Subp);
2770 begin
2771 pragma Assert
2772 (Modify_Tree_For_C
2773 and then Is_Subprogram (Enclosing_Subp)
2774 and then Present (Postconditions_Proc (Enclosing_Subp)));
2776 if Ekind (Enclosing_Subp) = E_Function then
2777 if Nkind (First (Parameter_Associations (N))) in
2778 N_Numeric_Or_String_Literal
2779 then
2780 Append_To (Declarations (Blk),
2781 Make_Object_Declaration (Loc,
2782 Defining_Identifier =>
2783 Make_Defining_Identifier (Loc, Name_uResult),
2784 Constant_Present => True,
2785 Object_Definition =>
2786 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2787 Expression =>
2788 New_Copy_Tree (First (Parameter_Associations (N)))));
2789 else
2790 Append_To (Declarations (Blk),
2791 Make_Object_Renaming_Declaration (Loc,
2792 Defining_Identifier =>
2793 Make_Defining_Identifier (Loc, Name_uResult),
2794 Subtype_Mark =>
2795 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2796 Name =>
2797 New_Copy_Tree (First (Parameter_Associations (N)))));
2798 end if;
2799 end if;
2800 end Declare_Postconditions_Result;
2802 ---------------------
2803 -- Make_Exit_Label --
2804 ---------------------
2806 procedure Make_Exit_Label is
2807 Lab_Ent : Entity_Id;
2808 begin
2809 if No (Exit_Lab) then
2810 Lab_Ent := Make_Temporary (Loc, 'L');
2811 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
2812 Exit_Lab := Make_Label (Loc, Lab_Id);
2813 Lab_Decl :=
2814 Make_Implicit_Label_Declaration (Loc,
2815 Defining_Identifier => Lab_Ent,
2816 Label_Construct => Exit_Lab);
2817 end if;
2818 end Make_Exit_Label;
2820 -----------------------------
2821 -- Make_Loop_Labels_Unique --
2822 -----------------------------
2824 procedure Make_Loop_Labels_Unique (HSS : Node_Id) is
2825 function Process_Loop (N : Node_Id) return Traverse_Result;
2827 ------------------
2828 -- Process_Loop --
2829 ------------------
2831 function Process_Loop (N : Node_Id) return Traverse_Result is
2832 Id : Entity_Id;
2834 begin
2835 if Nkind (N) = N_Loop_Statement
2836 and then Present (Identifier (N))
2837 then
2838 -- Create new external name for loop and update the
2839 -- corresponding entity.
2841 Id := Entity (Identifier (N));
2842 Set_Chars (Id, New_External_Name (Chars (Id), 'L', -1));
2843 Set_Chars (Identifier (N), Chars (Id));
2845 elsif Nkind (N) = N_Exit_Statement
2846 and then Present (Name (N))
2847 then
2848 -- The exit statement must name an enclosing loop, whose name
2849 -- has already been updated.
2851 Set_Chars (Name (N), Chars (Entity (Name (N))));
2852 end if;
2854 return OK;
2855 end Process_Loop;
2857 procedure Update_Loop_Names is new Traverse_Proc (Process_Loop);
2859 -- Local variables
2861 Stmt : Node_Id;
2863 -- Start of processing for Make_Loop_Labels_Unique
2865 begin
2866 if Modify_Tree_For_C then
2867 Stmt := First (Statements (HSS));
2868 while Present (Stmt) loop
2869 Update_Loop_Names (Stmt);
2870 Next (Stmt);
2871 end loop;
2872 end if;
2873 end Make_Loop_Labels_Unique;
2875 ---------------------
2876 -- Process_Formals --
2877 ---------------------
2879 function Process_Formals (N : Node_Id) return Traverse_Result is
2880 A : Entity_Id;
2881 E : Entity_Id;
2882 Ret : Node_Id;
2884 begin
2885 if Is_Entity_Name (N) and then Present (Entity (N)) then
2886 E := Entity (N);
2888 if Is_Formal (E) and then Scope (E) = Subp then
2889 A := Renamed_Object (E);
2891 -- Rewrite the occurrence of the formal into an occurrence of
2892 -- the actual. Also establish visibility on the proper view of
2893 -- the actual's subtype for the body's context (if the actual's
2894 -- subtype is private at the call point but its full view is
2895 -- visible to the body, then the inlined tree here must be
2896 -- analyzed with the full view).
2898 if Is_Entity_Name (A) then
2899 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
2900 Check_Private_View (N);
2902 elsif Nkind (A) = N_Defining_Identifier then
2903 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
2904 Check_Private_View (N);
2906 -- Numeric literal
2908 else
2909 Rewrite (N, New_Copy (A));
2910 end if;
2911 end if;
2913 return Skip;
2915 elsif Is_Entity_Name (N)
2916 and then Present (Return_Object)
2917 and then Chars (N) = Chars (Return_Object)
2918 then
2919 -- Occurrence within an extended return statement. The return
2920 -- object is local to the body been inlined, and thus the generic
2921 -- copy is not analyzed yet, so we match by name, and replace it
2922 -- with target of call.
2924 if Nkind (Targ) = N_Defining_Identifier then
2925 Rewrite (N, New_Occurrence_Of (Targ, Loc));
2926 else
2927 Rewrite (N, New_Copy_Tree (Targ));
2928 end if;
2930 return Skip;
2932 elsif Nkind (N) = N_Simple_Return_Statement then
2933 if No (Expression (N)) then
2934 Num_Ret := Num_Ret + 1;
2935 Make_Exit_Label;
2936 Rewrite (N,
2937 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2939 else
2940 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2941 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2942 then
2943 -- Function body is a single expression. No need for
2944 -- exit label.
2946 null;
2948 else
2949 Num_Ret := Num_Ret + 1;
2950 Make_Exit_Label;
2951 end if;
2953 -- Because of the presence of private types, the views of the
2954 -- expression and the context may be different, so place
2955 -- a type conversion to the context type to avoid spurious
2956 -- errors, e.g. when the expression is a numeric literal and
2957 -- the context is private. If the expression is an aggregate,
2958 -- use a qualified expression, because an aggregate is not a
2959 -- legal argument of a conversion. Ditto for numeric, character
2960 -- and string literals, and attributes that yield a universal
2961 -- type, because those must be resolved to a specific type.
2963 if Nkind_In (Expression (N), N_Aggregate,
2964 N_Character_Literal,
2965 N_Null,
2966 N_String_Literal)
2967 or else Yields_Universal_Type (Expression (N))
2968 then
2969 Ret :=
2970 Make_Qualified_Expression (Sloc (N),
2971 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2972 Expression => Relocate_Node (Expression (N)));
2974 -- Use an unchecked type conversion between access types, for
2975 -- which a type conversion would not always be valid, as no
2976 -- check may result from the conversion.
2978 elsif Is_Access_Type (Ret_Type) then
2979 Ret :=
2980 Unchecked_Convert_To
2981 (Ret_Type, Relocate_Node (Expression (N)));
2983 -- Otherwise use a type conversion, which may trigger a check
2985 else
2986 Ret :=
2987 Make_Type_Conversion (Sloc (N),
2988 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2989 Expression => Relocate_Node (Expression (N)));
2990 end if;
2992 if Nkind (Targ) = N_Defining_Identifier then
2993 Rewrite (N,
2994 Make_Assignment_Statement (Loc,
2995 Name => New_Occurrence_Of (Targ, Loc),
2996 Expression => Ret));
2997 else
2998 Rewrite (N,
2999 Make_Assignment_Statement (Loc,
3000 Name => New_Copy (Targ),
3001 Expression => Ret));
3002 end if;
3004 Set_Assignment_OK (Name (N));
3006 if Present (Exit_Lab) then
3007 Insert_After (N,
3008 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3009 end if;
3010 end if;
3012 return OK;
3014 -- An extended return becomes a block whose first statement is the
3015 -- assignment of the initial expression of the return object to the
3016 -- target of the call itself.
3018 elsif Nkind (N) = N_Extended_Return_Statement then
3019 declare
3020 Return_Decl : constant Entity_Id :=
3021 First (Return_Object_Declarations (N));
3022 Assign : Node_Id;
3024 begin
3025 Return_Object := Defining_Identifier (Return_Decl);
3027 if Present (Expression (Return_Decl)) then
3028 if Nkind (Targ) = N_Defining_Identifier then
3029 Assign :=
3030 Make_Assignment_Statement (Loc,
3031 Name => New_Occurrence_Of (Targ, Loc),
3032 Expression => Expression (Return_Decl));
3033 else
3034 Assign :=
3035 Make_Assignment_Statement (Loc,
3036 Name => New_Copy (Targ),
3037 Expression => Expression (Return_Decl));
3038 end if;
3040 Set_Assignment_OK (Name (Assign));
3042 if No (Handled_Statement_Sequence (N)) then
3043 Set_Handled_Statement_Sequence (N,
3044 Make_Handled_Sequence_Of_Statements (Loc,
3045 Statements => New_List));
3046 end if;
3048 Prepend (Assign,
3049 Statements (Handled_Statement_Sequence (N)));
3050 end if;
3052 Rewrite (N,
3053 Make_Block_Statement (Loc,
3054 Handled_Statement_Sequence =>
3055 Handled_Statement_Sequence (N)));
3057 return OK;
3058 end;
3060 -- Remove pragma Unreferenced since it may refer to formals that
3061 -- are not visible in the inlined body, and in any case we will
3062 -- not be posting warnings on the inlined body so it is unneeded.
3064 elsif Nkind (N) = N_Pragma
3065 and then Pragma_Name (N) = Name_Unreferenced
3066 then
3067 Rewrite (N, Make_Null_Statement (Sloc (N)));
3068 return OK;
3070 else
3071 return OK;
3072 end if;
3073 end Process_Formals;
3075 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3077 --------------------------------
3078 -- Process_Formals_In_Aspects --
3079 --------------------------------
3081 function Process_Formals_In_Aspects
3082 (N : Node_Id) return Traverse_Result
3084 A : Node_Id;
3086 begin
3087 if Has_Aspects (N) then
3088 A := First (Aspect_Specifications (N));
3089 while Present (A) loop
3090 Replace_Formals (Expression (A));
3092 Next (A);
3093 end loop;
3094 end if;
3095 return OK;
3096 end Process_Formals_In_Aspects;
3098 procedure Replace_Formals_In_Aspects is
3099 new Traverse_Proc (Process_Formals_In_Aspects);
3101 ------------------
3102 -- Process_Sloc --
3103 ------------------
3105 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3106 begin
3107 if not Debug_Generated_Code then
3108 Set_Sloc (Nod, Sloc (N));
3109 Set_Comes_From_Source (Nod, False);
3110 end if;
3112 return OK;
3113 end Process_Sloc;
3115 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3117 ------------------------------
3118 -- Reset_Dispatching_Calls --
3119 ------------------------------
3121 procedure Reset_Dispatching_Calls (N : Node_Id) is
3123 function Do_Reset (N : Node_Id) return Traverse_Result;
3124 -- Comment required ???
3126 --------------
3127 -- Do_Reset --
3128 --------------
3130 function Do_Reset (N : Node_Id) return Traverse_Result is
3131 begin
3132 if Nkind (N) = N_Procedure_Call_Statement
3133 and then Nkind (Name (N)) = N_Selected_Component
3134 and then Nkind (Prefix (Name (N))) = N_Identifier
3135 and then Is_Formal (Entity (Prefix (Name (N))))
3136 and then Is_Dispatching_Operation
3137 (Entity (Selector_Name (Name (N))))
3138 then
3139 Set_Entity (Selector_Name (Name (N)), Empty);
3140 end if;
3142 return OK;
3143 end Do_Reset;
3145 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
3147 -- Local variables
3149 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
3150 pragma Unreferenced (Dummy);
3152 -- Start of processing for Reset_Dispatching_Calls
3154 begin
3155 null;
3156 end Reset_Dispatching_Calls;
3158 ---------------------------
3159 -- Rewrite_Function_Call --
3160 ---------------------------
3162 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3163 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3164 Fst : constant Node_Id := First (Statements (HSS));
3166 begin
3167 Make_Loop_Labels_Unique (HSS);
3169 -- Optimize simple case: function body is a single return statement,
3170 -- which has been expanded into an assignment.
3172 if Is_Empty_List (Declarations (Blk))
3173 and then Nkind (Fst) = N_Assignment_Statement
3174 and then No (Next (Fst))
3175 then
3176 -- The function call may have been rewritten as the temporary
3177 -- that holds the result of the call, in which case remove the
3178 -- now useless declaration.
3180 if Nkind (N) = N_Identifier
3181 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3182 then
3183 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3184 end if;
3186 Rewrite (N, Expression (Fst));
3188 elsif Nkind (N) = N_Identifier
3189 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3190 then
3191 -- The block assigns the result of the call to the temporary
3193 Insert_After (Parent (Entity (N)), Blk);
3195 -- If the context is an assignment, and the left-hand side is free of
3196 -- side-effects, the replacement is also safe.
3197 -- Can this be generalized further???
3199 elsif Nkind (Parent (N)) = N_Assignment_Statement
3200 and then
3201 (Is_Entity_Name (Name (Parent (N)))
3202 or else
3203 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3204 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
3206 or else
3207 (Nkind (Name (Parent (N))) = N_Selected_Component
3208 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3209 then
3210 -- Replace assignment with the block
3212 declare
3213 Original_Assignment : constant Node_Id := Parent (N);
3215 begin
3216 -- Preserve the original assignment node to keep the complete
3217 -- assignment subtree consistent enough for Analyze_Assignment
3218 -- to proceed (specifically, the original Lhs node must still
3219 -- have an assignment statement as its parent).
3221 -- We cannot rely on Original_Node to go back from the block
3222 -- node to the assignment node, because the assignment might
3223 -- already be a rewrite substitution.
3225 Discard_Node (Relocate_Node (Original_Assignment));
3226 Rewrite (Original_Assignment, Blk);
3227 end;
3229 elsif Nkind (Parent (N)) = N_Object_Declaration then
3231 -- A call to a function which returns an unconstrained type
3232 -- found in the expression initializing an object-declaration is
3233 -- expanded into a procedure call which must be added after the
3234 -- object declaration.
3236 if Is_Unc_Decl and Back_End_Inlining then
3237 Insert_Action_After (Parent (N), Blk);
3238 else
3239 Set_Expression (Parent (N), Empty);
3240 Insert_After (Parent (N), Blk);
3241 end if;
3243 elsif Is_Unc and then not Back_End_Inlining then
3244 Insert_Before (Parent (N), Blk);
3245 end if;
3246 end Rewrite_Function_Call;
3248 ----------------------------
3249 -- Rewrite_Procedure_Call --
3250 ----------------------------
3252 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3253 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3255 begin
3256 Make_Loop_Labels_Unique (HSS);
3258 -- If there is a transient scope for N, this will be the scope of the
3259 -- actions for N, and the statements in Blk need to be within this
3260 -- scope. For example, they need to have visibility on the constant
3261 -- declarations created for the formals.
3263 -- If N needs no transient scope, and if there are no declarations in
3264 -- the inlined body, we can do a little optimization and insert the
3265 -- statements for the body directly after N, and rewrite N to a
3266 -- null statement, instead of rewriting N into a full-blown block
3267 -- statement.
3269 if not Scope_Is_Transient
3270 and then Is_Empty_List (Declarations (Blk))
3271 then
3272 Insert_List_After (N, Statements (HSS));
3273 Rewrite (N, Make_Null_Statement (Loc));
3274 else
3275 Rewrite (N, Blk);
3276 end if;
3277 end Rewrite_Procedure_Call;
3279 -------------------------
3280 -- Formal_Is_Used_Once --
3281 -------------------------
3283 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3284 Use_Counter : Int := 0;
3286 function Count_Uses (N : Node_Id) return Traverse_Result;
3287 -- Traverse the tree and count the uses of the formal parameter.
3288 -- In this case, for optimization purposes, we do not need to
3289 -- continue the traversal once more than one use is encountered.
3291 ----------------
3292 -- Count_Uses --
3293 ----------------
3295 function Count_Uses (N : Node_Id) return Traverse_Result is
3296 begin
3297 -- The original node is an identifier
3299 if Nkind (N) = N_Identifier
3300 and then Present (Entity (N))
3302 -- Original node's entity points to the one in the copied body
3304 and then Nkind (Entity (N)) = N_Identifier
3305 and then Present (Entity (Entity (N)))
3307 -- The entity of the copied node is the formal parameter
3309 and then Entity (Entity (N)) = Formal
3310 then
3311 Use_Counter := Use_Counter + 1;
3313 if Use_Counter > 1 then
3315 -- Denote more than one use and abandon the traversal
3317 Use_Counter := 2;
3318 return Abandon;
3320 end if;
3321 end if;
3323 return OK;
3324 end Count_Uses;
3326 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3328 -- Start of processing for Formal_Is_Used_Once
3330 begin
3331 Count_Formal_Uses (Orig_Bod);
3332 return Use_Counter = 1;
3333 end Formal_Is_Used_Once;
3335 -- Start of processing for Expand_Inlined_Call
3337 begin
3338 -- Initializations for old/new semantics
3340 if not Uses_Back_End then
3341 Is_Unc := Is_Array_Type (Etype (Subp))
3342 and then not Is_Constrained (Etype (Subp));
3343 Is_Unc_Decl := False;
3344 else
3345 Is_Unc := Returns_Unconstrained_Type (Subp)
3346 and then Optimization_Level > 0;
3347 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
3348 and then Is_Unc;
3349 end if;
3351 -- Check for an illegal attempt to inline a recursive procedure. If the
3352 -- subprogram has parameters this is detected when trying to supply a
3353 -- binding for parameters that already have one. For parameterless
3354 -- subprograms this must be done explicitly.
3356 if In_Open_Scopes (Subp) then
3357 Cannot_Inline
3358 ("cannot inline call to recursive subprogram?", N, Subp);
3359 Set_Is_Inlined (Subp, False);
3360 return;
3362 -- Skip inlining if this is not a true inlining since the attribute
3363 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3364 -- true inlining, Orig_Bod has code rather than being an entity.
3366 elsif Nkind (Orig_Bod) in N_Entity then
3367 return;
3368 end if;
3370 if Nkind (Orig_Bod) = N_Defining_Identifier
3371 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3372 then
3373 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3374 -- can be replaced with calls to the renamed entity directly, because
3375 -- the subprograms are subtype conformant. If the renamed subprogram
3376 -- is an inherited operation, we must redo the expansion because
3377 -- implicit conversions may be needed. Similarly, if the renamed
3378 -- entity is inlined, expand the call for further optimizations.
3380 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3382 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3383 Expand_Call (N);
3384 end if;
3386 return;
3387 end if;
3389 -- Register the call in the list of inlined calls
3391 Append_New_Elmt (N, To => Inlined_Calls);
3393 -- Use generic machinery to copy body of inlined subprogram, as if it
3394 -- were an instantiation, resetting source locations appropriately, so
3395 -- that nested inlined calls appear in the main unit.
3397 Save_Env (Subp, Empty);
3398 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3400 -- Old semantics
3402 if not Uses_Back_End then
3403 declare
3404 Bod : Node_Id;
3406 begin
3407 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3408 Blk :=
3409 Make_Block_Statement (Loc,
3410 Declarations => Declarations (Bod),
3411 Handled_Statement_Sequence =>
3412 Handled_Statement_Sequence (Bod));
3414 if No (Declarations (Bod)) then
3415 Set_Declarations (Blk, New_List);
3416 end if;
3418 -- When generating C code, declare _Result, which may be used to
3419 -- verify the return value.
3421 if Modify_Tree_For_C
3422 and then Nkind (N) = N_Procedure_Call_Statement
3423 and then Chars (Name (N)) = Name_uPostconditions
3424 then
3425 Declare_Postconditions_Result;
3426 end if;
3428 -- For the unconstrained case, capture the name of the local
3429 -- variable that holds the result. This must be the first
3430 -- declaration in the block, because its bounds cannot depend
3431 -- on local variables. Otherwise there is no way to declare the
3432 -- result outside of the block. Needless to say, in general the
3433 -- bounds will depend on the actuals in the call.
3435 -- If the context is an assignment statement, as is the case
3436 -- for the expansion of an extended return, the left-hand side
3437 -- provides bounds even if the return type is unconstrained.
3439 if Is_Unc then
3440 declare
3441 First_Decl : Node_Id;
3443 begin
3444 First_Decl := First (Declarations (Blk));
3446 -- If the body is a single extended return statement,the
3447 -- resulting block is a nested block.
3449 if No (First_Decl) then
3450 First_Decl :=
3451 First (Statements (Handled_Statement_Sequence (Blk)));
3453 if Nkind (First_Decl) = N_Block_Statement then
3454 First_Decl := First (Declarations (First_Decl));
3455 end if;
3456 end if;
3458 -- No front-end inlining possible
3460 if Nkind (First_Decl) /= N_Object_Declaration then
3461 return;
3462 end if;
3464 if Nkind (Parent (N)) /= N_Assignment_Statement then
3465 Targ1 := Defining_Identifier (First_Decl);
3466 else
3467 Targ1 := Name (Parent (N));
3468 end if;
3469 end;
3470 end if;
3471 end;
3473 -- New semantics
3475 else
3476 declare
3477 Bod : Node_Id;
3479 begin
3480 -- General case
3482 if not Is_Unc then
3483 Bod :=
3484 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3485 Blk :=
3486 Make_Block_Statement (Loc,
3487 Declarations => Declarations (Bod),
3488 Handled_Statement_Sequence =>
3489 Handled_Statement_Sequence (Bod));
3491 -- Inline a call to a function that returns an unconstrained type.
3492 -- The semantic analyzer checked that frontend-inlined functions
3493 -- returning unconstrained types have no declarations and have
3494 -- a single extended return statement. As part of its processing
3495 -- the function was split into two subprograms: a procedure P' and
3496 -- a function F' that has a block with a call to procedure P' (see
3497 -- Split_Unconstrained_Function).
3499 else
3500 pragma Assert
3501 (Nkind
3502 (First
3503 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3504 N_Block_Statement);
3506 declare
3507 Blk_Stmt : constant Node_Id :=
3508 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
3509 First_Stmt : constant Node_Id :=
3510 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
3511 Second_Stmt : constant Node_Id := Next (First_Stmt);
3513 begin
3514 pragma Assert
3515 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3516 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3517 and then No (Next (Second_Stmt)));
3519 Bod :=
3520 Copy_Generic_Node
3521 (First
3522 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3523 Empty, Instantiating => True);
3524 Blk := Bod;
3526 -- Capture the name of the local variable that holds the
3527 -- result. This must be the first declaration in the block,
3528 -- because its bounds cannot depend on local variables.
3529 -- Otherwise there is no way to declare the result outside
3530 -- of the block. Needless to say, in general the bounds will
3531 -- depend on the actuals in the call.
3533 if Nkind (Parent (N)) /= N_Assignment_Statement then
3534 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3536 -- If the context is an assignment statement, as is the case
3537 -- for the expansion of an extended return, the left-hand
3538 -- side provides bounds even if the return type is
3539 -- unconstrained.
3541 else
3542 Targ1 := Name (Parent (N));
3543 end if;
3544 end;
3545 end if;
3547 if No (Declarations (Bod)) then
3548 Set_Declarations (Blk, New_List);
3549 end if;
3550 end;
3551 end if;
3553 -- If this is a derived function, establish the proper return type
3555 if Present (Orig_Subp) and then Orig_Subp /= Subp then
3556 Ret_Type := Etype (Orig_Subp);
3557 else
3558 Ret_Type := Etype (Subp);
3559 end if;
3561 -- Create temporaries for the actuals that are expressions, or that are
3562 -- scalars and require copying to preserve semantics.
3564 F := First_Formal (Subp);
3565 A := First_Actual (N);
3566 while Present (F) loop
3567 if Present (Renamed_Object (F)) then
3569 -- If expander is active, it is an error to try to inline a
3570 -- recursive program. In GNATprove mode, just indicate that the
3571 -- inlining will not happen, and mark the subprogram as not always
3572 -- inlined.
3574 if GNATprove_Mode then
3575 Cannot_Inline
3576 ("cannot inline call to recursive subprogram?", N, Subp);
3577 Set_Is_Inlined_Always (Subp, False);
3578 else
3579 Error_Msg_N
3580 ("cannot inline call to recursive subprogram", N);
3581 end if;
3583 return;
3584 end if;
3586 -- Reset Last_Assignment for any parameters of mode out or in out, to
3587 -- prevent spurious warnings about overwriting for assignments to the
3588 -- formal in the inlined code.
3590 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
3591 Set_Last_Assignment (Entity (A), Empty);
3592 end if;
3594 -- If the argument may be a controlling argument in a call within
3595 -- the inlined body, we must preserve its classwide nature to insure
3596 -- that dynamic dispatching take place subsequently. If the formal
3597 -- has a constraint it must be preserved to retain the semantics of
3598 -- the body.
3600 if Is_Class_Wide_Type (Etype (F))
3601 or else (Is_Access_Type (Etype (F))
3602 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3603 then
3604 Temp_Typ := Etype (F);
3606 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3607 and then Etype (F) /= Base_Type (Etype (F))
3608 and then Is_Constrained (Etype (F))
3609 then
3610 Temp_Typ := Etype (F);
3612 else
3613 Temp_Typ := Etype (A);
3614 end if;
3616 -- If the actual is a simple name or a literal, no need to
3617 -- create a temporary, object can be used directly.
3619 -- If the actual is a literal and the formal has its address taken,
3620 -- we cannot pass the literal itself as an argument, so its value
3621 -- must be captured in a temporary. Skip this optimization in
3622 -- GNATprove mode, to make sure any check on a type conversion
3623 -- will be issued.
3625 if (Is_Entity_Name (A)
3626 and then
3627 (not Is_Scalar_Type (Etype (A))
3628 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3629 and then not GNATprove_Mode)
3631 -- When the actual is an identifier and the corresponding formal is
3632 -- used only once in the original body, the formal can be substituted
3633 -- directly with the actual parameter. Skip this optimization in
3634 -- GNATprove mode, to make sure any check on a type conversion
3635 -- will be issued.
3637 or else
3638 (Nkind (A) = N_Identifier
3639 and then Formal_Is_Used_Once (F)
3640 and then not GNATprove_Mode)
3642 or else
3643 (Nkind_In (A, N_Real_Literal,
3644 N_Integer_Literal,
3645 N_Character_Literal)
3646 and then not Address_Taken (F))
3647 then
3648 if Etype (F) /= Etype (A) then
3649 Set_Renamed_Object
3650 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3651 else
3652 Set_Renamed_Object (F, A);
3653 end if;
3655 else
3656 Temp := Make_Temporary (Loc, 'C');
3658 -- If the actual for an in/in-out parameter is a view conversion,
3659 -- make it into an unchecked conversion, given that an untagged
3660 -- type conversion is not a proper object for a renaming.
3662 -- In-out conversions that involve real conversions have already
3663 -- been transformed in Expand_Actuals.
3665 if Nkind (A) = N_Type_Conversion
3666 and then Ekind (F) /= E_In_Parameter
3667 then
3668 New_A :=
3669 Make_Unchecked_Type_Conversion (Loc,
3670 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3671 Expression => Relocate_Node (Expression (A)));
3673 -- In GNATprove mode, keep the most precise type of the actual for
3674 -- the temporary variable, when the formal type is unconstrained.
3675 -- Otherwise, the AST may contain unexpected assignment statements
3676 -- to a temporary variable of unconstrained type renaming a local
3677 -- variable of constrained type, which is not expected by
3678 -- GNATprove.
3680 elsif Etype (F) /= Etype (A)
3681 and then (not GNATprove_Mode or else Is_Constrained (Etype (F)))
3682 then
3683 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3684 Temp_Typ := Etype (F);
3686 else
3687 New_A := Relocate_Node (A);
3688 end if;
3690 Set_Sloc (New_A, Sloc (N));
3692 -- If the actual has a by-reference type, it cannot be copied,
3693 -- so its value is captured in a renaming declaration. Otherwise
3694 -- declare a local constant initialized with the actual.
3696 -- We also use a renaming declaration for expressions of an array
3697 -- type that is not bit-packed, both for efficiency reasons and to
3698 -- respect the semantics of the call: in most cases the original
3699 -- call will pass the parameter by reference, and thus the inlined
3700 -- code will have the same semantics.
3702 -- Finally, we need a renaming declaration in the case of limited
3703 -- types for which initialization cannot be by copy either.
3705 if Ekind (F) = E_In_Parameter
3706 and then not Is_By_Reference_Type (Etype (A))
3707 and then not Is_Limited_Type (Etype (A))
3708 and then
3709 (not Is_Array_Type (Etype (A))
3710 or else not Is_Object_Reference (A)
3711 or else Is_Bit_Packed_Array (Etype (A)))
3712 then
3713 Decl :=
3714 Make_Object_Declaration (Loc,
3715 Defining_Identifier => Temp,
3716 Constant_Present => True,
3717 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3718 Expression => New_A);
3720 else
3721 -- In GNATprove mode, make an explicit copy of input
3722 -- parameters when formal and actual types differ, to make
3723 -- sure any check on the type conversion will be issued.
3724 -- The legality of the copy is ensured by calling first
3725 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3727 if GNATprove_Mode
3728 and then Ekind (F) /= E_Out_Parameter
3729 and then not Same_Type (Etype (F), Etype (A))
3730 then
3731 pragma Assert (not Is_By_Reference_Type (Etype (A)));
3732 pragma Assert (not Is_Limited_Type (Etype (A)));
3734 Append_To (Decls,
3735 Make_Object_Declaration (Loc,
3736 Defining_Identifier => Make_Temporary (Loc, 'C'),
3737 Constant_Present => True,
3738 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3739 Expression => New_Copy_Tree (New_A)));
3740 end if;
3742 Decl :=
3743 Make_Object_Renaming_Declaration (Loc,
3744 Defining_Identifier => Temp,
3745 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3746 Name => New_A);
3747 end if;
3749 Append (Decl, Decls);
3750 Set_Renamed_Object (F, Temp);
3751 end if;
3753 Next_Formal (F);
3754 Next_Actual (A);
3755 end loop;
3757 -- Establish target of function call. If context is not assignment or
3758 -- declaration, create a temporary as a target. The declaration for the
3759 -- temporary may be subsequently optimized away if the body is a single
3760 -- expression, or if the left-hand side of the assignment is simple
3761 -- enough, i.e. an entity or an explicit dereference of one.
3763 if Ekind (Subp) = E_Function then
3764 if Nkind (Parent (N)) = N_Assignment_Statement
3765 and then Is_Entity_Name (Name (Parent (N)))
3766 then
3767 Targ := Name (Parent (N));
3769 elsif Nkind (Parent (N)) = N_Assignment_Statement
3770 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3771 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3772 then
3773 Targ := Name (Parent (N));
3775 elsif Nkind (Parent (N)) = N_Assignment_Statement
3776 and then Nkind (Name (Parent (N))) = N_Selected_Component
3777 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3778 then
3779 Targ := New_Copy_Tree (Name (Parent (N)));
3781 elsif Nkind (Parent (N)) = N_Object_Declaration
3782 and then Is_Limited_Type (Etype (Subp))
3783 then
3784 Targ := Defining_Identifier (Parent (N));
3786 -- New semantics: In an object declaration avoid an extra copy
3787 -- of the result of a call to an inlined function that returns
3788 -- an unconstrained type
3790 elsif Uses_Back_End
3791 and then Nkind (Parent (N)) = N_Object_Declaration
3792 and then Is_Unc
3793 then
3794 Targ := Defining_Identifier (Parent (N));
3796 else
3797 -- Replace call with temporary and create its declaration
3799 Temp := Make_Temporary (Loc, 'C');
3800 Set_Is_Internal (Temp);
3802 -- For the unconstrained case, the generated temporary has the
3803 -- same constrained declaration as the result variable. It may
3804 -- eventually be possible to remove that temporary and use the
3805 -- result variable directly.
3807 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
3808 then
3809 Decl :=
3810 Make_Object_Declaration (Loc,
3811 Defining_Identifier => Temp,
3812 Object_Definition =>
3813 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3815 Replace_Formals (Decl);
3817 else
3818 Decl :=
3819 Make_Object_Declaration (Loc,
3820 Defining_Identifier => Temp,
3821 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
3823 Set_Etype (Temp, Ret_Type);
3824 end if;
3826 Set_No_Initialization (Decl);
3827 Append (Decl, Decls);
3828 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3829 Targ := Temp;
3830 end if;
3831 end if;
3833 Insert_Actions (N, Decls);
3835 if Is_Unc_Decl then
3837 -- Special management for inlining a call to a function that returns
3838 -- an unconstrained type and initializes an object declaration: we
3839 -- avoid generating undesired extra calls and goto statements.
3841 -- Given:
3842 -- function Func (...) return String is
3843 -- begin
3844 -- declare
3845 -- Result : String (1 .. 4);
3846 -- begin
3847 -- Proc (Result, ...);
3848 -- return Result;
3849 -- end;
3850 -- end Func;
3852 -- Result : String := Func (...);
3854 -- Replace this object declaration by:
3856 -- Result : String (1 .. 4);
3857 -- Proc (Result, ...);
3859 Remove_Homonym (Targ);
3861 Decl :=
3862 Make_Object_Declaration
3863 (Loc,
3864 Defining_Identifier => Targ,
3865 Object_Definition =>
3866 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3867 Replace_Formals (Decl);
3868 Rewrite (Parent (N), Decl);
3869 Analyze (Parent (N));
3871 -- Avoid spurious warnings since we know that this declaration is
3872 -- referenced by the procedure call.
3874 Set_Never_Set_In_Source (Targ, False);
3876 -- Remove the local declaration of the extended return stmt from the
3877 -- inlined code
3879 Remove (Parent (Targ1));
3881 -- Update the reference to the result (since we have rewriten the
3882 -- object declaration)
3884 declare
3885 Blk_Call_Stmt : Node_Id;
3887 begin
3888 -- Capture the call to the procedure
3890 Blk_Call_Stmt :=
3891 First (Statements (Handled_Statement_Sequence (Blk)));
3892 pragma Assert
3893 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
3895 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
3896 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
3897 New_Occurrence_Of (Targ, Loc));
3898 end;
3900 -- Remove the return statement
3902 pragma Assert
3903 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3904 N_Simple_Return_Statement);
3906 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3907 end if;
3909 -- Traverse the tree and replace formals with actuals or their thunks.
3910 -- Attach block to tree before analysis and rewriting.
3912 Replace_Formals (Blk);
3913 Replace_Formals_In_Aspects (Blk);
3914 Set_Parent (Blk, N);
3916 if GNATprove_Mode then
3917 null;
3919 elsif not Comes_From_Source (Subp) or else Is_Predef then
3920 Reset_Slocs (Blk);
3921 end if;
3923 if Is_Unc_Decl then
3925 -- No action needed since return statement has been already removed
3927 null;
3929 elsif Present (Exit_Lab) then
3931 -- If there's a single return statement at the end of the subprogram,
3932 -- the corresponding goto statement and the corresponding label are
3933 -- useless.
3935 if Num_Ret = 1
3936 and then
3937 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3938 N_Goto_Statement
3939 then
3940 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3941 else
3942 Append (Lab_Decl, (Declarations (Blk)));
3943 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3944 end if;
3945 end if;
3947 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3948 -- on conflicting private views that Gigi would ignore. If this is a
3949 -- predefined unit, analyze with checks off, as is done in the non-
3950 -- inlined run-time units.
3952 declare
3953 I_Flag : constant Boolean := In_Inlined_Body;
3955 begin
3956 In_Inlined_Body := True;
3958 if Is_Predef then
3959 declare
3960 Style : constant Boolean := Style_Check;
3962 begin
3963 Style_Check := False;
3965 -- Search for dispatching calls that use the Object.Operation
3966 -- notation using an Object that is a parameter of the inlined
3967 -- function. We reset the decoration of Operation to force
3968 -- the reanalysis of the inlined dispatching call because
3969 -- the actual object has been inlined.
3971 Reset_Dispatching_Calls (Blk);
3973 Analyze (Blk, Suppress => All_Checks);
3974 Style_Check := Style;
3975 end;
3977 else
3978 Analyze (Blk);
3979 end if;
3981 In_Inlined_Body := I_Flag;
3982 end;
3984 if Ekind (Subp) = E_Procedure then
3985 Rewrite_Procedure_Call (N, Blk);
3987 else
3988 Rewrite_Function_Call (N, Blk);
3990 if Is_Unc_Decl then
3991 null;
3993 -- For the unconstrained case, the replacement of the call has been
3994 -- made prior to the complete analysis of the generated declarations.
3995 -- Propagate the proper type now.
3997 elsif Is_Unc then
3998 if Nkind (N) = N_Identifier then
3999 Set_Etype (N, Etype (Entity (N)));
4000 else
4001 Set_Etype (N, Etype (Targ1));
4002 end if;
4003 end if;
4004 end if;
4006 Restore_Env;
4008 -- Cleanup mapping between formals and actuals for other expansions
4010 F := First_Formal (Subp);
4011 while Present (F) loop
4012 Set_Renamed_Object (F, Empty);
4013 Next_Formal (F);
4014 end loop;
4015 end Expand_Inlined_Call;
4017 --------------------------
4018 -- Get_Code_Unit_Entity --
4019 --------------------------
4021 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
4022 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
4024 begin
4025 if Ekind (Unit) = E_Package_Body then
4026 Unit := Spec_Entity (Unit);
4027 end if;
4029 return Unit;
4030 end Get_Code_Unit_Entity;
4032 ------------------------------
4033 -- Has_Excluded_Declaration --
4034 ------------------------------
4036 function Has_Excluded_Declaration
4037 (Subp : Entity_Id;
4038 Decls : List_Id) return Boolean
4040 D : Node_Id;
4042 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4043 -- Nested subprograms make a given body ineligible for inlining, but
4044 -- we make an exception for instantiations of unchecked conversion.
4045 -- The body has not been analyzed yet, so check the name, and verify
4046 -- that the visible entity with that name is the predefined unit.
4048 -----------------------------
4049 -- Is_Unchecked_Conversion --
4050 -----------------------------
4052 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4053 Id : constant Node_Id := Name (D);
4054 Conv : Entity_Id;
4056 begin
4057 if Nkind (Id) = N_Identifier
4058 and then Chars (Id) = Name_Unchecked_Conversion
4059 then
4060 Conv := Current_Entity (Id);
4062 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4063 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4064 then
4065 Conv := Current_Entity (Selector_Name (Id));
4066 else
4067 return False;
4068 end if;
4070 return Present (Conv)
4071 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
4072 and then Is_Intrinsic_Subprogram (Conv);
4073 end Is_Unchecked_Conversion;
4075 -- Start of processing for Has_Excluded_Declaration
4077 begin
4078 -- No action needed if the check is not needed
4080 if not Check_Inlining_Restrictions then
4081 return False;
4082 end if;
4084 D := First (Decls);
4085 while Present (D) loop
4087 -- First declarations universally excluded
4089 if Nkind (D) = N_Package_Declaration then
4090 Cannot_Inline
4091 ("cannot inline & (nested package declaration)?", D, Subp);
4092 return True;
4094 elsif Nkind (D) = N_Package_Instantiation then
4095 Cannot_Inline
4096 ("cannot inline & (nested package instantiation)?", D, Subp);
4097 return True;
4098 end if;
4100 -- Then declarations excluded only for front-end inlining
4102 if Back_End_Inlining then
4103 null;
4105 elsif Nkind (D) = N_Task_Type_Declaration
4106 or else Nkind (D) = N_Single_Task_Declaration
4107 then
4108 Cannot_Inline
4109 ("cannot inline & (nested task type declaration)?", D, Subp);
4110 return True;
4112 elsif Nkind (D) = N_Protected_Type_Declaration
4113 or else Nkind (D) = N_Single_Protected_Declaration
4114 then
4115 Cannot_Inline
4116 ("cannot inline & (nested protected type declaration)?",
4117 D, Subp);
4118 return True;
4120 elsif Nkind (D) = N_Subprogram_Body then
4121 Cannot_Inline
4122 ("cannot inline & (nested subprogram)?", D, Subp);
4123 return True;
4125 elsif Nkind (D) = N_Function_Instantiation
4126 and then not Is_Unchecked_Conversion (D)
4127 then
4128 Cannot_Inline
4129 ("cannot inline & (nested function instantiation)?", D, Subp);
4130 return True;
4132 elsif Nkind (D) = N_Procedure_Instantiation then
4133 Cannot_Inline
4134 ("cannot inline & (nested procedure instantiation)?", D, Subp);
4135 return True;
4137 -- Subtype declarations with predicates will generate predicate
4138 -- functions, i.e. nested subprogram bodies, so inlining is not
4139 -- possible.
4141 elsif Nkind (D) = N_Subtype_Declaration
4142 and then Present (Aspect_Specifications (D))
4143 then
4144 declare
4145 A : Node_Id;
4146 A_Id : Aspect_Id;
4148 begin
4149 A := First (Aspect_Specifications (D));
4150 while Present (A) loop
4151 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4153 if A_Id = Aspect_Predicate
4154 or else A_Id = Aspect_Static_Predicate
4155 or else A_Id = Aspect_Dynamic_Predicate
4156 then
4157 Cannot_Inline
4158 ("cannot inline & (subtype declaration with "
4159 & "predicate)?", D, Subp);
4160 return True;
4161 end if;
4163 Next (A);
4164 end loop;
4165 end;
4166 end if;
4168 Next (D);
4169 end loop;
4171 return False;
4172 end Has_Excluded_Declaration;
4174 ----------------------------
4175 -- Has_Excluded_Statement --
4176 ----------------------------
4178 function Has_Excluded_Statement
4179 (Subp : Entity_Id;
4180 Stats : List_Id) return Boolean
4182 S : Node_Id;
4183 E : Node_Id;
4185 begin
4186 -- No action needed if the check is not needed
4188 if not Check_Inlining_Restrictions then
4189 return False;
4190 end if;
4192 S := First (Stats);
4193 while Present (S) loop
4194 if Nkind_In (S, N_Abort_Statement,
4195 N_Asynchronous_Select,
4196 N_Conditional_Entry_Call,
4197 N_Delay_Relative_Statement,
4198 N_Delay_Until_Statement,
4199 N_Selective_Accept,
4200 N_Timed_Entry_Call)
4201 then
4202 Cannot_Inline
4203 ("cannot inline & (non-allowed statement)?", S, Subp);
4204 return True;
4206 elsif Nkind (S) = N_Block_Statement then
4207 if Present (Declarations (S))
4208 and then Has_Excluded_Declaration (Subp, Declarations (S))
4209 then
4210 return True;
4212 elsif Present (Handled_Statement_Sequence (S)) then
4213 if not Back_End_Inlining
4214 and then
4215 Present
4216 (Exception_Handlers (Handled_Statement_Sequence (S)))
4217 then
4218 Cannot_Inline
4219 ("cannot inline& (exception handler)?",
4220 First (Exception_Handlers
4221 (Handled_Statement_Sequence (S))),
4222 Subp);
4223 return True;
4225 elsif Has_Excluded_Statement
4226 (Subp, Statements (Handled_Statement_Sequence (S)))
4227 then
4228 return True;
4229 end if;
4230 end if;
4232 elsif Nkind (S) = N_Case_Statement then
4233 E := First (Alternatives (S));
4234 while Present (E) loop
4235 if Has_Excluded_Statement (Subp, Statements (E)) then
4236 return True;
4237 end if;
4239 Next (E);
4240 end loop;
4242 elsif Nkind (S) = N_If_Statement then
4243 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
4244 return True;
4245 end if;
4247 if Present (Elsif_Parts (S)) then
4248 E := First (Elsif_Parts (S));
4249 while Present (E) loop
4250 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
4251 return True;
4252 end if;
4254 Next (E);
4255 end loop;
4256 end if;
4258 if Present (Else_Statements (S))
4259 and then Has_Excluded_Statement (Subp, Else_Statements (S))
4260 then
4261 return True;
4262 end if;
4264 elsif Nkind (S) = N_Loop_Statement
4265 and then Has_Excluded_Statement (Subp, Statements (S))
4266 then
4267 return True;
4269 elsif Nkind (S) = N_Extended_Return_Statement then
4270 if Present (Handled_Statement_Sequence (S))
4271 and then
4272 Has_Excluded_Statement
4273 (Subp, Statements (Handled_Statement_Sequence (S)))
4274 then
4275 return True;
4277 elsif not Back_End_Inlining
4278 and then Present (Handled_Statement_Sequence (S))
4279 and then
4280 Present (Exception_Handlers
4281 (Handled_Statement_Sequence (S)))
4282 then
4283 Cannot_Inline
4284 ("cannot inline& (exception handler)?",
4285 First (Exception_Handlers (Handled_Statement_Sequence (S))),
4286 Subp);
4287 return True;
4288 end if;
4289 end if;
4291 Next (S);
4292 end loop;
4294 return False;
4295 end Has_Excluded_Statement;
4297 --------------------------
4298 -- Has_Initialized_Type --
4299 --------------------------
4301 function Has_Initialized_Type (E : Entity_Id) return Boolean is
4302 E_Body : constant Node_Id := Subprogram_Body (E);
4303 Decl : Node_Id;
4305 begin
4306 if No (E_Body) then -- imported subprogram
4307 return False;
4309 else
4310 Decl := First (Declarations (E_Body));
4311 while Present (Decl) loop
4312 if Nkind (Decl) = N_Full_Type_Declaration
4313 and then Present (Init_Proc (Defining_Identifier (Decl)))
4314 then
4315 return True;
4316 end if;
4318 Next (Decl);
4319 end loop;
4320 end if;
4322 return False;
4323 end Has_Initialized_Type;
4325 -----------------------
4326 -- Has_Single_Return --
4327 -----------------------
4329 function Has_Single_Return (N : Node_Id) return Boolean is
4330 Return_Statement : Node_Id := Empty;
4332 function Check_Return (N : Node_Id) return Traverse_Result;
4334 ------------------
4335 -- Check_Return --
4336 ------------------
4338 function Check_Return (N : Node_Id) return Traverse_Result is
4339 begin
4340 if Nkind (N) = N_Simple_Return_Statement then
4341 if Present (Expression (N))
4342 and then Is_Entity_Name (Expression (N))
4343 then
4344 pragma Assert (Present (Entity (Expression (N))));
4346 if No (Return_Statement) then
4347 Return_Statement := N;
4348 return OK;
4350 else
4351 pragma Assert
4352 (Present (Entity (Expression (Return_Statement))));
4354 if Entity (Expression (N)) =
4355 Entity (Expression (Return_Statement))
4356 then
4357 return OK;
4358 else
4359 return Abandon;
4360 end if;
4361 end if;
4363 -- A return statement within an extended return is a noop after
4364 -- inlining.
4366 elsif No (Expression (N))
4367 and then Nkind (Parent (Parent (N))) =
4368 N_Extended_Return_Statement
4369 then
4370 return OK;
4372 else
4373 -- Expression has wrong form
4375 return Abandon;
4376 end if;
4378 -- We can only inline a build-in-place function if it has a single
4379 -- extended return.
4381 elsif Nkind (N) = N_Extended_Return_Statement then
4382 if No (Return_Statement) then
4383 Return_Statement := N;
4384 return OK;
4386 else
4387 return Abandon;
4388 end if;
4390 else
4391 return OK;
4392 end if;
4393 end Check_Return;
4395 function Check_All_Returns is new Traverse_Func (Check_Return);
4397 -- Start of processing for Has_Single_Return
4399 begin
4400 if Check_All_Returns (N) /= OK then
4401 return False;
4403 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4404 return True;
4406 else
4407 return
4408 Present (Declarations (N))
4409 and then Present (First (Declarations (N)))
4410 and then Entity (Expression (Return_Statement)) =
4411 Defining_Identifier (First (Declarations (N)));
4412 end if;
4413 end Has_Single_Return;
4415 -----------------------------
4416 -- In_Main_Unit_Or_Subunit --
4417 -----------------------------
4419 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
4420 Comp : Node_Id := Cunit (Get_Code_Unit (E));
4422 begin
4423 -- Check whether the subprogram or package to inline is within the main
4424 -- unit or its spec or within a subunit. In either case there are no
4425 -- additional bodies to process. If the subprogram appears in a parent
4426 -- of the current unit, the check on whether inlining is possible is
4427 -- done in Analyze_Inlined_Bodies.
4429 while Nkind (Unit (Comp)) = N_Subunit loop
4430 Comp := Library_Unit (Comp);
4431 end loop;
4433 return Comp = Cunit (Main_Unit)
4434 or else Comp = Library_Unit (Cunit (Main_Unit));
4435 end In_Main_Unit_Or_Subunit;
4437 ----------------
4438 -- Initialize --
4439 ----------------
4441 procedure Initialize is
4442 begin
4443 Pending_Instantiations.Init;
4444 Inlined_Bodies.Init;
4445 Successors.Init;
4446 Inlined.Init;
4448 for J in Hash_Headers'Range loop
4449 Hash_Headers (J) := No_Subp;
4450 end loop;
4452 Inlined_Calls := No_Elist;
4453 Backend_Calls := No_Elist;
4454 Backend_Instances := No_Elist;
4455 Backend_Inlined_Subps := No_Elist;
4456 Backend_Not_Inlined_Subps := No_Elist;
4457 end Initialize;
4459 ------------------------
4460 -- Instantiate_Bodies --
4461 ------------------------
4463 -- Generic bodies contain all the non-local references, so an
4464 -- instantiation does not need any more context than Standard
4465 -- itself, even if the instantiation appears in an inner scope.
4466 -- Generic associations have verified that the contract model is
4467 -- satisfied, so that any error that may occur in the analysis of
4468 -- the body is an internal error.
4470 procedure Instantiate_Bodies is
4472 procedure Instantiate_Body (Info : Pending_Body_Info);
4473 -- Instantiate a pending body
4475 ------------------------
4476 -- Instantiate_Body --
4477 ------------------------
4479 procedure Instantiate_Body (Info : Pending_Body_Info) is
4480 begin
4481 -- If the instantiation node is absent, it has been removed as part
4482 -- of unreachable code.
4484 if No (Info.Inst_Node) then
4485 null;
4487 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
4488 Instantiate_Package_Body (Info);
4489 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
4491 else
4492 Instantiate_Subprogram_Body (Info);
4493 end if;
4494 end Instantiate_Body;
4496 J, K : Nat;
4497 Info : Pending_Body_Info;
4499 -- Start of processing for Instantiate_Bodies
4501 begin
4502 if Serious_Errors_Detected = 0 then
4503 Expander_Active := (Operating_Mode = Opt.Generate_Code);
4504 Push_Scope (Standard_Standard);
4505 To_Clean := New_Elmt_List;
4507 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4508 Start_Generic;
4509 end if;
4511 -- A body instantiation may generate additional instantiations, so
4512 -- the following loop must scan to the end of a possibly expanding
4513 -- set (that's why we cannot simply use a FOR loop here). We must
4514 -- also capture the element lest the set be entirely reallocated.
4516 J := 0;
4517 if Back_End_Inlining then
4518 while J <= Called_Pending_Instantiations.Last
4519 and then Serious_Errors_Detected = 0
4520 loop
4521 K := Called_Pending_Instantiations.Table (J);
4522 Info := Pending_Instantiations.Table (K);
4523 Instantiate_Body (Info);
4525 J := J + 1;
4526 end loop;
4528 else
4529 while J <= Pending_Instantiations.Last
4530 and then Serious_Errors_Detected = 0
4531 loop
4532 Info := Pending_Instantiations.Table (J);
4533 Instantiate_Body (Info);
4535 J := J + 1;
4536 end loop;
4537 end if;
4539 -- Reset the table of instantiations. Additional instantiations
4540 -- may be added through inlining, when additional bodies are
4541 -- analyzed.
4543 if Back_End_Inlining then
4544 Called_Pending_Instantiations.Init;
4545 else
4546 Pending_Instantiations.Init;
4547 end if;
4549 -- We can now complete the cleanup actions of scopes that contain
4550 -- pending instantiations (skipped for generic units, since we
4551 -- never need any cleanups in generic units).
4553 if Expander_Active
4554 and then not Is_Generic_Unit (Main_Unit_Entity)
4555 then
4556 Cleanup_Scopes;
4557 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4558 End_Generic;
4559 end if;
4561 Pop_Scope;
4562 end if;
4563 end Instantiate_Bodies;
4565 ---------------
4566 -- Is_Nested --
4567 ---------------
4569 function Is_Nested (E : Entity_Id) return Boolean is
4570 Scop : Entity_Id;
4572 begin
4573 Scop := Scope (E);
4574 while Scop /= Standard_Standard loop
4575 if Is_Subprogram (Scop) then
4576 return True;
4578 elsif Ekind (Scop) = E_Task_Type
4579 or else Ekind (Scop) = E_Entry
4580 or else Ekind (Scop) = E_Entry_Family
4581 then
4582 return True;
4583 end if;
4585 Scop := Scope (Scop);
4586 end loop;
4588 return False;
4589 end Is_Nested;
4591 ------------------------
4592 -- List_Inlining_Info --
4593 ------------------------
4595 procedure List_Inlining_Info is
4596 Elmt : Elmt_Id;
4597 Nod : Node_Id;
4598 Count : Nat;
4600 begin
4601 if not Debug_Flag_Dot_J then
4602 return;
4603 end if;
4605 -- Generate listing of calls inlined by the frontend
4607 if Present (Inlined_Calls) then
4608 Count := 0;
4609 Elmt := First_Elmt (Inlined_Calls);
4610 while Present (Elmt) loop
4611 Nod := Node (Elmt);
4613 if not In_Internal_Unit (Nod) then
4614 Count := Count + 1;
4616 if Count = 1 then
4617 Write_Str ("List of calls inlined by the frontend");
4618 Write_Eol;
4619 end if;
4621 Write_Str (" ");
4622 Write_Int (Count);
4623 Write_Str (":");
4624 Write_Location (Sloc (Nod));
4625 Write_Str (":");
4626 Output.Write_Eol;
4627 end if;
4629 Next_Elmt (Elmt);
4630 end loop;
4631 end if;
4633 -- Generate listing of calls passed to the backend
4635 if Present (Backend_Calls) then
4636 Count := 0;
4638 Elmt := First_Elmt (Backend_Calls);
4639 while Present (Elmt) loop
4640 Nod := Node (Elmt);
4642 if not In_Internal_Unit (Nod) then
4643 Count := Count + 1;
4645 if Count = 1 then
4646 Write_Str ("List of inlined calls passed to the backend");
4647 Write_Eol;
4648 end if;
4650 Write_Str (" ");
4651 Write_Int (Count);
4652 Write_Str (":");
4653 Write_Location (Sloc (Nod));
4654 Output.Write_Eol;
4655 end if;
4657 Next_Elmt (Elmt);
4658 end loop;
4659 end if;
4661 -- Generate listing of instances inlined for the backend
4663 if Present (Backend_Instances) then
4664 Count := 0;
4666 Elmt := First_Elmt (Backend_Instances);
4667 while Present (Elmt) loop
4668 Nod := Node (Elmt);
4670 if not In_Internal_Unit (Nod) then
4671 Count := Count + 1;
4673 if Count = 1 then
4674 Write_Str ("List of instances inlined for the backend");
4675 Write_Eol;
4676 end if;
4678 Write_Str (" ");
4679 Write_Int (Count);
4680 Write_Str (":");
4681 Write_Location (Sloc (Nod));
4682 Output.Write_Eol;
4683 end if;
4685 Next_Elmt (Elmt);
4686 end loop;
4687 end if;
4689 -- Generate listing of subprograms passed to the backend
4691 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
4692 Count := 0;
4694 Elmt := First_Elmt (Backend_Inlined_Subps);
4695 while Present (Elmt) loop
4696 Nod := Node (Elmt);
4698 if not In_Internal_Unit (Nod) then
4699 Count := Count + 1;
4701 if Count = 1 then
4702 Write_Str
4703 ("List of inlined subprograms passed to the backend");
4704 Write_Eol;
4705 end if;
4707 Write_Str (" ");
4708 Write_Int (Count);
4709 Write_Str (":");
4710 Write_Name (Chars (Nod));
4711 Write_Str (" (");
4712 Write_Location (Sloc (Nod));
4713 Write_Str (")");
4714 Output.Write_Eol;
4715 end if;
4717 Next_Elmt (Elmt);
4718 end loop;
4719 end if;
4721 -- Generate listing of subprograms that cannot be inlined by the backend
4723 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
4724 Count := 0;
4726 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
4727 while Present (Elmt) loop
4728 Nod := Node (Elmt);
4730 if not In_Internal_Unit (Nod) then
4731 Count := Count + 1;
4733 if Count = 1 then
4734 Write_Str
4735 ("List of subprograms that cannot be inlined by backend");
4736 Write_Eol;
4737 end if;
4739 Write_Str (" ");
4740 Write_Int (Count);
4741 Write_Str (":");
4742 Write_Name (Chars (Nod));
4743 Write_Str (" (");
4744 Write_Location (Sloc (Nod));
4745 Write_Str (")");
4746 Output.Write_Eol;
4747 end if;
4749 Next_Elmt (Elmt);
4750 end loop;
4751 end if;
4752 end List_Inlining_Info;
4754 ----------
4755 -- Lock --
4756 ----------
4758 procedure Lock is
4759 begin
4760 Pending_Instantiations.Release;
4761 Pending_Instantiations.Locked := True;
4762 Inlined_Bodies.Release;
4763 Inlined_Bodies.Locked := True;
4764 Successors.Release;
4765 Successors.Locked := True;
4766 Inlined.Release;
4767 Inlined.Locked := True;
4768 end Lock;
4770 --------------------------------
4771 -- Remove_Aspects_And_Pragmas --
4772 --------------------------------
4774 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
4775 procedure Remove_Items (List : List_Id);
4776 -- Remove all useless aspects/pragmas from a particular list
4778 ------------------
4779 -- Remove_Items --
4780 ------------------
4782 procedure Remove_Items (List : List_Id) is
4783 Item : Node_Id;
4784 Item_Id : Node_Id;
4785 Next_Item : Node_Id;
4787 begin
4788 -- Traverse the list looking for an aspect specification or a pragma
4790 Item := First (List);
4791 while Present (Item) loop
4792 Next_Item := Next (Item);
4794 if Nkind (Item) = N_Aspect_Specification then
4795 Item_Id := Identifier (Item);
4796 elsif Nkind (Item) = N_Pragma then
4797 Item_Id := Pragma_Identifier (Item);
4798 else
4799 Item_Id := Empty;
4800 end if;
4802 if Present (Item_Id)
4803 and then Nam_In (Chars (Item_Id), Name_Contract_Cases,
4804 Name_Global,
4805 Name_Depends,
4806 Name_Postcondition,
4807 Name_Precondition,
4808 Name_Refined_Global,
4809 Name_Refined_Depends,
4810 Name_Refined_Post,
4811 Name_Test_Case,
4812 Name_Unmodified,
4813 Name_Unreferenced,
4814 Name_Unused)
4815 then
4816 Remove (Item);
4817 end if;
4819 Item := Next_Item;
4820 end loop;
4821 end Remove_Items;
4823 -- Start of processing for Remove_Aspects_And_Pragmas
4825 begin
4826 Remove_Items (Aspect_Specifications (Body_Decl));
4827 Remove_Items (Declarations (Body_Decl));
4829 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
4830 -- in the body of the subprogram.
4832 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
4833 end Remove_Aspects_And_Pragmas;
4835 --------------------------
4836 -- Remove_Dead_Instance --
4837 --------------------------
4839 procedure Remove_Dead_Instance (N : Node_Id) is
4840 J : Int;
4842 begin
4843 J := 0;
4844 while J <= Pending_Instantiations.Last loop
4845 if Pending_Instantiations.Table (J).Inst_Node = N then
4846 Pending_Instantiations.Table (J).Inst_Node := Empty;
4847 return;
4848 end if;
4850 J := J + 1;
4851 end loop;
4852 end Remove_Dead_Instance;
4854 end Inline;