import HsSyn ( HsBracket(..), HsExpr(..), HsSplice(..), LHsExpr, LHsDecl,
HsType, LHsType )
-import Convert ( convertToHsExpr, convertToHsDecls, convertToHsType )
+import Convert ( convertToHsExpr, convertToHsDecls, convertToHsType, thRdrName )
import RnExpr ( rnLExpr )
-import RnEnv ( lookupFixityRn, lookupSrcOcc_maybe )
-import RdrName ( RdrName, mkRdrUnqual, lookupLocalRdrEnv )
+import RnEnv ( lookupFixityRn, lookupSrcOcc_maybe, lookupImportedName )
+import RdrName ( RdrName, lookupLocalRdrEnv, isSrcRdrName )
import RnTypes ( rnLHsType )
-import TcExpr ( tcCheckRho, tcMonoExpr )
-import TcHsSyn ( mkHsLet, zonkTopLExpr )
+import TcExpr ( tcMonoExpr )
+import TcHsSyn ( mkHsDictLet, zonkTopLExpr )
import TcSimplify ( tcSimplifyTop, tcSimplifyBracket )
-import TcUnify ( Expected, zapExpectedTo, zapExpectedType )
-import TcType ( TcType, TcKind, liftedTypeKind, mkAppTy, tcSplitSigmaTy )
-import TcEnv ( spliceOK, tcMetaTy, bracketOK, tcLookup )
-import TcMType ( newTyVarTy, newKindVar, UserTypeCtxt(ExprSigCtxt), zonkTcType, zonkTcTyVar )
+import TcUnify ( boxyUnify, unBox )
+import TcType ( TcType, TcKind, BoxyRhoType, liftedTypeKind, mkAppTy, tcSplitSigmaTy )
+import TcEnv ( spliceOK, tcMetaTy, bracketOK )
+import TcMType ( newFlexiTyVarTy, newKindVar, UserTypeCtxt(ExprSigCtxt), zonkTcType )
import TcHsType ( tcHsSigType, kcHsType )
+import TcIface ( tcImportDecl )
import TypeRep ( Type(..), PredType(..), TyThing(..) ) -- For reification
-import Name ( Name, NamedThing(..), nameOccName, nameModule, isExternalName, mkInternalName )
+import PrelNames ( thFAKE )
+import Name ( Name, NamedThing(..), nameOccName, nameModule, isExternalName,
+ nameIsLocalOrFrom )
+import NameEnv ( lookupNameEnv )
+import HscTypes ( lookupType, ExternalPackageState(..), emptyModDetails )
import OccName
import Var ( Id, TyVar, idType )
-import Module ( moduleUserString, mkModuleName )
+import Module ( moduleString )
import TcRnMonad
import IfaceEnv ( lookupOrig )
-
-import Class ( Class, classBigSig )
-import TyCon ( TyCon, tyConTheta, tyConTyVars, getSynTyConDefn, isSynTyCon, isNewTyCon, tyConDataCons )
+import Class ( Class, classExtraBigSig )
+import TyCon ( TyCon, tyConTyVars, synTyConDefn,
+ isSynTyCon, isNewTyCon, tyConDataCons, isPrimTyCon, isFunTyCon,
+ tyConArity, tyConStupidTheta, isUnLiftedTyCon )
import DataCon ( DataCon, dataConTyCon, dataConOrigArgTys, dataConStrictMarks,
- dataConName, dataConFieldLabels, dataConWrapId )
+ dataConName, dataConFieldLabels, dataConWrapId, dataConIsInfix,
+ isVanillaDataCon )
import Id ( idName, globalIdDetails )
import IdInfo ( GlobalIdDetails(..) )
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 )
\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 {
-- Typecheck expr to make sure it is valid,
-- but throw away the results. We'll type check
-- it again when we actually use it.
+ recordThUse `thenM_`
newMutVar [] `thenM` \ pending_splices ->
getLIEVar `thenM` \ lie_var ->
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)
- = tcMetaTy nameTyConName
- -- Result type is Var (not Q-monadic)
+ = tcMetaTy nameTyConName -- Result type is Var (not Q-monadic)
tc_bracket (ExpBr expr)
- = newTyVarTy 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 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}
\begin{code}
tcSpliceExpr (HsSplice name expr) res_ty
- = addSrcSpan (getLoc expr) $
+ = setSrcSpan (getLoc expr) $
getStage `thenM` \ level ->
case spliceOK level of {
Nothing -> failWithTc (illegalSplice level) ;
Just next_level ->
case level of {
- Comp -> do { e <- tcTopSplice expr res_ty ;
- returnM (unLoc e) };
+ Comp -> do { e <- tcTopSplice expr res_ty
+ ; returnM (unLoc e) } ;
Brack _ ps_var lie_var ->
-- A splice inside brackets
-- 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 simple_expr
- in
traceTc (text "Got result" <+> ppr expr2) `thenM_`
showSplice "expression"
= checkNoErrs $ -- checkNoErrs: must not try to run the thing
-- if the type checker fails!
- setStage topSpliceStage $
+ setStage topSpliceStage $ do
- -- Typecheck the expression
- getLIE (tcCheckRho expr meta_ty) `thenM` \ (expr', lie) ->
+
+ do { recordThUse -- Record that TH is used (for pkg depdendency)
+ -- Typecheck the expression
+ ; (expr', lie) <- getLIE (tcMonoExpr expr meta_ty)
+
-- Solve the constraints
- tcSimplifyTop lie `thenM` \ const_binds ->
+ ; const_binds <- tcSimplifyTop lie
-- And zonk it
- zonkTopLExpr (mkHsLet const_binds expr')
+ ; zonkTopLExpr (mkHsDictLet const_binds expr') }
\end{code}
\begin{code}
kcSpliceType (HsSplice name hs_expr)
- = addSrcSpan (getLoc hs_expr) $ do
+ = setSrcSpan (getLoc hs_expr) $ do
{ level <- getStage
; case spliceOK level of {
Nothing -> failWithTc (illegalSplice level) ;
; 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 simple_ty
-
; traceTc (text "Got result" <+> ppr hs_ty2)
; showSplice "type" zonked_q_expr (ppr hs_ty2)
\begin{code}
-- Always at top level
+-- Type sig at top of file:
+-- tcSpliceDecls :: LHsExpr Name -> TcM [LHsDecl RdrName]
tcSpliceDecls expr
= do { meta_dec_ty <- tcMetaTy decTyConName
; meta_q_ty <- tcMetaTy qTyConName
-- 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 simple_expr)
; traceTc (text "Got result" <+> vcat (map ppr decls))
; showSplice "declarations"
- zonked_q_expr (vcat (map ppr decls))
+ zonked_q_expr
+ (ppr (getLoc expr) $$ (vcat (map ppr decls)))
; returnM decls }
where handleErrors :: [Either a Message] -> TcM [a]
%************************************************************************
\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
; let type_env = tcg_type_env tcg_env
rdr_env = tcg_rdr_env tcg_env
- -- Wrap the compile-and-run in an exception-catcher
- -- Compiling might fail if linking fails
- -- Running might fail if it throws an exception
- ; either_tval <- tryM $ do
- { -- Compile it
- hval <- ioToTcRn (HscMain.compileExpr
+
+ -- Compile and link it; might fail if linking fails
+ ; either_hval <- tryM $ ioToTcRn $
+ HscMain.compileExpr
hsc_env this_mod
- rdr_env type_env expr)
- -- Coerce it to Q t, and run it
- ; TH.runQ (unsafeCoerce# hval) }
+ rdr_env type_env expr
+ ; case either_hval of {
+ Left exn -> failWithTc (mk_msg "compile and link" exn) ;
+ Right hval -> do
+
+ { -- 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
+ --
+ -- 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 (vcat [text "Exception when trying to run compile-time code:",
- nest 4 (vcat [text "Code:" <+> ppr expr,
- text ("Exn: " ++ Panic.showException 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:",
+ nest 2 (text (Panic.showException exn)),
+ nest 2 (text "Code:" <+> ppr expr)]
\end{code}
To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
\begin{code}
instance TH.Quasi (IOEnv (Env TcGblEnv TcLclEnv)) where
- qNewName s = do { u <- newUnique
+ qNewName s = do { u <- newUnique
; let i = getKey u
; return (TH.mkNameU s i) }
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
reify :: TH.Name -> TcM TH.Info
reify th_name
= do { name <- lookupThName th_name
- ; thing <- tcLookup name
+ ; thing <- tcLookupTh name
-- ToDo: this tcLookup could fail, which would give a
-- rather unhelpful error message
+ ; traceIf (text "reify" <+> text (show th_name) <+> brackets (ppr_ns th_name) <+> ppr name)
; reifyThing thing
}
+ where
+ ppr_ns (TH.Name _ (TH.NameG TH.DataName mod)) = text "data"
+ ppr_ns (TH.Name _ (TH.NameG TH.TcClsName mod)) = text "tc"
+ ppr_ns (TH.Name _ (TH.NameG TH.VarName mod)) = text "var"
lookupThName :: TH.Name -> TcM Name
-lookupThName (TH.Name occ (TH.NameG th_ns mod))
- = lookupOrig (mkModuleName (TH.modString mod))
- (OccName.mkOccName ghc_ns (TH.occString occ))
- where
- ghc_ns = case th_ns of
- TH.DataName -> dataName
- TH.TcClsName -> tcClsName
- TH.VarName -> varName
+lookupThName th_name@(TH.Name occ flavour)
+ = do { let rdr_name = thRdrName guessed_ns occ_str flavour
-lookupThName th_name@(TH.Name occ TH.NameS)
- = do { let rdr_name = mkRdrUnqual (OccName.mkOccFS ns occ_fs)
+ -- Repeat much of lookupOccRn, becase we want
+ -- to report errors in a TH-relevant way
; rdr_env <- getLocalRdrEnv
; case lookupLocalRdrEnv rdr_env rdr_name of
- Just name -> return name
- Nothing -> do
- { mb_name <- lookupSrcOcc_maybe rdr_name
- ; case mb_name of
- Just name -> return name ;
- Nothing -> failWithTc (notInScope th_name)
- }}
+ Just name -> return name
+ Nothing | not (isSrcRdrName rdr_name) -- Exact, Orig
+ -> lookupImportedName rdr_name
+ | otherwise -- Unqual, Qual
+ -> do {
+ mb_name <- lookupSrcOcc_maybe rdr_name
+ ; case mb_name of
+ Just name -> return name
+ Nothing -> failWithTc (notInScope th_name) }
+ }
where
- ns | isLexCon occ_fs = OccName.dataName
- | otherwise = OccName.varName
- occ_fs = mkFastString (TH.occString occ)
-
-lookupThName (TH.Name occ (TH.NameU uniq))
- = return (mkInternalName (mk_uniq uniq) (OccName.mkOccFS bogus_ns occ_fs) noSrcLoc)
- where
- occ_fs = mkFastString (TH.occString occ)
- bogus_ns = OccName.varName -- Not yet recorded in the TH name
- -- but only the unique matters
-
-mk_uniq :: Int# -> Unique
-mk_uniq u = mkUniqueGrimily (I# u)
+ -- guessed_ns is the name space guessed from looking at the 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
+-- it gives a reify-related error message on failure, whereas in the normal
+-- tcLookup, failure is a bug.
+tcLookupTh name
+ = do { (gbl_env, lcl_env) <- getEnvs
+ ; case lookupNameEnv (tcl_env lcl_env) name of {
+ Just thing -> returnM thing;
+ Nothing -> do
+ { if nameIsLocalOrFrom (tcg_mod gbl_env) name
+ then -- It's defined in this module
+ case lookupNameEnv (tcg_type_env gbl_env) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> failWithTc (notInEnv name)
+
+ else do -- It's imported
+ { (eps,hpt) <- getEpsAndHpt
+ ; case lookupType hpt (eps_PTE eps) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> do { thing <- tcImportDecl name
+ ; return (AGlobal thing) }
+ -- Imported names should always be findable;
+ -- if not, we fail hard in tcImportDecl
+ }}}}
notInScope :: TH.Name -> SDoc
-notInScope th_name = quotes (text (show (TH.ppr th_name))) <+>
+notInScope th_name = quotes (text (TH.pprint th_name)) <+>
ptext SLIT("is not in scope at a reify")
-- Ugh! Rather an indirect way to display the name
+notInEnv :: Name -> SDoc
+notInEnv name = quotes (ppr name) <+>
+ ptext SLIT("is not in the type environment at a reify")
+
------------------------------
reifyThing :: TcTyThing -> TcM TH.Info
-- The only reason this is monadic is for error reporting,
other -> return (TH.VarI v ty Nothing fix)
}
-reifyThing (AGlobal (ATyCon tc)) = do { dec <- reifyTyCon tc; return (TH.TyConI dec) }
-reifyThing (AGlobal (AClass cls)) = do { dec <- reifyClass cls; return (TH.ClassI dec) }
+reifyThing (AGlobal (ATyCon tc)) = reifyTyCon tc
+reifyThing (AGlobal (AClass cls)) = reifyClass cls
reifyThing (AGlobal (ADataCon dc))
= do { let name = dataConName dc
; ty <- reifyType (idType (dataConWrapId dc))
; fix <- reifyFixity (idName id)
; return (TH.VarI (reifyName id) ty2 Nothing fix) }
-reifyThing (ATyVar tv)
- = do { ty1 <- zonkTcTyVar tv
+reifyThing (ATyVar tv ty)
+ = do { ty1 <- zonkTcType ty
; ty2 <- reifyType ty1
; return (TH.TyVarI (reifyName tv) ty2) }
------------------------------
-reifyTyCon :: TyCon -> TcM TH.Dec
+reifyTyCon :: TyCon -> TcM TH.Info
reifyTyCon tc
+ | 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.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
-
- | isNewTyCon tc
- = do { cxt <- reifyCxt (tyConTheta tc)
- ; con <- reifyDataCon (head (tyConDataCons tc))
- ; return (TH.NewtypeD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
- con [{- Don't know about deriving -}]) }
+ ; return (TH.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
- | otherwise -- Algebraic
- = do { cxt <- reifyCxt (tyConTheta tc)
+reifyTyCon tc
+ = do { cxt <- reifyCxt (tyConStupidTheta tc)
; cons <- mapM reifyDataCon (tyConDataCons tc)
- ; return (TH.DataD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
- cons [{- Don't know about deriving -}]) }
+ ; 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
+ | isVanillaDataCon dc
= do { arg_tys <- reifyTypes (dataConOrigArgTys dc)
; let stricts = map reifyStrict (dataConStrictMarks dc)
fields = dataConFieldLabels dc
- ; if null fields then
- return (TH.NormalC (reifyName dc) (stricts `zip` arg_tys))
+ name = reifyName dc
+ [a1,a2] = arg_tys
+ [s1,s2] = stricts
+ ; ASSERT( length arg_tys == length stricts )
+ if not (null fields) then
+ return (TH.RecC name (zip3 (map reifyName fields) stricts arg_tys))
+ else
+ if dataConIsInfix dc then
+ ASSERT( length arg_tys == 2 )
+ return (TH.InfixC (s1,a1) name (s2,a2))
else
- return (TH.RecC (reifyName dc) (zip3 (map reifyName fields) stricts arg_tys)) }
- -- NB: we don't remember whether the constructor was declared in an infix way
+ return (TH.NormalC name (stricts `zip` arg_tys)) }
+ | otherwise
+ = failWithTc (ptext SLIT("Can't reify a non-Haskell-98 data constructor:")
+ <+> quotes (ppr dc))
------------------------------
-reifyClass :: Class -> TcM TH.Dec
+reifyClass :: Class -> TcM TH.Info
reifyClass cls
= do { cxt <- reifyCxt theta
; ops <- mapM reify_op op_stuff
- ; return (TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) ops) }
+ ; return (TH.ClassI $ TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
where
- (tvs, theta, _, op_stuff) = classBigSig cls
+ (tvs, fds, theta, _, op_stuff) = classExtraBigSig cls
+ fds' = map reifyFunDep fds
reify_op (op, _) = do { ty <- reifyType (idType op)
; return (TH.SigD (reifyName op) ty) }
reifyType :: TypeRep.Type -> TcM TH.Type
reifyType (TyVarTy tv) = return (TH.VarT (reifyName tv))
reifyType (TyConApp tc tys) = reify_tc_app (reifyName tc) tys
-reifyType (NewTcApp tc tys) = reify_tc_app (reifyName tc) tys
reifyType (NoteTy _ ty) = reifyType ty
reifyType (AppTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (r1 `TH.AppT` r2) }
reifyType (FunTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (TH.ArrowT `TH.AppT` r1 `TH.AppT` r2) }
reifyTypes = mapM reifyType
reifyCxt = mapM reifyPred
+reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
+reifyFunDep (xs, ys) = TH.FunDep (map reifyName xs) (map reifyName ys)
+
reifyTyVars :: [TyVar] -> [TH.Name]
reifyTyVars = map reifyName
reifyName thing
| isExternalName name = mk_varg mod occ_str
| otherwise = TH.mkNameU occ_str (getKey (getUnique name))
+ -- Many of the things we reify have local bindings, and
+ -- NameL's aren't supposed to appear in binding positions, so
+ -- we use NameU. When/if we start to reify nested things, that
+ -- 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