]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/inline.adb
Daily bump.
[gcc.git] / gcc / ada / inline.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- I N L I N E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
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;
63
64 with GNAT.HTable;
65
66 package body Inline is
67
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.
73 --
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
87
88 Inlined_Calls : Elist_Id;
89 -- List of frontend inlined calls
90
91 Backend_Calls : Elist_Id;
92 -- List of inline calls passed to the backend
93
94 Backend_Instances : Elist_Id;
95 -- List of instances inlined for the backend
96
97 Backend_Inlined_Subps : Elist_Id;
98 -- List of subprograms inlined by the backend
99
100 Backend_Not_Inlined_Subps : Elist_Id;
101 -- List of subprograms that cannot be inlined by the backend
102
103 -----------------------------
104 -- Pending_Instantiations --
105 -----------------------------
106
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.
110
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");
118
119 -------------------------------------
120 -- Called_Pending_Instantiations --
121 -------------------------------------
122
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.
129
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");
137
138 ---------------------------------
139 -- To_Pending_Instantiations --
140 ---------------------------------
141
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.
144
145 Node_Table_Size : constant := 257;
146 -- Number of headers in hash table
147
148 subtype Node_Header_Num is Integer range 0 .. Node_Table_Size - 1;
149 -- Range of headers in hash table
150
151 function Node_Hash (Id : Node_Id) return Node_Header_Num;
152 -- Simple hash function for Node_Ids
153
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 => "=");
161
162 -----------------
163 -- Node_Hash --
164 -----------------
165
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;
170
171 --------------------
172 -- Inlined Bodies --
173 --------------------
174
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.
181
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");
189
190 -----------------------
191 -- Inline Processing --
192 -----------------------
193
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.
201
202 Last_Inlined : Entity_Id := Empty;
203
204 -- For each entry in the table we keep a list of successors in topological
205 -- order, i.e. callers of the current subprogram.
206
207 type Subp_Index is new Nat;
208 No_Subp : constant Subp_Index := 0;
209
210 -- The subprogram entities are hashed into the Inlined table
211
212 Num_Hash_Headers : constant := 512;
213
214 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
215 of Subp_Index;
216
217 type Succ_Index is new Nat;
218 No_Succ : constant Succ_Index := 0;
219
220 type Succ_Info is record
221 Subp : Subp_Index;
222 Next : Succ_Index;
223 end record;
224
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.
228
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");
236
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;
244
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");
252
253 -----------------------
254 -- Local Subprograms --
255 -----------------------
256
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.
261
262 procedure Add_Inlined_Instance (E : Entity_Id);
263 -- Add instance E to the list of inlined instances for the unit
264
265 procedure Add_Inlined_Subprogram (E : Entity_Id);
266 -- Add subprogram E to the list of inlined subprograms for the unit
267
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.
271
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.
284
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.
289
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.
293
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.
300
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
303
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).
310
311 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id);
312 -- Remove all aspects and/or pragmas that have no meaning in inlined body
313 -- Body_Decl. The analysis of these items is performed on the non-inlined
314 -- body. The items currently removed are:
315 -- Always_Terminates
316 -- Contract_Cases
317 -- Global
318 -- Depends
319 -- Exceptional_Cases
320 -- Postcondition
321 -- Precondition
322 -- Refined_Global
323 -- Refined_Depends
324 -- Refined_Post
325 -- Subprogram_Variant
326 -- Test_Case
327 -- Unmodified
328 -- Unreferenced
329
330 procedure Reset_Actual_Mapping_For_Inlined_Call (Subp : Entity_Id);
331 -- Reset the Renamed_Object field to Empty on all formals of Subp, which
332 -- can be set by a call to Establish_Actual_Mapping_For_Inlined_Call.
333
334 ------------------------------
335 -- Deferred Cleanup Actions --
336 ------------------------------
337
338 -- The cleanup actions for scopes that contain package instantiations with
339 -- a body are delayed until after the package body is instantiated. because
340 -- the body may contain finalizable objects or other constructs that affect
341 -- the cleanup code. A scope that contains such instantiations only needs
342 -- to be finalized once, even though it may contain more than one instance.
343 -- We keep a list of scopes that must still be finalized and Cleanup_Scopes
344 -- will be invoked after all the body instantiations have been completed.
345
346 To_Clean : Elist_Id;
347
348 procedure Add_Scope_To_Clean (Scop : Entity_Id);
349 -- Build set of scopes on which cleanup actions must be performed
350
351 procedure Cleanup_Scopes;
352 -- Complete cleanup actions on scopes that need it
353
354 --------------
355 -- Add_Call --
356 --------------
357
358 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
359 P1 : constant Subp_Index := Add_Subp (Called);
360 P2 : Subp_Index;
361 J : Succ_Index;
362
363 begin
364 if Present (Caller) then
365 P2 := Add_Subp (Caller);
366
367 -- Add P1 to the list of successors of P2, if not already there.
368 -- Note that P2 may contain more than one call to P1, and only
369 -- one needs to be recorded.
370
371 J := Inlined.Table (P2).First_Succ;
372 while J /= No_Succ loop
373 if Successors.Table (J).Subp = P1 then
374 return;
375 end if;
376
377 J := Successors.Table (J).Next;
378 end loop;
379
380 -- On exit, make a successor entry for P1
381
382 Successors.Increment_Last;
383 Successors.Table (Successors.Last).Subp := P1;
384 Successors.Table (Successors.Last).Next :=
385 Inlined.Table (P2).First_Succ;
386 Inlined.Table (P2).First_Succ := Successors.Last;
387 else
388 Inlined.Table (P1).Main_Call := True;
389 end if;
390 end Add_Call;
391
392 ----------------------
393 -- Add_Inlined_Body --
394 ----------------------
395
396 procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id) is
397
398 type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package);
399 -- Level of inlining for the call: Dont_Inline means no inlining,
400 -- Inline_Call means that only the call is considered for inlining,
401 -- Inline_Package means that the call is considered for inlining and
402 -- its package compiled and scanned for more inlining opportunities.
403
404 function Is_Non_Loading_Expression_Function
405 (Id : Entity_Id) return Boolean;
406 -- Determine whether arbitrary entity Id denotes a subprogram which is
407 -- either
408 --
409 -- * An expression function
410 --
411 -- * A function completed by an expression function where both the
412 -- spec and body are in the same context.
413
414 function Must_Inline return Inline_Level_Type;
415 -- Inlining is only done if the call statement N is in the main unit,
416 -- or within the body of another inlined subprogram.
417
418 ----------------------------------------
419 -- Is_Non_Loading_Expression_Function --
420 ----------------------------------------
421
422 function Is_Non_Loading_Expression_Function
423 (Id : Entity_Id) return Boolean
424 is
425 Body_Decl : Node_Id;
426 Body_Id : Entity_Id;
427 Spec_Decl : Node_Id;
428
429 begin
430 -- A stand-alone expression function is transformed into a spec-body
431 -- pair in-place. Since both the spec and body are in the same list,
432 -- the inlining of such an expression function does not need to load
433 -- anything extra.
434
435 if Is_Expression_Function (Id) then
436 return True;
437
438 -- A function may be completed by an expression function
439
440 elsif Ekind (Id) = E_Function then
441 Spec_Decl := Unit_Declaration_Node (Id);
442
443 if Nkind (Spec_Decl) = N_Subprogram_Declaration then
444 Body_Id := Corresponding_Body (Spec_Decl);
445
446 if Present (Body_Id) then
447 Body_Decl := Unit_Declaration_Node (Body_Id);
448
449 -- The inlining of a completing expression function does
450 -- not need to load anything extra when both the spec and
451 -- body are in the same context.
452
453 return
454 Was_Expression_Function (Body_Decl)
455 and then Parent (Spec_Decl) = Parent (Body_Decl);
456 end if;
457 end if;
458 end if;
459
460 return False;
461 end Is_Non_Loading_Expression_Function;
462
463 -----------------
464 -- Must_Inline --
465 -----------------
466
467 function Must_Inline return Inline_Level_Type is
468 Scop : Entity_Id;
469 Comp : Node_Id;
470
471 begin
472 -- Check if call is in main unit
473
474 Scop := Current_Scope;
475
476 -- Do not try to inline if scope is standard. This could happen, for
477 -- example, for a call to Add_Global_Declaration, and it causes
478 -- trouble to try to inline at this level.
479
480 if Scop = Standard_Standard then
481 return Dont_Inline;
482 end if;
483
484 -- Otherwise lookup scope stack to outer scope
485
486 while Scope (Scop) /= Standard_Standard
487 and then not Is_Child_Unit (Scop)
488 loop
489 Scop := Scope (Scop);
490 end loop;
491
492 Comp := Parent (Scop);
493 while Nkind (Comp) /= N_Compilation_Unit loop
494 Comp := Parent (Comp);
495 end loop;
496
497 -- If the call is in the main unit, inline the call and compile the
498 -- package of the subprogram to find more calls to be inlined.
499
500 if Comp = Cunit (Main_Unit)
501 or else Comp = Library_Unit (Cunit (Main_Unit))
502 then
503 Add_Call (E);
504 return Inline_Package;
505 end if;
506
507 -- The call is not in the main unit. See if it is in some subprogram
508 -- that can be inlined outside its unit. If so, inline the call and,
509 -- if the inlining level is set to 1, stop there; otherwise also
510 -- compile the package as above.
511
512 Scop := Current_Scope;
513 while Scope (Scop) /= Standard_Standard
514 and then not Is_Child_Unit (Scop)
515 loop
516 if Is_Overloadable (Scop)
517 and then Is_Inlined (Scop)
518 and then not Is_Nested (Scop)
519 then
520 Add_Call (E, Scop);
521
522 if Inline_Level = 1 then
523 return Inline_Call;
524 else
525 return Inline_Package;
526 end if;
527 end if;
528
529 Scop := Scope (Scop);
530 end loop;
531
532 return Dont_Inline;
533 end Must_Inline;
534
535 Inst : Entity_Id;
536 Inst_Decl : Node_Id;
537 Level : Inline_Level_Type;
538
539 -- Start of processing for Add_Inlined_Body
540
541 begin
542 Append_New_Elmt (N, To => Backend_Calls);
543
544 -- Skip subprograms that cannot or need not be inlined outside their
545 -- unit or parent subprogram.
546
547 if Is_Abstract_Subprogram (E)
548 or else Convention (E) = Convention_Protected
549 or else In_Main_Unit_Or_Subunit (E)
550 or else Is_Nested (E)
551 then
552 return;
553 end if;
554
555 -- Find out whether the call must be inlined. Unless the result is
556 -- Dont_Inline, Must_Inline also creates an edge for the call in the
557 -- callgraph; however, it will not be activated until after Is_Called
558 -- is set on the subprogram.
559
560 Level := Must_Inline;
561
562 if Level = Dont_Inline then
563 return;
564 end if;
565
566 -- If a previous call to the subprogram has been inlined, nothing to do
567
568 if Is_Called (E) then
569 return;
570 end if;
571
572 -- If the subprogram is an instance, then inline the instance
573
574 if Is_Generic_Instance (E) then
575 Add_Inlined_Instance (E);
576 end if;
577
578 -- Mark the subprogram as called
579
580 Set_Is_Called (E);
581
582 -- If the call was generated by the compiler and is to a subprogram in
583 -- a run-time unit, we need to suppress debugging information for it,
584 -- so that the code that is eventually inlined will not affect the
585 -- debugging of the program. We do not do it if the call comes from
586 -- source because, even if the call is inlined, the user may expect it
587 -- to be present in the debugging information.
588
589 if not Comes_From_Source (N)
590 and then In_Extended_Main_Source_Unit (N)
591 and then Is_Predefined_Unit (Get_Source_Unit (E))
592 then
593 Set_Needs_Debug_Info (E, False);
594 end if;
595
596 -- If the subprogram is an expression function, or is completed by one
597 -- where both the spec and body are in the same context, then there is
598 -- no need to load any package body since the body of the function is
599 -- in the spec.
600
601 if Is_Non_Loading_Expression_Function (E) then
602 return;
603 end if;
604
605 -- Find unit containing E, and add to list of inlined bodies if needed.
606 -- Library-level functions must be handled specially, because there is
607 -- no enclosing package to retrieve. In this case, it is the body of
608 -- the function that will have to be loaded.
609
610 declare
611 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
612
613 begin
614 if Pack = E then
615 Inlined_Bodies.Increment_Last;
616 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
617
618 else
619 pragma Assert (Ekind (Pack) = E_Package);
620
621 -- If the subprogram is within an instance, inline the instance
622
623 if Comes_From_Source (E) then
624 Inst := Scope (E);
625
626 while Present (Inst) and then Inst /= Standard_Standard loop
627 exit when Is_Generic_Instance (Inst);
628 Inst := Scope (Inst);
629 end loop;
630
631 if Present (Inst)
632 and then Is_Generic_Instance (Inst)
633 and then not Is_Called (Inst)
634 then
635 Inst_Decl := Unit_Declaration_Node (Inst);
636
637 -- Do not inline the instance if the body already exists,
638 -- or the instance node is simply missing.
639
640 if Present (Corresponding_Body (Inst_Decl))
641 or else (Nkind (Parent (Inst_Decl)) /= N_Compilation_Unit
642 and then No (Next (Inst_Decl)))
643 then
644 Set_Is_Called (Inst);
645 else
646 Add_Inlined_Instance (Inst);
647 end if;
648 end if;
649 end if;
650
651 -- If the unit containing E is an instance, nothing more to do
652
653 if Is_Generic_Instance (Pack) then
654 null;
655
656 -- Do not inline the package if the subprogram is an init proc
657 -- or other internally generated subprogram, because in that
658 -- case the subprogram body appears in the same unit that
659 -- declares the type, and that body is visible to the back end.
660 -- Do not inline it either if it is in the main unit.
661 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
662 -- calls if the back end takes care of inlining the call.
663 -- Note that Level is in Inline_Call | Inline_Package here.
664
665 elsif ((Level = Inline_Call
666 and then Has_Pragma_Inline_Always (E)
667 and then Back_End_Inlining)
668 or else Level = Inline_Package)
669 and then not Is_Inlined (Pack)
670 and then not Is_Internal (E)
671 and then not In_Main_Unit_Or_Subunit (Pack)
672 then
673 Set_Is_Inlined (Pack);
674 Inlined_Bodies.Increment_Last;
675 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
676 end if;
677 end if;
678
679 -- Ensure that Analyze_Inlined_Bodies will be invoked after
680 -- completing the analysis of the current unit.
681
682 Inline_Processing_Required := True;
683 end;
684 end Add_Inlined_Body;
685
686 --------------------------
687 -- Add_Inlined_Instance --
688 --------------------------
689
690 procedure Add_Inlined_Instance (E : Entity_Id) is
691 Decl_Node : constant Node_Id := Unit_Declaration_Node (E);
692 Index : Int;
693
694 begin
695 -- This machinery is only used with back-end inlining
696
697 if not Back_End_Inlining then
698 return;
699 end if;
700
701 -- Register the instance in the list
702
703 Append_New_Elmt (Decl_Node, To => Backend_Instances);
704
705 -- Retrieve the index of its corresponding pending instantiation
706 -- and mark this corresponding pending instantiation as needed.
707
708 Index := To_Pending_Instantiations.Get (Decl_Node);
709 if Index >= 0 then
710 Called_Pending_Instantiations.Append (Index);
711 else
712 pragma Assert (False);
713 null;
714 end if;
715
716 Set_Is_Called (E);
717 end Add_Inlined_Instance;
718
719 ----------------------------
720 -- Add_Inlined_Subprogram --
721 ----------------------------
722
723 procedure Add_Inlined_Subprogram (E : Entity_Id) is
724 Decl : constant Node_Id := Parent (Declaration_Node (E));
725 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
726
727 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
728 -- Append Subp to the list of subprograms inlined by the backend
729
730 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
731 -- Append Subp to the list of subprograms that cannot be inlined by
732 -- the backend.
733
734 -----------------------------------------
735 -- Register_Backend_Inlined_Subprogram --
736 -----------------------------------------
737
738 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
739 begin
740 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
741 end Register_Backend_Inlined_Subprogram;
742
743 ---------------------------------------------
744 -- Register_Backend_Not_Inlined_Subprogram --
745 ---------------------------------------------
746
747 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
748 begin
749 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
750 end Register_Backend_Not_Inlined_Subprogram;
751
752 -- Start of processing for Add_Inlined_Subprogram
753
754 begin
755 -- We can inline the subprogram if its unit is known to be inlined or is
756 -- an instance whose body will be analyzed anyway or the subprogram was
757 -- generated as a body by the compiler (for example an initialization
758 -- procedure) or its declaration was provided along with the body (for
759 -- example an expression function) and it does not declare types with
760 -- nontrivial initialization procedures.
761
762 if (Is_Inlined (Pack)
763 or else Is_Generic_Instance (Pack)
764 or else Nkind (Decl) = N_Subprogram_Body
765 or else Present (Corresponding_Body (Decl)))
766 and then not Has_Initialized_Type (E)
767 then
768 Register_Backend_Inlined_Subprogram (E);
769
770 if No (Last_Inlined) then
771 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
772 else
773 Set_Next_Inlined_Subprogram (Last_Inlined, E);
774 end if;
775
776 Last_Inlined := E;
777
778 else
779 Register_Backend_Not_Inlined_Subprogram (E);
780 end if;
781 end Add_Inlined_Subprogram;
782
783 --------------------------------
784 -- Add_Pending_Instantiation --
785 --------------------------------
786
787 procedure Add_Pending_Instantiation
788 (Inst : Node_Id;
789 Act_Decl : Node_Id;
790 Fin_Scop : Node_Id := Empty)
791 is
792 Act_Decl_Id : Entity_Id;
793 Index : Int;
794
795 begin
796 -- Here is a defense against a ludicrous number of instantiations
797 -- caused by a circular set of instantiation attempts.
798
799 if Pending_Instantiations.Last + 1 >= Maximum_Instantiations then
800 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
801 Error_Msg_N ("too many instantiations, exceeds max of^", Inst);
802 Error_Msg_N ("\limit can be changed using -gnateinn switch", Inst);
803 raise Unrecoverable_Error;
804 end if;
805
806 -- Capture the body of the generic instantiation along with its context
807 -- for later processing by Instantiate_Bodies.
808
809 Pending_Instantiations.Append
810 ((Inst_Node => Inst,
811 Act_Decl => Act_Decl,
812 Fin_Scop => Fin_Scop,
813 Config_Switches => Save_Config_Switches,
814 Current_Sem_Unit => Current_Sem_Unit,
815 Expander_Status => Expander_Active,
816 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
817 Scope_Suppress => Scope_Suppress,
818 Warnings => Save_Warnings));
819
820 -- With back-end inlining, also associate the index to the instantiation
821
822 if Back_End_Inlining then
823 Act_Decl_Id := Defining_Entity (Act_Decl);
824 Index := Pending_Instantiations.Last;
825
826 To_Pending_Instantiations.Set (Act_Decl, Index);
827
828 -- If an instantiation is in the main unit or subunit, or is a nested
829 -- subprogram, then its body is needed as per the analysis done in
830 -- Analyze_Package_Instantiation & Analyze_Subprogram_Instantiation.
831
832 if In_Main_Unit_Or_Subunit (Act_Decl_Id)
833 or else (Is_Subprogram (Act_Decl_Id)
834 and then Is_Nested (Act_Decl_Id))
835 then
836 Called_Pending_Instantiations.Append (Index);
837
838 Set_Is_Called (Act_Decl_Id);
839 end if;
840 end if;
841 end Add_Pending_Instantiation;
842
843 ------------------------
844 -- Add_Scope_To_Clean --
845 ------------------------
846
847 procedure Add_Scope_To_Clean (Scop : Entity_Id) is
848 begin
849 Append_Unique_Elmt (Scop, To_Clean);
850 end Add_Scope_To_Clean;
851
852 --------------
853 -- Add_Subp --
854 --------------
855
856 function Add_Subp (E : Entity_Id) return Subp_Index is
857 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
858 J : Subp_Index;
859
860 procedure New_Entry;
861 -- Initialize entry in Inlined table
862
863 procedure New_Entry is
864 begin
865 Inlined.Increment_Last;
866 Inlined.Table (Inlined.Last).Name := E;
867 Inlined.Table (Inlined.Last).Next := No_Subp;
868 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
869 Inlined.Table (Inlined.Last).Main_Call := False;
870 Inlined.Table (Inlined.Last).Processed := False;
871 end New_Entry;
872
873 -- Start of processing for Add_Subp
874
875 begin
876 if Hash_Headers (Index) = No_Subp then
877 New_Entry;
878 Hash_Headers (Index) := Inlined.Last;
879 return Inlined.Last;
880
881 else
882 J := Hash_Headers (Index);
883 while J /= No_Subp loop
884 if Inlined.Table (J).Name = E then
885 return J;
886 else
887 Index := J;
888 J := Inlined.Table (J).Next;
889 end if;
890 end loop;
891
892 -- On exit, subprogram was not found. Enter in table. Index is
893 -- the current last entry on the hash chain.
894
895 New_Entry;
896 Inlined.Table (Index).Next := Inlined.Last;
897 return Inlined.Last;
898 end if;
899 end Add_Subp;
900
901 ----------------------------
902 -- Analyze_Inlined_Bodies --
903 ----------------------------
904
905 procedure Analyze_Inlined_Bodies is
906 Comp_Unit : Node_Id;
907 J : Nat;
908 Pack : Entity_Id;
909 Subp : Subp_Index;
910 S : Succ_Index;
911
912 type Pending_Index is new Nat;
913
914 package Pending_Inlined is new Table.Table (
915 Table_Component_Type => Subp_Index,
916 Table_Index_Type => Pending_Index,
917 Table_Low_Bound => 1,
918 Table_Initial => Alloc.Inlined_Initial,
919 Table_Increment => Alloc.Inlined_Increment,
920 Table_Name => "Pending_Inlined");
921 -- The workpile used to compute the transitive closure
922
923 -- Start of processing for Analyze_Inlined_Bodies
924
925 begin
926 if Serious_Errors_Detected = 0 then
927 Push_Scope (Standard_Standard);
928
929 J := 0;
930 while J <= Inlined_Bodies.Last
931 and then Serious_Errors_Detected = 0
932 loop
933 Pack := Inlined_Bodies.Table (J);
934 while Present (Pack)
935 and then Scope (Pack) /= Standard_Standard
936 and then not Is_Child_Unit (Pack)
937 loop
938 Pack := Scope (Pack);
939 end loop;
940
941 Comp_Unit := Parent (Pack);
942 while Present (Comp_Unit)
943 and then Nkind (Comp_Unit) /= N_Compilation_Unit
944 loop
945 Comp_Unit := Parent (Comp_Unit);
946 end loop;
947
948 -- Load the body if it exists and contains inlineable entities,
949 -- unless it is the main unit, or is an instance whose body has
950 -- already been analyzed.
951
952 if Present (Comp_Unit)
953 and then Comp_Unit /= Cunit (Main_Unit)
954 and then Body_Required (Comp_Unit)
955 and then
956 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
957 or else
958 (No (Corresponding_Body (Unit (Comp_Unit)))
959 and then Body_Needed_For_Inlining
960 (Defining_Entity (Unit (Comp_Unit)))))
961 then
962 declare
963 Bname : constant Unit_Name_Type :=
964 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
965
966 OK : Boolean;
967
968 begin
969 if not Is_Loaded (Bname) then
970 Style_Check := False;
971 Load_Needed_Body (Comp_Unit, OK);
972
973 if not OK then
974
975 -- Warn that a body was not available for inlining
976 -- by the back-end.
977
978 Error_Msg_Unit_1 := Bname;
979 Error_Msg_N
980 ("one or more inlined subprograms accessed in $!??",
981 Comp_Unit);
982 Error_Msg_File_1 :=
983 Get_File_Name (Bname, Subunit => False);
984 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
985 end if;
986 end if;
987 end;
988 end if;
989
990 J := J + 1;
991
992 if J > Inlined_Bodies.Last then
993
994 -- The analysis of required bodies may have produced additional
995 -- generic instantiations. To obtain further inlining, we need
996 -- to perform another round of generic body instantiations.
997
998 Instantiate_Bodies;
999
1000 -- Symmetrically, the instantiation of required generic bodies
1001 -- may have caused additional bodies to be inlined. To obtain
1002 -- further inlining, we keep looping over the inlined bodies.
1003 end if;
1004 end loop;
1005
1006 -- The list of inlined subprograms is an overestimate, because it
1007 -- includes inlined functions called from functions that are compiled
1008 -- as part of an inlined package, but are not themselves called. An
1009 -- accurate computation of just those subprograms that are needed
1010 -- requires that we perform a transitive closure over the call graph,
1011 -- starting from calls in the main compilation unit.
1012
1013 for Index in Inlined.First .. Inlined.Last loop
1014 if not Is_Called (Inlined.Table (Index).Name) then
1015
1016 -- This means that Add_Inlined_Body added the subprogram to the
1017 -- table but wasn't able to handle its code unit. Do nothing.
1018
1019 Inlined.Table (Index).Processed := True;
1020
1021 elsif Inlined.Table (Index).Main_Call then
1022 Pending_Inlined.Increment_Last;
1023 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
1024 Inlined.Table (Index).Processed := True;
1025
1026 else
1027 Set_Is_Called (Inlined.Table (Index).Name, False);
1028 end if;
1029 end loop;
1030
1031 -- Iterate over the workpile until it is emptied, propagating the
1032 -- Is_Called flag to the successors of the processed subprogram.
1033
1034 while Pending_Inlined.Last >= Pending_Inlined.First loop
1035 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
1036 Pending_Inlined.Decrement_Last;
1037
1038 S := Inlined.Table (Subp).First_Succ;
1039
1040 while S /= No_Succ loop
1041 Subp := Successors.Table (S).Subp;
1042
1043 if not Inlined.Table (Subp).Processed then
1044 Set_Is_Called (Inlined.Table (Subp).Name);
1045 Pending_Inlined.Increment_Last;
1046 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
1047 Inlined.Table (Subp).Processed := True;
1048 end if;
1049
1050 S := Successors.Table (S).Next;
1051 end loop;
1052 end loop;
1053
1054 -- Finally add the called subprograms to the list of inlined
1055 -- subprograms for the unit.
1056
1057 for Index in Inlined.First .. Inlined.Last loop
1058 declare
1059 E : constant Subprogram_Kind_Id := Inlined.Table (Index).Name;
1060
1061 begin
1062 if Is_Called (E) and then not Is_Ignored_Ghost_Entity (E) then
1063 Add_Inlined_Subprogram (E);
1064 end if;
1065 end;
1066 end loop;
1067
1068 Pop_Scope;
1069 end if;
1070 end Analyze_Inlined_Bodies;
1071
1072 --------------------------
1073 -- Build_Body_To_Inline --
1074 --------------------------
1075
1076 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
1077 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1078 Original_Body : Node_Id;
1079 Body_To_Analyze : Node_Id;
1080 Max_Size : constant := 10;
1081
1082 function Has_Extended_Return return Boolean;
1083 -- This function returns True if the subprogram has an extended return
1084 -- statement.
1085
1086 function Has_Pending_Instantiation return Boolean;
1087 -- If some enclosing body contains instantiations that appear before
1088 -- the corresponding generic body, the enclosing body has a freeze node
1089 -- so that it can be elaborated after the generic itself. This might
1090 -- conflict with subsequent inlinings, so that it is unsafe to try to
1091 -- inline in such a case.
1092
1093 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1094 -- If the body of the subprogram includes a call that returns an
1095 -- unconstrained type, the secondary stack is involved, and it is
1096 -- not worth inlining.
1097 -------------------------
1098 -- Has_Extended_Return --
1099 -------------------------
1100
1101 function Has_Extended_Return return Boolean is
1102 Body_To_Inline : constant Node_Id := N;
1103
1104 function Check_Return (N : Node_Id) return Traverse_Result;
1105 -- Returns OK on node N if this is not an extended return statement
1106
1107 ------------------
1108 -- Check_Return --
1109 ------------------
1110
1111 function Check_Return (N : Node_Id) return Traverse_Result is
1112 begin
1113 case Nkind (N) is
1114 when N_Extended_Return_Statement =>
1115 return Abandon;
1116
1117 -- Skip locally declared subprogram bodies inside the body to
1118 -- inline, as the return statements inside those do not count.
1119
1120 when N_Subprogram_Body =>
1121 if N = Body_To_Inline then
1122 return OK;
1123 else
1124 return Skip;
1125 end if;
1126
1127 when others =>
1128 return OK;
1129 end case;
1130 end Check_Return;
1131
1132 function Check_All_Returns is new Traverse_Func (Check_Return);
1133
1134 -- Start of processing for Has_Extended_Return
1135
1136 begin
1137 return Check_All_Returns (N) /= OK;
1138 end Has_Extended_Return;
1139
1140 -------------------------------
1141 -- Has_Pending_Instantiation --
1142 -------------------------------
1143
1144 function Has_Pending_Instantiation return Boolean is
1145 S : Entity_Id;
1146
1147 begin
1148 S := Current_Scope;
1149 while Present (S) loop
1150 if Is_Compilation_Unit (S)
1151 or else Is_Child_Unit (S)
1152 then
1153 return False;
1154
1155 elsif Ekind (S) = E_Package
1156 and then Has_Forward_Instantiation (S)
1157 then
1158 return True;
1159 end if;
1160
1161 S := Scope (S);
1162 end loop;
1163
1164 return False;
1165 end Has_Pending_Instantiation;
1166
1167 --------------------------
1168 -- Uses_Secondary_Stack --
1169 --------------------------
1170
1171 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1172 function Check_Call (N : Node_Id) return Traverse_Result;
1173 -- Look for function calls that return an unconstrained type
1174
1175 ----------------
1176 -- Check_Call --
1177 ----------------
1178
1179 function Check_Call (N : Node_Id) return Traverse_Result is
1180 begin
1181 if Nkind (N) = N_Function_Call
1182 and then Is_Entity_Name (Name (N))
1183 and then Is_Composite_Type (Etype (Entity (Name (N))))
1184 and then not Is_Constrained (Etype (Entity (Name (N))))
1185 then
1186 Cannot_Inline
1187 ("cannot inline & (call returns unconstrained type)?",
1188 N, Spec_Id);
1189 return Abandon;
1190 else
1191 return OK;
1192 end if;
1193 end Check_Call;
1194
1195 function Check_Calls is new Traverse_Func (Check_Call);
1196
1197 begin
1198 return Check_Calls (Bod) = Abandon;
1199 end Uses_Secondary_Stack;
1200
1201 -- Start of processing for Build_Body_To_Inline
1202
1203 begin
1204 -- Return immediately if done already
1205
1206 if Nkind (Decl) = N_Subprogram_Declaration
1207 and then Present (Body_To_Inline (Decl))
1208 then
1209 return;
1210
1211 -- Functions that return controlled types cannot currently be inlined
1212 -- because they require secondary stack handling; controlled actions
1213 -- may also interfere in complex ways with inlining.
1214
1215 elsif Ekind (Spec_Id) = E_Function
1216 and then Needs_Finalization (Etype (Spec_Id))
1217 then
1218 Cannot_Inline
1219 ("cannot inline & (controlled return type)?", N, Spec_Id);
1220 return;
1221 end if;
1222
1223 if Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
1224 return;
1225 end if;
1226
1227 if Present (Handled_Statement_Sequence (N)) then
1228 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1229 Cannot_Inline
1230 ("cannot inline& (exception handler)?",
1231 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1232 Spec_Id);
1233 return;
1234
1235 elsif Has_Excluded_Statement
1236 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
1237 then
1238 return;
1239 end if;
1240 end if;
1241
1242 -- We do not inline a subprogram that is too large, unless it is marked
1243 -- Inline_Always or we are in GNATprove mode. This pragma does not
1244 -- suppress the other checks on inlining (forbidden declarations,
1245 -- handlers, etc).
1246
1247 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1248 and then List_Length
1249 (Statements (Handled_Statement_Sequence (N))) > Max_Size
1250 then
1251 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
1252 return;
1253 end if;
1254
1255 if Has_Pending_Instantiation then
1256 Cannot_Inline
1257 ("cannot inline& (forward instance within enclosing body)?",
1258 N, Spec_Id);
1259 return;
1260 end if;
1261
1262 -- Within an instance, the body to inline must be treated as a nested
1263 -- generic, so that the proper global references are preserved.
1264
1265 -- Note that we do not do this at the library level, because it is not
1266 -- needed, and furthermore this causes trouble if front-end inlining
1267 -- is activated (-gnatN).
1268
1269 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1270 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1271 Original_Body := Copy_Generic_Node (N, Empty, Instantiating => True);
1272 else
1273 Original_Body := Copy_Separate_Tree (N);
1274 end if;
1275
1276 -- We need to capture references to the formals in order to substitute
1277 -- the actuals at the point of inlining, i.e. instantiation. To treat
1278 -- the formals as globals to the body to inline, we nest it within a
1279 -- dummy parameterless subprogram, declared within the real one. To
1280 -- avoid generating an internal name (which is never public, and which
1281 -- affects serial numbers of other generated names), we use an internal
1282 -- symbol that cannot conflict with user declarations.
1283
1284 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1285 Set_Defining_Unit_Name
1286 (Specification (Original_Body),
1287 Make_Defining_Identifier (Sloc (N), Name_uParent));
1288 Set_Corresponding_Spec (Original_Body, Empty);
1289
1290 -- Remove all aspects/pragmas that have no meaning in an inlined body
1291
1292 Remove_Aspects_And_Pragmas (Original_Body);
1293
1294 Body_To_Analyze :=
1295 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
1296
1297 -- Set return type of function, which is also global and does not need
1298 -- to be resolved.
1299
1300 if Ekind (Spec_Id) = E_Function then
1301 Set_Result_Definition
1302 (Specification (Body_To_Analyze),
1303 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1304 end if;
1305
1306 if No (Declarations (N)) then
1307 Set_Declarations (N, New_List (Body_To_Analyze));
1308 else
1309 Append (Body_To_Analyze, Declarations (N));
1310 end if;
1311
1312 Start_Generic;
1313
1314 Analyze (Body_To_Analyze);
1315 Push_Scope (Defining_Entity (Body_To_Analyze));
1316 Save_Global_References (Original_Body);
1317 End_Scope;
1318 Remove (Body_To_Analyze);
1319
1320 End_Generic;
1321
1322 -- Restore environment if previously saved
1323
1324 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1325 Restore_Env;
1326 end if;
1327
1328 -- Functions that return unconstrained composite types require
1329 -- secondary stack handling, and cannot currently be inlined, unless
1330 -- all return statements return a local variable that is the first
1331 -- local declaration in the body. We had to delay this check until
1332 -- the body of the function is analyzed since Has_Single_Return()
1333 -- requires a minimum decoration.
1334
1335 if Ekind (Spec_Id) = E_Function
1336 and then not Is_Scalar_Type (Etype (Spec_Id))
1337 and then not Is_Access_Type (Etype (Spec_Id))
1338 and then not Is_Constrained (Etype (Spec_Id))
1339 then
1340 if not Has_Single_Return (Body_To_Analyze)
1341
1342 -- Skip inlining if the function returns an unconstrained type
1343 -- using an extended return statement, since this part of the
1344 -- new inlining model is not yet supported by the current
1345 -- implementation.
1346
1347 or else (Returns_Unconstrained_Type (Spec_Id)
1348 and then Has_Extended_Return)
1349 then
1350 Cannot_Inline
1351 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
1352 return;
1353 end if;
1354
1355 -- If secondary stack is used, there is no point in inlining. We have
1356 -- already issued the warning in this case, so nothing to do.
1357
1358 elsif Uses_Secondary_Stack (Body_To_Analyze) then
1359 return;
1360 end if;
1361
1362 Set_Body_To_Inline (Decl, Original_Body);
1363 Mutate_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1364 Set_Is_Inlined (Spec_Id);
1365 end Build_Body_To_Inline;
1366
1367 -------------------------------------------
1368 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1369 -------------------------------------------
1370
1371 function Call_Can_Be_Inlined_In_GNATprove_Mode
1372 (N : Node_Id;
1373 Subp : Entity_Id) return Boolean
1374 is
1375 function Has_Dereference (N : Node_Id) return Boolean;
1376 -- Return whether N contains an explicit dereference
1377
1378 ---------------------
1379 -- Has_Dereference --
1380 ---------------------
1381
1382 function Has_Dereference (N : Node_Id) return Boolean is
1383
1384 function Process (N : Node_Id) return Traverse_Result;
1385 -- Process one node in search for dereference
1386
1387 -------------
1388 -- Process --
1389 -------------
1390
1391 function Process (N : Node_Id) return Traverse_Result is
1392 begin
1393 if Nkind (N) = N_Explicit_Dereference then
1394 return Abandon;
1395 else
1396 return OK;
1397 end if;
1398 end Process;
1399
1400 function Traverse is new Traverse_Func (Process);
1401 -- Traverse tree to look for dereference
1402
1403 begin
1404 return Traverse (N) = Abandon;
1405 end Has_Dereference;
1406
1407 -- Local variables
1408
1409 F : Entity_Id;
1410 A : Node_Id;
1411
1412 begin
1413 -- Check if inlining may lead to missing a check on type conversion of
1414 -- input parameters otherwise.
1415
1416 F := First_Formal (Subp);
1417 A := First_Actual (N);
1418 while Present (F) loop
1419 if Ekind (F) /= E_Out_Parameter
1420 and then not Same_Type (Etype (F), Etype (A))
1421 and then
1422 (Is_By_Reference_Type (Etype (A))
1423 or else Is_Limited_Type (Etype (A)))
1424 then
1425 return False;
1426 end if;
1427
1428 Next_Formal (F);
1429 Next_Actual (A);
1430 end loop;
1431
1432 -- Check if inlining may lead to introducing temporaries of access type,
1433 -- which can lead to missing checks for memory leaks. This can only
1434 -- come from an (IN-)OUT parameter transformed into a renaming by SPARK
1435 -- expansion, whose side-effects are removed, and a dereference in the
1436 -- corresponding actual. If the formal itself is of a deep type (it has
1437 -- access subcomponents), the subprogram already cannot be inlined in
1438 -- GNATprove mode.
1439
1440 F := First_Formal (Subp);
1441 A := First_Actual (N);
1442 while Present (F) loop
1443 if Ekind (F) /= E_In_Parameter
1444 and then Has_Dereference (A)
1445 then
1446 return False;
1447 end if;
1448
1449 Next_Formal (F);
1450 Next_Actual (A);
1451 end loop;
1452
1453 return True;
1454 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1455
1456 --------------------------------------
1457 -- Can_Be_Inlined_In_GNATprove_Mode --
1458 --------------------------------------
1459
1460 function Can_Be_Inlined_In_GNATprove_Mode
1461 (Spec_Id : Entity_Id;
1462 Body_Id : Entity_Id) return Boolean
1463 is
1464 function Has_Constant_With_Address_Clause
1465 (Body_Node : Node_Id)
1466 return Boolean;
1467 -- Returns true if the subprogram contains a declaration of a constant
1468 -- with an address clause, which could become illegal in SPARK after
1469 -- inlining, if the address clause mentions a constant view of a mutable
1470 -- object at call site.
1471
1472 function Has_Formal_Or_Result_Of_Deep_Type
1473 (Id : Entity_Id) return Boolean;
1474 -- Returns true if the subprogram has at least one formal parameter or
1475 -- a return type of a deep type: either an access type or a composite
1476 -- type containing an access type.
1477
1478 function Has_Formal_With_Per_Object_Constrained_Component
1479 (Id : Entity_Id) return Boolean;
1480 -- Returns true if the subprogram has at least one formal parameter of
1481 -- an unconstrained record type with per-object constraints on component
1482 -- types.
1483
1484 function Has_Hide_Unhide_Annotation
1485 (Spec_Id, Body_Id : Entity_Id)
1486 return Boolean;
1487 -- Returns whether the subprogram has an annotation Hide_Info or
1488 -- Unhide_Info on its spec or body.
1489
1490 function Has_Skip_Proof_Annotation (Id : Entity_Id) return Boolean;
1491 -- Returns True if subprogram Id has an annotation Skip_Proof or
1492 -- Skip_Flow_And_Proof.
1493
1494 function Has_Some_Contract (Id : Entity_Id) return Boolean;
1495 -- Return True if subprogram Id has any contract. The presence of
1496 -- Extensions_Visible or Volatile_Function is also considered as a
1497 -- contract here.
1498
1499 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
1500 -- Return True if subprogram Id defines a compilation unit
1501
1502 function In_Package_Spec (Id : Entity_Id) return Boolean;
1503 -- Return True if subprogram Id is defined in the package specification,
1504 -- either its visible or private part.
1505
1506 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean;
1507 -- Return True if subprogram Id could be a traversal function, as
1508 -- defined in SPARK RM 3.10. This is only a safe approximation, as the
1509 -- knowledge of the SPARK boundary is needed to determine exactly
1510 -- traversal functions.
1511
1512 --------------------------------------
1513 -- Has_Constant_With_Address_Clause --
1514 --------------------------------------
1515
1516 function Has_Constant_With_Address_Clause
1517 (Body_Node : Node_Id)
1518 return Boolean
1519 is
1520 function Check_Constant_With_Addresss_Clause
1521 (N : Node_Id)
1522 return Traverse_Result;
1523 -- Returns Abandon on node N if this is a declaration of a constant
1524 -- object with an address clause.
1525
1526 -----------------------------------------
1527 -- Check_Constant_With_Addresss_Clause --
1528 -----------------------------------------
1529
1530 function Check_Constant_With_Addresss_Clause
1531 (N : Node_Id)
1532 return Traverse_Result
1533 is
1534 begin
1535 case Nkind (N) is
1536 when N_Object_Declaration =>
1537 declare
1538 Obj : constant Entity_Id := Defining_Entity (N);
1539 begin
1540 if Constant_Present (N)
1541 and then
1542 (Present (Address_Clause (Obj))
1543 or else Has_Aspect (Obj, Aspect_Address))
1544 then
1545 return Abandon;
1546 else
1547 return OK;
1548 end if;
1549 end;
1550
1551 -- Skip locally declared subprogram bodies inside the body to
1552 -- inline, as the declarations inside those do not count.
1553
1554 when N_Subprogram_Body =>
1555 if N = Body_Node then
1556 return OK;
1557 else
1558 return Skip;
1559 end if;
1560
1561 when others =>
1562 return OK;
1563 end case;
1564 end Check_Constant_With_Addresss_Clause;
1565
1566 function Check_All_Constants_With_Address_Clause is new
1567 Traverse_Func (Check_Constant_With_Addresss_Clause);
1568
1569 -- Start of processing for Has_Constant_With_Address_Clause
1570
1571 begin
1572 return Check_All_Constants_With_Address_Clause
1573 (Body_Node) = Abandon;
1574 end Has_Constant_With_Address_Clause;
1575
1576 ---------------------------------------
1577 -- Has_Formal_Or_Result_Of_Deep_Type --
1578 ---------------------------------------
1579
1580 function Has_Formal_Or_Result_Of_Deep_Type
1581 (Id : Entity_Id) return Boolean
1582 is
1583 function Is_Deep (Typ : Entity_Id) return Boolean;
1584 -- Return True if Typ is deep: either an access type or a composite
1585 -- type containing an access type.
1586
1587 -------------
1588 -- Is_Deep --
1589 -------------
1590
1591 function Is_Deep (Typ : Entity_Id) return Boolean is
1592 begin
1593 case Type_Kind'(Ekind (Typ)) is
1594 when Access_Kind =>
1595 return True;
1596
1597 when E_Array_Type
1598 | E_Array_Subtype
1599 =>
1600 return Is_Deep (Component_Type (Typ));
1601
1602 when Record_Kind =>
1603 declare
1604 Comp : Entity_Id := First_Component_Or_Discriminant (Typ);
1605 begin
1606 while Present (Comp) loop
1607 if Is_Deep (Etype (Comp)) then
1608 return True;
1609 end if;
1610 Next_Component_Or_Discriminant (Comp);
1611 end loop;
1612 end;
1613 return False;
1614
1615 when Scalar_Kind
1616 | E_String_Literal_Subtype
1617 | Concurrent_Kind
1618 | Incomplete_Kind
1619 | E_Exception_Type
1620 | E_Subprogram_Type
1621 =>
1622 return False;
1623
1624 when E_Private_Type
1625 | E_Private_Subtype
1626 | E_Limited_Private_Type
1627 | E_Limited_Private_Subtype
1628 =>
1629 -- Conservatively consider that the type might be deep if
1630 -- its completion has not been seen yet.
1631
1632 if No (Underlying_Type (Typ)) then
1633 return True;
1634
1635 -- Do not peek under a private type if its completion has
1636 -- SPARK_Mode Off. In such a case, a deep type is considered
1637 -- by GNATprove to be not deep.
1638
1639 elsif Present (Full_View (Typ))
1640 and then Present (SPARK_Pragma (Full_View (Typ)))
1641 and then Get_SPARK_Mode_From_Annotation
1642 (SPARK_Pragma (Full_View (Typ))) = Off
1643 then
1644 return False;
1645
1646 -- Otherwise peek under the private type.
1647
1648 else
1649 return Is_Deep (Underlying_Type (Typ));
1650 end if;
1651 end case;
1652 end Is_Deep;
1653
1654 -- Local variables
1655
1656 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1657 Formal : Entity_Id;
1658 Formal_Typ : Entity_Id;
1659
1660 -- Start of processing for Has_Formal_Or_Result_Of_Deep_Type
1661
1662 begin
1663 -- Inspect all parameters of the subprogram looking for a formal
1664 -- of a deep type.
1665
1666 Formal := First_Formal (Subp_Id);
1667 while Present (Formal) loop
1668 Formal_Typ := Etype (Formal);
1669
1670 if Is_Deep (Formal_Typ) then
1671 return True;
1672 end if;
1673
1674 Next_Formal (Formal);
1675 end loop;
1676
1677 -- Check whether this is a function whose return type is deep
1678
1679 if Ekind (Subp_Id) = E_Function
1680 and then Is_Deep (Etype (Subp_Id))
1681 then
1682 return True;
1683 end if;
1684
1685 return False;
1686 end Has_Formal_Or_Result_Of_Deep_Type;
1687
1688 ------------------------------------------------------
1689 -- Has_Formal_With_Per_Object_Constrained_Component --
1690 ------------------------------------------------------
1691
1692 function Has_Formal_With_Per_Object_Constrained_Component
1693 (Id : Entity_Id) return Boolean
1694 is
1695 function Has_Per_Object_Constrained_Component
1696 (Typ : Entity_Id) return Boolean;
1697 -- Determine whether unconstrained record type Typ has at least one
1698 -- component that depends on a discriminant.
1699
1700 ------------------------------------------
1701 -- Has_Per_Object_Constrained_Component --
1702 ------------------------------------------
1703
1704 function Has_Per_Object_Constrained_Component
1705 (Typ : Entity_Id) return Boolean
1706 is
1707 Comp : Entity_Id;
1708
1709 begin
1710 -- Inspect all components of the record type looking for one that
1711 -- depends on a discriminant.
1712
1713 Comp := First_Component (Typ);
1714 while Present (Comp) loop
1715 if Has_Per_Object_Constraint (Comp) then
1716 return True;
1717 end if;
1718
1719 Next_Component (Comp);
1720 end loop;
1721
1722 return False;
1723 end Has_Per_Object_Constrained_Component;
1724
1725 -- Local variables
1726
1727 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1728 Formal : Entity_Id;
1729 Formal_Typ : Entity_Id;
1730
1731 -- Start of processing for
1732 -- Has_Formal_With_Per_Object_Constrained_Component
1733
1734 begin
1735 -- Inspect all parameters of the subprogram looking for a formal
1736 -- of an unconstrained record type with at least one discriminant
1737 -- dependent component.
1738
1739 Formal := First_Formal (Subp_Id);
1740 while Present (Formal) loop
1741 Formal_Typ := Etype (Formal);
1742
1743 if Is_Record_Type (Formal_Typ)
1744 and then not Is_Constrained (Formal_Typ)
1745 and then Has_Per_Object_Constrained_Component (Formal_Typ)
1746 then
1747 return True;
1748 end if;
1749
1750 Next_Formal (Formal);
1751 end loop;
1752
1753 return False;
1754 end Has_Formal_With_Per_Object_Constrained_Component;
1755
1756 --------------------------------
1757 -- Has_Hide_Unhide_Annotation --
1758 --------------------------------
1759
1760 function Has_Hide_Unhide_Annotation
1761 (Spec_Id, Body_Id : Entity_Id)
1762 return Boolean
1763 is
1764 function Has_Hide_Unhide_Pragma (Prag : Node_Id) return Boolean;
1765 -- Return whether a pragma Hide/Unhide is present in the list of
1766 -- pragmas starting with Prag.
1767
1768 ----------------------------
1769 -- Has_Hide_Unhide_Pragma --
1770 ----------------------------
1771
1772 function Has_Hide_Unhide_Pragma (Prag : Node_Id) return Boolean is
1773 Decl : Node_Id := Prag;
1774 begin
1775 while Present (Decl)
1776 and then Nkind (Decl) = N_Pragma
1777 loop
1778 if Get_Pragma_Id (Decl) = Pragma_Annotate
1779 and then List_Length (Pragma_Argument_Associations (Decl)) = 4
1780 then
1781 declare
1782 Arg1 : constant Node_Id :=
1783 First (Pragma_Argument_Associations (Decl));
1784 Arg2 : constant Node_Id := Next (Arg1);
1785 Arg1_Name : constant Name_Id :=
1786 Chars (Get_Pragma_Arg (Arg1));
1787 Arg2_Name : constant String :=
1788 Get_Name_String (Chars (Get_Pragma_Arg (Arg2)));
1789 begin
1790 if Arg1_Name = Name_Gnatprove
1791 and then Arg2_Name in "hide_info" | "unhide_info"
1792 then
1793 return True;
1794 end if;
1795 end;
1796 end if;
1797
1798 Next (Decl);
1799 end loop;
1800
1801 return False;
1802 end Has_Hide_Unhide_Pragma;
1803
1804 begin
1805 if Present (Spec_Id)
1806 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
1807 and then Has_Hide_Unhide_Pragma
1808 (Next (Unit_Declaration_Node (Spec_Id)))
1809 then
1810 return True;
1811
1812 elsif Present (Body_Id) then
1813 declare
1814 Subp_Body : constant N_Subprogram_Body_Id :=
1815 Unit_Declaration_Node (Body_Id);
1816 begin
1817 return
1818 (Is_List_Member (Subp_Body)
1819 and then Has_Hide_Unhide_Pragma (Next (Subp_Body)))
1820 or else
1821 Has_Hide_Unhide_Pragma (First (Declarations (Subp_Body)));
1822 end;
1823
1824 else
1825 return False;
1826 end if;
1827 end Has_Hide_Unhide_Annotation;
1828
1829 -------------------------------
1830 -- Has_Skip_Proof_Annotation --
1831 -------------------------------
1832
1833 function Has_Skip_Proof_Annotation (Id : Entity_Id) return Boolean is
1834 Decl : Node_Id := Unit_Declaration_Node (Id);
1835
1836 begin
1837 Next (Decl);
1838
1839 while Present (Decl)
1840 and then Nkind (Decl) = N_Pragma
1841 loop
1842 if Get_Pragma_Id (Decl) = Pragma_Annotate
1843 and then List_Length (Pragma_Argument_Associations (Decl)) = 3
1844 then
1845 declare
1846 Arg1 : constant Node_Id :=
1847 First (Pragma_Argument_Associations (Decl));
1848 Arg2 : constant Node_Id := Next (Arg1);
1849 Arg1_Name : constant Name_Id :=
1850 Chars (Get_Pragma_Arg (Arg1));
1851 Arg2_Name : constant String :=
1852 Get_Name_String (Chars (Get_Pragma_Arg (Arg2)));
1853 begin
1854 if Arg1_Name = Name_Gnatprove
1855 and then Arg2_Name in "skip_proof" | "skip_flow_and_proof"
1856 then
1857 return True;
1858 end if;
1859 end;
1860 end if;
1861
1862 Next (Decl);
1863 end loop;
1864
1865 return False;
1866 end Has_Skip_Proof_Annotation;
1867
1868 -----------------------
1869 -- Has_Some_Contract --
1870 -----------------------
1871
1872 function Has_Some_Contract (Id : Entity_Id) return Boolean is
1873 Items : Node_Id;
1874
1875 begin
1876 -- A call to an expression function may precede the actual body which
1877 -- is inserted at the end of the enclosing declarations. Ensure that
1878 -- the related entity is decorated before inspecting the contract.
1879
1880 if Is_Subprogram_Or_Generic_Subprogram (Id) then
1881 Items := Contract (Id);
1882
1883 -- Note that Classifications is not Empty when Extensions_Visible
1884 -- or Volatile_Function is present, which causes such subprograms
1885 -- to be considered to have a contract here. This is fine as we
1886 -- want to avoid inlining these too.
1887
1888 return Present (Items)
1889 and then (Present (Pre_Post_Conditions (Items)) or else
1890 Present (Contract_Test_Cases (Items)) or else
1891 Present (Classifications (Items)));
1892 end if;
1893
1894 return False;
1895 end Has_Some_Contract;
1896
1897 ---------------------
1898 -- In_Package_Spec --
1899 ---------------------
1900
1901 function In_Package_Spec (Id : Entity_Id) return Boolean is
1902 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1903 -- Parent of the subprogram's declaration
1904
1905 begin
1906 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1907 end In_Package_Spec;
1908
1909 ------------------------
1910 -- Is_Unit_Subprogram --
1911 ------------------------
1912
1913 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1914 Decl : Node_Id := Parent (Parent (Id));
1915 begin
1916 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1917 Decl := Parent (Decl);
1918 end if;
1919
1920 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1921 end Is_Unit_Subprogram;
1922
1923 ------------------------------
1924 -- Maybe_Traversal_Function --
1925 ------------------------------
1926
1927 function Maybe_Traversal_Function (Id : Entity_Id) return Boolean is
1928 begin
1929 return Ekind (Id) = E_Function
1930
1931 -- Only traversal functions return an anonymous access-to-object
1932 -- type in SPARK.
1933
1934 and then Is_Anonymous_Access_Type (Etype (Id));
1935 end Maybe_Traversal_Function;
1936
1937 -- Local declarations
1938
1939 Id : Entity_Id;
1940 -- Procedure or function entity for the subprogram
1941
1942 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
1943
1944 begin
1945 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1946
1947 if Present (Spec_Id) then
1948 Id := Spec_Id;
1949 else
1950 Id := Body_Id;
1951 end if;
1952
1953 -- Only local subprograms without contracts are inlined in GNATprove
1954 -- mode, as these are the subprograms which a user is not interested in
1955 -- analyzing in isolation, but rather in the context of their call. This
1956 -- is a convenient convention, that could be changed for an explicit
1957 -- pragma/aspect one day.
1958
1959 -- In a number of special cases, inlining is not desirable or not
1960 -- possible, see below.
1961
1962 -- Do not inline unit-level subprograms
1963
1964 if Is_Unit_Subprogram (Id) then
1965 return False;
1966
1967 -- Do not inline subprograms declared in package specs, because they are
1968 -- not local, i.e. can be called either from anywhere (if declared in
1969 -- visible part) or from the child units (if declared in private part).
1970
1971 elsif In_Package_Spec (Id) then
1972 return False;
1973
1974 -- Do not inline subprograms declared in other units. This is important
1975 -- in particular for subprograms defined in the private part of a
1976 -- package spec, when analyzing one of its child packages, as otherwise
1977 -- we issue spurious messages about the impossibility to inline such
1978 -- calls.
1979
1980 elsif not In_Extended_Main_Code_Unit (Id) then
1981 return False;
1982
1983 -- Do not inline dispatching operations, as only their static calls
1984 -- can be analyzed in context, and not their dispatching calls.
1985
1986 elsif Is_Dispatching_Operation (Id) then
1987 return False;
1988
1989 -- Do not inline subprograms marked No_Return, possibly used for
1990 -- signaling errors, which GNATprove handles specially.
1991
1992 elsif No_Return (Id) then
1993 return False;
1994
1995 -- Do not inline subprograms that have a contract on the spec or the
1996 -- body. Use the contract(s) instead in GNATprove. This also prevents
1997 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
1998
1999 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
2000 or else
2001 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
2002 then
2003 return False;
2004
2005 -- Do not inline expression functions, which are directly inlined at the
2006 -- prover level.
2007
2008 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
2009 or else
2010 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
2011 then
2012 return False;
2013
2014 -- Do not inline generic subprogram instances. The visibility rules of
2015 -- generic instances plays badly with inlining.
2016
2017 elsif Is_Generic_Instance (Spec_Id) then
2018 return False;
2019
2020 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
2021 -- the subprogram body, a similar check is performed after the body
2022 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
2023
2024 elsif Present (Spec_Id)
2025 and then
2026 (No (SPARK_Pragma (Spec_Id))
2027 or else
2028 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
2029 then
2030 return False;
2031
2032 -- Do not inline subprograms and entries defined inside protected types,
2033 -- which typically are not helper subprograms, which also avoids getting
2034 -- spurious messages on calls that cannot be inlined.
2035
2036 elsif Within_Protected_Type (Id) then
2037 return False;
2038
2039 -- Do not inline predicate functions (treated specially by GNATprove)
2040
2041 elsif Is_Predicate_Function (Id) then
2042 return False;
2043
2044 -- Do not inline subprograms with a parameter of an unconstrained
2045 -- record type if it has discrimiant dependent fields. Indeed, with
2046 -- such parameters, the frontend cannot always ensure type compliance
2047 -- in record component accesses (in particular with records containing
2048 -- packed arrays).
2049
2050 elsif Has_Formal_With_Per_Object_Constrained_Component (Id) then
2051 return False;
2052
2053 -- Do not inline subprograms with a formal parameter or return type of
2054 -- a deep type, as in that case inlining might generate code that
2055 -- violates borrow-checking rules of SPARK 3.10 even if the original
2056 -- code did not.
2057
2058 elsif Has_Formal_Or_Result_Of_Deep_Type (Id) then
2059 return False;
2060
2061 -- Do not inline subprograms which may be traversal functions. Such
2062 -- inlining introduces temporary variables of named access type for
2063 -- which assignments are move instead of borrow/observe, possibly
2064 -- leading to spurious errors when checking SPARK rules related to
2065 -- pointer usage.
2066
2067 elsif Maybe_Traversal_Function (Id) then
2068 return False;
2069
2070 -- Do not inline subprograms with the Skip_Proof or Skip_Flow_And_Proof
2071 -- annotation, which should be handled separately.
2072
2073 elsif Has_Skip_Proof_Annotation (Id) then
2074 return False;
2075
2076 -- Do not inline subprograms with the Hide_Info or Unhide_Info
2077 -- annotation, since their scope has special visibility on the
2078 -- precise definition of some entities.
2079
2080 elsif Has_Hide_Unhide_Annotation (Spec_Id, Body_Id) then
2081 return False;
2082
2083 -- Do not inline subprograms containing constant declarations with an
2084 -- address clause, as inlining could lead to a spurious violation of
2085 -- SPARK rules.
2086
2087 elsif Present (Body_Id)
2088 and then
2089 Has_Constant_With_Address_Clause (Unit_Declaration_Node (Body_Id))
2090 then
2091 return False;
2092
2093 -- Otherwise, this is a subprogram declared inside the private part of a
2094 -- package, or inside a package body, or locally in a subprogram, and it
2095 -- does not have any contract. Inline it.
2096
2097 else
2098 return True;
2099 end if;
2100 end Can_Be_Inlined_In_GNATprove_Mode;
2101
2102 -------------------
2103 -- Cannot_Inline --
2104 -------------------
2105
2106 procedure Cannot_Inline
2107 (Msg : String;
2108 N : Node_Id;
2109 Subp : Entity_Id;
2110 Is_Serious : Boolean := False;
2111 Suppress_Info : Boolean := False)
2112 is
2113 Inline_Prefix : constant String := "cannot inline";
2114
2115 function Starts_With (S, Prefix : String) return Boolean is
2116 (S (S'First .. S'First + Prefix'Length - 1) = Prefix);
2117
2118 begin
2119 -- In GNATprove mode, inlining is the technical means by which the
2120 -- higher-level goal of contextual analysis is reached, so issue
2121 -- messages about failure to apply contextual analysis to a
2122 -- subprogram, rather than failure to inline it.
2123
2124 if GNATprove_Mode
2125 and then Starts_With (Msg, Inline_Prefix)
2126 then
2127 declare
2128 Msg_Txt : constant String :=
2129 Msg (Msg'First + Inline_Prefix'Length .. Msg'Last);
2130
2131 New_Msg : constant String :=
2132 "info: no contextual analysis of" & Msg_Txt;
2133 begin
2134 Cannot_Inline (New_Msg, N, Subp, Is_Serious, Suppress_Info);
2135 return;
2136 end;
2137 end if;
2138
2139 -- Legacy front-end inlining model
2140
2141 if not Back_End_Inlining then
2142
2143 -- Do not emit warning if this is a predefined unit which is not
2144 -- the main unit. With validity checks enabled, some predefined
2145 -- subprograms may contain nested subprograms and become ineligible
2146 -- for inlining.
2147
2148 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2149 and then not In_Extended_Main_Source_Unit (Subp)
2150 then
2151 null;
2152
2153 -- In GNATprove mode, issue an info message when -gnatd_f is set and
2154 -- Suppress_Info is False, and indicate that the subprogram is not
2155 -- always inlined by setting flag Is_Inlined_Always to False.
2156
2157 elsif GNATprove_Mode then
2158 Set_Is_Inlined_Always (Subp, False);
2159
2160 if Debug_Flag_Underscore_F and not Suppress_Info then
2161 Error_Msg_NE (Msg, N, Subp);
2162 end if;
2163
2164 elsif Has_Pragma_Inline_Always (Subp) then
2165
2166 -- Remove last character (question mark) to make this into an
2167 -- error, because the Inline_Always pragma cannot be obeyed.
2168
2169 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2170
2171 elsif Ineffective_Inline_Warnings then
2172 Error_Msg_NE (Msg & "p?", N, Subp);
2173 end if;
2174
2175 -- New semantics relying on back-end inlining
2176
2177 elsif Is_Serious then
2178
2179 -- Remove last character (question mark) to make this into an error.
2180
2181 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2182
2183 else
2184
2185 -- Do not emit warning if this is a predefined unit which is not
2186 -- the main unit. This behavior is currently provided for backward
2187 -- compatibility but it will be removed when we enforce the
2188 -- strictness of the new rules.
2189
2190 if Is_Predefined_Unit (Get_Source_Unit (Subp))
2191 and then not In_Extended_Main_Source_Unit (Subp)
2192 then
2193 null;
2194
2195 elsif Has_Pragma_Inline_Always (Subp) then
2196
2197 -- Emit a warning if this is a call to a runtime subprogram
2198 -- which is located inside a generic. Previously this call
2199 -- was silently skipped.
2200
2201 if Is_Generic_Instance (Subp) then
2202 declare
2203 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
2204 begin
2205 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
2206 Set_Is_Inlined (Subp, False);
2207 Error_Msg_NE (Msg & "p?", N, Subp);
2208 return;
2209 end if;
2210 end;
2211 end if;
2212
2213 -- Remove last character (question mark) to make this into an
2214 -- error, because the Inline_Always pragma cannot be obeyed.
2215
2216 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
2217
2218 else
2219 Set_Is_Inlined (Subp, False);
2220
2221 if Ineffective_Inline_Warnings then
2222 Error_Msg_NE (Msg & "p?", N, Subp);
2223 end if;
2224 end if;
2225 end if;
2226 end Cannot_Inline;
2227
2228 --------------------------------------------
2229 -- Check_And_Split_Unconstrained_Function --
2230 --------------------------------------------
2231
2232 procedure Check_And_Split_Unconstrained_Function
2233 (N : Node_Id;
2234 Spec_Id : Entity_Id;
2235 Body_Id : Entity_Id)
2236 is
2237 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
2238 -- Use generic machinery to build an unexpanded body for the subprogram.
2239 -- This body is subsequently used for inline expansions at call sites.
2240
2241 procedure Build_Return_Object_Formal
2242 (Loc : Source_Ptr;
2243 Obj_Decl : Node_Id;
2244 Formals : List_Id);
2245 -- Create a formal parameter for return object declaration Obj_Decl of
2246 -- an extended return statement and add it to list Formals.
2247
2248 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
2249 -- Return true if we generate code for the function body N, the function
2250 -- body N has no local declarations and its unique statement is a single
2251 -- extended return statement with a handled statements sequence.
2252
2253 procedure Copy_Formals
2254 (Loc : Source_Ptr;
2255 Subp_Id : Entity_Id;
2256 Formals : List_Id);
2257 -- Create new formal parameters from the formal parameters of subprogram
2258 -- Subp_Id and add them to list Formals.
2259
2260 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id;
2261 -- Create a copy of return object declaration Obj_Decl of an extended
2262 -- return statement.
2263
2264 procedure Split_Unconstrained_Function
2265 (N : Node_Id;
2266 Spec_Id : Entity_Id);
2267 -- N is an inlined function body that returns an unconstrained type and
2268 -- has a single extended return statement. Split N in two subprograms:
2269 -- a procedure P' and a function F'. The formals of P' duplicate the
2270 -- formals of N plus an extra formal which is used to return a value;
2271 -- its body is composed by the declarations and list of statements
2272 -- of the extended return statement of N.
2273
2274 --------------------------
2275 -- Build_Body_To_Inline --
2276 --------------------------
2277
2278 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
2279 procedure Generate_Subprogram_Body
2280 (N : Node_Id;
2281 Body_To_Inline : out Node_Id);
2282 -- Generate a parameterless duplicate of subprogram body N. Note that
2283 -- occurrences of pragmas referencing the formals are removed since
2284 -- they have no meaning when the body is inlined and the formals are
2285 -- rewritten (the analysis of the non-inlined body will handle these
2286 -- pragmas). A new internal name is associated with Body_To_Inline.
2287
2288 ------------------------------
2289 -- Generate_Subprogram_Body --
2290 ------------------------------
2291
2292 procedure Generate_Subprogram_Body
2293 (N : Node_Id;
2294 Body_To_Inline : out Node_Id)
2295 is
2296 begin
2297 -- Within an instance, the body to inline must be treated as a
2298 -- nested generic so that proper global references are preserved.
2299
2300 -- Note that we do not do this at the library level, because it
2301 -- is not needed, and furthermore this causes trouble if front
2302 -- end inlining is activated (-gnatN).
2303
2304 if In_Instance
2305 and then Scope (Current_Scope) /= Standard_Standard
2306 then
2307 Body_To_Inline :=
2308 Copy_Generic_Node (N, Empty, Instantiating => True);
2309 else
2310 Body_To_Inline := New_Copy_Tree (N);
2311 end if;
2312
2313 -- Remove aspects/pragmas that have no meaning in an inlined body
2314
2315 Remove_Aspects_And_Pragmas (Body_To_Inline);
2316
2317 -- We need to capture references to the formals in order
2318 -- to substitute the actuals at the point of inlining, i.e.
2319 -- instantiation. To treat the formals as globals to the body to
2320 -- inline, we nest it within a dummy parameterless subprogram,
2321 -- declared within the real one.
2322
2323 Set_Parameter_Specifications
2324 (Specification (Body_To_Inline), No_List);
2325
2326 -- A new internal name is associated with Body_To_Inline to avoid
2327 -- conflicts when the non-inlined body N is analyzed.
2328
2329 Set_Defining_Unit_Name (Specification (Body_To_Inline),
2330 Make_Temporary (Sloc (N), 'P'));
2331 Set_Corresponding_Spec (Body_To_Inline, Empty);
2332 end Generate_Subprogram_Body;
2333
2334 -- Local variables
2335
2336 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2337 Original_Body : Node_Id;
2338 Body_To_Analyze : Node_Id;
2339
2340 -- Start of processing for Build_Body_To_Inline
2341
2342 begin
2343 pragma Assert (Current_Scope = Spec_Id);
2344
2345 -- Within an instance, the body to inline must be treated as a nested
2346 -- generic, so that the proper global references are preserved. We
2347 -- do not do this at the library level, because it is not needed, and
2348 -- furthermore this causes trouble if front-end inlining is activated
2349 -- (-gnatN).
2350
2351 if In_Instance
2352 and then Scope (Current_Scope) /= Standard_Standard
2353 then
2354 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2355 end if;
2356
2357 -- Capture references to formals in order to substitute the actuals
2358 -- at the point of inlining or instantiation. To treat the formals
2359 -- as globals to the body to inline, nest the body within a dummy
2360 -- parameterless subprogram, declared within the real one.
2361
2362 Generate_Subprogram_Body (N, Original_Body);
2363 Body_To_Analyze :=
2364 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
2365
2366 -- Set return type of function, which is also global and does not
2367 -- need to be resolved.
2368
2369 if Ekind (Spec_Id) = E_Function then
2370 Set_Result_Definition (Specification (Body_To_Analyze),
2371 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
2372 end if;
2373
2374 if No (Declarations (N)) then
2375 Set_Declarations (N, New_List (Body_To_Analyze));
2376 else
2377 Append_To (Declarations (N), Body_To_Analyze);
2378 end if;
2379
2380 Preanalyze (Body_To_Analyze);
2381
2382 Push_Scope (Defining_Entity (Body_To_Analyze));
2383 Save_Global_References (Original_Body);
2384 End_Scope;
2385 Remove (Body_To_Analyze);
2386
2387 -- Restore environment if previously saved
2388
2389 if In_Instance
2390 and then Scope (Current_Scope) /= Standard_Standard
2391 then
2392 Restore_Env;
2393 end if;
2394
2395 pragma Assert (No (Body_To_Inline (Decl)));
2396 Set_Body_To_Inline (Decl, Original_Body);
2397 Mutate_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
2398 end Build_Body_To_Inline;
2399
2400 --------------------------------
2401 -- Build_Return_Object_Formal --
2402 --------------------------------
2403
2404 procedure Build_Return_Object_Formal
2405 (Loc : Source_Ptr;
2406 Obj_Decl : Node_Id;
2407 Formals : List_Id)
2408 is
2409 Obj_Def : constant Node_Id := Object_Definition (Obj_Decl);
2410 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2411 Typ_Def : Node_Id;
2412
2413 begin
2414 -- Build the type definition of the formal parameter. The use of
2415 -- New_Copy_Tree ensures that global references preserved in the
2416 -- case of generics.
2417
2418 if Is_Entity_Name (Obj_Def) then
2419 Typ_Def := New_Copy_Tree (Obj_Def);
2420 else
2421 Typ_Def := New_Copy_Tree (Subtype_Mark (Obj_Def));
2422 end if;
2423
2424 -- Generate:
2425 --
2426 -- Obj_Id : [out] Typ_Def
2427
2428 -- Mode OUT should not be used when the return object is declared as
2429 -- a constant. Check the definition of the object declaration because
2430 -- the object has not been analyzed yet.
2431
2432 Append_To (Formals,
2433 Make_Parameter_Specification (Loc,
2434 Defining_Identifier =>
2435 Make_Defining_Identifier (Loc, Chars (Obj_Id)),
2436 In_Present => False,
2437 Out_Present => not Constant_Present (Obj_Decl),
2438 Null_Exclusion_Present => False,
2439 Parameter_Type => Typ_Def));
2440 end Build_Return_Object_Formal;
2441
2442 --------------------------------------
2443 -- Can_Split_Unconstrained_Function --
2444 --------------------------------------
2445
2446 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
2447 Stmt : constant Node_Id :=
2448 First (Statements (Handled_Statement_Sequence (N)));
2449 Decl : Node_Id;
2450
2451 begin
2452 -- No user defined declarations allowed in the function except inside
2453 -- the unique return statement; implicit labels are the only allowed
2454 -- declarations.
2455
2456 Decl := First (Declarations (N));
2457 while Present (Decl) loop
2458 if Nkind (Decl) /= N_Implicit_Label_Declaration then
2459 return False;
2460 end if;
2461
2462 Next (Decl);
2463 end loop;
2464
2465 -- We only split the inlined function when we are generating the code
2466 -- of its body; otherwise we leave duplicated split subprograms in
2467 -- the tree which (if referenced) generate wrong references at link
2468 -- time.
2469
2470 return In_Extended_Main_Code_Unit (N)
2471 and then Present (Stmt)
2472 and then Nkind (Stmt) = N_Extended_Return_Statement
2473 and then No (Next (Stmt))
2474 and then Present (Handled_Statement_Sequence (Stmt));
2475 end Can_Split_Unconstrained_Function;
2476
2477 ------------------
2478 -- Copy_Formals --
2479 ------------------
2480
2481 procedure Copy_Formals
2482 (Loc : Source_Ptr;
2483 Subp_Id : Entity_Id;
2484 Formals : List_Id)
2485 is
2486 Formal : Entity_Id;
2487 Spec : Node_Id;
2488
2489 begin
2490 Formal := First_Formal (Subp_Id);
2491 while Present (Formal) loop
2492 Spec := Parent (Formal);
2493
2494 -- Create an exact copy of the formal parameter. The use of
2495 -- New_Copy_Tree ensures that global references are preserved
2496 -- in case of generics.
2497
2498 Append_To (Formals,
2499 Make_Parameter_Specification (Loc,
2500 Defining_Identifier =>
2501 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
2502 In_Present => In_Present (Spec),
2503 Out_Present => Out_Present (Spec),
2504 Null_Exclusion_Present => Null_Exclusion_Present (Spec),
2505 Parameter_Type =>
2506 New_Copy_Tree (Parameter_Type (Spec)),
2507 Expression => New_Copy_Tree (Expression (Spec))));
2508
2509 Next_Formal (Formal);
2510 end loop;
2511 end Copy_Formals;
2512
2513 ------------------------
2514 -- Copy_Return_Object --
2515 ------------------------
2516
2517 function Copy_Return_Object (Obj_Decl : Node_Id) return Node_Id is
2518 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
2519
2520 begin
2521 -- The use of New_Copy_Tree ensures that global references are
2522 -- preserved in case of generics.
2523
2524 return
2525 Make_Object_Declaration (Sloc (Obj_Decl),
2526 Defining_Identifier =>
2527 Make_Defining_Identifier (Sloc (Obj_Id), Chars (Obj_Id)),
2528 Aliased_Present => Aliased_Present (Obj_Decl),
2529 Constant_Present => Constant_Present (Obj_Decl),
2530 Null_Exclusion_Present => Null_Exclusion_Present (Obj_Decl),
2531 Object_Definition =>
2532 New_Copy_Tree (Object_Definition (Obj_Decl)),
2533 Expression => New_Copy_Tree (Expression (Obj_Decl)));
2534 end Copy_Return_Object;
2535
2536 ----------------------------------
2537 -- Split_Unconstrained_Function --
2538 ----------------------------------
2539
2540 procedure Split_Unconstrained_Function
2541 (N : Node_Id;
2542 Spec_Id : Entity_Id)
2543 is
2544 Loc : constant Source_Ptr := Sloc (N);
2545 Ret_Stmt : constant Node_Id :=
2546 First (Statements (Handled_Statement_Sequence (N)));
2547 Ret_Obj : constant Node_Id :=
2548 First (Return_Object_Declarations (Ret_Stmt));
2549
2550 procedure Build_Procedure
2551 (Proc_Id : out Entity_Id;
2552 Decl_List : out List_Id);
2553 -- Build a procedure containing the statements found in the extended
2554 -- return statement of the unconstrained function body N.
2555
2556 ---------------------
2557 -- Build_Procedure --
2558 ---------------------
2559
2560 procedure Build_Procedure
2561 (Proc_Id : out Entity_Id;
2562 Decl_List : out List_Id)
2563 is
2564 Formals : constant List_Id := New_List;
2565 Subp_Name : constant Name_Id := New_Internal_Name ('F');
2566
2567 Body_Decls : List_Id := No_List;
2568 Decl : Node_Id;
2569 Proc_Body : Node_Id;
2570 Proc_Spec : Node_Id;
2571
2572 begin
2573 -- Create formal parameters for the return object and all formals
2574 -- of the unconstrained function in order to pass their values to
2575 -- the procedure.
2576
2577 Build_Return_Object_Formal
2578 (Loc => Loc,
2579 Obj_Decl => Ret_Obj,
2580 Formals => Formals);
2581
2582 Copy_Formals
2583 (Loc => Loc,
2584 Subp_Id => Spec_Id,
2585 Formals => Formals);
2586
2587 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
2588
2589 Proc_Spec :=
2590 Make_Procedure_Specification (Loc,
2591 Defining_Unit_Name => Proc_Id,
2592 Parameter_Specifications => Formals);
2593
2594 Decl_List := New_List;
2595
2596 Append_To (Decl_List,
2597 Make_Subprogram_Declaration (Loc, Proc_Spec));
2598
2599 -- Can_Convert_Unconstrained_Function checked that the function
2600 -- has no local declarations except implicit label declarations.
2601 -- Copy these declarations to the built procedure.
2602
2603 if Present (Declarations (N)) then
2604 Body_Decls := New_List;
2605
2606 Decl := First (Declarations (N));
2607 while Present (Decl) loop
2608 pragma Assert (Nkind (Decl) = N_Implicit_Label_Declaration);
2609
2610 Append_To (Body_Decls,
2611 Make_Implicit_Label_Declaration (Loc,
2612 Make_Defining_Identifier (Loc,
2613 Chars => Chars (Defining_Identifier (Decl))),
2614 Label_Construct => Empty));
2615
2616 Next (Decl);
2617 end loop;
2618 end if;
2619
2620 pragma Assert (Present (Handled_Statement_Sequence (Ret_Stmt)));
2621
2622 Proc_Body :=
2623 Make_Subprogram_Body (Loc,
2624 Specification => Copy_Subprogram_Spec (Proc_Spec),
2625 Declarations => Body_Decls,
2626 Handled_Statement_Sequence =>
2627 New_Copy_Tree (Handled_Statement_Sequence (Ret_Stmt)));
2628
2629 Set_Defining_Unit_Name (Specification (Proc_Body),
2630 Make_Defining_Identifier (Loc, Subp_Name));
2631
2632 Append_To (Decl_List, Proc_Body);
2633 end Build_Procedure;
2634
2635 -- Local variables
2636
2637 New_Obj : constant Node_Id := Copy_Return_Object (Ret_Obj);
2638 Blk_Stmt : Node_Id;
2639 Proc_Call : Node_Id;
2640 Proc_Id : Entity_Id;
2641
2642 -- Start of processing for Split_Unconstrained_Function
2643
2644 begin
2645 -- Build the associated procedure, analyze it and insert it before
2646 -- the function body N.
2647
2648 declare
2649 Scope : constant Entity_Id := Current_Scope;
2650 Decl_List : List_Id;
2651 begin
2652 Pop_Scope;
2653 Build_Procedure (Proc_Id, Decl_List);
2654 Insert_Actions (N, Decl_List);
2655 Set_Is_Inlined (Proc_Id);
2656 Push_Scope (Scope);
2657 end;
2658
2659 -- Build the call to the generated procedure
2660
2661 declare
2662 Actual_List : constant List_Id := New_List;
2663 Formal : Entity_Id;
2664
2665 begin
2666 Append_To (Actual_List,
2667 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
2668
2669 Formal := First_Formal (Spec_Id);
2670 while Present (Formal) loop
2671 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
2672
2673 -- Avoid spurious warning on unreferenced formals
2674
2675 Set_Referenced (Formal);
2676 Next_Formal (Formal);
2677 end loop;
2678
2679 Proc_Call :=
2680 Make_Procedure_Call_Statement (Loc,
2681 Name => New_Occurrence_Of (Proc_Id, Loc),
2682 Parameter_Associations => Actual_List);
2683 end;
2684
2685 -- Generate:
2686
2687 -- declare
2688 -- New_Obj : ...
2689 -- begin
2690 -- Proc (New_Obj, ...);
2691 -- return New_Obj;
2692 -- end;
2693
2694 Blk_Stmt :=
2695 Make_Block_Statement (Loc,
2696 Declarations => New_List (New_Obj),
2697 Handled_Statement_Sequence =>
2698 Make_Handled_Sequence_Of_Statements (Loc,
2699 Statements => New_List (
2700
2701 Proc_Call,
2702
2703 Make_Simple_Return_Statement (Loc,
2704 Expression =>
2705 New_Occurrence_Of
2706 (Defining_Identifier (New_Obj), Loc)))));
2707
2708 Rewrite (Ret_Stmt, Blk_Stmt);
2709 end Split_Unconstrained_Function;
2710
2711 -- Local variables
2712
2713 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2714
2715 -- Start of processing for Check_And_Split_Unconstrained_Function
2716
2717 begin
2718 pragma Assert (Back_End_Inlining
2719 and then Ekind (Spec_Id) = E_Function
2720 and then Returns_Unconstrained_Type (Spec_Id)
2721 and then Comes_From_Source (Body_Id)
2722 and then (Has_Pragma_Inline_Always (Spec_Id)
2723 or else Optimization_Level > 0));
2724
2725 -- This routine must not be used in GNATprove mode since GNATprove
2726 -- relies on frontend inlining
2727
2728 pragma Assert (not GNATprove_Mode);
2729
2730 -- No need to split the function if we cannot generate the code
2731
2732 if Serious_Errors_Detected /= 0 then
2733 return;
2734 end if;
2735
2736 -- No action needed in stubs since the attribute Body_To_Inline
2737 -- is not available
2738
2739 if Nkind (Decl) = N_Subprogram_Body_Stub then
2740 return;
2741
2742 -- Cannot build the body to inline if the attribute is already set.
2743 -- This attribute may have been set if this is a subprogram renaming
2744 -- declarations (see Freeze.Build_Renamed_Body).
2745
2746 elsif Present (Body_To_Inline (Decl)) then
2747 return;
2748
2749 -- Do not generate a body to inline for protected functions, because the
2750 -- transformation generates a call to a protected procedure, causing
2751 -- spurious errors. We don't inline protected operations anyway, so
2752 -- this is no loss. We might as well ignore intrinsics and foreign
2753 -- conventions as well -- just allow Ada conventions.
2754
2755 elsif not (Convention (Spec_Id) = Convention_Ada
2756 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Copy
2757 or else Convention (Spec_Id) = Convention_Ada_Pass_By_Reference)
2758 then
2759 return;
2760
2761 -- Check excluded declarations
2762
2763 elsif Has_Excluded_Declaration (Spec_Id, Declarations (N)) then
2764 return;
2765
2766 -- Check excluded statements. There is no need to protect us against
2767 -- exception handlers since they are supported by the GCC backend.
2768
2769 elsif Present (Handled_Statement_Sequence (N))
2770 and then Has_Excluded_Statement
2771 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2772 then
2773 return;
2774 end if;
2775
2776 -- Build the body to inline only if really needed
2777
2778 if Can_Split_Unconstrained_Function (N) then
2779 Split_Unconstrained_Function (N, Spec_Id);
2780 Build_Body_To_Inline (N, Spec_Id);
2781 Set_Is_Inlined (Spec_Id);
2782 end if;
2783 end Check_And_Split_Unconstrained_Function;
2784
2785 ---------------------------------------------
2786 -- Check_Object_Renaming_In_GNATprove_Mode --
2787 ---------------------------------------------
2788
2789 procedure Check_Object_Renaming_In_GNATprove_Mode (Spec_Id : Entity_Id) is
2790 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2791 Body_Decl : constant Node_Id :=
2792 Unit_Declaration_Node (Corresponding_Body (Decl));
2793
2794 function Check_Object_Renaming (N : Node_Id) return Traverse_Result;
2795 -- Returns Abandon on node N if this is a reference to an object
2796 -- renaming, which will be expanded into the renamed object in
2797 -- GNATprove mode.
2798
2799 ---------------------------
2800 -- Check_Object_Renaming --
2801 ---------------------------
2802
2803 function Check_Object_Renaming (N : Node_Id) return Traverse_Result is
2804 begin
2805 case Nkind (Original_Node (N)) is
2806 when N_Expanded_Name
2807 | N_Identifier
2808 =>
2809 declare
2810 Obj_Id : constant Entity_Id := Entity (Original_Node (N));
2811 begin
2812 -- Recognize the case when SPARK expansion rewrites a
2813 -- reference to an object renaming.
2814
2815 if Present (Obj_Id)
2816 and then Is_Object (Obj_Id)
2817 and then Present (Renamed_Object (Obj_Id))
2818 and then Nkind (Renamed_Object (Obj_Id)) not in N_Entity
2819
2820 -- Copy_Generic_Node called for inlining expects the
2821 -- references to global entities to have the same kind
2822 -- in the "generic" code and its "instantiation".
2823
2824 and then Nkind (Original_Node (N)) /=
2825 Nkind (Renamed_Object (Obj_Id))
2826 then
2827 return Abandon;
2828 else
2829 return OK;
2830 end if;
2831 end;
2832
2833 when others =>
2834 return OK;
2835 end case;
2836 end Check_Object_Renaming;
2837
2838 function Check_All_Object_Renamings is new
2839 Traverse_Func (Check_Object_Renaming);
2840
2841 -- Start of processing for Check_Object_Renaming_In_GNATprove_Mode
2842
2843 begin
2844 -- Subprograms with object renamings replaced by the special SPARK
2845 -- expansion cannot be inlined.
2846
2847 if Check_All_Object_Renamings (Body_Decl) /= OK then
2848 Cannot_Inline ("cannot inline & (object renaming)?",
2849 Body_Decl, Spec_Id);
2850 Set_Body_To_Inline (Decl, Empty);
2851 end if;
2852 end Check_Object_Renaming_In_GNATprove_Mode;
2853
2854 -------------------------------------
2855 -- Check_Package_Body_For_Inlining --
2856 -------------------------------------
2857
2858 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
2859 Bname : Unit_Name_Type;
2860 E : Entity_Id;
2861 OK : Boolean;
2862
2863 begin
2864 -- Legacy implementation (relying on frontend inlining)
2865
2866 if not Back_End_Inlining
2867 and then Is_Compilation_Unit (P)
2868 and then not Is_Generic_Instance (P)
2869 then
2870 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2871
2872 E := First_Entity (P);
2873 while Present (E) loop
2874 if Has_Pragma_Inline_Always (E)
2875 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2876 then
2877 if not Is_Loaded (Bname) then
2878 Load_Needed_Body (N, OK);
2879
2880 if OK then
2881
2882 -- Check we are not trying to inline a parent whose body
2883 -- depends on a child, when we are compiling the body of
2884 -- the child. Otherwise we have a potential elaboration
2885 -- circularity with inlined subprograms and with
2886 -- Taft-Amendment types.
2887
2888 declare
2889 Comp : Node_Id; -- Body just compiled
2890 Child_Spec : Entity_Id; -- Spec of main unit
2891 Ent : Entity_Id; -- For iteration
2892 With_Clause : Node_Id; -- Context of body.
2893
2894 begin
2895 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2896 and then Present (Body_Entity (P))
2897 then
2898 Child_Spec :=
2899 Defining_Entity
2900 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2901
2902 Comp :=
2903 Parent (Unit_Declaration_Node (Body_Entity (P)));
2904
2905 -- Check whether the context of the body just
2906 -- compiled includes a child of itself, and that
2907 -- child is the spec of the main compilation.
2908
2909 With_Clause := First (Context_Items (Comp));
2910 while Present (With_Clause) loop
2911 if Nkind (With_Clause) = N_With_Clause
2912 and then
2913 Scope (Entity (Name (With_Clause))) = P
2914 and then
2915 Entity (Name (With_Clause)) = Child_Spec
2916 then
2917 Error_Msg_Node_2 := Child_Spec;
2918 Error_Msg_NE
2919 ("body of & depends on child unit&??",
2920 With_Clause, P);
2921 Error_Msg_N
2922 ("\subprograms in body cannot be inlined??",
2923 With_Clause);
2924
2925 -- Disable further inlining from this unit,
2926 -- and keep Taft-amendment types incomplete.
2927
2928 Ent := First_Entity (P);
2929 while Present (Ent) loop
2930 if Is_Type (Ent)
2931 and then Has_Completion_In_Body (Ent)
2932 then
2933 Set_Full_View (Ent, Empty);
2934
2935 elsif Is_Subprogram (Ent) then
2936 Set_Is_Inlined (Ent, False);
2937 end if;
2938
2939 Next_Entity (Ent);
2940 end loop;
2941
2942 return;
2943 end if;
2944
2945 Next (With_Clause);
2946 end loop;
2947 end if;
2948 end;
2949
2950 elsif Ineffective_Inline_Warnings then
2951 Error_Msg_Unit_1 := Bname;
2952 Error_Msg_N
2953 ("unable to inline subprograms defined in $?p?", P);
2954 Error_Msg_N ("\body not found?p?", P);
2955 return;
2956 end if;
2957 end if;
2958
2959 return;
2960 end if;
2961
2962 Next_Entity (E);
2963 end loop;
2964 end if;
2965 end Check_Package_Body_For_Inlining;
2966
2967 --------------------
2968 -- Cleanup_Scopes --
2969 --------------------
2970
2971 procedure Cleanup_Scopes is
2972 Decl : Node_Id;
2973 Elmt : Elmt_Id;
2974 Fin : Entity_Id;
2975 Kind : Entity_Kind;
2976 Scop : Entity_Id;
2977
2978 begin
2979 Elmt := First_Elmt (To_Clean);
2980 while Present (Elmt) loop
2981 Scop := Node (Elmt);
2982 Kind := Ekind (Scop);
2983
2984 if Kind = E_Block then
2985 Decl := Parent (Block_Node (Scop));
2986
2987 else
2988 Decl := Unit_Declaration_Node (Scop);
2989
2990 if Nkind (Decl) in N_Subprogram_Declaration
2991 | N_Task_Type_Declaration
2992 | N_Subprogram_Body_Stub
2993 then
2994 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2995 end if;
2996 end if;
2997
2998 -- Finalizers are built only for package specs and bodies that are
2999 -- compilation units, so check that we do not have anything else.
3000 -- Moreover, they must be built at most once for each entity during
3001 -- the compilation of the main unit. However, if other units are
3002 -- later compiled for inlining purposes, they may also contain body
3003 -- instances and, therefore, appear again here, so we need to make
3004 -- sure that we do not build two finalizers for them (note that the
3005 -- contents of the finalizer for these units is irrelevant since it
3006 -- is not output in the generated code).
3007
3008 if Kind in E_Package | E_Package_Body then
3009 declare
3010 Unit_Entity : constant Entity_Id :=
3011 (if Kind = E_Package then Scop else Spec_Entity (Scop));
3012
3013 begin
3014 pragma Assert (Is_Compilation_Unit (Unit_Entity)
3015 and then (No (Finalizer (Scop))
3016 or else Unit_Entity /= Main_Unit_Entity));
3017
3018 if No (Finalizer (Scop)) then
3019 Build_Finalizer
3020 (N => Decl,
3021 Clean_Stmts => No_List,
3022 Mark_Id => Empty,
3023 Defer_Abort => False,
3024 Fin_Id => Fin);
3025
3026 if Present (Fin) then
3027 Set_Finalizer (Scop, Fin);
3028 end if;
3029 end if;
3030 end;
3031
3032 else
3033 Push_Scope (Scop);
3034 Expand_Cleanup_Actions (Decl);
3035 Pop_Scope;
3036 end if;
3037
3038 Next_Elmt (Elmt);
3039 end loop;
3040 end Cleanup_Scopes;
3041
3042 -----------------------------------------------
3043 -- Establish_Actual_Mapping_For_Inlined_Call --
3044 -----------------------------------------------
3045
3046 procedure Establish_Actual_Mapping_For_Inlined_Call
3047 (N : Node_Id;
3048 Subp : Entity_Id;
3049 Decls : List_Id;
3050 Body_Or_Expr_To_Check : Node_Id)
3051 is
3052
3053 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3054 -- Determine whether a formal parameter is used only once in
3055 -- Body_Or_Expr_To_Check.
3056
3057 -------------------------
3058 -- Formal_Is_Used_Once --
3059 -------------------------
3060
3061 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3062 Use_Counter : Nat := 0;
3063
3064 function Count_Uses (N : Node_Id) return Traverse_Result;
3065 -- Traverse the tree and count the uses of the formal parameter.
3066 -- In this case, for optimization purposes, we do not need to
3067 -- continue the traversal once more than one use is encountered.
3068
3069 ----------------
3070 -- Count_Uses --
3071 ----------------
3072
3073 function Count_Uses (N : Node_Id) return Traverse_Result is
3074 begin
3075 -- The original node is an identifier
3076
3077 if Nkind (N) = N_Identifier
3078 and then Present (Entity (N))
3079
3080 -- Original node's entity points to the one in the copied body
3081
3082 and then Nkind (Entity (N)) = N_Identifier
3083 and then Present (Entity (Entity (N)))
3084
3085 -- The entity of the copied node is the formal parameter
3086
3087 and then Entity (Entity (N)) = Formal
3088 then
3089 Use_Counter := Use_Counter + 1;
3090
3091 -- If this is a second use then abandon the traversal
3092
3093 if Use_Counter > 1 then
3094 return Abandon;
3095 end if;
3096 end if;
3097
3098 return OK;
3099 end Count_Uses;
3100
3101 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3102
3103 -- Start of processing for Formal_Is_Used_Once
3104
3105 begin
3106 Count_Formal_Uses (Body_Or_Expr_To_Check);
3107 return Use_Counter = 1;
3108 end Formal_Is_Used_Once;
3109
3110 -- Local Data --
3111
3112 F : Entity_Id;
3113 A : Node_Id;
3114 Decl : Node_Id;
3115 Loc : constant Source_Ptr := Sloc (N);
3116 New_A : Node_Id;
3117 Temp : Entity_Id;
3118 Temp_Typ : Entity_Id;
3119
3120 -- Start of processing for Establish_Actual_Mapping_For_Inlined_Call
3121
3122 begin
3123 F := First_Formal (Subp);
3124 A := First_Actual (N);
3125 while Present (F) loop
3126 -- Reset Last_Assignment for any parameters of mode out or in out, to
3127 -- prevent spurious warnings about overwriting for assignments to the
3128 -- formal in the inlined code.
3129
3130 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
3131
3132 -- In GNATprove mode a protected component acting as an actual
3133 -- subprogram parameter will appear as inlined-for-proof. However,
3134 -- its E_Component entity is not an assignable object, so the
3135 -- assertion in Set_Last_Assignment will fail. We just omit the
3136 -- call to Set_Last_Assignment, because GNATprove flags useless
3137 -- assignments with its own flow analysis.
3138 --
3139 -- In GNAT mode such a problem does not occur, because protected
3140 -- components are inlined via object renamings whose entity kind
3141 -- E_Variable is assignable.
3142
3143 if Is_Assignable (Entity (A)) then
3144 Set_Last_Assignment (Entity (A), Empty);
3145 else
3146 pragma Assert
3147 (GNATprove_Mode and then Is_Protected_Component (Entity (A)));
3148 end if;
3149 end if;
3150
3151 -- If the argument may be a controlling argument in a call within
3152 -- the inlined body, we must preserve its class-wide nature to ensure
3153 -- that dynamic dispatching will take place subsequently. If the
3154 -- formal has a constraint, then it must be preserved to retain the
3155 -- semantics of the body.
3156
3157 if Is_Class_Wide_Type (Etype (F))
3158 or else (Is_Access_Type (Etype (F))
3159 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3160 then
3161 Temp_Typ := Etype (F);
3162
3163 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3164 and then Etype (F) /= Base_Type (Etype (F))
3165 and then (Is_Constrained (Etype (F))
3166 or else
3167 Is_Fixed_Lower_Bound_Array_Subtype (Etype (F)))
3168 then
3169 Temp_Typ := Etype (F);
3170
3171 else
3172 Temp_Typ := Etype (A);
3173 end if;
3174
3175 -- If the actual is a simple name or a literal, no need to create a
3176 -- temporary, object can be used directly. Skip this optimization in
3177 -- GNATprove mode, to make sure any check on a type conversion will
3178 -- be issued.
3179
3180 if (Is_Entity_Name (A)
3181 and then
3182 (not Is_Scalar_Type (Etype (A))
3183 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3184 and then not GNATprove_Mode)
3185
3186 -- When the actual is an identifier and the corresponding formal is
3187 -- used only once in the original body, the formal can be substituted
3188 -- directly with the actual parameter. Skip this optimization in
3189 -- GNATprove mode, to make sure any check on a type conversion
3190 -- will be issued.
3191
3192 or else
3193 (Nkind (A) = N_Identifier
3194 and then Formal_Is_Used_Once (F)
3195 and then not GNATprove_Mode)
3196
3197 -- If the actual is a literal and the formal has its address taken,
3198 -- we cannot pass the literal itself as an argument, so its value
3199 -- must be captured in a temporary.
3200
3201 or else
3202 (Nkind (A) in
3203 N_Real_Literal | N_Integer_Literal | N_Character_Literal
3204 and then not Address_Taken (F))
3205 then
3206 if Etype (F) /= Etype (A) then
3207 Set_Renamed_Object
3208 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3209 else
3210 Set_Renamed_Object (F, A);
3211 end if;
3212
3213 else
3214 Temp := Make_Temporary (Loc, 'C');
3215
3216 -- If the actual for an in/in-out parameter is a view conversion,
3217 -- make it into an unchecked conversion, given that an untagged
3218 -- type conversion is not a proper object for a renaming.
3219
3220 -- In-out conversions that involve real conversions have already
3221 -- been transformed in Expand_Actuals.
3222
3223 if Nkind (A) = N_Type_Conversion
3224 and then Ekind (F) /= E_In_Parameter
3225 then
3226 New_A := Unchecked_Convert_To (Etype (F), Expression (A));
3227
3228 -- In GNATprove mode, keep the most precise type of the actual for
3229 -- the temporary variable, when the formal type is unconstrained.
3230 -- Otherwise, the AST may contain unexpected assignment statements
3231 -- to a temporary variable of unconstrained type renaming a local
3232 -- variable of constrained type, which is not expected by
3233 -- GNATprove.
3234
3235 elsif Etype (F) /= Etype (A)
3236 and then
3237 (not GNATprove_Mode
3238 or else (Is_Constrained (Etype (F))
3239 or else
3240 Is_Fixed_Lower_Bound_Array_Subtype (Etype (F))))
3241 then
3242 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3243 Temp_Typ := Etype (F);
3244
3245 else
3246 New_A := Relocate_Node (A);
3247 end if;
3248
3249 Set_Sloc (New_A, Sloc (N));
3250
3251 -- If the actual has a by-reference type, it cannot be copied,
3252 -- so its value is captured in a renaming declaration. Otherwise
3253 -- declare a local constant initialized with the actual.
3254
3255 -- We also use a renaming declaration for expressions of an array
3256 -- type that is not bit-packed, both for efficiency reasons and to
3257 -- respect the semantics of the call: in most cases the original
3258 -- call will pass the parameter by reference, and thus the inlined
3259 -- code will have the same semantics.
3260
3261 -- Finally, we need a renaming declaration in the case of limited
3262 -- types for which initialization cannot be by copy either.
3263
3264 if Ekind (F) = E_In_Parameter
3265 and then not Is_By_Reference_Type (Etype (A))
3266 and then not Is_Limited_Type (Etype (A))
3267 and then
3268 (not Is_Array_Type (Etype (A))
3269 or else not Is_Object_Reference (A)
3270 or else Is_Bit_Packed_Array (Etype (A)))
3271 then
3272 Decl :=
3273 Make_Object_Declaration (Loc,
3274 Defining_Identifier => Temp,
3275 Constant_Present => True,
3276 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3277 Expression => New_A);
3278
3279 else
3280 -- In GNATprove mode, make an explicit copy of input
3281 -- parameters when formal and actual types differ, to make
3282 -- sure any check on the type conversion will be issued.
3283 -- The legality of the copy is ensured by calling first
3284 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3285
3286 if GNATprove_Mode
3287 and then Ekind (F) /= E_Out_Parameter
3288 and then not Same_Type (Etype (F), Etype (A))
3289 then
3290 pragma Assert (not Is_By_Reference_Type (Etype (A)));
3291 pragma Assert (not Is_Limited_Type (Etype (A)));
3292
3293 Append_To (Decls,
3294 Make_Object_Declaration (Loc,
3295 Defining_Identifier => Make_Temporary (Loc, 'C'),
3296 Constant_Present => True,
3297 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3298 Expression => New_Copy_Tree (New_A)));
3299 end if;
3300
3301 Decl :=
3302 Make_Object_Renaming_Declaration (Loc,
3303 Defining_Identifier => Temp,
3304 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3305 Name => New_A);
3306 end if;
3307
3308 Append (Decl, Decls);
3309 Set_Renamed_Object (F, Temp);
3310 end if;
3311
3312 Next_Formal (F);
3313 Next_Actual (A);
3314 end loop;
3315 end Establish_Actual_Mapping_For_Inlined_Call;
3316
3317 -------------------------
3318 -- Expand_Inlined_Call --
3319 -------------------------
3320
3321 procedure Expand_Inlined_Call
3322 (N : Node_Id;
3323 Subp : Entity_Id;
3324 Orig_Subp : Entity_Id)
3325 is
3326 Decls : constant List_Id := New_List;
3327 Is_Predef : constant Boolean :=
3328 Is_Predefined_Unit (Get_Source_Unit (Subp));
3329 Loc : constant Source_Ptr := Sloc (N);
3330 Orig_Bod : constant Node_Id :=
3331 Body_To_Inline (Unit_Declaration_Node (Subp));
3332
3333 Uses_Back_End : constant Boolean :=
3334 Back_End_Inlining and then Optimization_Level > 0;
3335 -- The back-end expansion is used if the target supports back-end
3336 -- inlining and some level of optimixation is required; otherwise
3337 -- the inlining takes place fully as a tree expansion.
3338
3339 Blk : Node_Id;
3340 Decl : Node_Id;
3341 Exit_Lab : Entity_Id := Empty;
3342 Lab_Decl : Node_Id := Empty;
3343 Lab_Id : Node_Id;
3344 Num_Ret : Nat := 0;
3345 Ret_Type : Entity_Id;
3346 Temp : Entity_Id;
3347
3348 Is_Unc : Boolean;
3349 Is_Unc_Decl : Boolean;
3350 -- If the type returned by the function is unconstrained and the call
3351 -- can be inlined, special processing is required.
3352
3353 Return_Object : Entity_Id := Empty;
3354 -- Entity in declaration in an extended_return_statement
3355
3356 Targ : Node_Id := Empty;
3357 -- The target of the call. If context is an assignment statement then
3358 -- this is the left-hand side of the assignment, else it is a temporary
3359 -- to which the return value is assigned prior to rewriting the call.
3360
3361 Targ1 : Node_Id := Empty;
3362 -- A separate target used when the return type is unconstrained
3363
3364 procedure Make_Exit_Label;
3365 -- Build declaration for exit label to be used in Return statements,
3366 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3367 -- declaration). Does nothing if Exit_Lab already set.
3368
3369 function Process_Formals (N : Node_Id) return Traverse_Result;
3370 -- Replace occurrence of a formal with the corresponding actual, or the
3371 -- thunk generated for it. Replace a return statement with an assignment
3372 -- to the target of the call, with appropriate conversions if needed.
3373
3374 function Process_Formals_In_Aspects (N : Node_Id) return Traverse_Result;
3375 -- Because aspects are linked indirectly to the rest of the tree,
3376 -- replacement of formals appearing in aspect specifications must
3377 -- be performed in a separate pass, using an instantiation of the
3378 -- previous subprogram over aspect specifications reachable from N.
3379
3380 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3381 -- If the call being expanded is that of an internal subprogram, set the
3382 -- sloc of the generated block to that of the call itself, so that the
3383 -- expansion is skipped by the "next" command in gdb. Same processing
3384 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
3385 -- Debug_Generated_Code is true, suppress this change to simplify our
3386 -- own development. Same in GNATprove mode, to ensure that warnings and
3387 -- diagnostics point to the proper location.
3388
3389 procedure Reset_Dispatching_Calls (N : Node_Id);
3390 -- In subtree N search for occurrences of dispatching calls that use the
3391 -- Ada 2005 Object.Operation notation and the object is a formal of the
3392 -- inlined subprogram. Reset the entity associated with Operation in all
3393 -- the found occurrences.
3394
3395 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3396 -- If the function body is a single expression, replace call with
3397 -- expression, else insert block appropriately.
3398
3399 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3400 -- If procedure body has no local variables, inline body without
3401 -- creating block, otherwise rewrite call with block.
3402
3403 ---------------------
3404 -- Make_Exit_Label --
3405 ---------------------
3406
3407 procedure Make_Exit_Label is
3408 Lab_Ent : Entity_Id;
3409 begin
3410 if No (Exit_Lab) then
3411 Lab_Ent := Make_Temporary (Loc, 'L');
3412 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
3413 Exit_Lab := Make_Label (Loc, Lab_Id);
3414 Lab_Decl :=
3415 Make_Implicit_Label_Declaration (Loc,
3416 Defining_Identifier => Lab_Ent,
3417 Label_Construct => Exit_Lab);
3418 end if;
3419 end Make_Exit_Label;
3420
3421 ---------------------
3422 -- Process_Formals --
3423 ---------------------
3424
3425 function Process_Formals (N : Node_Id) return Traverse_Result is
3426 Loc : constant Source_Ptr := Sloc (N);
3427 A : Entity_Id;
3428 E : Entity_Id;
3429 Ret : Node_Id;
3430
3431 Had_Private_View : Boolean;
3432
3433 begin
3434 if Is_Entity_Name (N) and then Present (Entity (N)) then
3435 E := Entity (N);
3436
3437 if Is_Formal (E) and then Scope (E) = Subp then
3438 A := Renamed_Object (E);
3439
3440 -- Rewrite the occurrence of the formal into an occurrence of
3441 -- the actual. Also establish visibility on the proper view of
3442 -- the actual's subtype for the body's context (if the actual's
3443 -- subtype is private at the call point but its full view is
3444 -- visible to the body, then the inlined tree here must be
3445 -- analyzed with the full view).
3446 --
3447 -- The Has_Private_View flag is cleared by rewriting, so it
3448 -- must be explicitly saved and restored, just like when
3449 -- instantiating the body to inline.
3450
3451 if Is_Entity_Name (A) then
3452 Had_Private_View := Has_Private_View (N);
3453 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3454 Set_Has_Private_View (N, Had_Private_View);
3455 Check_Private_View (N);
3456
3457 elsif Nkind (A) = N_Defining_Identifier then
3458 Had_Private_View := Has_Private_View (N);
3459 Rewrite (N, New_Occurrence_Of (A, Loc));
3460 Set_Has_Private_View (N, Had_Private_View);
3461 Check_Private_View (N);
3462
3463 -- Numeric literal
3464
3465 else
3466 Rewrite (N, New_Copy (A));
3467 end if;
3468 end if;
3469
3470 return Skip;
3471
3472 elsif Is_Entity_Name (N)
3473 and then Present (Return_Object)
3474 and then Chars (N) = Chars (Return_Object)
3475 then
3476 -- Occurrence within an extended return statement. The return
3477 -- object is local to the body been inlined, and thus the generic
3478 -- copy is not analyzed yet, so we match by name, and replace it
3479 -- with target of call.
3480
3481 if Nkind (Targ) = N_Defining_Identifier then
3482 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3483 else
3484 Rewrite (N, New_Copy_Tree (Targ));
3485 end if;
3486
3487 return Skip;
3488
3489 elsif Nkind (N) = N_Simple_Return_Statement then
3490 if No (Expression (N)) then
3491 Num_Ret := Num_Ret + 1;
3492 Make_Exit_Label;
3493 Rewrite (N,
3494 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3495
3496 else
3497 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3498 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3499 then
3500 -- Function body is a single expression. No need for
3501 -- exit label.
3502
3503 null;
3504
3505 else
3506 Num_Ret := Num_Ret + 1;
3507 Make_Exit_Label;
3508 end if;
3509
3510 -- Because of the presence of private types, the views of the
3511 -- expression and the context may be different, so place
3512 -- a type conversion to the context type to avoid spurious
3513 -- errors, e.g. when the expression is a numeric literal and
3514 -- the context is private. If the expression is an aggregate,
3515 -- use a qualified expression, because an aggregate is not a
3516 -- legal argument of a conversion. Ditto for numeric, character
3517 -- and string literals, and attributes that yield a universal
3518 -- type, because those must be resolved to a specific type.
3519
3520 if Nkind (Expression (N)) in N_Aggregate
3521 | N_Character_Literal
3522 | N_Null
3523 | N_String_Literal
3524 or else Yields_Universal_Type (Expression (N))
3525 then
3526 Ret :=
3527 Make_Qualified_Expression (Loc,
3528 Subtype_Mark => New_Occurrence_Of (Ret_Type, Loc),
3529 Expression => Relocate_Node (Expression (N)));
3530
3531 -- Use an unchecked type conversion between access types, for
3532 -- which a type conversion would not always be valid, as no
3533 -- check may result from the conversion.
3534
3535 elsif Is_Access_Type (Ret_Type) then
3536 Ret :=
3537 Unchecked_Convert_To
3538 (Ret_Type, Relocate_Node (Expression (N)));
3539
3540 -- Otherwise use a type conversion, which may trigger a check
3541
3542 else
3543 Ret :=
3544 Make_Type_Conversion (Loc,
3545 Subtype_Mark => New_Occurrence_Of (Ret_Type, Loc),
3546 Expression => Relocate_Node (Expression (N)));
3547 end if;
3548
3549 if Nkind (Targ) = N_Defining_Identifier then
3550 Rewrite (N,
3551 Make_Assignment_Statement (Loc,
3552 Name => New_Occurrence_Of (Targ, Loc),
3553 Expression => Ret));
3554 else
3555 Rewrite (N,
3556 Make_Assignment_Statement (Loc,
3557 Name => New_Copy (Targ),
3558 Expression => Ret));
3559 end if;
3560
3561 Set_Assignment_OK (Name (N));
3562
3563 if Present (Exit_Lab) then
3564 Insert_After (N,
3565 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3566 end if;
3567 end if;
3568
3569 return OK;
3570
3571 -- An extended return becomes a block whose first statement is the
3572 -- assignment of the initial expression of the return object to the
3573 -- target of the call itself.
3574
3575 elsif Nkind (N) = N_Extended_Return_Statement then
3576 declare
3577 Return_Decl : constant Entity_Id :=
3578 First (Return_Object_Declarations (N));
3579 Assign : Node_Id;
3580
3581 begin
3582 Return_Object := Defining_Identifier (Return_Decl);
3583
3584 if Present (Expression (Return_Decl)) then
3585 if Nkind (Targ) = N_Defining_Identifier then
3586 Assign :=
3587 Make_Assignment_Statement (Loc,
3588 Name => New_Occurrence_Of (Targ, Loc),
3589 Expression => Expression (Return_Decl));
3590 else
3591 Assign :=
3592 Make_Assignment_Statement (Loc,
3593 Name => New_Copy (Targ),
3594 Expression => Expression (Return_Decl));
3595 end if;
3596
3597 Set_Assignment_OK (Name (Assign));
3598
3599 if No (Handled_Statement_Sequence (N)) then
3600 Set_Handled_Statement_Sequence (N,
3601 Make_Handled_Sequence_Of_Statements (Loc,
3602 Statements => New_List));
3603 end if;
3604
3605 Prepend (Assign,
3606 Statements (Handled_Statement_Sequence (N)));
3607 end if;
3608
3609 Rewrite (N,
3610 Make_Block_Statement (Loc,
3611 Handled_Statement_Sequence =>
3612 Handled_Statement_Sequence (N)));
3613
3614 return OK;
3615 end;
3616
3617 -- Remove pragma Unreferenced since it may refer to formals that
3618 -- are not visible in the inlined body, and in any case we will
3619 -- not be posting warnings on the inlined body so it is unneeded.
3620
3621 elsif Nkind (N) = N_Pragma
3622 and then Pragma_Name (N) = Name_Unreferenced
3623 then
3624 Rewrite (N, Make_Null_Statement (Loc));
3625 return OK;
3626
3627 else
3628 return OK;
3629 end if;
3630 end Process_Formals;
3631
3632 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3633
3634 --------------------------------
3635 -- Process_Formals_In_Aspects --
3636 --------------------------------
3637
3638 function Process_Formals_In_Aspects
3639 (N : Node_Id) return Traverse_Result
3640 is
3641 begin
3642 if Nkind (N) = N_Aspect_Specification then
3643 Replace_Formals (Expression (N));
3644 end if;
3645 return OK;
3646 end Process_Formals_In_Aspects;
3647
3648 procedure Replace_Formals_In_Aspects is
3649 new Traverse_Proc (Process_Formals_In_Aspects);
3650
3651 ------------------
3652 -- Process_Sloc --
3653 ------------------
3654
3655 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3656 begin
3657 if not Debug_Generated_Code then
3658 Set_Sloc (Nod, Sloc (N));
3659 Set_Comes_From_Source (Nod, False);
3660 end if;
3661
3662 return OK;
3663 end Process_Sloc;
3664
3665 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3666
3667 ------------------------------
3668 -- Reset_Dispatching_Calls --
3669 ------------------------------
3670
3671 procedure Reset_Dispatching_Calls (N : Node_Id) is
3672
3673 function Do_Reset (N : Node_Id) return Traverse_Result;
3674
3675 --------------
3676 -- Do_Reset --
3677 --------------
3678
3679 function Do_Reset (N : Node_Id) return Traverse_Result is
3680 begin
3681 if Nkind (N) = N_Procedure_Call_Statement
3682 and then Nkind (Name (N)) = N_Selected_Component
3683 and then Nkind (Prefix (Name (N))) = N_Identifier
3684 and then Is_Formal (Entity (Prefix (Name (N))))
3685 and then Is_Dispatching_Operation
3686 (Entity (Selector_Name (Name (N))))
3687 then
3688 Set_Entity (Selector_Name (Name (N)), Empty);
3689 end if;
3690
3691 return OK;
3692 end Do_Reset;
3693
3694 procedure Do_Reset_Calls is new Traverse_Proc (Do_Reset);
3695
3696 begin
3697 Do_Reset_Calls (N);
3698 end Reset_Dispatching_Calls;
3699
3700 ---------------------------
3701 -- Rewrite_Function_Call --
3702 ---------------------------
3703
3704 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3705 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3706 Fst : constant Node_Id := First (Statements (HSS));
3707
3708 begin
3709 -- Optimize simple case: function body is a single return statement,
3710 -- which has been expanded into an assignment.
3711
3712 if Is_Empty_List (Declarations (Blk))
3713 and then Nkind (Fst) = N_Assignment_Statement
3714 and then No (Next (Fst))
3715 then
3716 -- The function call may have been rewritten as the temporary
3717 -- that holds the result of the call, in which case remove the
3718 -- now useless declaration.
3719
3720 if Nkind (N) = N_Identifier
3721 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3722 then
3723 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3724 end if;
3725
3726 Rewrite (N, Expression (Fst));
3727
3728 elsif Nkind (N) = N_Identifier
3729 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3730 then
3731 -- The block assigns the result of the call to the temporary
3732
3733 Insert_After (Parent (Entity (N)), Blk);
3734
3735 -- If the context is an assignment, and the left-hand side is free of
3736 -- side effects, the replacement is also safe.
3737
3738 elsif Nkind (Parent (N)) = N_Assignment_Statement
3739 and then
3740 (Is_Entity_Name (Name (Parent (N)))
3741 or else
3742 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3743 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
3744
3745 or else
3746 (Nkind (Name (Parent (N))) = N_Selected_Component
3747 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3748 then
3749 -- Replace assignment with the block
3750
3751 declare
3752 Original_Assignment : constant Node_Id := Parent (N);
3753
3754 begin
3755 -- Preserve the original assignment node to keep the complete
3756 -- assignment subtree consistent enough for Analyze_Assignment
3757 -- to proceed (specifically, the original Lhs node must still
3758 -- have an assignment statement as its parent).
3759
3760 -- We cannot rely on Original_Node to go back from the block
3761 -- node to the assignment node, because the assignment might
3762 -- already be a rewrite substitution.
3763
3764 Discard_Node (Relocate_Node (Original_Assignment));
3765 Rewrite (Original_Assignment, Blk);
3766 end;
3767
3768 elsif Nkind (Parent (N)) = N_Object_Declaration then
3769
3770 -- A call to a function which returns an unconstrained type
3771 -- found in the expression initializing an object-declaration is
3772 -- expanded into a procedure call which must be added after the
3773 -- object declaration.
3774
3775 if Is_Unc_Decl and Back_End_Inlining then
3776 Insert_Action_After (Parent (N), Blk);
3777 else
3778 Set_Expression (Parent (N), Empty);
3779 Insert_After (Parent (N), Blk);
3780 end if;
3781
3782 elsif Is_Unc and then not Back_End_Inlining then
3783 Insert_Before (Parent (N), Blk);
3784 end if;
3785 end Rewrite_Function_Call;
3786
3787 ----------------------------
3788 -- Rewrite_Procedure_Call --
3789 ----------------------------
3790
3791 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3792 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3793
3794 begin
3795 -- If there is a transient scope for N, this will be the scope of the
3796 -- actions for N, and the statements in Blk need to be within this
3797 -- scope. For example, they need to have visibility on the constant
3798 -- declarations created for the formals.
3799
3800 -- If N needs no transient scope, and if there are no declarations in
3801 -- the inlined body, we can do a little optimization and insert the
3802 -- statements for the body directly after N, and rewrite N to a
3803 -- null statement, instead of rewriting N into a full-blown block
3804 -- statement.
3805
3806 if not Scope_Is_Transient
3807 and then Is_Empty_List (Declarations (Blk))
3808 then
3809 Insert_List_After (N, Statements (HSS));
3810 Rewrite (N, Make_Null_Statement (Loc));
3811 else
3812 Rewrite (N, Blk);
3813 end if;
3814 end Rewrite_Procedure_Call;
3815
3816 -- Start of processing for Expand_Inlined_Call
3817
3818 begin
3819 -- Initializations for old/new semantics
3820
3821 if not Uses_Back_End then
3822 Is_Unc := Is_Array_Type (Etype (Subp))
3823 and then not Is_Constrained (Etype (Subp));
3824 Is_Unc_Decl := False;
3825 else
3826 Is_Unc := Returns_Unconstrained_Type (Subp)
3827 and then Optimization_Level > 0;
3828 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
3829 and then Is_Unc;
3830 end if;
3831
3832 -- Check for an illegal attempt to inline a recursive procedure. If the
3833 -- subprogram has parameters this is detected when trying to supply a
3834 -- binding for parameters that already have one. For parameterless
3835 -- subprograms this must be done explicitly.
3836
3837 if In_Open_Scopes (Subp) then
3838 Cannot_Inline
3839 ("cannot inline call to recursive subprogram?", N, Subp);
3840 Set_Is_Inlined (Subp, False);
3841 return;
3842
3843 -- Skip inlining if this is not a true inlining since the attribute
3844 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
3845 -- true inlining, Orig_Bod has code rather than being an entity.
3846
3847 elsif Nkind (Orig_Bod) in N_Entity then
3848 return;
3849 end if;
3850
3851 if Nkind (Orig_Bod) in N_Defining_Identifier
3852 | N_Defining_Operator_Symbol
3853 then
3854 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3855 -- can be replaced with calls to the renamed entity directly, because
3856 -- the subprograms are subtype conformant. If the renamed subprogram
3857 -- is an inherited operation, we must redo the expansion because
3858 -- implicit conversions may be needed. Similarly, if the renamed
3859 -- entity is inlined, expand the call for further optimizations.
3860
3861 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3862
3863 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
3864 Expand_Call (N);
3865 end if;
3866
3867 return;
3868 end if;
3869
3870 -- Register the call in the list of inlined calls
3871
3872 Append_New_Elmt (N, To => Inlined_Calls);
3873
3874 -- Use generic machinery to copy body of inlined subprogram, as if it
3875 -- were an instantiation, resetting source locations appropriately, so
3876 -- that nested inlined calls appear in the main unit.
3877
3878 Save_Env (Subp, Empty);
3879 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3880
3881 -- Old semantics
3882
3883 if not Uses_Back_End then
3884 declare
3885 Bod : Node_Id;
3886
3887 begin
3888 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3889 Blk :=
3890 Make_Block_Statement (Loc,
3891 Declarations => Declarations (Bod),
3892 Handled_Statement_Sequence =>
3893 Handled_Statement_Sequence (Bod));
3894
3895 if No (Declarations (Bod)) then
3896 Set_Declarations (Blk, New_List);
3897 end if;
3898
3899 -- For the unconstrained case, capture the name of the local
3900 -- variable that holds the result. This must be the first
3901 -- declaration in the block, because its bounds cannot depend
3902 -- on local variables. Otherwise there is no way to declare the
3903 -- result outside of the block. Needless to say, in general the
3904 -- bounds will depend on the actuals in the call.
3905
3906 -- If the context is an assignment statement, as is the case
3907 -- for the expansion of an extended return, the left-hand side
3908 -- provides bounds even if the return type is unconstrained.
3909
3910 if Is_Unc then
3911 declare
3912 First_Decl : Node_Id;
3913
3914 begin
3915 First_Decl := First (Declarations (Blk));
3916
3917 -- If the body is a single extended return statement,the
3918 -- resulting block is a nested block.
3919
3920 if No (First_Decl) then
3921 First_Decl :=
3922 First (Statements (Handled_Statement_Sequence (Blk)));
3923
3924 if Nkind (First_Decl) = N_Block_Statement then
3925 First_Decl := First (Declarations (First_Decl));
3926 end if;
3927 end if;
3928
3929 -- No front-end inlining possible
3930
3931 if Nkind (First_Decl) /= N_Object_Declaration then
3932 return;
3933 end if;
3934
3935 if Nkind (Parent (N)) /= N_Assignment_Statement then
3936 Targ1 := Defining_Identifier (First_Decl);
3937 else
3938 Targ1 := Name (Parent (N));
3939 end if;
3940 end;
3941 end if;
3942 end;
3943
3944 -- New semantics
3945
3946 else
3947 declare
3948 Bod : Node_Id;
3949
3950 begin
3951 -- General case
3952
3953 if not Is_Unc then
3954 Bod :=
3955 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3956 Blk :=
3957 Make_Block_Statement (Loc,
3958 Declarations => Declarations (Bod),
3959 Handled_Statement_Sequence =>
3960 Handled_Statement_Sequence (Bod));
3961
3962 -- Inline a call to a function that returns an unconstrained type.
3963 -- The semantic analyzer checked that frontend-inlined functions
3964 -- returning unconstrained types have no declarations and have
3965 -- a single extended return statement. As part of its processing
3966 -- the function was split into two subprograms: a procedure P' and
3967 -- a function F' that has a block with a call to procedure P' (see
3968 -- Split_Unconstrained_Function).
3969
3970 else
3971 pragma Assert
3972 (Nkind
3973 (First
3974 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3975 N_Block_Statement);
3976
3977 declare
3978 Blk_Stmt : constant Node_Id :=
3979 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
3980 First_Stmt : constant Node_Id :=
3981 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
3982 Second_Stmt : constant Node_Id := Next (First_Stmt);
3983
3984 begin
3985 pragma Assert
3986 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3987 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3988 and then No (Next (Second_Stmt)));
3989
3990 Bod :=
3991 Copy_Generic_Node
3992 (First
3993 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3994 Empty, Instantiating => True);
3995 Blk := Bod;
3996
3997 -- Capture the name of the local variable that holds the
3998 -- result. This must be the first declaration in the block,
3999 -- because its bounds cannot depend on local variables.
4000 -- Otherwise there is no way to declare the result outside
4001 -- of the block. Needless to say, in general the bounds will
4002 -- depend on the actuals in the call.
4003
4004 if Nkind (Parent (N)) /= N_Assignment_Statement then
4005 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4006
4007 -- If the context is an assignment statement, as is the case
4008 -- for the expansion of an extended return, the left-hand
4009 -- side provides bounds even if the return type is
4010 -- unconstrained.
4011
4012 else
4013 Targ1 := Name (Parent (N));
4014 end if;
4015 end;
4016 end if;
4017
4018 if No (Declarations (Bod)) then
4019 Set_Declarations (Blk, New_List);
4020 end if;
4021 end;
4022 end if;
4023
4024 -- If this is a derived function, establish the proper return type
4025
4026 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4027 Ret_Type := Etype (Orig_Subp);
4028 else
4029 Ret_Type := Etype (Subp);
4030 end if;
4031
4032 -- Create temporaries for the actuals that are expressions, or that are
4033 -- scalars and require copying to preserve semantics.
4034
4035 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Orig_Bod);
4036
4037 -- Establish target of function call. If context is not assignment or
4038 -- declaration, create a temporary as a target. The declaration for the
4039 -- temporary may be subsequently optimized away if the body is a single
4040 -- expression, or if the left-hand side of the assignment is simple
4041 -- enough, i.e. an entity or an explicit dereference of one.
4042
4043 if Ekind (Subp) = E_Function then
4044 if Nkind (Parent (N)) = N_Assignment_Statement
4045 and then Is_Entity_Name (Name (Parent (N)))
4046 then
4047 Targ := Name (Parent (N));
4048
4049 elsif Nkind (Parent (N)) = N_Assignment_Statement
4050 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4051 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4052 then
4053 Targ := Name (Parent (N));
4054
4055 elsif Nkind (Parent (N)) = N_Assignment_Statement
4056 and then Nkind (Name (Parent (N))) = N_Selected_Component
4057 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4058 then
4059 Targ := New_Copy_Tree (Name (Parent (N)));
4060
4061 elsif Nkind (Parent (N)) = N_Object_Declaration
4062 and then Is_Limited_Type (Etype (Subp))
4063 then
4064 Targ := Defining_Identifier (Parent (N));
4065
4066 -- New semantics: In an object declaration avoid an extra copy
4067 -- of the result of a call to an inlined function that returns
4068 -- an unconstrained type
4069
4070 elsif Uses_Back_End
4071 and then Nkind (Parent (N)) = N_Object_Declaration
4072 and then Is_Unc
4073 then
4074 Targ := Defining_Identifier (Parent (N));
4075
4076 else
4077 -- Replace call with temporary and create its declaration
4078
4079 Temp := Make_Temporary (Loc, 'C');
4080 Set_Is_Internal (Temp);
4081
4082 -- For the unconstrained case, the generated temporary has the
4083 -- same constrained declaration as the result variable. It may
4084 -- eventually be possible to remove that temporary and use the
4085 -- result variable directly.
4086
4087 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
4088 then
4089 Decl :=
4090 Make_Object_Declaration (Loc,
4091 Defining_Identifier => Temp,
4092 Object_Definition =>
4093 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4094
4095 Replace_Formals (Decl);
4096
4097 else
4098 Decl :=
4099 Make_Object_Declaration (Loc,
4100 Defining_Identifier => Temp,
4101 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
4102
4103 Set_Etype (Temp, Ret_Type);
4104 end if;
4105
4106 Set_No_Initialization (Decl);
4107 Append (Decl, Decls);
4108 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4109 Targ := Temp;
4110 end if;
4111 end if;
4112
4113 Insert_Actions (N, Decls);
4114
4115 if Is_Unc_Decl then
4116
4117 -- Special management for inlining a call to a function that returns
4118 -- an unconstrained type and initializes an object declaration: we
4119 -- avoid generating undesired extra calls and goto statements.
4120
4121 -- Given:
4122 -- function Func (...) return String is
4123 -- begin
4124 -- declare
4125 -- Result : String (1 .. 4);
4126 -- begin
4127 -- Proc (Result, ...);
4128 -- return Result;
4129 -- end;
4130 -- end Func;
4131
4132 -- Result : String := Func (...);
4133
4134 -- Replace this object declaration by:
4135
4136 -- Result : String (1 .. 4);
4137 -- Proc (Result, ...);
4138
4139 Remove_Homonym (Targ);
4140
4141 Decl :=
4142 Make_Object_Declaration
4143 (Loc,
4144 Defining_Identifier => Targ,
4145 Object_Definition =>
4146 New_Copy_Tree (Object_Definition (Parent (Targ1))));
4147 Replace_Formals (Decl);
4148 Set_No_Initialization (Decl);
4149 Rewrite (Parent (N), Decl);
4150 Analyze (Parent (N));
4151
4152 -- Avoid spurious warnings since we know that this declaration is
4153 -- referenced by the procedure call.
4154
4155 Set_Never_Set_In_Source (Targ, False);
4156
4157 -- Remove the local declaration of the extended return stmt from the
4158 -- inlined code
4159
4160 Remove (Parent (Targ1));
4161
4162 -- Update the reference to the result (since we have rewriten the
4163 -- object declaration)
4164
4165 declare
4166 Blk_Call_Stmt : Node_Id;
4167
4168 begin
4169 -- Capture the call to the procedure
4170
4171 Blk_Call_Stmt :=
4172 First (Statements (Handled_Statement_Sequence (Blk)));
4173 pragma Assert
4174 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
4175
4176 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
4177 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
4178 New_Occurrence_Of (Targ, Loc));
4179 end;
4180
4181 -- Remove the return statement
4182
4183 pragma Assert
4184 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4185 N_Simple_Return_Statement);
4186
4187 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4188 end if;
4189
4190 -- Traverse the tree and replace formals with actuals or their thunks.
4191 -- Attach block to tree before analysis and rewriting.
4192
4193 Replace_Formals (Blk);
4194 Replace_Formals_In_Aspects (Blk);
4195 Set_Parent (Blk, N);
4196
4197 if GNATprove_Mode then
4198 null;
4199
4200 elsif not Comes_From_Source (Subp) or else Is_Predef then
4201 Reset_Slocs (Blk);
4202 end if;
4203
4204 if Is_Unc_Decl then
4205
4206 -- No action needed since return statement has been already removed
4207
4208 null;
4209
4210 elsif Present (Exit_Lab) then
4211
4212 -- If there's a single return statement at the end of the subprogram,
4213 -- the corresponding goto statement and the corresponding label are
4214 -- useless.
4215
4216 if Num_Ret = 1
4217 and then
4218 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
4219 N_Goto_Statement
4220 then
4221 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
4222 else
4223 Append (Lab_Decl, (Declarations (Blk)));
4224 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
4225 end if;
4226 end if;
4227
4228 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
4229 -- on conflicting private views that Gigi would ignore. If this is a
4230 -- predefined unit, analyze with checks off, as is done in the non-
4231 -- inlined run-time units.
4232
4233 declare
4234 I_Flag : constant Boolean := In_Inlined_Body;
4235
4236 begin
4237 In_Inlined_Body := True;
4238
4239 if Is_Predef then
4240 declare
4241 Style : constant Boolean := Style_Check;
4242
4243 begin
4244 Style_Check := False;
4245
4246 -- Search for dispatching calls that use the Object.Operation
4247 -- notation using an Object that is a parameter of the inlined
4248 -- function. We reset the decoration of Operation to force
4249 -- the reanalysis of the inlined dispatching call because
4250 -- the actual object has been inlined.
4251
4252 Reset_Dispatching_Calls (Blk);
4253
4254 -- In GNATprove mode, always consider checks on, even for
4255 -- predefined units.
4256
4257 if GNATprove_Mode then
4258 Analyze (Blk);
4259 else
4260 Analyze (Blk, Suppress => All_Checks);
4261 end if;
4262
4263 Style_Check := Style;
4264 end;
4265
4266 else
4267 Analyze (Blk);
4268 end if;
4269
4270 In_Inlined_Body := I_Flag;
4271 end;
4272
4273 if Ekind (Subp) = E_Procedure then
4274 Rewrite_Procedure_Call (N, Blk);
4275
4276 else
4277 Rewrite_Function_Call (N, Blk);
4278
4279 if Is_Unc_Decl then
4280 null;
4281
4282 -- For the unconstrained case, the replacement of the call has been
4283 -- made prior to the complete analysis of the generated declarations.
4284 -- Propagate the proper type now.
4285
4286 elsif Is_Unc then
4287 if Nkind (N) = N_Identifier then
4288 Set_Etype (N, Etype (Entity (N)));
4289 else
4290 Set_Etype (N, Etype (Targ1));
4291 end if;
4292 end if;
4293 end if;
4294
4295 Restore_Env;
4296
4297 -- Cleanup mapping between formals and actuals for other expansions
4298
4299 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4300 end Expand_Inlined_Call;
4301
4302 --------------------------
4303 -- Get_Code_Unit_Entity --
4304 --------------------------
4305
4306 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
4307 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
4308
4309 begin
4310 if Ekind (Unit) = E_Package_Body then
4311 Unit := Spec_Entity (Unit);
4312 end if;
4313
4314 return Unit;
4315 end Get_Code_Unit_Entity;
4316
4317 ------------------------------
4318 -- Has_Excluded_Declaration --
4319 ------------------------------
4320
4321 function Has_Excluded_Declaration
4322 (Subp : Entity_Id;
4323 Decls : List_Id) return Boolean
4324 is
4325 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4326 -- Nested subprograms make a given body ineligible for inlining, but
4327 -- we make an exception for instantiations of unchecked conversion.
4328 -- The body has not been analyzed yet, so check the name, and verify
4329 -- that the visible entity with that name is the predefined unit.
4330
4331 -----------------------------
4332 -- Is_Unchecked_Conversion --
4333 -----------------------------
4334
4335 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4336 Id : constant Node_Id := Name (D);
4337 Conv : Entity_Id;
4338
4339 begin
4340 if Nkind (Id) = N_Identifier
4341 and then Chars (Id) = Name_Unchecked_Conversion
4342 then
4343 Conv := Current_Entity (Id);
4344
4345 elsif Nkind (Id) in N_Selected_Component | N_Expanded_Name
4346 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4347 then
4348 Conv := Current_Entity (Selector_Name (Id));
4349 else
4350 return False;
4351 end if;
4352
4353 return Present (Conv)
4354 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
4355 and then Is_Intrinsic_Subprogram (Conv);
4356 end Is_Unchecked_Conversion;
4357
4358 -- Local variables
4359
4360 Decl : Node_Id;
4361
4362 -- Start of processing for Has_Excluded_Declaration
4363
4364 begin
4365 -- No action needed if the check is not needed
4366
4367 if not Check_Inlining_Restrictions then
4368 return False;
4369 end if;
4370
4371 Decl := First (Decls);
4372 while Present (Decl) loop
4373
4374 -- First declarations universally excluded
4375
4376 if Nkind (Decl) = N_Package_Declaration then
4377 Cannot_Inline
4378 ("cannot inline & (nested package declaration)?", Decl, Subp);
4379 return True;
4380
4381 elsif Nkind (Decl) = N_Package_Instantiation then
4382 Cannot_Inline
4383 ("cannot inline & (nested package instantiation)?", Decl, Subp);
4384 return True;
4385 end if;
4386
4387 -- Then declarations excluded only for front-end inlining
4388
4389 if Back_End_Inlining then
4390 null;
4391
4392 elsif Nkind (Decl) = N_Task_Type_Declaration
4393 or else Nkind (Decl) = N_Single_Task_Declaration
4394 then
4395 Cannot_Inline
4396 ("cannot inline & (nested task type declaration)?", Decl, Subp);
4397 return True;
4398
4399 elsif Nkind (Decl) in N_Protected_Type_Declaration
4400 | N_Single_Protected_Declaration
4401 then
4402 Cannot_Inline
4403 ("cannot inline & (nested protected type declaration)?",
4404 Decl, Subp);
4405 return True;
4406
4407 elsif Nkind (Decl) = N_Subprogram_Body then
4408 Cannot_Inline
4409 ("cannot inline & (nested subprogram)?", Decl, Subp);
4410 return True;
4411
4412 elsif Nkind (Decl) = N_Function_Instantiation
4413 and then not Is_Unchecked_Conversion (Decl)
4414 then
4415 Cannot_Inline
4416 ("cannot inline & (nested function instantiation)?", Decl, Subp);
4417 return True;
4418
4419 elsif Nkind (Decl) = N_Procedure_Instantiation then
4420 Cannot_Inline
4421 ("cannot inline & (nested procedure instantiation)?",
4422 Decl, Subp);
4423 return True;
4424
4425 -- Subtype declarations with predicates will generate predicate
4426 -- functions, i.e. nested subprogram bodies, so inlining is not
4427 -- possible.
4428
4429 elsif Nkind (Decl) = N_Subtype_Declaration then
4430 declare
4431 A : Node_Id;
4432 A_Id : Aspect_Id;
4433
4434 begin
4435 A := First (Aspect_Specifications (Decl));
4436 while Present (A) loop
4437 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4438
4439 if A_Id = Aspect_Predicate
4440 or else A_Id = Aspect_Static_Predicate
4441 or else A_Id = Aspect_Dynamic_Predicate
4442 then
4443 Cannot_Inline
4444 ("cannot inline & (subtype declaration with "
4445 & "predicate)?", Decl, Subp);
4446 return True;
4447 end if;
4448
4449 Next (A);
4450 end loop;
4451 end;
4452 end if;
4453
4454 Next (Decl);
4455 end loop;
4456
4457 return False;
4458 end Has_Excluded_Declaration;
4459
4460 ----------------------------
4461 -- Has_Excluded_Statement --
4462 ----------------------------
4463
4464 function Has_Excluded_Statement
4465 (Subp : Entity_Id;
4466 Stats : List_Id) return Boolean
4467 is
4468 S : Node_Id;
4469 E : Node_Id;
4470
4471 begin
4472 -- No action needed if the check is not needed
4473
4474 if not Check_Inlining_Restrictions then
4475 return False;
4476 end if;
4477
4478 S := First (Stats);
4479 while Present (S) loop
4480 if Nkind (S) in N_Abort_Statement
4481 | N_Asynchronous_Select
4482 | N_Conditional_Entry_Call
4483 | N_Delay_Relative_Statement
4484 | N_Delay_Until_Statement
4485 | N_Selective_Accept
4486 | N_Timed_Entry_Call
4487 then
4488 Cannot_Inline
4489 ("cannot inline & (non-allowed statement)?", S, Subp);
4490 return True;
4491
4492 elsif Nkind (S) = N_Block_Statement then
4493 if Has_Excluded_Declaration (Subp, Declarations (S)) then
4494 return True;
4495
4496 elsif Present (Handled_Statement_Sequence (S)) then
4497 if not Back_End_Inlining
4498 and then
4499 Present
4500 (Exception_Handlers (Handled_Statement_Sequence (S)))
4501 then
4502 Cannot_Inline
4503 ("cannot inline& (exception handler)?",
4504 First (Exception_Handlers
4505 (Handled_Statement_Sequence (S))),
4506 Subp);
4507 return True;
4508
4509 elsif Has_Excluded_Statement
4510 (Subp, Statements (Handled_Statement_Sequence (S)))
4511 then
4512 return True;
4513 end if;
4514 end if;
4515
4516 elsif Nkind (S) = N_Case_Statement then
4517 E := First (Alternatives (S));
4518 while Present (E) loop
4519 if Has_Excluded_Statement (Subp, Statements (E)) then
4520 return True;
4521 end if;
4522
4523 Next (E);
4524 end loop;
4525
4526 elsif Nkind (S) = N_If_Statement then
4527 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
4528 return True;
4529 end if;
4530
4531 if Present (Elsif_Parts (S)) then
4532 E := First (Elsif_Parts (S));
4533 while Present (E) loop
4534 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
4535 return True;
4536 end if;
4537
4538 Next (E);
4539 end loop;
4540 end if;
4541
4542 if Present (Else_Statements (S))
4543 and then Has_Excluded_Statement (Subp, Else_Statements (S))
4544 then
4545 return True;
4546 end if;
4547
4548 elsif Nkind (S) = N_Loop_Statement
4549 and then Has_Excluded_Statement (Subp, Statements (S))
4550 then
4551 return True;
4552
4553 elsif Nkind (S) = N_Extended_Return_Statement then
4554 if Present (Handled_Statement_Sequence (S))
4555 and then
4556 Has_Excluded_Statement
4557 (Subp, Statements (Handled_Statement_Sequence (S)))
4558 then
4559 return True;
4560
4561 elsif not Back_End_Inlining
4562 and then Present (Handled_Statement_Sequence (S))
4563 and then
4564 Present (Exception_Handlers
4565 (Handled_Statement_Sequence (S)))
4566 then
4567 Cannot_Inline
4568 ("cannot inline& (exception handler)?",
4569 First (Exception_Handlers (Handled_Statement_Sequence (S))),
4570 Subp);
4571 return True;
4572 end if;
4573 end if;
4574
4575 Next (S);
4576 end loop;
4577
4578 return False;
4579 end Has_Excluded_Statement;
4580
4581 --------------------------
4582 -- Has_Initialized_Type --
4583 --------------------------
4584
4585 function Has_Initialized_Type (E : Entity_Id) return Boolean is
4586 E_Body : constant Node_Id := Subprogram_Body (E);
4587 Decl : Node_Id;
4588
4589 begin
4590 if No (E_Body) then -- imported subprogram
4591 return False;
4592
4593 else
4594 Decl := First (Declarations (E_Body));
4595 while Present (Decl) loop
4596 if Nkind (Decl) = N_Full_Type_Declaration
4597 and then Comes_From_Source (Decl)
4598 and then Present (Init_Proc (Defining_Identifier (Decl)))
4599 then
4600 return True;
4601 end if;
4602
4603 Next (Decl);
4604 end loop;
4605 end if;
4606
4607 return False;
4608 end Has_Initialized_Type;
4609
4610 -----------------------
4611 -- Has_Single_Return --
4612 -----------------------
4613
4614 function Has_Single_Return (N : Node_Id) return Boolean is
4615 Return_Statement : Node_Id := Empty;
4616
4617 function Check_Return (N : Node_Id) return Traverse_Result;
4618
4619 ------------------
4620 -- Check_Return --
4621 ------------------
4622
4623 function Check_Return (N : Node_Id) return Traverse_Result is
4624 begin
4625 if Nkind (N) = N_Simple_Return_Statement then
4626 if Present (Expression (N))
4627 and then Is_Entity_Name (Expression (N))
4628 then
4629 pragma Assert (Present (Entity (Expression (N))));
4630
4631 if No (Return_Statement) then
4632 Return_Statement := N;
4633 return OK;
4634
4635 else
4636 pragma Assert
4637 (Present (Entity (Expression (Return_Statement))));
4638
4639 if Entity (Expression (N)) =
4640 Entity (Expression (Return_Statement))
4641 then
4642 return OK;
4643 else
4644 return Abandon;
4645 end if;
4646 end if;
4647
4648 -- A return statement within an extended return is a noop after
4649 -- inlining.
4650
4651 elsif No (Expression (N))
4652 and then Nkind (Parent (Parent (N))) =
4653 N_Extended_Return_Statement
4654 then
4655 return OK;
4656
4657 else
4658 -- Expression has wrong form
4659
4660 return Abandon;
4661 end if;
4662
4663 -- We can only inline a build-in-place function if it has a single
4664 -- extended return.
4665
4666 elsif Nkind (N) = N_Extended_Return_Statement then
4667 if No (Return_Statement) then
4668 Return_Statement := N;
4669 return OK;
4670
4671 else
4672 return Abandon;
4673 end if;
4674
4675 else
4676 return OK;
4677 end if;
4678 end Check_Return;
4679
4680 function Check_All_Returns is new Traverse_Func (Check_Return);
4681
4682 -- Start of processing for Has_Single_Return
4683
4684 begin
4685 if Check_All_Returns (N) /= OK then
4686 return False;
4687
4688 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4689 return True;
4690
4691 else
4692 return
4693 Present (First (Declarations (N)))
4694 and then Nkind (First (Declarations (N))) = N_Object_Declaration
4695 and then Entity (Expression (Return_Statement)) =
4696 Defining_Identifier (First (Declarations (N)));
4697 end if;
4698 end Has_Single_Return;
4699
4700 -----------------------------
4701 -- In_Main_Unit_Or_Subunit --
4702 -----------------------------
4703
4704 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
4705 Comp : Node_Id := Cunit (Get_Code_Unit (E));
4706
4707 begin
4708 -- Check whether the subprogram or package to inline is within the main
4709 -- unit or its spec or within a subunit. In either case there are no
4710 -- additional bodies to process. If the subprogram appears in a parent
4711 -- of the current unit, the check on whether inlining is possible is
4712 -- done in Analyze_Inlined_Bodies.
4713
4714 while Nkind (Unit (Comp)) = N_Subunit loop
4715 Comp := Library_Unit (Comp);
4716 end loop;
4717
4718 return Comp = Cunit (Main_Unit)
4719 or else Comp = Library_Unit (Cunit (Main_Unit));
4720 end In_Main_Unit_Or_Subunit;
4721
4722 ----------------
4723 -- Initialize --
4724 ----------------
4725
4726 procedure Initialize is
4727 begin
4728 Pending_Instantiations.Init;
4729 Called_Pending_Instantiations.Init;
4730 Inlined_Bodies.Init;
4731 Successors.Init;
4732 Inlined.Init;
4733
4734 for J in Hash_Headers'Range loop
4735 Hash_Headers (J) := No_Subp;
4736 end loop;
4737
4738 Inlined_Calls := No_Elist;
4739 Backend_Calls := No_Elist;
4740 Backend_Instances := No_Elist;
4741 Backend_Inlined_Subps := No_Elist;
4742 Backend_Not_Inlined_Subps := No_Elist;
4743 end Initialize;
4744
4745 ---------------------------------
4746 -- Inline_Static_Function_Call --
4747 ---------------------------------
4748
4749 procedure Inline_Static_Function_Call (N : Node_Id; Subp : Entity_Id) is
4750
4751 function Replace_Formal (N : Node_Id) return Traverse_Result;
4752 -- Replace each occurrence of a formal with the
4753 -- corresponding actual, using the mapping created
4754 -- by Establish_Actual_Mapping_For_Inlined_Call.
4755
4756 function Reset_Sloc (Nod : Node_Id) return Traverse_Result;
4757 -- Reset the Sloc of a node to that of the call itself, so that errors
4758 -- will be flagged on the call to the static expression function itself
4759 -- rather than on the expression of the function's declaration.
4760
4761 --------------------
4762 -- Replace_Formal --
4763 --------------------
4764
4765 function Replace_Formal (N : Node_Id) return Traverse_Result is
4766 A : Entity_Id;
4767 E : Entity_Id;
4768
4769 begin
4770 if Is_Entity_Name (N) and then Present (Entity (N)) then
4771 E := Entity (N);
4772
4773 if Is_Formal (E) and then Scope (E) = Subp then
4774 A := Renamed_Object (E);
4775
4776 if Nkind (A) = N_Defining_Identifier then
4777 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
4778
4779 -- Literal cases
4780
4781 else
4782 Rewrite (N, New_Copy (A));
4783 end if;
4784 end if;
4785
4786 return Skip;
4787
4788 else
4789 return OK;
4790 end if;
4791 end Replace_Formal;
4792
4793 procedure Replace_Formals is new Traverse_Proc (Replace_Formal);
4794
4795 ------------------
4796 -- Process_Sloc --
4797 ------------------
4798
4799 function Reset_Sloc (Nod : Node_Id) return Traverse_Result is
4800 begin
4801 Set_Sloc (Nod, Sloc (N));
4802 Set_Comes_From_Source (Nod, False);
4803
4804 return OK;
4805 end Reset_Sloc;
4806
4807 procedure Reset_Slocs is new Traverse_Proc (Reset_Sloc);
4808
4809 -- Start of processing for Inline_Static_Function_Call
4810
4811 begin
4812 pragma Assert (Is_Static_Function_Call (N));
4813
4814 declare
4815 Decls : constant List_Id := New_List;
4816 Func_Expr : constant Node_Id :=
4817 Expression_Of_Expression_Function (Subp);
4818 Expr_Copy : constant Node_Id := New_Copy_Tree (Func_Expr);
4819
4820 begin
4821 -- Create a mapping from formals to actuals, also creating temps in
4822 -- Decls, when needed, to hold the actuals.
4823
4824 Establish_Actual_Mapping_For_Inlined_Call (N, Subp, Decls, Func_Expr);
4825
4826 -- Ensure that the copy has the same parent as the call (this seems
4827 -- to matter when GNATprove_Mode is set and there are nested static
4828 -- calls; prevents blowups in Insert_Actions, though it's not clear
4829 -- exactly why this is needed???).
4830
4831 Set_Parent (Expr_Copy, Parent (N));
4832
4833 Insert_Actions (N, Decls);
4834
4835 -- Now substitute actuals for their corresponding formal references
4836 -- within the expression.
4837
4838 Replace_Formals (Expr_Copy);
4839
4840 Reset_Slocs (Expr_Copy);
4841
4842 -- Apply a qualified expression with the function's result subtype,
4843 -- to ensure that we check the expression against any constraint
4844 -- or predicate, which will cause the call to be illegal if the
4845 -- folded expression doesn't satisfy them. (The predicate case
4846 -- might not get checked if the subtype hasn't been frozen yet,
4847 -- which can happen if this static expression happens to be what
4848 -- causes the freezing, because Has_Static_Predicate doesn't get
4849 -- set on the subtype until it's frozen and Build_Predicates is
4850 -- called. It's not clear how to address this case. ???)
4851
4852 Rewrite (Expr_Copy,
4853 Make_Qualified_Expression (Sloc (Expr_Copy),
4854 Subtype_Mark =>
4855 New_Occurrence_Of (Etype (N), Sloc (Expr_Copy)),
4856 Expression =>
4857 Relocate_Node (Expr_Copy)));
4858
4859 Set_Etype (Expr_Copy, Etype (N));
4860
4861 Analyze_And_Resolve (Expr_Copy, Etype (N));
4862
4863 -- Finally rewrite the function call as the folded static result
4864
4865 Rewrite (N, Expr_Copy);
4866
4867 -- Cleanup mapping between formals and actuals for other expansions
4868
4869 Reset_Actual_Mapping_For_Inlined_Call (Subp);
4870 end;
4871 end Inline_Static_Function_Call;
4872
4873 ------------------------
4874 -- Instantiate_Bodies --
4875 ------------------------
4876
4877 -- Generic bodies contain all the non-local references, so an
4878 -- instantiation does not need any more context than Standard
4879 -- itself, even if the instantiation appears in an inner scope.
4880 -- Generic associations have verified that the contract model is
4881 -- satisfied, so that any error that may occur in the analysis of
4882 -- the body is an internal error.
4883
4884 procedure Instantiate_Bodies is
4885
4886 procedure Instantiate_Body (Info : Pending_Body_Info);
4887 -- Instantiate a pending body
4888
4889 ------------------------
4890 -- Instantiate_Body --
4891 ------------------------
4892
4893 procedure Instantiate_Body (Info : Pending_Body_Info) is
4894 Scop : Entity_Id;
4895
4896 begin
4897 -- If the instantiation node is absent, it has been removed as part
4898 -- of unreachable code.
4899
4900 if No (Info.Inst_Node) then
4901 null;
4902
4903 -- If the instantiation node is a package body, this means that the
4904 -- instance is a compilation unit and the instantiation has already
4905 -- been performed by Build_Instance_Compilation_Unit_Nodes.
4906
4907 elsif Nkind (Info.Inst_Node) = N_Package_Body then
4908 null;
4909
4910 -- For other package instances, instantiate the body and register the
4911 -- finalization scope, if any, for subsequent generation of cleanups.
4912
4913 elsif Nkind (Info.Inst_Node) = N_Package_Instantiation then
4914
4915 -- If the enclosing finalization scope is a package body, set the
4916 -- In_Package_Body flag on its spec. This is required, in the case
4917 -- where the body contains other package instantiations that have
4918 -- a body, for Analyze_Package_Instantiation to compute a correct
4919 -- finalization scope.
4920
4921 if Present (Info.Fin_Scop)
4922 and then Ekind (Info.Fin_Scop) = E_Package_Body
4923 then
4924 Set_In_Package_Body (Spec_Entity (Info.Fin_Scop), True);
4925 end if;
4926
4927 Instantiate_Package_Body (Info);
4928
4929 if Present (Info.Fin_Scop) then
4930 Scop := Info.Fin_Scop;
4931
4932 -- If the enclosing finalization scope is dynamic, the instance
4933 -- may have been relocated, for example if it was declared in a
4934 -- protected entry, protected subprogram, or task body.
4935
4936 if Is_Dynamic_Scope (Scop) then
4937 Scop :=
4938 Enclosing_Dynamic_Scope (Defining_Entity (Info.Act_Decl));
4939 end if;
4940
4941 Add_Scope_To_Clean (Scop);
4942
4943 -- Reset the In_Package_Body flag if it was set above
4944
4945 if Ekind (Info.Fin_Scop) = E_Package_Body then
4946 Set_In_Package_Body (Spec_Entity (Info.Fin_Scop), False);
4947 end if;
4948 end if;
4949
4950 -- For subprogram instances, always instantiate the body
4951
4952 else
4953 Instantiate_Subprogram_Body (Info);
4954 end if;
4955 end Instantiate_Body;
4956
4957 J, K : Nat;
4958 Info : Pending_Body_Info;
4959
4960 -- Start of processing for Instantiate_Bodies
4961
4962 begin
4963 if Serious_Errors_Detected = 0 then
4964 Expander_Active := (Operating_Mode = Opt.Generate_Code);
4965 Push_Scope (Standard_Standard);
4966 To_Clean := New_Elmt_List;
4967
4968 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4969 Start_Generic;
4970 end if;
4971
4972 -- A body instantiation may generate additional instantiations, so
4973 -- the following loop must scan to the end of a possibly expanding
4974 -- set (that's why we cannot simply use a FOR loop here). We must
4975 -- also capture the element lest the set be entirely reallocated.
4976
4977 J := 0;
4978 if Back_End_Inlining then
4979 while J <= Called_Pending_Instantiations.Last
4980 and then Serious_Errors_Detected = 0
4981 loop
4982 K := Called_Pending_Instantiations.Table (J);
4983 Info := Pending_Instantiations.Table (K);
4984 Instantiate_Body (Info);
4985
4986 J := J + 1;
4987 end loop;
4988
4989 else
4990 while J <= Pending_Instantiations.Last
4991 and then Serious_Errors_Detected = 0
4992 loop
4993 Info := Pending_Instantiations.Table (J);
4994 Instantiate_Body (Info);
4995
4996 J := J + 1;
4997 end loop;
4998 end if;
4999
5000 -- Reset the table of instantiations. Additional instantiations
5001 -- may be added through inlining, when additional bodies are
5002 -- analyzed.
5003
5004 if Back_End_Inlining then
5005 Called_Pending_Instantiations.Init;
5006 else
5007 Pending_Instantiations.Init;
5008 end if;
5009
5010 -- We can now complete the cleanup actions of scopes that contain
5011 -- pending instantiations (skipped for generic units, since we
5012 -- never need any cleanups in generic units).
5013
5014 if Expander_Active
5015 and then not Is_Generic_Unit (Main_Unit_Entity)
5016 then
5017 Cleanup_Scopes;
5018 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
5019 End_Generic;
5020 end if;
5021
5022 Pop_Scope;
5023 end if;
5024 end Instantiate_Bodies;
5025
5026 ---------------
5027 -- Is_Nested --
5028 ---------------
5029
5030 function Is_Nested (E : Entity_Id) return Boolean is
5031 Scop : Entity_Id;
5032
5033 begin
5034 Scop := Scope (E);
5035 while Scop /= Standard_Standard loop
5036 if Is_Subprogram (Scop) then
5037 return True;
5038
5039 elsif Ekind (Scop) = E_Task_Type
5040 or else Ekind (Scop) = E_Entry
5041 or else Ekind (Scop) = E_Entry_Family
5042 then
5043 return True;
5044 end if;
5045
5046 Scop := Scope (Scop);
5047 end loop;
5048
5049 return False;
5050 end Is_Nested;
5051
5052 ------------------------
5053 -- List_Inlining_Info --
5054 ------------------------
5055
5056 procedure List_Inlining_Info is
5057 Elmt : Elmt_Id;
5058 Nod : Node_Id;
5059 Count : Nat;
5060
5061 begin
5062 if not Debug_Flag_Dot_J then
5063 return;
5064 end if;
5065
5066 -- Generate listing of calls inlined by the frontend
5067
5068 if Present (Inlined_Calls) then
5069 Count := 0;
5070 Elmt := First_Elmt (Inlined_Calls);
5071 while Present (Elmt) loop
5072 Nod := Node (Elmt);
5073
5074 if not In_Internal_Unit (Nod) then
5075 Count := Count + 1;
5076
5077 if Count = 1 then
5078 Write_Str ("List of calls inlined by the frontend");
5079 Write_Eol;
5080 end if;
5081
5082 Write_Str (" ");
5083 Write_Int (Count);
5084 Write_Str (":");
5085 Write_Location (Sloc (Nod));
5086 Write_Str (":");
5087 Output.Write_Eol;
5088 end if;
5089
5090 Next_Elmt (Elmt);
5091 end loop;
5092 end if;
5093
5094 -- Generate listing of calls passed to the backend
5095
5096 if Present (Backend_Calls) then
5097 Count := 0;
5098
5099 Elmt := First_Elmt (Backend_Calls);
5100 while Present (Elmt) loop
5101 Nod := Node (Elmt);
5102
5103 if not In_Internal_Unit (Nod) then
5104 Count := Count + 1;
5105
5106 if Count = 1 then
5107 Write_Str ("List of inlined calls passed to the backend");
5108 Write_Eol;
5109 end if;
5110
5111 Write_Str (" ");
5112 Write_Int (Count);
5113 Write_Str (":");
5114 Write_Location (Sloc (Nod));
5115 Output.Write_Eol;
5116 end if;
5117
5118 Next_Elmt (Elmt);
5119 end loop;
5120 end if;
5121
5122 -- Generate listing of instances inlined for the backend
5123
5124 if Present (Backend_Instances) then
5125 Count := 0;
5126
5127 Elmt := First_Elmt (Backend_Instances);
5128 while Present (Elmt) loop
5129 Nod := Node (Elmt);
5130
5131 if not In_Internal_Unit (Nod) then
5132 Count := Count + 1;
5133
5134 if Count = 1 then
5135 Write_Str ("List of instances inlined for the backend");
5136 Write_Eol;
5137 end if;
5138
5139 Write_Str (" ");
5140 Write_Int (Count);
5141 Write_Str (":");
5142 Write_Location (Sloc (Nod));
5143 Output.Write_Eol;
5144 end if;
5145
5146 Next_Elmt (Elmt);
5147 end loop;
5148 end if;
5149
5150 -- Generate listing of subprograms passed to the backend
5151
5152 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
5153 Count := 0;
5154
5155 Elmt := First_Elmt (Backend_Inlined_Subps);
5156 while Present (Elmt) loop
5157 Nod := Node (Elmt);
5158
5159 if not In_Internal_Unit (Nod) then
5160 Count := Count + 1;
5161
5162 if Count = 1 then
5163 Write_Str
5164 ("List of inlined subprograms passed to the backend");
5165 Write_Eol;
5166 end if;
5167
5168 Write_Str (" ");
5169 Write_Int (Count);
5170 Write_Str (":");
5171 Write_Name (Chars (Nod));
5172 Write_Str (" (");
5173 Write_Location (Sloc (Nod));
5174 Write_Str (")");
5175 Output.Write_Eol;
5176 end if;
5177
5178 Next_Elmt (Elmt);
5179 end loop;
5180 end if;
5181
5182 -- Generate listing of subprograms that cannot be inlined by the backend
5183
5184 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
5185 Count := 0;
5186
5187 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
5188 while Present (Elmt) loop
5189 Nod := Node (Elmt);
5190
5191 if not In_Internal_Unit (Nod) then
5192 Count := Count + 1;
5193
5194 if Count = 1 then
5195 Write_Str
5196 ("List of subprograms that cannot be inlined by backend");
5197 Write_Eol;
5198 end if;
5199
5200 Write_Str (" ");
5201 Write_Int (Count);
5202 Write_Str (":");
5203 Write_Name (Chars (Nod));
5204 Write_Str (" (");
5205 Write_Location (Sloc (Nod));
5206 Write_Str (")");
5207 Output.Write_Eol;
5208 end if;
5209
5210 Next_Elmt (Elmt);
5211 end loop;
5212 end if;
5213 end List_Inlining_Info;
5214
5215 ----------
5216 -- Lock --
5217 ----------
5218
5219 procedure Lock is
5220 begin
5221 Pending_Instantiations.Release;
5222 Pending_Instantiations.Locked := True;
5223 Called_Pending_Instantiations.Release;
5224 Called_Pending_Instantiations.Locked := True;
5225 Inlined_Bodies.Release;
5226 Inlined_Bodies.Locked := True;
5227 Successors.Release;
5228 Successors.Locked := True;
5229 Inlined.Release;
5230 Inlined.Locked := True;
5231 end Lock;
5232
5233 --------------------------------
5234 -- Remove_Aspects_And_Pragmas --
5235 --------------------------------
5236
5237 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
5238 procedure Remove_Items (List : List_Id);
5239 -- Remove all useless aspects/pragmas from a particular list
5240
5241 ------------------
5242 -- Remove_Items --
5243 ------------------
5244
5245 procedure Remove_Items (List : List_Id) is
5246 Item : Node_Id;
5247 Item_Id : Node_Id;
5248 Next_Item : Node_Id;
5249
5250 begin
5251 -- Traverse the list looking for an aspect specification or a pragma
5252
5253 Item := First (List);
5254 while Present (Item) loop
5255 Next_Item := Next (Item);
5256
5257 if Nkind (Item) = N_Aspect_Specification then
5258 Item_Id := Identifier (Item);
5259 elsif Nkind (Item) = N_Pragma then
5260 Item_Id := Pragma_Identifier (Item);
5261 else
5262 Item_Id := Empty;
5263 end if;
5264
5265 if Present (Item_Id)
5266 and then Chars (Item_Id) in Name_Always_Terminates
5267 | Name_Contract_Cases
5268 | Name_Global
5269 | Name_Depends
5270 | Name_Exceptional_Cases
5271 | Name_Postcondition
5272 | Name_Precondition
5273 | Name_Refined_Global
5274 | Name_Refined_Depends
5275 | Name_Refined_Post
5276 | Name_Subprogram_Variant
5277 | Name_Test_Case
5278 | Name_Unmodified
5279 | Name_Unreferenced
5280 | Name_Unused
5281 then
5282 Remove (Item);
5283 end if;
5284
5285 Item := Next_Item;
5286 end loop;
5287 end Remove_Items;
5288
5289 -- Start of processing for Remove_Aspects_And_Pragmas
5290
5291 begin
5292 Remove_Items (Aspect_Specifications (Body_Decl));
5293 Remove_Items (Declarations (Body_Decl));
5294
5295 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
5296 -- in the body of the subprogram.
5297
5298 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
5299 end Remove_Aspects_And_Pragmas;
5300
5301 --------------------------
5302 -- Remove_Dead_Instance --
5303 --------------------------
5304
5305 procedure Remove_Dead_Instance (N : Node_Id) is
5306 begin
5307 for J in 0 .. Pending_Instantiations.Last loop
5308 if Pending_Instantiations.Table (J).Inst_Node = N then
5309 Pending_Instantiations.Table (J).Inst_Node := Empty;
5310 return;
5311 end if;
5312 end loop;
5313 end Remove_Dead_Instance;
5314
5315 -------------------------------------------
5316 -- Reset_Actual_Mapping_For_Inlined_Call --
5317 -------------------------------------------
5318
5319 procedure Reset_Actual_Mapping_For_Inlined_Call (Subp : Entity_Id) is
5320 F : Entity_Id := First_Formal (Subp);
5321
5322 begin
5323 while Present (F) loop
5324 Set_Renamed_Object (F, Empty);
5325 Next_Formal (F);
5326 end loop;
5327 end Reset_Actual_Mapping_For_Inlined_Call;
5328
5329 end Inline;
This page took 0.274527 seconds and 6 git commands to generate.