module IOEnv
) where
+#include "HsVersions.h"
+
import TcRnTypes -- Re-export all
import IOEnv -- Re-export all
import FastString
import Panic
import Util
-
+
import System.IO
import Data.IORef
-import Control.Exception
import Control.Monad
\end{code}
initTc hsc_env hsc_src keep_rn_syntax mod do_this
= do { errs_var <- newIORef (emptyBag, emptyBag) ;
tvs_var <- newIORef emptyVarSet ;
- type_env_var <- newIORef emptyNameEnv ;
dfuns_var <- newIORef emptyNameSet ;
keep_var <- newIORef emptyNameSet ;
th_var <- newIORef False ;
dfun_n_var <- newIORef 1 ;
+ type_env_var <- case hsc_type_env_var hsc_env of {
+ Just (_mod, te_var) -> return te_var ;
+ Nothing -> newIORef emptyNameEnv } ;
let {
maybe_rn_syntax empty_val
| keep_rn_syntax = Just empty_val
tcg_src = hsc_src,
tcg_rdr_env = hsc_global_rdr_env hsc_env,
tcg_fix_env = emptyNameEnv,
- tcg_field_env = emptyNameEnv,
+ tcg_field_env = RecFields emptyNameEnv emptyNameSet,
tcg_default = Nothing,
tcg_type_env = hsc_global_type_env hsc_env,
tcg_type_env_var = type_env_var,
tcg_rn_decls = maybe_rn_syntax emptyRnGroup,
tcg_binds = emptyLHsBinds,
- tcg_deprecs = NoDeprecs,
+ tcg_warns = NoWarnings,
+ tcg_anns = [],
tcg_insts = [],
tcg_fam_insts= [],
tcg_rules = [],
tcl_arrow_ctxt = NoArrowCtxt,
tcl_env = emptyNameEnv,
tcl_tyvars = tvs_var,
- tcl_lie = panic "initTc:LIE" -- LIE only valid inside a getLIE
+ tcl_lie = panic "initTc:LIE", -- only valid inside getLIE
+ tcl_tybinds = panic "initTc:tybinds"
+ -- only valid inside a getTyBinds
} ;
} ;
:: HscEnv
-> Module
-> TcM r
- -> IO (Maybe r)
+ -> IO (Messages, Maybe r)
initTcPrintErrors env mod todo = do
(msgs, res) <- initTc env HsSrcFile False mod todo
- printErrorsAndWarnings (hsc_dflags env) msgs
- return res
+ return (msgs, res)
\end{code}
%************************************************************************
newSysLocalIds fs tys
= do { us <- newUniqueSupply
; return (zipWith (mkSysLocal fs) (uniqsFromSupply us) tys) }
+
+instance MonadUnique (IOEnv (Env gbl lcl)) where
+ getUniqueM = newUnique
+ getUniqueSupplyM = newUniqueSupply
\end{code}
dumpTcRn :: SDoc -> TcRn ()
dumpTcRn doc = do { rdr_env <- getGlobalRdrEnv ;
dflags <- getDOpts ;
- liftIO (printForUser stderr (mkPrintUnqualified dflags rdr_env) doc) }
+ liftIO (printForUser stderr (mkPrintUnqualified dflags rdr_env) doc) }
+
+debugDumpTcRn :: SDoc -> TcRn ()
+debugDumpTcRn doc | opt_NoDebugOutput = return ()
+ | otherwise = dumpTcRn doc
dumpOptTcRn :: DynFlag -> SDoc -> TcRn ()
dumpOptTcRn flag doc = ifOptM flag (dumpTcRn doc)
getRecFieldEnv :: TcRn RecFieldEnv
getRecFieldEnv = do { env <- getGblEnv; return (tcg_field_env env) }
-extendRecFieldEnv :: RecFieldEnv -> RnM a -> RnM a
-extendRecFieldEnv new_bit
- = updGblEnv (\env@(TcGblEnv { tcg_field_env = old_env }) ->
- env {tcg_field_env = old_env `plusNameEnv` new_bit})
-
getDeclaredDefaultTys :: TcRn (Maybe [Type])
getDeclaredDefaultTys = do { env <- getGblEnv; return (tcg_default env) }
\end{code}
setErrsVar :: TcRef Messages -> TcRn a -> TcRn a
setErrsVar v = updLclEnv (\ env -> env { tcl_errs = v })
-addErr :: Message -> TcRn ()
+addErr :: Message -> TcRn () -- Ignores the context stack
addErr msg = do { loc <- getSrcSpanM ; addErrAt loc msg }
+failWith :: Message -> TcRn a
+failWith msg = addErr msg >> failM
+
addLocErr :: Located e -> (e -> Message) -> TcRn ()
addLocErr (L loc e) fn = addErrAt loc (fn e)
\begin{code}
-try_m :: TcRn r -> TcRn (Either Exception r)
+try_m :: TcRn r -> TcRn (Either IOEnvFailure r)
-- Does try_m, with a debug-trace on failure
try_m thing
= do { mb_r <- tryM thing ;
-- Drop elements of the input that fail, so the result
-- list can be shorter than the argument list
mapAndRecoverM _ [] = return []
-mapAndRecoverM f (x:xs) = do { mb_r <- tryM (f x)
+mapAndRecoverM f (x:xs) = do { mb_r <- try_m (f x)
; rs <- mapAndRecoverM f xs
; return (case mb_r of
Left _ -> rs
| otherwise = return ()
\end{code}
- %************************************************************************
+%************************************************************************
%* *
Type constraints (the so-called LIE)
%* *
%************************************************************************
%* *
+ Meta type variable bindings
+%* *
+%************************************************************************
+
+\begin{code}
+getTcTyVarBindsVar :: TcM (TcRef TcTyVarBinds)
+getTcTyVarBindsVar = do { env <- getLclEnv; return (tcl_tybinds env) }
+
+getTcTyVarBinds :: TcM a -> TcM (a, TcTyVarBinds)
+getTcTyVarBinds thing_inside
+ = do { tybinds_var <- newMutVar emptyBag
+ ; res <- updLclEnv (\ env -> env { tcl_tybinds = tybinds_var })
+ thing_inside
+ ; tybinds <- readMutVar tybinds_var
+ ; return (res, tybinds)
+ }
+
+bindMetaTyVar :: TcTyVar -> TcType -> TcM ()
+bindMetaTyVar tv ty
+ = do { ASSERTM2( do { details <- readMutVar (metaTvRef tv)
+ ; return (isFlexi details) }, ppr tv )
+ ; tybinds_var <- getTcTyVarBindsVar
+ ; tybinds <- readMutVar tybinds_var
+ ; writeMutVar tybinds_var (tybinds `snocBag` TcTyVarBind tv ty)
+ }
+
+getTcTyVarBindsRelation :: TcM [(TcTyVar, TcTyVarSet)]
+getTcTyVarBindsRelation
+ = do { tybinds_var <- getTcTyVarBindsVar
+ ; tybinds <- readMutVar tybinds_var
+ ; return $ map freeTvs (bagToList tybinds)
+ }
+ where
+ freeTvs (TcTyVarBind tv ty) = (tv, tyVarsOfType ty)
+\end{code}
+
+%************************************************************************
+%* *
Template Haskell context
%* *
%************************************************************************
-- Used when checking the up-to-date-ness of the old Iface
-- Initialise the environment with no useful info at all
initIfaceCheck hsc_env do_this
- = do { let gbl_env = IfGblEnv { if_rec_types = Nothing }
- ; initTcRnIf 'i' hsc_env gbl_env () do_this
- }
+ = do let rec_types = case hsc_type_env_var hsc_env of
+ Just (mod,var) -> Just (mod, readMutVar var)
+ Nothing -> Nothing
+ gbl_env = IfGblEnv { if_rec_types = rec_types }
+ initTcRnIf 'i' hsc_env gbl_env () do_this
initIfaceTc :: ModIface
-> (TcRef TypeEnv -> IfL a) -> TcRnIf gbl lcl a