[project @ 2000-10-24 17:09:44 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcModule.lhs
index a26f066..585f8af 100644 (file)
@@ -12,16 +12,16 @@ module TcModule (
 #include "HsVersions.h"
 
 import CmdLineOpts     ( DynFlag(..), DynFlags, opt_PprStyle_Debug )
-import HsSyn           ( HsModule(..), HsBinds(..), MonoBinds(..), HsDecl(..) )
+import HsSyn           ( HsBinds(..), MonoBinds(..), HsDecl(..) )
 import HsTypes         ( toHsType )
-import RnHsSyn         ( RenamedHsModule, RenamedHsDecl )
+import RnHsSyn         ( RenamedHsDecl )
 import TcHsSyn         ( TypecheckedMonoBinds, 
                          TypecheckedForeignDecl, TypecheckedRuleDecl,
                          zonkTopBinds, zonkForeignExports, zonkRules
                        )
 
 import TcMonad
-import Inst            ( emptyLIE, plusLIE )
+import Inst            ( plusLIE )
 import TcBinds         ( tcTopBinds )
 import TcClassDcl      ( tcClassDecls2, mkImplicitClassBinds )
 import TcDefaults      ( tcDefaults )
@@ -33,7 +33,7 @@ import TcRules                ( tcRules )
 import TcForeign       ( tcForeignImports, tcForeignExports )
 import TcIfaceSig      ( tcInterfaceSigs )
 import TcInstDcls      ( tcInstDecls1, tcInstDecls2 )
-import TcInstUtil      ( InstInfo(..) )
+import InstEnv         ( InstInfo(..) )
 import TcSimplify      ( tcSimplifyTop )
 import TcTyClsDecls    ( tcTyAndClassDecls )
 import TcTyDecls       ( mkImplicitDataBinds )
@@ -43,24 +43,21 @@ import Type         ( funResultTy, splitForAllTys )
 import Bag             ( isEmptyBag )
 import ErrUtils                ( printErrorsAndWarnings, dumpIfSet_dyn )
 import Id              ( idType, idName, idUnfolding )
-import Module           ( Module, moduleName, {-mkThisModule,-} plusModuleEnv )
-import Name            ( nameOccName, isLocallyDefined, isGlobalName,
-                         toRdrName, nameEnvElts, emptyNameEnv
+import Module           ( Module, moduleName, plusModuleEnv )
+import Name            ( Name, nameOccName, isLocallyDefined, isGlobalName,
+                         toRdrName, nameEnvElts, emptyNameEnv, lookupNameEnv
                        )
-import TyCon           ( TyCon, isDataTyCon, tyConName, tyConGenInfo )
+import TyCon           ( tyConGenInfo, isClassTyCon )
 import OccName         ( isSysOcc )
-import TyCon           ( TyCon, isClassTyCon )
-import Class           ( Class )
 import PrelNames       ( mAIN_Name, mainName )
-import UniqSupply       ( UniqSupply )
-import Maybes          ( maybeToBool )
+import Maybes          ( thenMaybe )
 import Util
-import BasicTypes       ( EP(..) )
-import Bag             ( Bag, isEmptyBag )
+import BasicTypes       ( EP(..), Fixity )
+import Bag             ( isEmptyBag )
 import Outputable
-import HscTypes                ( PersistentCompilerState(..), HomeSymbolTable, 
-                         PackageSymbolTable, DFunId, 
-                         TypeEnv, extendTypeEnv,
+import HscTypes                ( PersistentCompilerState(..), HomeSymbolTable, HomeIfaceTable,
+                         PackageSymbolTable, PackageIfaceTable, DFunId, ModIface(..),
+                         TypeEnv, extendTypeEnv, lookupTable,
                          TyThing(..), groupTyThings )
 import FiniteMap       ( FiniteMap, delFromFM, lookupWithDefaultFM )
 \end{code}
@@ -73,9 +70,11 @@ data TcResults
   = TcResults {
        tc_pcs     :: PersistentCompilerState,  -- Augmented with imported information,
                                                -- (but not stuff from this module)
-       tc_env     :: TypeEnv,                  -- The TypeEnv just for the stuff from this module
-       tc_insts   :: [DFunId],                 -- Instances, just for this module
-       tc_binds   :: TypecheckedMonoBinds,
+
+       -- All these fields have info *just for this module*
+       tc_env     :: TypeEnv,                  -- The top level TypeEnv
+       tc_insts   :: [DFunId],                 -- Instances
+       tc_binds   :: TypecheckedMonoBinds,     -- Bindings
        tc_fords   :: [TypecheckedForeignDecl], -- Foreign import & exports.
        tc_rules   :: [TypecheckedRuleDecl]     -- Transformation rules
     }
@@ -83,31 +82,47 @@ data TcResults
 ---------------
 typecheckModule
        :: DynFlags
+       -> Module
        -> PersistentCompilerState
-       -> HomeSymbolTable
-       -> RenamedHsModule
-       -> IO (Maybe (TcEnv, TcResults))
-
-typecheckModule dflags pcs hst (HsModule mod_name _ _ _ decls _ src_loc)
-  = do env <- initTcEnv global_symbol_table
-       (maybe_result, (errs,warns)) <- initTc dflags env src_loc tc_module
-       printErrorsAndWarnings (errs,warns)
-       printTcDump dflags maybe_result
-       if isEmptyBag errs then 
-          return Nothing 
-         else 
-          return maybe_result
+       -> HomeSymbolTable -> HomeIfaceTable
+       -> [RenamedHsDecl]
+       -> IO (Maybe TcResults)
+
+typecheckModule dflags this_mod pcs hst hit decls
+  = do env <- initTcEnv global_symbol_table
+
+        (maybe_result, (errs,warns)) <- initTc dflags env tc_module
+
+       let { maybe_tc_result :: Maybe TcResults ;
+             maybe_tc_result = case maybe_result of
+                                 Nothing    -> Nothing
+                                 Just (_,r) -> Just r }
+
+        printErrorsAndWarnings (errs,warns)
+        printTcDump dflags maybe_tc_result
+
+        if isEmptyBag errs then 
+             return Nothing 
+           else 
+             return maybe_tc_result
   where
-    this_mod           = panic "mkThisModule: unimp"  -- WAS: mkThisModule
     global_symbol_table = pcs_PST pcs `plusModuleEnv` hst
 
-    tc_module = fixTc (\ ~(unf_env ,_) -> tcModule pcs hst this_mod decls unf_env)
+    tc_module :: TcM (TcEnv, TcResults)
+    tc_module = fixTc (\ ~(unf_env ,_) -> tcModule pcs hst get_fixity this_mod decls unf_env)
+
+    pit = pcs_PIT pcs
+
+    get_fixity :: Name -> Maybe Fixity
+    get_fixity nm = lookupTable hit pit nm     `thenMaybe` \ iface ->
+                   lookupNameEnv (mi_fixities iface) nm
 \end{code}
 
 The internal monster:
 \begin{code}
 tcModule :: PersistentCompilerState
         -> HomeSymbolTable
+        -> (Name -> Maybe Fixity)
         -> Module
         -> [RenamedHsDecl]
         -> TcEnv               -- The knot-tied environment
@@ -120,14 +135,13 @@ tcModule :: PersistentCompilerState
   -- unf_env is also used to get the pragama info
   -- for imported dfuns and default methods
 
-tcModule pcs hst this_mod decls unf_env
+tcModule pcs hst get_fixity this_mod decls unf_env
   =             -- Type-check the type and class decls
     tcTyAndClassDecls unf_env decls            `thenTc` \ env ->
     tcSetEnv env                               $
     let
         classes       = tcEnvClasses env
         tycons        = tcEnvTyCons env        -- INCLUDES tycons derived from classes
-        local_classes = filter isLocallyDefined classes
         local_tycons  = [ tc | tc <- tycons,
                               isLocallyDefined tc,
                               not (isClassTyCon tc)
@@ -137,8 +151,9 @@ tcModule pcs hst this_mod decls unf_env
     in
     
        -- Typecheck the instance decls, includes deriving
-    tcInstDecls1 pcs hst unf_env this_mod 
-                local_tycons decls             `thenTc` \ (pcs_with_insts, inst_env, inst_info, deriv_binds) ->
+    tcInstDecls1 (pcs_insts pcs) (pcs_PRS pcs) 
+                hst unf_env get_fixity this_mod 
+                local_tycons decls             `thenTc` \ (new_pcs_insts, inst_env, local_inst_info, deriv_binds) ->
     tcSetInstEnv inst_env                      $
     
         -- Default declarations
@@ -189,9 +204,9 @@ tcModule pcs hst this_mod decls unf_env
     
        -- Second pass over class and instance declarations,
        -- to compile the bindings themselves.
-    tcInstDecls2  inst_info            `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
+    tcInstDecls2  local_inst_info      `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
     tcClassDecls2 decls                        `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
-    tcRules decls                      `thenNF_Tc` \ (lie_rules,     rules) ->
+    tcRules (pcs_rules pcs) decls      `thenNF_Tc` \ (new_pcs_rules, lie_rules, local_rules) ->
     
          -- Deal with constant or ambiguous InstIds.  How could
          -- there be ambiguous ones?  They can only arise if a
@@ -226,7 +241,7 @@ tcModule pcs hst this_mod decls unf_env
     tcSetEnv final_env                 $
        -- zonkTopBinds puts all the top-level Ids into the tcGEnv
     zonkForeignExports foe_decls       `thenNF_Tc` \ foe_decls' ->
-    zonkRules rules                    `thenNF_Tc` \ rules' ->
+    zonkRules local_rules              `thenNF_Tc` \ local_rules' ->
     
     
     let        groups :: FiniteMap Module TypeEnv
@@ -239,13 +254,16 @@ tcModule pcs hst this_mod decls unf_env
        new_pst = extendTypeEnv (pcs_PST pcs) (delFromFM groups this_mod)
 
        final_pcs :: PersistentCompilerState
-       final_pcs = pcs_with_insts {pcs_PST = new_pst}
+       final_pcs = pcs { pcs_PST   = new_pst,
+                         pcs_insts = new_pcs_insts,
+                         pcs_rules = new_pcs_rules
+                   }
     in  
-    returnTc (final_env, -- WAS: really_final_env, 
+    returnTc (final_env,
              TcResults { tc_pcs     = final_pcs,
                          tc_env     = local_type_env,
                          tc_binds   = all_binds', 
-                         tc_insts   = map iDFunId inst_info,
+                         tc_insts   = map iDFunId local_inst_info,
                          tc_fords   = foi_decls ++ foe_decls',
                          tc_rules   = rules'
                         })
@@ -279,7 +297,7 @@ noMainErr
 
 \begin{code}
 printTcDump dflags Nothing = return ()
-printTcDump dflags (Just (_,results))
+printTcDump dflags (Just results)
   = do dumpIfSet_dyn dflags Opt_D_dump_types 
                      "Type signatures" (dump_sigs results)
        dumpIfSet_dyn dflags Opt_D_dump_tc    
@@ -287,8 +305,8 @@ printTcDump dflags (Just (_,results))
 
 dump_tc results
   = vcat [ppr (tc_binds results),
-         pp_rules (tc_rules results) --,
---       ppr_gen_tycons (tc_tycons results)
+         pp_rules (tc_rules results),
+         ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
     ]
 
 dump_sigs results      -- Print type signatures