[project @ 2001-10-18 16:29:12 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcModule.lhs
index 044118b..e799f09 100644 (file)
@@ -6,6 +6,7 @@
 \begin{code}
 module TcModule (
        typecheckModule, typecheckIface, typecheckStmt, typecheckExpr,
+       typecheckExtraDecls,
        TcResults(..)
     ) where
 
@@ -16,7 +17,7 @@ import HsSyn          ( HsBinds(..), MonoBinds(..), HsDecl(..), HsExpr(..),
                          Stmt(..), InPat(..), HsMatchContext(..), HsDoContext(..), RuleDecl(..),
                          isIfaceRuleDecl, nullBinds, andMonoBindList, mkSimpleMatch, placeHolderType
                        )
-import PrelNames       ( SyntaxMap, mAIN_Name, mainName, ioTyConName, printName,
+import PrelNames       ( mAIN_Name, mainName, ioTyConName, printName,
                          returnIOName, bindIOName, failIOName, 
                          itName
                        )
@@ -60,7 +61,7 @@ import ErrUtils               ( printErrorsAndWarnings, errorsFound,
 import Id              ( Id, idType, idUnfolding )
 import Module           ( Module, moduleName )
 import Name            ( Name )
-import NameEnv         ( nameEnvElts, lookupNameEnv )
+import NameEnv         ( lookupNameEnv )
 import TyCon           ( tyConGenInfo )
 import BasicTypes       ( EP(..), Fixity, RecFlag(..) )
 import SrcLoc          ( noSrcLoc )
@@ -69,8 +70,8 @@ import IO             ( stdout )
 import HscTypes                ( PersistentCompilerState(..), HomeSymbolTable, 
                          PackageTypeEnv, ModIface(..),
                          ModDetails(..), DFunId,
-                         TypeEnv, extendTypeEnvList, 
-                         TyThing(..), implicitTyThingIds, 
+                         TypeEnv, extendTypeEnvList, typeEnvTyCons, typeEnvElts,
+                         TyThing(..), 
                          mkTypeEnv
                        )
 \end{code}
@@ -91,8 +92,7 @@ typecheckStmt
    -> PrintUnqualified    -- For error printing
    -> Module              -- Is this really needed
    -> [Name]              -- Names bound by the Stmt (empty for expressions)
-   -> (SyntaxMap,
-       RenamedStmt,       -- The stmt itself
+   -> (RenamedStmt,       -- The stmt itself
        [RenamedHsDecl])           -- Plus extra decls it sucked in from interface files
    -> IO (Maybe (PersistentCompilerState, 
                 TypecheckedHsExpr, 
@@ -101,8 +101,8 @@ typecheckStmt
                -- The returned [Id] is the same as the input except for
                -- ExprStmt, in which case the returned [Name] is [itName]
 
-typecheckStmt dflags pcs hst ic_type_env unqual this_mod names (syn_map, stmt, iface_decls)
-  = typecheck dflags syn_map pcs hst unqual $
+typecheckStmt dflags pcs hst ic_type_env unqual this_mod names (stmt, iface_decls)
+  = typecheck dflags pcs hst unqual $
 
         -- use the default default settings, i.e. [Integer, Double]
     tcSetDefaultTys defaultDefaultTys $
@@ -235,16 +235,15 @@ typecheckExpr :: DynFlags
              -> TypeEnv           -- The interactive context's type envt 
              -> PrintUnqualified       -- For error printing
              -> Module
-             -> (SyntaxMap,
-                 RenamedHsExpr,        -- The expression itself
+             -> (RenamedHsExpr,        -- The expression itself
                  [RenamedHsDecl])      -- Plus extra decls it sucked in from interface files
              -> IO (Maybe (PersistentCompilerState, 
                            TypecheckedHsExpr, 
                            [Id],       -- always empty (matches typecheckStmt)
                            Type))
 
-typecheckExpr dflags pcs hst ic_type_env unqual this_mod (syn_map, expr, decls)
-  = typecheck dflags syn_map pcs hst unqual $
+typecheckExpr dflags pcs hst ic_type_env unqual this_mod (expr, decls)
+  = typecheck dflags pcs hst unqual $
 
         -- use the default default settings, i.e. [Integer, Double]
     tcSetDefaultTys defaultDefaultTys $
@@ -291,6 +290,33 @@ typecheckExpr dflags pcs hst ic_type_env unqual this_mod (syn_map, expr, decls)
 
 %************************************************************************
 %*                                                                     *
+\subsection{Typechecking extra declarations}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+typecheckExtraDecls 
+   :: DynFlags
+   -> PersistentCompilerState
+   -> HomeSymbolTable
+   -> PrintUnqualified    -- For error printing
+   -> Module              -- Is this really needed
+   -> [RenamedHsDecl]     -- extra decls sucked in from interface files
+   -> IO (Maybe PersistentCompilerState)
+
+typecheckExtraDecls  dflags pcs hst unqual this_mod decls
+ = typecheck dflags pcs hst unqual $
+     fixTc (\ ~(unf_env, _, _, _, _) ->
+         tcImports unf_env pcs hst get_fixity this_mod decls
+     ) `thenTc` \ (env, new_pcs, local_inst_info, deriv_binds, local_rules) ->
+     ASSERT( null local_inst_info && nullBinds deriv_binds && null local_rules )
+     returnTc new_pcs
+ where
+    get_fixity n = pprPanic "typecheckExpr" (ppr n)
+\end{code}
+
+%************************************************************************
+%*                                                                     *
 \subsection{Typechecking a module}
 %*                                                                     *
 %************************************************************************
@@ -302,7 +328,7 @@ typecheckModule
        -> HomeSymbolTable
        -> ModIface             -- Iface for this module
        -> PrintUnqualified     -- For error printing
-       -> (SyntaxMap, [RenamedHsDecl])
+       -> [RenamedHsDecl]
        -> IO (Maybe (PersistentCompilerState, TcResults))
                        -- The new PCS is Augmented with imported information,
                                                -- (but not stuff from this module)
@@ -318,8 +344,8 @@ data TcResults
     }
 
 
-typecheckModule dflags pcs hst mod_iface unqual (syn_map, decls)
-  = do { maybe_tc_result <- typecheck dflags syn_map pcs hst unqual $
+typecheckModule dflags pcs hst mod_iface unqual decls
+  = do { maybe_tc_result <- typecheck dflags pcs hst unqual $
                             tcModule pcs hst get_fixity this_mod decls
        ; printTcDump dflags unqual maybe_tc_result
        ; return maybe_tc_result }
@@ -421,17 +447,7 @@ tcModule pcs hst get_fixity this_mod decls
        zonkRules more_local_rules      `thenNF_Tc` \ more_local_rules' ->
        
        
-       let     local_things = filter (isLocalThing this_mod) (nameEnvElts (getTcGEnv final_env))
-       
-               -- Create any necessary "implicit" bindings (data constructors etc)
-               -- Should we create bindings for dictionary constructors?
-               -- They are always fully applied, and the bindings are just there
-               -- to support partial applications. But it's easier to let them through.
-               implicit_binds = andMonoBindList [ CoreMonoBind id (unfoldingTemplate unf)
-                                                | id <- implicitTyThingIds local_things
-                                                , let unf = idUnfolding id
-                                                , hasUnfolding unf
-                                                ]
+       let     local_things = filter (isLocalThing this_mod) (typeEnvElts (getTcGEnv final_env))
        
                local_type_env :: TypeEnv
                local_type_env = mkTypeEnv local_things
@@ -443,7 +459,7 @@ tcModule pcs hst get_fixity this_mod decls
                  new_pcs,
                  TcResults { tc_env     = local_type_env,
                              tc_insts   = map iDFunId local_insts,
-                             tc_binds   = implicit_binds `AndMonoBinds` all_binds', 
+                             tc_binds   = all_binds', 
                              tc_fords   = foi_decls ++ foe_decls',
                              tc_rules   = all_local_rules
                            }
@@ -469,13 +485,13 @@ typecheckIface
        -> PersistentCompilerState
        -> HomeSymbolTable
        -> ModIface             -- Iface for this module (just module & fixities)
-       -> (SyntaxMap, [RenamedHsDecl])
+       -> [RenamedHsDecl]
        -> IO (Maybe (PersistentCompilerState, ModDetails))
                        -- The new PCS is Augmented with imported information,
                        -- (but not stuff from this module).
 
-typecheckIface dflags pcs hst mod_iface (syn_map, decls)
-  = do { maybe_tc_stuff <- typecheck dflags syn_map pcs hst alwaysQualify $
+typecheckIface dflags pcs hst mod_iface decls
+  = do { maybe_tc_stuff <- typecheck dflags pcs hst alwaysQualify $
                            tcIfaceImports pcs hst get_fixity this_mod decls
        ; printIfaceDump dflags maybe_tc_stuff
        ; return maybe_tc_stuff }
@@ -493,7 +509,7 @@ typecheckIface dflags pcs hst mod_iface (syn_map, decls)
                            deriv_binds, local_rules) ->
          ASSERT(nullBinds deriv_binds)
          let 
-             local_things = filter (isLocalThing this_mod) (nameEnvElts (getTcGEnv env))
+             local_things = filter (isLocalThing this_mod) (typeEnvElts (getTcGEnv env))
 
              mod_details = ModDetails { md_types = mkTypeEnv local_things,
                                         md_insts = map iDFunId local_inst_info,
@@ -532,9 +548,9 @@ tcImports unf_env pcs hst get_fixity this_mod decls
        -- tcImports recovers internally, but if anything gave rise to
        -- an error we'd better stop now, to avoid a cascade
        
-    traceTc (text "Tc1")                       `thenNF_Tc_`
-    tcTyAndClassDecls unf_env tycl_decls       `thenTc` \ env ->
-    tcSetEnv env                               $
+    traceTc (text "Tc1")                               `thenNF_Tc_`
+    tcTyAndClassDecls unf_env this_mod tycl_decls      `thenTc` \ env ->
+    tcSetEnv env                                       $
     
        -- Typecheck the instance decls, includes deriving
     traceTc (text "Tc2")       `thenNF_Tc_`
@@ -554,14 +570,14 @@ tcImports unf_env pcs hst get_fixity this_mod decls
     tcExtendGlobalValEnv sig_ids               $
     
     
-    tcIfaceRules (pcs_rules pcs) this_mod iface_rules  `thenNF_Tc` \ (new_pcs_rules, local_rules) ->
+    tcIfaceRules unf_env (pcs_rules pcs) this_mod iface_rules  `thenNF_Tc` \ (new_pcs_rules, local_rules) ->
        -- When relinking this module from its interface-file decls
        -- we'll have IfaceRules that are in fact local to this module
        -- That's the reason we we get any local_rules out here
     
     tcGetEnv                                           `thenTc` \ unf_env ->
     let
-        all_things = nameEnvElts (getTcGEnv unf_env)
+        all_things = typeEnvElts (getTcGEnv unf_env)
     
          -- sometimes we're compiling in the context of a package module
          -- (on the GHCi command line, for example).  In this case, we
@@ -645,16 +661,15 @@ noMainErr = hsep [ptext SLIT("Module") <+> quotes (ppr mAIN_Name),
 
 \begin{code}
 typecheck :: DynFlags
-         -> SyntaxMap
          -> PersistentCompilerState
          -> HomeSymbolTable
          -> PrintUnqualified   -- For error printing
          -> TcM r
          -> IO (Maybe r)
 
-typecheck dflags syn_map pcs hst unqual thing_inside 
+typecheck dflags pcs hst unqual thing_inside 
  = do  { showPass dflags "Typechecker";
-       ; env <- initTcEnv syn_map hst (pcs_PTE pcs)
+       ; env <- initTcEnv hst (pcs_PTE pcs)
 
        ; (maybe_tc_result, errs) <- initTc dflags env thing_inside
 
@@ -697,7 +712,7 @@ dump_tc_iface dflags results
          ppr_rules (tc_rules results),
 
          if dopt Opt_Generics dflags then
-               ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
+               ppr_gen_tycons (typeEnvTyCons (tc_env results))
          else 
                empty
     ]