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 / lib.ads < prev    next >
Text File  |  1996-09-28  |  23KB  |  449 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                                  L I B                                   --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.53 $                             --
  10. --                                                                          --
  11. --        Copyright (c) 1992,1993,1994,1995 NYU, All Rights Reserved        --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. --  This package contains routines for accessing and outputting the library
  27. --  information. It contains the routine to load subsidiary units.
  28.  
  29. with Casing; use Casing;
  30. with Table;
  31. with Types;  use Types;
  32.  
  33. package Lib is
  34.  
  35.    --------------------------------------------
  36.    -- General Approach to Library Management --
  37.    --------------------------------------------
  38.  
  39.    --  As described in GNote #1, when a unit is compiled, all its subsidiary
  40.    --  units are recompiled, including the following:
  41.  
  42.    --    (a) Corresponding spec for a body
  43.    --    (b) Parent spec of a child library spec
  44.    --    (d) With'ed specs
  45.    --    (d) Parent body of a subunit
  46.    --    (e) Subunits corresponding to any specified stubs
  47.    --    (f) Bodies of inlined subprograms that are called
  48.    --    (g) Bodies of generic subprograms or packages that are instantiated
  49.    --    (h) Bodies of packages containing either of the above two items
  50.    --    (i) Specs and bodies of runtime units
  51.    --    (j) Parent specs for with'ed child library units
  52.  
  53.    --  If a unit is being compiled only for syntax checking, then no subsidiary
  54.    --  units are loaded, the the syntax check applies only to the main unit,
  55.    --  i.e. the one contained in the source submitted to the library.
  56.  
  57.    --  If a unit is being compiled for syntax and semantic checking, then only
  58.    --  cases (a)-(d) loads are performed, since the full semantic checking can
  59.    --  be carried out without needing (e)-(i) loads. In this case no object
  60.    --  file, or library information file, is generated, so the missing units
  61.    --  do not affect the results.
  62.  
  63.    --  Specifications of library subprograms, subunits, and generic specs
  64.    --  and bodies, can only be compiled in syntax/semantic checking mode,
  65.    --  since no code is ever generated directly for these units. In the case
  66.    --  of subunits, only the compilation of the ultimate parent unit generates
  67.    --  actual code. If a subunit is submitted to the compiler in syntax/
  68.    --  semantic checking mode, the parent (or parents in the nested case) are
  69.    --  semantically checked only up to the point of the corresponding stub.
  70.  
  71.    --  If code is being generated, then all the above units are required,
  72.    --  although the need for bodies of inlined procedures can be suppressed
  73.    --  by the use of a switch that sets the mode to ignore pragma Inline
  74.    --  statements.
  75.  
  76.    --  The two main sections of the front end, Par and Sem, are recursive.
  77.    --  Compilation proceeds unit by unit making recursive calls as necessary.
  78.    --  The process is controlled from the GNAT main program, which makes calls
  79.    --  to Par and Sem sequence for the main unit.
  80.  
  81.    --  Par parses the given unit, and then, after the parse is complete, uses
  82.    --  the Par.Load subprogram to load all its subsidiary units in categories
  83.    --  (a)-(d) above, installing pointers to the loaded units in the parse
  84.    --  tree, as described in a later section of this spec. If any of these
  85.    --  required units is missing, a fatal error is signalled, so that no
  86.    --  attempt is made to run Sem in such cases, since it is assumed that
  87.    --  too many cascaded errors would result, and the confusion would not
  88.    --  be helpful.
  89.  
  90.    --  Following the call to Par on the main unit, the entire tree of required
  91.    --  units is thus loaded, and Sem is called on the main unit. The parameter
  92.    --  passed to Sem is the unit to be analyzed. The visibility table, which
  93.    --  is a single global structure, starts out containing only the entries
  94.    --  for the visible entities in Standard. Every call to Sem establishes a
  95.    --  new scope stack table, pushing an entry for Standard on entry to provide
  96.    --  the proper initial scope environment.
  97.  
  98.    --  Sem first proceeds to perform semantic analysis on the currently loaded
  99.    --  units as follows:
  100.  
  101.    --    In the case of a body (case (a) above), Sem analyzes the corresponding
  102.    --    spec, using a recursive call to Sem. As is always expected to be the
  103.    --    case with calls to Sem, any entities installed in the visibility table
  104.    --    are removed on exit from Sem, so that these entities have to be
  105.    --    reinstalled on return to continue the analysis of the body which of
  106.    --    course needs visibility of these entities.
  107.    --
  108.    --    In the case of the parent of a child spec (case (b) above), a similar
  109.    --    call is made to Sem to analyze the parent. Again, on return, the
  110.    --    entities from the analyzed parent spec have to be installed in the
  111.    --    visibility table of the caller (the child unit), which must have
  112.    --    visibility to the entities in its parent spec.
  113.  
  114.    --    For with'ed specs (case (c) above), a recursive call to Sem is made
  115.    --    to analyze each spec in turn. After all the spec's have been analyzed,
  116.    --    but not till that point, the entities from all the with'ed units are
  117.    --    reinstalled in the visibility table so that the caller can proceed
  118.    --    with the analysis of the unit doing the with's with the necessary
  119.    --    entities made either potentially use visible or visible by selection
  120.    --    as needed.
  121.  
  122.    --    Case (d) arises when Sem is passed a subunit to analyze. This means
  123.    --    that the main unit is a subunit, and the unit passed to Sem is either
  124.    --    the main unit, or one of its ancestors that is still a subunit. Since
  125.    --    analysis must start at the top of the tree, Sem essentially cancels
  126.    --    the current call by immediately making a call to analyze the parent
  127.    --    (when this call is finished it immediately returns, so logically this
  128.    --    call is like a goto). The subunit will then be analyzed at the proper
  129.    --    time as described for the stub case. Note that we also turn off the
  130.    --    indication that code should be generated in this case, since the only
  131.    --    time we generate code for subunits is when compiling the main parent.
  132.  
  133.    --    Case (e), subunits corresponding to stubs, are handled as the stubs
  134.    --    are encountered. There are three sub-cases:
  135.  
  136.    --      If the subunit has already been loaded, then this means that the
  137.    --      main unit was a subunit, and we are back on our way down to it
  138.    --      after following the initial processing described for case (d).
  139.    --      In this case we analyze this particular subunit, as described
  140.    --      for the case where we are generating code, but when we get back
  141.    --      we are all done, since the rest of the parent is irrelevant. To
  142.    --      get out of the parent, we raise the exception Subunit_Found, which
  143.    --      is handled at the outer level of Sem.
  144.  
  145.    --      The cases where the subunit has not already been loaded correspond
  146.    --      to cases where the main unit was a parent. In this case the action
  147.    --      depends on whether or not we are generating code. If we are not
  148.    --      generating code, then this is the case where we can simply ignore
  149.    --      the subunit, since in checking mode we don't even want to insist
  150.    --      that the subunit exist, much less waste time checking it.
  151.  
  152.    --      If we are generating code, then we need to load and analyze
  153.    --      all subunits. This is achieved with a call to Lib.Load to load
  154.    --      and parse the unit, followed by processing that installs the
  155.    --      context clause of the subunit, analyzes the subunit, and then
  156.    --      removes the context clause (from the visibility chains of the
  157.    --      parent). Note that we do *not* do a recursive call to Sem in
  158.    --      this case, precisely because we need to do the analysis of the
  159.    --      subunit with the current visibility table and scope stack.
  160.  
  161.    --    Case (f) applies only to subprograms for which a pragma Inline is
  162.    --    given, providing that the compiler is operating in the mode where
  163.    --    pragma Inline's are activated. When the expander encounters a call
  164.    --    to such a subprogram, it loads the body of the subprogram if it has
  165.    --    not already been loaded, and calls Sem to process it.
  166.  
  167.    --    Case (g) is similar to case (f), except that the body of a generic
  168.    --    is unconditionally required, regardless of compiler mode settings.
  169.    --    As in the subprogram case, when the expander encounters a generic
  170.    --    instantiation, it loads the generic body of the subprogram if it
  171.    --    has not already been loaded, and calls Sem to process it.
  172.  
  173.    --    Case (h) arises when a package contains either an inlined subprogram
  174.    --    which is called, or a generic which is instantiated. In this case the
  175.    --    body of the package must be loaded and analyzed with a call to Sem.
  176.  
  177.    --    Case (i) is handled by adding implicit with clauses to the context
  178.    --    clauses of all units that potentially reference the relevant runtime
  179.    --    entities. Note that since we have the full set of units available,
  180.    --    the parser can always determine the set of runtime units that is
  181.    --    needed. These with clauses do not have associated use clauses, so
  182.    --    all references to the entities must be by selection. Once the with
  183.    --    clauses have been added, subsequent processing is as for normal
  184.    --    with clauses.
  185.  
  186.    --    Case (j) is also handled by adding appropriate implicit with clauses
  187.    --    to any unit that withs a child unit. Again there is no use clause,
  188.    --    and subsequent processing proceeds as for an explicit with clause.
  189.  
  190.    --  Sem thus completes the loading of all required units, except those
  191.    --  required for inline subprogram bodies or inlined generics. If any
  192.    --  of these load attempts fails, then the expander will not be called,
  193.    --  even if code was to be generated. If the load attempts all succeed
  194.    --  then the expander is called, though the attempt to generate code may
  195.    --  still fail if an error occurs during a load attempt for an inlined
  196.    --  body or a generic body.
  197.  
  198.    -----------------
  199.    -- Global Data --
  200.    -----------------
  201.  
  202.    Current_Sem_Unit : Unit_Number_Type;
  203.    --  Unit number of unit currently being analyzed/expanded. This is set when
  204.    --  ever a new unit is entered, saving and restoring the old value, so that
  205.    --  it always reflects the unit currently being analyzed.
  206.  
  207.    -----------------
  208.    -- Units Table --
  209.    -----------------
  210.  
  211.    --  The units table has an entry for each unit (source file) read in by the
  212.    --  current compilation. The table is indexed by the unit number value,
  213.    --  The first entry in the table, subscript Main_Unit, is for the main file.
  214.    --  Each entry in this units table contains the following data.
  215.  
  216.    --    Unit_File_Name
  217.    --      The name of the source file containing the unit. Set when the entry
  218.    --      is created by a call to Lib.Load, and then cannot be changed.
  219.  
  220.    --    Source_Index
  221.    --      The index in the source file table of the corresponding source file.
  222.    --      Set when the entry is created by a call to Lib.Load and then cannot
  223.    --      be changed.
  224.  
  225.    --    Expected_Unit
  226.    --      This is the expected unit name for a file other than the main unit,
  227.    --      since these are cases where we load the unit using Lib.Load and we
  228.    --      know the unit that is expected. It must be the same as Unit_Name
  229.    --      if it is set (see test in Par.Load). Expected_Unit is set to
  230.    --      No_Name for the main unit.
  231.  
  232.    --    Unit_Name
  233.    --      The name of the unit. Initialized to No_Name by Lib.Load, and then
  234.    --      set by the parser when the unit is parsed to the unit name actually
  235.    --      found in the file (which should, in the absence of errors) be the
  236.    --      same name as Expected_Unit.
  237.  
  238.    --    Cunit
  239.    --      Pointer to the N_Compilation_Unit node. Initially set to Empty by
  240.    --      Lib.Load, and then reset to the required node by the parser when
  241.    --      the unit is parsed.
  242.  
  243.    --    Cunit_Entity
  244.    --      Pointer to the entity node for the compilation unit. Initially set
  245.    --      to Empty by Lib.Load, and then reset to the required entity by the
  246.    --      parser when the unit is parsed.
  247.  
  248.    --    Fatal_Error
  249.    --      A flag that is initialized to False, and gets set to True if a fatal
  250.    --      error occurs during the processing of a unit. A fatal error is one
  251.    --      defined as serious enough to stop the next phase of the compiler
  252.    --      from running (i.e. fatal error during parsing stops semantics,
  253.    --      fatal error during semantics stops code generation). Note that
  254.    --      currently, errors of any kind cause Fatal_Error to be set, but
  255.    --      eventually perhaps only errors labeled as Fatal_Errors should be
  256.    --      this severe if we decide to try Sem on sources with minor errors.
  257.  
  258.    --    Generate_Code
  259.    --      This flag is set True for all units in the current file for which
  260.    --      code is to be generated. This includes the unit explicitly compiled,
  261.    --      together with its specification, and any subunits.
  262.  
  263.    --    Loading
  264.    --      A flag that is used to catch circular WITH dependencies. It is set
  265.    --      True when an entry is initially created in the file table, and set
  266.    --      False when the load is completed, or ends with an error.
  267.  
  268.    --    Main_Priority
  269.    --      This field is used to indicate the priority of a possible main
  270.    --      program, as set by a pragma Priority. A value of -1 indicates
  271.    --      that the default priority is to be used (and is also used for
  272.    --      entries that do not correspond to possible main programs).
  273.  
  274.    --    Serial_Number
  275.    --      This field holds a serial number used by New_Internal_Name to
  276.    --      generate unique temporary numbers on a unit by unit basis. The
  277.    --      only access to this field is via the Increment_Serial_Number
  278.    --      routine which increments the current value and returns it.
  279.  
  280.    --    Version
  281.    --      This field holds the version of the unit, which is stored
  282.    --      internally as a 32-bit unsigned number. Access to the version
  283.    --      information is not direct, but is done through the routines
  284.    --      described below. When a unit table entry is created, the version
  285.    --      reflects the version of the corresponding source file. Calls to
  286.    --      Update_Version are then made to reflect the versions of any units
  287.    --      on which this unit is semantically dependent.
  288.  
  289.    --  The units table is reset to empty at the start of the compilation of
  290.    --  each main unit by Lib.Initialize. Entries are then added by calls to
  291.    --  the Lib.Load procedure. The following subprograms are used to access
  292.    --  and modify entries in the Units table. Individual entries are accessed
  293.    --  using a unit number value which ranges from Main_Unit (the first entry,
  294.    --  which is always for the current main unit) to Last_Unit.
  295.  
  296.    Default_Main_Priority : constant Int := -1;
  297.    --  Value used in Main_Priority field to indicate default main priority
  298.  
  299.    function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type;
  300.    function Unit_Name      (U : Unit_Number_Type) return Unit_Name_Type;
  301.    function Expected_Unit  (U : Unit_Number_Type) return Unit_Name_Type;
  302.    function Source_Index   (U : Unit_Number_Type) return Source_File_Index;
  303.    function Cunit          (U : Unit_Number_Type) return Node_Id;
  304.    function Cunit_Entity   (U : Unit_Number_Type) return Entity_Id;
  305.    function Fatal_Error    (U : Unit_Number_Type) return Boolean;
  306.    function Generate_Code  (U : Unit_Number_Type) return Boolean;
  307.    function Loading        (U : Unit_Number_Type) return Boolean;
  308.    function Main_Priority  (U : Unit_Number_Type) return Int;
  309.    --  Get value of named field from given units table entry
  310.  
  311.    procedure Set_Unit_Name     (U : Unit_Number_Type; N : Unit_Name_Type);
  312.    procedure Set_Cunit         (U : Unit_Number_Type; N : Node_Id);
  313.    procedure Set_Cunit_Entity  (U : Unit_Number_Type; E : Entity_Id);
  314.    procedure Set_Fatal_Error   (U : Unit_Number_Type; B : Boolean := True);
  315.    procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True);
  316.    procedure Set_Loading       (U : Unit_Number_Type; B : Boolean := True);
  317.    procedure Set_Main_Priority (U : Unit_Number_Type; P : Int);
  318.    --  Set value of named field for given units table entry
  319.  
  320.    subtype Version_String is String (1 .. 8);
  321.  
  322.    function Version_Get (U : Unit_Number_Type) return Version_String;
  323.    --  Returns the version as a string with 8 hex digits (upper case letters)
  324.  
  325.    function Last_Unit return Unit_Number_Type;
  326.    --  Unit number of last allocated unit
  327.  
  328.    function Num_Units return Nat;
  329.    --  Number of units currently in unit table
  330.  
  331.    function Get_Sloc_Unit_Number (S : Source_Ptr) return Unit_Number_Type;
  332.    --  Return unit number of file identified by given source pointer value.
  333.    --  This call must always succeed, since any valid source pointer value
  334.    --  belongs to some previously loaded module.
  335.  
  336.    function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type;
  337.    --  Return unit number of the unit whose N_Compilation_Unit node is the
  338.    --  one passed as an argument. This must always succeed since the node
  339.    --  could not have been built without making a unit table entry.
  340.  
  341.    function Get_Cunit_Entity_Unit_Number
  342.      (E    : Entity_Id)
  343.       return Unit_Number_Type;
  344.    --  Return unit number of the unit whose compilation unit spec entity is
  345.    --  the one passed as an argument. This must always succeed since the
  346.    --  entity could not have been built without making a unit table entry.
  347.  
  348.    procedure Tree_Write;
  349.    --  Writes out internal tables to current tree file using Tree_Write
  350.  
  351.    procedure Tree_Read;
  352.    --  Initializes internal tables from current tree file using Tree_Read
  353.  
  354.    function Is_Loaded (Uname : Unit_Name_Type) return Boolean;
  355.    --  Determines if unit with given name is already loaded, i.e. there is
  356.    --  already an entry in the file table with this unit name for which the
  357.    --  corresponding file was found and parsed. Note that the Fatal_Error flag
  358.    --  of this entry must be checked before proceeding with further processing.
  359.  
  360.    procedure List;
  361.    --  Lists units in active library (i.e. generates output consisting of a
  362.    --  sorted listing of the units represented in File table, with the
  363.    --  exception of the main unit).
  364.  
  365. private
  366.    pragma Inline (Unit_File_Name);
  367.    pragma Inline (Unit_Name);
  368.    pragma Inline (Source_Index);
  369.    pragma Inline (Cunit);
  370.    pragma Inline (Cunit_Entity);
  371.    pragma Inline (Fatal_Error);
  372.    pragma Inline (Generate_Code);
  373.    pragma Inline (Loading);
  374.    pragma Inline (Main_Priority);
  375.    pragma Inline (Set_Unit_Name);
  376.    pragma Inline (Set_Cunit);
  377.    pragma Inline (Set_Cunit_Entity);
  378.    pragma Inline (Set_Fatal_Error);
  379.    pragma Inline (Set_Generate_Code);
  380.    pragma Inline (Set_Loading);
  381.    pragma Inline (Set_Main_Priority);
  382.  
  383.    type Version_Id is mod 2 ** 32;
  384.  
  385.    type Unit_Record is record
  386.       Unit_File_Name : File_Name_Type;
  387.       Unit_Name      : Unit_Name_Type;
  388.       Expected_Unit  : Unit_Name_Type;
  389.       Source_Index   : Source_File_Index;
  390.       Cunit          : Node_Id;
  391.       Cunit_Entity   : Node_Id;
  392.       Fatal_Error    : Boolean;
  393.       Generate_Code  : Boolean;
  394.       Loading        : Boolean;
  395.       Main_Priority  : Int;
  396.       Serial_Number  : Int;
  397.       Version        : Version_Id;
  398.    end record;
  399.  
  400.    package Units is new Table (
  401.      Table_Component_Type => Unit_Record,
  402.      Table_Index_Type     => Unit_Number_Type,
  403.      Table_Low_Bound      => Main_Unit,
  404.      Table_Initial        => 50,
  405.      Table_Increment      => 100,
  406.      Table_Name           => "Units");
  407.  
  408.    --  The following table stores strings from pragma Linker_Option lines
  409.  
  410.    package Linker_Option_Lines is new Table (
  411.      Table_Component_Type => String_Id,
  412.      Table_Index_Type     => Integer,
  413.      Table_Low_Bound      => 1,
  414.      Table_Initial        => 10,
  415.      Table_Increment      => 100,
  416.      Table_Name           => "Linker_Option_Lines");
  417.  
  418.    Load_Msg_Sloc : Source_Ptr;
  419.    --  Location for placing error messages (a token in the main source text)
  420.    --  This is set from Sloc (Enode) by Load only in the case where this Sloc
  421.    --  is in the main source file. This ensures that not found messages and
  422.    --  circular dependency messages reference the original with in this source.
  423.  
  424.    type Unit_Ref_Table is array (Pos range <>) of Unit_Number_Type;
  425.    --  Type to hold list of indirect references to unit number table
  426.  
  427.    --  The Load_Stack table contains a list of unit numbers (indexes into the
  428.    --  unit table) of units being loaded on a single dependency chain. The
  429.    --  First entry is the main unit. The second entry, if present is a unit
  430.    --  on which the first unit depends, etc. This stack is used to generate
  431.    --  error messages showing the dependency chain if a file is not found.
  432.    --  The Load function makes an entry in this table when it is called, and
  433.    --  removes the entry just before it returns.
  434.  
  435.    package Load_Stack is new Table (
  436.      Table_Component_Type => Unit_Number_Type,
  437.      Table_Index_Type     => Nat,
  438.      Table_Low_Bound      => 0,
  439.      Table_Initial        => 10,
  440.      Table_Increment      => 100,
  441.      Table_Name           => "Lib.Load_Stack");
  442.  
  443.    procedure Sort (Tbl : in out Unit_Ref_Table);
  444.    --  This procedure sorts the given unit reference table in order of
  445.    --  ascending unit names, where the ordering relation is as described
  446.    --  by the comparison routines provided by package Uname.
  447.  
  448. end Lib;
  449.