Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / inline.adb
blobc3911cf70e862e02c63a7592946226d918a08cc3
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-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Alloc;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Fname.UF; use Fname.UF;
41 with Lib; use Lib;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
45 with Output; use Output;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Ch10; use Sem_Ch10;
49 with Sem_Ch12; use Sem_Ch12;
50 with Sem_Prag; use Sem_Prag;
51 with Sem_Res; use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Sinfo.Nodes; use Sinfo.Nodes;
55 with Sinfo.Utils; use Sinfo.Utils;
56 with Sinput; use Sinput;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Table;
60 with Tbuild; use Tbuild;
61 with Uintp; use Uintp;
62 with Uname; use Uname;
64 with GNAT.HTable;
66 package body Inline is
68 Check_Inlining_Restrictions : constant Boolean := True;
69 -- In the following cases the frontend rejects inlining because they
70 -- are not handled well by the backend. This variable facilitates
71 -- disabling these restrictions to evaluate future versions of the
72 -- GCC backend in which some of the restrictions may be supported.
74 -- - subprograms that have:
75 -- - nested subprograms
76 -- - instantiations
77 -- - package declarations
78 -- - task or protected object declarations
79 -- - some of the following statements:
80 -- - abort
81 -- - asynchronous-select
82 -- - conditional-entry-call
83 -- - delay-relative
84 -- - delay-until
85 -- - selective-accept
86 -- - timed-entry-call
88 Inlined_Calls : Elist_Id;
89 -- List of frontend inlined calls
91 Backend_Calls : Elist_Id;
92 -- List of inline calls passed to the backend
94 Backend_Instances : Elist_Id;
95 -- List of instances inlined for the backend
97 Backend_Inlined_Subps : Elist_Id;
98 -- List of subprograms inlined by the backend
100 Backend_Not_Inlined_Subps : Elist_Id;
101 -- List of subprograms that cannot be inlined by the backend
103 -----------------------------
104 -- Pending_Instantiations --
105 -----------------------------
107 -- We make entries in this table for the pending instantiations of generic
108 -- bodies that are created during semantic analysis. After the analysis is
109 -- complete, calling Instantiate_Bodies performs the actual instantiations.
111 package Pending_Instantiations is new Table.Table (
112 Table_Component_Type => Pending_Body_Info,
113 Table_Index_Type => Int,
114 Table_Low_Bound => 0,
115 Table_Initial => Alloc.Pending_Instantiations_Initial,
116 Table_Increment => Alloc.Pending_Instantiations_Increment,
117 Table_Name => "Pending_Instantiations");
119 -------------------------------------
120 -- Called_Pending_Instantiations --
121 -------------------------------------
123 -- With back-end inlining, the pending instantiations that are not in the
124 -- main unit or subunit are performed only after a call to the subprogram
125 -- instance, or to a subprogram within the package instance, is inlined.
126 -- Since such a call can be within a subsequent pending instantiation,
127 -- we make entries in this table that stores the index of these "called"
128 -- pending instantiations and perform them when the table is populated.
130 package Called_Pending_Instantiations is new Table.Table (
131 Table_Component_Type => Int,
132 Table_Index_Type => Int,
133 Table_Low_Bound => 0,
134 Table_Initial => Alloc.Pending_Instantiations_Initial,
135 Table_Increment => Alloc.Pending_Instantiations_Increment,
136 Table_Name => "Called_Pending_Instantiations");
138 ---------------------------------
139 -- To_Pending_Instantiations --
140 ---------------------------------
142 -- With back-end inlining, we also need to have a map from the pending
143 -- instantiations to their index in the Pending_Instantiations table.
145 Node_Table_Size : constant := 257;
146 -- Number of headers in hash table
148 subtype Node_Header_Num is Integer range 0 .. Node_Table_Size - 1;
149 -- Range of headers in hash table
151 function Node_Hash (Id : Node_Id) return Node_Header_Num;
152 -- Simple hash function for Node_Ids
154 package To_Pending_Instantiations is new GNAT.Htable.Simple_HTable
155 (Header_Num => Node_Header_Num,
156 Element => Int,
157 No_Element => -1,
158 Key => Node_Id,
159 Hash => Node_Hash,
160 Equal => "=");
162 -----------------
163 -- Node_Hash --
164 -----------------
166 function Node_Hash (Id : Node_Id) return Node_Header_Num is
167 begin
168 return Node_Header_Num (Id mod Node_Table_Size);
169 end Node_Hash;
171 --------------------
172 -- Inlined Bodies --
173 --------------------
175 -- Inlined functions are actually placed in line by the backend if the
176 -- corresponding bodies are available (i.e. compiled). Whenever we find
177 -- a call to an inlined subprogram, we add the name of the enclosing
178 -- compilation unit to a worklist. After all compilation, and after
179 -- expansion of generic bodies, we traverse the list of pending bodies
180 -- and compile them as well.
182 package Inlined_Bodies is new Table.Table (
183 Table_Component_Type => Entity_Id,
184 Table_Index_Type => Int,
185 Table_Low_Bound => 0,
186 Table_Initial => Alloc.Inlined_Bodies_Initial,
187 Table_Increment => Alloc.Inlined_Bodies_Increment,
188 Table_Name => "Inlined_Bodies");
190 -----------------------
191 -- Inline Processing --
192 -----------------------
194 -- For each call to an inlined subprogram, we make entries in a table
195 -- that stores caller and callee, and indicates the call direction from
196 -- one to the other. We also record the compilation unit that contains
197 -- the callee. After analyzing the bodies of all such compilation units,
198 -- we compute the transitive closure of inlined subprograms called from
199 -- the main compilation unit and make it available to the code generator
200 -- in no particular order, thus allowing cycles in the call graph.
202 Last_Inlined : Entity_Id := Empty;
204 -- For each entry in the table we keep a list of successors in topological
205 -- order, i.e. callers of the current subprogram.
207 type Subp_Index is new Nat;
208 No_Subp : constant Subp_Index := 0;
210 -- The subprogram entities are hashed into the Inlined table
212 Num_Hash_Headers : constant := 512;
214 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
215 of Subp_Index;
217 type Succ_Index is new Nat;
218 No_Succ : constant Succ_Index := 0;
220 type Succ_Info is record
221 Subp : Subp_Index;
222 Next : Succ_Index;
223 end record;
225 -- The following table stores list elements for the successor lists. These
226 -- lists cannot be chained directly through entries in the Inlined table,
227 -- because a given subprogram can appear in several such lists.
229 package Successors is new Table.Table (
230 Table_Component_Type => Succ_Info,
231 Table_Index_Type => Succ_Index,
232 Table_Low_Bound => 1,
233 Table_Initial => Alloc.Successors_Initial,
234 Table_Increment => Alloc.Successors_Increment,
235 Table_Name => "Successors");
237 type Subp_Info is record
238 Name : Entity_Id := Empty;
239 Next : Subp_Index := No_Subp;
240 First_Succ : Succ_Index := No_Succ;
241 Main_Call : Boolean := False;
242 Processed : Boolean := False;
243 end record;
245 package Inlined is new Table.Table (
246 Table_Component_Type => Subp_Info,
247 Table_Index_Type => Subp_Index,
248 Table_Low_Bound => 1,
249 Table_Initial => Alloc.Inlined_Initial,
250 Table_Increment => Alloc.Inlined_Increment,
251 Table_Name => "Inlined");
253 -----------------------
254 -- Local Subprograms --
255 -----------------------
257 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
258 -- Make two entries in Inlined table, for an inlined subprogram being
259 -- called, and for the inlined subprogram that contains the call. If
260 -- the call is in the main compilation unit, Caller is Empty.
262 procedure Add_Inlined_Instance (E : Entity_Id);
263 -- Add instance E to the list of inlined instances for the unit
265 procedure Add_Inlined_Subprogram (E : Entity_Id);
266 -- Add subprogram E to the list of inlined subprograms for the unit
268 function Add_Subp (E : Entity_Id) return Subp_Index;
269 -- Make entry in Inlined table for subprogram E, or return table index
270 -- that already holds E.
272 procedure Establish_Actual_Mapping_For_Inlined_Call
273 (N : Node_Id;
274 Subp : Entity_Id;
275 Decls : List_Id;
276 Body_Or_Expr_To_Check : Node_Id);
277 -- Establish a mapping from formals to actuals in the call N for the target
278 -- subprogram Subp, and create temporaries or renamings when needed for the
279 -- actuals that are expressions (except for actuals given by simple entity
280 -- names or literals) or that are scalars that require copying to preserve
281 -- semantics. Any temporary objects that are created are inserted in Decls.
282 -- Body_Or_Expr_To_Check indicates the target body (or possibly expression
283 -- of an expression function), which may be traversed to count formal uses.
285 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id;
286 pragma Inline (Get_Code_Unit_Entity);
287 -- Return the entity node for the unit containing E. Always return the spec
288 -- for a package.
290 function Has_Initialized_Type (E : Entity_Id) return Boolean;
291 -- If a candidate for inlining contains type declarations for types with
292 -- nontrivial initialization procedures, they are not worth inlining.
294 function Has_Single_Return (N : Node_Id) return Boolean;
295 -- In general we cannot inline functions that return unconstrained type.
296 -- However, we can handle such functions if all return statements return
297 -- a local variable that is the first declaration in the body of the
298 -- function. In that case the call can be replaced by that local
299 -- variable as is done for other inlined calls.
301 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean;
302 -- Return True if E is in the main unit or its spec or in a subunit
304 function Is_Nested (E : Entity_Id) return Boolean;
305 -- If the function is nested inside some other function, it will always
306 -- be compiled if that function is, so don't add it to the inline list.
307 -- We cannot compile a nested function outside the scope of the containing
308 -- function anyway. This is also the case if the function is defined in a
309 -- task body or within an entry (for example, an initialization procedure).
311 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id);
312 -- Remove all aspects and/or pragmas that have no meaning in inlined body
313 -- Body_Decl. The analysis of these items is performed on the non-inlined
314 -- body. The items currently removed are:
315 -- Contract_Cases
316 -- Global
317 -- Depends
318 -- Postcondition
319 -- Precondition
320 -- Refined_Global
321 -- Refined_Depends
322 -- Refined_Post
323 -- Subprogram_Variant
324 -- Test_Case
325 -- Unmodified
326 -- Unreferenced
328 procedure Reset_Actual_Mapping_For_Inlined_Call (Subp : Entity_Id);
329 -- Reset the Renamed_Object field to Empty on all formals of Subp, which
330 -- can be set by a call to Establish_Actual_Mapping_For_Inlined_Call.
332 ------------------------------
333 -- Deferred Cleanup Actions --
334 ------------------------------
336 -- The cleanup actions for scopes that contain instantiations is delayed
337 -- until after expansion of those instantiations, because they may contain
338 -- finalizable objects or tasks that affect the cleanup code. A scope
339 -- that contains instantiations only needs to be finalized once, even
340 -- if it contains more than one instance. We keep a list of scopes
341 -- that must still be finalized, and call cleanup_actions after all
342 -- the instantiations have been completed.
344 To_Clean : Elist_Id;
346 procedure Add_Scope_To_Clean (Inst : Entity_Id);
347 -- Build set of scopes on which cleanup actions must be performed
349 procedure Cleanup_Scopes;
350 -- Complete cleanup actions on scopes that need it
352 --------------
353 -- Add_Call --
354 --------------
356 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
357 P1 : constant Subp_Index := Add_Subp (Called);
358 P2 : Subp_Index;
359 J : Succ_Index;
361 begin
362 if Present (Caller) then
363 P2 := Add_Subp (Caller);
365 -- Add P1 to the list of successors of P2, if not already there.
366 -- Note that P2 may contain more than one call to P1, and only
367 -- one needs to be recorded.
369 J := Inlined.Table (P2).First_Succ;
370 while J /= No_Succ loop
371 if Successors.Table (J).Subp = P1 then
372 return;
373 end if;
375 J := Successors.Table (J).Next;
376 end loop;
378 -- On exit, make a successor entry for P1
380 Successors.Increment_Last;
381 Successors.Table (Successors.Last).Subp := P1;
382 Successors.Table (Successors.Last).Next :=
383 Inlined.Table (P2).First_Succ;
384 Inlined.Table (P2).First_Succ := Successors.Last;
385 else
386 Inlined.Table (P1).Main_Call := True;
387 end if;
388 end Add_Call;
390 ----------------------
391 -- Add_Inlined_Body --
392 ----------------------
394 procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id) is
396 type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package);
397 -- Level of inlining for the call: Dont_Inline means no inlining,
398 -- Inline_Call means that only the call is considered for inlining,
399 -- Inline_Package means that the call is considered for inlining and
400 -- its package compiled and scanned for more inlining opportunities.
402 function Is_Non_Loading_Expression_Function
403 (Id : Entity_Id) return Boolean;
404 -- Determine whether arbitrary entity Id denotes a subprogram which is
405 -- either
407 -- * An expression function
409 -- * A function completed by an expression function where both the
410 -- spec and body are in the same context.
412 function Must_Inline return Inline_Level_Type;
413 -- Inlining is only done if the call statement N is in the main unit,
414 -- or within the body of another inlined subprogram.
416 ----------------------------------------
417 -- Is_Non_Loading_Expression_Function --
418 ----------------------------------------
420 function Is_Non_Loading_Expression_Function
421 (Id : Entity_Id) return Boolean
423 Body_Decl : Node_Id;
424 Body_Id : Entity_Id;
425 Spec_Decl : Node_Id;
427 begin
428 -- A stand-alone expression function is transformed into a spec-body
429 -- pair in-place. Since both the spec and body are in the same list,
430 -- the inlining of such an expression function does not need to load
431 -- anything extra.
433 if Is_Expression_Function (Id) then
434 return True;
436 -- A function may be completed by an expression function
438 elsif Ekind (Id) = E_Function then
439 Spec_Decl := Unit_Declaration_Node (Id);
441 if Nkind (Spec_Decl) = N_Subprogram_Declaration then
442 Body_Id := Corresponding_Body (Spec_Decl);
444 if Present (Body_Id) then
445 Body_Decl := Unit_Declaration_Node (Body_Id);
447 -- The inlining of a completing expression function does
448 -- not need to load anything extra when both the spec and
449 -- body are in the same context.
451 return
452 Was_Expression_Function (Body_Decl)
453 and then Parent (Spec_Decl) = Parent (Body_Decl);
454 end if;
455 end if;
456 end if;
458 return False;
459 end Is_Non_Loading_Expression_Function;
461 -----------------
462 -- Must_Inline --
463 -----------------
465 function Must_Inline return Inline_Level_Type is
466 Scop : Entity_Id;
467 Comp : Node_Id;
469 begin
470 -- Check if call is in main unit
472 Scop := Current_Scope;
474 -- Do not try to inline if scope is standard. This could happen, for
475 -- example, for a call to Add_Global_Declaration, and it causes
476 -- trouble to try to inline at this level.
478 if Scop = Standard_Standard then
479 return Dont_Inline;
480 end if;
482 -- Otherwise lookup scope stack to outer scope
484 while Scope (Scop) /= Standard_Standard
485 and then not Is_Child_Unit (Scop)
486 loop
487 Scop := Scope (Scop);
488 end loop;
490 Comp := Parent (Scop);
491 while Nkind (Comp) /= N_Compilation_Unit loop
492 Comp := Parent (Comp);
493 end loop;
495 -- If the call is in the main unit, inline the call and compile the
496 -- package of the subprogram to find more calls to be inlined.
498 if Comp = Cunit (Main_Unit)
499 or else Comp = Library_Unit (Cunit (Main_Unit))
500 then
501 Add_Call (E);
502 return Inline_Package;
503 end if;
505 -- The call is not in the main unit. See if it is in some subprogram
506 -- that can be inlined outside its unit. If so, inline the call and,
507 -- if the inlining level is set to 1, stop there; otherwise also
508 -- compile the package as above.
510 Scop := Current_Scope;
511 while Scope (Scop) /= Standard_Standard
512 and then not Is_Child_Unit (Scop)
513 loop
514 if Is_Overloadable (Scop)
515 and then Is_Inlined (Scop)
516 and then not Is_Nested (Scop)
517 then
518 Add_Call (E, Scop);
520 if Inline_Level = 1 then
521 return Inline_Call;
522 else
523 return Inline_Package;
524 end if;
525 end if;
527 Scop := Scope (Scop);
528 end loop;
530 return Dont_Inline;
531 end Must_Inline;
533 Inst : Entity_Id;
534 Inst_Decl : Node_Id;
535 Level : Inline_Level_Type;
537 -- Start of processing for Add_Inlined_Body
539 begin
540 Append_New_Elmt (N, To => Backend_Calls);
542 -- Skip subprograms that cannot or need not be inlined outside their
543 -- unit or parent subprogram.
545 if Is_Abstract_Subprogram (E)
546 or else Convention (E) = Convention_Protected
547 or else In_Main_Unit_Or_Subunit (E)
548 or else Is_Nested (E)
549 then
550 return;
551 end if;
553 -- Find out whether the call must be inlined. Unless the result is
554 -- Dont_Inline, Must_Inline also creates an edge for the call in the
555 -- callgraph; however, it will not be activated until after Is_Called
556 -- is set on the subprogram.
558 Level := Must_Inline;
560 if Level = Dont_Inline then
561 return;
562 end if;
564 -- If a previous call to the subprogram has been inlined, nothing to do
566 if Is_Called (E) then
567 return;
568 end if;
570 -- If the subprogram is an instance, then inline the instance
572 if Is_Generic_Instance (E) then
573 Add_Inlined_Instance (E);
574 end if;
576 -- Mark the subprogram as called
578 Set_Is_Called (E);
580 -- If the call was generated by the compiler and is to a subprogram in
581 -- a run-time unit, we need to suppress debugging information for it,
582 -- so that the code that is eventually inlined will not affect the
583 -- debugging of the program. We do not do it if the call comes from
584 -- source because, even if the call is inlined, the user may expect it
585 -- to be present in the debugging information.
587 if not Comes_From_Source (N)
588 and then In_Extended_Main_Source_Unit (N)
589 and then Is_Predefined_Unit (Get_Source_Unit (E))
590 then
591 Set_Needs_Debug_Info (E, False);
592 end if;
594 -- If the subprogram is an expression function, or is completed by one
595 -- where both the spec and body are in the same context, then there is
596 -- no need to load any package body since the body of the function is
597 -- in the spec.
599 if Is_Non_Loading_Expression_Function (E) then
600 return;
601 end if;
603 -- Find unit containing E, and add to list of inlined bodies if needed.
604 -- Library-level functions must be handled specially, because there is
605 -- no enclosing package to retrieve. In this case, it is the body of
606 -- the function that will have to be loaded.
608 declare
609 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
611 begin
612 if Pack = E then
613 Inlined_Bodies.Increment_Last;
614 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
616 else
617 pragma Assert (Ekind (Pack) = E_Package);
619 -- If the subprogram is within an instance, inline the instance
621 if Comes_From_Source (E) then
622 Inst := Scope (E);
624 while Present (Inst) and then Inst /= Standard_Standard loop
625 exit when Is_Generic_Instance (Inst);
626 Inst := Scope (Inst);
627 end loop;
629 if Present (Inst)
630 and then Is_Generic_Instance (Inst)
631 and then not Is_Called (Inst)
632 then
633 Inst_Decl := Unit_Declaration_Node (Inst);
635 -- Do not inline the instance if the body already exists,
636 -- or the instance node is simply missing.
638 if Present (Corresponding_Body (Inst_Decl))
639 or else (Nkind (Parent (Inst_Decl)) /= N_Compilation_Unit
640 and then No (Next (Inst_Decl)))
641 then
642 Set_Is_Called (Inst);
643 else
644 Add_Inlined_Instance (Inst);
645 end if;
646 end if;
647 end if;
649 -- If the unit containing E is an instance, nothing more to do
651 if Is_Generic_Instance (Pack) then
652 null;
654 -- Do not inline the package if the subprogram is an init proc
655 -- or other internally generated subprogram, because in that
656 -- case the subprogram body appears in the same unit that
657 -- declares the type, and that body is visible to the back end.
658 -- Do not inline it either if it is in the main unit.
659 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
660 -- calls if the back end takes care of inlining the call.
661 -- Note that Level is in Inline_Call | Inline_Package here.
663 elsif ((Level = Inline_Call
664 and then Has_Pragma_Inline_Always (E)
665 and then Back_End_Inlining)
666 or else Level = Inline_Package)
667 and then not Is_Inlined (Pack)
668 and then not Is_Internal (E)
669 and then not In_Main_Unit_Or_Subunit (Pack)
670 then
671 Set_Is_Inlined (Pack);
672 Inlined_Bodies.Increment_Last;
673 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
674 end if;
675 end if;
677 -- Ensure that Analyze_Inlined_Bodies will be invoked after
678 -- completing the analysis of the current unit.
680 Inline_Processing_Required := True;
681 end;
682 end Add_Inlined_Body;
684 --------------------------
685 -- Add_Inlined_Instance --
686 --------------------------
688 procedure Add_Inlined_Instance (E : Entity_Id) is
689 Decl_Node : constant Node_Id := Unit_Declaration_Node (E);
690 Index : Int;
692 begin
693 -- This machinery is only used with back-end inlining
695 if not Back_End_Inlining then
696 return;
697 end if;
699 -- Register the instance in the list
701 Append_New_Elmt (Decl_Node, To => Backend_Instances);
703 -- Retrieve the index of its corresponding pending instantiation
704 -- and mark this corresponding pending instantiation as needed.
706 Index := To_Pending_Instantiations.Get (Decl_Node);
707 if Index >= 0 then
708 Called_Pending_Instantiations.Append (Index);
709 else
710 pragma Assert (False);
711 null;
712 end if;
714 Set_Is_Called (E);
715 end Add_Inlined_Instance;
717 ----------------------------
718 -- Add_Inlined_Subprogram --
719 ----------------------------
721 procedure Add_Inlined_Subprogram (E : Entity_Id) is
722 Decl : constant Node_Id := Parent (Declaration_Node (E));
723 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
725 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
726 -- Append Subp to the list of subprograms inlined by the backend
728 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
729 -- Append Subp to the list of subprograms that cannot be inlined by
730 -- the backend.
732 -----------------------------------------
733 -- Register_Backend_Inlined_Subprogram --
734 -----------------------------------------
736 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
737 begin
738 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
739 end Register_Backend_Inlined_Subprogram;
741 ---------------------------------------------
742 -- Register_Backend_Not_Inlined_Subprogram --
743 ---------------------------------------------
745 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
746 begin
747 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
748 end Register_Backend_Not_Inlined_Subprogram;
750 -- Start of processing for Add_Inlined_Subprogram
752 begin
753 -- We can inline the subprogram if its unit is known to be inlined or is
754 -- an instance whose body will be analyzed anyway or the subprogram was
755 -- generated as a body by the compiler (for example an initialization
756 -- procedure) or its declaration was provided along with the body (for
757 -- example an expression function) and it does not declare types with
758 -- nontrivial initialization procedures.
760 if (Is_Inlined (Pack)
761 or else Is_Generic_Instance (Pack)
762 or else Nkind (Decl) = N_Subprogram_Body
763 or else Present (Corresponding_Body (Decl)))
764 and then not Has_Initialized_Type (E)
765 then
766 Register_Backend_Inlined_Subprogram (E);
768 if No (Last_Inlined) then
769 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
770 else
771 Set_Next_Inlined_Subprogram (Last_Inlined, E);
772 end if;
774 Last_Inlined := E;
776 else
777 Register_Backend_Not_Inlined_Subprogram (E);
778 end if;
779 end Add_Inlined_Subprogram;
781 --------------------------------
782 -- Add_Pending_Instantiation --
783 --------------------------------
785 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
786 Act_Decl_Id : Entity_Id;
787 Index : Int;
789 begin
790 -- Here is a defense against a ludicrous number of instantiations
791 -- caused by a circular set of instantiation attempts.
793 if Pending_Instantiations.Last + 1 >= Maximum_Instantiations then
794 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
795 Error_Msg_N ("too many instantiations, exceeds max of^", Inst);
796 Error_Msg_N ("\limit can be changed using -gnateinn switch", Inst);
797 raise Unrecoverable_Error;
798 end if;
800 -- Capture the body of the generic instantiation along with its context
801 -- for later processing by Instantiate_Bodies.
803 Pending_Instantiations.Append
804 ((Act_Decl => Act_Decl,
805 Config_Switches => Save_Config_Switches,
806 Current_Sem_Unit => Current_Sem_Unit,
807 Expander_Status => Expander_Active,
808 Inst_Node => Inst,
809 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
810 Scope_Suppress => Scope_Suppress,
811 Warnings => Save_Warnings));
813 -- With back-end inlining, also associate the index to the instantiation
815 if Back_End_Inlining then
816 Act_Decl_Id := Defining_Entity (Act_Decl);
817 Index := Pending_Instantiations.Last;
819 To_Pending_Instantiations.Set (Act_Decl, Index);
821 -- If an instantiation is in the main unit or subunit, or is a nested
822 -- subprogram, then its body is needed as per the analysis done in
823 -- Analyze_Package_Instantiation & Analyze_Subprogram_Instantiation.
825 if In_Main_Unit_Or_Subunit (Act_Decl_Id)
826 or else (Is_Subprogram (Act_Decl_Id)
827 and then Is_Nested (Act_Decl_Id))
828 then
829 Called_Pending_Instantiations.Append (Index);
831 Set_Is_Called (Act_Decl_Id);
832 end if;
833 end if;
834 end Add_Pending_Instantiation;
836 ------------------------
837 -- Add_Scope_To_Clean --
838 ------------------------
840 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
841 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
842 Elmt : Elmt_Id;
844 begin
845 -- If the instance appears in a library-level package declaration,
846 -- all finalization is global, and nothing needs doing here.
848 if Scop = Standard_Standard then
849 return;
850 end if;
852 -- If the instance is within a generic unit, no finalization code
853 -- can be generated. Note that at this point all bodies have been
854 -- analyzed, and the scope stack itself is not present, and the flag
855 -- Inside_A_Generic is not set.
857 declare
858 S : Entity_Id;
860 begin
861 S := Scope (Inst);
862 while Present (S) and then S /= Standard_Standard loop
863 if Is_Generic_Unit (S) then
864 return;
865 end if;
867 S := Scope (S);
868 end loop;
869 end;
871 Elmt := First_Elmt (To_Clean);
872 while Present (Elmt) loop
873 if Node (Elmt) = Scop then
874 return;
875 end if;
877 Next_Elmt (Elmt);
878 end loop;
880 Append_Elmt (Scop, To_Clean);
881 end Add_Scope_To_Clean;
883 --------------
884 -- Add_Subp --
885 --------------
887 function Add_Subp (E : Entity_Id) return Subp_Index is
888 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
889 J : Subp_Index;
891 procedure New_Entry;
892 -- Initialize entry in Inlined table
894 procedure New_Entry is
895 begin
896 Inlined.Increment_Last;
897 Inlined.Table (Inlined.Last).Name := E;
898 Inlined.Table (Inlined.Last).Next := No_Subp;
899 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
900 Inlined.Table (Inlined.Last).Main_Call := False;
901 Inlined.Table (Inlined.Last).Processed := False;
902 end New_Entry;
904 -- Start of processing for Add_Subp
906 begin
907 if Hash_Headers (Index) = No_Subp then
908 New_Entry;
909 Hash_Headers (Index) := Inlined.Last;
910 return Inlined.Last;
912 else
913 J := Hash_Headers (Index);
914 while J /= No_Subp loop
915 if Inlined.Table (J).Name = E then
916 return J;
917 else
918 Index := J;
919 J := Inlined.Table (J).Next;
920 end if;
921 end loop;
923 -- On exit, subprogram was not found. Enter in table. Index is
924 -- the current last entry on the hash chain.
926 New_Entry;
927 Inlined.Table (Index).Next := Inlined.Last;
928 return Inlined.Last;
929 end if;
930 end Add_Subp;
932 ----------------------------
933 -- Analyze_Inlined_Bodies --
934 ----------------------------
936 procedure Analyze_Inlined_Bodies is
937 Comp_Unit : Node_Id;
938 J : Int;
939 Pack : Entity_Id;
940 Subp : Subp_Index;
941 S : Succ_Index;
943 type Pending_Index is new Nat;
945 package Pending_Inlined is new Table.Table (
946 Table_Component_Type => Subp_Index,
947 Table_Index_Type => Pending_Index,
948 Table_Low_Bound => 1,
949 Table_Initial => Alloc.Inlined_Initial,
950 Table_Increment => Alloc.Inlined_Increment,
951 Table_Name => "Pending_Inlined");
952 -- The workpile used to compute the transitive closure
954 -- Start of processing for Analyze_Inlined_Bodies
956 begin
957 if Serious_Errors_Detected = 0 then
958 Push_Scope (Standard_Standard);
960 J := 0;
961 while J <= Inlined_Bodies.Last
962 and then Serious_Errors_Detected = 0
963 loop
964 Pack := Inlined_Bodies.Table (J);
965 while Present (Pack)
966 and then Scope (Pack) /= Standard_Standard
967 and then not Is_Child_Unit (Pack)
968 loop
969 Pack := Scope (Pack);
970 end loop;
972 Comp_Unit := Parent (Pack);
973 while Present (Comp_Unit)
974 and then Nkind (Comp_Unit) /= N_Compilation_Unit
975 loop
976 Comp_Unit := Parent (Comp_Unit);
977 end loop;
979 -- Load the body if it exists and contains inlineable entities,
980 -- unless it is the main unit, or is an instance whose body has
981 -- already been analyzed.
983 if Present (Comp_Unit)
984 and then Comp_Unit /= Cunit (Main_Unit)
985 and then Body_Required (Comp_Unit)
986 and then
987 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
988 or else
989 (No (Corresponding_Body (Unit (Comp_Unit)))
990 and then Body_Needed_For_Inlining
991 (Defining_Entity (Unit (Comp_Unit)))))
992 then
993 declare
994 Bname : constant Unit_Name_Type :=
995 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
997 OK : Boolean;
999 begin
1000 if not Is_Loaded (Bname) then
1001 Style_Check := False;
1002 Load_Needed_Body (Comp_Unit, OK);
1004 if not OK then
1006 -- Warn that a body was not available for inlining
1007 -- by the back-end.
1009 Error_Msg_Unit_1 := Bname;
1010 Error_Msg_N
1011 ("one or more inlined subprograms accessed in $!??",
1012 Comp_Unit);
1013 Error_Msg_File_1 :=
1014 Get_File_Name (Bname, Subunit => False);
1015 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
1016 end if;
1017 end if;
1018 end;
1019 end if;
1021 J := J + 1;
1023 if J > Inlined_Bodies.Last then
1025 -- The analysis of required bodies may have produced additional
1026 -- generic instantiations. To obtain further inlining, we need
1027 -- to perform another round of generic body instantiations.
1029 Instantiate_Bodies;
1031 -- Symmetrically, the instantiation of required generic bodies
1032 -- may have caused additional bodies to be inlined. To obtain
1033 -- further inlining, we keep looping over the inlined bodies.
1034 end if;
1035 end loop;
1037 -- The list of inlined subprograms is an overestimate, because it
1038 -- includes inlined functions called from functions that are compiled
1039 -- as part of an inlined package, but are not themselves called. An
1040 -- accurate computation of just those subprograms that are needed
1041 -- requires that we perform a transitive closure over the call graph,
1042 -- starting from calls in the main compilation unit.
1044 for Index in Inlined.First .. Inlined.Last loop
1045 if not Is_Called (Inlined.Table (Index).Name) then
1047 -- This means that Add_Inlined_Body added the subprogram to the
1048 -- table but wasn't able to handle its code unit. Do nothing.
1050 Inlined.Table (Index).Processed := True;
1052 elsif Inlined.Table (Index).Main_Call then
1053 Pending_Inlined.Increment_Last;
1054 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
1055 Inlined.Table (Index).Processed := True;
1057 else
1058 Set_Is_Called (Inlined.Table (Index).Name, False);
1059 end if;
1060 end loop;
1062 -- Iterate over the workpile until it is emptied, propagating the
1063 -- Is_Called flag to the successors of the processed subprogram.
1065 while Pending_Inlined.Last >= Pending_Inlined.First loop
1066 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
1067 Pending_Inlined.Decrement_Last;
1069 S := Inlined.Table (Subp).First_Succ;
1071 while S /= No_Succ loop
1072 Subp := Successors.Table (S).Subp;
1074 if not Inlined.Table (Subp).Processed then
1075 Set_Is_Called (Inlined.Table (Subp).Name);
1076 Pending_Inlined.Increment_Last;
1077 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
1078 Inlined.Table (Subp).Processed := True;
1079 end if;
1081 S := Successors.Table (S).Next;
1082 end loop;
1083 end loop;
1085 -- Finally add the called subprograms to the list of inlined
1086 -- subprograms for the unit.
1088 for Index in Inlined.First .. Inlined.Last loop
1089 declare
1090 E : constant Subprogram_Kind_Id := Inlined.Table (Index).Name;
1092 begin
1093 if Is_Called (E) and then not Is_Ignored_Ghost_Entity (E) then
1094 Add_Inlined_Subprogram (E);
1095 end if;
1096 end;
1097 end loop;
1099 Pop_Scope;
1100 end if;
1101 end Analyze_Inlined_Bodies;
1103 --------------------------
1104 -- Build_Body_To_Inline --
1105 --------------------------
1107 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1108 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1109 Original_Body : Node_Id;
1110 Body_To_Analyze : Node_Id;
1111 Max_Size : constant := 10;
1113 function Has_Extended_Return return Boolean;
1114 -- This function returns True if the subprogram has an extended return
1115 -- statement.
1117 function Has_Pending_Instantiation return Boolean;
1118 -- If some enclosing body contains instantiations that appear before
1119 -- the corresponding generic body, the enclosing body has a freeze node
1120 -- so that it can be elaborated after the generic itself. This might
1121 -- conflict with subsequent inlinings, so that it is unsafe to try to
1122 -- inline in such a case.
1124 function Has_Single_Return_In_GNATprove_Mode return Boolean;
1125 -- This function is called only in GNATprove mode, and it returns
1126 -- True if the subprogram has no return statement or a single return
1127 -- statement as last statement. It returns False for subprogram with
1128 -- a single return as last statement inside one or more blocks, as
1129 -- inlining would generate gotos in that case as well (although the
1130 -- goto is useless in that case).
1132 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1133 -- If the body of the subprogram includes a call that returns an
1134 -- unconstrained type, the secondary stack is involved, and it is
1135 -- not worth inlining.
1137 -------------------------
1138 -- Has_Extended_Return --
1139 -------------------------
1141 function Has_Extended_Return return Boolean is
1142 Body_To_Inline : constant Node_Id := N;
1144 function Check_Return (N : Node_Id) return Traverse_Result;
1145 -- Returns OK on node N if this is not an extended return statement
1147 ------------------
1148 -- Check_Return --
1149 ------------------
1151 function Check_Return (N : Node_Id) return Traverse_Result is
1152 begin
1153 case Nkind (N) is
1154 when N_Extended_Return_Statement =>
1155 return Abandon;
1157 -- Skip locally declared subprogram bodies inside the body to
1158 -- inline, as the return statements inside those do not count.
1160 when N_Subprogram_Body =>
1161 if N = Body_To_Inline then
1162 return OK;
1163 else
1164 return Skip;
1165 end if;
1167 when others =>
1168 return OK;
1169 end case;
1170 end Check_Return;
1172 function Check_All_Returns is new Traverse_Func (Check_Return);
1174 -- Start of processing for Has_Extended_Return
1176 begin
1177 return Check_All_Returns (N) /= OK;
1178 end Has_Extended_Return;
1180 -------------------------------
1181 -- Has_Pending_Instantiation --
1182 -------------------------------
1184 function Has_Pending_Instantiation return Boolean is
1185 S : Entity_Id;
1187 begin
1188 S := Current_Scope;
1189 while Present (S) loop
1190 if Is_Compilation_Unit (S)
1191 or else Is_Child_Unit (S)
1192 then
1193 return False;
1195 elsif Ekind (S) = E_Package
1196 and then Has_Forward_Instantiation (S)
1197 then
1198 return True;
1199 end if;
1201 S := Scope (S);
1202 end loop;
1204 return False;
1205 end Has_Pending_Instantiation;
1207 -----------------------------------------
1208 -- Has_Single_Return_In_GNATprove_Mode --
1209 -----------------------------------------
1211 function Has_Single_Return_In_GNATprove_Mode return Boolean is
1212 Body_To_Inline : constant Node_Id := N;
1213 Last_Statement : Node_Id := Empty;
1215 function Check_Return (N : Node_Id) return Traverse_Result;
1216 -- Returns OK on node N if this is not a return statement different
1217 -- from the last statement in the subprogram.
1219 ------------------
1220 -- Check_Return --
1221 ------------------
1223 function Check_Return (N : Node_Id) return Traverse_Result is
1224 begin
1225 case Nkind (N) is
1226 when N_Extended_Return_Statement
1227 | N_Simple_Return_Statement
1229 if N = Last_Statement then
1230 return OK;
1231 else
1232 return Abandon;
1233 end if;
1235 -- Skip locally declared subprogram bodies inside the body to
1236 -- inline, as the return statements inside those do not count.
1238 when N_Subprogram_Body =>
1239 if N = Body_To_Inline then
1240 return OK;
1241 else
1242 return Skip;
1243 end if;
1245 when others =>
1246 return OK;
1247 end case;
1248 end Check_Return;
1250 function Check_All_Returns is new Traverse_Func (Check_Return);
1252 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
1254 begin
1255 -- Retrieve the last statement
1257 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
1259 -- Check that the last statement is the only possible return
1260 -- statement in the subprogram.
1262 return Check_All_Returns (N) = OK;
1263 end Has_Single_Return_In_GNATprove_Mode;
1265 --------------------------
1266 -- Uses_Secondary_Stack --
1267 --------------------------
1269 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1270 function Check_Call (N : Node_Id) return Traverse_Result;
1271 -- Look for function calls that return an unconstrained type
1273 ----------------
1274 -- Check_Call --
1275 ----------------
1277 function Check_Call (N : Node_Id) return Traverse_Result is
1278 begin
1279 if Nkind (N) = N_Function_Call
1280 and then Is_Entity_Name (Name (N))
1281 and then Is_Composite_Type (Etype (Entity (Name (N))))
1282 and then not Is_Constrained (Etype (Entity (Name (N))))
1283 then
1284 Cannot_Inline
1285 ("cannot inline & (call returns unconstrained type)?",
1286 N, Spec_Id);
1287 return Abandon;
1288 else
1289 return OK;
1290 end if;
1291 end Check_Call;
1293 function Check_Calls is new Traverse_Func (Check_Call);
1295 begin
1296 return Check_Calls (Bod) = Abandon;
1297 end Uses_Secondary_Stack;
1299 -- Start of processing for Build_Body_To_Inline
1301 begin
1302 -- Return immediately if done already
1304 if Nkind (Decl) = N_Subprogram_Declaration
1305 and then Present (Body_To_Inline (Decl))
1306 then
1307 return;
1309 -- Subprograms that have return statements in the middle of the body are
1310 -- inlined with gotos. GNATprove does not currently support gotos, so
1311 -- we prevent such inlining.
1313 elsif GNATprove_Mode
1314 and then not Has_Single_Return_In_GNATprove_Mode
1315 then
1316 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
1317 return;
1319 -- Functions that return controlled types cannot currently be inlined
1320 -- because they require secondary stack handling; controlled actions
1321 -- may also interfere in complex ways with inlining.
1323 elsif Ekind (Spec_Id) = E_Function
1324 and then Needs_Finalization (Etype (Spec_Id))
1325 then
1326 Cannot_Inline
1327 ("cannot inline & (controlled return type)?", N, Spec_Id);
1328 return;
1329 end if;
1331 if Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
1332 return;
1333 end if;
1335 if Present (Handled_Statement_Sequence (N)) then
1336 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1337 Cannot_Inline
1338 ("cannot inline& (exception handler)?",
1339 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1340 Spec_Id);
1341 return;
1343 elsif Has_Excluded_Statement
1344 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1345 then
1346 return;
1347 end if;
1348 end if;
1350 -- We do not inline a subprogram that is too large, unless it is marked
1351 -- Inline_Always or we are in GNATprove mode. This pragma does not
1352 -- suppress the other checks on inlining (forbidden declarations,
1353 -- handlers, etc).
1355 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1356 and then List_Length
1357 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1358 then
1359 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1360 return;
1361 end if;
1363 if Has_Pending_Instantiation then
1364 Cannot_Inline
1365 ("cannot inline& (forward instance within enclosing body)?",
1366 N, Spec_Id);
1367 return;
1368 end if;
1370 -- Within an instance, the body to inline must be treated as a nested
1371 -- generic, so that the proper global references are preserved.
1373 -- Note that we do not do this at the library level, because it is not
1374 -- needed, and furthermore this causes trouble if front-end inlining
1375 -- is activated (-gnatN).
1377 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1378 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1379 Original_Body := Copy_Generic_Node (N, Empty, Instantiating => True);
1380 else
1381 Original_Body := Copy_Separate_Tree (N);
1382 end if;
1384 -- We need to capture references to the formals in order to substitute
1385 -- the actuals at the point of inlining, i.e. instantiation. To treat
1386 -- the formals as globals to the body to inline, we nest it within a
1387 -- dummy parameterless subprogram, declared within the real one. To
1388 -- avoid generating an internal name (which is never public, and which
1389 -- affects serial numbers of other generated names), we use an internal
1390 -- symbol that cannot conflict with user declarations.
1392 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1393 Set_Defining_Unit_Name
1394 (Specification (Original_Body),
1395 Make_Defining_Identifier (Sloc (N), Name_uParent));
1396 Set_Corresponding_Spec (Original_Body, Empty);
1398 -- Remove all aspects/pragmas that have no meaning in an inlined body
1400 Remove_Aspects_And_Pragmas (Original_Body);
1402 Body_To_Analyze :=
1403 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1405 -- Set return type of function, which is also global and does not need
1406 -- to be resolved.
1408 if Ekind (Spec_Id) = E_Function then
1409 Set_Result_Definition
1410 (Specification (Body_To_Analyze),
1411 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1412 end if;
1414 if No (Declarations (N)) then
1415 Set_Declarations (N, New_List (Body_To_Analyze));
1416 else
1417 Append (Body_To_Analyze, Declarations (N));
1418 end if;
1420 Start_Generic;
1422 Analyze (Body_To_Analyze);
1423 Push_Scope (Defining_Entity (Body_To_Analyze));
1424 Save_Global_References (Original_Body);
1425 End_Scope;
1426 Remove (Body_To_Analyze);
1428 End_Generic;
1430 -- Restore environment if previously saved
1432 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1433 Restore_Env;
1434 end if;
1436 -- Functions that return unconstrained composite types require
1437 -- secondary stack handling, and cannot currently be inlined, unless
1438 -- all return statements return a local variable that is the first
1439 -- local declaration in the body. We had to delay this check until
1440 -- the body of the function is analyzed since Has_Single_Return()
1441 -- requires a minimum decoration.
1443 if Ekind (Spec_Id) = E_Function
1444 and then not Is_Scalar_Type (Etype (Spec_Id))
1445 and then not Is_Access_Type (Etype (Spec_Id))
1446 and then not Is_Constrained (Etype (Spec_Id))
1447 then
1448 if not Has_Single_Return (Body_To_Analyze)
1450 -- Skip inlining if the function returns an unconstrained type
1451 -- using an extended return statement, since this part of the
1452 -- new inlining model is not yet supported by the current
1453 -- implementation.
1455 or else (Returns_Unconstrained_Type (Spec_Id)
1456 and then Has_Extended_Return)
1457 then
1458 Cannot_Inline
1459 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1460 return;
1461 end if;
1463 -- If secondary stack is used, there is no point in inlining. We have
1464 -- already issued the warning in this case, so nothing to do.
1466 elsif Uses_Secondary_Stack (Body_To_Analyze) then
1467 return;
1468 end if;
1470 Set_Body_To_Inline (Decl, Original_Body);
1471 Mutate_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1472 Set_Is_Inlined (Spec_Id);
1473 end Build_Body_To_Inline;
1475 -------------------------------------------
1476 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1477 -------------------------------------------
1479 function Call_Can_Be_Inlined_In_GNATprove_Mode
1480 (N : Node_Id;
1481 Subp : Entity_Id) return Boolean
1483 F : Entity_Id;
1484 A : Node_Id;
1486 begin
1487 F := First_Formal (Subp);
1488 A := First_Actual (N);
1489 while Present (F) loop
1490 if Ekind (F) /= E_Out_Parameter
1491 and then not Same_Type (Etype (F), Etype (A))
1492 and then
1493 (Is_By_Reference_Type (Etype (A))
1494 or else Is_Limited_Type (Etype (A)))
1495 then
1496 return False;
1497 end if;
1499 Next_Formal (F);
1500 Next_Actual (A);
1501 end loop;
1503 return True;
1504 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1506 --------------------------------------
1507 -- Can_Be_Inlined_In_GNATprove_Mode --
1508 --------------------------------------
1510 function Can_Be_Inlined_In_GNATprove_Mode
1511 (Spec_Id : Entity_Id;
1512 Body_Id : Entity_Id) return Boolean
1514 function Has_Formal_Or_Result_Of_Deep_Type
1515 (Id : Entity_Id) return Boolean;
1516 -- Returns true if the subprogram has at least one formal parameter or
1517 -- a return type of a deep type: either an access type or a composite
1518 -- type containing an access type.
1520 function Has_Formal_With_Discriminant_Dependent_Fields
1521 (Id : Entity_Id) return Boolean;
1522 -- Returns true if the subprogram has at least one formal parameter of
1523 -- an unconstrained record type with per-object constraints on component
1524 -- types.
1526 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1527 -- Return True if subprogram Id has any contract. The presence of
1528 -- Extensions_Visible or Volatile_Function is also considered as a
1529 -- contract here.
1531 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1532 -- Return True if subprogram Id defines a compilation unit
1534 function In_Package_Spec (Id : Entity_Id) return Boolean;
1535 -- Return True if subprogram Id is defined in the package specification,
1536 -- either its visible or private part.
1538 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean;
1539 -- Return True if subprogram Id could be a traversal function, as
1540 -- defined in SPARK RM 3.10. This is only a safe approximation, as the
1541 -- knowledge of the SPARK boundary is needed to determine exactly
1542 -- traversal functions.
1544 ---------------------------------------
1545 -- Has_Formal_Or_Result_Of_Deep_Type --
1546 ---------------------------------------
1548 function Has_Formal_Or_Result_Of_Deep_Type
1549 (Id : Entity_Id) return Boolean
1551 function Is_Deep (Typ : Entity_Id) return Boolean;
1552 -- Return True if Typ is deep: either an access type or a composite
1553 -- type containing an access type.
1555 -------------
1556 -- Is_Deep --
1557 -------------
1559 function Is_Deep (Typ : Entity_Id) return Boolean is
1560 begin
1561 case Type_Kind'(Ekind (Typ)) is
1562 when Access_Kind =>
1563 return True;
1565 when E_Array_Type
1566 | E_Array_Subtype
1568 return Is_Deep (Component_Type (Typ));
1570 when Record_Kind =>
1571 declare
1572 Comp : Entity_Id := First_Component_Or_Discriminant (Typ);
1573 begin
1574 while Present (Comp) loop
1575 if Is_Deep (Etype (Comp)) then
1576 return True;
1577 end if;
1578 Next_Component_Or_Discriminant (Comp);
1579 end loop;
1580 end;
1581 return False;
1583 when Scalar_Kind
1584 | E_String_Literal_Subtype
1585 | Concurrent_Kind
1586 | Incomplete_Kind
1587 | E_Exception_Type
1588 | E_Subprogram_Type
1590 return False;
1592 when E_Private_Type
1593 | E_Private_Subtype
1594 | E_Limited_Private_Type
1595 | E_Limited_Private_Subtype
1597 -- Conservatively consider that the type might be deep if
1598 -- its completion has not been seen yet.
1600 if No (Underlying_Type (Typ)) then
1601 return True;
1603 -- Do not peek under a private type if its completion has
1604 -- SPARK_Mode Off. In such a case, a deep type is considered
1605 -- by GNATprove to be not deep.
1607 elsif Present (Full_View (Typ))
1608 and then Present (SPARK_Pragma (Full_View (Typ)))
1609 and then Get_SPARK_Mode_From_Annotation
1610 (SPARK_Pragma (Full_View (Typ))) = Off
1611 then
1612 return False;
1614 -- Otherwise peek under the private type.
1616 else
1617 return Is_Deep (Underlying_Type (Typ));
1618 end if;
1619 end case;
1620 end Is_Deep;
1622 -- Local variables
1624 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1625 Formal : Entity_Id;
1626 Formal_Typ : Entity_Id;
1628 -- Start of processing for Has_Formal_Or_Result_Of_Deep_Type
1630 begin
1631 -- Inspect all parameters of the subprogram looking for a formal
1632 -- of a deep type.
1634 Formal := First_Formal (Subp_Id);
1635 while Present (Formal) loop
1636 Formal_Typ := Etype (Formal);
1638 if Is_Deep (Formal_Typ) then
1639 return True;
1640 end if;
1642 Next_Formal (Formal);
1643 end loop;
1645 -- Check whether this is a function whose return type is deep
1647 if Ekind (Subp_Id) = E_Function
1648 and then Is_Deep (Etype (Subp_Id))
1649 then
1650 return True;
1651 end if;
1653 return False;
1654 end Has_Formal_Or_Result_Of_Deep_Type;
1656 ---------------------------------------------------
1657 -- Has_Formal_With_Discriminant_Dependent_Fields --
1658 ---------------------------------------------------
1660 function Has_Formal_With_Discriminant_Dependent_Fields
1661 (Id : Entity_Id) return Boolean
1663 function Has_Discriminant_Dependent_Component
1664 (Typ : Entity_Id) return Boolean;
1665 -- Determine whether unconstrained record type Typ has at least one
1666 -- component that depends on a discriminant.
1668 ------------------------------------------
1669 -- Has_Discriminant_Dependent_Component --
1670 ------------------------------------------
1672 function Has_Discriminant_Dependent_Component
1673 (Typ : Entity_Id) return Boolean
1675 Comp : Entity_Id;
1677 begin
1678 -- Inspect all components of the record type looking for one that
1679 -- depends on a discriminant.
1681 Comp := First_Component (Typ);
1682 while Present (Comp) loop
1683 if Has_Discriminant_Dependent_Constraint (Comp) then
1684 return True;
1685 end if;
1687 Next_Component (Comp);
1688 end loop;
1690 return False;
1691 end Has_Discriminant_Dependent_Component;
1693 -- Local variables
1695 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1696 Formal : Entity_Id;
1697 Formal_Typ : Entity_Id;
1699 -- Start of processing for
1700 -- Has_Formal_With_Discriminant_Dependent_Fields
1702 begin
1703 -- Inspect all parameters of the subprogram looking for a formal
1704 -- of an unconstrained record type with at least one discriminant
1705 -- dependent component.
1707 Formal := First_Formal (Subp_Id);
1708 while Present (Formal) loop
1709 Formal_Typ := Etype (Formal);
1711 if Is_Record_Type (Formal_Typ)
1712 and then not Is_Constrained (Formal_Typ)
1713 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1714 then
1715 return True;
1716 end if;
1718 Next_Formal (Formal);
1719 end loop;
1721 return False;
1722 end Has_Formal_With_Discriminant_Dependent_Fields;
1724 -----------------------
1725 -- Has_Some_Contract --
1726 -----------------------
1728 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1729 Items : Node_Id;
1731 begin
1732 -- A call to an expression function may precede the actual body which
1733 -- is inserted at the end of the enclosing declarations. Ensure that
1734 -- the related entity is decorated before inspecting the contract.
1736 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1737 Items := Contract (Id);
1739 -- Note that Classifications is not Empty when Extensions_Visible
1740 -- or Volatile_Function is present, which causes such subprograms
1741 -- to be considered to have a contract here. This is fine as we
1742 -- want to avoid inlining these too.
1744 return Present (Items)
1745 and then (Present (Pre_Post_Conditions (Items)) or else
1746 Present (Contract_Test_Cases (Items)) or else
1747 Present (Classifications (Items)));
1748 end if;
1750 return False;
1751 end Has_Some_Contract;
1753 ---------------------
1754 -- In_Package_Spec --
1755 ---------------------
1757 function In_Package_Spec (Id : Entity_Id) return Boolean is
1758 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1759 -- Parent of the subprogram's declaration
1761 begin
1762 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1763 end In_Package_Spec;
1765 ------------------------
1766 -- Is_Unit_Subprogram --
1767 ------------------------
1769 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1770 Decl : Node_Id := Parent (Parent (Id));
1771 begin
1772 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1773 Decl := Parent (Decl);
1774 end if;
1776 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1777 end Is_Unit_Subprogram;
1779 ------------------------------
1780 -- Maybe_Traversal_Function --
1781 ------------------------------
1783 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean is
1784 begin
1785 return Ekind (Id) = E_Function
1787 -- Only traversal functions return an anonymous access-to-object
1788 -- type in SPARK.
1790 and then Is_Anonymous_Access_Type (Etype (Id));
1791 end Maybe_Traversal_Function;
1793 -- Local declarations
1795 Id : Entity_Id;
1796 -- Procedure or function entity for the subprogram
1798 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1800 begin
1801 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1803 if Present (Spec_Id) then
1804 Id := Spec_Id;
1805 else
1806 Id := Body_Id;
1807 end if;
1809 -- Only local subprograms without contracts are inlined in GNATprove
1810 -- mode, as these are the subprograms which a user is not interested in
1811 -- analyzing in isolation, but rather in the context of their call. This
1812 -- is a convenient convention, that could be changed for an explicit
1813 -- pragma/aspect one day.
1815 -- In a number of special cases, inlining is not desirable or not
1816 -- possible, see below.
1818 -- Do not inline unit-level subprograms
1820 if Is_Unit_Subprogram (Id) then
1821 return False;
1823 -- Do not inline subprograms declared in package specs, because they are
1824 -- not local, i.e. can be called either from anywhere (if declared in
1825 -- visible part) or from the child units (if declared in private part).
1827 elsif In_Package_Spec (Id) then
1828 return False;
1830 -- Do not inline subprograms declared in other units. This is important
1831 -- in particular for subprograms defined in the private part of a
1832 -- package spec, when analyzing one of its child packages, as otherwise
1833 -- we issue spurious messages about the impossibility to inline such
1834 -- calls.
1836 elsif not In_Extended_Main_Code_Unit (Id) then
1837 return False;
1839 -- Do not inline dispatching operations, as only their static calls
1840 -- can be analyzed in context, and not their dispatching calls.
1842 elsif Is_Dispatching_Operation (Id) then
1843 return False;
1845 -- Do not inline subprograms marked No_Return, possibly used for
1846 -- signaling errors, which GNATprove handles specially.
1848 elsif No_Return (Id) then
1849 return False;
1851 -- Do not inline subprograms that have a contract on the spec or the
1852 -- body. Use the contract(s) instead in GNATprove. This also prevents
1853 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1855 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
1856 or else
1857 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
1858 then
1859 return False;
1861 -- Do not inline expression functions, which are directly inlined at the
1862 -- prover level.
1864 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
1865 or else
1866 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
1867 then
1868 return False;
1870 -- Do not inline generic subprogram instances. The visibility rules of
1871 -- generic instances plays badly with inlining.
1873 elsif Is_Generic_Instance (Spec_Id) then
1874 return False;
1876 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1877 -- the subprogram body, a similar check is performed after the body
1878 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1880 elsif Present (Spec_Id)
1881 and then
1882 (No (SPARK_Pragma (Spec_Id))
1883 or else
1884 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
1885 then
1886 return False;
1888 -- Do not inline subprograms and entries defined inside protected types,
1889 -- which typically are not helper subprograms, which also avoids getting
1890 -- spurious messages on calls that cannot be inlined.
1892 elsif Within_Protected_Type (Id) then
1893 return False;
1895 -- Do not inline predicate functions (treated specially by GNATprove)
1897 elsif Is_Predicate_Function (Id) then
1898 return False;
1900 -- Do not inline subprograms with a parameter of an unconstrained
1901 -- record type if it has discrimiant dependent fields. Indeed, with
1902 -- such parameters, the frontend cannot always ensure type compliance
1903 -- in record component accesses (in particular with records containing
1904 -- packed arrays).
1906 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
1907 return False;
1909 -- Do not inline subprograms with a formal parameter or return type of
1910 -- a deep type, as in that case inlining might generate code that
1911 -- violates borrow-checking rules of SPARK 3.10 even if the original
1912 -- code did not.
1914 elsif Has_Formal_Or_Result_Of_Deep_Type (Id) then
1915 return False;
1917 -- Do not inline subprograms which may be traversal functions. Such
1918 -- inlining introduces temporary variables of named access type for
1919 -- which assignments are move instead of borrow/observe, possibly
1920 -- leading to spurious errors when checking SPARK rules related to
1921 -- pointer usage.
1923 elsif Maybe_Traversal_Function (Id) then
1924 return False;
1926 -- Otherwise, this is a subprogram declared inside the private part of a
1927 -- package, or inside a package body, or locally in a subprogram, and it
1928 -- does not have any contract. Inline it.
1930 else
1931 return True;
1932 end if;
1933 end Can_Be_Inlined_In_GNATprove_Mode;
1935 -------------------
1936 -- Cannot_Inline --
1937 -------------------
1939 procedure Cannot_Inline
1940 (Msg : String;
1941 N : Node_Id;
1942 Subp : Entity_Id;
1943 Is_Serious : Boolean := False;
1944 Suppress_Info : Boolean := False)
1946 begin
1947 -- In GNATprove mode, inlining is the technical means by which the
1948 -- higher-level goal of contextual analysis is reached, so issue
1949 -- messages about failure to apply contextual analysis to a
1950 -- subprogram, rather than failure to inline it.
1952 if GNATprove_Mode
1953 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1954 then
1955 declare
1956 Len1 : constant Positive :=
1957 String (String'("cannot inline"))'Length;
1958 Len2 : constant Positive :=
1959 String (String'("info: no contextual analysis of"))'Length;
1961 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1963 begin
1964 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1965 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1966 Msg (Msg'First + Len1 .. Msg'Last);
1967 Cannot_Inline (New_Msg, N, Subp, Is_Serious, Suppress_Info);
1968 return;
1969 end;
1970 end if;
1972 pragma Assert (Msg (Msg'Last) = '?');
1974 -- Legacy front-end inlining model
1976 if not Back_End_Inlining then
1978 -- Do not emit warning if this is a predefined unit which is not
1979 -- the main unit. With validity checks enabled, some predefined
1980 -- subprograms may contain nested subprograms and become ineligible
1981 -- for inlining.
1983 if Is_Predefined_Unit (Get_Source_Unit (Subp))
1984 and then not In_Extended_Main_Source_Unit (Subp)
1985 then
1986 null;
1988 -- In GNATprove mode, issue an info message when -gnatd_f is set and
1989 -- Suppress_Info is False, and indicate that the subprogram is not
1990 -- always inlined by setting flag Is_Inlined_Always to False.
1992 elsif GNATprove_Mode then
1993 Set_Is_Inlined_Always (Subp, False);
1995 if Debug_Flag_Underscore_F and not Suppress_Info then
1996 Error_Msg_NE (Msg, N, Subp);
1997 end if;
1999 elsif Has_Pragma_Inline_Always (Subp) then
2001 -- Remove last character (question mark) to make this into an
2002 -- error, because the Inline_Always pragma cannot be obeyed.
2004 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2006 elsif Ineffective_Inline_Warnings then
2007 Error_Msg_NE (Msg & "p?", N, Subp);
2008 end if;
2010 -- New semantics relying on back-end inlining
2012 elsif Is_Serious then
2014 -- Remove last character (question mark) to make this into an error.
2016 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2018 -- In GNATprove mode, issue an info message when -gnatd_f is set and
2019 -- Suppress_Info is False, and indicate that the subprogram is not
2020 -- always inlined by setting flag Is_Inlined_Always to False.
2022 elsif GNATprove_Mode then
2023 Set_Is_Inlined_Always (Subp, False);
2025 if Debug_Flag_Underscore_F and not Suppress_Info then
2026 Error_Msg_NE (Msg, N, Subp);
2027 end if;
2029 else
2031 -- Do not emit warning if this is a predefined unit which is not
2032 -- the main unit. This behavior is currently provided for backward
2033 -- compatibility but it will be removed when we enforce the
2034 -- strictness of the new rules.
2036 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2037 and then not In_Extended_Main_Source_Unit (Subp)
2038 then
2039 null;
2041 elsif Has_Pragma_Inline_Always (Subp) then
2043 -- Emit a warning if this is a call to a runtime subprogram
2044 -- which is located inside a generic. Previously this call
2045 -- was silently skipped.
2047 if Is_Generic_Instance (Subp) then
2048 declare
2049 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
2050 begin
2051 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
2052 Set_Is_Inlined (Subp, False);
2053 Error_Msg_NE (Msg & "p?", N, Subp);
2054 return;
2055 end if;
2056 end;
2057 end if;
2059 -- Remove last character (question mark) to make this into an
2060 -- error, because the Inline_Always pragma cannot be obeyed.
2062 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2064 else
2065 Set_Is_Inlined (Subp, False);
2067 if Ineffective_Inline_Warnings then
2068 Error_Msg_NE (Msg & "p?", N, Subp);
2069 end if;
2070 end if;
2071 end if;
2072 end Cannot_Inline;
2074 --------------------------------------------
2075 -- Check_And_Split_Unconstrained_Function --
2076 --------------------------------------------
2078 procedure Check_And_Split_Unconstrained_Function
2079 (N : Node_Id;
2080 Spec_Id : Entity_Id;
2081 Body_Id : Entity_Id)
2083 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
2084 -- Use generic machinery to build an unexpanded body for the subprogram.
2085 -- This body is subsequently used for inline expansions at call sites.
2087 procedure Build_Return_Object_Formal
2088 (Loc : Source_Ptr;
2089 Obj_Decl : Node_Id;
2090 Formals : List_Id);
2091 -- Create a formal parameter for return object declaration Obj_Decl of
2092 -- an extended return statement and add it to list Formals.
2094 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
2095 -- Return true if we generate code for the function body N, the function
2096 -- body N has no local declarations and its unique statement is a single
2097 -- extended return statement with a handled statements sequence.
2099 procedure Copy_Formals
2100 (Loc : Source_Ptr;
2101 Subp_Id : Entity_Id;
2102 Formals : List_Id);
2103 -- Create new formal parameters from the formal parameters of subprogram
2104 -- Subp_Id and add them to list Formals.
2106 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id;
2107 -- Create a copy of return object declaration Obj_Decl of an extended
2108 -- return statement.
2110 procedure Split_Unconstrained_Function
2111 (N : Node_Id;
2112 Spec_Id : Entity_Id);
2113 -- N is an inlined function body that returns an unconstrained type and
2114 -- has a single extended return statement. Split N in two subprograms:
2115 -- a procedure P' and a function F'. The formals of P' duplicate the
2116 -- formals of N plus an extra formal which is used to return a value;
2117 -- its body is composed by the declarations and list of statements
2118 -- of the extended return statement of N.
2120 --------------------------
2121 -- Build_Body_To_Inline --
2122 --------------------------
2124 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
2125 procedure Generate_Subprogram_Body
2126 (N : Node_Id;
2127 Body_To_Inline : out Node_Id);
2128 -- Generate a parameterless duplicate of subprogram body N. Note that
2129 -- occurrences of pragmas referencing the formals are removed since
2130 -- they have no meaning when the body is inlined and the formals are
2131 -- rewritten (the analysis of the non-inlined body will handle these
2132 -- pragmas). A new internal name is associated with Body_To_Inline.
2134 ------------------------------
2135 -- Generate_Subprogram_Body --
2136 ------------------------------
2138 procedure Generate_Subprogram_Body
2139 (N : Node_Id;
2140 Body_To_Inline : out Node_Id)
2142 begin
2143 -- Within an instance, the body to inline must be treated as a
2144 -- nested generic so that proper global references are preserved.
2146 -- Note that we do not do this at the library level, because it
2147 -- is not needed, and furthermore this causes trouble if front
2148 -- end inlining is activated (-gnatN).
2150 if In_Instance
2151 and then Scope (Current_Scope) /= Standard_Standard
2152 then
2153 Body_To_Inline :=
2154 Copy_Generic_Node (N, Empty, Instantiating => True);
2155 else
2156 Body_To_Inline := New_Copy_Tree (N);
2157 end if;
2159 -- Remove aspects/pragmas that have no meaning in an inlined body
2161 Remove_Aspects_And_Pragmas (Body_To_Inline);
2163 -- We need to capture references to the formals in order
2164 -- to substitute the actuals at the point of inlining, i.e.
2165 -- instantiation. To treat the formals as globals to the body to
2166 -- inline, we nest it within a dummy parameterless subprogram,
2167 -- declared within the real one.
2169 Set_Parameter_Specifications
2170 (Specification (Body_To_Inline), No_List);
2172 -- A new internal name is associated with Body_To_Inline to avoid
2173 -- conflicts when the non-inlined body N is analyzed.
2175 Set_Defining_Unit_Name (Specification (Body_To_Inline),
2176 Make_Temporary (Sloc (N), 'P'));
2177 Set_Corresponding_Spec (Body_To_Inline, Empty);
2178 end Generate_Subprogram_Body;
2180 -- Local variables
2182 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2183 Original_Body : Node_Id;
2184 Body_To_Analyze : Node_Id;
2186 -- Start of processing for Build_Body_To_Inline
2188 begin
2189 pragma Assert (Current_Scope = Spec_Id);
2191 -- Within an instance, the body to inline must be treated as a nested
2192 -- generic, so that the proper global references are preserved. We
2193 -- do not do this at the library level, because it is not needed, and
2194 -- furthermore this causes trouble if front-end inlining is activated
2195 -- (-gnatN).
2197 if In_Instance
2198 and then Scope (Current_Scope) /= Standard_Standard
2199 then
2200 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2201 end if;
2203 -- Capture references to formals in order to substitute the actuals
2204 -- at the point of inlining or instantiation. To treat the formals
2205 -- as globals to the body to inline, nest the body within a dummy
2206 -- parameterless subprogram, declared within the real one.
2208 Generate_Subprogram_Body (N, Original_Body);
2209 Body_To_Analyze :=
2210 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
2212 -- Set return type of function, which is also global and does not
2213 -- need to be resolved.
2215 if Ekind (Spec_Id) = E_Function then
2216 Set_Result_Definition (Specification (Body_To_Analyze),
2217 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
2218 end if;
2220 if No (Declarations (N)) then
2221 Set_Declarations (N, New_List (Body_To_Analyze));
2222 else
2223 Append_To (Declarations (N), Body_To_Analyze);
2224 end if;
2226 Preanalyze (Body_To_Analyze);
2228 Push_Scope (Defining_Entity (Body_To_Analyze));
2229 Save_Global_References (Original_Body);
2230 End_Scope;
2231 Remove (Body_To_Analyze);
2233 -- Restore environment if previously saved
2235 if In_Instance
2236 and then Scope (Current_Scope) /= Standard_Standard
2237 then
2238 Restore_Env;
2239 end if;
2241 pragma Assert (No (Body_To_Inline (Decl)));
2242 Set_Body_To_Inline (Decl, Original_Body);
2243 Mutate_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
2244 end Build_Body_To_Inline;
2246 --------------------------------
2247 -- Build_Return_Object_Formal --
2248 --------------------------------
2250 procedure Build_Return_Object_Formal
2251 (Loc : Source_Ptr;
2252 Obj_Decl : Node_Id;
2253 Formals : List_Id)
2255 Obj_Def : constant Node_Id := Object_Definition (Obj_Decl);
2256 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2257 Typ_Def : Node_Id;
2259 begin
2260 -- Build the type definition of the formal parameter. The use of
2261 -- New_Copy_Tree ensures that global references preserved in the
2262 -- case of generics.
2264 if Is_Entity_Name (Obj_Def) then
2265 Typ_Def := New_Copy_Tree (Obj_Def);
2266 else
2267 Typ_Def := New_Copy_Tree (Subtype_Mark (Obj_Def));
2268 end if;
2270 -- Generate:
2272 -- Obj_Id : [out] Typ_Def
2274 -- Mode OUT should not be used when the return object is declared as
2275 -- a constant. Check the definition of the object declaration because
2276 -- the object has not been analyzed yet.
2278 Append_To (Formals,
2279 Make_Parameter_Specification (Loc,
2280 Defining_Identifier =>
2281 Make_Defining_Identifier (Loc, Chars (Obj_Id)),
2282 In_Present => False,
2283 Out_Present => not Constant_Present (Obj_Decl),
2284 Null_Exclusion_Present => False,
2285 Parameter_Type => Typ_Def));
2286 end Build_Return_Object_Formal;
2288 --------------------------------------
2289 -- Can_Split_Unconstrained_Function --
2290 --------------------------------------
2292 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
2293 Stmt : constant Node_Id :=
2294 First (Statements (Handled_Statement_Sequence (N)));
2295 Decl : Node_Id;
2297 begin
2298 -- No user defined declarations allowed in the function except inside
2299 -- the unique return statement; implicit labels are the only allowed
2300 -- declarations.
2302 Decl := First (Declarations (N));
2303 while Present (Decl) loop
2304 if Nkind (Decl) /= N_Implicit_Label_Declaration then
2305 return False;
2306 end if;
2308 Next (Decl);
2309 end loop;
2311 -- We only split the inlined function when we are generating the code
2312 -- of its body; otherwise we leave duplicated split subprograms in
2313 -- the tree which (if referenced) generate wrong references at link
2314 -- time.
2316 return In_Extended_Main_Code_Unit (N)
2317 and then Present (Stmt)
2318 and then Nkind (Stmt) = N_Extended_Return_Statement
2319 and then No (Next (Stmt))
2320 and then Present (Handled_Statement_Sequence (Stmt));
2321 end Can_Split_Unconstrained_Function;
2323 ------------------
2324 -- Copy_Formals --
2325 ------------------
2327 procedure Copy_Formals
2328 (Loc : Source_Ptr;
2329 Subp_Id : Entity_Id;
2330 Formals : List_Id)
2332 Formal : Entity_Id;
2333 Spec : Node_Id;
2335 begin
2336 Formal := First_Formal (Subp_Id);
2337 while Present (Formal) loop
2338 Spec := Parent (Formal);
2340 -- Create an exact copy of the formal parameter. The use of
2341 -- New_Copy_Tree ensures that global references are preserved
2342 -- in case of generics.
2344 Append_To (Formals,
2345 Make_Parameter_Specification (Loc,
2346 Defining_Identifier =>
2347 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
2348 In_Present => In_Present (Spec),
2349 Out_Present => Out_Present (Spec),
2350 Null_Exclusion_Present => Null_Exclusion_Present (Spec),
2351 Parameter_Type =>
2352 New_Copy_Tree (Parameter_Type (Spec)),
2353 Expression => New_Copy_Tree (Expression (Spec))));
2355 Next_Formal (Formal);
2356 end loop;
2357 end Copy_Formals;
2359 ------------------------
2360 -- Copy_Return_Object --
2361 ------------------------
2363 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id is
2364 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2366 begin
2367 -- The use of New_Copy_Tree ensures that global references are
2368 -- preserved in case of generics.
2370 return
2371 Make_Object_Declaration (Sloc (Obj_Decl),
2372 Defining_Identifier =>
2373 Make_Defining_Identifier (Sloc (Obj_Id), Chars (Obj_Id)),
2374 Aliased_Present => Aliased_Present (Obj_Decl),
2375 Constant_Present => Constant_Present (Obj_Decl),
2376 Null_Exclusion_Present => Null_Exclusion_Present (Obj_Decl),
2377 Object_Definition =>
2378 New_Copy_Tree (Object_Definition (Obj_Decl)),
2379 Expression => New_Copy_Tree (Expression (Obj_Decl)));
2380 end Copy_Return_Object;
2382 ----------------------------------
2383 -- Split_Unconstrained_Function --
2384 ----------------------------------
2386 procedure Split_Unconstrained_Function
2387 (N : Node_Id;
2388 Spec_Id : Entity_Id)
2390 Loc : constant Source_Ptr := Sloc (N);
2391 Ret_Stmt : constant Node_Id :=
2392 First (Statements (Handled_Statement_Sequence (N)));
2393 Ret_Obj : constant Node_Id :=
2394 First (Return_Object_Declarations (Ret_Stmt));
2396 procedure Build_Procedure
2397 (Proc_Id : out Entity_Id;
2398 Decl_List : out List_Id);
2399 -- Build a procedure containing the statements found in the extended
2400 -- return statement of the unconstrained function body N.
2402 ---------------------
2403 -- Build_Procedure --
2404 ---------------------
2406 procedure Build_Procedure
2407 (Proc_Id : out Entity_Id;
2408 Decl_List : out List_Id)
2410 Formals : constant List_Id := New_List;
2411 Subp_Name : constant Name_Id := New_Internal_Name ('F');
2413 Body_Decls : List_Id := No_List;
2414 Decl : Node_Id;
2415 Proc_Body : Node_Id;
2416 Proc_Spec : Node_Id;
2418 begin
2419 -- Create formal parameters for the return object and all formals
2420 -- of the unconstrained function in order to pass their values to
2421 -- the procedure.
2423 Build_Return_Object_Formal
2424 (Loc => Loc,
2425 Obj_Decl => Ret_Obj,
2426 Formals => Formals);
2428 Copy_Formals
2429 (Loc => Loc,
2430 Subp_Id => Spec_Id,
2431 Formals => Formals);
2433 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
2435 Proc_Spec :=
2436 Make_Procedure_Specification (Loc,
2437 Defining_Unit_Name => Proc_Id,
2438 Parameter_Specifications => Formals);
2440 Decl_List := New_List;
2442 Append_To (Decl_List,
2443 Make_Subprogram_Declaration (Loc, Proc_Spec));
2445 -- Can_Convert_Unconstrained_Function checked that the function
2446 -- has no local declarations except implicit label declarations.
2447 -- Copy these declarations to the built procedure.
2449 if Present (Declarations (N)) then
2450 Body_Decls := New_List;
2452 Decl := First (Declarations (N));
2453 while Present (Decl) loop
2454 pragma Assert (Nkind (Decl) = N_Implicit_Label_Declaration);
2456 Append_To (Body_Decls,
2457 Make_Implicit_Label_Declaration (Loc,
2458 Make_Defining_Identifier (Loc,
2459 Chars => Chars (Defining_Identifier (Decl))),
2460 Label_Construct => Empty));
2462 Next (Decl);
2463 end loop;
2464 end if;
2466 pragma Assert (Present (Handled_Statement_Sequence (Ret_Stmt)));
2468 Proc_Body :=
2469 Make_Subprogram_Body (Loc,
2470 Specification => Copy_Subprogram_Spec (Proc_Spec),
2471 Declarations => Body_Decls,
2472 Handled_Statement_Sequence =>
2473 New_Copy_Tree (Handled_Statement_Sequence (Ret_Stmt)));
2475 Set_Defining_Unit_Name (Specification (Proc_Body),
2476 Make_Defining_Identifier (Loc, Subp_Name));
2478 Append_To (Decl_List, Proc_Body);
2479 end Build_Procedure;
2481 -- Local variables
2483 New_Obj : constant Node_Id := Copy_Return_Object (Ret_Obj);
2484 Blk_Stmt : Node_Id;
2485 Proc_Call : Node_Id;
2486 Proc_Id : Entity_Id;
2488 -- Start of processing for Split_Unconstrained_Function
2490 begin
2491 -- Build the associated procedure, analyze it and insert it before
2492 -- the function body N.
2494 declare
2495 Scope : constant Entity_Id := Current_Scope;
2496 Decl_List : List_Id;
2497 begin
2498 Pop_Scope;
2499 Build_Procedure (Proc_Id, Decl_List);
2500 Insert_Actions (N, Decl_List);
2501 Set_Is_Inlined (Proc_Id);
2502 Push_Scope (Scope);
2503 end;
2505 -- Build the call to the generated procedure
2507 declare
2508 Actual_List : constant List_Id := New_List;
2509 Formal : Entity_Id;
2511 begin
2512 Append_To (Actual_List,
2513 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
2515 Formal := First_Formal (Spec_Id);
2516 while Present (Formal) loop
2517 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
2519 -- Avoid spurious warning on unreferenced formals
2521 Set_Referenced (Formal);
2522 Next_Formal (Formal);
2523 end loop;
2525 Proc_Call :=
2526 Make_Procedure_Call_Statement (Loc,
2527 Name => New_Occurrence_Of (Proc_Id, Loc),
2528 Parameter_Associations => Actual_List);
2529 end;
2531 -- Generate:
2533 -- declare
2534 -- New_Obj : ...
2535 -- begin
2536 -- Proc (New_Obj, ...);
2537 -- return New_Obj;
2538 -- end;
2540 Blk_Stmt :=
2541 Make_Block_Statement (Loc,
2542 Declarations => New_List (New_Obj),
2543 Handled_Statement_Sequence =>
2544 Make_Handled_Sequence_Of_Statements (Loc,
2545 Statements => New_List (
2547 Proc_Call,
2549 Make_Simple_Return_Statement (Loc,
2550 Expression =>
2551 New_Occurrence_Of
2552 (Defining_Identifier (New_Obj), Loc)))));
2554 Rewrite (Ret_Stmt, Blk_Stmt);
2555 end Split_Unconstrained_Function;
2557 -- Local variables
2559 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2561 -- Start of processing for Check_And_Split_Unconstrained_Function
2563 begin
2564 pragma Assert (Back_End_Inlining
2565 and then Ekind (Spec_Id) = E_Function
2566 and then Returns_Unconstrained_Type (Spec_Id)
2567 and then Comes_From_Source (Body_Id)
2568 and then (Has_Pragma_Inline_Always (Spec_Id)
2569 or else Optimization_Level > 0));
2571 -- This routine must not be used in GNATprove mode since GNATprove
2572 -- relies on frontend inlining
2574 pragma Assert (not GNATprove_Mode);
2576 -- No need to split the function if we cannot generate the code
2578 if Serious_Errors_Detected /= 0 then
2579 return;
2580 end if;
2582 -- No action needed in stubs since the attribute Body_To_Inline
2583 -- is not available
2585 if Nkind (Decl) = N_Subprogram_Body_Stub then
2586 return;
2588 -- Cannot build the body to inline if the attribute is already set.
2589 -- This attribute may have been set if this is a subprogram renaming
2590 -- declarations (see Freeze.Build_Renamed_Body).
2592 elsif Present (Body_To_Inline (Decl)) then
2593 return;
2595 -- Do not generate a body to inline for protected functions, because the
2596 -- transformation generates a call to a protected procedure, causing
2597 -- spurious errors. We don't inline protected operations anyway, so
2598 -- this is no loss. We might as well ignore intrinsics and foreign
2599 -- conventions as well -- just allow Ada conventions.
2601 elsif not (Convention (Spec_Id) = Convention_Ada
2602 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Copy
2603 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Reference)
2604 then
2605 return;
2607 -- Check excluded declarations
2609 elsif Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
2610 return;
2612 -- Check excluded statements. There is no need to protect us against
2613 -- exception handlers since they are supported by the GCC backend.
2615 elsif Present (Handled_Statement_Sequence (N))
2616 and then Has_Excluded_Statement
2617 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2618 then
2619 return;
2620 end if;
2622 -- Build the body to inline only if really needed
2624 if Can_Split_Unconstrained_Function (N) then
2625 Split_Unconstrained_Function (N, Spec_Id);
2626 Build_Body_To_Inline (N, Spec_Id);
2627 Set_Is_Inlined (Spec_Id);
2628 end if;
2629 end Check_And_Split_Unconstrained_Function;
2631 ---------------------------------------------
2632 -- Check_Object_Renaming_In_GNATprove_Mode --
2633 ---------------------------------------------
2635 procedure Check_Object_Renaming_In_GNATprove_Mode (Spec_Id : Entity_Id) is
2636 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2637 Body_Decl : constant Node_Id :=
2638 Unit_Declaration_Node (Corresponding_Body (Decl));
2640 function Check_Object_Renaming (N : Node_Id) return Traverse_Result;
2641 -- Returns Abandon on node N if this is a reference to an object
2642 -- renaming, which will be expanded into the renamed object in
2643 -- GNATprove mode.
2645 ---------------------------
2646 -- Check_Object_Renaming --
2647 ---------------------------
2649 function Check_Object_Renaming (N : Node_Id) return Traverse_Result is
2650 begin
2651 case Nkind (Original_Node (N)) is
2652 when N_Expanded_Name
2653 | N_Identifier
2655 declare
2656 Obj_Id : constant Entity_Id := Entity (Original_Node (N));
2657 begin
2658 -- Recognize the case when SPARK expansion rewrites a
2659 -- reference to an object renaming.
2661 if Present (Obj_Id)
2662 and then Is_Object (Obj_Id)
2663 and then Present (Renamed_Object (Obj_Id))
2664 and then Nkind (Renamed_Object (Obj_Id)) not in N_Entity
2666 -- Copy_Generic_Node called for inlining expects the
2667 -- references to global entities to have the same kind
2668 -- in the "generic" code and its "instantiation".
2670 and then Nkind (Original_Node (N)) /=
2671 Nkind (Renamed_Object (Obj_Id))
2672 then
2673 return Abandon;
2674 else
2675 return OK;
2676 end if;
2677 end;
2679 when others =>
2680 return OK;
2681 end case;
2682 end Check_Object_Renaming;
2684 function Check_All_Object_Renamings is new
2685 Traverse_Func (Check_Object_Renaming);
2687 -- Start of processing for Check_Object_Renaming_In_GNATprove_Mode
2689 begin
2690 -- Subprograms with object renamings replaced by the special SPARK
2691 -- expansion cannot be inlined.
2693 if Check_All_Object_Renamings (Body_Decl) /= OK then
2694 Cannot_Inline ("cannot inline & (object renaming)?",
2695 Body_Decl, Spec_Id);
2696 Set_Body_To_Inline (Decl, Empty);
2697 end if;
2698 end Check_Object_Renaming_In_GNATprove_Mode;
2700 -------------------------------------
2701 -- Check_Package_Body_For_Inlining --
2702 -------------------------------------
2704 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2705 Bname : Unit_Name_Type;
2706 E : Entity_Id;
2707 OK : Boolean;
2709 begin
2710 -- Legacy implementation (relying on frontend inlining)
2712 if not Back_End_Inlining
2713 and then Is_Compilation_Unit (P)
2714 and then not Is_Generic_Instance (P)
2715 then
2716 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2718 E := First_Entity (P);
2719 while Present (E) loop
2720 if Has_Pragma_Inline_Always (E)
2721 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2722 then
2723 if not Is_Loaded (Bname) then
2724 Load_Needed_Body (N, OK);
2726 if OK then
2728 -- Check we are not trying to inline a parent whose body
2729 -- depends on a child, when we are compiling the body of
2730 -- the child. Otherwise we have a potential elaboration
2731 -- circularity with inlined subprograms and with
2732 -- Taft-Amendment types.
2734 declare
2735 Comp : Node_Id; -- Body just compiled
2736 Child_Spec : Entity_Id; -- Spec of main unit
2737 Ent : Entity_Id; -- For iteration
2738 With_Clause : Node_Id; -- Context of body.
2740 begin
2741 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2742 and then Present (Body_Entity (P))
2743 then
2744 Child_Spec :=
2745 Defining_Entity
2746 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2748 Comp :=
2749 Parent (Unit_Declaration_Node (Body_Entity (P)));
2751 -- Check whether the context of the body just
2752 -- compiled includes a child of itself, and that
2753 -- child is the spec of the main compilation.
2755 With_Clause := First (Context_Items (Comp));
2756 while Present (With_Clause) loop
2757 if Nkind (With_Clause) = N_With_Clause
2758 and then
2759 Scope (Entity (Name (With_Clause))) = P
2760 and then
2761 Entity (Name (With_Clause)) = Child_Spec
2762 then
2763 Error_Msg_Node_2 := Child_Spec;
2764 Error_Msg_NE
2765 ("body of & depends on child unit&??",
2766 With_Clause, P);
2767 Error_Msg_N
2768 ("\subprograms in body cannot be inlined??",
2769 With_Clause);
2771 -- Disable further inlining from this unit,
2772 -- and keep Taft-amendment types incomplete.
2774 Ent := First_Entity (P);
2775 while Present (Ent) loop
2776 if Is_Type (Ent)
2777 and then Has_Completion_In_Body (Ent)
2778 then
2779 Set_Full_View (Ent, Empty);
2781 elsif Is_Subprogram (Ent) then
2782 Set_Is_Inlined (Ent, False);
2783 end if;
2785 Next_Entity (Ent);
2786 end loop;
2788 return;
2789 end if;
2791 Next (With_Clause);
2792 end loop;
2793 end if;
2794 end;
2796 elsif Ineffective_Inline_Warnings then
2797 Error_Msg_Unit_1 := Bname;
2798 Error_Msg_N
2799 ("unable to inline subprograms defined in $?p?", P);
2800 Error_Msg_N ("\body not found?p?", P);
2801 return;
2802 end if;
2803 end if;
2805 return;
2806 end if;
2808 Next_Entity (E);
2809 end loop;
2810 end if;
2811 end Check_Package_Body_For_Inlining;
2813 --------------------
2814 -- Cleanup_Scopes --
2815 --------------------
2817 procedure Cleanup_Scopes is
2818 Elmt : Elmt_Id;
2819 Decl : Node_Id;
2820 Scop : Entity_Id;
2822 begin
2823 Elmt := First_Elmt (To_Clean);
2824 while Present (Elmt) loop
2825 Scop := Node (Elmt);
2827 if Ekind (Scop) = E_Entry then
2828 Scop := Protected_Body_Subprogram (Scop);
2830 elsif Is_Subprogram (Scop)
2831 and then Is_Protected_Type (Underlying_Type (Scope (Scop)))
2832 and then Present (Protected_Body_Subprogram (Scop))
2833 then
2834 -- If a protected operation contains an instance, its cleanup
2835 -- operations have been delayed, and the subprogram has been
2836 -- rewritten in the expansion of the enclosing protected body. It
2837 -- is the corresponding subprogram that may require the cleanup
2838 -- operations, so propagate the information that triggers cleanup
2839 -- activity.
2841 Set_Uses_Sec_Stack
2842 (Protected_Body_Subprogram (Scop),
2843 Uses_Sec_Stack (Scop));
2845 Scop := Protected_Body_Subprogram (Scop);
2846 end if;
2848 if Ekind (Scop) = E_Block then
2849 Decl := Parent (Block_Node (Scop));
2851 else
2852 Decl := Unit_Declaration_Node (Scop);
2854 if Nkind (Decl) in N_Subprogram_Declaration
2855 | N_Task_Type_Declaration
2856 | N_Subprogram_Body_Stub
2857 then
2858 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2859 end if;
2860 end if;
2862 Push_Scope (Scop);
2863 Expand_Cleanup_Actions (Decl);
2864 End_Scope;
2866 Next_Elmt (Elmt);
2867 end loop;
2868 end Cleanup_Scopes;
2870 procedure Establish_Actual_Mapping_For_Inlined_Call
2871 (N : Node_Id;
2872 Subp : Entity_Id;
2873 Decls : List_Id;
2874 Body_Or_Expr_To_Check : Node_Id)
2877 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2878 -- Determine whether a formal parameter is used only once in
2879 -- Body_Or_Expr_To_Check.
2881 -------------------------
2882 -- Formal_Is_Used_Once --
2883 -------------------------
2885 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2886 Use_Counter : Nat := 0;
2888 function Count_Uses (N : Node_Id) return Traverse_Result;
2889 -- Traverse the tree and count the uses of the formal parameter.
2890 -- In this case, for optimization purposes, we do not need to
2891 -- continue the traversal once more than one use is encountered.
2893 ----------------
2894 -- Count_Uses --
2895 ----------------
2897 function Count_Uses (N : Node_Id) return Traverse_Result is
2898 begin
2899 -- The original node is an identifier
2901 if Nkind (N) = N_Identifier
2902 and then Present (Entity (N))
2904 -- Original node's entity points to the one in the copied body
2906 and then Nkind (Entity (N)) = N_Identifier
2907 and then Present (Entity (Entity (N)))
2909 -- The entity of the copied node is the formal parameter
2911 and then Entity (Entity (N)) = Formal
2912 then
2913 Use_Counter := Use_Counter + 1;
2915 -- If this is a second use then abandon the traversal
2917 if Use_Counter > 1 then
2918 return Abandon;
2919 end if;
2920 end if;
2922 return OK;
2923 end Count_Uses;
2925 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2927 -- Start of processing for Formal_Is_Used_Once
2929 begin
2930 Count_Formal_Uses (Body_Or_Expr_To_Check);
2931 return Use_Counter = 1;
2932 end Formal_Is_Used_Once;
2934 -- Local Data --
2936 F : Entity_Id;
2937 A : Node_Id;
2938 Decl : Node_Id;
2939 Loc : constant Source_Ptr := Sloc (N);
2940 New_A : Node_Id;
2941 Temp : Entity_Id;
2942 Temp_Typ : Entity_Id;
2944 -- Start of processing for Establish_Actual_Mapping_For_Inlined_Call
2946 begin
2947 F := First_Formal (Subp);
2948 A := First_Actual (N);
2949 while Present (F) loop
2950 if Present (Renamed_Object (F)) then
2952 -- If expander is active, it is an error to try to inline a
2953 -- recursive subprogram. In GNATprove mode, just indicate that the
2954 -- inlining will not happen, and mark the subprogram as not always
2955 -- inlined.
2957 if GNATprove_Mode then
2958 Cannot_Inline
2959 ("cannot inline call to recursive subprogram?", N, Subp);
2960 Set_Is_Inlined_Always (Subp, False);
2961 else
2962 Error_Msg_N
2963 ("cannot inline call to recursive subprogram", N);
2964 end if;
2966 return;
2967 end if;
2969 -- Reset Last_Assignment for any parameters of mode out or in out, to
2970 -- prevent spurious warnings about overwriting for assignments to the
2971 -- formal in the inlined code.
2973 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
2975 -- In GNATprove mode a protected component acting as an actual
2976 -- subprogram parameter will appear as inlined-for-proof. However,
2977 -- its E_Component entity is not an assignable object, so the
2978 -- assertion in Set_Last_Assignment will fail. We just omit the
2979 -- call to Set_Last_Assignment, because GNATprove flags useless
2980 -- assignments with its own flow analysis.
2982 -- In GNAT mode such a problem does not occur, because protected
2983 -- components are inlined via object renamings whose entity kind
2984 -- E_Variable is assignable.
2986 if Is_Assignable (Entity (A)) then
2987 Set_Last_Assignment (Entity (A), Empty);
2988 else
2989 pragma Assert
2990 (GNATprove_Mode and then Is_Protected_Component (Entity (A)));
2991 end if;
2992 end if;
2994 -- If the argument may be a controlling argument in a call within
2995 -- the inlined body, we must preserve its class-wide nature to ensure
2996 -- that dynamic dispatching will take place subsequently. If the
2997 -- formal has a constraint, then it must be preserved to retain the
2998 -- semantics of the body.
3000 if Is_Class_Wide_Type (Etype (F))
3001 or else (Is_Access_Type (Etype (F))
3002 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3003 then
3004 Temp_Typ := Etype (F);
3006 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3007 and then Etype (F) /= Base_Type (Etype (F))
3008 and then Is_Constrained (Etype (F))
3009 then
3010 Temp_Typ := Etype (F);
3012 else
3013 Temp_Typ := Etype (A);
3014 end if;
3016 -- If the actual is a simple name or a literal, no need to create a
3017 -- temporary, object can be used directly. Skip this optimization in
3018 -- GNATprove mode, to make sure any check on a type conversion will
3019 -- be issued.
3021 if (Is_Entity_Name (A)
3022 and then
3023 (not Is_Scalar_Type (Etype (A))
3024 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3025 and then not GNATprove_Mode)
3027 -- When the actual is an identifier and the corresponding formal is
3028 -- used only once in the original body, the formal can be substituted
3029 -- directly with the actual parameter. Skip this optimization in
3030 -- GNATprove mode, to make sure any check on a type conversion
3031 -- will be issued.
3033 or else
3034 (Nkind (A) = N_Identifier
3035 and then Formal_Is_Used_Once (F)
3036 and then not GNATprove_Mode)
3038 -- If the actual is a literal and the formal has its address taken,
3039 -- we cannot pass the literal itself as an argument, so its value
3040 -- must be captured in a temporary.
3042 or else
3043 (Nkind (A) in
3044 N_Real_Literal | N_Integer_Literal | N_Character_Literal
3045 and then not Address_Taken (F))
3046 then
3047 if Etype (F) /= Etype (A) then
3048 Set_Renamed_Object
3049 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3050 else
3051 Set_Renamed_Object (F, A);
3052 end if;
3054 else
3055 Temp := Make_Temporary (Loc, 'C');
3057 -- If the actual for an in/in-out parameter is a view conversion,
3058 -- make it into an unchecked conversion, given that an untagged
3059 -- type conversion is not a proper object for a renaming.
3061 -- In-out conversions that involve real conversions have already
3062 -- been transformed in Expand_Actuals.
3064 if Nkind (A) = N_Type_Conversion
3065 and then Ekind (F) /= E_In_Parameter
3066 then
3067 New_A := Unchecked_Convert_To (Etype (F), Expression (A));
3069 -- In GNATprove mode, keep the most precise type of the actual for
3070 -- the temporary variable, when the formal type is unconstrained.
3071 -- Otherwise, the AST may contain unexpected assignment statements
3072 -- to a temporary variable of unconstrained type renaming a local
3073 -- variable of constrained type, which is not expected by
3074 -- GNATprove.
3076 elsif Etype (F) /= Etype (A)
3077 and then (not GNATprove_Mode or else Is_Constrained (Etype (F)))
3078 then
3079 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3080 Temp_Typ := Etype (F);
3082 else
3083 New_A := Relocate_Node (A);
3084 end if;
3086 Set_Sloc (New_A, Sloc (N));
3088 -- If the actual has a by-reference type, it cannot be copied,
3089 -- so its value is captured in a renaming declaration. Otherwise
3090 -- declare a local constant initialized with the actual.
3092 -- We also use a renaming declaration for expressions of an array
3093 -- type that is not bit-packed, both for efficiency reasons and to
3094 -- respect the semantics of the call: in most cases the original
3095 -- call will pass the parameter by reference, and thus the inlined
3096 -- code will have the same semantics.
3098 -- Finally, we need a renaming declaration in the case of limited
3099 -- types for which initialization cannot be by copy either.
3101 if Ekind (F) = E_In_Parameter
3102 and then not Is_By_Reference_Type (Etype (A))
3103 and then not Is_Limited_Type (Etype (A))
3104 and then
3105 (not Is_Array_Type (Etype (A))
3106 or else not Is_Object_Reference (A)
3107 or else Is_Bit_Packed_Array (Etype (A)))
3108 then
3109 Decl :=
3110 Make_Object_Declaration (Loc,
3111 Defining_Identifier => Temp,
3112 Constant_Present => True,
3113 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3114 Expression => New_A);
3116 else
3117 -- In GNATprove mode, make an explicit copy of input
3118 -- parameters when formal and actual types differ, to make
3119 -- sure any check on the type conversion will be issued.
3120 -- The legality of the copy is ensured by calling first
3121 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3123 if GNATprove_Mode
3124 and then Ekind (F) /= E_Out_Parameter
3125 and then not Same_Type (Etype (F), Etype (A))
3126 then
3127 pragma Assert (not Is_By_Reference_Type (Etype (A)));
3128 pragma Assert (not Is_Limited_Type (Etype (A)));
3130 Append_To (Decls,
3131 Make_Object_Declaration (Loc,
3132 Defining_Identifier => Make_Temporary (Loc, 'C'),
3133 Constant_Present => True,
3134 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3135 Expression => New_Copy_Tree (New_A)));
3136 end if;
3138 Decl :=
3139 Make_Object_Renaming_Declaration (Loc,
3140 Defining_Identifier => Temp,
3141 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3142 Name => New_A);
3143 end if;
3145 Append (Decl, Decls);
3146 Set_Renamed_Object (F, Temp);
3147 end if;
3149 Next_Formal (F);
3150 Next_Actual (A);
3151 end loop;
3152 end Establish_Actual_Mapping_For_Inlined_Call;
3154 -------------------------
3155 -- Expand_Inlined_Call --
3156 -------------------------
3158 procedure Expand_Inlined_Call
3159 (N : Node_Id;
3160 Subp : Entity_Id;
3161 Orig_Subp : Entity_Id)
3163 Decls : constant List_Id := New_List;
3164 Is_Predef : constant Boolean :=
3165 Is_Predefined_Unit (Get_Source_Unit (Subp));
3166 Loc : constant Source_Ptr := Sloc (N);
3167 Orig_Bod : constant Node_Id :=
3168 Body_To_Inline (Unit_Declaration_Node (Subp));
3170 Uses_Back_End : constant Boolean :=
3171 Back_End_Inlining and then Optimization_Level > 0;
3172 -- The back-end expansion is used if the target supports back-end
3173 -- inlining and some level of optimixation is required; otherwise
3174 -- the inlining takes place fully as a tree expansion.
3176 Blk : Node_Id;
3177 Decl : Node_Id;
3178 Exit_Lab : Entity_Id := Empty;
3179 Lab_Decl : Node_Id := Empty;
3180 Lab_Id : Node_Id;
3181 Num_Ret : Nat := 0;
3182 Ret_Type : Entity_Id;
3183 Temp : Entity_Id;
3185 Is_Unc : Boolean;
3186 Is_Unc_Decl : Boolean;
3187 -- If the type returned by the function is unconstrained and the call
3188 -- can be inlined, special processing is required.
3190 Return_Object : Entity_Id := Empty;
3191 -- Entity in declaration in an extended_return_statement
3193 Targ : Node_Id := Empty;
3194 -- The target of the call. If context is an assignment statement then
3195 -- this is the left-hand side of the assignment, else it is a temporary
3196 -- to which the return value is assigned prior to rewriting the call.
3198 Targ1 : Node_Id := Empty;
3199 -- A separate target used when the return type is unconstrained
3201 procedure Declare_Postconditions_Result;
3202 -- When generating C code, declare _Result, which may be used in the
3203 -- inlined _Postconditions procedure to verify the return value.
3205 procedure Make_Exit_Label;
3206 -- Build declaration for exit label to be used in Return statements,
3207 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3208 -- declaration). Does nothing if Exit_Lab already set.
3210 procedure Make_Loop_Labels_Unique (HSS : Node_Id);
3211 -- When compiling for CCG and performing front-end inlining, replace
3212 -- loop names and references to them so that they do not conflict with
3213 -- homographs in the current subprogram.
3215 function Process_Formals (N : Node_Id) return Traverse_Result;
3216 -- Replace occurrence of a formal with the corresponding actual, or the
3217 -- thunk generated for it. Replace a return statement with an assignment
3218 -- to the target of the call, with appropriate conversions if needed.
3220 function Process_Formals_In_Aspects (N : Node_Id) return Traverse_Result;
3221 -- Because aspects are linked indirectly to the rest of the tree,
3222 -- replacement of formals appearing in aspect specifications must
3223 -- be performed in a separate pass, using an instantiation of the
3224 -- previous subprogram over aspect specifications reachable from N.
3226 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3227 -- If the call being expanded is that of an internal subprogram, set the
3228 -- sloc of the generated block to that of the call itself, so that the
3229 -- expansion is skipped by the "next" command in gdb. Same processing
3230 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
3231 -- Debug_Generated_Code is true, suppress this change to simplify our
3232 -- own development. Same in GNATprove mode, to ensure that warnings and
3233 -- diagnostics point to the proper location.
3235 procedure Reset_Dispatching_Calls (N : Node_Id);
3236 -- In subtree N search for occurrences of dispatching calls that use the
3237 -- Ada 2005 Object.Operation notation and the object is a formal of the
3238 -- inlined subprogram. Reset the entity associated with Operation in all
3239 -- the found occurrences.
3241 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3242 -- If the function body is a single expression, replace call with
3243 -- expression, else insert block appropriately.
3245 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3246 -- If procedure body has no local variables, inline body without
3247 -- creating block, otherwise rewrite call with block.
3249 -----------------------------------
3250 -- Declare_Postconditions_Result --
3251 -----------------------------------
3253 procedure Declare_Postconditions_Result is
3254 Enclosing_Subp : constant Entity_Id := Scope (Subp);
3256 begin
3257 pragma Assert
3258 (Modify_Tree_For_C
3259 and then Is_Subprogram (Enclosing_Subp)
3260 and then Present (Wrapped_Statements (Enclosing_Subp)));
3262 if Ekind (Enclosing_Subp) = E_Function then
3263 if Nkind (First (Parameter_Associations (N))) in
3264 N_Numeric_Or_String_Literal
3265 then
3266 Append_To (Declarations (Blk),
3267 Make_Object_Declaration (Loc,
3268 Defining_Identifier =>
3269 Make_Defining_Identifier (Loc, Name_uResult),
3270 Constant_Present => True,
3271 Object_Definition =>
3272 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
3273 Expression =>
3274 New_Copy_Tree (First (Parameter_Associations (N)))));
3275 else
3276 Append_To (Declarations (Blk),
3277 Make_Object_Renaming_Declaration (Loc,
3278 Defining_Identifier =>
3279 Make_Defining_Identifier (Loc, Name_uResult),
3280 Subtype_Mark =>
3281 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
3282 Name =>
3283 New_Copy_Tree (First (Parameter_Associations (N)))));
3284 end if;
3285 end if;
3286 end Declare_Postconditions_Result;
3288 ---------------------
3289 -- Make_Exit_Label --
3290 ---------------------
3292 procedure Make_Exit_Label is
3293 Lab_Ent : Entity_Id;
3294 begin
3295 if No (Exit_Lab) then
3296 Lab_Ent := Make_Temporary (Loc, 'L');
3297 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
3298 Exit_Lab := Make_Label (Loc, Lab_Id);
3299 Lab_Decl :=
3300 Make_Implicit_Label_Declaration (Loc,
3301 Defining_Identifier => Lab_Ent,
3302 Label_Construct => Exit_Lab);
3303 end if;
3304 end Make_Exit_Label;
3306 -----------------------------
3307 -- Make_Loop_Labels_Unique --
3308 -----------------------------
3310 procedure Make_Loop_Labels_Unique (HSS : Node_Id) is
3311 function Process_Loop (N : Node_Id) return Traverse_Result;
3313 ------------------
3314 -- Process_Loop --
3315 ------------------
3317 function Process_Loop (N : Node_Id) return Traverse_Result is
3318 Id : Entity_Id;
3320 begin
3321 if Nkind (N) = N_Loop_Statement
3322 and then Present (Identifier (N))
3323 then
3324 -- Create new external name for loop and update the
3325 -- corresponding entity.
3327 Id := Entity (Identifier (N));
3328 Set_Chars (Id, New_External_Name (Chars (Id), 'L', -1));
3329 Set_Chars (Identifier (N), Chars (Id));
3331 elsif Nkind (N) = N_Exit_Statement
3332 and then Present (Name (N))
3333 then
3334 -- The exit statement must name an enclosing loop, whose name
3335 -- has already been updated.
3337 Set_Chars (Name (N), Chars (Entity (Name (N))));
3338 end if;
3340 return OK;
3341 end Process_Loop;
3343 procedure Update_Loop_Names is new Traverse_Proc (Process_Loop);
3345 -- Local variables
3347 Stmt : Node_Id;
3349 -- Start of processing for Make_Loop_Labels_Unique
3351 begin
3352 if Modify_Tree_For_C then
3353 Stmt := First (Statements (HSS));
3354 while Present (Stmt) loop
3355 Update_Loop_Names (Stmt);
3356 Next (Stmt);
3357 end loop;
3358 end if;
3359 end Make_Loop_Labels_Unique;
3361 ---------------------
3362 -- Process_Formals --
3363 ---------------------
3365 function Process_Formals (N : Node_Id) return Traverse_Result is
3366 A : Entity_Id;
3367 E : Entity_Id;
3368 Ret : Node_Id;
3370 Had_Private_View : Boolean;
3372 begin
3373 if Is_Entity_Name (N) and then Present (Entity (N)) then
3374 E := Entity (N);
3376 if Is_Formal (E) and then Scope (E) = Subp then
3377 A := Renamed_Object (E);
3379 -- Rewrite the occurrence of the formal into an occurrence of
3380 -- the actual. Also establish visibility on the proper view of
3381 -- the actual's subtype for the body's context (if the actual's
3382 -- subtype is private at the call point but its full view is
3383 -- visible to the body, then the inlined tree here must be
3384 -- analyzed with the full view).
3386 -- The Has_Private_View flag is cleared by rewriting, so it
3387 -- must be explicitly saved and restored, just like when
3388 -- instantiating the body to inline.
3390 if Is_Entity_Name (A) then
3391 Had_Private_View := Has_Private_View (N);
3392 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
3393 Set_Has_Private_View (N, Had_Private_View);
3394 Check_Private_View (N);
3396 elsif Nkind (A) = N_Defining_Identifier then
3397 Had_Private_View := Has_Private_View (N);
3398 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
3399 Set_Has_Private_View (N, Had_Private_View);
3400 Check_Private_View (N);
3402 -- Numeric literal
3404 else
3405 Rewrite (N, New_Copy (A));
3406 end if;
3407 end if;
3409 return Skip;
3411 elsif Is_Entity_Name (N)
3412 and then Present (Return_Object)
3413 and then Chars (N) = Chars (Return_Object)
3414 then
3415 -- Occurrence within an extended return statement. The return
3416 -- object is local to the body been inlined, and thus the generic
3417 -- copy is not analyzed yet, so we match by name, and replace it
3418 -- with target of call.
3420 if Nkind (Targ) = N_Defining_Identifier then
3421 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3422 else
3423 Rewrite (N, New_Copy_Tree (Targ));
3424 end if;
3426 return Skip;
3428 elsif Nkind (N) = N_Simple_Return_Statement then
3429 if No (Expression (N)) then
3430 Num_Ret := Num_Ret + 1;
3431 Make_Exit_Label;
3432 Rewrite (N,
3433 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3435 else
3436 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3437 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3438 then
3439 -- Function body is a single expression. No need for
3440 -- exit label.
3442 null;
3444 else
3445 Num_Ret := Num_Ret + 1;
3446 Make_Exit_Label;
3447 end if;
3449 -- Because of the presence of private types, the views of the
3450 -- expression and the context may be different, so place
3451 -- a type conversion to the context type to avoid spurious
3452 -- errors, e.g. when the expression is a numeric literal and
3453 -- the context is private. If the expression is an aggregate,
3454 -- use a qualified expression, because an aggregate is not a
3455 -- legal argument of a conversion. Ditto for numeric, character
3456 -- and string literals, and attributes that yield a universal
3457 -- type, because those must be resolved to a specific type.
3459 if Nkind (Expression (N)) in N_Aggregate
3460 | N_Character_Literal
3461 | N_Null
3462 | N_String_Literal
3463 or else Yields_Universal_Type (Expression (N))
3464 then
3465 Ret :=
3466 Make_Qualified_Expression (Sloc (N),
3467 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3468 Expression => Relocate_Node (Expression (N)));
3470 -- Use an unchecked type conversion between access types, for
3471 -- which a type conversion would not always be valid, as no
3472 -- check may result from the conversion.
3474 elsif Is_Access_Type (Ret_Type) then
3475 Ret :=
3476 Unchecked_Convert_To
3477 (Ret_Type, Relocate_Node (Expression (N)));
3479 -- Otherwise use a type conversion, which may trigger a check
3481 else
3482 Ret :=
3483 Make_Type_Conversion (Sloc (N),
3484 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3485 Expression => Relocate_Node (Expression (N)));
3486 end if;
3488 if Nkind (Targ) = N_Defining_Identifier then
3489 Rewrite (N,
3490 Make_Assignment_Statement (Loc,
3491 Name => New_Occurrence_Of (Targ, Loc),
3492 Expression => Ret));
3493 else
3494 Rewrite (N,
3495 Make_Assignment_Statement (Loc,
3496 Name => New_Copy (Targ),
3497 Expression => Ret));
3498 end if;
3500 Set_Assignment_OK (Name (N));
3502 if Present (Exit_Lab) then
3503 Insert_After (N,
3504 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3505 end if;
3506 end if;
3508 return OK;
3510 -- An extended return becomes a block whose first statement is the
3511 -- assignment of the initial expression of the return object to the
3512 -- target of the call itself.
3514 elsif Nkind (N) = N_Extended_Return_Statement then
3515 declare
3516 Return_Decl : constant Entity_Id :=
3517 First (Return_Object_Declarations (N));
3518 Assign : Node_Id;
3520 begin
3521 Return_Object := Defining_Identifier (Return_Decl);
3523 if Present (Expression (Return_Decl)) then
3524 if Nkind (Targ) = N_Defining_Identifier then
3525 Assign :=
3526 Make_Assignment_Statement (Loc,
3527 Name => New_Occurrence_Of (Targ, Loc),
3528 Expression => Expression (Return_Decl));
3529 else
3530 Assign :=
3531 Make_Assignment_Statement (Loc,
3532 Name => New_Copy (Targ),
3533 Expression => Expression (Return_Decl));
3534 end if;
3536 Set_Assignment_OK (Name (Assign));
3538 if No (Handled_Statement_Sequence (N)) then
3539 Set_Handled_Statement_Sequence (N,
3540 Make_Handled_Sequence_Of_Statements (Loc,
3541 Statements => New_List));
3542 end if;
3544 Prepend (Assign,
3545 Statements (Handled_Statement_Sequence (N)));
3546 end if;
3548 Rewrite (N,
3549 Make_Block_Statement (Loc,
3550 Handled_Statement_Sequence =>
3551 Handled_Statement_Sequence (N)));
3553 return OK;
3554 end;
3556 -- Remove pragma Unreferenced since it may refer to formals that
3557 -- are not visible in the inlined body, and in any case we will
3558 -- not be posting warnings on the inlined body so it is unneeded.
3560 elsif Nkind (N) = N_Pragma
3561 and then Pragma_Name (N) = Name_Unreferenced
3562 then
3563 Rewrite (N, Make_Null_Statement (Sloc (N)));
3564 return OK;
3566 else
3567 return OK;
3568 end if;
3569 end Process_Formals;
3571 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3573 --------------------------------
3574 -- Process_Formals_In_Aspects --
3575 --------------------------------
3577 function Process_Formals_In_Aspects
3578 (N : Node_Id) return Traverse_Result
3580 A : Node_Id;
3582 begin
3583 if Has_Aspects (N) then
3584 A := First (Aspect_Specifications (N));
3585 while Present (A) loop
3586 Replace_Formals (Expression (A));
3588 Next (A);
3589 end loop;
3590 end if;
3591 return OK;
3592 end Process_Formals_In_Aspects;
3594 procedure Replace_Formals_In_Aspects is
3595 new Traverse_Proc (Process_Formals_In_Aspects);
3597 ------------------
3598 -- Process_Sloc --
3599 ------------------
3601 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3602 begin
3603 if not Debug_Generated_Code then
3604 Set_Sloc (Nod, Sloc (N));
3605 Set_Comes_From_Source (Nod, False);
3606 end if;
3608 return OK;
3609 end Process_Sloc;
3611 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3613 ------------------------------
3614 -- Reset_Dispatching_Calls --
3615 ------------------------------
3617 procedure Reset_Dispatching_Calls (N : Node_Id) is
3619 function Do_Reset (N : Node_Id) return Traverse_Result;
3621 --------------
3622 -- Do_Reset --
3623 --------------
3625 function Do_Reset (N : Node_Id) return Traverse_Result is
3626 begin
3627 if Nkind (N) = N_Procedure_Call_Statement
3628 and then Nkind (Name (N)) = N_Selected_Component
3629 and then Nkind (Prefix (Name (N))) = N_Identifier
3630 and then Is_Formal (Entity (Prefix (Name (N))))
3631 and then Is_Dispatching_Operation
3632 (Entity (Selector_Name (Name (N))))
3633 then
3634 Set_Entity (Selector_Name (Name (N)), Empty);
3635 end if;
3637 return OK;
3638 end Do_Reset;
3640 procedure Do_Reset_Calls is new Traverse_Proc (Do_Reset);
3642 begin
3643 Do_Reset_Calls (N);
3644 end Reset_Dispatching_Calls;
3646 ---------------------------
3647 -- Rewrite_Function_Call --
3648 ---------------------------
3650 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3651 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3652 Fst : constant Node_Id := First (Statements (HSS));
3654 begin
3655 Make_Loop_Labels_Unique (HSS);
3657 -- Optimize simple case: function body is a single return statement,
3658 -- which has been expanded into an assignment.
3660 if Is_Empty_List (Declarations (Blk))
3661 and then Nkind (Fst) = N_Assignment_Statement
3662 and then No (Next (Fst))
3663 then
3664 -- The function call may have been rewritten as the temporary
3665 -- that holds the result of the call, in which case remove the
3666 -- now useless declaration.
3668 if Nkind (N) = N_Identifier
3669 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3670 then
3671 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3672 end if;
3674 Rewrite (N, Expression (Fst));
3676 elsif Nkind (N) = N_Identifier
3677 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3678 then
3679 -- The block assigns the result of the call to the temporary
3681 Insert_After (Parent (Entity (N)), Blk);
3683 -- If the context is an assignment, and the left-hand side is free of
3684 -- side-effects, the replacement is also safe.
3686 elsif Nkind (Parent (N)) = N_Assignment_Statement
3687 and then
3688 (Is_Entity_Name (Name (Parent (N)))
3689 or else
3690 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3691 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
3693 or else
3694 (Nkind (Name (Parent (N))) = N_Selected_Component
3695 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3696 then
3697 -- Replace assignment with the block
3699 declare
3700 Original_Assignment : constant Node_Id := Parent (N);
3702 begin
3703 -- Preserve the original assignment node to keep the complete
3704 -- assignment subtree consistent enough for Analyze_Assignment
3705 -- to proceed (specifically, the original Lhs node must still
3706 -- have an assignment statement as its parent).
3708 -- We cannot rely on Original_Node to go back from the block
3709 -- node to the assignment node, because the assignment might
3710 -- already be a rewrite substitution.
3712 Discard_Node (Relocate_Node (Original_Assignment));
3713 Rewrite (Original_Assignment, Blk);
3714 end;
3716 elsif Nkind (Parent (N)) = N_Object_Declaration then
3718 -- A call to a function which returns an unconstrained type
3719 -- found in the expression initializing an object-declaration is
3720 -- expanded into a procedure call which must be added after the
3721 -- object declaration.
3723 if Is_Unc_Decl and Back_End_Inlining then
3724 Insert_Action_After (Parent (N), Blk);
3725 else
3726 Set_Expression (Parent (N), Empty);
3727 Insert_After (Parent (N), Blk);
3728 end if;
3730 elsif Is_Unc and then not Back_End_Inlining then
3731 Insert_Before (Parent (N), Blk);
3732 end if;
3733 end Rewrite_Function_Call;
3735 ----------------------------
3736 -- Rewrite_Procedure_Call --
3737 ----------------------------
3739 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3740 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3742 begin
3743 Make_Loop_Labels_Unique (HSS);
3745 -- If there is a transient scope for N, this will be the scope of the
3746 -- actions for N, and the statements in Blk need to be within this
3747 -- scope. For example, they need to have visibility on the constant
3748 -- declarations created for the formals.
3750 -- If N needs no transient scope, and if there are no declarations in
3751 -- the inlined body, we can do a little optimization and insert the
3752 -- statements for the body directly after N, and rewrite N to a
3753 -- null statement, instead of rewriting N into a full-blown block
3754 -- statement.
3756 if not Scope_Is_Transient
3757 and then Is_Empty_List (Declarations (Blk))
3758 then
3759 Insert_List_After (N, Statements (HSS));
3760 Rewrite (N, Make_Null_Statement (Loc));
3761 else
3762 Rewrite (N, Blk);
3763 end if;
3764 end Rewrite_Procedure_Call;
3766 -- Start of processing for Expand_Inlined_Call
3768 begin
3769 -- Initializations for old/new semantics
3771 if not Uses_Back_End then
3772 Is_Unc := Is_Array_Type (Etype (Subp))
3773 and then not Is_Constrained (Etype (Subp));
3774 Is_Unc_Decl := False;
3775 else
3776 Is_Unc := Returns_Unconstrained_Type (Subp)
3777 and then Optimization_Level > 0;
3778 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
3779 and then Is_Unc;
3780 end if;
3782 -- Check for an illegal attempt to inline a recursive procedure. If the
3783 -- subprogram has parameters this is detected when trying to supply a
3784 -- binding for parameters that already have one. For parameterless
3785 -- subprograms this must be done explicitly.
3787 if In_Open_Scopes (Subp) then
3788 Cannot_Inline
3789 ("cannot inline call to recursive subprogram?", N, Subp);
3790 Set_Is_Inlined (Subp, False);
3791 return;
3793 -- Skip inlining if this is not a true inlining since the attribute
3794 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3795 -- true inlining, Orig_Bod has code rather than being an entity.
3797 elsif Nkind (Orig_Bod) in N_Entity then
3798 return;
3799 end if;
3801 if Nkind (Orig_Bod) in N_Defining_Identifier
3802 | N_Defining_Operator_Symbol
3803 then
3804 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3805 -- can be replaced with calls to the renamed entity directly, because
3806 -- the subprograms are subtype conformant. If the renamed subprogram
3807 -- is an inherited operation, we must redo the expansion because
3808 -- implicit conversions may be needed. Similarly, if the renamed
3809 -- entity is inlined, expand the call for further optimizations.
3811 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3813 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3814 Expand_Call (N);
3815 end if;
3817 return;
3818 end if;
3820 -- Register the call in the list of inlined calls
3822 Append_New_Elmt (N, To => Inlined_Calls);
3824 -- Use generic machinery to copy body of inlined subprogram, as if it
3825 -- were an instantiation, resetting source locations appropriately, so
3826 -- that nested inlined calls appear in the main unit.
3828 Save_Env (Subp, Empty);
3829 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3831 -- Old semantics
3833 if not Uses_Back_End then
3834 declare
3835 Bod : Node_Id;
3837 begin
3838 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3839 Blk :=
3840 Make_Block_Statement (Loc,
3841 Declarations => Declarations (Bod),
3842 Handled_Statement_Sequence =>
3843 Handled_Statement_Sequence (Bod));
3845 if No (Declarations (Bod)) then
3846 Set_Declarations (Blk, New_List);
3847 end if;
3849 -- When generating C code, declare _Result, which may be used to
3850 -- verify the return value.
3852 if Modify_Tree_For_C
3853 and then Nkind (N) = N_Procedure_Call_Statement
3854 and then Chars (Name (N)) = Name_uWrapped_Statements
3855 then
3856 Declare_Postconditions_Result;
3857 end if;
3859 -- For the unconstrained case, capture the name of the local
3860 -- variable that holds the result. This must be the first
3861 -- declaration in the block, because its bounds cannot depend
3862 -- on local variables. Otherwise there is no way to declare the
3863 -- result outside of the block. Needless to say, in general the
3864 -- bounds will depend on the actuals in the call.
3866 -- If the context is an assignment statement, as is the case
3867 -- for the expansion of an extended return, the left-hand side
3868 -- provides bounds even if the return type is unconstrained.
3870 if Is_Unc then
3871 declare
3872 First_Decl : Node_Id;
3874 begin
3875 First_Decl := First (Declarations (Blk));
3877 -- If the body is a single extended return statement,the
3878 -- resulting block is a nested block.
3880 if No (First_Decl) then
3881 First_Decl :=
3882 First (Statements (Handled_Statement_Sequence (Blk)));
3884 if Nkind (First_Decl) = N_Block_Statement then
3885 First_Decl := First (Declarations (First_Decl));
3886 end if;
3887 end if;
3889 -- No front-end inlining possible
3891 if Nkind (First_Decl) /= N_Object_Declaration then
3892 return;
3893 end if;
3895 if Nkind (Parent (N)) /= N_Assignment_Statement then
3896 Targ1 := Defining_Identifier (First_Decl);
3897 else
3898 Targ1 := Name (Parent (N));
3899 end if;
3900 end;
3901 end if;
3902 end;
3904 -- New semantics
3906 else
3907 declare
3908 Bod : Node_Id;
3910 begin
3911 -- General case
3913 if not Is_Unc then
3914 Bod :=
3915 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3916 Blk :=
3917 Make_Block_Statement (Loc,
3918 Declarations => Declarations (Bod),
3919 Handled_Statement_Sequence =>
3920 Handled_Statement_Sequence (Bod));
3922 -- Inline a call to a function that returns an unconstrained type.
3923 -- The semantic analyzer checked that frontend-inlined functions
3924 -- returning unconstrained types have no declarations and have
3925 -- a single extended return statement. As part of its processing
3926 -- the function was split into two subprograms: a procedure P' and
3927 -- a function F' that has a block with a call to procedure P' (see
3928 -- Split_Unconstrained_Function).
3930 else
3931 pragma Assert
3932 (Nkind
3933 (First
3934 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3935 N_Block_Statement);
3937 declare
3938 Blk_Stmt : constant Node_Id :=
3939 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
3940 First_Stmt : constant Node_Id :=
3941 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
3942 Second_Stmt : constant Node_Id := Next (First_Stmt);
3944 begin
3945 pragma Assert
3946 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3947 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3948 and then No (Next (Second_Stmt)));
3950 Bod :=
3951 Copy_Generic_Node
3952 (First
3953 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3954 Empty, Instantiating => True);
3955 Blk := Bod;
3957 -- Capture the name of the local variable that holds the
3958 -- result. This must be the first declaration in the block,
3959 -- because its bounds cannot depend on local variables.
3960 -- Otherwise there is no way to declare the result outside
3961 -- of the block. Needless to say, in general the bounds will
3962 -- depend on the actuals in the call.
3964 if Nkind (Parent (N)) /= N_Assignment_Statement then
3965 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3967 -- If the context is an assignment statement, as is the case
3968 -- for the expansion of an extended return, the left-hand
3969 -- side provides bounds even if the return type is
3970 -- unconstrained.
3972 else
3973 Targ1 := Name (Parent (N));
3974 end if;
3975 end;
3976 end if;
3978 if No (Declarations (Bod)) then
3979 Set_Declarations (Blk, New_List);
3980 end if;
3981 end;
3982 end if;
3984 -- If this is a derived function, establish the proper return type
3986 if Present (Orig_Subp) and then Orig_Subp /= Subp then
3987 Ret_Type := Etype (Orig_Subp);
3988 else
3989 Ret_Type := Etype (Subp);
3990 end if;
3992 -- Create temporaries for the actuals that are expressions, or that are
3993 -- scalars and require copying to preserve semantics.
3995 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Orig_Bod);
3997 -- Establish target of function call. If context is not assignment or
3998 -- declaration, create a temporary as a target. The declaration for the
3999 -- temporary may be subsequently optimized away if the body is a single
4000 -- expression, or if the left-hand side of the assignment is simple
4001 -- enough, i.e. an entity or an explicit dereference of one.
4003 if Ekind (Subp) = E_Function then
4004 if Nkind (Parent (N)) = N_Assignment_Statement
4005 and then Is_Entity_Name (Name (Parent (N)))
4006 then
4007 Targ := Name (Parent (N));
4009 elsif Nkind (Parent (N)) = N_Assignment_Statement
4010 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4011 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4012 then
4013 Targ := Name (Parent (N));
4015 elsif Nkind (Parent (N)) = N_Assignment_Statement
4016 and then Nkind (Name (Parent (N))) = N_Selected_Component
4017 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4018 then
4019 Targ := New_Copy_Tree (Name (Parent (N)));
4021 elsif Nkind (Parent (N)) = N_Object_Declaration
4022 and then Is_Limited_Type (Etype (Subp))
4023 then
4024 Targ := Defining_Identifier (Parent (N));
4026 -- New semantics: In an object declaration avoid an extra copy
4027 -- of the result of a call to an inlined function that returns
4028 -- an unconstrained type
4030 elsif Uses_Back_End
4031 and then Nkind (Parent (N)) = N_Object_Declaration
4032 and then Is_Unc
4033 then
4034 Targ := Defining_Identifier (Parent (N));
4036 else
4037 -- Replace call with temporary and create its declaration
4039 Temp := Make_Temporary (Loc, 'C');
4040 Set_Is_Internal (Temp);
4042 -- For the unconstrained case, the generated temporary has the
4043 -- same constrained declaration as the result variable. It may
4044 -- eventually be possible to remove that temporary and use the
4045 -- result variable directly.
4047 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
4048 then
4049 Decl :=
4050 Make_Object_Declaration (Loc,
4051 Defining_Identifier => Temp,
4052 Object_Definition =>
4053 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4055 Replace_Formals (Decl);
4057 else
4058 Decl :=
4059 Make_Object_Declaration (Loc,
4060 Defining_Identifier => Temp,
4061 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
4063 Set_Etype (Temp, Ret_Type);
4064 end if;
4066 Set_No_Initialization (Decl);
4067 Append (Decl, Decls);
4068 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4069 Targ := Temp;
4070 end if;
4071 end if;
4073 Insert_Actions (N, Decls);
4075 if Is_Unc_Decl then
4077 -- Special management for inlining a call to a function that returns
4078 -- an unconstrained type and initializes an object declaration: we
4079 -- avoid generating undesired extra calls and goto statements.
4081 -- Given:
4082 -- function Func (...) return String is
4083 -- begin
4084 -- declare
4085 -- Result : String (1 .. 4);
4086 -- begin
4087 -- Proc (Result, ...);
4088 -- return Result;
4089 -- end;
4090 -- end Func;
4092 -- Result : String := Func (...);
4094 -- Replace this object declaration by:
4096 -- Result : String (1 .. 4);
4097 -- Proc (Result, ...);
4099 Remove_Homonym (Targ);
4101 Decl :=
4102 Make_Object_Declaration
4103 (Loc,
4104 Defining_Identifier => Targ,
4105 Object_Definition =>
4106 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4107 Replace_Formals (Decl);
4108 Rewrite (Parent (N), Decl);
4109 Analyze (Parent (N));
4111 -- Avoid spurious warnings since we know that this declaration is
4112 -- referenced by the procedure call.
4114 Set_Never_Set_In_Source (Targ, False);
4116 -- Remove the local declaration of the extended return stmt from the
4117 -- inlined code
4119 Remove (Parent (Targ1));
4121 -- Update the reference to the result (since we have rewriten the
4122 -- object declaration)
4124 declare
4125 Blk_Call_Stmt : Node_Id;
4127 begin
4128 -- Capture the call to the procedure
4130 Blk_Call_Stmt :=
4131 First (Statements (Handled_Statement_Sequence (Blk)));
4132 pragma Assert
4133 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
4135 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
4136 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
4137 New_Occurrence_Of (Targ, Loc));
4138 end;
4140 -- Remove the return statement
4142 pragma Assert
4143 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4144 N_Simple_Return_Statement);
4146 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4147 end if;
4149 -- Traverse the tree and replace formals with actuals or their thunks.
4150 -- Attach block to tree before analysis and rewriting.
4152 Replace_Formals (Blk);
4153 Replace_Formals_In_Aspects (Blk);
4154 Set_Parent (Blk, N);
4156 if GNATprove_Mode then
4157 null;
4159 elsif not Comes_From_Source (Subp) or else Is_Predef then
4160 Reset_Slocs (Blk);
4161 end if;
4163 if Is_Unc_Decl then
4165 -- No action needed since return statement has been already removed
4167 null;
4169 elsif Present (Exit_Lab) then
4171 -- If there's a single return statement at the end of the subprogram,
4172 -- the corresponding goto statement and the corresponding label are
4173 -- useless.
4175 if Num_Ret = 1
4176 and then
4177 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4178 N_Goto_Statement
4179 then
4180 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4181 else
4182 Append (Lab_Decl, (Declarations (Blk)));
4183 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4184 end if;
4185 end if;
4187 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
4188 -- on conflicting private views that Gigi would ignore. If this is a
4189 -- predefined unit, analyze with checks off, as is done in the non-
4190 -- inlined run-time units.
4192 declare
4193 I_Flag : constant Boolean := In_Inlined_Body;
4195 begin
4196 In_Inlined_Body := True;
4198 if Is_Predef then
4199 declare
4200 Style : constant Boolean := Style_Check;
4202 begin
4203 Style_Check := False;
4205 -- Search for dispatching calls that use the Object.Operation
4206 -- notation using an Object that is a parameter of the inlined
4207 -- function. We reset the decoration of Operation to force
4208 -- the reanalysis of the inlined dispatching call because
4209 -- the actual object has been inlined.
4211 Reset_Dispatching_Calls (Blk);
4213 -- In GNATprove mode, always consider checks on, even for
4214 -- predefined units.
4216 if GNATprove_Mode then
4217 Analyze (Blk);
4218 else
4219 Analyze (Blk, Suppress => All_Checks);
4220 end if;
4222 Style_Check := Style;
4223 end;
4225 else
4226 Analyze (Blk);
4227 end if;
4229 In_Inlined_Body := I_Flag;
4230 end;
4232 if Ekind (Subp) = E_Procedure then
4233 Rewrite_Procedure_Call (N, Blk);
4235 else
4236 Rewrite_Function_Call (N, Blk);
4238 if Is_Unc_Decl then
4239 null;
4241 -- For the unconstrained case, the replacement of the call has been
4242 -- made prior to the complete analysis of the generated declarations.
4243 -- Propagate the proper type now.
4245 elsif Is_Unc then
4246 if Nkind (N) = N_Identifier then
4247 Set_Etype (N, Etype (Entity (N)));
4248 else
4249 Set_Etype (N, Etype (Targ1));
4250 end if;
4251 end if;
4252 end if;
4254 Restore_Env;
4256 -- Cleanup mapping between formals and actuals for other expansions
4258 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4259 end Expand_Inlined_Call;
4261 --------------------------
4262 -- Get_Code_Unit_Entity --
4263 --------------------------
4265 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
4266 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
4268 begin
4269 if Ekind (Unit) = E_Package_Body then
4270 Unit := Spec_Entity (Unit);
4271 end if;
4273 return Unit;
4274 end Get_Code_Unit_Entity;
4276 ------------------------------
4277 -- Has_Excluded_Declaration --
4278 ------------------------------
4280 function Has_Excluded_Declaration
4281 (Subp : Entity_Id;
4282 Decls : List_Id) return Boolean
4284 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4285 -- Nested subprograms make a given body ineligible for inlining, but
4286 -- we make an exception for instantiations of unchecked conversion.
4287 -- The body has not been analyzed yet, so check the name, and verify
4288 -- that the visible entity with that name is the predefined unit.
4290 -----------------------------
4291 -- Is_Unchecked_Conversion --
4292 -----------------------------
4294 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4295 Id : constant Node_Id := Name (D);
4296 Conv : Entity_Id;
4298 begin
4299 if Nkind (Id) = N_Identifier
4300 and then Chars (Id) = Name_Unchecked_Conversion
4301 then
4302 Conv := Current_Entity (Id);
4304 elsif Nkind (Id) in N_Selected_Component | N_Expanded_Name
4305 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4306 then
4307 Conv := Current_Entity (Selector_Name (Id));
4308 else
4309 return False;
4310 end if;
4312 return Present (Conv)
4313 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
4314 and then Is_Intrinsic_Subprogram (Conv);
4315 end Is_Unchecked_Conversion;
4317 -- Local variables
4319 Decl : Node_Id;
4321 -- Start of processing for Has_Excluded_Declaration
4323 begin
4324 -- No action needed if the check is not needed
4326 if not Check_Inlining_Restrictions then
4327 return False;
4328 end if;
4330 Decl := First (Decls);
4331 while Present (Decl) loop
4333 -- First declarations universally excluded
4335 if Nkind (Decl) = N_Package_Declaration then
4336 Cannot_Inline
4337 ("cannot inline & (nested package declaration)?", Decl, Subp);
4338 return True;
4340 elsif Nkind (Decl) = N_Package_Instantiation then
4341 Cannot_Inline
4342 ("cannot inline & (nested package instantiation)?", Decl, Subp);
4343 return True;
4344 end if;
4346 -- Then declarations excluded only for front-end inlining
4348 if Back_End_Inlining then
4349 null;
4351 elsif Nkind (Decl) = N_Task_Type_Declaration
4352 or else Nkind (Decl) = N_Single_Task_Declaration
4353 then
4354 Cannot_Inline
4355 ("cannot inline & (nested task type declaration)?", Decl, Subp);
4356 return True;
4358 elsif Nkind (Decl) in N_Protected_Type_Declaration
4359 | N_Single_Protected_Declaration
4360 then
4361 Cannot_Inline
4362 ("cannot inline & (nested protected type declaration)?",
4363 Decl, Subp);
4364 return True;
4366 elsif Nkind (Decl) = N_Subprogram_Body then
4367 Cannot_Inline
4368 ("cannot inline & (nested subprogram)?", Decl, Subp);
4369 return True;
4371 elsif Nkind (Decl) = N_Function_Instantiation
4372 and then not Is_Unchecked_Conversion (Decl)
4373 then
4374 Cannot_Inline
4375 ("cannot inline & (nested function instantiation)?", Decl, Subp);
4376 return True;
4378 elsif Nkind (Decl) = N_Procedure_Instantiation then
4379 Cannot_Inline
4380 ("cannot inline & (nested procedure instantiation)?",
4381 Decl, Subp);
4382 return True;
4384 -- Subtype declarations with predicates will generate predicate
4385 -- functions, i.e. nested subprogram bodies, so inlining is not
4386 -- possible.
4388 elsif Nkind (Decl) = N_Subtype_Declaration then
4389 declare
4390 A : Node_Id;
4391 A_Id : Aspect_Id;
4393 begin
4394 A := First (Aspect_Specifications (Decl));
4395 while Present (A) loop
4396 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4398 if A_Id = Aspect_Predicate
4399 or else A_Id = Aspect_Static_Predicate
4400 or else A_Id = Aspect_Dynamic_Predicate
4401 then
4402 Cannot_Inline
4403 ("cannot inline & (subtype declaration with "
4404 & "predicate)?", Decl, Subp);
4405 return True;
4406 end if;
4408 Next (A);
4409 end loop;
4410 end;
4411 end if;
4413 Next (Decl);
4414 end loop;
4416 return False;
4417 end Has_Excluded_Declaration;
4419 ----------------------------
4420 -- Has_Excluded_Statement --
4421 ----------------------------
4423 function Has_Excluded_Statement
4424 (Subp : Entity_Id;
4425 Stats : List_Id) return Boolean
4427 S : Node_Id;
4428 E : Node_Id;
4430 begin
4431 -- No action needed if the check is not needed
4433 if not Check_Inlining_Restrictions then
4434 return False;
4435 end if;
4437 S := First (Stats);
4438 while Present (S) loop
4439 if Nkind (S) in N_Abort_Statement
4440 | N_Asynchronous_Select
4441 | N_Conditional_Entry_Call
4442 | N_Delay_Relative_Statement
4443 | N_Delay_Until_Statement
4444 | N_Selective_Accept
4445 | N_Timed_Entry_Call
4446 then
4447 Cannot_Inline
4448 ("cannot inline & (non-allowed statement)?", S, Subp);
4449 return True;
4451 elsif Nkind (S) = N_Block_Statement then
4452 if Has_Excluded_Declaration (Subp, Declarations (S)) then
4453 return True;
4455 elsif Present (Handled_Statement_Sequence (S)) then
4456 if not Back_End_Inlining
4457 and then
4458 Present
4459 (Exception_Handlers (Handled_Statement_Sequence (S)))
4460 then
4461 Cannot_Inline
4462 ("cannot inline& (exception handler)?",
4463 First (Exception_Handlers
4464 (Handled_Statement_Sequence (S))),
4465 Subp);
4466 return True;
4468 elsif Has_Excluded_Statement
4469 (Subp, Statements (Handled_Statement_Sequence (S)))
4470 then
4471 return True;
4472 end if;
4473 end if;
4475 elsif Nkind (S) = N_Case_Statement then
4476 E := First (Alternatives (S));
4477 while Present (E) loop
4478 if Has_Excluded_Statement (Subp, Statements (E)) then
4479 return True;
4480 end if;
4482 Next (E);
4483 end loop;
4485 elsif Nkind (S) = N_If_Statement then
4486 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
4487 return True;
4488 end if;
4490 if Present (Elsif_Parts (S)) then
4491 E := First (Elsif_Parts (S));
4492 while Present (E) loop
4493 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
4494 return True;
4495 end if;
4497 Next (E);
4498 end loop;
4499 end if;
4501 if Present (Else_Statements (S))
4502 and then Has_Excluded_Statement (Subp, Else_Statements (S))
4503 then
4504 return True;
4505 end if;
4507 elsif Nkind (S) = N_Loop_Statement
4508 and then Has_Excluded_Statement (Subp, Statements (S))
4509 then
4510 return True;
4512 elsif Nkind (S) = N_Extended_Return_Statement then
4513 if Present (Handled_Statement_Sequence (S))
4514 and then
4515 Has_Excluded_Statement
4516 (Subp, Statements (Handled_Statement_Sequence (S)))
4517 then
4518 return True;
4520 elsif not Back_End_Inlining
4521 and then Present (Handled_Statement_Sequence (S))
4522 and then
4523 Present (Exception_Handlers
4524 (Handled_Statement_Sequence (S)))
4525 then
4526 Cannot_Inline
4527 ("cannot inline& (exception handler)?",
4528 First (Exception_Handlers (Handled_Statement_Sequence (S))),
4529 Subp);
4530 return True;
4531 end if;
4532 end if;
4534 Next (S);
4535 end loop;
4537 return False;
4538 end Has_Excluded_Statement;
4540 --------------------------
4541 -- Has_Initialized_Type --
4542 --------------------------
4544 function Has_Initialized_Type (E : Entity_Id) return Boolean is
4545 E_Body : constant Node_Id := Subprogram_Body (E);
4546 Decl : Node_Id;
4548 begin
4549 if No (E_Body) then -- imported subprogram
4550 return False;
4552 else
4553 Decl := First (Declarations (E_Body));
4554 while Present (Decl) loop
4555 if Nkind (Decl) = N_Full_Type_Declaration
4556 and then Comes_From_Source (Decl)
4557 and then Present (Init_Proc (Defining_Identifier (Decl)))
4558 then
4559 return True;
4560 end if;
4562 Next (Decl);
4563 end loop;
4564 end if;
4566 return False;
4567 end Has_Initialized_Type;
4569 -----------------------
4570 -- Has_Single_Return --
4571 -----------------------
4573 function Has_Single_Return (N : Node_Id) return Boolean is
4574 Return_Statement : Node_Id := Empty;
4576 function Check_Return (N : Node_Id) return Traverse_Result;
4578 ------------------
4579 -- Check_Return --
4580 ------------------
4582 function Check_Return (N : Node_Id) return Traverse_Result is
4583 begin
4584 if Nkind (N) = N_Simple_Return_Statement then
4585 if Present (Expression (N))
4586 and then Is_Entity_Name (Expression (N))
4587 then
4588 pragma Assert (Present (Entity (Expression (N))));
4590 if No (Return_Statement) then
4591 Return_Statement := N;
4592 return OK;
4594 else
4595 pragma Assert
4596 (Present (Entity (Expression (Return_Statement))));
4598 if Entity (Expression (N)) =
4599 Entity (Expression (Return_Statement))
4600 then
4601 return OK;
4602 else
4603 return Abandon;
4604 end if;
4605 end if;
4607 -- A return statement within an extended return is a noop after
4608 -- inlining.
4610 elsif No (Expression (N))
4611 and then Nkind (Parent (Parent (N))) =
4612 N_Extended_Return_Statement
4613 then
4614 return OK;
4616 else
4617 -- Expression has wrong form
4619 return Abandon;
4620 end if;
4622 -- We can only inline a build-in-place function if it has a single
4623 -- extended return.
4625 elsif Nkind (N) = N_Extended_Return_Statement then
4626 if No (Return_Statement) then
4627 Return_Statement := N;
4628 return OK;
4630 else
4631 return Abandon;
4632 end if;
4634 else
4635 return OK;
4636 end if;
4637 end Check_Return;
4639 function Check_All_Returns is new Traverse_Func (Check_Return);
4641 -- Start of processing for Has_Single_Return
4643 begin
4644 if Check_All_Returns (N) /= OK then
4645 return False;
4647 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4648 return True;
4650 else
4651 return
4652 Present (Declarations (N))
4653 and then Present (First (Declarations (N)))
4654 and then Nkind (First (Declarations (N))) = N_Object_Declaration
4655 and then Entity (Expression (Return_Statement)) =
4656 Defining_Identifier (First (Declarations (N)));
4657 end if;
4658 end Has_Single_Return;
4660 -----------------------------
4661 -- In_Main_Unit_Or_Subunit --
4662 -----------------------------
4664 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
4665 Comp : Node_Id := Cunit (Get_Code_Unit (E));
4667 begin
4668 -- Check whether the subprogram or package to inline is within the main
4669 -- unit or its spec or within a subunit. In either case there are no
4670 -- additional bodies to process. If the subprogram appears in a parent
4671 -- of the current unit, the check on whether inlining is possible is
4672 -- done in Analyze_Inlined_Bodies.
4674 while Nkind (Unit (Comp)) = N_Subunit loop
4675 Comp := Library_Unit (Comp);
4676 end loop;
4678 return Comp = Cunit (Main_Unit)
4679 or else Comp = Library_Unit (Cunit (Main_Unit));
4680 end In_Main_Unit_Or_Subunit;
4682 ----------------
4683 -- Initialize --
4684 ----------------
4686 procedure Initialize is
4687 begin
4688 Pending_Instantiations.Init;
4689 Called_Pending_Instantiations.Init;
4690 Inlined_Bodies.Init;
4691 Successors.Init;
4692 Inlined.Init;
4694 for J in Hash_Headers'Range loop
4695 Hash_Headers (J) := No_Subp;
4696 end loop;
4698 Inlined_Calls := No_Elist;
4699 Backend_Calls := No_Elist;
4700 Backend_Instances := No_Elist;
4701 Backend_Inlined_Subps := No_Elist;
4702 Backend_Not_Inlined_Subps := No_Elist;
4703 end Initialize;
4705 ---------------------------------
4706 -- Inline_Static_Function_Call --
4707 ---------------------------------
4709 procedure Inline_Static_Function_Call (N : Node_Id; Subp : Entity_Id) is
4711 function Replace_Formal (N : Node_Id) return Traverse_Result;
4712 -- Replace each occurrence of a formal with the
4713 -- corresponding actual, using the mapping created
4714 -- by Establish_Actual_Mapping_For_Inlined_Call.
4716 function Reset_Sloc (Nod : Node_Id) return Traverse_Result;
4717 -- Reset the Sloc of a node to that of the call itself, so that errors
4718 -- will be flagged on the call to the static expression function itself
4719 -- rather than on the expression of the function's declaration.
4721 --------------------
4722 -- Replace_Formal --
4723 --------------------
4725 function Replace_Formal (N : Node_Id) return Traverse_Result is
4726 A : Entity_Id;
4727 E : Entity_Id;
4729 begin
4730 if Is_Entity_Name (N) and then Present (Entity (N)) then
4731 E := Entity (N);
4733 if Is_Formal (E) and then Scope (E) = Subp then
4734 A := Renamed_Object (E);
4736 if Nkind (A) = N_Defining_Identifier then
4737 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
4739 -- Literal cases
4741 else
4742 Rewrite (N, New_Copy (A));
4743 end if;
4744 end if;
4746 return Skip;
4748 else
4749 return OK;
4750 end if;
4751 end Replace_Formal;
4753 procedure Replace_Formals is new Traverse_Proc (Replace_Formal);
4755 ------------------
4756 -- Process_Sloc --
4757 ------------------
4759 function Reset_Sloc (Nod : Node_Id) return Traverse_Result is
4760 begin
4761 Set_Sloc (Nod, Sloc (N));
4762 Set_Comes_From_Source (Nod, False);
4764 return OK;
4765 end Reset_Sloc;
4767 procedure Reset_Slocs is new Traverse_Proc (Reset_Sloc);
4769 -- Start of processing for Inline_Static_Function_Call
4771 begin
4772 pragma Assert (Is_Static_Function_Call (N));
4774 declare
4775 Decls : constant List_Id := New_List;
4776 Func_Expr : constant Node_Id :=
4777 Expression_Of_Expression_Function (Subp);
4778 Expr_Copy : constant Node_Id := New_Copy_Tree (Func_Expr);
4780 begin
4781 -- Create a mapping from formals to actuals, also creating temps in
4782 -- Decls, when needed, to hold the actuals.
4784 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Func_Expr);
4786 -- Ensure that the copy has the same parent as the call (this seems
4787 -- to matter when GNATprove_Mode is set and there are nested static
4788 -- calls; prevents blowups in Insert_Actions, though it's not clear
4789 -- exactly why this is needed???).
4791 Set_Parent (Expr_Copy, Parent (N));
4793 Insert_Actions (N, Decls);
4795 -- Now substitute actuals for their corresponding formal references
4796 -- within the expression.
4798 Replace_Formals (Expr_Copy);
4800 Reset_Slocs (Expr_Copy);
4802 -- Apply a qualified expression with the function's result subtype,
4803 -- to ensure that we check the expression against any constraint
4804 -- or predicate, which will cause the call to be illegal if the
4805 -- folded expression doesn't satisfy them. (The predicate case
4806 -- might not get checked if the subtype hasn't been frozen yet,
4807 -- which can happen if this static expression happens to be what
4808 -- causes the freezing, because Has_Static_Predicate doesn't get
4809 -- set on the subtype until it's frozen and Build_Predicates is
4810 -- called. It's not clear how to address this case. ???)
4812 Rewrite (Expr_Copy,
4813 Make_Qualified_Expression (Sloc (Expr_Copy),
4814 Subtype_Mark =>
4815 New_Occurrence_Of (Etype (N), Sloc (Expr_Copy)),
4816 Expression =>
4817 Relocate_Node (Expr_Copy)));
4819 Set_Etype (Expr_Copy, Etype (N));
4821 Analyze_And_Resolve (Expr_Copy, Etype (N));
4823 -- Finally rewrite the function call as the folded static result
4825 Rewrite (N, Expr_Copy);
4827 -- Cleanup mapping between formals and actuals for other expansions
4829 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4830 end;
4831 end Inline_Static_Function_Call;
4833 ------------------------
4834 -- Instantiate_Bodies --
4835 ------------------------
4837 -- Generic bodies contain all the non-local references, so an
4838 -- instantiation does not need any more context than Standard
4839 -- itself, even if the instantiation appears in an inner scope.
4840 -- Generic associations have verified that the contract model is
4841 -- satisfied, so that any error that may occur in the analysis of
4842 -- the body is an internal error.
4844 procedure Instantiate_Bodies is
4846 procedure Instantiate_Body (Info : Pending_Body_Info);
4847 -- Instantiate a pending body
4849 ------------------------
4850 -- Instantiate_Body --
4851 ------------------------
4853 procedure Instantiate_Body (Info : Pending_Body_Info) is
4854 begin
4855 -- If the instantiation node is absent, it has been removed as part
4856 -- of unreachable code.
4858 if No (Info.Inst_Node) then
4859 null;
4861 -- If the instantiation node is a package body, this means that the
4862 -- instance is a compilation unit and the instantiation has already
4863 -- been performed by Build_Instance_Compilation_Unit_Nodes.
4865 elsif Nkind (Info.Inst_Node) = N_Package_Body then
4866 null;
4868 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
4869 Instantiate_Package_Body (Info);
4870 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
4872 else
4873 Instantiate_Subprogram_Body (Info);
4874 end if;
4875 end Instantiate_Body;
4877 J, K : Nat;
4878 Info : Pending_Body_Info;
4880 -- Start of processing for Instantiate_Bodies
4882 begin
4883 if Serious_Errors_Detected = 0 then
4884 Expander_Active := (Operating_Mode = Opt.Generate_Code);
4885 Push_Scope (Standard_Standard);
4886 To_Clean := New_Elmt_List;
4888 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4889 Start_Generic;
4890 end if;
4892 -- A body instantiation may generate additional instantiations, so
4893 -- the following loop must scan to the end of a possibly expanding
4894 -- set (that's why we cannot simply use a FOR loop here). We must
4895 -- also capture the element lest the set be entirely reallocated.
4897 J := 0;
4898 if Back_End_Inlining then
4899 while J <= Called_Pending_Instantiations.Last
4900 and then Serious_Errors_Detected = 0
4901 loop
4902 K := Called_Pending_Instantiations.Table (J);
4903 Info := Pending_Instantiations.Table (K);
4904 Instantiate_Body (Info);
4906 J := J + 1;
4907 end loop;
4909 else
4910 while J <= Pending_Instantiations.Last
4911 and then Serious_Errors_Detected = 0
4912 loop
4913 Info := Pending_Instantiations.Table (J);
4914 Instantiate_Body (Info);
4916 J := J + 1;
4917 end loop;
4918 end if;
4920 -- Reset the table of instantiations. Additional instantiations
4921 -- may be added through inlining, when additional bodies are
4922 -- analyzed.
4924 if Back_End_Inlining then
4925 Called_Pending_Instantiations.Init;
4926 else
4927 Pending_Instantiations.Init;
4928 end if;
4930 -- We can now complete the cleanup actions of scopes that contain
4931 -- pending instantiations (skipped for generic units, since we
4932 -- never need any cleanups in generic units).
4934 if Expander_Active
4935 and then not Is_Generic_Unit (Main_Unit_Entity)
4936 then
4937 Cleanup_Scopes;
4938 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4939 End_Generic;
4940 end if;
4942 Pop_Scope;
4943 end if;
4944 end Instantiate_Bodies;
4946 ---------------
4947 -- Is_Nested --
4948 ---------------
4950 function Is_Nested (E : Entity_Id) return Boolean is
4951 Scop : Entity_Id;
4953 begin
4954 Scop := Scope (E);
4955 while Scop /= Standard_Standard loop
4956 if Is_Subprogram (Scop) then
4957 return True;
4959 elsif Ekind (Scop) = E_Task_Type
4960 or else Ekind (Scop) = E_Entry
4961 or else Ekind (Scop) = E_Entry_Family
4962 then
4963 return True;
4964 end if;
4966 Scop := Scope (Scop);
4967 end loop;
4969 return False;
4970 end Is_Nested;
4972 ------------------------
4973 -- List_Inlining_Info --
4974 ------------------------
4976 procedure List_Inlining_Info is
4977 Elmt : Elmt_Id;
4978 Nod : Node_Id;
4979 Count : Nat;
4981 begin
4982 if not Debug_Flag_Dot_J then
4983 return;
4984 end if;
4986 -- Generate listing of calls inlined by the frontend
4988 if Present (Inlined_Calls) then
4989 Count := 0;
4990 Elmt := First_Elmt (Inlined_Calls);
4991 while Present (Elmt) loop
4992 Nod := Node (Elmt);
4994 if not In_Internal_Unit (Nod) then
4995 Count := Count + 1;
4997 if Count = 1 then
4998 Write_Str ("List of calls inlined by the frontend");
4999 Write_Eol;
5000 end if;
5002 Write_Str (" ");
5003 Write_Int (Count);
5004 Write_Str (":");
5005 Write_Location (Sloc (Nod));
5006 Write_Str (":");
5007 Output.Write_Eol;
5008 end if;
5010 Next_Elmt (Elmt);
5011 end loop;
5012 end if;
5014 -- Generate listing of calls passed to the backend
5016 if Present (Backend_Calls) then
5017 Count := 0;
5019 Elmt := First_Elmt (Backend_Calls);
5020 while Present (Elmt) loop
5021 Nod := Node (Elmt);
5023 if not In_Internal_Unit (Nod) then
5024 Count := Count + 1;
5026 if Count = 1 then
5027 Write_Str ("List of inlined calls passed to the backend");
5028 Write_Eol;
5029 end if;
5031 Write_Str (" ");
5032 Write_Int (Count);
5033 Write_Str (":");
5034 Write_Location (Sloc (Nod));
5035 Output.Write_Eol;
5036 end if;
5038 Next_Elmt (Elmt);
5039 end loop;
5040 end if;
5042 -- Generate listing of instances inlined for the backend
5044 if Present (Backend_Instances) then
5045 Count := 0;
5047 Elmt := First_Elmt (Backend_Instances);
5048 while Present (Elmt) loop
5049 Nod := Node (Elmt);
5051 if not In_Internal_Unit (Nod) then
5052 Count := Count + 1;
5054 if Count = 1 then
5055 Write_Str ("List of instances inlined for the backend");
5056 Write_Eol;
5057 end if;
5059 Write_Str (" ");
5060 Write_Int (Count);
5061 Write_Str (":");
5062 Write_Location (Sloc (Nod));
5063 Output.Write_Eol;
5064 end if;
5066 Next_Elmt (Elmt);
5067 end loop;
5068 end if;
5070 -- Generate listing of subprograms passed to the backend
5072 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
5073 Count := 0;
5075 Elmt := First_Elmt (Backend_Inlined_Subps);
5076 while Present (Elmt) loop
5077 Nod := Node (Elmt);
5079 if not In_Internal_Unit (Nod) then
5080 Count := Count + 1;
5082 if Count = 1 then
5083 Write_Str
5084 ("List of inlined subprograms passed to the backend");
5085 Write_Eol;
5086 end if;
5088 Write_Str (" ");
5089 Write_Int (Count);
5090 Write_Str (":");
5091 Write_Name (Chars (Nod));
5092 Write_Str (" (");
5093 Write_Location (Sloc (Nod));
5094 Write_Str (")");
5095 Output.Write_Eol;
5096 end if;
5098 Next_Elmt (Elmt);
5099 end loop;
5100 end if;
5102 -- Generate listing of subprograms that cannot be inlined by the backend
5104 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
5105 Count := 0;
5107 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
5108 while Present (Elmt) loop
5109 Nod := Node (Elmt);
5111 if not In_Internal_Unit (Nod) then
5112 Count := Count + 1;
5114 if Count = 1 then
5115 Write_Str
5116 ("List of subprograms that cannot be inlined by backend");
5117 Write_Eol;
5118 end if;
5120 Write_Str (" ");
5121 Write_Int (Count);
5122 Write_Str (":");
5123 Write_Name (Chars (Nod));
5124 Write_Str (" (");
5125 Write_Location (Sloc (Nod));
5126 Write_Str (")");
5127 Output.Write_Eol;
5128 end if;
5130 Next_Elmt (Elmt);
5131 end loop;
5132 end if;
5133 end List_Inlining_Info;
5135 ----------
5136 -- Lock --
5137 ----------
5139 procedure Lock is
5140 begin
5141 Pending_Instantiations.Release;
5142 Pending_Instantiations.Locked := True;
5143 Called_Pending_Instantiations.Release;
5144 Called_Pending_Instantiations.Locked := True;
5145 Inlined_Bodies.Release;
5146 Inlined_Bodies.Locked := True;
5147 Successors.Release;
5148 Successors.Locked := True;
5149 Inlined.Release;
5150 Inlined.Locked := True;
5151 end Lock;
5153 --------------------------------
5154 -- Remove_Aspects_And_Pragmas --
5155 --------------------------------
5157 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
5158 procedure Remove_Items (List : List_Id);
5159 -- Remove all useless aspects/pragmas from a particular list
5161 ------------------
5162 -- Remove_Items --
5163 ------------------
5165 procedure Remove_Items (List : List_Id) is
5166 Item : Node_Id;
5167 Item_Id : Node_Id;
5168 Next_Item : Node_Id;
5170 begin
5171 -- Traverse the list looking for an aspect specification or a pragma
5173 Item := First (List);
5174 while Present (Item) loop
5175 Next_Item := Next (Item);
5177 if Nkind (Item) = N_Aspect_Specification then
5178 Item_Id := Identifier (Item);
5179 elsif Nkind (Item) = N_Pragma then
5180 Item_Id := Pragma_Identifier (Item);
5181 else
5182 Item_Id := Empty;
5183 end if;
5185 if Present (Item_Id)
5186 and then Chars (Item_Id) in Name_Contract_Cases
5187 | Name_Global
5188 | Name_Depends
5189 | Name_Postcondition
5190 | Name_Precondition
5191 | Name_Refined_Global
5192 | Name_Refined_Depends
5193 | Name_Refined_Post
5194 | Name_Subprogram_Variant
5195 | Name_Test_Case
5196 | Name_Unmodified
5197 | Name_Unreferenced
5198 | Name_Unused
5199 then
5200 Remove (Item);
5201 end if;
5203 Item := Next_Item;
5204 end loop;
5205 end Remove_Items;
5207 -- Start of processing for Remove_Aspects_And_Pragmas
5209 begin
5210 Remove_Items (Aspect_Specifications (Body_Decl));
5211 Remove_Items (Declarations (Body_Decl));
5213 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
5214 -- in the body of the subprogram.
5216 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
5217 end Remove_Aspects_And_Pragmas;
5219 --------------------------
5220 -- Remove_Dead_Instance --
5221 --------------------------
5223 procedure Remove_Dead_Instance (N : Node_Id) is
5224 begin
5225 for J in 0 .. Pending_Instantiations.Last loop
5226 if Pending_Instantiations.Table (J).Inst_Node = N then
5227 Pending_Instantiations.Table (J).Inst_Node := Empty;
5228 return;
5229 end if;
5230 end loop;
5231 end Remove_Dead_Instance;
5233 -------------------------------------------
5234 -- Reset_Actual_Mapping_For_Inlined_Call --
5235 -------------------------------------------
5237 procedure Reset_Actual_Mapping_For_Inlined_Call (Subp : Entity_Id) is
5238 F : Entity_Id := First_Formal (Subp);
5240 begin
5241 while Present (F) loop
5242 Set_Renamed_Object (F, Empty);
5243 Next_Formal (F);
5244 end loop;
5245 end Reset_Actual_Mapping_For_Inlined_Call;
5247 end Inline;