import HsSyn ( HsBracket(..), HsExpr(..), HsSplice(..), LHsExpr, LHsDecl,
HsType, LHsType )
-import LoadIface ( loadHomeInterface )
import Convert ( convertToHsExpr, convertToHsDecls, convertToHsType, thRdrName )
import RnExpr ( rnLExpr )
import RnEnv ( lookupFixityRn, lookupSrcOcc_maybe, lookupImportedName )
-import RdrName ( RdrName, mkRdrQual, mkRdrUnqual, lookupLocalRdrEnv, isSrcRdrName )
+import RdrName ( RdrName, lookupLocalRdrEnv, isSrcRdrName )
import RnTypes ( rnLHsType )
-import TcExpr ( tcCheckRho, tcMonoExpr )
+import TcExpr ( tcMonoExpr )
import TcHsSyn ( mkHsDictLet, zonkTopLExpr )
import TcSimplify ( tcSimplifyTop, tcSimplifyBracket )
-import TcUnify ( Expected, zapExpectedTo, zapExpectedType )
-import TcType ( TcType, TcKind, liftedTypeKind, mkAppTy, tcSplitSigmaTy )
+import TcUnify ( boxyUnify, unBox )
+import TcType ( TcType, TcKind, BoxyRhoType, liftedTypeKind, mkAppTy, tcSplitSigmaTy )
import TcEnv ( spliceOK, tcMetaTy, bracketOK )
-import TcMType ( newTyFlexiVarTy, newKindVar, UserTypeCtxt(ExprSigCtxt), zonkTcType, zonkTcTyVar )
+import TcMType ( newFlexiTyVarTy, newKindVar, UserTypeCtxt(ExprSigCtxt), zonkTcType )
import TcHsType ( tcHsSigType, kcHsType )
import TcIface ( tcImportDecl )
import TypeRep ( Type(..), PredType(..), TyThing(..) ) -- For reification
+import PrelNames ( thFAKE )
import Name ( Name, NamedThing(..), nameOccName, nameModule, isExternalName,
- mkInternalName, nameIsLocalOrFrom )
+ nameIsLocalOrFrom )
import NameEnv ( lookupNameEnv )
import HscTypes ( lookupType, ExternalPackageState(..), emptyModDetails )
import OccName
import Var ( Id, TyVar, idType )
-import Module ( moduleUserString, mkModule )
+import Module ( moduleString )
import TcRnMonad
import IfaceEnv ( lookupOrig )
import Class ( Class, classExtraBigSig )
-import TyCon ( TyCon, AlgTyConRhs(..), tyConTyVars, getSynTyConDefn,
- isSynTyCon, isNewTyCon, tyConDataCons, algTyConRhs, isPrimTyCon, isFunTyCon,
+import TyCon ( TyCon, tyConTyVars, synTyConDefn,
+ isSynTyCon, isNewTyCon, tyConDataCons, isPrimTyCon, isFunTyCon,
tyConArity, tyConStupidTheta, isUnLiftedTyCon )
import DataCon ( DataCon, dataConTyCon, dataConOrigArgTys, dataConStrictMarks,
dataConName, dataConFieldLabels, dataConWrapId, dataConIsInfix,
import TysWiredIn ( mkListTy )
import DsMeta ( expQTyConName, typeQTyConName, decTyConName, qTyConName, nameTyConName )
import ErrUtils ( Message )
-import SrcLoc ( noLoc, unLoc, getLoc, noSrcLoc )
+import SrcLoc ( SrcSpan, noLoc, unLoc, getLoc )
import Outputable
import Unique ( Unique, Uniquable(..), getKey, mkUniqueGrimily )
import GHC.Base ( unsafeCoerce#, Int#, Int(..) ) -- Should have a better home in the module hierarchy
import Monad ( liftM )
-import Maybes ( orElse )
#ifdef GHCI
import FastString ( mkFastString )
\begin{code}
tcSpliceDecls :: LHsExpr Name -> TcM [LHsDecl RdrName]
-tcSpliceExpr :: HsSplice Name -> Expected TcType -> TcM (HsExpr TcId)
+tcSpliceExpr :: HsSplice Name -> BoxyRhoType -> TcM (HsExpr TcId)
kcSpliceType :: HsSplice Name -> TcM (HsType Name, TcKind)
#ifndef GHCI
%************************************************************************
\begin{code}
-tcBracket :: HsBracket Name -> Expected TcType -> TcM (LHsExpr Id)
+tcBracket :: HsBracket Name -> BoxyRhoType -> TcM (LHsExpr Id)
tcBracket brack res_ty
= getStage `thenM` \ level ->
case bracketOK level of {
tcSimplifyBracket lie `thenM_`
-- Make the expected type have the right shape
- zapExpectedTo res_ty meta_ty `thenM_`
+ boxyUnify meta_ty res_ty `thenM_`
-- Return the original expression, not the type-decorated one
readMutVar pending_splices `thenM` \ pendings ->
tc_bracket :: HsBracket Name -> TcM TcType
tc_bracket (VarBr v)
- = do { loadHomeInterface msg v -- Reason: deprecation checking asumes the
- -- home interface is loaded, and this is the
- -- only way that is going to happen
- ; tcMetaTy nameTyConName -- Result type is Var (not Q-monadic)
- }
- where
- msg = ptext SLIT("Need interface for Template Haskell quoted Name")
+ = tcMetaTy nameTyConName -- Result type is Var (not Q-monadic)
tc_bracket (ExpBr expr)
- = newTyFlexiVarTy liftedTypeKind `thenM` \ any_ty ->
- tcCheckRho expr any_ty `thenM_`
+ = newFlexiTyVarTy liftedTypeKind `thenM` \ any_ty ->
+ tcMonoExpr expr any_ty `thenM_`
tcMetaTy expQTyConName
-- Result type is Expr (= Q Exp)
-- Result type is Type (= Q Typ)
tc_bracket (DecBr decls)
- = tcTopSrcDecls emptyModDetails decls `thenM_`
+ = do { tcTopSrcDecls emptyModDetails decls
-- Typecheck the declarations, dicarding the result
-- We'll get all that stuff later, when we splice it in
- tcMetaTy decTyConName `thenM` \ decl_ty ->
- tcMetaTy qTyConName `thenM` \ q_ty ->
- returnM (mkAppTy q_ty (mkListTy decl_ty))
+ ; decl_ty <- tcMetaTy decTyConName
+ ; q_ty <- tcMetaTy qTyConName
+ ; return (mkAppTy q_ty (mkListTy decl_ty))
-- Result type is Q [Dec]
+ }
+
+tc_bracket (PatBr _)
+ = failWithTc (ptext SLIT("Tempate Haskell pattern brackets are not supported yet"))
\end{code}
-- Here (h 4) :: Q Exp
-- but $(h 4) :: forall a.a i.e. anything!
- zapExpectedType res_ty liftedTypeKind `thenM_`
+ unBox res_ty `thenM_`
tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
setStage (Splice next_level) (
setLIEVar lie_var $
- tcCheckRho expr meta_exp_ty
+ tcMonoExpr expr meta_exp_ty
) `thenM` \ expr' ->
-- Write the pending splice into the bucket
-- The recursive call to tcMonoExpr will simply expand the
-- inner escape before dealing with the outer one
-tcTopSplice :: LHsExpr Name -> Expected TcType -> TcM (LHsExpr Id)
+tcTopSplice :: LHsExpr Name -> BoxyRhoType -> TcM (LHsExpr Id)
tcTopSplice expr res_ty
= tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
-- Run the expression
traceTc (text "About to run" <+> ppr zonked_q_expr) `thenM_`
- runMetaE zonked_q_expr `thenM` \ simple_expr ->
+ runMetaE convertToHsExpr zonked_q_expr `thenM` \ expr2 ->
- let
- -- simple_expr :: TH.Exp
-
- expr2 :: LHsExpr RdrName
- expr2 = convertToHsExpr (getLoc expr) simple_expr
- in
traceTc (text "Got result" <+> ppr expr2) `thenM_`
showSplice "expression"
do { recordThUse -- Record that TH is used (for pkg depdendency)
-- Typecheck the expression
- ; (expr', lie) <- getLIE (tcCheckRho expr meta_ty)
+ ; (expr', lie) <- getLIE (tcMonoExpr expr meta_ty)
-- Solve the constraints
; const_binds <- tcSimplifyTop lie
; meta_ty <- tcMetaTy typeQTyConName
; expr' <- setStage (Splice next_level) $
setLIEVar lie_var $
- tcCheckRho hs_expr meta_ty
+ tcMonoExpr hs_expr meta_ty
-- Write the pending splice into the bucket
; ps <- readMutVar ps_var
-- Run the expression
; traceTc (text "About to run" <+> ppr zonked_q_expr)
- ; simple_ty <- runMetaT zonked_q_expr
+ ; hs_ty2 <- runMetaT convertToHsType zonked_q_expr
- ; let -- simple_ty :: TH.Type
- hs_ty2 :: LHsType RdrName
- hs_ty2 = convertToHsType (getLoc expr) simple_ty
-
; traceTc (text "Got result" <+> ppr hs_ty2)
; showSplice "type" zonked_q_expr (ppr hs_ty2)
-- Run the expression
; traceTc (text "About to run" <+> ppr zonked_q_expr)
- ; simple_expr <- runMetaD zonked_q_expr
+ ; decls <- runMetaD convertToHsDecls zonked_q_expr
- -- simple_expr :: [TH.Dec]
- -- decls :: [RdrNameHsDecl]
- ; decls <- handleErrors (convertToHsDecls (getLoc expr) simple_expr)
; traceTc (text "Got result" <+> vcat (map ppr decls))
; showSplice "declarations"
zonked_q_expr
%************************************************************************
\begin{code}
-runMetaE :: LHsExpr Id -- Of type (Q Exp)
- -> TcM TH.Exp -- Of type Exp
-runMetaE e = runMeta e
-
-runMetaT :: LHsExpr Id -- Of type (Q Type)
- -> TcM TH.Type -- Of type Type
-runMetaT e = runMeta e
-
-runMetaD :: LHsExpr Id -- Of type Q [Dec]
- -> TcM [TH.Dec] -- Of type [Dec]
-runMetaD e = runMeta e
-
-runMeta :: LHsExpr Id -- Of type X
- -> TcM t -- Of type t
-runMeta expr
+runMetaE :: (SrcSpan -> TH.Exp -> Either Message (LHsExpr RdrName))
+ -> LHsExpr Id -- Of type (Q Exp)
+ -> TcM (LHsExpr RdrName)
+runMetaE = runMeta
+
+runMetaT :: (SrcSpan -> TH.Type -> Either Message (LHsType RdrName))
+ -> LHsExpr Id -- Of type (Q Type)
+ -> TcM (LHsType RdrName)
+runMetaT = runMeta
+
+runMetaD :: (SrcSpan -> [TH.Dec] -> Either Message [LHsDecl RdrName])
+ -> LHsExpr Id -- Of type Q [Dec]
+ -> TcM [LHsDecl RdrName]
+runMetaD = runMeta
+
+runMeta :: (SrcSpan -> th_syn -> Either Message hs_syn)
+ -> LHsExpr Id -- Of type X
+ -> TcM hs_syn -- Of type t
+runMeta convert expr
= do { hsc_env <- getTopEnv
; tcg_env <- getGblEnv
; this_mod <- getModule
{ -- Coerce it to Q t, and run it
-- Running might fail if it throws an exception of any kind (hence tryAllM)
-- including, say, a pattern-match exception in the code we are running
- either_tval <- tryAllM (TH.runQ (unsafeCoerce# hval))
+ --
+ -- We also do the TH -> HS syntax conversion inside the same
+ -- exception-cacthing thing so that if there are any lurking
+ -- exceptions in the data structure returned by hval, we'll
+ -- encounter them inside the tryALlM
+ either_tval <- tryAllM $ do
+ { th_syn <- TH.runQ (unsafeCoerce# hval)
+ ; case convert (getLoc expr) th_syn of
+ Left err -> do { addErrTc err; return Nothing }
+ Right hs_syn -> return (Just hs_syn) }
; case either_tval of
- Left exn -> failWithTc (mk_msg "run" exn)
- Right v -> returnM v
+ Right (Just v) -> return v
+ Right Nothing -> failM -- Error already in Tc monad
+ Left exn -> failWithTc (mk_msg "run" exn) -- Exception
}}}
where
mk_msg s exn = vcat [text "Exception when trying to" <+> text s <+> text "compile-time code:",
qReport True msg = addErr (text msg)
qReport False msg = addReport (text msg)
- qCurrentModule = do { m <- getModule; return (moduleUserString m) }
+ qCurrentModule = do { m <- getModule; return (moduleString m) }
qReify v = reify v
qRecover = recoverM
ppr_ns (TH.Name _ (TH.NameG TH.VarName mod)) = text "var"
lookupThName :: TH.Name -> TcM Name
-lookupThName th_name
- = do { let rdr_name = thRdrName guessed_ns th_name
+lookupThName th_name@(TH.Name occ flavour)
+ = do { let rdr_name = thRdrName guessed_ns occ_str flavour
-- Repeat much of lookupOccRn, becase we want
-- to report errors in a TH-relevant way
}
where
-- guessed_ns is the name space guessed from looking at the TH name
- guessed_ns | isLexCon occ_fs = OccName.dataName
- | otherwise = OccName.varName
- occ_fs = mkFastString (TH.nameBase th_name)
+ guessed_ns | isLexCon (mkFastString occ_str) = OccName.dataName
+ | otherwise = OccName.varName
+ occ_str = TH.occString occ
tcLookupTh :: Name -> TcM TcTyThing
-- This is a specialised version of TcEnv.tcLookup; specialised mainly in that
-- if not, we fail hard in tcImportDecl
}}}}
-mk_uniq :: Int# -> Unique
-mk_uniq u = mkUniqueGrimily (I# u)
-
notInScope :: TH.Name -> SDoc
notInScope th_name = quotes (text (TH.pprint th_name)) <+>
ptext SLIT("is not in scope at a reify")
; fix <- reifyFixity name
; return (TH.DataConI (reifyName name) ty (reifyName (dataConTyCon dc)) fix) }
-reifyThing (ATcId id _)
+reifyThing (ATcId id _ _)
= do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
-- though it may be incomplete
; ty2 <- reifyType ty1
| isFunTyCon tc = return (TH.PrimTyConI (reifyName tc) 2 False)
| isPrimTyCon tc = return (TH.PrimTyConI (reifyName tc) (tyConArity tc) (isUnLiftedTyCon tc))
| isSynTyCon tc
- = do { let (tvs, rhs) = getSynTyConDefn tc
+ = do { let (tvs, rhs) = synTyConDefn tc
; rhs' <- reifyType rhs
; return (TH.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
reifyTyCon tc
- = case algTyConRhs tc of
- NewTyCon data_con _ _
- -> do { cxt <- reifyCxt (tyConStupidTheta tc)
- ; con <- reifyDataCon data_con
- ; return (TH.TyConI $ TH.NewtypeD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
- con [{- Don't know about deriving -}]) }
-
- DataTyCon cons _
- -> do { cxt <- reifyCxt (tyConStupidTheta tc)
- ; cons <- mapM reifyDataCon (tyConDataCons tc)
- ; return (TH.TyConI $ TH.DataD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
- cons [{- Don't know about deriving -}]) }
+ = do { cxt <- reifyCxt (tyConStupidTheta tc)
+ ; cons <- mapM reifyDataCon (tyConDataCons tc)
+ ; let name = reifyName tc
+ tvs = reifyTyVars (tyConTyVars tc)
+ deriv = [] -- Don't know about deriving
+ decl | isNewTyCon tc = TH.NewtypeD cxt name tvs (head cons) deriv
+ | otherwise = TH.DataD cxt name tvs cons deriv
+ ; return (TH.TyConI decl) }
reifyDataCon :: DataCon -> TcM TH.Con
reifyDataCon dc
else
if dataConIsInfix dc then
ASSERT( length arg_tys == 2 )
- return (TH.InfixC (s1,a1) name (s1,a2))
+ return (TH.InfixC (s1,a1) name (s2,a2))
else
return (TH.NormalC name (stricts `zip` arg_tys)) }
| otherwise
-- have free variables, we may need to generate NameL's for them.
where
name = getName thing
- mod = moduleUserString (nameModule name)
- occ_str = occNameUserString occ
+ mod = moduleString (nameModule name)
+ occ_str = occNameString occ
occ = nameOccName name
mk_varg | OccName.isDataOcc occ = TH.mkNameG_d
| OccName.isVarOcc occ = TH.mkNameG_v