[project @ 2000-10-24 17:09:44 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcModule.lhs
index a47d783..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 )
@@ -47,16 +47,13 @@ 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, thenMaybe )
+import Maybes          ( thenMaybe )
 import Util
 import BasicTypes       ( EP(..), Fixity )
-import Bag             ( Bag, isEmptyBag )
+import Bag             ( isEmptyBag )
 import Outputable
 import HscTypes                ( PersistentCompilerState(..), HomeSymbolTable, HomeIfaceTable,
                          PackageSymbolTable, PackageIfaceTable, DFunId, ModIface(..),
@@ -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
     }
@@ -85,26 +84,34 @@ typecheckModule
        :: DynFlags
        -> Module
        -> PersistentCompilerState
-       -> HomeSymbolTable
-       -> HomeIfaceTable
-       -> PackageIfaceTable
-       -> RenamedHsModule
-       -> IO (Maybe (TcEnv, TcResults))
-
-typecheckModule dflags this_mod pcs hst hit pit (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
     global_symbol_table = pcs_PST pcs `plusModuleEnv` hst
 
-    tc_module = fixTc (\ ~(unf_env ,_) 
-                        -> tcModule pcs hst get_fixity 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 ->
@@ -135,7 +142,6 @@ tcModule pcs hst get_fixity this_mod decls unf_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)
@@ -145,8 +151,9 @@ tcModule pcs hst get_fixity this_mod decls unf_env
     in
     
        -- Typecheck the instance decls, includes deriving
-    tcInstDecls1 pcs hst unf_env get_fixity 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
@@ -197,9 +204,9 @@ tcModule pcs hst get_fixity 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
@@ -234,7 +241,7 @@ tcModule pcs hst get_fixity 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
@@ -247,13 +254,16 @@ tcModule pcs hst get_fixity 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'
                         })
@@ -287,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    
@@ -295,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