OSDN Git Service

2011-08-02 Eric Botcazou <ebotcazou@adacore.com>
[pf3gnuchains/gcc-fork.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-2010, 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 Atree;    use Atree;
27 with Einfo;    use Einfo;
28 with Elists;   use Elists;
29 with Errout;   use Errout;
30 with Exp_Ch7;  use Exp_Ch7;
31 with Exp_Tss;  use Exp_Tss;
32 with Fname;    use Fname;
33 with Fname.UF; use Fname.UF;
34 with Lib;      use Lib;
35 with Namet;    use Namet;
36 with Nlists;   use Nlists;
37 with Sem_Aux;  use Sem_Aux;
38 with Sem_Ch8;  use Sem_Ch8;
39 with Sem_Ch10; use Sem_Ch10;
40 with Sem_Ch12; use Sem_Ch12;
41 with Sem_Util; use Sem_Util;
42 with Sinfo;    use Sinfo;
43 with Snames;   use Snames;
44 with Stand;    use Stand;
45 with Uname;    use Uname;
46
47 package body Inline is
48
49    --------------------
50    -- Inlined Bodies --
51    --------------------
52
53    --  Inlined functions are actually placed in line by the backend if the
54    --  corresponding bodies are available (i.e. compiled). Whenever we find
55    --  a call to an inlined subprogram, we add the name of the enclosing
56    --  compilation unit to a worklist. After all compilation, and after
57    --  expansion of generic bodies, we traverse the list of pending bodies
58    --  and compile them as well.
59
60    package Inlined_Bodies is new Table.Table (
61      Table_Component_Type => Entity_Id,
62      Table_Index_Type     => Int,
63      Table_Low_Bound      => 0,
64      Table_Initial        => Alloc.Inlined_Bodies_Initial,
65      Table_Increment      => Alloc.Inlined_Bodies_Increment,
66      Table_Name           => "Inlined_Bodies");
67
68    -----------------------
69    -- Inline Processing --
70    -----------------------
71
72    --  For each call to an inlined subprogram, we make entries in a table
73    --  that stores caller and callee, and indicates a prerequisite from
74    --  one to the other. We also record the compilation unit that contains
75    --  the callee. After analyzing the bodies of all such compilation units,
76    --  we produce a list of subprograms in  topological order, for use by the
77    --  back-end. If P2 is a prerequisite of P1, then P1 calls P2, and for
78    --  proper inlining the back-end must analyze the body of P2 before that of
79    --  P1. The code below guarantees that the transitive closure of inlined
80    --  subprograms called from the main compilation unit is made available to
81    --  the code generator.
82
83    Last_Inlined : Entity_Id := Empty;
84
85    --  For each entry in the table we keep a list of successors in topological
86    --  order, i.e. callers of the current subprogram.
87
88    type Subp_Index is new Nat;
89    No_Subp : constant Subp_Index := 0;
90
91    --  The subprogram entities are hashed into the Inlined table
92
93    Num_Hash_Headers : constant := 512;
94
95    Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
96                                                           of Subp_Index;
97
98    type Succ_Index is new Nat;
99    No_Succ : constant Succ_Index := 0;
100
101    type Succ_Info is record
102       Subp : Subp_Index;
103       Next : Succ_Index;
104    end record;
105
106    --  The following table stores list elements for the successor lists.
107    --  These lists cannot be chained directly through entries in the Inlined
108    --  table, because a given subprogram can appear in several such lists.
109
110    package Successors is new Table.Table (
111       Table_Component_Type => Succ_Info,
112       Table_Index_Type     => Succ_Index,
113       Table_Low_Bound      => 1,
114       Table_Initial        => Alloc.Successors_Initial,
115       Table_Increment      => Alloc.Successors_Increment,
116       Table_Name           => "Successors");
117
118    type Subp_Info is record
119       Name        : Entity_Id  := Empty;
120       First_Succ  : Succ_Index := No_Succ;
121       Count       : Integer    := 0;
122       Listed      : Boolean    := False;
123       Main_Call   : Boolean    := False;
124       Next        : Subp_Index := No_Subp;
125       Next_Nopred : Subp_Index := No_Subp;
126    end record;
127
128    package Inlined is new Table.Table (
129       Table_Component_Type => Subp_Info,
130       Table_Index_Type     => Subp_Index,
131       Table_Low_Bound      => 1,
132       Table_Initial        => Alloc.Inlined_Initial,
133       Table_Increment      => Alloc.Inlined_Increment,
134       Table_Name           => "Inlined");
135
136    -----------------------
137    -- Local Subprograms --
138    -----------------------
139
140    function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id;
141    pragma Inline (Get_Code_Unit_Entity);
142    --  Return the entity node for the unit containing E
143
144    function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean;
145    --  Return True if Scop is in the main unit or its spec
146
147    procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
148    --  Make two entries in Inlined table, for an inlined subprogram being
149    --  called, and for the inlined subprogram that contains the call. If
150    --  the call is in the main compilation unit, Caller is Empty.
151
152    function Add_Subp (E : Entity_Id) return Subp_Index;
153    --  Make entry in Inlined table for subprogram E, or return table index
154    --  that already holds E.
155
156    function Has_Initialized_Type (E : Entity_Id) return Boolean;
157    --  If a candidate for inlining contains type declarations for types with
158    --  non-trivial initialization procedures, they are not worth inlining.
159
160    function Is_Nested (E : Entity_Id) return Boolean;
161    --  If the function is nested inside some other function, it will
162    --  always be compiled if that function is, so don't add it to the
163    --  inline list. We cannot compile a nested function outside the
164    --  scope of the containing function anyway. This is also the case if
165    --  the function is defined in a task body or within an entry (for
166    --  example, an initialization procedure).
167
168    procedure Add_Inlined_Subprogram (Index : Subp_Index);
169    --  Add subprogram to Inlined List once all of its predecessors have been
170    --  placed on the list. Decrement the count of all its successors, and
171    --  add them to list (recursively) if count drops to zero.
172
173    ------------------------------
174    -- Deferred Cleanup Actions --
175    ------------------------------
176
177    --  The cleanup actions for scopes that contain instantiations is delayed
178    --  until after expansion of those instantiations, because they may
179    --  contain finalizable objects or tasks that affect the cleanup code.
180    --  A scope that contains instantiations only needs to be finalized once,
181    --  even if it contains more than one instance. We keep a list of scopes
182    --  that must still be finalized, and call cleanup_actions after all the
183    --  instantiations have been completed.
184
185    To_Clean : Elist_Id;
186
187    procedure Add_Scope_To_Clean (Inst : Entity_Id);
188    --  Build set of scopes on which cleanup actions must be performed
189
190    procedure Cleanup_Scopes;
191    --  Complete cleanup actions on scopes that need it
192
193    --------------
194    -- Add_Call --
195    --------------
196
197    procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
198       P1 : constant Subp_Index := Add_Subp (Called);
199       P2 : Subp_Index;
200       J  : Succ_Index;
201
202    begin
203       if Present (Caller) then
204          P2 := Add_Subp (Caller);
205
206          --  Add P2 to the list of successors of P1, if not already there.
207          --  Note that P2 may contain more than one call to P1, and only
208          --  one needs to be recorded.
209
210          J := Inlined.Table (P1).First_Succ;
211          while J /= No_Succ loop
212             if Successors.Table (J).Subp = P2 then
213                return;
214             end if;
215
216             J := Successors.Table (J).Next;
217          end loop;
218
219          --  On exit, make a successor entry for P2
220
221          Successors.Increment_Last;
222          Successors.Table (Successors.Last).Subp := P2;
223          Successors.Table (Successors.Last).Next :=
224                              Inlined.Table (P1).First_Succ;
225          Inlined.Table (P1).First_Succ := Successors.Last;
226
227          Inlined.Table (P2).Count := Inlined.Table (P2).Count + 1;
228
229       else
230          Inlined.Table (P1).Main_Call := True;
231       end if;
232    end Add_Call;
233
234    ----------------------
235    -- Add_Inlined_Body --
236    ----------------------
237
238    procedure Add_Inlined_Body (E : Entity_Id) is
239
240       function Must_Inline return Boolean;
241       --  Inlining is only done if the call statement N is in the main unit,
242       --  or within the body of another inlined subprogram.
243
244       -----------------
245       -- Must_Inline --
246       -----------------
247
248       function Must_Inline return Boolean is
249          Scop : Entity_Id;
250          Comp : Node_Id;
251
252       begin
253          --  Check if call is in main unit
254
255          Scop := Current_Scope;
256
257          --  Do not try to inline if scope is standard. This could happen, for
258          --  example, for a call to Add_Global_Declaration, and it causes
259          --  trouble to try to inline at this level.
260
261          if Scop = Standard_Standard then
262             return False;
263          end if;
264
265          --  Otherwise lookup scope stack to outer scope
266
267          while Scope (Scop) /= Standard_Standard
268            and then not Is_Child_Unit (Scop)
269          loop
270             Scop := Scope (Scop);
271          end loop;
272
273          Comp := Parent (Scop);
274          while Nkind (Comp) /= N_Compilation_Unit loop
275             Comp := Parent (Comp);
276          end loop;
277
278          if Comp = Cunit (Main_Unit)
279            or else Comp = Library_Unit (Cunit (Main_Unit))
280          then
281             Add_Call (E);
282             return True;
283          end if;
284
285          --  Call is not in main unit. See if it's in some inlined subprogram
286
287          Scop := Current_Scope;
288          while Scope (Scop) /= Standard_Standard
289            and then not Is_Child_Unit (Scop)
290          loop
291             if Is_Overloadable (Scop)
292               and then Is_Inlined (Scop)
293             then
294                Add_Call (E, Scop);
295                return True;
296             end if;
297
298             Scop := Scope (Scop);
299          end loop;
300
301          return False;
302       end Must_Inline;
303
304    --  Start of processing for Add_Inlined_Body
305
306    begin
307       --  Find unit containing E, and add to list of inlined bodies if needed.
308       --  If the body is already present, no need to load any other unit. This
309       --  is the case for an initialization procedure, which appears in the
310       --  package declaration that contains the type. It is also the case if
311       --  the body has already been analyzed. Finally, if the unit enclosing
312       --  E is an instance, the instance body will be analyzed in any case,
313       --  and there is no need to add the enclosing unit (whose body might not
314       --  be available).
315
316       --  Library-level functions must be handled specially, because there is
317       --  no enclosing package to retrieve. In this case, it is the body of
318       --  the function that will have to be loaded.
319
320       if not Is_Abstract_Subprogram (E)
321         and then not Is_Nested (E)
322         and then Convention (E) /= Convention_Protected
323         and then Must_Inline
324       then
325          declare
326             Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
327
328          begin
329             if Pack = E then
330
331                --  Library-level inlined function. Add function itself to
332                --  list of needed units.
333
334                Set_Is_Called (E);
335                Inlined_Bodies.Increment_Last;
336                Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
337
338             elsif Ekind (Pack) = E_Package then
339                Set_Is_Called (E);
340
341                if Is_Generic_Instance (Pack) then
342                   null;
343
344                elsif not Is_Inlined (Pack)
345                  and then not Has_Completion (E)
346                then
347                   Set_Is_Inlined (Pack);
348                   Inlined_Bodies.Increment_Last;
349                   Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
350                end if;
351             end if;
352          end;
353       end if;
354    end Add_Inlined_Body;
355
356    ----------------------------
357    -- Add_Inlined_Subprogram --
358    ----------------------------
359
360    procedure Add_Inlined_Subprogram (Index : Subp_Index) is
361       E    : constant Entity_Id := Inlined.Table (Index).Name;
362       Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
363       Succ : Succ_Index;
364       Subp : Subp_Index;
365
366       function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean;
367       --  There are various conditions under which back-end inlining cannot
368       --  be done reliably:
369       --
370       --    a) If a body has handlers, it must not be inlined, because this
371       --    may violate program semantics, and because in zero-cost exception
372       --    mode it will lead to undefined symbols at link time.
373       --
374       --    b) If a body contains inlined function instances, it cannot be
375       --    inlined under ZCX because the numeric suffix generated by gigi
376       --    will be different in the body and the place of the inlined call.
377       --
378       --  This procedure must be carefully coordinated with the back end.
379
380       ----------------------------
381       -- Back_End_Cannot_Inline --
382       ----------------------------
383
384       function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean is
385          Decl     : constant Node_Id := Unit_Declaration_Node (Subp);
386          Body_Ent : Entity_Id;
387          Ent      : Entity_Id;
388
389       begin
390          if Nkind (Decl) = N_Subprogram_Declaration
391            and then Present (Corresponding_Body (Decl))
392          then
393             Body_Ent := Corresponding_Body (Decl);
394          else
395             return False;
396          end if;
397
398          --  If subprogram is marked Inline_Always, inlining is mandatory
399
400          if Has_Pragma_Inline_Always (Subp) then
401             return False;
402          end if;
403
404          if Present
405           (Exception_Handlers
406             (Handled_Statement_Sequence
407               (Unit_Declaration_Node (Corresponding_Body (Decl)))))
408          then
409             return True;
410          end if;
411
412          Ent := First_Entity (Body_Ent);
413          while Present (Ent) loop
414             if Is_Subprogram (Ent)
415               and then Is_Generic_Instance (Ent)
416             then
417                return True;
418             end if;
419
420             Next_Entity (Ent);
421          end loop;
422
423          return False;
424       end Back_End_Cannot_Inline;
425
426    --  Start of processing for Add_Inlined_Subprogram
427
428    begin
429       --  Insert the current subprogram in the list of inlined subprograms, if
430       --  it can actually be inlined by the back-end, and if its unit is known
431       --  to be inlined, or is an instance whose body will be analyzed anyway.
432
433       if (Is_Inlined (Pack) or else Is_Generic_Instance (Pack))
434         and then not Scope_In_Main_Unit (E)
435         and then Is_Inlined (E)
436         and then not Is_Nested (E)
437         and then not Has_Initialized_Type (E)
438       then
439          if Back_End_Cannot_Inline (E) then
440             Set_Is_Inlined (E, False);
441
442          else
443             if No (Last_Inlined) then
444                Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
445             else
446                Set_Next_Inlined_Subprogram (Last_Inlined, E);
447             end if;
448
449             Last_Inlined := E;
450          end if;
451       end if;
452
453       Inlined.Table (Index).Listed := True;
454
455       --  Now add to the list those callers of the current subprogram that
456       --  are themselves called. They may appear on the graph as callers
457       --  of the current one, even if they are themselves not called, and
458       --  there is no point in including them in the list for the backend.
459       --  Furthermore, they might not even be public, in which case the
460       --  back-end cannot handle them at all.
461
462       Succ := Inlined.Table (Index).First_Succ;
463       while Succ /= No_Succ loop
464          Subp := Successors.Table (Succ).Subp;
465          Inlined.Table (Subp).Count := Inlined.Table (Subp).Count - 1;
466
467          if Inlined.Table (Subp).Count = 0
468            and then Is_Called (Inlined.Table (Subp).Name)
469          then
470             Add_Inlined_Subprogram (Subp);
471          end if;
472
473          Succ := Successors.Table (Succ).Next;
474       end loop;
475    end Add_Inlined_Subprogram;
476
477    ------------------------
478    -- Add_Scope_To_Clean --
479    ------------------------
480
481    procedure Add_Scope_To_Clean (Inst : Entity_Id) is
482       Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
483       Elmt : Elmt_Id;
484
485    begin
486       --  If the instance appears in a library-level package declaration,
487       --  all finalization is global, and nothing needs doing here.
488
489       if Scop = Standard_Standard then
490          return;
491       end if;
492
493       --  If the instance appears within a generic subprogram there is nothing
494       --  to finalize either.
495
496       declare
497          S : Entity_Id;
498
499       begin
500          S := Scope (Inst);
501          while Present (S) and then S /= Standard_Standard loop
502             if Is_Generic_Subprogram (S) then
503                return;
504             end if;
505
506             S := Scope (S);
507          end loop;
508       end;
509
510       Elmt := First_Elmt (To_Clean);
511       while Present (Elmt) loop
512          if Node (Elmt) = Scop then
513             return;
514          end if;
515
516          Elmt := Next_Elmt (Elmt);
517       end loop;
518
519       Append_Elmt (Scop, To_Clean);
520    end Add_Scope_To_Clean;
521
522    --------------
523    -- Add_Subp --
524    --------------
525
526    function Add_Subp (E : Entity_Id) return Subp_Index is
527       Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
528       J     : Subp_Index;
529
530       procedure New_Entry;
531       --  Initialize entry in Inlined table
532
533       procedure New_Entry is
534       begin
535          Inlined.Increment_Last;
536          Inlined.Table (Inlined.Last).Name        := E;
537          Inlined.Table (Inlined.Last).First_Succ  := No_Succ;
538          Inlined.Table (Inlined.Last).Count       := 0;
539          Inlined.Table (Inlined.Last).Listed      := False;
540          Inlined.Table (Inlined.Last).Main_Call   := False;
541          Inlined.Table (Inlined.Last).Next        := No_Subp;
542          Inlined.Table (Inlined.Last).Next_Nopred := No_Subp;
543       end New_Entry;
544
545    --  Start of processing for Add_Subp
546
547    begin
548       if Hash_Headers (Index) = No_Subp then
549          New_Entry;
550          Hash_Headers (Index) := Inlined.Last;
551          return Inlined.Last;
552
553       else
554          J := Hash_Headers (Index);
555          while J /= No_Subp loop
556             if Inlined.Table (J).Name = E then
557                return J;
558             else
559                Index := J;
560                J := Inlined.Table (J).Next;
561             end if;
562          end loop;
563
564          --  On exit, subprogram was not found. Enter in table. Index is
565          --  the current last entry on the hash chain.
566
567          New_Entry;
568          Inlined.Table (Index).Next := Inlined.Last;
569          return Inlined.Last;
570       end if;
571    end Add_Subp;
572
573    ----------------------------
574    -- Analyze_Inlined_Bodies --
575    ----------------------------
576
577    procedure Analyze_Inlined_Bodies is
578       Comp_Unit : Node_Id;
579       J         : Int;
580       Pack      : Entity_Id;
581       S         : Succ_Index;
582
583       function Is_Ancestor_Of_Main
584         (U_Name : Entity_Id;
585          Nam    : Node_Id) return Boolean;
586       --  Determine whether the unit whose body is loaded is an ancestor of
587       --  the main unit, and has a with_clause on it. The body is not
588       --  analyzed yet, so the check is purely lexical: the name of the with
589       --  clause is a selected component, and names of ancestors must match.
590
591       -------------------------
592       -- Is_Ancestor_Of_Main --
593       -------------------------
594
595       function Is_Ancestor_Of_Main
596         (U_Name : Entity_Id;
597          Nam    : Node_Id) return Boolean
598       is
599          Pref : Node_Id;
600
601       begin
602          if Nkind (Nam) /= N_Selected_Component then
603             return False;
604
605          else
606             if Chars (Selector_Name (Nam)) /=
607                Chars (Cunit_Entity (Main_Unit))
608             then
609                return False;
610             end if;
611
612             Pref := Prefix (Nam);
613             if Nkind (Pref) = N_Identifier then
614
615                --  Par is an ancestor of Par.Child.
616
617                return Chars (Pref) = Chars (U_Name);
618
619             elsif Nkind (Pref) = N_Selected_Component
620               and then Chars (Selector_Name (Pref)) = Chars (U_Name)
621             then
622                --  Par.Child is an ancestor of Par.Child.Grand.
623
624                return True;   --  should check that ancestor match
625
626             else
627                --  A is an ancestor of A.B.C if it is an ancestor of A.B
628
629                return Is_Ancestor_Of_Main (U_Name, Pref);
630             end if;
631          end if;
632       end Is_Ancestor_Of_Main;
633
634    --  Start of processing for  Analyze_Inlined_Bodies
635
636    begin
637       Analyzing_Inlined_Bodies := False;
638
639       if Serious_Errors_Detected = 0 then
640          Push_Scope (Standard_Standard);
641
642          J := 0;
643          while J <= Inlined_Bodies.Last
644            and then Serious_Errors_Detected = 0
645          loop
646             Pack := Inlined_Bodies.Table (J);
647             while Present (Pack)
648               and then Scope (Pack) /= Standard_Standard
649               and then not Is_Child_Unit (Pack)
650             loop
651                Pack := Scope (Pack);
652             end loop;
653
654             Comp_Unit := Parent (Pack);
655             while Present (Comp_Unit)
656               and then Nkind (Comp_Unit) /= N_Compilation_Unit
657             loop
658                Comp_Unit := Parent (Comp_Unit);
659             end loop;
660
661             --  Load the body, unless it the main unit, or is an instance whose
662             --  body has already been analyzed.
663
664             if Present (Comp_Unit)
665               and then Comp_Unit /= Cunit (Main_Unit)
666               and then Body_Required (Comp_Unit)
667               and then (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
668                          or else No (Corresponding_Body (Unit (Comp_Unit))))
669             then
670                declare
671                   Bname : constant Unit_Name_Type :=
672                             Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
673
674                   OK : Boolean;
675
676                begin
677                   if not Is_Loaded (Bname) then
678                      Style_Check := False;
679                      Load_Needed_Body (Comp_Unit, OK, Do_Analyze => False);
680
681                      if not OK then
682
683                         --  Warn that a body was not available for inlining
684                         --  by the back-end.
685
686                         Error_Msg_Unit_1 := Bname;
687                         Error_Msg_N
688                           ("one or more inlined subprograms accessed in $!?",
689                            Comp_Unit);
690                         Error_Msg_File_1 :=
691                           Get_File_Name (Bname, Subunit => False);
692                         Error_Msg_N ("\but file{ was not found!?", Comp_Unit);
693
694                      else
695                         --  If the package to be inlined is an ancestor unit of
696                         --  the main unit, and it has a semantic dependence on
697                         --  it, the inlining cannot take place to prevent an
698                         --  elaboration circularity. The desired body is not
699                         --  analyzed yet, to prevent the completion of Taft
700                         --  amendment types that would lead to elaboration
701                         --  circularities in gigi.
702
703                         declare
704                            U_Id      : constant Entity_Id :=
705                                          Defining_Entity (Unit (Comp_Unit));
706                            Body_Unit : constant Node_Id :=
707                                          Library_Unit (Comp_Unit);
708                            Item      : Node_Id;
709
710                         begin
711                            Item := First (Context_Items (Body_Unit));
712                            while Present (Item) loop
713                               if Nkind (Item) = N_With_Clause
714                                 and then
715                                   Is_Ancestor_Of_Main (U_Id, Name (Item))
716                               then
717                                  Set_Is_Inlined (U_Id, False);
718                                  exit;
719                               end if;
720
721                               Next (Item);
722                            end loop;
723
724                            --  If no suspicious with_clauses, analyze the body.
725
726                            if Is_Inlined (U_Id) then
727                               Semantics (Body_Unit);
728                            end if;
729                         end;
730                      end if;
731                   end if;
732                end;
733             end if;
734
735             J := J + 1;
736          end loop;
737
738          --  The analysis of required bodies may have produced additional
739          --  generic instantiations. To obtain further inlining, we perform
740          --  another round of generic body instantiations. Establishing a
741          --  fully recursive loop between inlining and generic instantiations
742          --  is unlikely to yield more than this one additional pass.
743
744          Instantiate_Bodies;
745
746          --  The list of inlined subprograms is an overestimate, because it
747          --  includes inlined functions called from functions that are compiled
748          --  as part of an inlined package, but are not themselves called. An
749          --  accurate computation of just those subprograms that are needed
750          --  requires that we perform a transitive closure over the call graph,
751          --  starting from calls in the main program. Here we do one step of
752          --  the inverse transitive closure, and reset the Is_Called flag on
753          --  subprograms all of whose callers are not.
754
755          for Index in Inlined.First .. Inlined.Last loop
756             S := Inlined.Table (Index).First_Succ;
757
758             if S /= No_Succ
759               and then not Inlined.Table (Index).Main_Call
760             then
761                Set_Is_Called (Inlined.Table (Index).Name, False);
762
763                while S /= No_Succ loop
764                   if Is_Called
765                     (Inlined.Table (Successors.Table (S).Subp).Name)
766                    or else Inlined.Table (Successors.Table (S).Subp).Main_Call
767                   then
768                      Set_Is_Called (Inlined.Table (Index).Name);
769                      exit;
770                   end if;
771
772                   S := Successors.Table (S).Next;
773                end loop;
774             end if;
775          end loop;
776
777          --  Now that the units are compiled, chain the subprograms within
778          --  that are called and inlined. Produce list of inlined subprograms
779          --  sorted in  topological order. Start with all subprograms that
780          --  have no prerequisites, i.e. inlined subprograms that do not call
781          --  other inlined subprograms.
782
783          for Index in Inlined.First .. Inlined.Last loop
784
785             if Is_Called (Inlined.Table (Index).Name)
786               and then Inlined.Table (Index).Count = 0
787               and then not Inlined.Table (Index).Listed
788             then
789                Add_Inlined_Subprogram (Index);
790             end if;
791          end loop;
792
793          --  Because Add_Inlined_Subprogram treats recursively nodes that have
794          --  no prerequisites left, at the end of the loop all subprograms
795          --  must have been listed. If there are any unlisted subprograms
796          --  left, there must be some recursive chains that cannot be inlined.
797
798          for Index in Inlined.First .. Inlined.Last loop
799             if Is_Called (Inlined.Table (Index).Name)
800               and then Inlined.Table (Index).Count /= 0
801               and then not Is_Predefined_File_Name
802                 (Unit_File_Name
803                   (Get_Source_Unit (Inlined.Table (Index).Name)))
804             then
805                Error_Msg_N
806                  ("& cannot be inlined?", Inlined.Table (Index).Name);
807
808                --  A warning on the first one might be sufficient ???
809             end if;
810          end loop;
811
812          Pop_Scope;
813       end if;
814    end Analyze_Inlined_Bodies;
815
816    -----------------------------
817    -- Check_Body_For_Inlining --
818    -----------------------------
819
820    procedure Check_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
821       Bname : Unit_Name_Type;
822       E     : Entity_Id;
823       OK    : Boolean;
824
825    begin
826       if Is_Compilation_Unit (P)
827         and then not Is_Generic_Instance (P)
828       then
829          Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
830
831          E := First_Entity (P);
832          while Present (E) loop
833             if Has_Pragma_Inline_Always (E)
834               or else (Front_End_Inlining and then Has_Pragma_Inline (E))
835             then
836                if not Is_Loaded (Bname) then
837                   Load_Needed_Body (N, OK);
838
839                   if OK then
840
841                      --  Check we are not trying to inline a parent whose body
842                      --  depends on a child, when we are compiling the body of
843                      --  the child. Otherwise we have a potential elaboration
844                      --  circularity with inlined subprograms and with
845                      --  Taft-Amendment types.
846
847                      declare
848                         Comp        : Node_Id;      --  Body just compiled
849                         Child_Spec  : Entity_Id;    --  Spec of main unit
850                         Ent         : Entity_Id;    --  For iteration
851                         With_Clause : Node_Id;      --  Context of body.
852
853                      begin
854                         if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
855                           and then Present (Body_Entity (P))
856                         then
857                            Child_Spec :=
858                              Defining_Entity
859                                ((Unit (Library_Unit (Cunit (Main_Unit)))));
860
861                            Comp :=
862                              Parent (Unit_Declaration_Node (Body_Entity (P)));
863
864                            --  Check whether the context of the body just
865                            --  compiled includes a child of itself, and that
866                            --  child is the spec of the main compilation.
867
868                            With_Clause := First (Context_Items (Comp));
869                            while Present (With_Clause) loop
870                               if Nkind (With_Clause) = N_With_Clause
871                                 and then
872                                   Scope (Entity (Name (With_Clause))) = P
873                                 and then
874                                   Entity (Name (With_Clause)) = Child_Spec
875                               then
876                                  Error_Msg_Node_2 := Child_Spec;
877                                  Error_Msg_NE
878                                    ("body of & depends on child unit&?",
879                                       With_Clause, P);
880                                  Error_Msg_N
881                                    ("\subprograms in body cannot be inlined?",
882                                       With_Clause);
883
884                                  --  Disable further inlining from this unit,
885                                  --  and keep Taft-amendment types incomplete.
886
887                                  Ent := First_Entity (P);
888                                  while Present (Ent) loop
889                                     if Is_Type (Ent)
890                                        and then Has_Completion_In_Body (Ent)
891                                     then
892                                        Set_Full_View (Ent, Empty);
893
894                                     elsif Is_Subprogram (Ent) then
895                                        Set_Is_Inlined (Ent, False);
896                                     end if;
897
898                                     Next_Entity (Ent);
899                                  end loop;
900
901                                  return;
902                               end if;
903
904                               Next (With_Clause);
905                            end loop;
906                         end if;
907                      end;
908
909                   elsif Ineffective_Inline_Warnings then
910                      Error_Msg_Unit_1 := Bname;
911                      Error_Msg_N
912                        ("unable to inline subprograms defined in $?", P);
913                      Error_Msg_N ("\body not found?", P);
914                      return;
915                   end if;
916                end if;
917
918                return;
919             end if;
920
921             Next_Entity (E);
922          end loop;
923       end if;
924    end Check_Body_For_Inlining;
925
926    --------------------
927    -- Cleanup_Scopes --
928    --------------------
929
930    procedure Cleanup_Scopes is
931       Elmt : Elmt_Id;
932       Decl : Node_Id;
933       Scop : Entity_Id;
934
935    begin
936       Elmt := First_Elmt (To_Clean);
937       while Present (Elmt) loop
938          Scop := Node (Elmt);
939
940          if Ekind (Scop) = E_Entry then
941             Scop := Protected_Body_Subprogram (Scop);
942
943          elsif Is_Subprogram (Scop)
944            and then Is_Protected_Type (Scope (Scop))
945            and then Present (Protected_Body_Subprogram (Scop))
946          then
947             --  If a protected operation contains an instance, its
948             --  cleanup operations have been delayed, and the subprogram
949             --  has been rewritten in the expansion of the enclosing
950             --  protected body. It is the corresponding subprogram that
951             --  may require the cleanup operations, so propagate the
952             --  information that triggers cleanup activity.
953
954             Set_Uses_Sec_Stack
955               (Protected_Body_Subprogram (Scop),
956                 Uses_Sec_Stack (Scop));
957             Set_Finalization_Chain_Entity
958               (Protected_Body_Subprogram (Scop),
959                 Finalization_Chain_Entity (Scop));
960             Scop := Protected_Body_Subprogram (Scop);
961          end if;
962
963          if Ekind (Scop) = E_Block then
964             Decl := Parent (Block_Node (Scop));
965
966          else
967             Decl := Unit_Declaration_Node (Scop);
968
969             if Nkind (Decl) = N_Subprogram_Declaration
970               or else Nkind (Decl) = N_Task_Type_Declaration
971               or else Nkind (Decl) = N_Subprogram_Body_Stub
972             then
973                Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
974             end if;
975          end if;
976
977          Push_Scope (Scop);
978          Expand_Cleanup_Actions (Decl);
979          End_Scope;
980
981          Elmt := Next_Elmt (Elmt);
982       end loop;
983    end Cleanup_Scopes;
984
985    --------------------------
986    -- Has_Initialized_Type --
987    --------------------------
988
989    function Has_Initialized_Type (E : Entity_Id) return Boolean is
990       E_Body : constant Node_Id := Get_Subprogram_Body (E);
991       Decl   : Node_Id;
992
993    begin
994       if No (E_Body) then        --  imported subprogram
995          return False;
996
997       else
998          Decl := First (Declarations (E_Body));
999          while Present (Decl) loop
1000
1001             if Nkind (Decl) = N_Full_Type_Declaration
1002               and then Present (Init_Proc (Defining_Identifier (Decl)))
1003             then
1004                return True;
1005             end if;
1006
1007             Next (Decl);
1008          end loop;
1009       end if;
1010
1011       return False;
1012    end Has_Initialized_Type;
1013
1014    ----------------
1015    -- Initialize --
1016    ----------------
1017
1018    procedure Initialize is
1019    begin
1020       Analyzing_Inlined_Bodies := False;
1021       Pending_Descriptor.Init;
1022       Pending_Instantiations.Init;
1023       Inlined_Bodies.Init;
1024       Successors.Init;
1025       Inlined.Init;
1026
1027       for J in Hash_Headers'Range loop
1028          Hash_Headers (J) := No_Subp;
1029       end loop;
1030    end Initialize;
1031
1032    ------------------------
1033    -- Instantiate_Bodies --
1034    ------------------------
1035
1036    --  Generic bodies contain all the non-local references, so an
1037    --  instantiation does not need any more context than Standard
1038    --  itself, even if the instantiation appears in an inner scope.
1039    --  Generic associations have verified that the contract model is
1040    --  satisfied, so that any error that may occur in the analysis of
1041    --  the body is an internal error.
1042
1043    procedure Instantiate_Bodies is
1044       J    : Int;
1045       Info : Pending_Body_Info;
1046
1047    begin
1048       if Serious_Errors_Detected = 0 then
1049
1050          Expander_Active := (Operating_Mode = Opt.Generate_Code);
1051          Push_Scope (Standard_Standard);
1052          To_Clean := New_Elmt_List;
1053
1054          if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
1055             Start_Generic;
1056          end if;
1057
1058          --  A body instantiation may generate additional instantiations, so
1059          --  the following loop must scan to the end of a possibly expanding
1060          --  set (that's why we can't simply use a FOR loop here).
1061
1062          J := 0;
1063          while J <= Pending_Instantiations.Last
1064            and then Serious_Errors_Detected = 0
1065          loop
1066             Info := Pending_Instantiations.Table (J);
1067
1068             --  If the instantiation node is absent, it has been removed
1069             --  as part of unreachable code.
1070
1071             if No (Info.Inst_Node) then
1072                null;
1073
1074             elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
1075                Instantiate_Package_Body (Info);
1076                Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
1077
1078             else
1079                Instantiate_Subprogram_Body (Info);
1080             end if;
1081
1082             J := J + 1;
1083          end loop;
1084
1085          --  Reset the table of instantiations. Additional instantiations
1086          --  may be added through inlining, when additional bodies are
1087          --  analyzed.
1088
1089          Pending_Instantiations.Init;
1090
1091          --  We can now complete the cleanup actions of scopes that contain
1092          --  pending instantiations (skipped for generic units, since we
1093          --  never need any cleanups in generic units).
1094          --  pending instantiations.
1095
1096          if Expander_Active
1097            and then not Is_Generic_Unit (Main_Unit_Entity)
1098          then
1099             Cleanup_Scopes;
1100          elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
1101             End_Generic;
1102          end if;
1103
1104          Pop_Scope;
1105       end if;
1106    end Instantiate_Bodies;
1107
1108    ---------------
1109    -- Is_Nested --
1110    ---------------
1111
1112    function Is_Nested (E : Entity_Id) return Boolean is
1113       Scop : Entity_Id;
1114
1115    begin
1116       Scop := Scope (E);
1117       while Scop /= Standard_Standard loop
1118          if Ekind (Scop) in Subprogram_Kind then
1119             return True;
1120
1121          elsif Ekind (Scop) = E_Task_Type
1122            or else Ekind (Scop) = E_Entry
1123            or else Ekind (Scop) = E_Entry_Family then
1124             return True;
1125          end if;
1126
1127          Scop := Scope (Scop);
1128       end loop;
1129
1130       return False;
1131    end Is_Nested;
1132
1133    ----------
1134    -- Lock --
1135    ----------
1136
1137    procedure Lock is
1138    begin
1139       Pending_Instantiations.Locked := True;
1140       Inlined_Bodies.Locked := True;
1141       Successors.Locked := True;
1142       Inlined.Locked := True;
1143       Pending_Instantiations.Release;
1144       Inlined_Bodies.Release;
1145       Successors.Release;
1146       Inlined.Release;
1147    end Lock;
1148
1149    --------------------------
1150    -- Remove_Dead_Instance --
1151    --------------------------
1152
1153    procedure Remove_Dead_Instance (N : Node_Id) is
1154       J : Int;
1155
1156    begin
1157       J := 0;
1158       while J <= Pending_Instantiations.Last loop
1159          if Pending_Instantiations.Table (J).Inst_Node = N then
1160             Pending_Instantiations.Table (J).Inst_Node := Empty;
1161             return;
1162          end if;
1163
1164          J := J + 1;
1165       end loop;
1166    end Remove_Dead_Instance;
1167
1168    --------------------------
1169    -- Get_Code_Unit_Entity --
1170    --------------------------
1171
1172    function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
1173    begin
1174       return Cunit_Entity (Get_Code_Unit (E));
1175    end Get_Code_Unit_Entity;
1176
1177    ------------------------
1178    -- Scope_In_Main_Unit --
1179    ------------------------
1180
1181    function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean is
1182       Comp : constant Node_Id := Cunit (Get_Code_Unit (Scop));
1183
1184    begin
1185       --  Check whether the scope of the subprogram to inline is within the
1186       --  main unit or within its spec. In either case there are no additional
1187       --  bodies to process. If the subprogram appears in a parent of the
1188       --  current unit, the check on whether inlining is possible is done in
1189       --  Analyze_Inlined_Bodies.
1190
1191       return
1192         Comp = Cunit (Main_Unit)
1193           or else Comp = Library_Unit (Cunit (Main_Unit));
1194    end Scope_In_Main_Unit;
1195
1196 end Inline;