home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / sem_ch10.adb < prev    next >
Text File  |  1996-09-28  |  48KB  |  1,370 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                             S E M _ C H 1 0                              --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.183 $                            --
  10. --                                                                          --
  11. --        Copyright (c) 1992,1993,1994,1995 NYU, All Rights Reserved        --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. with Atree;    use Atree;
  26. with Debug;    use Debug;
  27. with Einfo;    use Einfo;
  28. with Errout;   use Errout;
  29. with Expander; use Expander;
  30. with Exp_Dist; use Exp_Dist;
  31. with Fname;    use Fname;
  32. with Lib;      use Lib;
  33. with Lib.Load; use Lib.Load;
  34. with Lib.Writ; use Lib.Writ;
  35. with Namet;    use Namet;
  36. with Nlists;   use Nlists;
  37. with Nmake;    use Nmake;
  38. with Opt;      use Opt;
  39. with Output;   use Output;
  40. with Sem;      use Sem;
  41. with Sem_Ch6;  use Sem_Ch6;
  42. with Sem_Ch7;  use Sem_Ch7;
  43. with Sem_Ch8;  use Sem_Ch8;
  44. with Sem_Dist; use Sem_Dist;
  45. with Sem_Util; use Sem_Util;
  46. with Stand;    use Stand;
  47. with Sinfo;    use Sinfo;
  48. with Sinfo.CN; use Sinfo.CN;
  49. with Sinput;   use Sinput;
  50. with Snames;   use Snames;
  51. with Stringt;  use Stringt;
  52. with Tbuild;   use Tbuild;
  53. with Uname;    use Uname;
  54.  
  55.  
  56. package body Sem_Ch10 is
  57.  
  58.    -----------------------
  59.    -- Local Subprograms --
  60.    -----------------------
  61.  
  62.    procedure Analyze_Context (N : Node_Id);
  63.    --  Analyzes items in the context clause of compilation unit
  64.  
  65.    function Ancestor (Lib_Unit : Node_Id) return Entity_Id;
  66.    --  Return the root ancestor of a child unit.
  67.  
  68.    procedure Check_Private_Child_Unit (N : Node_Id);
  69.    --  If a with_clause mentions a private child unit, the compilation
  70.    --  unit must be a member of the same family, as described in 10.1.2 (8).
  71.  
  72.    function Find_Lib_Unit_Entity (Lib_Unit : Node_Id) return Entity_Id;
  73.    --  Retrieve the entity for various kinds of library unit nodes that
  74.    --  have different structure.
  75.  
  76.    procedure Implicit_With_On_Parent (Child_Unit : Node_Id; N : Node_Id);
  77.    --  When a child unit appears in a context clause,  the implicit with on
  78.    --  parents is made explicit, and with clauses are inserted in the context
  79.    --  clause after the one for the child. In addition, if the main unit is a
  80.    --  child unit, implicit withs are also added for all its ancestors. N is
  81.    --  the compilation unit whose list of context items receives the implicit
  82.    --  with clauses.
  83.  
  84.    procedure Install_Context (N : Node_Id);
  85.    --  Installs the entities from the context clause of the given compilation
  86.    --  unit into the visibility chains. This is done before analyzing a unit.
  87.  
  88.    procedure Install_Withed_Unit (Unit_Name : Entity_Id);
  89.    --  If the unit is not a child unit, make unit immediately visible.
  90.    --  The caller ensures that the unit is not already currently installed.
  91.  
  92.    procedure Install_Parents (Lib_Unit : Node_Id);
  93.    --  This procedure establishes the context for the compilation of a child
  94.    --  unit. If Lib_Unit is a child library spec then the context of the parent
  95.    --  is installed, and the parent itself made immediately visible, so that
  96.    --  the child unit is processed in the declarative region of the parent.
  97.    --  Install_Parents makes a recursive call to itself to ensure that all
  98.    --  parents are loaded in the nested case. If Lib_Unit is a library body,
  99.    --  the only effect of Install_Parents is to install the private decls of
  100.    --  the parents, because the visible parent declarations will have been
  101.    --  installed as part of the context of the corresponding spec.
  102.  
  103.    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean;
  104.    --  Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
  105.    --  returns True if Lib_Unit is a library spec which is a child spec, i.e.
  106.    --  a library spec that has a parent. If the call to Is_Child_Spec returns
  107.    --  True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
  108.    --  compilation unit for the parent spec.
  109.    --  Lib_Unit can also be a subprogram body that acts as its own spec. If
  110.    --  the Parent_Spec is  non-empty, this is also a child unit.
  111.  
  112.    procedure Remove_Context (N : Node_Id);
  113.    --  Removes the entities from the context clause of the given compilation
  114.    --  unit from the visibility chains. This is done on exit from a unit as
  115.    --  part of cleaning up the visibility chains for the caller. A special
  116.    --  case is that the call from the Main_Unit can be ignored, since at the
  117.    --  end of the main unit the visibility table won't be needed in any case.
  118.  
  119.    procedure Remove_Parents (Lib_Unit : Node_Id);
  120.    --  Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
  121.    --  contexts established by the corresponding call to Install_Parents are
  122.    --  removed. Remove_Parents contains a recursive call to itself to ensure
  123.    --  that all parents are removed in the nested case.
  124.  
  125.    procedure Remove_Withed_Unit (Unit_Name : Entity_Id);
  126.    --  This procedure makes the given unit not visible.
  127.  
  128.    procedure Analyze_Proper_Body (N : Node_Id);
  129.    --  Common processing for subprogram stubs and package stubs. Once the
  130.    --  subunit name is established, load and analyze.
  131.  
  132.    ------------------------------
  133.    -- Analyze_Compilation_Unit --
  134.    ------------------------------
  135.  
  136.    procedure Analyze_Compilation_Unit (N : Node_Id) is
  137.       Lib_Unit        : constant Node_Id := Unit (N);
  138.       Spec_Id         : Node_Id;
  139.       Stub_Gen_Ok     : Boolean := False;
  140.  
  141.    begin
  142.       --  If the unit is a subunit whose parent has not been analyzed (which
  143.       --  indicates that the main unit is a subunit, either the current one or
  144.       --  one of its descendents) then the subunit is compiled as part of the
  145.       --  analysis of the parent, which we proceed to do. Basically this gets
  146.       --  handled from the top down and we don't want to do anything at this
  147.       --  level (i.e. this subunit will be handled on the way down from the
  148.       --  parent), so at this level we immediately return.
  149.  
  150.       if  N = Cunit (Main_Unit)
  151.         and then Is_RCI_Pkg_Spec_Or_Body (N)
  152.         and then (Stub_Mode = Generate_Receiver_Stub_Body
  153.                    or else Stub_Mode = Generate_Caller_Stub_Body)
  154.       then
  155.          Stub_Gen_Ok := True;
  156.       end if;
  157.  
  158.       if Nkind (Lib_Unit) = N_Subunit
  159.         and then not Analyzed (Library_Unit (N))
  160.       then
  161.          Semantics (Library_Unit (N));
  162.          return;
  163.       end if;
  164.  
  165.       --  Analyze context (this will call Sem recursively for with'ed units)
  166.  
  167.       Analyze_Context (N);
  168.  
  169.       --  If the unit is a package body, the spec is already loaded and must
  170.       --  be analyzed first, before we analyze the body.
  171.  
  172.       if Nkind (Lib_Unit) = N_Package_Body then
  173.  
  174.          --  If the package body is a stub package body, then we perform
  175.          --  appropriate changes on the spec compilation unit before analyzing
  176.          --  it.
  177.  
  178.          if Stub_Mode = Compile_Caller_Stub_Spec then
  179.             To_Calling_Stubs_Decls_Cunit (Library_Unit (N));
  180.  
  181.          elsif Stub_Mode = Compile_Receiver_Stub_Spec then
  182.             To_Receiving_Stubs_Decls_Cunit (Library_Unit (N));
  183.  
  184.          end if;
  185.  
  186.          Semantics (Library_Unit (N));
  187.  
  188.          Spec_Id :=
  189.            Defining_Unit_Simple_Name (Specification (Unit (Library_Unit (N))));
  190.  
  191.          --  The following check is an error defense, get out if as a result
  192.          --  of errors we do not have a proper package spec around!
  193.  
  194.          if No (Spec_Id)
  195.            or else (Ekind (Spec_Id) /= E_Package
  196.                       and then Ekind (Spec_Id) /= E_Generic_Package)
  197.          then
  198.             return;
  199.  
  200.          --  If we have a proper package spec, then set it visible and
  201.          --  update the version to reflect our dependence on the spec.
  202.  
  203.          else
  204.             Set_Is_Immediately_Visible (Spec_Id, True);
  205.             Version_Update (N, Library_Unit (N));
  206.          end if;
  207.  
  208.       --  If the unit is a subprogram body, then we similarly need to analyze
  209.       --  its spec. However, things are a little simpler in this case, because
  210.       --  here, this analysis is done only for error checking and consistency
  211.       --  purposes, so there's nothing else to be done.
  212.  
  213.       elsif Nkind (Lib_Unit) = N_Subprogram_Body
  214.         and then not Acts_As_Spec (N)
  215.       then
  216.          Semantics (Library_Unit (N));
  217.          Version_Update (N, Library_Unit (N));
  218.  
  219.       --  If it is a subprogram declaration it does not need an elaboration
  220.       --  procedure. A renamed package also needs no elaboration procedure.
  221.  
  222.       elsif Nkind (Lib_Unit) = N_Subprogram_Declaration
  223.         or else Nkind (Lib_Unit) = N_Package_Renaming_Declaration
  224.       then
  225.          Set_Has_No_Elab_Code (N, True);
  226.       end if;
  227.  
  228.       --  If it is a child unit, the parent must be elaborated first
  229.       --  and we update version, since we are dependent on our parent.
  230.  
  231.       if Is_Child_Spec (Lib_Unit) then
  232.          Semantics (Parent_Spec (Lib_Unit));
  233.          Version_Update (N, Parent_Spec (Lib_Unit));
  234.       end if;
  235.  
  236.       --  With the analysis done, install the context. Note that we can't
  237.       --  install the context from the with clauses as we analyze them,
  238.       --  because each with clause must be analyzed in a clean visibility
  239.       --  context, so we have to wait and install them all at once.
  240.  
  241.       Install_Context (N);
  242.  
  243.       --  All components of the context: with-clauses, library unit, ancestors
  244.       --  if any, (and their context)  are analyzed and installed. Now analyze
  245.       --  the unit itself, which is either a package, subprogram spec or body.
  246.  
  247.       Analyze (Lib_Unit);
  248.  
  249.       --  Treat compilation unit pragmas that appear after the library unit
  250.  
  251.       if Present (Following_Pragmas (N)) then
  252.          declare
  253.             Prag_Node : Node_Id := First (Following_Pragmas (N));
  254.  
  255.          begin
  256.             while Present (Prag_Node) loop
  257.                Analyze (Prag_Node);
  258.                Prag_Node := Next (Prag_Node);
  259.             end loop;
  260.          end;
  261.       end if;
  262.  
  263.  
  264.       if Stub_Gen_Ok
  265.         and then not Fatal_Error (Main_Unit)
  266.       then
  267.          Generate_Stubs_Files (N);
  268.       end if;
  269.  
  270.       --  Last step is to deinstall the context we just installed
  271.       --  as well as the unit just compiled.
  272.  
  273.       Remove_Context (N);
  274.  
  275.       if Nkind (Lib_Unit) = N_Package_Declaration
  276.         or else Nkind (Lib_Unit) = N_Generic_Package_Declaration
  277.       then
  278.          Remove_Withed_Unit
  279.            (Defining_Unit_Simple_Name (Specification (Lib_Unit)));
  280.  
  281.       elsif Nkind (Lib_Unit) = N_Package_Renaming_Declaration then
  282.          Remove_Withed_Unit
  283.            (Defining_Unit_Simple_Name (Lib_Unit));
  284.  
  285.       elsif Nkind (Lib_Unit) = N_Package_Body
  286.         or else (Nkind (Lib_Unit) = N_Subprogram_Body
  287.                   and then not Acts_As_Spec (N))
  288.       then
  289.          --  Bodies that are not the main unit are compiled if they
  290.          --  are generic or contain generic or inlined units. Their
  291.          --  analysis brings in the context of the corresponding spec
  292.          --  (unit declaration) which must be removed as well, to
  293.          --  return the compilation environment to its proper state.
  294.  
  295.          Remove_Context (Library_Unit (N));
  296.       end if;
  297.  
  298.    end Analyze_Compilation_Unit;
  299.  
  300.    ----------------------------
  301.    -- Analyze_Task_Body_Stub --
  302.    ----------------------------
  303.  
  304.    procedure Analyze_Task_Body_Stub (N : Node_Id) is
  305.       Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
  306.       Loc : constant Source_Ptr := Sloc (N);
  307.  
  308.    begin
  309.       --  First occurence of name may have been as an incomplete type.
  310.  
  311.       if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
  312.          Nam := Full_View (Nam);
  313.       end if;
  314.  
  315.       if No (Nam)
  316.         or else not Is_Task_Type (Etype (Nam))
  317.       then
  318.          Error_Msg_N ("missing specification for task body", N);
  319.       else
  320.          Set_Has_Completion (Nam);
  321.          Analyze_Proper_Body (N);
  322.  
  323.          --  Set elaboration flag to indicate that entity is callable.
  324.          --  This cannot be done in the expansion of the body  itself,
  325.          --  because the proper body is not in a declarative part. This
  326.          --  is only done if expansion is active, because the context
  327.          --  may be generic and the flag not defined yet.
  328.  
  329.          if Expander_Active then
  330.             Insert_After (N,
  331.               Make_Assignment_Statement (Loc,
  332.                 Name =>
  333.                   Make_Identifier (Loc,
  334.                     New_External_Name (Chars (Etype (Nam)), 'E')),
  335.                  Expression => New_Reference_To (Standard_True, Loc)));
  336.          end if;
  337.  
  338.       end if;
  339.    end Analyze_Task_Body_Stub;
  340.  
  341.    ---------------------
  342.    -- Analyze_Context --
  343.    ---------------------
  344.  
  345.    procedure Analyze_Context (N : Node_Id) is
  346.       Item : Node_Id;
  347.  
  348.    begin
  349.       --  Loop through context items
  350.  
  351.       Item := First (Context_Items (N));
  352.       while Present (Item) loop
  353.  
  354.          --  For with clause, analyze the with clause, and then update
  355.          --  the version, since we are dependent on a unit that we with.
  356.  
  357.          if Nkind (Item) = N_With_Clause then
  358.             Analyze (Item);
  359.             Version_Update (N, Library_Unit (N));
  360.  
  361.          --  Analyze pragmas
  362.  
  363.          elsif Nkind (Item) = N_Pragma then
  364.             Analyze (Item);
  365.  
  366.          --  But skip use clauses at this stage, since we don't want to do
  367.          --  any installing of potentially use visible entities until we
  368.          --  we actually install the complete context (in Install_Context).
  369.          --  Otherwise things can get installed in the wrong context.
  370.  
  371.          else
  372.             null;
  373.          end if;
  374.  
  375.          Item := Next (Item);
  376.       end loop;
  377.    end Analyze_Context;
  378.  
  379.    -------------------------------
  380.    -- Analyze_Package_Body_Stub --
  381.    -------------------------------
  382.  
  383.    procedure Analyze_Package_Body_Stub (N : Node_Id) is
  384.       Id   : constant Entity_Id := Defining_Identifier (N);
  385.       Nam  : Entity_Id;
  386.  
  387.    begin
  388.       --  The package declaration must be in the current declarative part
  389.  
  390.       Nam := Current_Entity_In_Scope (Id);
  391.  
  392.       if No (Nam)
  393.         or else
  394.           (Ekind (Nam) /= E_Package and then Ekind (Nam) /= E_Generic_Package)
  395.       then
  396.          Error_Msg_N ("missing specification for package stub", N);
  397.  
  398.       else
  399.          --  Indicate that the body of the package exists. If we are doing
  400.          --  only semantic analysis, the stub stands for the body. If we are
  401.          --  generating code, the existence of the body will be confirmed
  402.          --  when we load the proper body.
  403.  
  404.          Set_Has_Completion (Nam);
  405.          Analyze_Proper_Body (N);
  406.       end if;
  407.    end Analyze_Package_Body_Stub;
  408.  
  409.    -------------------------
  410.    -- Analyze_Proper_Body --
  411.    -------------------------
  412.  
  413.    --  If the subunit is already loaded, it means that the main unit
  414.    --  was a subunit, and that the current unit is one of its parents
  415.    --  which was being analyzed to provide the needed context for the
  416.    --  analysis of the subunit. In this case we analyze the subunit
  417.    --  and then raise Subunit_Found, since we don't need to analyze
  418.    --  any more of the parent (only the part up to here is relevant
  419.    --  to the desired analysis of the subunit).
  420.  
  421.    procedure Analyze_Proper_Body (N : Node_Id) is
  422.       Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
  423.       Unum         : Unit_Number_Type;
  424.  
  425.    begin
  426.       if Is_Loaded (Subunit_Name) then
  427.  
  428.          --  If the proper body is already linked to the stub node,
  429.          --  the stub is in a generic unit and just needs analyzing.
  430.  
  431.          if Present (Library_Unit (N)) then
  432.             Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
  433.             Analyze_Subunit (Library_Unit (N));
  434.  
  435.          --  Otherwise we must load the subunit and link to it
  436.  
  437.          else
  438.             --  Load the subunit, this must work, since we originally
  439.             --  loaded the subunit earlier on. So this will not really
  440.             --  load it, just give access to it.
  441.  
  442.             Unum := Load_Unit (Subunit_Name, True, N);
  443.  
  444.             --  And analyze the subunit in the parent context (note that we
  445.             --  do not call Semantics, since that would remove the parent
  446.             --  context). Because of this, we have to manually reset the
  447.             --  compiler state to Analyzing since it got destroyed by Load.
  448.  
  449.             Compiler_State := Analyzing;
  450.             Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
  451.             Analyze_Subunit (Cunit (Unum));
  452.             Set_Library_Unit (N, Cunit (Unum));
  453.             raise Subunit_Found;
  454.          end if;
  455.  
  456.       --  If the main unit is a subunit, then we are just performing semantic
  457.       --  analysis on that subunit, and any other subunits of any parent unit
  458.       --  should be ignored, except that a stub may provide a declaration.
  459.  
  460.       elsif Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
  461.         and then Subunit_Name /= Unit_Name (Main_Unit)
  462.         and then not Xref_Analyze
  463.       then
  464.          if Nkind (N) = N_Subprogram_Body_Stub then
  465.             Analyze_Subprogram_Body (N);
  466.          end if;
  467.  
  468.          return;
  469.  
  470.       --  If the subunit is not already loaded, and we are generating code,
  471.       --  then this is the case where compilation started from the parent,
  472.       --  and we are generating code for an entire subunit tree. In that
  473.       --  case we definitely need to load the subunit.
  474.  
  475.       --  If the semantic analysis is done for gnatf, we try to load
  476.       --  subunit corresponding to the stub without requiring it to
  477.       --  avoid messages about files not found.
  478.  
  479.       elsif Operating_Mode = Generate_Code or else Xref_Analyze then
  480.  
  481.          --  If the proper body is already linked to the stub node,
  482.          --  the stub is in a generic unit and just needs analyzing.
  483.  
  484.          --  We update the version. Although we are not technically
  485.          --  semantically dependent on the subunit, given our approach
  486.          --  of macro substitution of subunits, it makes sense to
  487.          --  include it in the version identification.
  488.  
  489.          if Present (Library_Unit (N)) then
  490.             Set_Corresponding_Stub (Unit (Library_Unit (N)), N);
  491.             Analyze_Subunit (Library_Unit (N));
  492.             Version_Update (Cunit (Main_Unit), Library_Unit (N));
  493.  
  494.          --  Otherwise we must load the subunit and link to it
  495.  
  496.          else
  497.             if Operating_Mode = Generate_Code then
  498.                Unum := Load_Unit (Subunit_Name, True, N);
  499.             else
  500.                Unum := Load_Unit (Subunit_Name, False, N);
  501.             end if;
  502.  
  503.             --  Load_Unit may reset Compiler_State, since it may have been
  504.             --  necessary to parse an additional units, so we make sure
  505.             --  that we reset it to the Analyzing state.
  506.  
  507.             Compiler_State := Analyzing;
  508.  
  509.             if Unum /= No_Unit and then not Fatal_Error (Unum) then
  510.  
  511.                if Debug_Flag_L then
  512.                   Write_Str ("*** Loaded subunit from stub. Analyze");
  513.                   Write_Eol;
  514.                end if;
  515.  
  516.                Set_Corresponding_Stub (Unit (Cunit (Unum)), N);
  517.                Analyze_Subunit (Cunit (Unum));
  518.                Set_Library_Unit (N, Cunit (Unum));
  519.  
  520.                --  We update the version. Although we are not technically
  521.                --  semantically dependent on the subunit, given our approach
  522.                --  of macro substitution of subunits, it makes sense to
  523.                --  include it in the version identification.
  524.  
  525.                Version_Update (Cunit (Main_Unit), Cunit (Unum));
  526.  
  527.             else
  528.                --  If the subunit corresponding to the stub has not
  529.                --  been found, then in an analyze called by gnatf, we
  530.                --  avoid messages about missing bodies for procedures
  531.                --  and functions
  532.  
  533.                if Xref_Analyze then
  534.  
  535.                   case Nkind (N) is
  536.  
  537.                      when N_Subprogram_Body_Stub =>
  538.                         declare
  539.                            Spec      : constant Node_Id := Specification (N);
  540.                            Spec_Node : Entity_Id;
  541.                            Subp      : Entity_Id;
  542.  
  543.                         begin
  544.                            Subp := Analyze_Spec (Spec);
  545.                            Spec_Node := Find_Corresponding_Spec (N);
  546.                         end;
  547.  
  548.                      when others =>
  549.                         null;
  550.                   end case;
  551.  
  552.                end if;
  553.  
  554.             end if;
  555.          end if;
  556.  
  557.          --  The remaining case is when the subunit is not already loaded and
  558.          --  we are not generating code. In this case we are just performing
  559.          --  semantic analysis on the parent, and we are not interested in
  560.          --  the subunit. The caller has already processed the stub as a
  561.          --  declaration, if necessary.
  562.  
  563.       else
  564.          null;
  565.       end if;
  566.  
  567.    end Analyze_Proper_Body;
  568.  
  569.    ----------------------------------
  570.    -- Analyze_Protected_Body_Stub --
  571.    ----------------------------------
  572.  
  573.    procedure Analyze_Protected_Body_Stub (N : Node_Id) is
  574.       Nam : Entity_Id := Current_Entity_In_Scope (Defining_Identifier (N));
  575.       Loc : constant Source_Ptr := Sloc (N);
  576.  
  577.    begin
  578.       --  First occurence of name may have been as an incomplete type.
  579.  
  580.       if Present (Nam) and then Ekind (Nam) = E_Incomplete_Type then
  581.          Nam := Full_View (Nam);
  582.       end if;
  583.  
  584.       if No (Nam)
  585.         or else not Is_Protected_Type (Etype (Nam))
  586.       then
  587.          Error_Msg_N ("missing specification for Protected body", N);
  588.       else
  589.          Set_Has_Completion (Nam);
  590.          Analyze_Proper_Body (N);
  591.       end if;
  592.    end Analyze_Protected_Body_Stub;
  593.  
  594.    ----------------------------------
  595.    -- Analyze_Subprogram_Body_Stub --
  596.    ----------------------------------
  597.  
  598.    --  A subprogram body stub can appear with or without a previous
  599.    --  specification. If there is one, the analysis of the body will
  600.    --  find it and verify conformance.  The formals appearing in the
  601.    --  specification of the stub play no role, except for requiring
  602.    --  an additional conformance check. However, if we are performing
  603.    --  semantic checks only, the stub must be analyzed like a body,
  604.    --  because it may be the declaration of the subprogram.
  605.  
  606.    procedure Analyze_Subprogram_Body_Stub (N : Node_Id) is
  607.    begin
  608.       if Operating_Mode /= Generate_Code and then not Xref_Analyze then
  609.          Analyze_Subprogram_Body (N);
  610.       else
  611.          Analyze_Proper_Body (N);
  612.       end if;
  613.    end Analyze_Subprogram_Body_Stub;
  614.  
  615.    ---------------------
  616.    -- Analyze_Subunit --
  617.    ---------------------
  618.  
  619.    --  A subunit is compiled either by itself (for semantic checking)
  620.    --  or as part of compiling the parent (for code generation). In
  621.    --  either case, by the time we actually process the subunit, the
  622.    --  parent has already been installed and analyzed. The node N is
  623.    --  a compilation unit, whose context needs to be treated here,
  624.    --  because we come directly here from the parent without calling
  625.    --  Analyze_Compilation_Unit.
  626.  
  627.    --  The compilation context includes the explicit context of the
  628.    --  subunit, and the context of the parent, together with the parent
  629.    --  itself. In order to compile the current context, we remove the
  630.    --  one inherited from the parent, in order to have a clean visibility
  631.    --  table. We restore the parent context before analyzing the proper
  632.    --  body itself. On exit, we remove only the explicit context of the
  633.    --  subunit.
  634.  
  635.    procedure Analyze_Subunit (N : Node_Id) is
  636.       Lib        : constant Node_Id := Library_Unit (N);
  637.       Lib_Spec   : Node_Id := Library_Unit (Lib);
  638.       Par_Unit   : constant Entity_Id := Current_Scope;
  639.       Use_Clause : Node_Id;
  640.  
  641.       procedure Re_Install_Parents (L : Node_Id);
  642.       --  Recursive procedure to restore scope of all ancestors of subunit,
  643.       --  from outermost in. If parent is not a subunit, the call to install
  644.       --  context installs context of spec and (if parent is a child unit)
  645.       --  the context of its parents as well. It is confusing that parents
  646.       --  should be treated differently in both cases, but the semantics are
  647.       --  just not identical.
  648.  
  649.       procedure Re_Install_Use_Clauses;
  650.       --  As part of the removal of the parent scope, the use clauses are
  651.       --  removed, to be reinstalled when the context of the subunit has
  652.       --  been analyzed.
  653.  
  654.       procedure Re_Install_Parents (L : Node_Id) is
  655.       begin
  656.          if Nkind (Unit (L)) = N_Subunit then
  657.             Re_Install_Parents (Library_Unit (L));
  658.          end if;
  659.  
  660.          Install_Context (L);
  661.       end Re_Install_Parents;
  662.  
  663.  
  664.       procedure Re_Install_Use_Clauses is
  665.          U : Node_Id;
  666.  
  667.       begin
  668.          while Present (Use_Clause) loop
  669.             U := Use_Clause;
  670.             Use_Clause := Next_Use_Clause (U);
  671.  
  672.             if Nkind (U) = N_Use_Package_Clause then
  673.                Analyze_Use_Package (U);
  674.             else
  675.                Analyze_Use_Type (U);
  676.             end if;
  677.          end loop;
  678.       end Re_Install_Use_Clauses;
  679.  
  680.    begin
  681.       if not Is_Empty_List (Context_Items (N)) then
  682.  
  683.          --  Save current use clauses.
  684.  
  685.          Use_Clause := Scope_Stack.Table (Scope_Stack.Last).First_Use_Clause;
  686.          Pop_Scope;
  687.          Remove_Context (Lib);
  688.  
  689.          --  If the parent is a package body, remove the context of the spec
  690.          --  as well. If it is a subprogram body, verify first that there is
  691.          --  a spec for it. If the parent is a subunit, Lib_Spec is its
  692.          --  parent, whose context must also be removed, together with that
  693.          --  of further ancestors.
  694.  
  695.          if Present (Lib_Spec) then
  696.             Remove_Context (Lib_Spec);
  697.  
  698.             while Nkind (Unit (Lib_Spec)) = N_Subunit loop
  699.                Lib_Spec := Library_Unit (Lib_Spec);
  700.                Remove_Context (Lib_Spec);
  701.             end loop;
  702.          end if;
  703.  
  704.          Analyze_Context (N);
  705.  
  706.          Re_Install_Parents (Lib);
  707.          New_Scope (Par_Unit);
  708.          Re_Install_Use_Clauses;
  709.          Install_Context (N);
  710.       end if;
  711.  
  712.       Analyze (Proper_Body (Unit (N)));
  713.       Remove_Context (N);
  714.  
  715.    end Analyze_Subunit;
  716.  
  717.    -------------------------
  718.    -- Analyze_With_Clause --
  719.    -------------------------
  720.  
  721.    --  Analyze the declaration of a unit in  a with clause. At end,
  722.    --  label the with clause with the defining entity for the unit.
  723.  
  724.    procedure Analyze_With_Clause (N : Node_Id) is
  725.       Unit_Kind : constant Node_Kind := Nkind (Unit (Library_Unit (N)));
  726.       E_Name    : Entity_Id;
  727.  
  728.    begin
  729.       Semantics (Library_Unit (N));
  730.  
  731.       if Unit_Kind in N_Generic_Declaration then
  732.  
  733.          --  Semantic analysis of a generic unit is performed on a copy of
  734.          --  the original tree. Retrieve the entity on  which semantic info
  735.          --  actually appears.
  736.  
  737.          E_Name :=
  738.           Defining_Unit_Simple_Name (Specification (Unit (Library_Unit (N))));
  739.  
  740.       elsif Unit_Kind = N_Package_Instantiation
  741.            and then Nkind (Unit (Library_Unit (N))) = N_Package_Body
  742.       then
  743.          --  Instantiation node is replaced with body of instance.
  744.          --  Unit name is defining unit name in corresponding spec.
  745.  
  746.          E_Name := Corresponding_Spec (Unit (Library_Unit (N)));
  747.  
  748.       elsif Unit_Kind = N_Procedure_Instantiation
  749.         or else Unit_Kind = N_Function_Instantiation
  750.       then
  751.          --  Instantiation node is replaced with a package that contains
  752.          --  renaming declarations and instance itself. The subprogram
  753.          --  specification is the last declaration in the package spec.
  754.  
  755.          E_Name := Defining_Unit_Simple_Name (Specification (
  756.                Last (Visible_Declarations (Specification (
  757.                            Unit (Library_Unit (N)))))));
  758.  
  759.       elsif Unit_Kind = N_Package_Renaming_Declaration then
  760.          E_Name := Defining_Unit_Simple_Name (Unit (Library_Unit (N)));
  761.  
  762.       elsif Unit_Kind in N_Generic_Renaming_Declaration then
  763.          E_Name := Defining_Unit_Simple_Name (Unit (Library_Unit (N)));
  764.  
  765.       else
  766.          E_Name := Defining_Unit_Simple_Name
  767.                              (Specification (Unit (Library_Unit (N))));
  768.       end if;
  769.  
  770.       if Nkind (Name (N)) = N_Selected_Component then
  771.  
  772.          --  Child unit in a with clause
  773.  
  774.          Change_Selected_Component_To_Expanded_Name (Name (N));
  775.       end if;
  776.  
  777.       Set_Entity_With_Style_Check (Name (N), E_Name);
  778.    end Analyze_With_Clause;
  779.  
  780.    --------------
  781.    -- Ancestor --
  782.    --------------
  783.  
  784.    function Ancestor (Lib_Unit : Node_Id) return Entity_Id is
  785.       P      : constant Node_Id := Parent_Spec (Lib_Unit);
  786.       P_Name : Entity_Id;
  787.  
  788.    begin
  789.  
  790.       if No (P) then
  791.          return Empty;
  792.       else
  793.          P_Name := Defining_Unit_Simple_Name (Specification (Unit (P)));
  794.  
  795.          while Scope (P_Name) /= Standard_Standard loop
  796.             P_Name := Scope (P_Name);
  797.          end loop;
  798.  
  799.          return P_Name;
  800.       end if;
  801.    end Ancestor;
  802.  
  803.    ------------------------------
  804.    -- Check_Private_Child_Unit --
  805.    ------------------------------
  806.  
  807.    procedure Check_Private_Child_Unit (N : Node_Id) is
  808.       Lib_Unit      : Node_Id := Unit (N);
  809.       Item          : Node_Id;
  810.       Curr_Unit     : Entity_Id;
  811.       Parent        : Node_Id;
  812.       Priv_Child    : Entity_Id;
  813.       Par_Lib       : Entity_Id;
  814.  
  815.       function Parent_Lib_Unit (E : Entity_Id) return Entity_Id;
  816.       --  Find ultimate ancestor of analyzed unit.
  817.  
  818.       function Parent_Lib_Unit (E : Entity_Id) return Entity_Id is
  819.          Par : Entity_Id := E;
  820.  
  821.       begin
  822.          while Present (Par)
  823.            and then Is_Child_Unit (Par)
  824.          loop
  825.             Par := Scope (Par);
  826.          end loop;
  827.  
  828.          return Par;
  829.       end Parent_Lib_Unit;
  830.  
  831.    begin
  832.       if Nkind (Lib_Unit) = N_Package_Body
  833.         or else Nkind (Lib_Unit) = N_Subprogram_Body
  834.       then
  835.          Curr_Unit := Find_Lib_Unit_Entity (Unit (Library_Unit (N)));
  836.          Par_Lib   := Parent_Lib_Unit (Curr_Unit);
  837.  
  838.       elsif Nkind (Lib_Unit) = N_Subunit then
  839.  
  840.          --  The parent is itself a body. The parent entity is to be found
  841.          --  in the corresponding spec.
  842.  
  843.          Parent    := Library_Unit (N);
  844.          Curr_Unit := Find_Lib_Unit_Entity (Unit (Library_Unit (Parent)));
  845.          Par_Lib   := Parent_Lib_Unit (Curr_Unit);
  846.  
  847.       else
  848.          Curr_Unit := Find_Lib_Unit_Entity (Lib_Unit);
  849.          Par_Lib   := Ancestor (Lib_Unit);
  850.       end if;
  851.  
  852.       Item := First (Context_Items (N));
  853.       while Present (Item) loop
  854.  
  855.          if Nkind (Item) = N_With_Clause
  856.             and then not Implicit_With (Item)
  857.             and then Is_Private_Descendant (Entity (Name (Item)))
  858.          then
  859.             Priv_Child := Entity (Name (Item));
  860.  
  861.             if not Is_Child_Unit (Curr_Unit)
  862.               or else Parent_Lib_Unit (Priv_Child) /= Par_Lib
  863.             then
  864.                Error_Msg_N
  865.                  ("?unit in with clause is private child unit", Item);
  866.                Error_Msg_N
  867.                  ("?current unit must have same ancestor!", Item);
  868.                Temporary_Msg_N
  869.                  ("?this will be a fatal error in the next release!",
  870.                    Item);
  871.                Temporary_Msg_N ("?see gnatinfo.txt for details!", Item);
  872.             elsif not Is_Private_Descendant (Curr_Unit)
  873.               and then Nkind (Lib_Unit) /= N_Package_Body
  874.               and then Nkind (Lib_Unit) /= N_Subprogram_Body
  875.             then
  876.                Error_Msg_NE
  877.                  ("?current unit must also be private child of &",
  878.                    Item, Parent_Lib_Unit (Priv_Child));
  879.                Temporary_Msg_N
  880.                  ("?this will be a fatal error in 2.06, see gnatinfo.txt!",
  881.                    Item);
  882.             end if;
  883.          end if;
  884.  
  885.          Item := Next (Item);
  886.       end loop;
  887.  
  888.    end Check_Private_Child_Unit;
  889.  
  890.    --------------------------
  891.    -- Find_Lib_Unit_Entity --
  892.    --------------------------
  893.  
  894.    function Find_Lib_Unit_Entity (Lib_Unit : Node_Id) return Entity_Id is
  895.    begin
  896.       if Nkind (Lib_Unit) in N_Generic_Instantiation
  897.         or else Nkind (Lib_Unit)  = N_Package_Renaming_Declaration
  898.         or else Nkind (Lib_Unit) in N_Generic_Renaming_Declaration
  899.       then
  900.          return Defining_Unit_Simple_Name (Lib_Unit);
  901.  
  902.       else
  903.          return Defining_Unit_Simple_Name (Specification (Lib_Unit));
  904.       end if;
  905.    end Find_Lib_Unit_Entity;
  906.  
  907.    ---------------------
  908.    -- Install_Context --
  909.    ---------------------
  910.  
  911.    procedure Install_Context (N : Node_Id) is
  912.       Lib_Unit      : Node_Id := Unit (N);
  913.       Item          : Node_Id;
  914.       Uname_Node    : Entity_Id;
  915.       Unit_Num      : constant Unit_Number_Type := Get_Cunit_Unit_Number (N);
  916.       Check_Private : Boolean := False;
  917.  
  918.    begin
  919.       --  Loop through context clauses to find the with clauses
  920.  
  921.       Item := First (Context_Items (N));
  922.       while Present (Item) loop
  923.  
  924.          if Nkind (Item) = N_With_Clause
  925.             and then not Implicit_With (Item)
  926.          then
  927.             Uname_Node := Entity (Name (Item));
  928.  
  929.             if Is_Private_Descendant (Uname_Node) then
  930.                Check_Private := True;
  931.             end if;
  932.  
  933.             if not Is_Immediately_Visible (Uname_Node) then
  934.                Install_Withed_Unit (Uname_Node);
  935.                Set_Context_Installed (Item, True);
  936.  
  937.             else
  938.                --  Unit has already been installed for an earlier context.
  939.  
  940.                null;
  941.             end if;
  942.  
  943.             if Is_Child_Spec (Get_Declaration_Node (Uname_Node)) then
  944.                Implicit_With_On_Parent (Get_Declaration_Node (Uname_Node), N);
  945.             end if;
  946.  
  947.          elsif Nkind (Item) = N_Use_Package_Clause
  948.            or else Nkind (Item) = N_Use_Type_Clause
  949.          then
  950.             --  Use clauses are not allowed in the context clause of specs
  951.             --  of predefined packages (this ensures meeting the rule that
  952.             --  nothing with'ed by rtsfind is allowed to have use clauses)
  953.  
  954.             if Is_Language_Defined_Unit (Unit_File_Name (Unit_Num))
  955.               and then Is_Spec_Name (Unit_Name (Unit_Num))
  956.             then
  957.                Error_Msg_N
  958.                  ("use clause not allowed in predefined spec", Item);
  959.                raise Unrecoverable_Error;
  960.  
  961.             elsif Nkind (Item) = N_Use_Package_Clause then
  962.                Analyze_Use_Package (Item);
  963.  
  964.             else
  965.                Analyze_Use_Type (Item);
  966.             end if;
  967.          end if;
  968.  
  969.          Item := Next (Item);
  970.       end loop;
  971.  
  972.       if Is_Child_Spec (Lib_Unit) then
  973.  
  974.          --  The unit also has implicit withs on its own parents.
  975.  
  976.          if No (Context_Items (N)) then
  977.             Set_Context_Items (N, New_List);
  978.          end if;
  979.  
  980.          Implicit_With_On_Parent (Lib_Unit, N);
  981.       end if;
  982.  
  983.       --  If the unit is a body, the context of the specification must also
  984.       --  be installed.
  985.  
  986.       if Nkind (Lib_Unit) = N_Package_Body
  987.         or else (Nkind (Lib_Unit) = N_Subprogram_Body
  988.                   and then not Acts_As_Spec (N))
  989.       then
  990.          Install_Context (Library_Unit (N));
  991.  
  992.          if Is_Child_Spec (Unit (Library_Unit (N))) then
  993.  
  994.             --  If the unit is the body of a public child unit, the private
  995.             --  declarations of the parent must be made visible. If the child
  996.             --  unit is private, the private declarations have been installed
  997.             --  already in the call to Install_Parents for the spec. Installing
  998.             --  private declarations must be done for all ancestors of public
  999.             --  child units.
  1000.  
  1001.             declare
  1002.                Lib_Spec : Node_Id := Unit (Library_Unit (N));
  1003.                P        : Node_Id;
  1004.                P_Name   : Entity_Id;
  1005.  
  1006.             begin
  1007.                while Is_Child_Spec (Lib_Spec) loop
  1008.                   P := Unit (Parent_Spec (Lib_Spec));
  1009.  
  1010.                   if not (Private_Present (Parent (Lib_Spec))) then
  1011.                      P_Name := Defining_Unit_Simple_Name (Specification (P));
  1012.                      Install_Private_Declarations (P_Name);
  1013.                      Set_Use (Private_Declarations (Specification (P)));
  1014.                   end if;
  1015.  
  1016.                   Lib_Spec := P;
  1017.                end loop;
  1018.             end;
  1019.          end if;
  1020.       end if;
  1021.  
  1022.       Install_Parents (Lib_Unit);
  1023.  
  1024.       if Check_Private then
  1025.          Check_Private_Child_Unit (N);
  1026.       end if;
  1027.    end Install_Context;
  1028.  
  1029.    -----------------------------
  1030.    -- Implicit_With_On_Parent --
  1031.    -----------------------------
  1032.  
  1033.    procedure Implicit_With_On_Parent (
  1034.      Child_Unit : Node_Id;
  1035.      N          : Node_Id)
  1036.  
  1037.    is
  1038.       Loc    : constant Source_Ptr := Sloc (N);
  1039.       P      : constant Node_Id := Parent_Spec (Child_Unit);
  1040.       P_Unit : constant Node_Id := Unit (P);
  1041.       P_Name : Entity_Id := Find_Lib_Unit_Entity (P_Unit);
  1042.       Withn  : Node_Id;
  1043.  
  1044.       function Build_Unit_Name return Node_Id;
  1045.       --  If the unit is a child unit, build qualified name with all
  1046.       --  ancestors.
  1047.  
  1048.       function Build_Ancestor_Name (P : Node_Id)  return Node_Id;
  1049.       --  Build prefix of child unit name. Recurse if needed.
  1050.  
  1051.       function Build_Unit_Name return Node_Id is
  1052.          Result : Node_Id;
  1053.       begin
  1054.          if No (Parent_Spec (P_Unit)) then
  1055.             return New_Reference_To (P_Name, Loc);
  1056.          else
  1057.             Result :=
  1058.               Make_Expanded_Name (Loc,
  1059.                 Chars  => Chars (P_Name),
  1060.                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P_Unit))),
  1061.                 Selector_Name => New_Reference_To (P_Name, Loc));
  1062.             Set_Entity (Result, P_Name);
  1063.             return Result;
  1064.          end if;
  1065.       end Build_Unit_Name;
  1066.  
  1067.       function Build_Ancestor_Name (P : Node_Id) return Node_Id is
  1068.          P_Ref : Node_Id := New_Reference_To (Find_Lib_Unit_Entity (P), Loc);
  1069.  
  1070.       begin
  1071.          if No (Parent_Spec (P)) then
  1072.             return P_Ref;
  1073.          else
  1074.             return
  1075.               Make_Selected_Component (Loc,
  1076.                 Prefix => Build_Ancestor_Name (Unit (Parent_Spec (P))),
  1077.                 Selector_Name => P_Ref);
  1078.          end if;
  1079.       end Build_Ancestor_Name;
  1080.  
  1081.    begin
  1082.       Withn  := Make_With_Clause (Loc, Name => Build_Unit_Name);
  1083.  
  1084.       Set_Library_Unit          (Withn, P);
  1085.       Set_Corresponding_Spec    (Withn, P_Name);
  1086.       Set_First_Name            (Withn, True);
  1087.       Set_Implicit_With         (Withn, True);
  1088.  
  1089.       --  Node is placed at the beginning of the context items, so that
  1090.       --  subsequent use clauses on the parent can be validated.
  1091.  
  1092.       Prepend (Withn, Context_Items (N));
  1093.       Mark_Rewrite_Insertion (Withn);
  1094.  
  1095.       if not Is_Immediately_Visible (P_Name) then
  1096.          Install_Withed_Unit (P_Name);
  1097.          Set_Context_Installed (Withn, True);
  1098.       end if;
  1099.  
  1100.       if Is_Child_Spec (P_Unit) then
  1101.          Implicit_With_On_Parent (P_Unit, N);
  1102.       end if;
  1103.    end Implicit_With_On_Parent;
  1104.  
  1105.    -------------------------
  1106.    -- Install_Withed_Unit --
  1107.    -------------------------
  1108.  
  1109.    procedure Install_Withed_Unit (Unit_Name : Entity_Id) is
  1110.       P : Entity_Id := Scope (Unit_Name);
  1111.    begin
  1112.  
  1113.       if P /= Standard_Standard then
  1114.  
  1115.          --  Unit is child unit, only ultimate ancestor is immediately visible
  1116.  
  1117.          while Scope (P) /= Standard_Standard loop
  1118.             P := Scope (P);
  1119.          end loop;
  1120.  
  1121.          Set_Is_Immediately_Visible (P);
  1122.  
  1123.       else
  1124.          Set_Is_Immediately_Visible (Unit_Name);
  1125.       end if;
  1126.  
  1127.    end Install_Withed_Unit;
  1128.  
  1129.    -----------------------
  1130.    -- Load_Needed_Body --
  1131.    -----------------------
  1132.  
  1133.    --  N is a generic unit named in a with clause, or else it is
  1134.    --  a unit that contains a generic unit or an inlined function.
  1135.    --  In order to perform an instantiation, the body of the unit
  1136.    --  must be present. If the unit itself is generic, we assume
  1137.    --  that an instantiation follows, and  load and analyze the body
  1138.    --  unconditionally. This forces analysis of the spec as well.
  1139.    --  If the unit is not generic, but contains a generic unit, it
  1140.    --  is loaded on demand, at the point of instantiation (see ch12).
  1141.  
  1142.    procedure Load_Needed_Body (N : Node_Id) is
  1143.       Body_Name : Unit_Name_Type;
  1144.       Unum      : Unit_Number_Type;
  1145.  
  1146.    begin
  1147.       Body_Name := Get_Body_Name (Get_Unit_Name (Unit (N)));
  1148.       Unum := Load_Unit (Body_Name, True, N);
  1149.       Compiler_State := Analyzing; -- reset after load
  1150.  
  1151.       if Unum /= No_Unit
  1152.         and then not Fatal_Error (Unum)
  1153.       then
  1154.          if Debug_Flag_L then
  1155.             Write_Str ("*** Loaded generic body");
  1156.             Write_Eol;
  1157.          end if;
  1158.  
  1159.          Semantics (Cunit (Unum));
  1160.       end if;
  1161.    end Load_Needed_Body;
  1162.  
  1163.    ----------------------
  1164.    --  Install_Parents --
  1165.    ----------------------
  1166.  
  1167.    procedure Install_Parents (Lib_Unit : Node_Id) is
  1168.       P      : Node_Id;
  1169.       E_Name : Entity_Id;
  1170.       P_Name : Entity_Id;
  1171.       P_Spec : Node_Id;
  1172.  
  1173.    begin
  1174.       if Is_Child_Spec (Lib_Unit) then
  1175.  
  1176.          P := Unit (Parent_Spec (Lib_Unit));
  1177.          P_Name := Find_Lib_Unit_Entity (P);
  1178.  
  1179.          if Ekind (P_Name) = E_Generic_Package
  1180.            and then Nkind (Lib_Unit) /= N_Generic_Subprogram_Declaration
  1181.            and then Nkind (Lib_Unit) /= N_Generic_Package_Declaration
  1182.          then
  1183.             Error_Msg_N
  1184.               ("child of a generic package must be generic unit", Lib_Unit);
  1185.  
  1186.          elsif Ekind (P_Name) /= E_Generic_Package
  1187.            and then Ekind (P_Name) /= E_Package
  1188.          then
  1189.             Error_Msg_N
  1190.               ("Parent unit must be package or generic package", Lib_Unit);
  1191.             raise Unrecoverable_Error;
  1192.  
  1193.          elsif Present (Renamed_Object (P_Name)) then
  1194.             Error_Msg_N ("parent unit cannot be a renaming", Lib_Unit);
  1195.             raise Unrecoverable_Error;
  1196.          end if;
  1197.  
  1198.          --  This is the recursive call that ensures all parents are loaded
  1199.  
  1200.          Install_Parents (P);
  1201.  
  1202.          --  Now we can install the context for this parent
  1203.  
  1204.          Install_Context (Parent_Spec (Lib_Unit));
  1205.  
  1206.          --  The child unit is in the declarative region of the parent. The
  1207.          --  parent must therefore appear in the scope stack and be visible,
  1208.          --  as when compiling the corresponding body. If the child unit is
  1209.          --  private or it is a package body, private declarations must be
  1210.          --  accessible as well. Use declarations in the parent must also
  1211.          --  be installed.
  1212.  
  1213.          Set_Is_Immediately_Visible (P_Name, True);
  1214.  
  1215.          --  Find entity for compilation unit, and set its private descendant
  1216.          --  status as needed.
  1217.  
  1218.          E_Name := Find_Lib_Unit_Entity (Lib_Unit);
  1219.  
  1220.          Set_Is_Child_Unit (E_Name);
  1221.  
  1222.          Set_Is_Private_Descendant (E_Name,
  1223.             Is_Private_Descendant (P_Name)
  1224.               or else Private_Present (Parent (Lib_Unit)));
  1225.  
  1226.          P_Spec := Specification (Get_Declaration_Node (P_Name));
  1227.          New_Scope (P_Name);
  1228.          Install_Visible_Declarations (P_Name);
  1229.          Set_Use (Visible_Declarations (P_Spec));
  1230.  
  1231.          if Private_Present (Parent (Lib_Unit)) then
  1232.             Install_Private_Declarations (P_Name);
  1233.             Set_Use (Private_Declarations (P_Spec));
  1234.          end if;
  1235.  
  1236.       --  If the unit is not a child unit, or is a body, nothing to do.
  1237.  
  1238.       else
  1239.          null;
  1240.       end if;
  1241.    end Install_Parents;
  1242.  
  1243.    -------------------
  1244.    -- Is_Child_Spec --
  1245.    -------------------
  1246.  
  1247.    function Is_Child_Spec (Lib_Unit : Node_Id) return Boolean is
  1248.       K : constant Node_Kind := Nkind (Lib_Unit);
  1249.  
  1250.    begin
  1251.       return (K in N_Generic_Declaration              or else
  1252.               K in N_Generic_Instantiation            or else
  1253.               K in N_Generic_Renaming_Declaration     or else
  1254.               K =  N_Package_Declaration              or else
  1255.               K =  N_Package_Renaming_Declaration     or else
  1256.               K =  N_Subprogram_Declaration           or else
  1257.               K =  N_Subprogram_Renaming_Declaration)
  1258.         and then Present (Parent_Spec (Lib_Unit));
  1259.    end Is_Child_Spec;
  1260.  
  1261.    --------------------
  1262.    -- Remove_Parents --
  1263.    --------------------
  1264.  
  1265.    procedure Remove_Parents (Lib_Unit : Node_Id) is
  1266.       P      : Node_Id;
  1267.       P_Name : Entity_Id;
  1268.       P_Spec : Node_Id;
  1269.  
  1270.    begin
  1271.       if Is_Child_Spec (Lib_Unit) then
  1272.          P := Unit (Parent_Spec (Lib_Unit));
  1273.          P_Name := Find_Lib_Unit_Entity (P);
  1274.          P_Spec := Specification (Get_Declaration_Node (P_Name));
  1275.  
  1276.          Remove_Context (Parent_Spec (Lib_Unit));
  1277.          End_Package_Scope (P_Name);
  1278.          Set_In_Package_Body (P_Name, False);
  1279.  
  1280.          --  This is the recursive call to remove the context of any
  1281.          --  higher level parent. This recursion ensures that all parents
  1282.          --  are removed in the reverse order of their installation.
  1283.  
  1284.          Remove_Parents (P);
  1285.       end if;
  1286.    end Remove_Parents;
  1287.  
  1288.    --------------------
  1289.    -- Remove_Context --
  1290.    --------------------
  1291.  
  1292.    procedure Remove_Context (N : Node_Id) is
  1293.       Lib_Unit  : constant Node_Id := Unit (N);
  1294.       Item      : Node_Id;
  1295.       Unit_Name : Entity_Id;
  1296.  
  1297.    begin
  1298.  
  1299.       --  Loop through context items looking for with clauses
  1300.  
  1301.       Item := First (Context_Items (N));
  1302.  
  1303.       while Present (Item) loop
  1304.  
  1305.          --  We are interested only in with clauses which got installed
  1306.          --  on entry, as indicated by their Context_Installed flag set
  1307.  
  1308.          if Nkind (Item) = N_With_Clause
  1309.             and then Context_Installed (Item)
  1310.          then
  1311.  
  1312.             --  Remove items from one with'ed unit
  1313.  
  1314.             Unit_Name := Entity (Name (Item));
  1315.             Remove_Withed_Unit (Unit_Name);
  1316.             Set_Context_Installed (Item, False);
  1317.  
  1318.          elsif Nkind (Item) = N_Use_Package_Clause then
  1319.             End_Use_Package (Item);
  1320.  
  1321.          elsif Nkind (Item) = N_Use_Type_Clause then
  1322.             End_Use_Type (Item);
  1323.          end if;
  1324.  
  1325.          Item := Next (Item);
  1326.       end loop;
  1327.  
  1328.       Remove_Parents (Lib_Unit);
  1329.  
  1330.    end Remove_Context;
  1331.  
  1332.    ------------------------
  1333.    -- Remove_Withed_Unit --
  1334.    ------------------------
  1335.  
  1336.    procedure Remove_Withed_Unit (Unit_Name : Entity_Id) is
  1337.       P : Entity_Id := Scope (Unit_Name);
  1338.  
  1339.    begin
  1340.  
  1341.       if Debug_Flag_I then
  1342.          Write_Str ("remove withed unit ");
  1343.          Write_Name (Chars (Unit_Name));
  1344.          Write_Eol;
  1345.       end if;
  1346.  
  1347.  
  1348.       if P /= Standard_Standard then
  1349.  
  1350.          --  Ultimate ancestor is not immediately visible any longer.
  1351.  
  1352.          while Scope (P) /= Standard_Standard loop
  1353.             P := Scope (P);
  1354.          end loop;
  1355.  
  1356.          --  Set_Is_Immediately_Visible (P, False);
  1357.          --  This cannot be done unconditionally, because the unit may
  1358.          --  be otherwise visible. It is necessary to know whether this
  1359.          --  withed unit was the one installed, or whether there is a
  1360.          --  separate with-clause that installed the ancestor. ???
  1361.  
  1362.       end if;
  1363.  
  1364.       Set_Is_Potentially_Use_Visible (Unit_Name, False);
  1365.       Set_Is_Immediately_Visible     (Unit_Name, False);
  1366.  
  1367.    end Remove_Withed_Unit;
  1368.  
  1369. end Sem_Ch10;
  1370.