[project @ 2000-10-17 12:48:34 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / HscTypes.lhs
index 2138d48..9a6dfd1 100644 (file)
@@ -6,18 +6,25 @@
 \begin{code}
 module HscTypes ( 
        ModDetails(..), GlobalSymbolTable, 
+       HomeSymbolTable, PackageSymbolTable,
 
-       TyThing(..), lookupTypeEnv,
+       TyThing(..), lookupTypeEnv, lookupFixityEnv,
 
        WhetherHasOrphans, ImportVersion, ExportItem,
        PersistentRenamerState(..), IsBootInterface, Avails, DeclsMap,
-       IfaceInsts, IfaceRules, DeprecationEnv, OrigNameEnv, AvailEnv,
+       IfaceInsts, IfaceRules, DeprecationEnv, OrigNameEnv, 
+       AvailEnv, AvailInfo, GenAvailInfo(..),
+       PersistentCompilerState(..),
 
-       InstEnv, 
+       InstEnv, ClsInstEnv, DFunId,
 
-               -- Provenance
+       GlobalRdrEnv, RdrAvailInfo,
+
+       CompResult(..), HscResult(..),
+
+       -- Provenance
        Provenance(..), ImportReason(..), PrintUnqualified,
-        pprProvenance, hasBetterProv
+        pprNameProvenance, hasBetterProv
 
     ) where
 
@@ -25,7 +32,8 @@ module HscTypes (
 
 import Name            ( Name, NameEnv, NamedThing,
                          unitNameEnv, extendNameEnv, plusNameEnv, 
-                         lookupNameEnv, emptyNameEnv, getName, nameModule )
+                         lookupNameEnv, emptyNameEnv, getName, nameModule,
+                         nameSrcLoc )
 import Module          ( Module, ModuleName,
                          extendModuleEnv, lookupModuleEnv )
 import Class           ( Class )
@@ -36,22 +44,23 @@ import UniqFM               ( UniqFM )
 import FiniteMap       ( FiniteMap, emptyFM, addToFM, lookupFM, foldFM )
 import Bag             ( Bag )
 import Id              ( Id )
-import VarEnv          ( IdEnv )
+import VarEnv          ( IdEnv, emptyVarEnv )
 import BasicTypes      ( Version, Fixity, defaultFixity )
 import TyCon           ( TyCon )
 import ErrUtils                ( ErrMsg, WarnMsg )
 import CmLink          ( Linkable )
 import RdrHsSyn                ( RdrNameInstDecl, RdrNameRuleDecl, RdrNameHsDecl,
                          RdrNameDeprecation, RdrNameFixitySig )
+import InterpSyn       ( UnlinkedIBind )
 import UniqSupply      ( UniqSupply )
 import HsDecls         ( DeprecTxt )
 import CoreSyn         ( CoreRule )
 import NameSet         ( NameSet )
 import Type            ( Type )
 import VarSet          ( TyVarSet )
-import {-# SOURCE #-} 
-       TcInstUtil ( emptyInstEnv )
 import Panic           ( panic )
+import Outputable
+import SrcLoc          ( SrcLoc, isGoodSrcLoc )
 \end{code}
 
 %************************************************************************
@@ -60,34 +69,61 @@ import Panic                ( panic )
 %*                                                                     *
 %************************************************************************
 
-A @ModDetails@ summarises everything we know about a compiled module.
+A @ModIface@ plus a @ModDetails@ summarises everything we know 
+about a compiled module.  The @ModIface@ is the stuff *before* linking,
+and can be written out to an interface file.  The @ModDetails@ is after
+linking; it is the "linked" form of the mi_decls field.
 
 \begin{code}
 data ModDetails
    = ModDetails {
-       moduleId      :: Module,
-        moduleExports :: Avails,               -- What it exports
-       mdVersion     :: VersionInfo,
-        moduleEnv     :: GlobalRdrEnv,         -- Its top level environment
+        md_module   :: Module,                 -- Complete with package info
+        md_version  :: VersionInfo,            -- Module version number
+        md_orphan   :: WhetherHasOrphans,       -- Whether this module has orphans
+        md_usages   :: [ImportVersion Name],   -- Usages
+
+        md_exports  :: Avails,                 -- What it exports
+        md_globals  :: GlobalRdrEnv,           -- Its top level environment
 
-        fixityEnv     :: NameEnv Fixity,
-       deprecEnv     :: NameEnv DeprecTxt,
-        typeEnv       :: TypeEnv,
+        md_fixities :: NameEnv Fixity,         -- Fixities
+       md_deprecs  :: NameEnv DeprecTxt,       -- Deprecations
 
-        mdInsts       :: [DFunId],     -- Dfun-ids for the instances in this module
-        mdRules       :: RuleEnv       -- Domain may include Id from other modules
+       -- The next three fields are created by the typechecker
+        md_types    :: TypeEnv,
+        md_insts    :: [DFunId],       -- Dfun-ids for the instances in this module
+        md_rules    :: RuleEnv         -- Domain may include Ids from other modules
      }
 
+-- ModIFace is nearly the same as RnMonad.ParsedIface.
+-- Right now it's identical :)
+data ModIFace 
+   = ModIFace {
+        mi_mod       :: Module,                   -- Complete with package info
+        mi_vers      :: Version,                  -- Module version number
+        mi_orphan    :: WhetherHasOrphans,        -- Whether this module has orphans
+        mi_usages    :: [ImportVersion OccName],  -- Usages
+        mi_exports   :: [ExportItem],             -- Exports
+        mi_insts     :: [RdrNameInstDecl],        -- Local instance declarations
+        mi_decls     :: [(Version, RdrNameHsDecl)],    -- Local definitions
+        mi_fixity    :: (Version, [RdrNameFixitySig]), -- Local fixity declarations, 
+                                                       -- with their version
+        mi_rules     :: (Version, [RdrNameRuleDecl]),  -- Rules, with their version
+        mi_deprecs   :: [RdrNameDeprecation]           -- Deprecations
+     }
+
+\end{code}
+
+\begin{code}
 emptyModDetails :: Module -> ModDetails
 emptyModDetails mod
-  = ModDetails { moduleId      = mod,
-                moduleExports = [],
-                moduleEnv     = emptyRdrEnv,
-                fixityEnv     = emptyNameEnv,
-                deprecEnv     = emptyNameEnv,
-                typeEnv       = emptyNameEnv,
-                mdInsts       = [],
-                mdRules       = emptyRuleEnv
+  = ModDetails { md_module   = mod,
+                md_exports  = [],
+                md_globals  = emptyRdrEnv,
+                md_fixities = emptyNameEnv,
+                md_deprecs  = emptyNameEnv,
+                md_types    = emptyNameEnv,
+                md_insts    = [],
+                md_rules    = emptyRuleEnv
     }          
 \end{code}
 
@@ -108,7 +144,7 @@ lookupFixityEnv :: SymbolTable -> Name -> Maybe Fixity
 lookupFixityEnv tbl name
   = case lookupModuleEnv tbl (nameModule name) of
        Nothing      -> Nothing
-       Just details -> lookupNameEnv (fixityEnv details) name
+       Just details -> lookupNameEnv (md_fixities details) name
 \end{code}
 
 
@@ -136,7 +172,7 @@ instance NamedThing TyThing where
 lookupTypeEnv :: SymbolTable -> Name -> Maybe TyThing
 lookupTypeEnv tbl name
   = case lookupModuleEnv tbl (nameModule name) of
-       Just details -> lookupNameEnv (typeEnv details) name
+       Just details -> lookupNameEnv (md_types details) name
        Nothing      -> Nothing
 
 
@@ -163,8 +199,8 @@ extendTypeEnv tbl things
        where
          new_details 
              = case lookupModuleEnv tbl mod of
-                  Nothing      -> (emptyModDetails mod) {typeEnv = type_env}
-                  Just details -> details {typeEnv = typeEnv details 
+                  Nothing      -> (emptyModDetails mod) {md_types = type_env}
+                  Just details -> details {md_types = md_types details 
                                                      `plusNameEnv` type_env}
 \end{code}
 
@@ -181,8 +217,8 @@ but they are mostly elaborated elsewhere
 \begin{code}
 data VersionInfo 
   = VersionInfo {
-       modVers :: Version,
-       fixVers :: Version,
+       modVers  :: Version,
+       fixVers  :: Version,
        ruleVers :: Version,
        declVers :: NameEnv Version
     }
@@ -224,23 +260,6 @@ type AvailEnv        = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contain
 %************************************************************************
 
 \begin{code}
--- ModIFace is nearly the same as RnMonad.ParsedIface.
--- Right now it's identical :)
-data ModIFace 
-   = ModIFace {
-        mi_mod       :: Module,                   -- Complete with package info
-        mi_vers      :: Version,                  -- Module version number
-        mi_orphan    :: WhetherHasOrphans,        -- Whether this module has orphans
-        mi_usages    :: [ImportVersion OccName],  -- Usages
-        mi_exports   :: [ExportItem],             -- Exports
-        mi_insts     :: [RdrNameInstDecl],        -- Local instance declarations
-        mi_decls     :: [(Version, RdrNameHsDecl)],    -- Local definitions
-        mi_fixity    :: (Version, [RdrNameFixitySig]), -- Local fixity declarations, 
-                                                       -- with their version
-        mi_rules     :: (Version, [RdrNameRuleDecl]),  -- Rules, with their version
-        mi_deprecs   :: [RdrNameDeprecation]           -- Deprecations
-     }
-
 type ExportItem                 = (ModuleName, [RdrAvailInfo])
 
 type ImportVersion name  = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
@@ -289,13 +308,13 @@ data WhatsImported name  = NothingAtAll                           -- The module is below us in the
 \begin{code}
 data PersistentCompilerState 
    = PCS {
-        pcsPST :: PackageSymbolTable,          -- Domain = non-home-package modules
-                                               --   except that the InstEnv components is empty
-       pcsInsts :: InstEnv,                    -- The total InstEnv accumulated from all
-                                               --   the non-home-package modules
-       pcsRules :: RuleEnv,                    -- Ditto RuleEnv
+        pcs_PST :: PackageSymbolTable, -- Domain = non-home-package modules
+                                       --   except that the InstEnv components is empty
+       pcs_insts :: InstEnv,           -- The total InstEnv accumulated from all
+                                       --   the non-home-package modules
+       pcs_rules :: RuleEnv,           -- Ditto RuleEnv
 
-        pcsPRS :: PersistentRenamerState
+        pcs_PRS :: PersistentRenamerState
      }
 \end{code}
 
@@ -306,7 +325,7 @@ It contains:
   * A name supply, which deals with allocating unique names to
     (Module,OccName) original names, 
  
-  * An accumulated InstEnv from all the modules in pcsPST
+  * An accumulated InstEnv from all the modules in pcs_PST
     The point is that we don't want to keep recreating it whenever
     we compile a new module.  The InstEnv component of pcPST is empty.
     (This means we might "see" instances that we shouldn't "really" see;
@@ -388,18 +407,18 @@ data CompResult
 -- generate Linkables.
 
 data HscResult
-   = HscOK   ModDetails                -- new details (HomeSymbolTable additions)
-            (Maybe ModIFace)           -- new iface (if any compilation was done)
-            (Maybe String)             -- generated stub_h
-            (Maybe String)             -- generated stub_c
-             PersistentCompilerState   -- updated PCS
-             [SDoc]                    -- warnings
-
-   | HscErrs PersistentCompilerState   -- updated PCS
-             [SDoc]                    -- errors
-             [SDoc]                    -- warnings
-
-       
+   = HscOK   ModDetails             -- new details (HomeSymbolTable additions)
+            (Maybe ModIFace)        -- new iface (if any compilation was done)
+            (Maybe String)          -- generated stub_h filename (in /tmp)
+            (Maybe String)          -- generated stub_c filename (in /tmp)
+            (Maybe [UnlinkedIBind]) -- interpreted code, if any
+             PersistentCompilerState -- updated PCS
+             (Bag WarnMsg)             -- warnings
+
+   | HscErrs PersistentCompilerState -- updated PCS
+             (Bag ErrMsg)              -- errors
+             (Bag WarnMsg)             -- warnings
+
 -- These two are only here to avoid recursion between CmCompile and
 -- CompManager.  They really ought to be in the latter.
 type ModuleEnv a = UniqFM a   -- Domain is Module
@@ -434,6 +453,16 @@ data Provenance
        ImportReason
        PrintUnqualified
 
+{-
+Moved here from Name.
+pp_prov (LocalDef _ Exported)          = char 'x'
+pp_prov (LocalDef _ NotExported)       = char 'l'
+pp_prov (NonLocalDef ImplicitImport _) = char 'j'
+pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I'      -- Imported by name
+pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i'      -- Imported by ..
+pp_prov SystemProv                    = char 's'
+-}
+
 data ImportReason
   = UserImport Module SrcLoc Bool      -- Imported from module M on line L
                                        -- Note the M may well not be the defining module
@@ -466,8 +495,8 @@ hasBetterProv (NonLocalDef (UserImport _ _ _   ) _) (NonLocalDef ImplicitImport
 hasBetterProv _                                            _                              = False
 
 pprNameProvenance :: Name -> Provenance -> SDoc
-pprProvenance name LocalDef           = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
-pprProvenance name (NonLocalDef why _) = sep [ppr_reason why, 
+pprNameProvenance name LocalDef               = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
+pprNameProvenance name (NonLocalDef why _) = sep [ppr_reason why, 
                                              nest 2 (parens (ppr_defn (nameSrcLoc name)))]
 
 ppr_reason ImplicitImport        = ptext SLIT("implicitly imported")