1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch7; use Exp_Ch7;
32 with Exp_Ch11; use Exp_Ch11;
33 with Exp_Tss; use Exp_Tss;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
37 with Nlists; use Nlists;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Ch10; use Sem_Ch10;
41 with Sem_Ch12; use Sem_Ch12;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Uname; use Uname;
48 package body Inline is
54 -- Inlined functions are actually placed in line by the backend if the
55 -- corresponding bodies are available (i.e. compiled). Whenever we find
56 -- a call to an inlined subprogram, we add the name of the enclosing
57 -- compilation unit to a worklist. After all compilation, and after
58 -- expansion of generic bodies, we traverse the list of pending bodies
59 -- and compile them as well.
61 package Inlined_Bodies is new Table.Table (
62 Table_Component_Type => Entity_Id,
63 Table_Index_Type => Int,
65 Table_Initial => Alloc.Inlined_Bodies_Initial,
66 Table_Increment => Alloc.Inlined_Bodies_Increment,
67 Table_Name => "Inlined_Bodies");
69 -----------------------
70 -- Inline Processing --
71 -----------------------
73 -- For each call to an inlined subprogram, we make entries in a table
74 -- that stores caller and callee, and indicates a prerequisite from
75 -- one to the other. We also record the compilation unit that contains
76 -- the callee. After analyzing the bodies of all such compilation units,
77 -- we produce a list of subprograms in topological order, for use by the
78 -- back-end. If P2 is a prerequisite of P1, then P1 calls P2, and for
79 -- proper inlining the back-end must analyze the body of P2 before that of
80 -- P1. The code below guarantees that the transitive closure of inlined
81 -- subprograms called from the main compilation unit is made available to
82 -- the code generator.
84 Last_Inlined : Entity_Id := Empty;
86 -- For each entry in the table we keep a list of successors in topological
87 -- order, i.e. callers of the current subprogram.
89 type Subp_Index is new Nat;
90 No_Subp : constant Subp_Index := 0;
92 -- The subprogram entities are hashed into the Inlined table.
94 Num_Hash_Headers : constant := 512;
96 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
99 type Succ_Index is new Nat;
100 No_Succ : constant Succ_Index := 0;
102 type Succ_Info is record
107 -- The following table stores list elements for the successor lists.
108 -- These lists cannot be chained directly through entries in the Inlined
109 -- table, because a given subprogram can appear in several such lists.
111 package Successors is new Table.Table (
112 Table_Component_Type => Succ_Info,
113 Table_Index_Type => Succ_Index,
114 Table_Low_Bound => 1,
115 Table_Initial => Alloc.Successors_Initial,
116 Table_Increment => Alloc.Successors_Increment,
117 Table_Name => "Successors");
119 type Subp_Info is record
120 Name : Entity_Id := Empty;
121 First_Succ : Succ_Index := No_Succ;
122 Count : Integer := 0;
123 Listed : Boolean := False;
124 Main_Call : Boolean := False;
125 Next : Subp_Index := No_Subp;
126 Next_Nopred : Subp_Index := No_Subp;
129 package Inlined is new Table.Table (
130 Table_Component_Type => Subp_Info,
131 Table_Index_Type => Subp_Index,
132 Table_Low_Bound => 1,
133 Table_Initial => Alloc.Inlined_Initial,
134 Table_Increment => Alloc.Inlined_Increment,
135 Table_Name => "Inlined");
137 -----------------------
138 -- Local Subprograms --
139 -----------------------
141 function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean;
142 -- Return True if Scop is in the main unit or its spec, or in a
143 -- parent of the main unit if it is a child unit.
145 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
146 -- Make two entries in Inlined table, for an inlined subprogram being
147 -- called, and for the inlined subprogram that contains the call. If
148 -- the call is in the main compilation unit, Caller is Empty.
150 function Add_Subp (E : Entity_Id) return Subp_Index;
151 -- Make entry in Inlined table for subprogram E, or return table index
152 -- that already holds E.
154 function Has_Initialized_Type (E : Entity_Id) return Boolean;
155 -- If a candidate for inlining contains type declarations for types with
156 -- non-trivial initialization procedures, they are not worth inlining.
158 function Is_Nested (E : Entity_Id) return Boolean;
159 -- If the function is nested inside some other function, it will
160 -- always be compiled if that function is, so don't add it to the
161 -- inline list. We cannot compile a nested function outside the
162 -- scope of the containing function anyway. This is also the case if
163 -- the function is defined in a task body or within an entry (for
164 -- example, an initialization procedure).
166 procedure Add_Inlined_Subprogram (Index : Subp_Index);
167 -- Add subprogram to Inlined List once all of its predecessors have been
168 -- placed on the list. Decrement the count of all its successors, and
169 -- add them to list (recursively) if count drops to zero.
171 ------------------------------
172 -- Deferred Cleanup Actions --
173 ------------------------------
175 -- The cleanup actions for scopes that contain instantiations is delayed
176 -- until after expansion of those instantiations, because they may
177 -- contain finalizable objects or tasks that affect the cleanup code.
178 -- A scope that contains instantiations only needs to be finalized once,
179 -- even if it contains more than one instance. We keep a list of scopes
180 -- that must still be finalized, and call cleanup_actions after all the
181 -- instantiations have been completed.
185 procedure Add_Scope_To_Clean (Inst : Entity_Id);
186 -- Build set of scopes on which cleanup actions must be performed.
188 procedure Cleanup_Scopes;
189 -- Complete cleanup actions on scopes that need it.
195 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
196 P1 : constant Subp_Index := Add_Subp (Called);
201 if Present (Caller) then
202 P2 := Add_Subp (Caller);
204 -- Add P2 to the list of successors of P1, if not already there.
205 -- Note that P2 may contain more than one call to P1, and only
206 -- one needs to be recorded.
208 J := Inlined.Table (P1).First_Succ;
210 while J /= No_Succ loop
212 if Successors.Table (J).Subp = P2 then
216 J := Successors.Table (J).Next;
219 -- On exit, make a successor entry for P2.
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;
227 Inlined.Table (P2).Count := Inlined.Table (P2).Count + 1;
230 Inlined.Table (P1).Main_Call := True;
234 ----------------------
235 -- Add_Inlined_Body --
236 ----------------------
238 procedure Add_Inlined_Body (E : Entity_Id) is
241 function Must_Inline return Boolean;
242 -- Inlining is only done if the call statement N is in the main unit,
243 -- or within the body of another inlined subprogram.
249 function Must_Inline return Boolean is
250 Scop : Entity_Id := Current_Scope;
254 -- Check if call is in main unit
256 while Scope (Scop) /= Standard_Standard
257 and then not Is_Child_Unit (Scop)
259 Scop := Scope (Scop);
262 Comp := Parent (Scop);
264 while Nkind (Comp) /= N_Compilation_Unit loop
265 Comp := Parent (Comp);
268 if Comp = Cunit (Main_Unit)
269 or else Comp = Library_Unit (Cunit (Main_Unit))
275 -- Call is not in main unit. See if it's in some inlined
278 Scop := Current_Scope;
279 while Scope (Scop) /= Standard_Standard
280 and then not Is_Child_Unit (Scop)
282 if Is_Overloadable (Scop)
283 and then Is_Inlined (Scop)
289 Scop := Scope (Scop);
296 -- Start of processing for Add_Inlined_Body
299 -- Find unit containing E, and add to list of inlined bodies if needed.
300 -- If the body is already present, no need to load any other unit. This
301 -- is the case for an initialization procedure, which appears in the
302 -- package declaration that contains the type. It is also the case if
303 -- the body has already been analyzed. Finally, if the unit enclosing
304 -- E is an instance, the instance body will be analyzed in any case,
305 -- and there is no need to add the enclosing unit (whose body might not
308 -- Library-level functions must be handled specially, because there is
309 -- no enclosing package to retrieve. In this case, it is the body of
310 -- the function that will have to be loaded.
312 if not Is_Abstract (E) and then not Is_Nested (E)
313 and then Convention (E) /= Convention_Protected
318 and then Ekind (Pack) = E_Package
322 if Pack = Standard_Standard then
324 -- Library-level inlined function. Add function iself to
325 -- list of needed units.
327 Inlined_Bodies.Increment_Last;
328 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
330 elsif Is_Generic_Instance (Pack) then
333 elsif not Is_Inlined (Pack)
334 and then not Has_Completion (E)
335 and then not Scope_In_Main_Unit (Pack)
337 Set_Is_Inlined (Pack);
338 Inlined_Bodies.Increment_Last;
339 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
343 end Add_Inlined_Body;
345 ----------------------------
346 -- Add_Inlined_Subprogram --
347 ----------------------------
349 procedure Add_Inlined_Subprogram (Index : Subp_Index) is
350 E : constant Entity_Id := Inlined.Table (Index).Name;
354 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean;
355 -- There are various conditions under which back-end inlining cannot
358 -- a) If a body has handlers, it must not be inlined, because this
359 -- may violate program semantics, and because in zero-cost exception
360 -- mode it will lead to undefined symbols at link time.
362 -- b) If a body contains inlined function instances, it cannot be
363 -- inlined under ZCX because the numerix suffix generated by gigi
364 -- will be different in the body and the place of the inlined call.
366 -- This procedure must be carefully coordinated with the back end
368 ----------------------------
369 -- Back_End_Cannot_Inline --
370 ----------------------------
372 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean is
373 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
374 Body_Ent : Entity_Id;
378 if Nkind (Decl) = N_Subprogram_Declaration
379 and then Present (Corresponding_Body (Decl))
381 Body_Ent := Corresponding_Body (Decl);
386 -- If subprogram is marked Inline_Always, inlining is mandatory
388 if Is_Always_Inlined (Subp) then
394 (Handled_Statement_Sequence
395 (Unit_Declaration_Node (Corresponding_Body (Decl)))))
400 Ent := First_Entity (Body_Ent);
402 while Present (Ent) loop
403 if Is_Subprogram (Ent)
404 and then Is_Generic_Instance (Ent)
412 end Back_End_Cannot_Inline;
414 -- Start of processing for Add_Inlined_Subprogram
417 -- Insert the current subprogram in the list of inlined subprograms,
418 -- if it can actually be inlined by the back-end.
420 if not Scope_In_Main_Unit (E)
421 and then Is_Inlined (E)
422 and then not Is_Nested (E)
423 and then not Has_Initialized_Type (E)
425 if Back_End_Cannot_Inline (E) then
426 Set_Is_Inlined (E, False);
429 if No (Last_Inlined) then
430 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
432 Set_Next_Inlined_Subprogram (Last_Inlined, E);
439 Inlined.Table (Index).Listed := True;
440 Succ := Inlined.Table (Index).First_Succ;
442 while Succ /= No_Succ loop
443 Subp := Successors.Table (Succ).Subp;
444 Inlined.Table (Subp).Count := Inlined.Table (Subp).Count - 1;
446 if Inlined.Table (Subp).Count = 0 then
447 Add_Inlined_Subprogram (Subp);
450 Succ := Successors.Table (Succ).Next;
452 end Add_Inlined_Subprogram;
454 ------------------------
455 -- Add_Scope_To_Clean --
456 ------------------------
458 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
459 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
463 -- If the instance appears in a library-level package declaration,
464 -- all finalization is global, and nothing needs doing here.
466 if Scop = Standard_Standard then
470 -- If the instance appears within a generic subprogram there is nothing
471 -- to finalize either.
477 while Present (S) and then S /= Standard_Standard loop
478 if Is_Generic_Subprogram (S) then
486 Elmt := First_Elmt (To_Clean);
488 while Present (Elmt) loop
490 if Node (Elmt) = Scop then
494 Elmt := Next_Elmt (Elmt);
497 Append_Elmt (Scop, To_Clean);
498 end Add_Scope_To_Clean;
504 function Add_Subp (E : Entity_Id) return Subp_Index is
505 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
509 -- Initialize entry in Inlined table.
511 procedure New_Entry is
513 Inlined.Increment_Last;
514 Inlined.Table (Inlined.Last).Name := E;
515 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
516 Inlined.Table (Inlined.Last).Count := 0;
517 Inlined.Table (Inlined.Last).Listed := False;
518 Inlined.Table (Inlined.Last).Main_Call := False;
519 Inlined.Table (Inlined.Last).Next := No_Subp;
520 Inlined.Table (Inlined.Last).Next_Nopred := No_Subp;
523 -- Start of processing for Add_Subp
526 if Hash_Headers (Index) = No_Subp then
528 Hash_Headers (Index) := Inlined.Last;
532 J := Hash_Headers (Index);
534 while J /= No_Subp loop
536 if Inlined.Table (J).Name = E then
540 J := Inlined.Table (J).Next;
544 -- On exit, subprogram was not found. Enter in table. Index is
545 -- the current last entry on the hash chain.
548 Inlined.Table (Index).Next := Inlined.Last;
553 ----------------------------
554 -- Analyze_Inlined_Bodies --
555 ----------------------------
557 procedure Analyze_Inlined_Bodies is
564 Analyzing_Inlined_Bodies := False;
566 if Serious_Errors_Detected = 0 then
567 New_Scope (Standard_Standard);
570 while J <= Inlined_Bodies.Last
571 and then Serious_Errors_Detected = 0
573 Pack := Inlined_Bodies.Table (J);
576 and then Scope (Pack) /= Standard_Standard
577 and then not Is_Child_Unit (Pack)
579 Pack := Scope (Pack);
582 Comp_Unit := Parent (Pack);
584 while Present (Comp_Unit)
585 and then Nkind (Comp_Unit) /= N_Compilation_Unit
587 Comp_Unit := Parent (Comp_Unit);
590 -- Load the body, unless it the main unit, or is an instance
591 -- whose body has already been analyzed.
593 if Present (Comp_Unit)
594 and then Comp_Unit /= Cunit (Main_Unit)
595 and then Body_Required (Comp_Unit)
596 and then (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
597 or else No (Corresponding_Body (Unit (Comp_Unit))))
600 Bname : constant Unit_Name_Type :=
601 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
606 if not Is_Loaded (Bname) then
607 Load_Needed_Body (Comp_Unit, OK);
610 Error_Msg_Unit_1 := Bname;
612 ("one or more inlined subprograms accessed in $!",
615 Get_File_Name (Bname, Subunit => False);
616 Error_Msg_N ("\but file{ was not found!", Comp_Unit);
617 raise Unrecoverable_Error;
626 -- The analysis of required bodies may have produced additional
627 -- generic instantiations. To obtain further inlining, we perform
628 -- another round of generic body instantiations. Establishing a
629 -- fully recursive loop between inlining and generic instantiations
630 -- is unlikely to yield more than this one additional pass.
634 -- The list of inlined subprograms is an overestimate, because
635 -- it includes inlined functions called from functions that are
636 -- compiled as part of an inlined package, but are not themselves
637 -- called. An accurate computation of just those subprograms that
638 -- are needed requires that we perform a transitive closure over
639 -- the call graph, starting from calls in the main program. Here
640 -- we do one step of the inverse transitive closure, and reset
641 -- the Is_Called flag on subprograms all of whose callers are not.
643 for Index in Inlined.First .. Inlined.Last loop
644 S := Inlined.Table (Index).First_Succ;
647 and then not Inlined.Table (Index).Main_Call
649 Set_Is_Called (Inlined.Table (Index).Name, False);
651 while S /= No_Succ loop
654 (Inlined.Table (Successors.Table (S).Subp).Name)
655 or else Inlined.Table (Successors.Table (S).Subp).Main_Call
657 Set_Is_Called (Inlined.Table (Index).Name);
661 S := Successors.Table (S).Next;
666 -- Now that the units are compiled, chain the subprograms within
667 -- that are called and inlined. Produce list of inlined subprograms
668 -- sorted in topological order. Start with all subprograms that
669 -- have no prerequisites, i.e. inlined subprograms that do not call
670 -- other inlined subprograms.
672 for Index in Inlined.First .. Inlined.Last loop
674 if Is_Called (Inlined.Table (Index).Name)
675 and then Inlined.Table (Index).Count = 0
676 and then not Inlined.Table (Index).Listed
678 Add_Inlined_Subprogram (Index);
682 -- Because Add_Inlined_Subprogram treats recursively nodes that have
683 -- no prerequisites left, at the end of the loop all subprograms
684 -- must have been listed. If there are any unlisted subprograms
685 -- left, there must be some recursive chains that cannot be inlined.
687 for Index in Inlined.First .. Inlined.Last loop
688 if Is_Called (Inlined.Table (Index).Name)
689 and then Inlined.Table (Index).Count /= 0
690 and then not Is_Predefined_File_Name
692 (Get_Source_Unit (Inlined.Table (Index).Name)))
695 ("& cannot be inlined?", Inlined.Table (Index).Name);
696 -- A warning on the first one might be sufficient.
702 end Analyze_Inlined_Bodies;
704 -----------------------------
705 -- Check_Body_For_Inlining --
706 -----------------------------
708 procedure Check_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
709 Bname : Unit_Name_Type;
714 if Is_Compilation_Unit (P)
715 and then not Is_Generic_Instance (P)
717 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
718 E := First_Entity (P);
720 while Present (E) loop
721 if Is_Always_Inlined (E)
722 or else (Front_End_Inlining and then Has_Pragma_Inline (E))
724 if not Is_Loaded (Bname) then
725 Load_Needed_Body (N, OK);
729 -- Check that we are not trying to inline a parent
730 -- whose body depends on a child, when we are compiling
731 -- the body of the child. Otherwise we have a potential
732 -- elaboration circularity with inlined subprograms and
733 -- with Taft-Amendment types.
736 Comp : Node_Id; -- Body just compiled
737 Child_Spec : Entity_Id; -- Spec of main unit
738 Ent : Entity_Id; -- For iteration
739 With_Clause : Node_Id; -- Context of body.
742 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
743 and then Present (Body_Entity (P))
747 (Unit (Library_Unit (Cunit (Main_Unit)))));
750 Parent (Unit_Declaration_Node (Body_Entity (P)));
752 With_Clause := First (Context_Items (Comp));
754 -- Check whether the context of the body just
755 -- compiled includes a child of itself, and that
756 -- child is the spec of the main compilation.
758 while Present (With_Clause) loop
759 if Nkind (With_Clause) = N_With_Clause
761 Scope (Entity (Name (With_Clause))) = P
763 Entity (Name (With_Clause)) = Child_Spec
765 Error_Msg_Node_2 := Child_Spec;
767 ("body of & depends on child unit&?",
770 ("\subprograms in body cannot be inlined?",
773 -- Disable further inlining from this unit,
774 -- and keep Taft-amendment types incomplete.
776 Ent := First_Entity (P);
778 while Present (Ent) loop
780 and then Has_Completion_In_Body (Ent)
782 Set_Full_View (Ent, Empty);
784 elsif Is_Subprogram (Ent) then
785 Set_Is_Inlined (Ent, False);
799 elsif Ineffective_Inline_Warnings then
800 Error_Msg_Unit_1 := Bname;
802 ("unable to inline subprograms defined in $?", P);
803 Error_Msg_N ("\body not found?", P);
814 end Check_Body_For_Inlining;
820 procedure Cleanup_Scopes is
826 Elmt := First_Elmt (To_Clean);
828 while Present (Elmt) loop
831 if Ekind (Scop) = E_Entry then
832 Scop := Protected_Body_Subprogram (Scop);
834 elsif Is_Subprogram (Scop)
835 and then Is_Protected_Type (Scope (Scop))
836 and then Present (Protected_Body_Subprogram (Scop))
838 -- If a protected operation contains an instance, its
839 -- cleanup operations have been delayed, and the subprogram
840 -- has been rewritten in the expansion of the enclosing
841 -- protected body. It is the corresponding subprogram that
842 -- may require the cleanup operations.
845 (Protected_Body_Subprogram (Scop),
846 Uses_Sec_Stack (Scop));
847 Scop := Protected_Body_Subprogram (Scop);
850 if Ekind (Scop) = E_Block then
851 Decl := Parent (Block_Node (Scop));
854 Decl := Unit_Declaration_Node (Scop);
856 if Nkind (Decl) = N_Subprogram_Declaration
857 or else Nkind (Decl) = N_Task_Type_Declaration
858 or else Nkind (Decl) = N_Subprogram_Body_Stub
860 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
865 Expand_Cleanup_Actions (Decl);
868 Elmt := Next_Elmt (Elmt);
872 --------------------------
873 -- Has_Initialized_Type --
874 --------------------------
876 function Has_Initialized_Type (E : Entity_Id) return Boolean is
877 E_Body : constant Node_Id := Get_Subprogram_Body (E);
881 if No (E_Body) then -- imported subprogram
885 Decl := First (Declarations (E_Body));
887 while Present (Decl) loop
889 if Nkind (Decl) = N_Full_Type_Declaration
890 and then Present (Init_Proc (Defining_Identifier (Decl)))
900 end Has_Initialized_Type;
906 procedure Initialize is
908 Analyzing_Inlined_Bodies := False;
909 Pending_Descriptor.Init;
910 Pending_Instantiations.Init;
915 for J in Hash_Headers'Range loop
916 Hash_Headers (J) := No_Subp;
920 ------------------------
921 -- Instantiate_Bodies --
922 ------------------------
924 -- Generic bodies contain all the non-local references, so an
925 -- instantiation does not need any more context than Standard
926 -- itself, even if the instantiation appears in an inner scope.
927 -- Generic associations have verified that the contract model is
928 -- satisfied, so that any error that may occur in the analysis of
929 -- the body is an internal error.
931 procedure Instantiate_Bodies is
933 Info : Pending_Body_Info;
936 if Serious_Errors_Detected = 0 then
938 Expander_Active := (Operating_Mode = Opt.Generate_Code);
939 New_Scope (Standard_Standard);
940 To_Clean := New_Elmt_List;
942 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
946 -- A body instantiation may generate additional instantiations, so
947 -- the following loop must scan to the end of a possibly expanding
948 -- set (that's why we can't simply use a FOR loop here).
952 while J <= Pending_Instantiations.Last
953 and then Serious_Errors_Detected = 0
955 Info := Pending_Instantiations.Table (J);
957 -- If the instantiation node is absent, it has been removed
958 -- as part of unreachable code.
960 if No (Info.Inst_Node) then
963 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
964 Instantiate_Package_Body (Info);
965 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
968 Instantiate_Subprogram_Body (Info);
974 -- Reset the table of instantiations. Additional instantiations
975 -- may be added through inlining, when additional bodies are
978 Pending_Instantiations.Init;
980 -- We can now complete the cleanup actions of scopes that contain
981 -- pending instantiations (skipped for generic units, since we
982 -- never need any cleanups in generic units).
983 -- pending instantiations.
986 and then not Is_Generic_Unit (Main_Unit_Entity)
990 -- Also generate subprogram descriptors that were delayed
992 for J in Pending_Descriptor.First .. Pending_Descriptor.Last loop
994 Ent : constant Entity_Id := Pending_Descriptor.Table (J);
997 if Is_Subprogram (Ent) then
998 Generate_Subprogram_Descriptor_For_Subprogram
999 (Get_Subprogram_Body (Ent), Ent);
1001 elsif Ekind (Ent) = E_Package then
1002 Generate_Subprogram_Descriptor_For_Package
1003 (Parent (Declaration_Node (Ent)), Ent);
1005 elsif Ekind (Ent) = E_Package_Body then
1006 Generate_Subprogram_Descriptor_For_Package
1007 (Declaration_Node (Ent), Ent);
1012 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
1018 end Instantiate_Bodies;
1024 function Is_Nested (E : Entity_Id) return Boolean is
1025 Scop : Entity_Id := Scope (E);
1028 while Scop /= Standard_Standard loop
1029 if Ekind (Scop) in Subprogram_Kind then
1032 elsif Ekind (Scop) = E_Task_Type
1033 or else Ekind (Scop) = E_Entry
1034 or else Ekind (Scop) = E_Entry_Family then
1038 Scop := Scope (Scop);
1050 Pending_Instantiations.Locked := True;
1051 Inlined_Bodies.Locked := True;
1052 Successors.Locked := True;
1053 Inlined.Locked := True;
1054 Pending_Instantiations.Release;
1055 Inlined_Bodies.Release;
1060 --------------------------
1061 -- Remove_Dead_Instance --
1062 --------------------------
1064 procedure Remove_Dead_Instance (N : Node_Id) is
1070 while J <= Pending_Instantiations.Last loop
1072 if Pending_Instantiations.Table (J).Inst_Node = N then
1073 Pending_Instantiations.Table (J).Inst_Node := Empty;
1079 end Remove_Dead_Instance;
1081 ------------------------
1082 -- Scope_In_Main_Unit --
1083 ------------------------
1085 function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean is
1087 S : Entity_Id := Scop;
1088 Ent : Entity_Id := Cunit_Entity (Main_Unit);
1091 -- The scope may be within the main unit, or it may be an ancestor
1092 -- of the main unit, if the main unit is a child unit. In both cases
1093 -- it makes no sense to process the body before the main unit. In
1094 -- the second case, this may lead to circularities if a parent body
1095 -- depends on a child spec, and we are analyzing the child.
1097 while Scope (S) /= Standard_Standard
1098 and then not Is_Child_Unit (S)
1105 while Present (Comp)
1106 and then Nkind (Comp) /= N_Compilation_Unit
1108 Comp := Parent (Comp);
1111 if Is_Child_Unit (Ent) then
1114 and then Is_Child_Unit (Ent)
1116 if Scope (Ent) = S then
1125 Comp = Cunit (Main_Unit)
1126 or else Comp = Library_Unit (Cunit (Main_Unit));
1127 end Scope_In_Main_Unit;