2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / inline.adb
blob296ff6b1df579ccb1ea5b526ce7590f46a5927e5
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-2008, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with 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 Opt; use Opt;
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;
47 package body Inline is
49 --------------------
50 -- Inlined Bodies --
51 --------------------
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.
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");
68 -----------------------
69 -- Inline Processing --
70 -----------------------
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.
83 Last_Inlined : Entity_Id := Empty;
85 -- For each entry in the table we keep a list of successors in topological
86 -- order, i.e. callers of the current subprogram.
88 type Subp_Index is new Nat;
89 No_Subp : constant Subp_Index := 0;
91 -- The subprogram entities are hashed into the Inlined table
93 Num_Hash_Headers : constant := 512;
95 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
96 of Subp_Index;
98 type Succ_Index is new Nat;
99 No_Succ : constant Succ_Index := 0;
101 type Succ_Info is record
102 Subp : Subp_Index;
103 Next : Succ_Index;
104 end record;
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.
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");
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;
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");
136 -----------------------
137 -- Local Subprograms --
138 -----------------------
140 function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean;
141 -- Return True if Scop is in the main unit or its spec, or in a
142 -- parent of the main unit if it is a child unit.
144 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
145 -- Make two entries in Inlined table, for an inlined subprogram being
146 -- called, and for the inlined subprogram that contains the call. If
147 -- the call is in the main compilation unit, Caller is Empty.
149 function Add_Subp (E : Entity_Id) return Subp_Index;
150 -- Make entry in Inlined table for subprogram E, or return table index
151 -- that already holds E.
153 function Has_Initialized_Type (E : Entity_Id) return Boolean;
154 -- If a candidate for inlining contains type declarations for types with
155 -- non-trivial initialization procedures, they are not worth inlining.
157 function Is_Nested (E : Entity_Id) return Boolean;
158 -- If the function is nested inside some other function, it will
159 -- always be compiled if that function is, so don't add it to the
160 -- inline list. We cannot compile a nested function outside the
161 -- scope of the containing function anyway. This is also the case if
162 -- the function is defined in a task body or within an entry (for
163 -- example, an initialization procedure).
165 procedure Add_Inlined_Subprogram (Index : Subp_Index);
166 -- Add subprogram to Inlined List once all of its predecessors have been
167 -- placed on the list. Decrement the count of all its successors, and
168 -- add them to list (recursively) if count drops to zero.
170 ------------------------------
171 -- Deferred Cleanup Actions --
172 ------------------------------
174 -- The cleanup actions for scopes that contain instantiations is delayed
175 -- until after expansion of those instantiations, because they may
176 -- contain finalizable objects or tasks that affect the cleanup code.
177 -- A scope that contains instantiations only needs to be finalized once,
178 -- even if it contains more than one instance. We keep a list of scopes
179 -- that must still be finalized, and call cleanup_actions after all the
180 -- instantiations have been completed.
182 To_Clean : Elist_Id;
184 procedure Add_Scope_To_Clean (Inst : Entity_Id);
185 -- Build set of scopes on which cleanup actions must be performed
187 procedure Cleanup_Scopes;
188 -- Complete cleanup actions on scopes that need it
190 --------------
191 -- Add_Call --
192 --------------
194 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
195 P1 : constant Subp_Index := Add_Subp (Called);
196 P2 : Subp_Index;
197 J : Succ_Index;
199 begin
200 if Present (Caller) then
201 P2 := Add_Subp (Caller);
203 -- Add P2 to the list of successors of P1, if not already there.
204 -- Note that P2 may contain more than one call to P1, and only
205 -- one needs to be recorded.
207 J := Inlined.Table (P1).First_Succ;
209 while J /= No_Succ loop
211 if Successors.Table (J).Subp = P2 then
212 return;
213 end if;
215 J := Successors.Table (J).Next;
216 end loop;
218 -- On exit, make a successor entry for P2
220 Successors.Increment_Last;
221 Successors.Table (Successors.Last).Subp := P2;
222 Successors.Table (Successors.Last).Next :=
223 Inlined.Table (P1).First_Succ;
224 Inlined.Table (P1).First_Succ := Successors.Last;
226 Inlined.Table (P2).Count := Inlined.Table (P2).Count + 1;
228 else
229 Inlined.Table (P1).Main_Call := True;
230 end if;
231 end Add_Call;
233 ----------------------
234 -- Add_Inlined_Body --
235 ----------------------
237 procedure Add_Inlined_Body (E : Entity_Id) is
238 Pack : Entity_Id;
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.
244 -----------------
245 -- Must_Inline --
246 -----------------
248 function Must_Inline return Boolean is
249 Scop : Entity_Id;
250 Comp : Node_Id;
252 begin
253 -- Check if call is in main unit
255 Scop := Current_Scope;
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.
261 if Scop = Standard_Standard then
262 return False;
263 end if;
265 -- Otherwise lookup scope stack to outer scope
267 while Scope (Scop) /= Standard_Standard
268 and then not Is_Child_Unit (Scop)
269 loop
270 Scop := Scope (Scop);
271 end loop;
273 Comp := Parent (Scop);
274 while Nkind (Comp) /= N_Compilation_Unit loop
275 Comp := Parent (Comp);
276 end loop;
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;
285 -- Call is not in main unit. See if it's in some inlined subprogram
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;
298 Scop := Scope (Scop);
299 end loop;
301 return False;
302 end Must_Inline;
304 -- Start of processing for Add_Inlined_Body
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).
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.
320 if not Is_Abstract_Subprogram (E) and then not Is_Nested (E)
321 and then Convention (E) /= Convention_Protected
322 then
323 Pack := Scope (E);
325 if Must_Inline
326 and then Ekind (Pack) = E_Package
327 then
328 Set_Is_Called (E);
330 if Pack = Standard_Standard then
332 -- Library-level inlined function. Add function itself to
333 -- list of needed units.
335 Inlined_Bodies.Increment_Last;
336 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
338 elsif Is_Generic_Instance (Pack) then
339 null;
341 elsif not Is_Inlined (Pack)
342 and then not Has_Completion (E)
343 and then not Scope_In_Main_Unit (Pack)
344 then
345 Set_Is_Inlined (Pack);
346 Inlined_Bodies.Increment_Last;
347 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
348 end if;
349 end if;
350 end if;
351 end Add_Inlined_Body;
353 ----------------------------
354 -- Add_Inlined_Subprogram --
355 ----------------------------
357 procedure Add_Inlined_Subprogram (Index : Subp_Index) is
358 E : constant Entity_Id := Inlined.Table (Index).Name;
359 Succ : Succ_Index;
360 Subp : Subp_Index;
362 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean;
363 -- There are various conditions under which back-end inlining cannot
364 -- be done reliably:
366 -- a) If a body has handlers, it must not be inlined, because this
367 -- may violate program semantics, and because in zero-cost exception
368 -- mode it will lead to undefined symbols at link time.
370 -- b) If a body contains inlined function instances, it cannot be
371 -- inlined under ZCX because the numeric suffix generated by gigi
372 -- will be different in the body and the place of the inlined call.
374 -- This procedure must be carefully coordinated with the back end
376 ----------------------------
377 -- Back_End_Cannot_Inline --
378 ----------------------------
380 function Back_End_Cannot_Inline (Subp : Entity_Id) return Boolean is
381 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
382 Body_Ent : Entity_Id;
383 Ent : Entity_Id;
385 begin
386 if Nkind (Decl) = N_Subprogram_Declaration
387 and then Present (Corresponding_Body (Decl))
388 then
389 Body_Ent := Corresponding_Body (Decl);
390 else
391 return False;
392 end if;
394 -- If subprogram is marked Inline_Always, inlining is mandatory
396 if Has_Pragma_Inline_Always (Subp) then
397 return False;
398 end if;
400 if Present
401 (Exception_Handlers
402 (Handled_Statement_Sequence
403 (Unit_Declaration_Node (Corresponding_Body (Decl)))))
404 then
405 return True;
406 end if;
408 Ent := First_Entity (Body_Ent);
410 while Present (Ent) loop
411 if Is_Subprogram (Ent)
412 and then Is_Generic_Instance (Ent)
413 then
414 return True;
415 end if;
417 Next_Entity (Ent);
418 end loop;
419 return False;
420 end Back_End_Cannot_Inline;
422 -- Start of processing for Add_Inlined_Subprogram
424 begin
425 -- Insert the current subprogram in the list of inlined subprograms,
426 -- if it can actually be inlined by the back-end.
428 if not Scope_In_Main_Unit (E)
429 and then Is_Inlined (E)
430 and then not Is_Nested (E)
431 and then not Has_Initialized_Type (E)
432 then
433 if Back_End_Cannot_Inline (E) then
434 Set_Is_Inlined (E, False);
436 else
437 if No (Last_Inlined) then
438 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
439 else
440 Set_Next_Inlined_Subprogram (Last_Inlined, E);
441 end if;
443 Last_Inlined := E;
444 end if;
445 end if;
447 Inlined.Table (Index).Listed := True;
448 Succ := Inlined.Table (Index).First_Succ;
450 while Succ /= No_Succ loop
451 Subp := Successors.Table (Succ).Subp;
452 Inlined.Table (Subp).Count := Inlined.Table (Subp).Count - 1;
454 if Inlined.Table (Subp).Count = 0 then
455 Add_Inlined_Subprogram (Subp);
456 end if;
458 Succ := Successors.Table (Succ).Next;
459 end loop;
460 end Add_Inlined_Subprogram;
462 ------------------------
463 -- Add_Scope_To_Clean --
464 ------------------------
466 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
467 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
468 Elmt : Elmt_Id;
470 begin
471 -- If the instance appears in a library-level package declaration,
472 -- all finalization is global, and nothing needs doing here.
474 if Scop = Standard_Standard then
475 return;
476 end if;
478 -- If the instance appears within a generic subprogram there is nothing
479 -- to finalize either.
481 declare
482 S : Entity_Id;
483 begin
484 S := Scope (Inst);
485 while Present (S) and then S /= Standard_Standard loop
486 if Is_Generic_Subprogram (S) then
487 return;
488 end if;
490 S := Scope (S);
491 end loop;
492 end;
494 Elmt := First_Elmt (To_Clean);
496 while Present (Elmt) loop
498 if Node (Elmt) = Scop then
499 return;
500 end if;
502 Elmt := Next_Elmt (Elmt);
503 end loop;
505 Append_Elmt (Scop, To_Clean);
506 end Add_Scope_To_Clean;
508 --------------
509 -- Add_Subp --
510 --------------
512 function Add_Subp (E : Entity_Id) return Subp_Index is
513 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
514 J : Subp_Index;
516 procedure New_Entry;
517 -- Initialize entry in Inlined table
519 procedure New_Entry is
520 begin
521 Inlined.Increment_Last;
522 Inlined.Table (Inlined.Last).Name := E;
523 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
524 Inlined.Table (Inlined.Last).Count := 0;
525 Inlined.Table (Inlined.Last).Listed := False;
526 Inlined.Table (Inlined.Last).Main_Call := False;
527 Inlined.Table (Inlined.Last).Next := No_Subp;
528 Inlined.Table (Inlined.Last).Next_Nopred := No_Subp;
529 end New_Entry;
531 -- Start of processing for Add_Subp
533 begin
534 if Hash_Headers (Index) = No_Subp then
535 New_Entry;
536 Hash_Headers (Index) := Inlined.Last;
537 return Inlined.Last;
539 else
540 J := Hash_Headers (Index);
542 while J /= No_Subp loop
544 if Inlined.Table (J).Name = E then
545 return J;
546 else
547 Index := J;
548 J := Inlined.Table (J).Next;
549 end if;
550 end loop;
552 -- On exit, subprogram was not found. Enter in table. Index is
553 -- the current last entry on the hash chain.
555 New_Entry;
556 Inlined.Table (Index).Next := Inlined.Last;
557 return Inlined.Last;
558 end if;
559 end Add_Subp;
561 ----------------------------
562 -- Analyze_Inlined_Bodies --
563 ----------------------------
565 procedure Analyze_Inlined_Bodies is
566 Comp_Unit : Node_Id;
567 J : Int;
568 Pack : Entity_Id;
569 S : Succ_Index;
571 begin
572 Analyzing_Inlined_Bodies := False;
574 if Serious_Errors_Detected = 0 then
575 Push_Scope (Standard_Standard);
577 J := 0;
578 while J <= Inlined_Bodies.Last
579 and then Serious_Errors_Detected = 0
580 loop
581 Pack := Inlined_Bodies.Table (J);
583 while Present (Pack)
584 and then Scope (Pack) /= Standard_Standard
585 and then not Is_Child_Unit (Pack)
586 loop
587 Pack := Scope (Pack);
588 end loop;
590 Comp_Unit := Parent (Pack);
591 while Present (Comp_Unit)
592 and then Nkind (Comp_Unit) /= N_Compilation_Unit
593 loop
594 Comp_Unit := Parent (Comp_Unit);
595 end loop;
597 -- Load the body, unless it the main unit, or is an instance
598 -- whose body has already been analyzed.
600 if Present (Comp_Unit)
601 and then Comp_Unit /= Cunit (Main_Unit)
602 and then Body_Required (Comp_Unit)
603 and then (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
604 or else No (Corresponding_Body (Unit (Comp_Unit))))
605 then
606 declare
607 Bname : constant Unit_Name_Type :=
608 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
610 OK : Boolean;
612 begin
613 if not Is_Loaded (Bname) then
614 Load_Needed_Body (Comp_Unit, OK);
616 if not OK then
617 Error_Msg_Unit_1 := Bname;
618 Error_Msg_N
619 ("one or more inlined subprograms accessed in $!",
620 Comp_Unit);
621 Error_Msg_File_1 :=
622 Get_File_Name (Bname, Subunit => False);
623 Error_Msg_N ("\but file{ was not found!", Comp_Unit);
624 raise Unrecoverable_Error;
625 end if;
626 end if;
627 end;
628 end if;
630 J := J + 1;
631 end loop;
633 -- The analysis of required bodies may have produced additional
634 -- generic instantiations. To obtain further inlining, we perform
635 -- another round of generic body instantiations. Establishing a
636 -- fully recursive loop between inlining and generic instantiations
637 -- is unlikely to yield more than this one additional pass.
639 Instantiate_Bodies;
641 -- The list of inlined subprograms is an overestimate, because
642 -- it includes inlined functions called from functions that are
643 -- compiled as part of an inlined package, but are not themselves
644 -- called. An accurate computation of just those subprograms that
645 -- are needed requires that we perform a transitive closure over
646 -- the call graph, starting from calls in the main program. Here
647 -- we do one step of the inverse transitive closure, and reset
648 -- the Is_Called flag on subprograms all of whose callers are not.
650 for Index in Inlined.First .. Inlined.Last loop
651 S := Inlined.Table (Index).First_Succ;
653 if S /= No_Succ
654 and then not Inlined.Table (Index).Main_Call
655 then
656 Set_Is_Called (Inlined.Table (Index).Name, False);
658 while S /= No_Succ loop
660 if Is_Called
661 (Inlined.Table (Successors.Table (S).Subp).Name)
662 or else Inlined.Table (Successors.Table (S).Subp).Main_Call
663 then
664 Set_Is_Called (Inlined.Table (Index).Name);
665 exit;
666 end if;
668 S := Successors.Table (S).Next;
669 end loop;
670 end if;
671 end loop;
673 -- Now that the units are compiled, chain the subprograms within
674 -- that are called and inlined. Produce list of inlined subprograms
675 -- sorted in topological order. Start with all subprograms that
676 -- have no prerequisites, i.e. inlined subprograms that do not call
677 -- other inlined subprograms.
679 for Index in Inlined.First .. Inlined.Last loop
681 if Is_Called (Inlined.Table (Index).Name)
682 and then Inlined.Table (Index).Count = 0
683 and then not Inlined.Table (Index).Listed
684 then
685 Add_Inlined_Subprogram (Index);
686 end if;
687 end loop;
689 -- Because Add_Inlined_Subprogram treats recursively nodes that have
690 -- no prerequisites left, at the end of the loop all subprograms
691 -- must have been listed. If there are any unlisted subprograms
692 -- left, there must be some recursive chains that cannot be inlined.
694 for Index in Inlined.First .. Inlined.Last loop
695 if Is_Called (Inlined.Table (Index).Name)
696 and then Inlined.Table (Index).Count /= 0
697 and then not Is_Predefined_File_Name
698 (Unit_File_Name
699 (Get_Source_Unit (Inlined.Table (Index).Name)))
700 then
701 Error_Msg_N
702 ("& cannot be inlined?", Inlined.Table (Index).Name);
704 -- A warning on the first one might be sufficient ???
705 end if;
706 end loop;
708 Pop_Scope;
709 end if;
710 end Analyze_Inlined_Bodies;
712 -----------------------------
713 -- Check_Body_For_Inlining --
714 -----------------------------
716 procedure Check_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
717 Bname : Unit_Name_Type;
718 E : Entity_Id;
719 OK : Boolean;
721 begin
722 if Is_Compilation_Unit (P)
723 and then not Is_Generic_Instance (P)
724 then
725 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
726 E := First_Entity (P);
728 while Present (E) loop
729 if Has_Pragma_Inline_Always (E)
730 or else (Front_End_Inlining and then Has_Pragma_Inline (E))
731 then
732 if not Is_Loaded (Bname) then
733 Load_Needed_Body (N, OK);
735 if OK then
737 -- Check that we are not trying to inline a parent
738 -- whose body depends on a child, when we are compiling
739 -- the body of the child. Otherwise we have a potential
740 -- elaboration circularity with inlined subprograms and
741 -- with Taft-Amendment types.
743 declare
744 Comp : Node_Id; -- Body just compiled
745 Child_Spec : Entity_Id; -- Spec of main unit
746 Ent : Entity_Id; -- For iteration
747 With_Clause : Node_Id; -- Context of body.
749 begin
750 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
751 and then Present (Body_Entity (P))
752 then
753 Child_Spec :=
754 Defining_Entity (
755 (Unit (Library_Unit (Cunit (Main_Unit)))));
757 Comp :=
758 Parent (Unit_Declaration_Node (Body_Entity (P)));
760 With_Clause := First (Context_Items (Comp));
762 -- Check whether the context of the body just
763 -- compiled includes a child of itself, and that
764 -- child is the spec of the main compilation.
766 while Present (With_Clause) loop
767 if Nkind (With_Clause) = N_With_Clause
768 and then
769 Scope (Entity (Name (With_Clause))) = P
770 and then
771 Entity (Name (With_Clause)) = Child_Spec
772 then
773 Error_Msg_Node_2 := Child_Spec;
774 Error_Msg_NE
775 ("body of & depends on child unit&?",
776 With_Clause, P);
777 Error_Msg_N
778 ("\subprograms in body cannot be inlined?",
779 With_Clause);
781 -- Disable further inlining from this unit,
782 -- and keep Taft-amendment types incomplete.
784 Ent := First_Entity (P);
786 while Present (Ent) loop
787 if Is_Type (Ent)
788 and then Has_Completion_In_Body (Ent)
789 then
790 Set_Full_View (Ent, Empty);
792 elsif Is_Subprogram (Ent) then
793 Set_Is_Inlined (Ent, False);
794 end if;
796 Next_Entity (Ent);
797 end loop;
799 return;
800 end if;
802 Next (With_Clause);
803 end loop;
804 end if;
805 end;
807 elsif Ineffective_Inline_Warnings then
808 Error_Msg_Unit_1 := Bname;
809 Error_Msg_N
810 ("unable to inline subprograms defined in $?", P);
811 Error_Msg_N ("\body not found?", P);
812 return;
813 end if;
814 end if;
816 return;
817 end if;
819 Next_Entity (E);
820 end loop;
821 end if;
822 end Check_Body_For_Inlining;
824 --------------------
825 -- Cleanup_Scopes --
826 --------------------
828 procedure Cleanup_Scopes is
829 Elmt : Elmt_Id;
830 Decl : Node_Id;
831 Scop : Entity_Id;
833 begin
834 Elmt := First_Elmt (To_Clean);
836 while Present (Elmt) loop
837 Scop := Node (Elmt);
839 if Ekind (Scop) = E_Entry then
840 Scop := Protected_Body_Subprogram (Scop);
842 elsif Is_Subprogram (Scop)
843 and then Is_Protected_Type (Scope (Scop))
844 and then Present (Protected_Body_Subprogram (Scop))
845 then
846 -- If a protected operation contains an instance, its
847 -- cleanup operations have been delayed, and the subprogram
848 -- has been rewritten in the expansion of the enclosing
849 -- protected body. It is the corresponding subprogram that
850 -- may require the cleanup operations, so propagate the
851 -- information that triggers cleanup activity.
853 Set_Uses_Sec_Stack
854 (Protected_Body_Subprogram (Scop),
855 Uses_Sec_Stack (Scop));
856 Set_Finalization_Chain_Entity
857 (Protected_Body_Subprogram (Scop),
858 Finalization_Chain_Entity (Scop));
859 Scop := Protected_Body_Subprogram (Scop);
860 end if;
862 if Ekind (Scop) = E_Block then
863 Decl := Parent (Block_Node (Scop));
865 else
866 Decl := Unit_Declaration_Node (Scop);
868 if Nkind (Decl) = N_Subprogram_Declaration
869 or else Nkind (Decl) = N_Task_Type_Declaration
870 or else Nkind (Decl) = N_Subprogram_Body_Stub
871 then
872 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
873 end if;
874 end if;
876 Push_Scope (Scop);
877 Expand_Cleanup_Actions (Decl);
878 End_Scope;
880 Elmt := Next_Elmt (Elmt);
881 end loop;
882 end Cleanup_Scopes;
884 --------------------------
885 -- Has_Initialized_Type --
886 --------------------------
888 function Has_Initialized_Type (E : Entity_Id) return Boolean is
889 E_Body : constant Node_Id := Get_Subprogram_Body (E);
890 Decl : Node_Id;
892 begin
893 if No (E_Body) then -- imported subprogram
894 return False;
896 else
897 Decl := First (Declarations (E_Body));
899 while Present (Decl) loop
901 if Nkind (Decl) = N_Full_Type_Declaration
902 and then Present (Init_Proc (Defining_Identifier (Decl)))
903 then
904 return True;
905 end if;
907 Next (Decl);
908 end loop;
909 end if;
911 return False;
912 end Has_Initialized_Type;
914 ----------------
915 -- Initialize --
916 ----------------
918 procedure Initialize is
919 begin
920 Analyzing_Inlined_Bodies := False;
921 Pending_Descriptor.Init;
922 Pending_Instantiations.Init;
923 Inlined_Bodies.Init;
924 Successors.Init;
925 Inlined.Init;
927 for J in Hash_Headers'Range loop
928 Hash_Headers (J) := No_Subp;
929 end loop;
930 end Initialize;
932 ------------------------
933 -- Instantiate_Bodies --
934 ------------------------
936 -- Generic bodies contain all the non-local references, so an
937 -- instantiation does not need any more context than Standard
938 -- itself, even if the instantiation appears in an inner scope.
939 -- Generic associations have verified that the contract model is
940 -- satisfied, so that any error that may occur in the analysis of
941 -- the body is an internal error.
943 procedure Instantiate_Bodies is
944 J : Int;
945 Info : Pending_Body_Info;
947 begin
948 if Serious_Errors_Detected = 0 then
950 Expander_Active := (Operating_Mode = Opt.Generate_Code);
951 Push_Scope (Standard_Standard);
952 To_Clean := New_Elmt_List;
954 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
955 Start_Generic;
956 end if;
958 -- A body instantiation may generate additional instantiations, so
959 -- the following loop must scan to the end of a possibly expanding
960 -- set (that's why we can't simply use a FOR loop here).
962 J := 0;
963 while J <= Pending_Instantiations.Last
964 and then Serious_Errors_Detected = 0
965 loop
966 Info := Pending_Instantiations.Table (J);
968 -- If the instantiation node is absent, it has been removed
969 -- as part of unreachable code.
971 if No (Info.Inst_Node) then
972 null;
974 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
975 Instantiate_Package_Body (Info);
976 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
978 else
979 Instantiate_Subprogram_Body (Info);
980 end if;
982 J := J + 1;
983 end loop;
985 -- Reset the table of instantiations. Additional instantiations
986 -- may be added through inlining, when additional bodies are
987 -- analyzed.
989 Pending_Instantiations.Init;
991 -- We can now complete the cleanup actions of scopes that contain
992 -- pending instantiations (skipped for generic units, since we
993 -- never need any cleanups in generic units).
994 -- pending instantiations.
996 if Expander_Active
997 and then not Is_Generic_Unit (Main_Unit_Entity)
998 then
999 Cleanup_Scopes;
1000 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
1001 End_Generic;
1002 end if;
1004 Pop_Scope;
1005 end if;
1006 end Instantiate_Bodies;
1008 ---------------
1009 -- Is_Nested --
1010 ---------------
1012 function Is_Nested (E : Entity_Id) return Boolean is
1013 Scop : Entity_Id := Scope (E);
1015 begin
1016 while Scop /= Standard_Standard loop
1017 if Ekind (Scop) in Subprogram_Kind then
1018 return True;
1020 elsif Ekind (Scop) = E_Task_Type
1021 or else Ekind (Scop) = E_Entry
1022 or else Ekind (Scop) = E_Entry_Family then
1023 return True;
1024 end if;
1026 Scop := Scope (Scop);
1027 end loop;
1029 return False;
1030 end Is_Nested;
1032 ----------
1033 -- Lock --
1034 ----------
1036 procedure Lock is
1037 begin
1038 Pending_Instantiations.Locked := True;
1039 Inlined_Bodies.Locked := True;
1040 Successors.Locked := True;
1041 Inlined.Locked := True;
1042 Pending_Instantiations.Release;
1043 Inlined_Bodies.Release;
1044 Successors.Release;
1045 Inlined.Release;
1046 end Lock;
1048 --------------------------
1049 -- Remove_Dead_Instance --
1050 --------------------------
1052 procedure Remove_Dead_Instance (N : Node_Id) is
1053 J : Int;
1055 begin
1056 J := 0;
1058 while J <= Pending_Instantiations.Last loop
1060 if Pending_Instantiations.Table (J).Inst_Node = N then
1061 Pending_Instantiations.Table (J).Inst_Node := Empty;
1062 return;
1063 end if;
1065 J := J + 1;
1066 end loop;
1067 end Remove_Dead_Instance;
1069 ------------------------
1070 -- Scope_In_Main_Unit --
1071 ------------------------
1073 function Scope_In_Main_Unit (Scop : Entity_Id) return Boolean is
1074 Comp : Node_Id;
1075 S : Entity_Id := Scop;
1076 Ent : Entity_Id := Cunit_Entity (Main_Unit);
1078 begin
1079 -- The scope may be within the main unit, or it may be an ancestor
1080 -- of the main unit, if the main unit is a child unit. In both cases
1081 -- it makes no sense to process the body before the main unit. In
1082 -- the second case, this may lead to circularities if a parent body
1083 -- depends on a child spec, and we are analyzing the child.
1085 while Scope (S) /= Standard_Standard
1086 and then not Is_Child_Unit (S)
1087 loop
1088 S := Scope (S);
1089 end loop;
1091 Comp := Parent (S);
1093 while Present (Comp)
1094 and then Nkind (Comp) /= N_Compilation_Unit
1095 loop
1096 Comp := Parent (Comp);
1097 end loop;
1099 if Is_Child_Unit (Ent) then
1101 while Present (Ent)
1102 and then Is_Child_Unit (Ent)
1103 loop
1104 if Scope (Ent) = S then
1105 return True;
1106 end if;
1108 Ent := Scope (Ent);
1109 end loop;
1110 end if;
1112 return
1113 Comp = Cunit (Main_Unit)
1114 or else Comp = Library_Unit (Cunit (Main_Unit));
1115 end Scope_In_Main_Unit;
1117 end Inline;