{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# LANGUAGE NamedFieldPuns #-}
module GHC.Tc.Gen.Splice(
tcTypedSplice, tcTypedBracket, tcUntypedBracket,
runAnnotation, getUntypedSpliceBody,
runMetaE, runMetaP, runMetaT, runMetaD, runQuasi,
tcTopSpliceExpr, lookupThName_maybe,
defaultRunMeta, runMeta', runRemoteModFinalizers,
finishTH, runTopSplice
) where
import GHC.Prelude
import GHC.Driver.Errors
import GHC.Driver.Plugins
import GHC.Driver.Main
import GHC.Driver.DynFlags
import GHC.Driver.Env
import GHC.Driver.Hooks
import GHC.Driver.Config.Diagnostic
import GHC.Driver.Config.Finder
import GHC.Hs
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.TcType
import GHC.Tc.Gen.Expr
import GHC.Tc.Utils.Unify
import GHC.Tc.Utils.Env
import GHC.Tc.Types.Origin
import GHC.Tc.Types.LclEnv
import GHC.Tc.Types.Evidence
import GHC.Tc.Zonk.Type
import GHC.Tc.Zonk.TcType
import GHC.Tc.Solver
import GHC.Tc.Utils.TcMType
import GHC.Tc.Gen.HsType
import GHC.Tc.Instance.Family
import GHC.Tc.Utils.Instantiate
import GHC.Core.Multiplicity
import GHC.Core.Coercion( etaExpandCoAxBranch )
import GHC.Core.Type as Type
import GHC.Core.TyCo.Rep as TyCoRep
import GHC.Core.FamInstEnv
import GHC.Core.InstEnv as InstEnv
import GHC.Builtin.Names.TH
import GHC.Builtin.Names
import GHC.Builtin.Types
import GHC.ThToHs
import GHC.HsToCore.Docs
import GHC.HsToCore.Expr
import GHC.HsToCore.Monad
import GHC.IfaceToCore
import GHC.Iface.Load
import GHCi.Message
import GHCi.RemoteTypes
import GHC.Runtime.Interpreter
import GHC.Rename.Splice( traceSplice, SpliceInfo(..))
import GHC.Rename.Expr
import GHC.Rename.Env
import GHC.Rename.Fixity ( lookupFixityRn_help )
import GHC.Rename.HsType
import GHC.Core.Class
import GHC.Core.TyCon
import GHC.Core.Coercion.Axiom
import GHC.Core.PatSyn
import GHC.Core.ConLike
import GHC.Core.DataCon as DataCon
import GHC.Types.SrcLoc
import GHC.Types.Name.Env
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Types.Name.Occurrence as OccName
import GHC.Types.Var
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Unique
import GHC.Types.Var.Set
import GHC.Types.Meta
import GHC.Types.Basic hiding( SuccessFlag(..) )
import GHC.Types.Error
import GHC.Types.Fixity as Hs
import GHC.Types.Annotations
import GHC.Types.Name
import GHC.Types.Unique.Map
import GHC.Serialized
import GHC.Unit.Finder
import GHC.Unit.Module
import GHC.Unit.Module.ModIface
import GHC.Unit.Module.Deps
import GHC.Utils.Misc
import GHC.Utils.Panic as Panic
import GHC.Utils.Lexeme
import GHC.Utils.Outputable
import GHC.Utils.Logger
import GHC.Utils.Exception (throwIO, ErrorCall(..))
import GHC.Utils.TmpFs ( newTempName, TempFileLifetime(..) )
import GHC.Data.FastString
import GHC.Data.Maybe( MaybeErr(..) )
import qualified GHC.Data.EnumSet as EnumSet
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH
#if defined(HAVE_INTERNAL_INTERPRETER)
import GHC.Desugar ( AnnotationWrapper(..) )
import Unsafe.Coerce ( unsafeCoerce )
#endif
import Control.Monad
import Data.Binary
import Data.Binary.Get
import Data.Maybe
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Dynamic ( fromDynamic, toDyn )
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep )
import Data.Data (Data)
import Data.Proxy ( Proxy (..) )
import Data.IORef
import GHC.Parser.HaddockLex (lexHsDoc)
import GHC.Parser (parseIdentifier)
import GHC.Rename.Doc (rnHsDoc)
tcTypedBracket :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcUntypedBracket :: HsExpr GhcRn -> HsQuote GhcRn -> [PendingRnSplice] -> ExpRhoType
-> TcM (HsExpr GhcTc)
tcTypedSplice :: Name -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
getUntypedSpliceBody :: HsUntypedSpliceResult (HsExpr GhcRn) -> TcM (HsExpr GhcRn)
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
tcTypedBracket :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedBracket HsExpr GhcRn
rn_expr LHsExpr GhcRn
expr ExpRhoType
res_ty
= SDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (LHsExpr GhcRn -> SDoc
quotationCtxtDoc LHsExpr GhcRn
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
do { cur_stage <- TcM ThStage
getStage
; ps_ref <- newMutVar []
; lie_var <- getConstraintVar
; m_var <- mkTyVarTy <$> mkMetaTyVar
; ev_var <- emitQuoteWanted m_var
; let wrapper = TyVar -> Type -> QuoteWrapper
QuoteWrapper TyVar
ev_var Type
m_var
; (tc_expr, expr_ty) <- setStage (Brack cur_stage (TcPending ps_ref lie_var wrapper)) $
tcScalingUsage ManyTy $
tcInferRhoNC expr
; let rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
expr_ty
; meta_ty <- tcTExpTy m_var expr_ty
; ps' <- readMutVar ps_ref
; codeco <- tcLookupId unsafeCodeCoerceName
; bracket_ty <- mkAppTy m_var <$> tcMetaTy expTyConName
; let brack_tc = HsBracketTc { hsb_quote :: HsQuote GhcRn
hsb_quote = XExpBr GhcRn -> LHsExpr GhcRn -> HsQuote GhcRn
forall p. XExpBr p -> LHsExpr p -> HsQuote p
ExpBr XExpBr GhcRn
NoExtField
noExtField LHsExpr GhcRn
expr, hsb_ty :: Type
hsb_ty = Type
bracket_ty
, hsb_wrap :: Maybe QuoteWrapper
hsb_wrap = QuoteWrapper -> Maybe QuoteWrapper
forall a. a -> Maybe a
Just QuoteWrapper
wrapper, hsb_splices :: [PendingTcSplice]
hsb_splices = [PendingTcSplice]
ps' }
brack_expr = XTypedBracket GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XTypedBracket p -> LHsExpr p -> HsExpr p
HsTypedBracket XTypedBracket GhcTc
HsBracketTc
brack_tc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
tc_expr
; tcWrapResultO (Shouldn'tHappenOrigin "TH typed bracket expression")
rn_expr
(unLoc (mkHsApp (mkLHsWrap (applyQuoteWrapper wrapper)
(nlHsTyApp codeco [rep, expr_ty]))
(noLocA brack_expr)))
meta_ty res_ty }
tcUntypedBracket :: HsExpr GhcRn
-> HsQuote GhcRn
-> [PendingRnSplice]
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcUntypedBracket HsExpr GhcRn
rn_expr HsQuote GhcRn
brack [PendingRnSplice]
ps ExpRhoType
res_ty
= do { String -> SDoc -> TcRn ()
traceTc String
"tc_bracket untyped" (HsQuote GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsQuote GhcRn
brack SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [PendingRnSplice] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PendingRnSplice]
ps)
; (brack_info, expected_type) <- HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsQuote GhcRn
brack
; ps' <- case quoteWrapperTyVarTy <$> brack_info of
Just Type
m_var -> (PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice)
-> [PendingRnSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type
-> PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice Type
m_var) [PendingRnSplice]
ps
DFunInstType
Nothing -> Bool
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a. HasCallStack => Bool -> a -> a
assert ([PendingRnSplice] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PendingRnSplice]
ps) (IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice])
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a b. (a -> b) -> a -> b
$ [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
; traceTc "tc_bracket done untyped" (ppr expected_type)
; tcWrapResultO BracketOrigin rn_expr
(HsUntypedBracket (HsBracketTc { hsb_quote = brack, hsb_ty = expected_type
, hsb_wrap = brack_info, hsb_splices = ps' })
(XQuote noExtField))
expected_type res_ty
}
mkMetaTyVar :: TcM TyVar
mkMetaTyVar :: IOEnv (Env TcGblEnv TcLclEnv) TyVar
mkMetaTyVar =
FastString -> Type -> IOEnv (Env TcGblEnv TcLclEnv) TyVar
newNamedFlexiTyVar (String -> FastString
fsLit String
"m") (HasDebugCallStack => Type -> Type -> Type
Type -> Type -> Type
mkVisFunTyMany Type
liftedTypeKind Type
liftedTypeKind)
emitQuoteWanted :: Type -> TcM EvVar
emitQuoteWanted :: Type -> IOEnv (Env TcGblEnv TcLclEnv) TyVar
emitQuoteWanted Type
m_var = do
quote_con <- Name -> TcM TyCon
tcLookupTyCon Name
quoteClassName
emitWantedEvVar BracketOrigin $
mkTyConApp quote_con [m_var]
brackTy :: HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy :: HsQuote GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsQuote GhcRn
b =
let mkTy :: Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
n = do
m_var <- TyVar -> Type
mkTyVarTy (TyVar -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) TyVar
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) TyVar
mkMetaTyVar
ev_var <- emitQuoteWanted m_var
final_ty <- mkAppTy m_var <$> tcMetaTy n
let wrapper = TyVar -> Type -> QuoteWrapper
QuoteWrapper TyVar
ev_var Type
m_var
return (Just wrapper, final_ty)
in
case HsQuote GhcRn
b of
(VarBr {}) -> (Maybe QuoteWrapper
forall a. Maybe a
Nothing,) (Type -> (Maybe QuoteWrapper, Type))
-> IOEnv (Env TcGblEnv TcLclEnv) Type
-> TcM (Maybe QuoteWrapper, Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
nameTyConName
(ExpBr {}) -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
expTyConName
(TypBr {}) -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
typeTyConName
(DecBrG {}) -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
decsTyConName
(PatBr {}) -> Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
patTyConName
(DecBrL {}) -> String -> TcM (Maybe QuoteWrapper, Type)
forall a. HasCallStack => String -> a
panic String
"tcBrackTy: Unexpected DecBrL"
tcPendingSplice :: TcType
-> PendingRnSplice
-> TcM PendingTcSplice
tcPendingSplice :: Type
-> PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice Type
m_var (PendingRnSplice UntypedSpliceFlavour
flavour Name
splice_name LHsExpr GhcRn
expr)
= do { meta_ty <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
meta_ty_name
; let expected_type = Type -> Type -> Type
mkAppTy Type
m_var Type
meta_ty
; expr' <- tcScalingUsage ManyTy $ tcCheckPolyExpr expr expected_type
; return (PendingTcSplice splice_name expr') }
where
meta_ty_name :: Name
meta_ty_name = case UntypedSpliceFlavour
flavour of
UntypedSpliceFlavour
UntypedExpSplice -> Name
expTyConName
UntypedSpliceFlavour
UntypedPatSplice -> Name
patTyConName
UntypedSpliceFlavour
UntypedTypeSplice -> Name
typeTyConName
UntypedSpliceFlavour
UntypedDeclSplice -> Name
decsTyConName
tcTExpTy :: TcType -> TcType -> TcM TcType
tcTExpTy :: Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_ty Type
exp_ty
= do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Type -> Bool
isTauTy Type
exp_ty) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$
THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ TypedTHError -> THError
TypedTHError (TypedTHError -> THError) -> TypedTHError -> THError
forall a b. (a -> b) -> a -> b
$ Type -> TypedTHError
TypedTHWithPolyType Type
exp_ty
; codeCon <- Name -> TcM TyCon
tcLookupTyCon Name
codeTyConName
; let rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
exp_ty
; return (mkTyConApp codeCon [m_ty, rep, exp_ty]) }
quotationCtxtDoc :: LHsExpr GhcRn -> SDoc
quotationCtxtDoc :: LHsExpr GhcRn -> SDoc
quotationCtxtDoc LHsExpr GhcRn
br_body
= SDoc -> SumArity -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the Template Haskell quotation")
SumArity
2 (SDoc -> SDoc
thTyBrackets (SDoc -> SDoc) -> (LHsExpr GhcRn -> SDoc) -> LHsExpr GhcRn -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsExpr GhcRn -> SDoc
GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr (LHsExpr GhcRn -> SDoc) -> LHsExpr GhcRn -> SDoc
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn
br_body)
getUntypedSpliceBody :: HsUntypedSpliceResult (HsExpr GhcRn) -> TcM (HsExpr GhcRn)
getUntypedSpliceBody (HsUntypedSpliceTop { utsplice_result_finalizers :: forall thing. HsUntypedSpliceResult thing -> ThModFinalizers
utsplice_result_finalizers = ThModFinalizers
mod_finalizers
, utsplice_result :: forall thing. HsUntypedSpliceResult thing -> thing
utsplice_result = HsExpr GhcRn
rn_expr })
= do { ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv ThModFinalizers
mod_finalizers
; HsExpr GhcRn -> TcM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return HsExpr GhcRn
rn_expr }
getUntypedSpliceBody (HsUntypedSpliceNested {})
= String -> TcM (HsExpr GhcRn)
forall a. HasCallStack => String -> a
panic String
"tcTopUntypedSplice: invalid nested splice"
tcTypedSplice :: Name -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedSplice Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
= SDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (Name -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc Name
splice_name LHsExpr GhcRn
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
SrcSpan -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SrcSpan
forall a e. HasLoc a => GenLocated a e -> SrcSpan
getLocA LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ do
{ stage <- TcM ThStage
getStage
; case stage of
Splice {} -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
Brack ThStage
pop_stage PendingStuff
pend -> ThStage
-> PendingStuff
-> Name
-> LHsExpr GhcRn
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcNestedSplice ThStage
pop_stage PendingStuff
pend Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
RunSplice TcRef [ForeignRef (Q ())]
_ ->
String -> SDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> SDoc -> a
pprPanic (String
"tcSpliceExpr: attempted to typecheck a splice when " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"running another splice") (Maybe Name -> LHsExpr GhcRn -> SDoc
forall (p :: Pass).
OutputableBndrId p =>
Maybe Name -> LHsExpr (GhcPass p) -> SDoc
pprTypedSplice (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
splice_name) LHsExpr GhcRn
expr)
ThStage
Comp -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
}
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
= do {
res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
; q_type <- tcMetaTy qTyConName
; meta_exp_ty <- tcTExpTy q_type res_ty
; q_expr <- tcTopSpliceExpr Typed $
tcCheckMonoExpr expr meta_exp_ty
; lcl_env <- getLclEnv
; let delayed_splice
= TcLclEnv -> LHsExpr GhcRn -> Type -> LHsExpr GhcTc -> DelayedSplice
DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
expr Type
res_ty LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
q_expr
; return (HsTypedSplice delayed_splice q_expr)
}
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
isTypedSplice TcM (LHsExpr GhcTc)
tc_action
= TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall r. TcM r -> TcM r
checkNoErrs (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
ThStage -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. ThStage -> TcM a -> TcM a
setStage (SpliceType -> ThStage
Splice SpliceType
isTypedSplice) (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
do {
(mb_expr', wanted) <- IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> TcM
(Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc)), WantedConstraints)
forall a. TcM a -> TcM (Maybe a, WantedConstraints)
tryCaptureConstraints TcM (LHsExpr GhcTc)
IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
tc_action
; const_binds <- simplifyTop wanted
; case mb_expr' of
Maybe (GenLocated SrcSpanAnnA (HsExpr GhcTc))
Nothing -> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall env a. IOEnv env a
failM
Just GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsExpr GhcTc -> TcM (LHsExpr GhcTc))
-> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ TcEvBinds -> LHsExpr GhcTc -> LHsExpr GhcTc
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
const_binds) LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' }
tcNestedSplice :: ThStage -> PendingStuff -> Name
-> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcNestedSplice :: ThStage
-> PendingStuff
-> Name
-> LHsExpr GhcRn
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcNestedSplice ThStage
pop_stage (TcPending IORef [PendingTcSplice]
ps_var TcRef WantedConstraints
lie_var q :: QuoteWrapper
q@(QuoteWrapper TyVar
_ Type
m_var)) Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
= do { res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
; let rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
res_ty
; meta_exp_ty <- tcTExpTy m_var res_ty
; expr' <- setStage pop_stage $
setConstraintVar lie_var $
tcCheckMonoExpr expr meta_exp_ty
; untype_code <- tcLookupId unTypeCodeName
; let expr'' = LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkHsApp
(HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap (QuoteWrapper -> HsWrapper
applyQuoteWrapper QuoteWrapper
q)
(TyVar -> [Type] -> LHsExpr GhcTc
nlHsTyApp TyVar
untype_code [Type
rep, Type
res_ty])) LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr'
; ps <- readMutVar ps_var
; writeMutVar ps_var (PendingTcSplice splice_name expr'' : ps)
; return stubNestedSplice }
tcNestedSplice ThStage
_ PendingStuff
_ Name
splice_name LHsExpr GhcRn
_ ExpRhoType
_
= String -> SDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcNestedSplice: rename stage found" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
splice_name)
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice (DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
orig_expr Type
res_ty LHsExpr GhcTc
q_expr)
= TcLclEnv -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall gbl a.
TcLclEnv -> TcRnIf gbl TcLclEnv a -> TcRnIf gbl TcLclEnv a
restoreLclEnv TcLclEnv
lcl_env (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
do { zonked_ty <- ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. ZonkM a -> TcM a
liftZonkM (ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type)
-> ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a b. (a -> b) -> a -> b
$ Type -> ZonkM Type
zonkTcType Type
res_ty
; zonked_q_expr <- zonkTopLExpr q_expr
; modfinalizers_ref <- newTcRef []
; expr2 <- setStage (RunSplice modfinalizers_ref) $
runMetaE zonked_q_expr
; mod_finalizers <- readTcRef modfinalizers_ref
; addModFinalizersWithLclEnv $ ThModFinalizers mod_finalizers
; traceSplice (SpliceInfo { spliceDescription = "expression"
, spliceIsDecl = False
, spliceSource = Just orig_expr
, spliceGenerated = ppr expr2 })
; (res, wcs) <-
captureConstraints $
addErrCtxt (spliceResultDoc zonked_q_expr) $ do
{ (exp3, _fvs) <- rnLExpr expr2
; tcCheckMonoExpr exp3 zonked_ty }
; ev <- simplifyTop wcs
; return $ unLoc (mkHsDictLet (EvBinds ev) res)
}
typedSpliceCtxtDoc :: SplicePointName -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc :: Name -> LHsExpr GhcRn -> SDoc
typedSpliceCtxtDoc Name
n LHsExpr GhcRn
splice
= SDoc -> SumArity -> SDoc -> SDoc
hang (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the Template Haskell splice")
SumArity
2 (Maybe Name -> LHsExpr GhcRn -> SDoc
forall (p :: Pass).
OutputableBndrId p =>
Maybe Name -> LHsExpr (GhcPass p) -> SDoc
pprTypedSplice (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n) LHsExpr GhcRn
splice)
spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc LHsExpr GhcTc
expr
= [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the result of the splice:"
, SumArity -> SDoc -> SDoc
nest SumArity
2 (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"$$" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr)
, String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"To see what the splice expanded to, use -ddump-splices"]
stubNestedSplice :: HsExpr GhcTc
stubNestedSplice :: HsExpr GhcTc
stubNestedSplice = Bool -> String -> SDoc -> HsExpr GhcTc -> HsExpr GhcTc
forall a. HasCallStack => Bool -> String -> SDoc -> a -> a
warnPprTrace Bool
True String
"stubNestedSplice" SDoc
forall doc. IsOutput doc => doc
empty (HsExpr GhcTc -> HsExpr GhcTc) -> HsExpr GhcTc -> HsExpr GhcTc
forall a b. (a -> b) -> a -> b
$
XLitE GhcTc -> HsLit GhcTc -> HsExpr GhcTc
forall p. XLitE p -> HsLit p -> HsExpr p
HsLit XLitE GhcTc
NoExtField
noExtField (String -> HsLit GhcTc
forall (p :: Pass). String -> HsLit (GhcPass p)
mkHsString String
"stubNestedSplice")
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
runAnnotation CoreAnnTarget
target LHsExpr GhcRn
expr = do
loc <- TcRn SrcSpan
getSrcSpanM
data_class <- tcLookupClass dataClassName
to_annotation_wrapper_id <- tcLookupId toAnnotationWrapperName
zonked_wrapped_expr' <- zonkTopLExpr =<< tcTopSpliceExpr Untyped (
do { (expr', expr_ty) <- tcInferRhoNC expr
; wrapper <- instCall AnnOrigin [expr_ty] [mkClassPred data_class [expr_ty]]
; let loc' = SrcSpan -> SrcSpanAnnA
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc
; let specialised_to_annotation_wrapper_expr
= SrcSpanAnnA
-> HsExpr GhcTc -> GenLocated SrcSpanAnnA (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrapper
(XVar GhcTc -> LIdP GhcTc -> HsExpr GhcTc
forall p. XVar p -> LIdP p -> HsExpr p
HsVar XVar GhcTc
NoExtField
noExtField (SrcSpanAnnN -> TyVar -> GenLocated SrcSpanAnnN TyVar
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnN
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) TyVar
to_annotation_wrapper_id)))
; return (L loc' (HsApp noExtField
specialised_to_annotation_wrapper_expr expr'))
})
serialized <- runMetaAW zonked_wrapped_expr'
return Annotation {
ann_target = target,
ann_value = serialized
}
convertAnnotationWrapper :: ForeignHValue -> TcM Serialized
convertAnnotationWrapper :: ForeignHValue -> TcM Serialized
convertAnnotationWrapper ForeignHValue
fhv = do
interp <- TcM Interp
tcGetInterp
case interpInstance interp of
ExternalInterp {} -> THResultType -> ForeignHValue -> TcM Serialized
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THAnnWrapper ForeignHValue
fhv
#if defined(HAVE_INTERNAL_INTERPRETER)
InterpInstance
InternalInterp -> do
annotation_wrapper <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue)
-> IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a b. (a -> b) -> a -> b
$ Interp -> ForeignHValue -> IO HValue
forall a. Interp -> ForeignRef a -> IO a
wormhole Interp
interp ForeignHValue
fhv
return $
case unsafeCoerce annotation_wrapper of
AnnotationWrapper a
value | let serialized :: Serialized
serialized = (a -> [Word8]) -> a -> Serialized
forall a. Typeable a => (a -> [Word8]) -> a -> Serialized
toSerialized a -> [Word8]
forall a. Data a => a -> [Word8]
serializeWithData a
value ->
Serialized -> ()
seqSerialized Serialized
serialized () -> Serialized -> Serialized
forall a b. a -> b -> b
`seq` Serialized
serialized
seqSerialized :: Serialized -> ()
seqSerialized :: Serialized -> ()
seqSerialized (Serialized TypeRep
the_type [Word8]
bytes) = TypeRep
the_type TypeRep -> () -> ()
forall a b. a -> b -> b
`seq` [Word8]
bytes [Word8] -> () -> ()
forall a b. [a] -> b -> b
`seqList` ()
#endif
runQuasi :: TH.Q a -> TcM a
runQuasi :: forall a. Q a -> TcM a
runQuasi Q a
act = Q a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Quasi m => Q a -> m a
TH.runQ Q a
act
runRemoteModFinalizers :: ThModFinalizers -> TcM ()
runRemoteModFinalizers :: ThModFinalizers -> TcRn ()
runRemoteModFinalizers (ThModFinalizers [ForeignRef (Q ())]
finRefs) = do
let withForeignRefs :: [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [] [RemoteRef a] -> IO b
f = [RemoteRef a] -> IO b
f []
withForeignRefs (ForeignRef a
x : [ForeignRef a]
xs) [RemoteRef a] -> IO b
f = ForeignRef a -> (RemoteRef a -> IO b) -> IO b
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef a
x ((RemoteRef a -> IO b) -> IO b) -> (RemoteRef a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \RemoteRef a
r ->
[ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef a]
xs (([RemoteRef a] -> IO b) -> IO b)
-> ([RemoteRef a] -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \[RemoteRef a]
rs -> [RemoteRef a] -> IO b
f (RemoteRef a
r RemoteRef a -> [RemoteRef a] -> [RemoteRef a]
forall a. a -> [a] -> [a]
: [RemoteRef a]
rs)
interp <- TcM Interp
tcGetInterp
case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InterpInstance
InternalInterp -> do
qs <- IO [Q ()] -> IOEnv (Env TcGblEnv TcLclEnv) [Q ()]
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([ForeignRef (Q ())]
-> ([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()]
forall {a} {b}. [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef (Q ())]
finRefs (([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()])
-> ([RemoteRef (Q ())] -> IO [Q ()]) -> IO [Q ()]
forall a b. (a -> b) -> a -> b
$ (RemoteRef (Q ()) -> IO (Q ())) -> [RemoteRef (Q ())] -> IO [Q ()]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM RemoteRef (Q ()) -> IO (Q ())
forall a. RemoteRef a -> IO a
localRef)
runQuasi $ sequence_ qs
#endif
ExternalInterp ExtInterp
ext -> ExtInterp -> (forall d. ExtInterpInstance d -> TcRn ()) -> TcRn ()
forall (m :: * -> *) a.
ExceptionMonad m =>
ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
withExtInterp ExtInterp
ext ((forall d. ExtInterpInstance d -> TcRn ()) -> TcRn ())
-> (forall d. ExtInterpInstance d -> TcRn ()) -> TcRn ()
forall a b. (a -> b) -> a -> b
$ \ExtInterpInstance d
inst -> do
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
th_state <- readTcRef (tcg_th_remote_state tcg)
case th_state of
Maybe (ForeignRef (IORef QState))
Nothing -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just ForeignRef (IORef QState)
fhv -> do
r <- IO (DelayedResponse (QResult ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (DelayedResponse (QResult ()))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (DelayedResponse (QResult ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (DelayedResponse (QResult ()))
forall a b. (a -> b) -> a -> b
$ ForeignRef (IORef QState)
-> (RemoteRef (IORef QState) -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ()))
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef (IORef QState)
fhv ((RemoteRef (IORef QState) -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ())))
-> (RemoteRef (IORef QState) -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ()))
forall a b. (a -> b) -> a -> b
$ \RemoteRef (IORef QState)
st ->
[ForeignRef (Q ())]
-> ([RemoteRef (Q ())] -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ()))
forall {a} {b}. [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef (Q ())]
finRefs (([RemoteRef (Q ())] -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ())))
-> ([RemoteRef (Q ())] -> IO (DelayedResponse (QResult ())))
-> IO (DelayedResponse (QResult ()))
forall a b. (a -> b) -> a -> b
$ \[RemoteRef (Q ())]
qrefs ->
ExtInterpInstance d
-> Message (QResult ()) -> IO (DelayedResponse (QResult ()))
forall d a.
ExtInterpInstance d -> Message a -> IO (DelayedResponse a)
sendMessageDelayedResponse ExtInterpInstance d
inst (RemoteRef (IORef QState)
-> [RemoteRef (Q ())] -> Message (QResult ())
RunModFinalizers RemoteRef (IORef QState)
st [RemoteRef (Q ())]
qrefs)
() <- runRemoteTH inst []
qr <- liftIO $ receiveDelayedResponse inst r
checkQResult qr
runQResult
:: (a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult :: forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult a -> String
show_th Origin -> SrcSpan -> a -> b
f ForeignHValue -> TcM a
runQ SrcSpan
expr_span ForeignHValue
hval
= do { th_result <- ForeignHValue -> TcM a
runQ ForeignHValue
hval
; th_origin <- getThSpliceOrigin
; traceTc "Got TH result:" (text (show_th th_result))
; return (f th_origin expr_span th_result) }
runMeta :: (MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta :: forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap LHsExpr GhcTc
e = do
hooks <- IOEnv (Env TcGblEnv TcLclEnv) Hooks
forall (m :: * -> *). HasHooks m => m Hooks
getHooks
case runMetaHook hooks of
Maybe (MetaHook (IOEnv (Env TcGblEnv TcLclEnv)))
Nothing -> MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
defaultRunMeta LHsExpr GhcTc
e
Just MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
h -> MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn
unwrap MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
h LHsExpr GhcTc
e
defaultRunMeta :: MetaHook TcM
defaultRunMeta :: MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
defaultRunMeta (MetaE LHsExpr GhcPs -> MetaResult
r)
= (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> MetaResult)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsExpr GhcPs -> MetaResult
GenLocated SrcSpanAnnA (HsExpr GhcPs) -> MetaResult
r (IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SDoc)
-> (SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Exp -> String)
-> (Origin
-> SrcSpan
-> Exp
-> Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> (ForeignHValue -> TcM Exp)
-> SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Exp -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> Exp -> Either RunSpliceFailReason (LHsExpr GhcPs)
Origin
-> SrcSpan
-> Exp
-> Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsExpr GhcPs))
convertToHsExpr ForeignHValue -> TcM Exp
runTHExp)
defaultRunMeta (MetaP LPat GhcPs -> MetaResult
r)
= (GenLocated SrcSpanAnnA (Pat GhcPs) -> MetaResult)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LPat GhcPs -> MetaResult
GenLocated SrcSpanAnnA (Pat GhcPs) -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
-> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (Pat GhcPs) -> SDoc)
-> (SrcSpan
-> ForeignHValue
-> TcM
(Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (Pat GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Pat -> String)
-> (Origin
-> SrcSpan
-> Pat
-> Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> (ForeignHValue -> TcM Pat)
-> SrcSpan
-> ForeignHValue
-> TcM
(Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Pat -> String
forall a. Ppr a => a -> String
TH.pprint Origin -> SrcSpan -> Pat -> Either RunSpliceFailReason (LPat GhcPs)
Origin
-> SrcSpan
-> Pat
-> Either RunSpliceFailReason (GenLocated SrcSpanAnnA (Pat GhcPs))
convertToPat ForeignHValue -> TcM Pat
runTHPat)
defaultRunMeta (MetaT LHsType GhcPs -> MetaResult
r)
= (GenLocated SrcSpanAnnA (HsType GhcPs) -> MetaResult)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsType GhcPs -> MetaResult
GenLocated SrcSpanAnnA (HsType GhcPs) -> MetaResult
r (IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
-> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (GenLocated SrcSpanAnnA (HsType GhcPs) -> SDoc)
-> (SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs))))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True GenLocated SrcSpanAnnA (HsType GhcPs) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((Type -> String)
-> (Origin
-> SrcSpan
-> Type
-> Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> (ForeignHValue -> TcM Type)
-> SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs)))
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Type -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> Type -> Either RunSpliceFailReason (LHsType GhcPs)
Origin
-> SrcSpan
-> Type
-> Either
RunSpliceFailReason (GenLocated SrcSpanAnnA (HsType GhcPs))
convertToHsType ForeignHValue -> TcM Type
runTHType)
defaultRunMeta (MetaD [LHsDecl GhcPs] -> MetaResult
r)
= ([GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> MetaResult)
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [LHsDecl GhcPs] -> MetaResult
[GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> MetaResult
r (IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ([GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> SDoc)
-> (SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)]))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True [GenLocated SrcSpanAnnA (HsDecl GhcPs)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (([Dec] -> String)
-> (Origin
-> SrcSpan
-> [Dec]
-> Either
RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> (ForeignHValue -> TcM [Dec])
-> SrcSpan
-> ForeignHValue
-> TcM
(Either
RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
forall a b.
(a -> String)
-> (Origin -> SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult [Dec] -> String
forall a. Ppr a => a -> String
TH.pprint Origin
-> SrcSpan -> [Dec] -> Either RunSpliceFailReason [LHsDecl GhcPs]
Origin
-> SrcSpan
-> [Dec]
-> Either
RunSpliceFailReason [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
convertToHsDecls ForeignHValue -> TcM [Dec]
runTHDec)
defaultRunMeta (MetaAW Serialized -> MetaResult
r)
= (Serialized -> MetaResult) -> TcM Serialized -> TcM MetaResult
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Serialized -> MetaResult
r (TcM Serialized -> TcM MetaResult)
-> (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> TcM Serialized)
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> TcM MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (Serialized -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> LHsExpr GhcTc
-> TcM Serialized
forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
False (SDoc -> Serialized -> SDoc
forall a b. a -> b -> a
const SDoc
forall doc. IsOutput doc => doc
empty) ((ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> SrcSpan
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall a b. a -> b -> a
const ((ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> SrcSpan
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized))
-> (ForeignHValue -> TcM (Either RunSpliceFailReason Serialized))
-> SrcSpan
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall a b. (a -> b) -> a -> b
$ (Serialized -> Either RunSpliceFailReason Serialized)
-> TcM Serialized -> TcM (Either RunSpliceFailReason Serialized)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Serialized -> Either RunSpliceFailReason Serialized
forall a b. b -> Either a b
Right (TcM Serialized -> TcM (Either RunSpliceFailReason Serialized))
-> (ForeignHValue -> TcM Serialized)
-> ForeignHValue
-> TcM (Either RunSpliceFailReason Serialized)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForeignHValue -> TcM Serialized
convertAnnotationWrapper)
runMetaAW :: LHsExpr GhcTc
-> TcM Serialized
runMetaAW :: LHsExpr GhcTc -> TcM Serialized
runMetaAW = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM Serialized)
-> LHsExpr GhcTc -> TcM Serialized
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM Serialized
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f Serialized
metaRequestAW
runMetaE :: LHsExpr GhcTc
-> TcM (LHsExpr GhcPs)
runMetaE :: LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
runMetaE = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LHsExpr GhcPs)
metaRequestE
runMetaP :: LHsExpr GhcTc
-> TcM (LPat GhcPs)
runMetaP :: LHsExpr GhcTc -> TcM (LPat GhcPs)
runMetaP = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LPat GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LPat GhcPs)
metaRequestP
runMetaT :: LHsExpr GhcTc
-> TcM (LHsType GhcPs)
runMetaT :: LHsExpr GhcTc -> TcM (LHsType GhcPs)
runMetaT = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs)))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM (LHsType GhcPs)
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LHsType GhcPs)
metaRequestT
runMetaD :: LHsExpr GhcTc
-> TcM [LHsDecl GhcPs]
runMetaD :: LHsExpr GhcTc -> TcM [LHsDecl GhcPs]
runMetaD = (MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall hs_syn.
(MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc -> TcM [LHsDecl GhcPs]
MetaHook (IOEnv (Env TcGblEnv TcLclEnv))
-> LHsExpr GhcTc
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f [LHsDecl GhcPs]
metaRequestD
runMeta' :: Bool
-> (hs_syn -> SDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' :: forall hs_syn.
Bool
-> (hs_syn -> SDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
show_code hs_syn -> SDoc
ppr_hs SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn)
run_and_convert LHsExpr GhcTc
expr
= do { String -> SDoc -> TcRn ()
traceTc String
"About to run" (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr)
; TcRn ()
recordThSpliceUse
; TcRn ()
failIfErrsM
; hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
; expr' <- withPlugins (hsc_plugins hsc_env) spliceRunAction expr
; (ds_msgs, mb_ds_expr) <- initDsTc (dsLExpr expr')
; logger <- getLogger
; diag_opts <- initDiagOpts <$> getDynFlags
; print_config <- initDsMessageOpts <$> getDynFlags
; liftIO $ printMessages logger print_config diag_opts ds_msgs
; ds_expr <- case mb_ds_expr of
Maybe CoreExpr
Nothing -> IOEnv (Env TcGblEnv TcLclEnv) CoreExpr
forall env a. IOEnv env a
failM
Just CoreExpr
ds_expr ->
do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Messages DsMessage -> Bool
forall e. Messages e -> Bool
errorsOrFatalWarningsFound Messages DsMessage
ds_msgs)
TcRn ()
forall env a. IOEnv env a
failM
; CoreExpr -> IOEnv (Env TcGblEnv TcLclEnv) CoreExpr
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
ds_expr }
; src_span <- getSrcSpanM
; traceTc "About to run (desugared)" (ppr ds_expr)
; either_hval <- tryM $ liftIO $
GHC.Driver.Main.hscCompileCoreExpr hsc_env src_span ds_expr
; case either_hval of {
Left IOEnvFailure
exn -> SplicePhase -> IOEnvFailure -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
SplicePhase_CompileAndLink IOEnvFailure
exn ;
Right (ForeignHValue
hval, [Linkable]
needed_mods, PkgsLoaded
needed_pkgs) -> do
{
let expr_span :: SrcSpan
expr_span = GenLocated SrcSpanAnnA (HsExpr GhcTc) -> SrcSpan
forall a e. HasLoc a => GenLocated a e -> SrcSpan
getLocA LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr
; [Linkable] -> PkgsLoaded -> TcRn ()
recordThNeededRuntimeDeps [Linkable]
needed_mods PkgsLoaded
needed_pkgs
; either_tval <- IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall env r. IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn))
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall a b. (a -> b) -> a -> b
$
SrcSpan
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
expr_span (IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn)
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a b. (a -> b) -> a -> b
$
do { mb_result <- SrcSpan -> ForeignHValue -> TcM (Either RunSpliceFailReason hs_syn)
run_and_convert SrcSpan
expr_span ForeignHValue
hval
; case mb_result of
Left RunSpliceFailReason
err -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn)
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a b. (a -> b) -> a -> b
$
THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ SpliceFailReason -> THError
THSpliceFailed (SpliceFailReason -> THError) -> SpliceFailReason -> THError
forall a b. (a -> b) -> a -> b
$ RunSpliceFailReason -> SpliceFailReason
RunSpliceFailure RunSpliceFailReason
err
Right hs_syn
result -> do { String -> SDoc -> TcRn ()
traceTc String
"Got HsSyn result:" (hs_syn -> SDoc
ppr_hs hs_syn
result)
; hs_syn -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (hs_syn -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn)
-> hs_syn -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a b. (a -> b) -> a -> b
$! hs_syn
result } }
; case either_tval of
Right hs_syn
v -> hs_syn -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return hs_syn
v
Left SomeException
se -> case SomeException -> Maybe IOEnvFailure
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se of
Just IOEnvFailure
IOEnvFailure -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall env a. IOEnv env a
failM
Maybe IOEnvFailure
_ -> SplicePhase
-> SomeException -> IOEnv (Env TcGblEnv TcLclEnv) hs_syn
forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
SplicePhase_Run SomeException
se
}}}
where
fail_with_exn :: Exception e => SplicePhase -> e -> TcM a
fail_with_exn :: forall e a. Exception e => SplicePhase -> e -> TcM a
fail_with_exn SplicePhase
phase e
exn = do
exn_msg <- IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> IOEnv (Env TcGblEnv TcLclEnv) String)
-> IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall a b. (a -> b) -> a -> b
$ e -> IO String
forall e. Exception e => e -> IO String
Panic.safeShowException e
exn
failWithTc $ TcRnTHError $ THSpliceFailed $
SpliceThrewException phase (toException exn) exn_msg expr show_code
instance TH.Quasi TcM where
qNewName :: String -> TcM Name
qNewName String
s = do { u <- TcRnIf TcGblEnv TcLclEnv Unique
forall gbl lcl. TcRnIf gbl lcl Unique
newUnique
; let i = Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Unique -> Word64
getKey Unique
u)
; return (TH.mkNameU s i) }
qReport :: Bool -> String -> TcRn ()
qReport Bool
True String
msg = String -> TcRn () -> TcRn ()
forall a b. [a] -> b -> b
seqList String
msg (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ Bool -> String -> THError
ReportCustomQuasiError Bool
True String
msg
qReport Bool
False String
msg = String -> TcRn () -> TcRn ()
forall a b. [a] -> b -> b
seqList String
msg (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addDiagnostic (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ Bool -> String -> THError
ReportCustomQuasiError Bool
False String
msg
qLocation :: TcM TH.Loc
qLocation :: TcM Loc
qLocation = do { m <- IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule
; l <- getSrcSpanM
; r <- case l of
UnhelpfulSpan UnhelpfulSpanReason
_ -> String -> SDoc -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"qLocation: Unhelpful location"
(SrcSpan -> SDoc
forall a. Outputable a => a -> SDoc
ppr SrcSpan
l)
RealSrcSpan RealSrcSpan
s Maybe BufSpan
_ -> RealSrcSpan -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return RealSrcSpan
s
; return (TH.Loc { TH.loc_filename = unpackFS (srcSpanFile r)
, TH.loc_module = moduleNameString (moduleName m)
, TH.loc_package = unitString (moduleUnit m)
, TH.loc_start = (srcSpanStartLine r, srcSpanStartCol r)
, TH.loc_end = (srcSpanEndLine r, srcSpanEndCol r) }) }
qLookupName :: Bool -> String -> TcM (Maybe Name)
qLookupName = Bool -> String -> TcM (Maybe Name)
lookupName
qReify :: Name -> TcM Info
qReify = Name -> TcM Info
reify
qReifyFixity :: Name -> TcM (Maybe Fixity)
qReifyFixity Name
nm = Name -> TcM Name
lookupThName Name
nm TcM Name -> (Name -> TcM (Maybe Fixity)) -> TcM (Maybe Fixity)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe Fixity)
reifyFixity
qReifyType :: Name -> TcM Type
qReifyType = Name -> TcM Type
reifyTypeOfThing
qReifyInstances :: Name -> [Type] -> TcM [Dec]
qReifyInstances = Name -> [Type] -> TcM [Dec]
reifyInstances
qReifyRoles :: Name -> TcM [Role]
qReifyRoles = Name -> TcM [Role]
reifyRoles
qReifyAnnotations :: forall a. Data a => AnnLookup -> TcM [a]
qReifyAnnotations = AnnLookup -> TcM [a]
forall a. Data a => AnnLookup -> TcM [a]
reifyAnnotations
qReifyModule :: Module -> TcM ModuleInfo
qReifyModule = Module -> TcM ModuleInfo
reifyModule
qReifyConStrictness :: Name -> TcM [DecidedStrictness]
qReifyConStrictness Name
nm = do { nm' <- Name -> TcM Name
lookupThName Name
nm
; dc <- tcLookupDataCon nm'
; let bangs = DataCon -> [HsImplBang]
dataConImplBangs DataCon
dc
; return (map reifyDecidedStrictness bangs) }
qRecover :: forall a. TcM a -> TcM a -> TcM a
qRecover TcM a
recover TcM a
main = TcM a -> TcM a -> TcM a
forall a. TcM a -> TcM a -> TcM a
tryTcDiscardingErrs TcM a
recover TcM a
main
qGetPackageRoot :: IOEnv (Env TcGblEnv TcLclEnv) String
qGetPackageRoot = do
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
return $ fromMaybe "." (workingDirectory dflags)
qAddDependentFile :: String -> TcRn ()
qAddDependentFile String
fp = do
ref <- (TcGblEnv -> TcRef [String])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [String])
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [String]
tcg_dependent_files TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
dep_files <- readTcRef ref
writeTcRef ref (fp:dep_files)
qAddTempFile :: String -> IOEnv (Env TcGblEnv TcLclEnv) String
qAddTempFile String
suffix = do
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
logger <- getLogger
tmpfs <- hsc_tmpfs <$> getTopEnv
liftIO $ newTempName logger tmpfs (tmpDir dflags) TFL_GhcSession suffix
qAddTopDecls :: [Dec] -> TcRn ()
qAddTopDecls [Dec]
thds = do
l <- TcRn SrcSpan
getSrcSpanM
th_origin <- getThSpliceOrigin
let either_hval = Origin
-> SrcSpan -> [Dec] -> Either RunSpliceFailReason [LHsDecl GhcPs]
convertToHsDecls Origin
th_origin SrcSpan
l [Dec]
thds
ds <- case either_hval of
Left RunSpliceFailReason
exn -> TcRnMessage
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> TcRnMessage
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ AddTopDeclsError -> THError
AddTopDeclsError (AddTopDeclsError -> THError) -> AddTopDeclsError -> THError
forall a b. (a -> b) -> a -> b
$
RunSpliceFailReason -> AddTopDeclsError
AddTopDeclsRunSpliceFailure RunSpliceFailReason
exn
Right [LHsDecl GhcPs]
ds -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> IOEnv
(Env TcGblEnv TcLclEnv) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [LHsDecl GhcPs]
[GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds
mapM_ (checkTopDecl . unLoc) ds
th_topdecls_var <- fmap tcg_th_topdecls getGblEnv
updTcRef th_topdecls_var (\[GenLocated SrcSpanAnnA (HsDecl GhcPs)]
topds -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
ds [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
forall a. [a] -> [a] -> [a]
++ [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
topds)
where
checkTopDecl :: HsDecl GhcPs -> TcM ()
checkTopDecl :: HsDecl GhcPs -> TcRn ()
checkTopDecl (ValD XValD GhcPs
_ HsBind GhcPs
binds)
= (RdrName -> TcRn ()) -> [RdrName] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ RdrName -> TcRn ()
bindName (CollectFlag GhcPs -> HsBind GhcPs -> [IdP GhcPs]
forall p idR.
CollectPass p =>
CollectFlag p -> HsBindLR p idR -> [IdP p]
collectHsBindBinders CollectFlag GhcPs
forall p. CollectFlag p
CollNoDictBinders HsBind GhcPs
binds)
checkTopDecl (SigD XSigD GhcPs
_ Sig GhcPs
_)
= () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (AnnD XAnnD GhcPs
_ AnnDecl GhcPs
_)
= () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (ForD XForD GhcPs
_ (ForeignImport { fd_name :: forall pass. ForeignDecl pass -> LIdP pass
fd_name = L SrcSpanAnnN
_ RdrName
name }))
= RdrName -> TcRn ()
bindName RdrName
name
checkTopDecl HsDecl GhcPs
d
= TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ AddTopDeclsError -> THError
AddTopDeclsError (AddTopDeclsError -> THError) -> AddTopDeclsError -> THError
forall a b. (a -> b) -> a -> b
$ HsDecl GhcPs -> AddTopDeclsError
InvalidTopDecl HsDecl GhcPs
d
bindName :: RdrName -> TcM ()
bindName :: RdrName -> TcRn ()
bindName (Exact Name
n)
= do { th_topnames_var <- (TcGblEnv -> TcRef FreeVars)
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef FreeVars)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef FreeVars
tcg_th_topnames TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; updTcRef th_topnames_var (\FreeVars
ns -> FreeVars -> Name -> FreeVars
extendNameSet FreeVars
ns Name
n)
}
bindName RdrName
name = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THNameError -> THError
THNameError (THNameError -> THError) -> THNameError -> THError
forall a b. (a -> b) -> a -> b
$ RdrName -> THNameError
NonExactName RdrName
name
qAddForeignFilePath :: ForeignSrcLang -> String -> TcRn ()
qAddForeignFilePath ForeignSrcLang
lang String
fp = do
var <- (TcGblEnv -> TcRef [(ForeignSrcLang, String)])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [(ForeignSrcLang, String)])
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [(ForeignSrcLang, String)]
tcg_th_foreign_files TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
updTcRef var ((lang, fp) :)
qAddModFinalizer :: Q () -> TcRn ()
qAddModFinalizer Q ()
fin = do
r <- IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ()))
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ())))
-> IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ()))
forall a b. (a -> b) -> a -> b
$ Q () -> IO (RemoteRef (Q ()))
forall a. a -> IO (RemoteRef a)
mkRemoteRef Q ()
fin
fref <- liftIO $ mkForeignRef r (freeRemoteRef r)
addModFinalizerRef fref
qAddCorePlugin :: String -> TcRn ()
qAddCorePlugin String
plugin = do
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
let fc = HscEnv -> FinderCache
hsc_FC HscEnv
hsc_env
let home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
let dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
let fopts = DynFlags -> FinderOpts
initFinderOpts DynFlags
dflags
r <- liftIO $ findHomeModule fc fopts home_unit (mkModuleName plugin)
let err = THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ String -> THError
AddInvalidCorePlugin String
plugin
case r of
Found {} -> TcRnMessage -> TcRn ()
addErr TcRnMessage
err
FoundMultiple {} -> TcRnMessage -> TcRn ()
addErr TcRnMessage
err
FindResult
_ -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
th_coreplugins_var <- tcg_th_coreplugins <$> getGblEnv
updTcRef th_coreplugins_var (plugin:)
qGetQ :: forall a. Typeable a => TcM (Maybe a)
qGetQ :: forall a. Typeable a => TcM (Maybe a)
qGetQ = do
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef (Map TypeRep Dynamic)
tcg_th_state TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
th_state <- readTcRef th_state_var
return (Map.lookup (typeRep (Proxy :: Proxy a)) th_state >>= fromDynamic)
qPutQ :: forall a. Typeable a => a -> TcRn ()
qPutQ a
x = do
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef (Map TypeRep Dynamic)
tcg_th_state TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
updTcRef th_state_var (\Map TypeRep Dynamic
m -> TypeRep -> Dynamic -> Map TypeRep Dynamic -> Map TypeRep Dynamic
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (a -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf a
x) (a -> Dynamic
forall a. Typeable a => a -> Dynamic
toDyn a
x) Map TypeRep Dynamic
m)
qIsExtEnabled :: Extension -> TcM Bool
qIsExtEnabled = Extension -> TcM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM
qExtsEnabled :: TcM [Extension]
qExtsEnabled =
EnumSet Extension -> [Extension]
forall a. Enum a => EnumSet a -> [a]
EnumSet.toList (EnumSet Extension -> [Extension])
-> (HscEnv -> EnumSet Extension) -> HscEnv -> [Extension]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> EnumSet Extension
extensionFlags (DynFlags -> EnumSet Extension)
-> (HscEnv -> DynFlags) -> HscEnv -> EnumSet Extension
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> DynFlags
hsc_dflags (HscEnv -> [Extension])
-> TcRnIf TcGblEnv TcLclEnv HscEnv -> TcM [Extension]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
qPutDoc :: DocLoc -> String -> TcRn ()
qPutDoc DocLoc
doc_loc String
s = do
th_doc_var <- TcGblEnv -> TcRef THDocs
tcg_th_docs (TcGblEnv -> TcRef THDocs)
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef THDocs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
resolved_doc_loc <- resolve_loc doc_loc
is_local <- checkLocalName resolved_doc_loc
unless is_local $ failWithTc $ TcRnTHError $ AddDocToNonLocalDefn doc_loc
let ds = String -> HsDocString
mkGeneratedHsDocString String
s
hd = P (GenLocated SrcSpanAnnN RdrName) -> HsDocString -> HsDoc GhcPs
lexHsDoc P (GenLocated SrcSpanAnnN RdrName)
parseIdentifier HsDocString
ds
hd' <- rnHsDoc hd
updTcRef th_doc_var (Map.insert resolved_doc_loc hd')
where
resolve_loc :: DocLoc -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
resolve_loc (TH.DeclDoc Name
n) = Name -> DocLoc
DeclDoc (Name -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcM Name
lookupThName Name
n
resolve_loc (TH.ArgDoc Name
n SumArity
i) = Name -> SumArity -> DocLoc
ArgDoc (Name -> SumArity -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) (SumArity -> DocLoc)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcM Name
lookupThName Name
n IOEnv (Env TcGblEnv TcLclEnv) (SumArity -> DocLoc)
-> IOEnv (Env TcGblEnv TcLclEnv) SumArity
-> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) (a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SumArity -> IOEnv (Env TcGblEnv TcLclEnv) SumArity
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SumArity
i
resolve_loc (TH.InstDoc Type
t) = Name -> DocLoc
InstDoc (Name -> DocLoc)
-> TcM Name -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Name -> Name) -> TcM Name -> TcM Name
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Name
forall a. NamedThing a => a -> Name
getName (Type -> TcM Name
lookupThInstName Type
t)
resolve_loc DocLoc
TH.ModuleDoc = DocLoc -> IOEnv (Env TcGblEnv TcLclEnv) DocLoc
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DocLoc
ModuleDoc
checkLocalName :: DocLoc -> f Bool
checkLocalName (DeclDoc Name
n) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
checkLocalName (ArgDoc Name
n SumArity
_) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
checkLocalName (InstDoc Name
n) = GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> f (GenModule Unit) -> f (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule f (Name -> Bool) -> f Name -> f Bool
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> f Name
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
checkLocalName DocLoc
ModuleDoc = Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
qGetDoc :: DocLoc -> TcM (Maybe String)
qGetDoc (TH.DeclDoc Name
n) = Name -> TcM Name
lookupThName Name
n TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe String)
lookupDeclDoc
qGetDoc (TH.InstDoc Type
t) = Type -> TcM Name
lookupThInstName Type
t TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name -> TcM (Maybe String)
lookupDeclDoc
qGetDoc (TH.ArgDoc Name
n SumArity
i) = Name -> TcM Name
lookupThName Name
n TcM Name -> (Name -> TcM (Maybe String)) -> TcM (Maybe String)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SumArity -> Name -> TcM (Maybe String)
lookupArgDoc SumArity
i
qGetDoc DocLoc
TH.ModuleDoc = do
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
docs <- getGblEnv >>= extractDocs df
return (renderHsDocString . hsDocString <$> (docs_mod_hdr =<< docs))
lookupDeclDoc :: Name -> TcM (Maybe String)
lookupDeclDoc :: Name -> TcM (Maybe String)
lookupDeclDoc Name
nm = do
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
Docs{docs_decls} <- fmap (fromMaybe emptyDocs) $ getGblEnv >>= extractDocs df
case lookupUniqMap docs_decls nm of
Just [WithHsDocIdentifiers HsDocString GhcRn]
doc -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just ([HsDocString] -> String
renderHsDocStrings ([HsDocString] -> String) -> [HsDocString] -> String
forall a b. (a -> b) -> a -> b
$ (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> [WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString]
forall a b. (a -> b) -> [a] -> [b]
map WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString [WithHsDocIdentifiers HsDocString GhcRn]
doc)
Maybe [WithHsDocIdentifiers HsDocString GhcRn]
Nothing -> do
mIface <- Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm
case mIface of
Just ModIface { mi_docs :: forall (phase :: ModIfacePhase). ModIface_ phase -> Maybe Docs
mi_docs = Just Docs{docs_decls :: Docs -> UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
docs_decls = UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
dmap} } ->
Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ [HsDocString] -> String
renderHsDocStrings ([HsDocString] -> String)
-> ([WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString])
-> [WithHsDocIdentifiers HsDocString GhcRn]
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> [WithHsDocIdentifiers HsDocString GhcRn] -> [HsDocString]
forall a b. (a -> b) -> [a] -> [b]
map WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString ([WithHsDocIdentifiers HsDocString GhcRn] -> String)
-> Maybe [WithHsDocIdentifiers HsDocString GhcRn] -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
-> Name -> Maybe [WithHsDocIdentifiers HsDocString GhcRn]
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name [WithHsDocIdentifiers HsDocString GhcRn]
dmap Name
nm
Maybe ModIface
_ -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing
lookupArgDoc :: Int -> Name -> TcM (Maybe String)
lookupArgDoc :: SumArity -> Name -> TcM (Maybe String)
lookupArgDoc SumArity
i Name
nm = do
df <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
Docs{docs_args = argDocs} <- fmap (fromMaybe emptyDocs) $ getGblEnv >>= extractDocs df
case lookupUniqMap argDocs nm of
Just IntMap (WithHsDocIdentifiers HsDocString GhcRn)
m -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ HsDocString -> String
renderHsDocString (HsDocString -> String)
-> (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> WithHsDocIdentifiers HsDocString GhcRn
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString (WithHsDocIdentifiers HsDocString GhcRn -> String)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SumArity
-> IntMap (WithHsDocIdentifiers HsDocString GhcRn)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a. SumArity -> IntMap a -> Maybe a
IntMap.lookup SumArity
i IntMap (WithHsDocIdentifiers HsDocString GhcRn)
m
Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
Nothing -> do
mIface <- Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm
case mIface of
Just ModIface { mi_docs :: forall (phase :: ModIfacePhase). ModIface_ phase -> Maybe Docs
mi_docs = Just Docs{docs_args :: Docs
-> UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
docs_args = UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
amap} } ->
Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe String -> TcM (Maybe String))
-> Maybe String -> TcM (Maybe String)
forall a b. (a -> b) -> a -> b
$ HsDocString -> String
renderHsDocString (HsDocString -> String)
-> (WithHsDocIdentifiers HsDocString GhcRn -> HsDocString)
-> WithHsDocIdentifiers HsDocString GhcRn
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithHsDocIdentifiers HsDocString GhcRn -> HsDocString
forall a pass. WithHsDocIdentifiers a pass -> a
hsDocString (WithHsDocIdentifiers HsDocString GhcRn -> String)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
-> Name -> Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap Name (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
amap Name
nm Maybe (IntMap (WithHsDocIdentifiers HsDocString GhcRn))
-> (IntMap (WithHsDocIdentifiers HsDocString GhcRn)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn))
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SumArity
-> IntMap (WithHsDocIdentifiers HsDocString GhcRn)
-> Maybe (WithHsDocIdentifiers HsDocString GhcRn)
forall a. SumArity -> IntMap a -> Maybe a
IntMap.lookup SumArity
i)
Maybe ModIface
_ -> Maybe String -> TcM (Maybe String)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing
getExternalModIface :: Name -> TcM (Maybe ModIface)
getExternalModIface :: Name -> TcM (Maybe ModIface)
getExternalModIface Name
nm = do
isLocal <- GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (GenModule Unit -> Name -> Bool)
-> IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
-> IOEnv (Env TcGblEnv TcLclEnv) (Name -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule IOEnv (Env TcGblEnv TcLclEnv) (Name -> Bool)
-> TcM Name -> TcM Bool
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) (a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
nm
if isLocal
then pure Nothing
else case nameModule_maybe nm of
Maybe (GenModule Unit)
Nothing -> Maybe ModIface -> TcM (Maybe ModIface)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ModIface
forall a. Maybe a
Nothing
Just GenModule Unit
modNm -> do
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
iface <- liftIO $ hscGetModuleInterface hsc_env modNm
pure (Just iface)
lookupThInstName :: TH.Type -> TcM Name
lookupThInstName :: Type -> TcM Name
lookupThInstName Type
th_type = do
cls_name <- Type -> TcM Name
inst_cls_name Type
th_type
insts <- reifyInstances' cls_name (inst_arg_types th_type)
case insts of
Left (Class
_, (ClsInst
inst:[ClsInst]
_)) -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> TcM Name) -> Name -> TcM Name
forall a b. (a -> b) -> a -> b
$ ClsInst -> Name
forall a. NamedThing a => a -> Name
getName ClsInst
inst
Left (Class
_, []) -> TcM Name
noMatches
Right (TyCon
_, (FamInst
inst:[FamInst]
_)) -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> TcM Name) -> Name -> TcM Name
forall a b. (a -> b) -> a -> b
$ FamInst -> Name
forall a. NamedThing a => a -> Name
getName FamInst
inst
Right (TyCon
_, []) -> TcM Name
noMatches
where
noMatches :: TcM Name
noMatches = TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM Name) -> TcRnMessage -> TcM Name
forall a b. (a -> b) -> a -> b
$
THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ Type -> LookupTHInstNameErrReason -> THError
FailedToLookupThInstName Type
th_type LookupTHInstNameErrReason
NoMatchesFound
inst_cls_name :: TH.Type -> TcM TH.Name
inst_cls_name :: Type -> TcM Name
inst_cls_name (TH.AppT Type
t Type
_) = Type -> TcM Name
inst_cls_name Type
t
inst_cls_name (TH.SigT Type
n Type
_) = Type -> TcM Name
inst_cls_name Type
n
inst_cls_name (TH.VarT Name
n) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.ConT Name
n) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.PromotedT Name
n) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.InfixT Type
_ Name
n Type
_) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.UInfixT Type
_ Name
n Type
_) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.PromotedInfixT Type
_ Name
n Type
_) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.PromotedUInfixT Type
_ Name
n Type
_) = Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
inst_cls_name (TH.ParensT Type
t) = Type -> TcM Name
inst_cls_name Type
t
inst_cls_name (TH.ForallT [TyVarBndr Specificity]
_ [Type]
_ Type
_) = TcM Name
inst_cls_name_err
inst_cls_name (TH.ForallVisT [TyVarBndr ()]
_ Type
_) = TcM Name
inst_cls_name_err
inst_cls_name (TH.AppKindT Type
_ Type
_) = TcM Name
inst_cls_name_err
inst_cls_name (TH.TupleT SumArity
_) = TcM Name
inst_cls_name_err
inst_cls_name (TH.UnboxedTupleT SumArity
_) = TcM Name
inst_cls_name_err
inst_cls_name (TH.UnboxedSumT SumArity
_) = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.ArrowT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.MulArrowT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.EqualityT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.ListT = TcM Name
inst_cls_name_err
inst_cls_name (TH.PromotedTupleT SumArity
_) = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.PromotedNilT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.PromotedConsT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.StarT = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.ConstraintT = TcM Name
inst_cls_name_err
inst_cls_name (TH.LitT TyLit
_) = TcM Name
inst_cls_name_err
inst_cls_name Type
TH.WildCardT = TcM Name
inst_cls_name_err
inst_cls_name (TH.ImplicitParamT String
_ Type
_) = TcM Name
inst_cls_name_err
inst_cls_name_err :: TcM Name
inst_cls_name_err = TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM Name) -> TcRnMessage -> TcM Name
forall a b. (a -> b) -> a -> b
$
THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ Type -> LookupTHInstNameErrReason -> THError
FailedToLookupThInstName Type
th_type LookupTHInstNameErrReason
CouldNotDetermineInstance
inst_arg_types :: TH.Type -> [TH.Type]
inst_arg_types :: Type -> [Type]
inst_arg_types (TH.AppT Type
_ Type
args) =
let go :: Type -> [Type]
go (TH.AppT Type
t Type
ts) = Type
tType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:Type -> [Type]
go Type
ts
go Type
t = [Type
t]
in Type -> [Type]
go Type
args
inst_arg_types Type
_ = []
addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM ()
addModFinalizerRef :: ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef ForeignRef (Q ())
finRef = do
th_stage <- TcM ThStage
getStage
case th_stage of
RunSplice TcRef [ForeignRef (Q ())]
th_modfinalizers_var -> TcRef [ForeignRef (Q ())]
-> ([ForeignRef (Q ())] -> [ForeignRef (Q ())]) -> TcRn ()
forall (m :: * -> *) a. MonadIO m => TcRef a -> (a -> a) -> m ()
updTcRef TcRef [ForeignRef (Q ())]
th_modfinalizers_var (ForeignRef (Q ())
finRef ForeignRef (Q ()) -> [ForeignRef (Q ())] -> [ForeignRef (Q ())]
forall a. a -> [a] -> [a]
:)
ThStage
_ ->
String -> SDoc -> TcRn ()
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"addModFinalizer was called when no finalizers were collected"
(ThStage -> SDoc
forall a. Outputable a => a -> SDoc
ppr ThStage
th_stage)
finishTH :: TcM ()
finishTH :: TcRn ()
finishTH = do
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
case interpInstance <$> hsc_interp hsc_env of
Maybe InterpInstance
Nothing -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#if defined(HAVE_INTERNAL_INTERPRETER)
Just InterpInstance
InternalInterp -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#endif
Just (ExternalInterp {}) -> do
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
writeTcRef (tcg_th_remote_state tcg) Nothing
runTHExp :: ForeignHValue -> TcM TH.Exp
runTHExp :: ForeignHValue -> TcM Exp
runTHExp = THResultType -> ForeignHValue -> TcM Exp
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THExp
runTHPat :: ForeignHValue -> TcM TH.Pat
runTHPat :: ForeignHValue -> TcM Pat
runTHPat = THResultType -> ForeignHValue -> TcM Pat
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THPat
runTHType :: ForeignHValue -> TcM TH.Type
runTHType :: ForeignHValue -> TcM Type
runTHType = THResultType -> ForeignHValue -> TcM Type
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THType
runTHDec :: ForeignHValue -> TcM [TH.Dec]
runTHDec :: ForeignHValue -> TcM [Dec]
runTHDec = THResultType -> ForeignHValue -> TcM [Dec]
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THDec
runTH :: Binary a => THResultType -> ForeignHValue -> TcM a
runTH :: forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
ty ForeignHValue
fhv = do
interp <- TcM Interp
tcGetInterp
case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InterpInstance
InternalInterp -> do
hv <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue)
-> IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
forall a b. (a -> b) -> a -> b
$ Interp -> ForeignHValue -> IO HValue
forall a. Interp -> ForeignRef a -> IO a
wormhole Interp
interp ForeignHValue
fhv
r <- runQuasi (unsafeCoerce hv :: TH.Q a)
return r
#endif
ExternalInterp ExtInterp
ext -> ExtInterp
-> (forall d.
ExtInterpInstance d -> IOEnv (Env TcGblEnv TcLclEnv) a)
-> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a.
ExceptionMonad m =>
ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
withExtInterp ExtInterp
ext ((forall d. ExtInterpInstance d -> IOEnv (Env TcGblEnv TcLclEnv) a)
-> IOEnv (Env TcGblEnv TcLclEnv) a)
-> (forall d.
ExtInterpInstance d -> IOEnv (Env TcGblEnv TcLclEnv) a)
-> IOEnv (Env TcGblEnv TcLclEnv) a
forall a b. (a -> b) -> a -> b
$ \ExtInterpInstance d
inst -> do
rstate <- ExtInterpInstance d -> TcM (ForeignRef (IORef QState))
forall d. ExtInterpInstance d -> TcM (ForeignRef (IORef QState))
getTHState ExtInterpInstance d
inst
loc <- TH.qLocation
r <- liftIO $
withForeignRef rstate $ \RemoteRef (IORef QState)
state_hv ->
ForeignHValue
-> (RemoteRef HValue -> IO (DelayedResponse (QResult ByteString)))
-> IO (DelayedResponse (QResult ByteString))
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignHValue
fhv ((RemoteRef HValue -> IO (DelayedResponse (QResult ByteString)))
-> IO (DelayedResponse (QResult ByteString)))
-> (RemoteRef HValue -> IO (DelayedResponse (QResult ByteString)))
-> IO (DelayedResponse (QResult ByteString))
forall a b. (a -> b) -> a -> b
$ \RemoteRef HValue
q_hv ->
ExtInterpInstance d
-> Message (QResult ByteString)
-> IO (DelayedResponse (QResult ByteString))
forall d a.
ExtInterpInstance d -> Message a -> IO (DelayedResponse a)
sendMessageDelayedResponse ExtInterpInstance d
inst (RemoteRef (IORef QState)
-> RemoteRef HValue
-> THResultType
-> Maybe Loc
-> Message (QResult ByteString)
RunTH RemoteRef (IORef QState)
state_hv RemoteRef HValue
q_hv THResultType
ty (Loc -> Maybe Loc
forall a. a -> Maybe a
Just Loc
loc))
runRemoteTH inst []
qr <- liftIO $ receiveDelayedResponse inst r
bs <- checkQResult qr
return $! runGet get (LB.fromStrict bs)
runRemoteTH
:: ExtInterpInstance d
-> [Messages TcRnMessage]
-> TcM ()
runRemoteTH :: forall d. ExtInterpInstance d -> [Messages TcRnMessage] -> TcRn ()
runRemoteTH ExtInterpInstance d
inst [Messages TcRnMessage]
recovers = do
THMsg msg <- IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg)
-> IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg
forall a b. (a -> b) -> a -> b
$ ExtInterpInstance d -> IO THMsg
forall d. ExtInterpInstance d -> IO THMsg
receiveTHMessage ExtInterpInstance d
inst
case msg of
THMessage a
RunTHDone -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
THMessage a
StartRecover -> do
v <- TcRn (TcRef (Messages TcRnMessage))
getErrsVar
msgs <- readTcRef v
writeTcRef v emptyMessages
runRemoteTH inst (msgs : recovers)
EndRecover Bool
caught_error -> do
let (Messages TcRnMessage
prev_msgs, [Messages TcRnMessage]
rest) = case [Messages TcRnMessage]
recovers of
[] -> String -> (Messages TcRnMessage, [Messages TcRnMessage])
forall a. HasCallStack => String -> a
panic String
"EndRecover"
Messages TcRnMessage
a : [Messages TcRnMessage]
b -> (Messages TcRnMessage
a,[Messages TcRnMessage]
b)
v <- TcRn (TcRef (Messages TcRnMessage))
getErrsVar
warn_msgs <- getWarningMessages <$> readTcRef v
writeTcRef v $ if caught_error
then prev_msgs
else mkMessages warn_msgs `unionMessages` prev_msgs
runRemoteTH inst rest
THMessage a
_other -> do
r <- THMessage a -> TcM a
forall a. THMessage a -> TcM a
handleTHMessage THMessage a
msg
liftIO $ sendAnyValue inst r
runRemoteTH inst recovers
checkQResult :: QResult a -> TcM a
checkQResult :: forall a. QResult a -> TcM a
checkQResult QResult a
qr =
case QResult a
qr of
QDone a
a -> a -> TcM a
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
QException String
str -> IO a -> TcM a
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> TcM a) -> IO a -> TcM a
forall a b. (a -> b) -> a -> b
$ ErrorCall -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (String -> ErrorCall
ErrorCall String
str)
QFail String
str -> String -> TcM a
forall a. String -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
str
getTHState :: ExtInterpInstance d -> TcM (ForeignRef (IORef QState))
getTHState :: forall d. ExtInterpInstance d -> TcM (ForeignRef (IORef QState))
getTHState ExtInterpInstance d
inst = do
th_state_var <- TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState))))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv
(Env TcGblEnv TcLclEnv) (TcRef (Maybe (ForeignRef (IORef QState))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
liftIO $ do
th_state <- readIORef th_state_var
case th_state of
Just ForeignRef (IORef QState)
rhv -> ForeignRef (IORef QState) -> IO (ForeignRef (IORef QState))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
rhv
Maybe (ForeignRef (IORef QState))
Nothing -> do
rref <- ExtInterpInstance d
-> Message (RemoteRef (IORef QState))
-> IO (RemoteRef (IORef QState))
forall a d. Binary a => ExtInterpInstance d -> Message a -> IO a
sendMessage ExtInterpInstance d
inst Message (RemoteRef (IORef QState))
StartTH
fhv <- mkForeignRef rref (freeReallyRemoteRef inst rref)
writeIORef th_state_var (Just fhv)
return fhv
wrapTHResult :: TcM a -> TcM (THResult a)
wrapTHResult :: forall a. TcM a -> TcM (THResult a)
wrapTHResult TcM a
tcm = do
e <- TcM a -> IOEnv (Env TcGblEnv TcLclEnv) (Either IOEnvFailure a)
forall env r. IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM TcM a
tcm
case e of
Left IOEnvFailure
e -> THResult a -> IOEnv (Env TcGblEnv TcLclEnv) (THResult a)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> THResult a
forall a. String -> THResult a
THException (IOEnvFailure -> String
forall a. Show a => a -> String
show IOEnvFailure
e))
Right a
a -> THResult a -> IOEnv (Env TcGblEnv TcLclEnv) (THResult a)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> THResult a
forall a. a -> THResult a
THComplete a
a)
handleTHMessage :: THMessage a -> TcM a
handleTHMessage :: forall a. THMessage a -> TcM a
handleTHMessage THMessage a
msg = case THMessage a
msg of
NewName String
a -> TcM Name -> TcM (THResult Name)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Name -> TcM (THResult Name))
-> TcM Name -> TcM (THResult Name)
forall a b. (a -> b) -> a -> b
$ String -> TcM Name
forall (m :: * -> *). Quasi m => String -> m Name
TH.qNewName String
a
Report Bool
b String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcRn ()
forall (m :: * -> *). Quasi m => Bool -> String -> m ()
TH.qReport Bool
b String
str
LookupName Bool
b String
str -> TcM (Maybe Name) -> TcM (THResult (Maybe Name))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Name) -> TcM (THResult (Maybe Name)))
-> TcM (Maybe Name) -> TcM (THResult (Maybe Name))
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcM (Maybe Name)
forall (m :: * -> *). Quasi m => Bool -> String -> m (Maybe Name)
TH.qLookupName Bool
b String
str
Reify Name
n -> TcM Info -> TcM (THResult Info)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Info -> TcM (THResult Info))
-> TcM Info -> TcM (THResult Info)
forall a b. (a -> b) -> a -> b
$ Name -> TcM Info
forall (m :: * -> *). Quasi m => Name -> m Info
TH.qReify Name
n
ReifyFixity Name
n -> TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity)))
-> TcM (Maybe Fixity) -> TcM (THResult (Maybe Fixity))
forall a b. (a -> b) -> a -> b
$ Name -> TcM (Maybe Fixity)
forall (m :: * -> *). Quasi m => Name -> m (Maybe Fixity)
TH.qReifyFixity Name
n
ReifyType Name
n -> TcM Type -> TcM (THResult Type)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Type -> TcM (THResult Type))
-> TcM Type -> TcM (THResult Type)
forall a b. (a -> b) -> a -> b
$ Name -> TcM Type
forall (m :: * -> *). Quasi m => Name -> m Type
TH.qReifyType Name
n
ReifyInstances Name
n [Type]
ts -> TcM [Dec] -> TcM (THResult [Dec])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Dec] -> TcM (THResult [Dec]))
-> TcM [Dec] -> TcM (THResult [Dec])
forall a b. (a -> b) -> a -> b
$ Name -> [Type] -> TcM [Dec]
forall (m :: * -> *). Quasi m => Name -> [Type] -> m [Dec]
TH.qReifyInstances Name
n [Type]
ts
ReifyRoles Name
n -> TcM [Role] -> TcM (THResult [Role])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Role] -> TcM (THResult [Role]))
-> TcM [Role] -> TcM (THResult [Role])
forall a b. (a -> b) -> a -> b
$ Name -> TcM [Role]
forall (m :: * -> *). Quasi m => Name -> m [Role]
TH.qReifyRoles Name
n
ReifyAnnotations AnnLookup
lookup TypeRep
tyrep ->
TcM [ByteString] -> TcM (THResult [ByteString])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [ByteString] -> TcM (THResult [ByteString]))
-> TcM [ByteString] -> TcM (THResult [ByteString])
forall a b. (a -> b) -> a -> b
$ (([Word8] -> ByteString) -> [[Word8]] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map [Word8] -> ByteString
B.pack ([[Word8]] -> [ByteString])
-> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]] -> TcM [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnnLookup -> TypeRep -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
getAnnotationsByTypeRep AnnLookup
lookup TypeRep
tyrep)
ReifyModule Module
m -> TcM ModuleInfo -> TcM (THResult ModuleInfo)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM ModuleInfo -> TcM (THResult ModuleInfo))
-> TcM ModuleInfo -> TcM (THResult ModuleInfo)
forall a b. (a -> b) -> a -> b
$ Module -> TcM ModuleInfo
forall (m :: * -> *). Quasi m => Module -> m ModuleInfo
TH.qReifyModule Module
m
ReifyConStrictness Name
nm -> TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness]))
-> TcM [DecidedStrictness] -> TcM (THResult [DecidedStrictness])
forall a b. (a -> b) -> a -> b
$ Name -> TcM [DecidedStrictness]
forall (m :: * -> *). Quasi m => Name -> m [DecidedStrictness]
TH.qReifyConStrictness Name
nm
THMessage a
GetPackageRoot -> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String))
-> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a b. (a -> b) -> a -> b
$ IOEnv (Env TcGblEnv TcLclEnv) String
forall (m :: * -> *). Quasi m => m String
TH.qGetPackageRoot
AddDependentFile String
f -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddDependentFile String
f
AddTempFile String
s -> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String))
-> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM (THResult String)
forall a b. (a -> b) -> a -> b
$ String -> IOEnv (Env TcGblEnv TcLclEnv) String
forall (m :: * -> *). Quasi m => String -> m String
TH.qAddTempFile String
s
AddModFinalizer RemoteRef (Q ())
r -> do
interp <- HscEnv -> Interp
hscInterp (HscEnv -> Interp) -> TcRnIf TcGblEnv TcLclEnv HscEnv -> TcM Interp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
wrapTHResult $ liftIO (mkFinalizedHValue interp r) >>= addModFinalizerRef
AddCorePlugin String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddCorePlugin String
str
AddTopDecls [Dec]
decs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ [Dec] -> TcRn ()
forall (m :: * -> *). Quasi m => [Dec] -> m ()
TH.qAddTopDecls [Dec]
decs
AddForeignFilePath ForeignSrcLang
lang String
str -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ ForeignSrcLang -> String -> TcRn ()
forall (m :: * -> *). Quasi m => ForeignSrcLang -> String -> m ()
TH.qAddForeignFilePath ForeignSrcLang
lang String
str
IsExtEnabled Extension
ext -> TcM Bool -> TcM (THResult Bool)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Bool -> TcM (THResult Bool))
-> TcM Bool -> TcM (THResult Bool)
forall a b. (a -> b) -> a -> b
$ Extension -> TcM Bool
forall (m :: * -> *). Quasi m => Extension -> m Bool
TH.qIsExtEnabled Extension
ext
THMessage a
ExtsEnabled -> TcM [Extension] -> TcM (THResult [Extension])
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Extension] -> TcM (THResult [Extension]))
-> TcM [Extension] -> TcM (THResult [Extension])
forall a b. (a -> b) -> a -> b
$ TcM [Extension]
forall (m :: * -> *). Quasi m => m [Extension]
TH.qExtsEnabled
PutDoc DocLoc
l String
s -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ DocLoc -> String -> TcRn ()
forall (m :: * -> *). Quasi m => DocLoc -> String -> m ()
TH.qPutDoc DocLoc
l String
s
GetDoc DocLoc
l -> TcM (Maybe String) -> TcM (THResult (Maybe String))
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe String) -> TcM (THResult (Maybe String)))
-> TcM (Maybe String) -> TcM (THResult (Maybe String))
forall a b. (a -> b) -> a -> b
$ DocLoc -> TcM (Maybe String)
forall (m :: * -> *). Quasi m => DocLoc -> m (Maybe String)
TH.qGetDoc DocLoc
l
THMessage a
FailIfErrs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult TcRn ()
failIfErrsM
THMessage a
_ -> String -> TcM a
forall a. HasCallStack => String -> a
panic (String
"handleTHMessage: unexpected message " String -> String -> String
forall a. [a] -> [a] -> [a]
++ THMessage a -> String
forall a. Show a => a -> String
show THMessage a
msg)
getAnnotationsByTypeRep :: TH.AnnLookup -> TypeRep -> TcM [[Word8]]
getAnnotationsByTypeRep :: AnnLookup -> TypeRep -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
getAnnotationsByTypeRep AnnLookup
th_name TypeRep
tyrep
= do { name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; topEnv <- getTopEnv
; epsHptAnns <- liftIO $ prepareAnnotations topEnv Nothing
; tcg <- getGblEnv
; let selectedEpsHptAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep AnnEnv
epsHptAnns CoreAnnTarget
name TypeRep
tyrep
; let selectedTcgAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep (TcGblEnv -> AnnEnv
tcg_ann_env TcGblEnv
tcg) CoreAnnTarget
name TypeRep
tyrep
; return (selectedEpsHptAnns ++ selectedTcgAnns) }
reifyInstances :: TH.Name -> [TH.Type] -> TcM [TH.Dec]
reifyInstances :: Name -> [Type] -> TcM [Dec]
reifyInstances Name
th_nm [Type]
th_tys
= do { insts <- Name
-> [Type] -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' Name
th_nm [Type]
th_tys
; case insts of
Left (Class
cls, [ClsInst]
cls_insts) ->
Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances Class
cls [ClsInst]
cls_insts
Right (TyCon
tc, [FamInst]
fam_insts) ->
TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc [FamInst]
fam_insts }
reifyInstances' :: TH.Name
-> [TH.Type]
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' :: Name
-> [Type] -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
reifyInstances' Name
th_nm [Type]
th_tys
= SDoc
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the argument of reifyInstances:"
SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Name -> SDoc
forall a. Ppr a => a -> SDoc
ppr_th Name
th_nm SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep ((Type -> SDoc) -> [Type] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> SDoc
forall a. Ppr a => a -> SDoc
ppr_th [Type]
th_tys)) (TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$
do { loc <- TcRn SrcSpan
getSrcSpanM
; th_origin <- getThSpliceOrigin
; rdr_ty <- cvt th_origin loc (mkThAppTs (TH.ConT th_nm) th_tys)
; tv_rdrs <- filterInScopeM $ extractHsTyRdrTyVars rdr_ty
; ((tv_names, rn_ty), _fvs)
<- checkNoErrs $
rnImplicitTvOccs Nothing tv_rdrs $ \ [Name]
tv_names ->
do { (rn_ty, fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
doc LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
rdr_ty
; return ((tv_names, rn_ty), fvs) }
; skol_info <- mkSkolemInfo ReifySkol
; (tclvl, wanted, (tvs, ty))
<- pushLevelAndSolveEqualitiesX "reifyInstances" $
bindImplicitTKBndrs_Skol skol_info tv_names $
tcInferLHsType rn_ty
; tvs <- zonkAndScopedSort tvs
; reportUnsolvedEqualities skol_info tvs tclvl wanted
; ty <- zonkTcTypeToType ty
; traceTc "reifyInstances'" (ppr ty $$ ppr (typeKind ty))
; case splitTyConApp_maybe ty of
Just (TyCon
tc, [Type]
tys)
| Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
-> do { inst_envs <- TcM InstEnvs
tcGetInstEnvs
; let (matches, unifies, _) = lookupInstEnv False inst_envs cls tys
; traceTc "reifyInstances'1" (ppr matches)
; return $ Left (cls, map fst matches ++ getPotentialUnifiers unifies) }
| TyCon -> Bool
isOpenFamilyTyCon TyCon
tc
-> do { inst_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
; let matches = FamInstEnvs -> TyCon -> [Type] -> [FamInstMatch]
lookupFamInstEnv FamInstEnvs
inst_envs TyCon
tc [Type]
tys
; traceTc "reifyInstances'2" (ppr matches)
; return $ Right (tc, map fim_instance matches) }
Maybe (TyCon, [Type])
_ -> TcRnMessage -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a. TcRnMessage -> TcM a
bale_out (TcRnMessage -> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst])))
-> TcRnMessage
-> TcM (Either (Class, [ClsInst]) (TyCon, [FamInst]))
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THReifyError -> THError
THReifyError (THReifyError -> THError) -> THReifyError -> THError
forall a b. (a -> b) -> a -> b
$ Type -> THReifyError
CannotReifyInstance Type
ty }
where
doc :: HsDocContext
doc = HsDocContext
ClassInstanceCtx
bale_out :: TcRnMessage -> TcM a
bale_out TcRnMessage
msg = TcRnMessage -> TcM a
forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
msg
cvt :: Origin -> SrcSpan -> TH.Type -> TcM (LHsType GhcPs)
cvt :: Origin -> SrcSpan -> Type -> TcM (LHsType GhcPs)
cvt Origin
origin SrcSpan
loc Type
th_ty = case Origin
-> SrcSpan -> Type -> Either RunSpliceFailReason (LHsType GhcPs)
convertToHsType Origin
origin SrcSpan
loc Type
th_ty of
Left RunSpliceFailReason
msg -> TcRnMessage
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall a. TcRnMessage -> TcM a
failWithTc (THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ SpliceFailReason -> THError
THSpliceFailed (SpliceFailReason -> THError) -> SpliceFailReason -> THError
forall a b. (a -> b) -> a -> b
$ RunSpliceFailReason -> SpliceFailReason
RunSpliceFailure RunSpliceFailReason
msg)
Right LHsType GhcPs
ty -> GenLocated SrcSpanAnnA (HsType GhcPs)
-> IOEnv
(Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsType GhcPs))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
ty
lookupName :: Bool
-> String -> TcM (Maybe TH.Name)
lookupName :: Bool -> String -> TcM (Maybe Name)
lookupName Bool
is_type_name String
s
= do { mb_nm <- RdrName -> RnM (Maybe GlobalRdrElt)
lookupOccRn_maybe RdrName
rdr_name
; return (fmap (reifyName . greName) mb_nm) }
where
th_name :: Name
th_name = String -> Name
TH.mkName String
s
occ_fs :: FastString
occ_fs :: FastString
occ_fs = String -> FastString
mkFastString (Name -> String
TH.nameBase Name
th_name)
occ :: OccName
occ :: OccName
occ | Bool
is_type_name
= if FastString -> Bool
isLexVarSym FastString
occ_fs Bool -> Bool -> Bool
|| FastString -> Bool
isLexCon FastString
occ_fs
then FastString -> OccName
mkTcOccFS FastString
occ_fs
else FastString -> OccName
mkTyVarOccFS FastString
occ_fs
| Bool
otherwise
= if FastString -> Bool
isLexCon FastString
occ_fs then FastString -> OccName
mkDataOccFS FastString
occ_fs
else FastString -> OccName
mkVarOccFS FastString
occ_fs
rdr_name :: RdrName
rdr_name = case Name -> Maybe String
TH.nameModule Name
th_name of
Maybe String
Nothing -> OccName -> RdrName
mkRdrUnqual OccName
occ
Just String
mod -> ModuleName -> OccName -> RdrName
mkRdrQual (String -> ModuleName
mkModuleName String
mod) OccName
occ
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin = do
warn <- GeneralFlag -> TcM Bool
forall gbl lcl. GeneralFlag -> TcRnIf gbl lcl Bool
goptM GeneralFlag
Opt_EnableThSpliceWarnings
if warn then return FromSource else return (Generated OtherExpansion SkipPmc)
getThing :: TH.Name -> TcM TcTyThing
getThing :: Name -> TcM TcTyThing
getThing Name
th_name
= do { name <- Name -> TcM Name
lookupThName Name
th_name
; traceIf (text "reify" <+> text (show th_name) <+> brackets (ppr_ns th_name) <+> ppr name)
; tcLookupTh name }
where
ppr_ns :: Name -> doc
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.DataName PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"data"
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.TcClsName PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"tc"
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.VarName PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"var"
ppr_ns (TH.Name OccName
_ (TH.NameG (TH.FldName {}) PkgName
_pkg ModName
_mod)) = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fld"
ppr_ns Name
_ = String -> doc
forall a. HasCallStack => String -> a
panic String
"reify/ppr_ns"
reify :: TH.Name -> TcM TH.Info
reify :: Name -> TcM Info
reify Name
th_name
= do { String -> SDoc -> TcRn ()
traceTc String
"reify 1" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Name -> String
TH.showName Name
th_name))
; thing <- Name -> TcM TcTyThing
getThing Name
th_name
; traceTc "reify 2" (ppr thing)
; reifyThing thing }
lookupThName :: TH.Name -> TcM Name
lookupThName :: Name -> TcM Name
lookupThName Name
th_name = do
mb_name <- Name -> TcM (Maybe Name)
lookupThName_maybe Name
th_name
case mb_name of
Maybe Name
Nothing -> TcRnMessage -> TcM Name
forall a. TcRnMessage -> TcM a
failWithTc (Name -> TcRnMessage
notInScope Name
th_name)
Just Name
name -> Name -> TcM Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Name
name
lookupThName_maybe :: TH.Name -> TcM (Maybe Name)
lookupThName_maybe :: Name -> TcM (Maybe Name)
lookupThName_maybe Name
th_name
= do { let guesses :: [RdrName]
guesses = Name -> [RdrName]
thRdrNameGuesses Name
th_name
; case [RdrName]
guesses of
{ [RdrName
for_sure] -> RdrName -> TcM (Maybe Name)
lookupSameOccRn_maybe RdrName
for_sure
; [RdrName]
_ ->
do { gres <- (RdrName -> RnM (Maybe GlobalRdrElt))
-> [RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) [GlobalRdrElt]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM RdrName -> RnM (Maybe GlobalRdrElt)
lookupOccRn_maybe [RdrName]
guesses
; return (fmap greName $ listToMaybe gres) } } }
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh Name
name
= do { (gbl_env, lcl_env) <- TcRnIf TcGblEnv TcLclEnv (TcGblEnv, TcLclEnv)
forall gbl lcl. TcRnIf gbl lcl (gbl, lcl)
getEnvs
; case lookupNameEnv (getLclEnvTypeEnv lcl_env) name of {
Just TcTyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return TcTyThing
thing;
Maybe TcTyThing
Nothing ->
case NameEnv TyThing -> Name -> Maybe TyThing
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv (TcGblEnv -> NameEnv TyThing
tcg_type_env TcGblEnv
gbl_env) Name
name of {
Just TyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing);
Maybe TyThing
Nothing ->
if GenModule Unit -> Name -> Bool
nameIsLocalOrFrom (TcGblEnv -> GenModule Unit
tcg_semantic_mod TcGblEnv
gbl_env) Name
name
then
TcRnMessage -> TcM TcTyThing
forall a. TcRnMessage -> TcM a
failWithTc (Name -> TcRnMessage
notInEnv Name
name)
else
do { mb_thing <- Name -> TcM (MaybeErr IfaceMessage TyThing)
tcLookupImported_maybe Name
name
; case mb_thing of
Succeeded TyThing
thing -> TcTyThing -> TcM TcTyThing
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing)
Failed IfaceMessage
msg -> TcRnMessage -> TcM TcTyThing
forall a. TcRnMessage -> TcM a
failWithTc (IfaceMessage -> TcRnMessage
TcRnInterfaceError IfaceMessage
msg)
}}}}
notInScope :: TH.Name -> TcRnMessage
notInScope :: Name -> TcRnMessage
notInScope Name
th_name =
THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THReifyError -> THError
THReifyError (THReifyError -> THError) -> THReifyError -> THError
forall a b. (a -> b) -> a -> b
$ Name -> THReifyError
CannotReifyOutOfScopeThing Name
th_name
notInEnv :: Name -> TcRnMessage
notInEnv :: Name -> TcRnMessage
notInEnv Name
name = THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THReifyError -> THError
THReifyError (THReifyError -> THError) -> THReifyError -> THError
forall a b. (a -> b) -> a -> b
$ Name -> THReifyError
CannotReifyThingNotInTypeEnv Name
name
reifyRoles :: TH.Name -> TcM [TH.Role]
reifyRoles :: Name -> TcM [Role]
reifyRoles Name
th_name
= do { thing <- Name -> TcM TcTyThing
getThing Name
th_name
; case thing of
AGlobal (ATyCon TyCon
tc) -> [Role] -> TcM [Role]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Role -> Role) -> [Role] -> [Role]
forall a b. (a -> b) -> [a] -> [b]
map Role -> Role
reify_role (TyCon -> [Role]
tyConRoles TyCon
tc))
TcTyThing
_ -> TcRnMessage -> TcM [Role]
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM [Role]) -> TcRnMessage -> TcM [Role]
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THReifyError -> THError
THReifyError (THReifyError -> THError) -> THReifyError -> THError
forall a b. (a -> b) -> a -> b
$
TcTyThing -> THReifyError
NoRolesAssociatedWithThing TcTyThing
thing
}
where
reify_role :: Role -> Role
reify_role Role
Nominal = Role
TH.NominalR
reify_role Role
Representational = Role
TH.RepresentationalR
reify_role Role
Phantom = Role
TH.PhantomR
reifyThing :: TcTyThing -> TcM TH.Info
reifyThing :: TcTyThing -> TcM Info
reifyThing (AGlobal (AnId TyVar
id))
= do { ty <- Type -> TcM Type
reifyType (TyVar -> Type
idType TyVar
id)
; let v = TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName TyVar
id
; case idDetails id of
ClassOpId Class
cls Bool
_ -> Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Name -> Info
TH.ClassOpI Name
v Type
ty (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls))
IdDetails
_ -> Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI Name
v Type
ty Maybe Dec
forall a. Maybe a
Nothing)
}
reifyThing (AGlobal (ATyCon TyCon
tc)) = TyCon -> TcM Info
reifyTyCon TyCon
tc
reifyThing (AGlobal (AConLike (RealDataCon DataCon
dc)))
= DataCon -> TcM Info
mkDataConI DataCon
dc
reifyThing (AGlobal (AConLike (PatSynCon PatSyn
ps)))
= do { let name :: Name
name = PatSyn -> Name
forall n. NamedThing n => n -> Name
reifyName PatSyn
ps
; ty <- ([InvisTVBinder], [Type], [InvisTVBinder], [Type], [Scaled Type],
Type)
-> TcM Type
reifyPatSynType (PatSyn
-> ([InvisTVBinder], [Type], [InvisTVBinder], [Type],
[Scaled Type], Type)
patSynSigBndr PatSyn
ps)
; return (TH.PatSynI name ty) }
reifyThing (ATcId {tct_id :: TcTyThing -> TyVar
tct_id = TyVar
id})
= do { ty1 <- ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. ZonkM a -> TcM a
liftZonkM (ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type)
-> ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a b. (a -> b) -> a -> b
$ Type -> ZonkM Type
zonkTcType (TyVar -> Type
idType TyVar
id)
; ty2 <- reifyType ty1
; return (TH.VarI (reifyName id) ty2 Nothing) }
reifyThing (ATyVar Name
tv TyVar
tv1)
= do { ty1 <- ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. ZonkM a -> TcM a
liftZonkM (ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type)
-> ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a b. (a -> b) -> a -> b
$ TyVar -> ZonkM Type
zonkTcTyVar TyVar
tv1
; ty2 <- reifyType ty1
; return (TH.TyVarI (reifyName tv) ty2) }
reifyThing TcTyThing
thing = String -> SDoc -> TcM Info
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"reifyThing" (TcTyThing -> SDoc
pprTcTyThingCategory TcTyThing
thing)
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TH.TySynEqn
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch TyCon
fam_tc (CoAxBranch { cab_tvs :: CoAxBranch -> [TyVar]
cab_tvs = [TyVar]
tvs
, cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs
, cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs })
= do { tvs' <- [TyVar] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [TyVar]
tvs
; let lhs_types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
lhs
; lhs' <- reifyTypes lhs_types_only
; annot_th_lhs <- zipWith3M annotThType (tyConArgsPolyKinded fam_tc)
lhs_types_only lhs'
; let lhs_type = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT (Name -> Type) -> Name -> Type
forall a b. (a -> b) -> a -> b
$ TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
fam_tc) [Type]
annot_th_lhs
; rhs' <- reifyType rhs
; return (TH.TySynEqn tvs' lhs_type rhs') }
reifyTyCon :: TyCon -> TcM TH.Info
reifyTyCon :: TyCon -> TcM Info
reifyTyCon TyCon
tc
| Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
= Class -> TcM Info
reifyClass Class
cls
| TyCon -> Bool
isPrimTyCon TyCon
tc
= Info -> TcM Info
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> SumArity -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) ([TyVar] -> SumArity
forall a. [a] -> SumArity
forall (t :: * -> *) a. Foldable t => t a -> SumArity
length (TyCon -> [TyVar]
tyConVisibleTyVars TyCon
tc))
(Type -> Bool
isUnliftedTypeKind (TyCon -> Type
tyConResKind TyCon
tc)))
| TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
= do { let tvs :: [TyVar]
tvs = TyCon -> [TyVar]
tyConTyVars TyCon
tc
res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc
resVar :: Maybe Name
resVar = TyCon -> Maybe Name
tyConFamilyResVar_maybe TyCon
tc
; kind' <- Type -> TcM Type
reifyKind Type
res_kind
; let (resultSig, injectivity) =
case resVar of
Maybe Name
Nothing -> (Type -> FamilyResultSig
TH.KindSig Type
kind', Maybe InjectivityAnn
forall a. Maybe a
Nothing)
Just Name
name ->
let thName :: Name
thName = Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
name
injAnnot :: Injectivity
injAnnot = TyCon -> Injectivity
tyConInjectivityInfo TyCon
tc
sig :: FamilyResultSig
sig = TyVarBndr () -> FamilyResultSig
TH.TyVarSig (Name -> () -> Type -> TyVarBndr ()
forall flag. Name -> flag -> Type -> TyVarBndr flag
TH.KindedTV Name
thName () Type
kind')
inj :: Maybe InjectivityAnn
inj = case Injectivity
injAnnot of
Injectivity
NotInjective -> Maybe InjectivityAnn
forall a. Maybe a
Nothing
Injective [Bool]
ms ->
InjectivityAnn -> Maybe InjectivityAnn
forall a. a -> Maybe a
Just (Name -> [Name] -> InjectivityAnn
TH.InjectivityAnn Name
thName [Name]
injRHS)
where
injRHS :: [Name]
injRHS = (TyVar -> Name) -> [TyVar] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> (TyVar -> Name) -> TyVar -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> Name
tyVarName)
([Bool] -> [TyVar] -> [TyVar]
forall a. [Bool] -> [a] -> [a]
filterByList [Bool]
ms [TyVar]
tvs)
in (FamilyResultSig
sig, Maybe InjectivityAnn
inj)
; tvs' <- reifyTyConBinders tc
; let tfHead =
Name
-> [TyVarBndr BndrVis]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> TypeFamilyHead
TH.TypeFamilyHead (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr BndrVis]
tvs' FamilyResultSig
resultSig Maybe InjectivityAnn
injectivity
; if isOpenTypeFamilyTyCon tc
then do { fam_envs <- tcGetFamInstEnvs
; instances <- reifyFamilyInstances tc
(familyInstances fam_envs tc)
; return (TH.FamilyI (TH.OpenTypeFamilyD tfHead) instances) }
else do { eqns <-
case isClosedSynFamilyTyConWithAxiom_maybe tc of
Just CoAxiom Branched
ax -> (CoAxBranch -> TcM TySynEqn)
-> [CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch TyCon
tc) ([CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn])
-> [CoAxBranch] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall a b. (a -> b) -> a -> b
$
Branches Branched -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches (Branches Branched -> [CoAxBranch])
-> Branches Branched -> [CoAxBranch]
forall a b. (a -> b) -> a -> b
$ CoAxiom Branched -> Branches Branched
forall (br :: BranchFlag). CoAxiom br -> Branches br
coAxiomBranches CoAxiom Branched
ax
Maybe (CoAxiom Branched)
Nothing -> [TySynEqn] -> IOEnv (Env TcGblEnv TcLclEnv) [TySynEqn]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
; return (TH.FamilyI (TH.ClosedTypeFamilyD tfHead eqns)
[]) } }
| TyCon -> Bool
isDataFamilyTyCon TyCon
tc
= do { let res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc
; kind' <- (Type -> Maybe Type)
-> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type -> Maybe Type
forall a. a -> Maybe a
Just (Type -> TcM Type
reifyKind Type
res_kind)
; tvs' <- reifyTyConBinders tc
; fam_envs <- tcGetFamInstEnvs
; instances <- reifyFamilyInstances tc (familyInstances fam_envs tc)
; return (TH.FamilyI
(TH.DataFamilyD (reifyName tc) tvs' kind') instances) }
| Just ([TyVar]
_, Type
rhs) <- TyCon -> Maybe ([TyVar], Type)
synTyConDefn_maybe TyCon
tc
= do { rhs' <- Type -> TcM Type
reifyType Type
rhs
; tvs' <- reifyTyConBinders tc
; return (TH.TyConI
(TH.TySynD (reifyName tc) tvs' rhs'))
}
| Just DataCon
dc <- TyCon -> Maybe DataCon
isPromotedDataCon_maybe TyCon
tc
, DataCon -> Bool
isTypeDataCon DataCon
dc
= DataCon -> TcM Info
mkDataConI DataCon
dc
| Bool
otherwise
= do { cxt <- [Type] -> TcM [Type]
reifyCxt (TyCon -> [Type]
tyConStupidTheta TyCon
tc)
; let tvs = TyCon -> [TyVar]
tyConTyVars TyCon
tc
dataCons = TyCon -> [DataCon]
tyConDataCons TyCon
tc
isGadt = TyCon -> Bool
isGadtSyntaxTyCon TyCon
tc
; cons <- mapM (reifyDataCon isGadt (mkTyVarTys tvs)) dataCons
; r_tvs <- reifyTyConBinders tc
; let name = TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc
deriv = []
decl | TyCon -> Bool
isTypeDataTyCon TyCon
tc =
Name -> [TyVarBndr BndrVis] -> Maybe Type -> [Con] -> Dec
TH.TypeDataD Name
name [TyVarBndr BndrVis]
r_tvs Maybe Type
forall a. Maybe a
Nothing [Con]
cons
| TyCon -> Bool
isNewTyCon TyCon
tc =
[Type]
-> Name
-> [TyVarBndr BndrVis]
-> Maybe Type
-> Con
-> [DerivClause]
-> Dec
TH.NewtypeD [Type]
cxt Name
name [TyVarBndr BndrVis]
r_tvs Maybe Type
forall a. Maybe a
Nothing ([Con] -> Con
forall a. HasCallStack => [a] -> a
head [Con]
cons) [DerivClause]
forall a. [a]
deriv
| Bool
otherwise =
[Type]
-> Name
-> [TyVarBndr BndrVis]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
TH.DataD [Type]
cxt Name
name [TyVarBndr BndrVis]
r_tvs Maybe Type
forall a. Maybe a
Nothing [Con]
cons [DerivClause]
forall a. [a]
deriv
; return (TH.TyConI decl) }
reifyDataCon :: Bool -> [Type] -> DataCon -> TcM TH.Con
reifyDataCon :: Bool -> [Type] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadtDataCon [Type]
tys DataCon
dc
= do { let
([TyVar]
ex_tvs, [Type]
theta, [Type]
arg_tys)
= DataCon -> [Type] -> ([TyVar], [Type], [Type])
dataConInstSig DataCon
dc [Type]
tys
g_user_tvs' :: [InvisTVBinder]
g_user_tvs' = DataCon -> [InvisTVBinder]
dataConUserTyVarBinders DataCon
dc
([TyVar]
g_univ_tvs, [TyVar]
_, [EqSpec]
g_eq_spec, [Type]
g_theta', [Scaled Type]
g_arg_tys', Type
g_res_ty')
= DataCon
-> ([TyVar], [TyVar], [EqSpec], [Type], [Scaled Type], Type)
dataConFullSig DataCon
dc
([SourceUnpackedness]
srcUnpks, [SourceStrictness]
srcStricts)
= (HsSrcBang -> (SourceUnpackedness, SourceStrictness))
-> [HsSrcBang] -> ([SourceUnpackedness], [SourceStrictness])
forall a b c. (a -> (b, c)) -> [a] -> ([b], [c])
mapAndUnzip HsSrcBang -> (SourceUnpackedness, SourceStrictness)
reifySourceBang (DataCon -> [HsSrcBang]
dataConSrcBangs DataCon
dc)
dcdBangs :: [Bang]
dcdBangs = (SourceUnpackedness -> SourceStrictness -> Bang)
-> [SourceUnpackedness] -> [SourceStrictness] -> [Bang]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith SourceUnpackedness -> SourceStrictness -> Bang
TH.Bang [SourceUnpackedness]
srcUnpks [SourceStrictness]
srcStricts
fields :: [FieldLabel]
fields = DataCon -> [FieldLabel]
dataConFieldLabels DataCon
dc
name :: Name
name = DataCon -> Name
forall n. NamedThing n => n -> Name
reifyName DataCon
dc
eq_spec_tvs :: VarSet
eq_spec_tvs = [TyVar] -> VarSet
mkVarSet ((EqSpec -> TyVar) -> [EqSpec] -> [TyVar]
forall a b. (a -> b) -> [a] -> [b]
map EqSpec -> TyVar
eqSpecTyVar [EqSpec]
g_eq_spec)
; (univ_subst, _)
<- [TyVar] -> TcM (Subst, [TyVar])
freshenTyVarBndrs ([TyVar] -> TcM (Subst, [TyVar]))
-> [TyVar] -> TcM (Subst, [TyVar])
forall a b. (a -> b) -> a -> b
$
(TyVar -> Bool) -> [TyVar] -> [TyVar]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (TyVar -> VarSet -> Bool
`elemVarSet` VarSet
eq_spec_tvs) [TyVar]
g_univ_tvs
; let (tvb_subst, g_user_tvs) = subst_tv_binders univ_subst g_user_tvs'
g_theta = HasDebugCallStack => Subst -> [Type] -> [Type]
Subst -> [Type] -> [Type]
substTys Subst
tvb_subst [Type]
g_theta'
g_arg_tys = HasDebugCallStack => Subst -> [Type] -> [Type]
Subst -> [Type] -> [Type]
substTys Subst
tvb_subst ((Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> a
scaledThing [Scaled Type]
g_arg_tys')
g_res_ty = HasDebugCallStack => Subst -> Type -> Type
Subst -> Type -> Type
substTy Subst
tvb_subst Type
g_res_ty'
; r_arg_tys <- reifyTypes (if isGadtDataCon then g_arg_tys else arg_tys)
; main_con <-
if | not (null fields) && not isGadtDataCon ->
return $ TH.RecC name (zip3 (map reifyFieldLabel fields)
dcdBangs r_arg_tys)
| not (null fields) -> do
{ res_ty <- reifyType g_res_ty
; return $ TH.RecGadtC [name]
(zip3 (map reifyFieldLabel fields)
dcdBangs r_arg_tys) res_ty }
| dataConIsInfix dc && not isGadtDataCon ->
assert (r_arg_tys `lengthIs` 2) $ do
{ let [r_a1, r_a2] = r_arg_tys
[s1, s2] = dcdBangs
; return $ TH.InfixC (s1,r_a1) name (s2,r_a2) }
| isGadtDataCon -> do
{ res_ty <- reifyType g_res_ty
; return $ TH.GadtC [name]
(dcdBangs `zip` r_arg_tys) res_ty }
| otherwise ->
return $ TH.NormalC name (dcdBangs `zip` r_arg_tys)
; let (ex_tvs', theta') | isGadtDataCon = (g_user_tvs, g_theta)
| otherwise = assert (all isTyVar ex_tvs)
(map mk_specified ex_tvs, theta)
ret_con | [InvisTVBinder] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [InvisTVBinder]
ex_tvs' Bool -> Bool -> Bool
&& [Type] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
theta' = Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Con
main_con
| Bool
otherwise = do
{ cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta'
; ex_tvs'' <- reifyTyVarBndrs ex_tvs'
; return (TH.ForallC ex_tvs'' cxt main_con) }
; assert (r_arg_tys `equalLength` dcdBangs)
ret_con }
where
mk_specified :: var -> VarBndr var Specificity
mk_specified var
tv = var -> Specificity -> VarBndr var Specificity
forall var argf. var -> argf -> VarBndr var argf
Bndr var
tv Specificity
SpecifiedSpec
subst_tv_binders :: Subst -> [VarBndr TyVar argf] -> (Subst, [VarBndr TyVar argf])
subst_tv_binders Subst
subst [VarBndr TyVar argf]
tv_bndrs =
let tvs :: [TyVar]
tvs = [VarBndr TyVar argf] -> [TyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr TyVar argf]
tv_bndrs
flags :: [argf]
flags = [VarBndr TyVar argf] -> [argf]
forall tv argf. [VarBndr tv argf] -> [argf]
binderFlags [VarBndr TyVar argf]
tv_bndrs
(Subst
subst', [TyVar]
tvs') = HasDebugCallStack => Subst -> [TyVar] -> (Subst, [TyVar])
Subst -> [TyVar] -> (Subst, [TyVar])
substTyVarBndrs Subst
subst [TyVar]
tvs
tv_bndrs' :: [VarBndr TyVar argf]
tv_bndrs' = ((TyVar, argf) -> VarBndr TyVar argf)
-> [(TyVar, argf)] -> [VarBndr TyVar argf]
forall a b. (a -> b) -> [a] -> [b]
map (\(TyVar
tv,argf
fl) -> TyVar -> argf -> VarBndr TyVar argf
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv argf
fl) ([TyVar] -> [argf] -> [(TyVar, argf)]
forall a b. [a] -> [b] -> [(a, b)]
zip [TyVar]
tvs' [argf]
flags)
in (Subst
subst', [VarBndr TyVar argf]
tv_bndrs')
mkDataConI :: DataCon -> TcM TH.Info
mkDataConI :: DataCon -> TcM Info
mkDataConI DataCon
dc
= do { let name :: Name
name = DataCon -> Name
dataConName DataCon
dc
; ty <- Type -> TcM Type
reifyType (TyVar -> Type
idType (DataCon -> TyVar
dataConWrapId DataCon
dc))
; return (TH.DataConI (reifyName name) ty
(reifyName (dataConOrigTyCon dc)))
}
reifyClass :: Class -> TcM TH.Info
reifyClass :: Class -> TcM Info
reifyClass Class
cls
= do { cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta
; inst_envs <- tcGetInstEnvs
; insts <- reifyClassInstances cls (InstEnv.classInstances inst_envs cls)
; assocTys <- concatMapM reifyAT ats
; ops <- concatMapM reify_op op_stuff
; tvs' <- reifyTyConBinders (classTyCon cls)
; let dec = [Type] -> Name -> [TyVarBndr BndrVis] -> [FunDep] -> [Dec] -> Dec
TH.ClassD [Type]
cxt (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls) [TyVarBndr BndrVis]
tvs' [FunDep]
fds' ([Dec]
assocTys [Dec] -> [Dec] -> [Dec]
forall a. [a] -> [a] -> [a]
++ [Dec]
ops)
; return (TH.ClassI dec insts) }
where
([TyVar]
_, [FunDep TyVar]
fds, [Type]
theta, [TyVar]
_, [ClassATItem]
ats, [(TyVar, Maybe (Name, DefMethSpec Type))]
op_stuff) = Class
-> ([TyVar], [FunDep TyVar], [Type], [TyVar], [ClassATItem],
[(TyVar, Maybe (Name, DefMethSpec Type))])
classExtraBigSig Class
cls
fds' :: [FunDep]
fds' = (FunDep TyVar -> FunDep) -> [FunDep TyVar] -> [FunDep]
forall a b. (a -> b) -> [a] -> [b]
map FunDep TyVar -> FunDep
reifyFunDep [FunDep TyVar]
fds
reify_op :: (TyVar, Maybe (a, DefMethSpec Type)) -> TcM [Dec]
reify_op (TyVar
op, Maybe (a, DefMethSpec Type)
def_meth)
= do { let ([TyVar]
_, Type
_, Type
ty) = Type -> ([TyVar], Type, Type)
tcSplitMethodTy (TyVar -> Type
idType TyVar
op)
; ty' <- Type -> TcM Type
reifyType Type
ty
; let nm' = TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName TyVar
op
; case def_meth of
Just (a
_, GenericDM Type
gdm_ty) ->
do { gdm_ty' <- Type -> TcM Type
reifyType Type
gdm_ty
; return [TH.SigD nm' ty', TH.DefaultSigD nm' gdm_ty'] }
Maybe (a, DefMethSpec Type)
_ -> [Dec] -> TcM [Dec]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Name -> Type -> Dec
TH.SigD Name
nm' Type
ty'] }
reifyAT :: ClassATItem -> TcM [TH.Dec]
reifyAT :: ClassATItem -> TcM [Dec]
reifyAT (ATI TyCon
tycon Maybe (Type, TyFamEqnValidityInfo)
def) = do
tycon' <- TyCon -> TcM Info
reifyTyCon TyCon
tycon
case tycon' of
TH.FamilyI Dec
dec [Dec]
_ -> do
let (Name
tyName, [Name]
tyArgs) = Dec -> (Name, [Name])
tfNames Dec
dec
(Dec
dec Dec -> [Dec] -> [Dec]
forall a. a -> [a] -> [a]
:) ([Dec] -> [Dec]) -> TcM [Dec] -> TcM [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcM [Dec]
-> ((Type, TyFamEqnValidityInfo) -> TcM [Dec])
-> Maybe (Type, TyFamEqnValidityInfo)
-> TcM [Dec]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Dec] -> TcM [Dec]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
((Dec -> [Dec]) -> IOEnv (Env TcGblEnv TcLclEnv) Dec -> TcM [Dec]
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Dec -> [Dec] -> [Dec]
forall a. a -> [a] -> [a]
:[]) (IOEnv (Env TcGblEnv TcLclEnv) Dec -> TcM [Dec])
-> ((Type, TyFamEqnValidityInfo)
-> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> (Type, TyFamEqnValidityInfo)
-> TcM [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> [Name] -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyDefImpl Name
tyName [Name]
tyArgs (Type -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> ((Type, TyFamEqnValidityInfo) -> Type)
-> (Type, TyFamEqnValidityInfo)
-> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Type, TyFamEqnValidityInfo) -> Type
forall a b. (a, b) -> a
fst)
Maybe (Type, TyFamEqnValidityInfo)
def
Info
_ -> String -> SDoc -> TcM [Dec]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"reifyAT" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Info -> String
forall a. Show a => a -> String
show Info
tycon'))
reifyDefImpl :: TH.Name -> [TH.Name] -> Type -> TcM TH.Dec
reifyDefImpl :: Name -> [Name] -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyDefImpl Name
n [Name]
args Type
ty =
TySynEqn -> Dec
TH.TySynInstD (TySynEqn -> Dec) -> (Type -> TySynEqn) -> Type -> Dec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TH.TySynEqn Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing (Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
n) ((Name -> Type) -> [Name] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Type
TH.VarT [Name]
args))
(Type -> Dec) -> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> TcM Type
reifyType Type
ty
tfNames :: TH.Dec -> (TH.Name, [TH.Name])
tfNames :: Dec -> (Name, [Name])
tfNames (TH.OpenTypeFamilyD (TH.TypeFamilyHead Name
n [TyVarBndr BndrVis]
args FamilyResultSig
_ Maybe InjectivityAnn
_))
= (Name
n, (TyVarBndr BndrVis -> Name) -> [TyVarBndr BndrVis] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVarBndr BndrVis -> Name
forall flag. TyVarBndr flag -> Name
bndrName [TyVarBndr BndrVis]
args)
tfNames Dec
d = String -> SDoc -> (Name, [Name])
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tfNames" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Dec -> String
forall a. Show a => a -> String
show Dec
d))
bndrName :: TH.TyVarBndr flag -> TH.Name
bndrName :: forall flag. TyVarBndr flag -> Name
bndrName (TH.PlainTV Name
n flag
_) = Name
n
bndrName (TH.KindedTV Name
n flag
_ Type
_) = Name
n
annotThType :: Bool
-> TyCoRep.Type -> TH.Type -> TcM TH.Type
annotThType :: Bool -> Type -> Type -> TcM Type
annotThType Bool
_ Type
_ th_ty :: Type
th_ty@(TH.SigT {}) = Type -> TcM Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_ty
annotThType Bool
True Type
ty Type
th_ty
| Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ VarSet -> Bool
isEmptyVarSet (VarSet -> Bool) -> VarSet -> Bool
forall a b. (a -> b) -> a -> b
$ (TyVar -> Bool) -> VarSet -> VarSet
filterVarSet TyVar -> Bool
isTyVar (VarSet -> VarSet) -> VarSet -> VarSet
forall a b. (a -> b) -> a -> b
$ Type -> VarSet
tyCoVarsOfType Type
ty
= do { let ki :: Type
ki = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty
; th_ki <- Type -> TcM Type
reifyKind Type
ki
; return (TH.SigT th_ty th_ki) }
annotThType Bool
_ Type
_ Type
th_ty = Type -> TcM Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_ty
tyConArgsPolyKinded :: TyCon -> [Bool]
tyConArgsPolyKinded :: TyCon -> [Bool]
tyConArgsPolyKinded TyCon
tc =
(TyVar -> Bool) -> [TyVar] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Bool
is_poly_ty (Type -> Bool) -> (TyVar -> Type) -> TyVar -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> Type
tyVarKind) [TyVar]
tc_vis_tvs
[Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ (PiTyBinder -> Bool) -> [PiTyBinder] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Bool
is_poly_ty (Type -> Bool) -> (PiTyBinder -> Type) -> PiTyBinder -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PiTyBinder -> Type
piTyBinderType) [PiTyBinder]
tc_res_kind_vis_bndrs
[Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ Bool -> [Bool]
forall a. a -> [a]
repeat Bool
True
where
is_poly_ty :: Type -> Bool
is_poly_ty :: Type -> Bool
is_poly_ty Type
ty = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$
VarSet -> Bool
isEmptyVarSet (VarSet -> Bool) -> VarSet -> Bool
forall a b. (a -> b) -> a -> b
$
(TyVar -> Bool) -> VarSet -> VarSet
filterVarSet TyVar -> Bool
isTyVar (VarSet -> VarSet) -> VarSet -> VarSet
forall a b. (a -> b) -> a -> b
$
Type -> VarSet
tyCoVarsOfType Type
ty
tc_vis_tvs :: [TyVar]
tc_vis_tvs :: [TyVar]
tc_vis_tvs = TyCon -> [TyVar]
tyConVisibleTyVars TyCon
tc
tc_res_kind_vis_bndrs :: [PiTyBinder]
tc_res_kind_vis_bndrs :: [PiTyBinder]
tc_res_kind_vis_bndrs = (PiTyBinder -> Bool) -> [PiTyBinder] -> [PiTyBinder]
forall a. (a -> Bool) -> [a] -> [a]
filter PiTyBinder -> Bool
isVisiblePiTyBinder ([PiTyBinder] -> [PiTyBinder]) -> [PiTyBinder] -> [PiTyBinder]
forall a b. (a -> b) -> a -> b
$ ([PiTyBinder], Type) -> [PiTyBinder]
forall a b. (a, b) -> a
fst (([PiTyBinder], Type) -> [PiTyBinder])
-> ([PiTyBinder], Type) -> [PiTyBinder]
forall a b. (a -> b) -> a -> b
$ Type -> ([PiTyBinder], Type)
splitPiTys (Type -> ([PiTyBinder], Type)) -> Type -> ([PiTyBinder], Type)
forall a b. (a -> b) -> a -> b
$ TyCon -> Type
tyConResKind TyCon
tc
reifyClassInstances :: Class -> [ClsInst] -> TcM [TH.Dec]
reifyClassInstances :: Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances Class
cls [ClsInst]
insts
= (ClsInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> [ClsInst] -> TcM [Dec]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Bool] -> ClsInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyClassInstance (TyCon -> [Bool]
tyConArgsPolyKinded (Class -> TyCon
classTyCon Class
cls))) [ClsInst]
insts
reifyClassInstance :: [Bool]
-> ClsInst -> TcM TH.Dec
reifyClassInstance :: [Bool] -> ClsInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyClassInstance [Bool]
is_poly_tvs ClsInst
i
= do { cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta
; let vis_types = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
cls_tc [Type]
types
; thtypes <- reifyTypes vis_types
; annot_thtypes <- zipWith3M annotThType is_poly_tvs vis_types thtypes
; let head_ty = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls)) [Type]
annot_thtypes
; return $ (TH.InstanceD over cxt head_ty []) }
where
([TyVar]
_tvs, [Type]
theta, Class
cls, [Type]
types) = Type -> ([TyVar], [Type], Class, [Type])
tcSplitDFunTy (TyVar -> Type
idType TyVar
dfun)
cls_tc :: TyCon
cls_tc = Class -> TyCon
classTyCon Class
cls
dfun :: TyVar
dfun = ClsInst -> TyVar
instanceDFunId ClsInst
i
over :: Maybe Overlap
over = case OverlapFlag -> OverlapMode
overlapMode (ClsInst -> OverlapFlag
is_flag ClsInst
i) of
NoOverlap SourceText
_ -> Maybe Overlap
forall a. Maybe a
Nothing
Overlappable SourceText
_ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlappable
Overlapping SourceText
_ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlapping
Overlaps SourceText
_ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlaps
Incoherent SourceText
_ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Incoherent
NonCanonical SourceText
_ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Incoherent
reifyFamilyInstances :: TyCon -> [FamInst] -> TcM [TH.Dec]
reifyFamilyInstances :: TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
fam_tc [FamInst]
fam_insts
= (FamInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> [FamInst] -> TcM [Dec]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Bool] -> FamInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyFamilyInstance (TyCon -> [Bool]
tyConArgsPolyKinded TyCon
fam_tc)) [FamInst]
fam_insts
reifyFamilyInstance :: [Bool]
-> FamInst -> TcM TH.Dec
reifyFamilyInstance :: [Bool] -> FamInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyFamilyInstance [Bool]
is_poly_tvs (FamInst { fi_flavor :: FamInst -> FamFlavor
fi_flavor = FamFlavor
flavor
, fi_axiom :: FamInst -> CoAxiom Unbranched
fi_axiom = CoAxiom Unbranched
ax
, fi_fam :: FamInst -> Name
fi_fam = Name
fam })
| let fam_tc :: TyCon
fam_tc = CoAxiom Unbranched -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom Unbranched
ax
branch :: CoAxBranch
branch = CoAxiom Unbranched -> CoAxBranch
coAxiomSingleBranch CoAxiom Unbranched
ax
, CoAxBranch { cab_tvs :: CoAxBranch -> [TyVar]
cab_tvs = [TyVar]
tvs, cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs, cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs } <- CoAxBranch
branch
= case FamFlavor
flavor of
FamFlavor
SynFamilyInst ->
do { th_tvs <- [TyVar] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [TyVar]
tvs
; let lhs_types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
lhs
; th_lhs <- reifyTypes lhs_types_only
; annot_th_lhs <- zipWith3M annotThType is_poly_tvs lhs_types_only
th_lhs
; let lhs_type = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT (Name -> Type) -> Name -> Type
forall a b. (a -> b) -> a -> b
$ Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
fam) [Type]
annot_th_lhs
; th_rhs <- reifyType rhs
; return (TH.TySynInstD (TH.TySynEqn th_tvs lhs_type th_rhs)) }
DataFamilyInst TyCon
rep_tc ->
do { let
([TyVar]
ee_tvs, [Type]
ee_lhs, Type
_) = CoAxBranch -> ([TyVar], [Type], Type)
etaExpandCoAxBranch CoAxBranch
branch
fam' :: Name
fam' = Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
fam
dataCons :: [DataCon]
dataCons = TyCon -> [DataCon]
tyConDataCons TyCon
rep_tc
isGadt :: Bool
isGadt = TyCon -> Bool
isGadtSyntaxTyCon TyCon
rep_tc
; th_tvs <- [TyVar] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [TyVar]
ee_tvs
; cons <- mapM (reifyDataCon isGadt (mkTyVarTys ee_tvs)) dataCons
; let types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
ee_lhs
; th_tys <- reifyTypes types_only
; annot_th_tys <- zipWith3M annotThType is_poly_tvs types_only th_tys
; let lhs_type = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
fam') [Type]
annot_th_tys
; mb_sig <-
if (null cons || isGadtSyntaxTyCon rep_tc)
&& tyConAppNeedsKindSig False fam_tc (length ee_lhs)
then do { let full_kind = HasDebugCallStack => Type -> Type
Type -> Type
typeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
fam_tc [Type]
ee_lhs)
; th_full_kind <- reifyKind full_kind
; pure $ Just th_full_kind }
else pure Nothing
; return $
if isNewTyCon rep_tc
then TH.NewtypeInstD [] th_tvs lhs_type mb_sig (head cons) []
else TH.DataInstD [] th_tvs lhs_type mb_sig cons []
}
reifyType :: TyCoRep.Type -> TcM TH.Type
reifyType :: Type -> TcM Type
reifyType Type
ty | Type -> Bool
tcIsLiftedTypeKind Type
ty = Type -> TcM Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TH.StarT
reifyType ty :: Type
ty@(ForAllTy (Bndr TyVar
_ ForAllTyFlag
argf) Type
_)
= ForAllTyFlag -> Type -> TcM Type
reify_for_all ForAllTyFlag
argf Type
ty
reifyType (LitTy TyLit
t) = do { r <- TyLit -> TcM TyLit
reifyTyLit TyLit
t; return (TH.LitT r) }
reifyType (TyVarTy TyVar
tv) = Type -> TcM Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
TH.VarT (TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName TyVar
tv))
reifyType (TyConApp TyCon
tc [Type]
tys) = TyCon -> [Type] -> TcM Type
reify_tc_app TyCon
tc [Type]
tys
reifyType ty :: Type
ty@(AppTy {}) = do
let (Type
ty_head, [Type]
ty_args) = HasDebugCallStack => Type -> (Type, [Type])
Type -> (Type, [Type])
splitAppTys Type
ty
ty_head' <- Type -> TcM Type
reifyType Type
ty_head
ty_args' <- reifyTypes (filter_out_invisible_args ty_head ty_args)
pure $ mkThAppTs ty_head' ty_args'
where
filter_out_invisible_args :: Type -> [Type] -> [Type]
filter_out_invisible_args :: Type -> [Type] -> [Type]
filter_out_invisible_args Type
ty_head [Type]
ty_args =
[Bool] -> [Type] -> [Type]
forall a. [Bool] -> [a] -> [a]
filterByList ((ForAllTyFlag -> Bool) -> [ForAllTyFlag] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map ForAllTyFlag -> Bool
isVisibleForAllTyFlag ([ForAllTyFlag] -> [Bool]) -> [ForAllTyFlag] -> [Bool]
forall a b. (a -> b) -> a -> b
$ Type -> [Type] -> [ForAllTyFlag]
appTyForAllTyFlags Type
ty_head [Type]
ty_args)
[Type]
ty_args
reifyType ty :: Type
ty@(FunTy { ft_af :: Type -> FunTyFlag
ft_af = FunTyFlag
af, ft_mult :: Type -> Type
ft_mult = Type
ManyTy, ft_arg :: Type -> Type
ft_arg = Type
t1, ft_res :: Type -> Type
ft_res = Type
t2 })
| FunTyFlag -> Bool
isInvisibleFunArg FunTyFlag
af = ForAllTyFlag -> Type -> TcM Type
reify_for_all ForAllTyFlag
Inferred Type
ty
| Bool
otherwise = do { [r1,r2] <- [Type] -> TcM [Type]
reifyTypes [Type
t1,Type
t2]
; return (TH.ArrowT `TH.AppT` r1 `TH.AppT` r2) }
reifyType ty :: Type
ty@(FunTy { ft_af :: Type -> FunTyFlag
ft_af = FunTyFlag
af, ft_mult :: Type -> Type
ft_mult = Type
tm, ft_arg :: Type -> Type
ft_arg = Type
t1, ft_res :: Type -> Type
ft_res = Type
t2 })
| FunTyFlag -> Bool
isInvisibleFunArg FunTyFlag
af = UnrepresentableTypeDescr -> Type -> TcM Type
forall a. UnrepresentableTypeDescr -> Type -> TcM a
noTH UnrepresentableTypeDescr
LinearInvisibleArgument Type
ty
| Bool
otherwise = do { [rm,r1,r2] <- [Type] -> TcM [Type]
reifyTypes [Type
tm,Type
t1,Type
t2]
; return (TH.MulArrowT `TH.AppT` rm `TH.AppT` r1 `TH.AppT` r2) }
reifyType (CastTy Type
t KindCoercion
_) = Type -> TcM Type
reifyType Type
t
reifyType ty :: Type
ty@(CoercionTy {})= UnrepresentableTypeDescr -> Type -> TcM Type
forall a. UnrepresentableTypeDescr -> Type -> TcM a
noTH UnrepresentableTypeDescr
CoercionsInTypes Type
ty
reify_for_all :: TyCoRep.ForAllTyFlag -> TyCoRep.Type -> TcM TH.Type
reify_for_all :: ForAllTyFlag -> Type -> TcM Type
reify_for_all ForAllTyFlag
argf Type
ty
| ForAllTyFlag -> Bool
isVisibleForAllTyFlag ForAllTyFlag
argf
= do let ([TcReqTVBinder]
req_bndrs, Type
phi) = Type -> ([TcReqTVBinder], Type)
tcSplitForAllReqTVBinders Type
ty
tvbndrs' <- [TcReqTVBinder] -> TcM [TyVarBndr ()]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [TcReqTVBinder]
req_bndrs
phi' <- reifyType phi
pure $ TH.ForallVisT tvbndrs' phi'
| Bool
otherwise
= do let ([InvisTVBinder]
inv_bndrs, Type
phi) = Type -> ([InvisTVBinder], Type)
tcSplitForAllInvisTVBinders Type
ty
tvbndrs' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
inv_bndrs
let (cxt, tau) = tcSplitPhiTy phi
cxt' <- reifyCxt cxt
tau' <- reifyType tau
pure $ TH.ForallT tvbndrs' cxt' tau'
reifyTyLit :: TyCoRep.TyLit -> TcM TH.TyLit
reifyTyLit :: TyLit -> TcM TyLit
reifyTyLit (NumTyLit Integer
n) = TyLit -> TcM TyLit
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> TyLit
TH.NumTyLit Integer
n)
reifyTyLit (StrTyLit FastString
s) = TyLit -> TcM TyLit
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> TyLit
TH.StrTyLit (FastString -> String
unpackFS FastString
s))
reifyTyLit (CharTyLit Char
c) = TyLit -> TcM TyLit
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Char -> TyLit
TH.CharTyLit Char
c)
reifyTypes :: [Type] -> TcM [TH.Type]
reifyTypes :: [Type] -> TcM [Type]
reifyTypes = (Type -> TcM Type) -> [Type] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Type -> TcM Type
reifyType
reifyPatSynType
:: ([InvisTVBinder], ThetaType, [InvisTVBinder], ThetaType, [Scaled Type], Type) -> TcM TH.Type
reifyPatSynType :: ([InvisTVBinder], [Type], [InvisTVBinder], [Type], [Scaled Type],
Type)
-> TcM Type
reifyPatSynType ([InvisTVBinder]
univTyVars, [Type]
req, [InvisTVBinder]
exTyVars, [Type]
prov, [Scaled Type]
argTys, Type
resTy)
= do { univTyVars' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
univTyVars
; req' <- reifyCxt req
; exTyVars' <- reifyTyVarBndrs exTyVars
; prov' <- reifyCxt prov
; tau' <- reifyType (mkScaledFunTys argTys resTy)
; return $ TH.ForallT univTyVars' req'
$ TH.ForallT exTyVars' prov' tau' }
reifyKind :: Kind -> TcM TH.Kind
reifyKind :: Type -> TcM Type
reifyKind = Type -> TcM Type
reifyType
reifyCxt :: [PredType] -> TcM [TH.Pred]
reifyCxt :: [Type] -> TcM [Type]
reifyCxt = (Type -> TcM Type) -> [Type] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Type -> TcM Type
reifyType
reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
reifyFunDep :: FunDep TyVar -> FunDep
reifyFunDep ([TyVar]
xs, [TyVar]
ys) = [Name] -> [Name] -> FunDep
TH.FunDep ((TyVar -> Name) -> [TyVar] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName [TyVar]
xs) ((TyVar -> Name) -> [TyVar] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName [TyVar]
ys)
class ReifyFlag flag flag' | flag -> flag' where
reifyFlag :: flag -> flag'
instance ReifyFlag () () where
reifyFlag :: () -> ()
reifyFlag () = ()
instance ReifyFlag Specificity TH.Specificity where
reifyFlag :: Specificity -> Specificity
reifyFlag Specificity
SpecifiedSpec = Specificity
TH.SpecifiedSpec
reifyFlag Specificity
InferredSpec = Specificity
TH.InferredSpec
instance ReifyFlag TyConBndrVis (Maybe TH.BndrVis) where
reifyFlag :: TyConBndrVis -> Maybe BndrVis
reifyFlag TyConBndrVis
AnonTCB = BndrVis -> Maybe BndrVis
forall a. a -> Maybe a
Just BndrVis
TH.BndrReq
reifyFlag (NamedTCB ForAllTyFlag
Required) = BndrVis -> Maybe BndrVis
forall a. a -> Maybe a
Just BndrVis
TH.BndrReq
reifyFlag (NamedTCB (Invisible Specificity
_)) =
Maybe BndrVis
forall a. Maybe a
Nothing
reifyTyConBinders :: TyCon -> TcM [TH.TyVarBndr TH.BndrVis]
reifyTyConBinders :: TyCon -> TcM [TyVarBndr BndrVis]
reifyTyConBinders TyCon
tc = ([TyVarBndr (Maybe BndrVis)] -> [TyVarBndr BndrVis])
-> IOEnv (Env TcGblEnv TcLclEnv) [TyVarBndr (Maybe BndrVis)]
-> TcM [TyVarBndr BndrVis]
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TyVarBndr (Maybe BndrVis) -> Maybe (TyVarBndr BndrVis))
-> [TyVarBndr (Maybe BndrVis)] -> [TyVarBndr BndrVis]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe TyVarBndr (Maybe BndrVis) -> Maybe (TyVarBndr BndrVis)
forall flag. TyVarBndr (Maybe flag) -> Maybe (TyVarBndr flag)
get_bndr) ([VarBndr TyVar TyConBndrVis]
-> IOEnv (Env TcGblEnv TcLclEnv) [TyVarBndr (Maybe BndrVis)]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs (TyCon -> [VarBndr TyVar TyConBndrVis]
tyConBinders TyCon
tc))
where
get_bndr :: TH.TyVarBndr (Maybe flag) -> Maybe (TH.TyVarBndr flag)
get_bndr :: forall flag. TyVarBndr (Maybe flag) -> Maybe (TyVarBndr flag)
get_bndr = TyVarBndr (Maybe flag) -> Maybe (TyVarBndr flag)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
TyVarBndr (f a) -> f (TyVarBndr a)
sequenceA
reifyTyVars :: [TyVar] -> TcM [TH.TyVarBndr ()]
reifyTyVars :: [TyVar] -> TcM [TyVarBndr ()]
reifyTyVars = [TcReqTVBinder] -> TcM [TyVarBndr ()]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs ([TcReqTVBinder] -> TcM [TyVarBndr ()])
-> ([TyVar] -> [TcReqTVBinder]) -> [TyVar] -> TcM [TyVarBndr ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TyVar -> TcReqTVBinder) -> [TyVar] -> [TcReqTVBinder]
forall a b. (a -> b) -> [a] -> [b]
map TyVar -> TcReqTVBinder
forall {var}. var -> VarBndr var ()
mk_bndr
where
mk_bndr :: var -> VarBndr var ()
mk_bndr var
tv = var -> () -> VarBndr var ()
forall var argf. var -> argf -> VarBndr var argf
Bndr var
tv ()
reifyTyVarBndrs :: ReifyFlag flag flag'
=> [VarBndr TyVar flag] -> TcM [TH.TyVarBndr flag']
reifyTyVarBndrs :: forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr TyVar flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs = (VarBndr TyVar flag
-> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag'))
-> [VarBndr TyVar flag]
-> IOEnv (Env TcGblEnv TcLclEnv) [TyVarBndr flag']
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM VarBndr TyVar flag
-> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag')
forall {flag} {flag}.
ReifyFlag flag flag =>
VarBndr TyVar flag
-> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag)
reify_tvbndr
where
reify_tvbndr :: VarBndr TyVar flag
-> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag)
reify_tvbndr (Bndr TyVar
tv flag
fl) = Name -> flag -> Type -> TyVarBndr flag
forall flag. Name -> flag -> Type -> TyVarBndr flag
TH.KindedTV (TyVar -> Name
forall n. NamedThing n => n -> Name
reifyName TyVar
tv)
(flag -> flag
forall flag flag'. ReifyFlag flag flag' => flag -> flag'
reifyFlag flag
fl)
(Type -> TyVarBndr flag)
-> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> TcM Type
reifyKind (TyVar -> Type
tyVarKind TyVar
tv)
reifyTyVarsToMaybe :: [TyVar] -> TcM (Maybe [TH.TyVarBndr ()])
reifyTyVarsToMaybe :: [TyVar] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [] = Maybe [TyVarBndr ()] -> TcM (Maybe [TyVarBndr ()])
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing
reifyTyVarsToMaybe [TyVar]
tys = [TyVarBndr ()] -> Maybe [TyVarBndr ()]
forall a. a -> Maybe a
Just ([TyVarBndr ()] -> Maybe [TyVarBndr ()])
-> TcM [TyVarBndr ()] -> TcM (Maybe [TyVarBndr ()])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TyVar] -> TcM [TyVarBndr ()]
reifyTyVars [TyVar]
tys
reify_tc_app :: TyCon -> [Type.Type] -> TcM TH.Type
reify_tc_app :: TyCon -> [Type] -> TcM Type
reify_tc_app TyCon
tc [Type]
tys
= do { tys' <- [Type] -> TcM [Type]
reifyTypes (TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
tc [Type]
tys)
; maybe_sig_t (mkThAppTs r_tc tys') }
where
arity :: SumArity
arity = TyCon -> SumArity
tyConArity TyCon
tc
r_tc :: Type
r_tc | TyCon -> Bool
isUnboxedSumTyCon TyCon
tc = SumArity -> Type
TH.UnboxedSumT (SumArity
arity SumArity -> SumArity -> SumArity
forall a. Integral a => a -> a -> a
`div` SumArity
2)
| TyCon -> Bool
isUnboxedTupleTyCon TyCon
tc = SumArity -> Type
TH.UnboxedTupleT (SumArity
arity SumArity -> SumArity -> SumArity
forall a. Integral a => a -> a -> a
`div` SumArity
2)
| TyCon -> Bool
isPromotedTupleTyCon TyCon
tc = SumArity -> Type
TH.PromotedTupleT (SumArity
arity SumArity -> SumArity -> SumArity
forall a. Integral a => a -> a -> a
`div` SumArity
2)
| TyCon -> Bool
isTupleTyCon TyCon
tc = if TyCon -> Bool
isPromotedDataCon TyCon
tc
then SumArity -> Type
TH.PromotedTupleT SumArity
arity
else SumArity -> Type
TH.TupleT SumArity
arity
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
constraintKindTyConKey
= Type
TH.ConstraintT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
unrestrictedFunTyConKey = Type
TH.ArrowT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
listTyConKey = Type
TH.ListT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
nilDataConKey = Type
TH.PromotedNilT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
consDataConKey = Type
TH.PromotedConsT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
heqTyConKey = Type
TH.EqualityT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey = Type
TH.EqualityT
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey = Name -> Type
TH.ConT (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
coercibleTyCon)
| TyCon -> Bool
isDataKindsPromotedDataCon TyCon
tc = Name -> Type
TH.PromotedT (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc)
| Bool
otherwise = Name -> Type
TH.ConT (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc)
maybe_sig_t :: Type -> TcM Type
maybe_sig_t Type
th_type
| Bool -> TyCon -> SumArity -> Bool
tyConAppNeedsKindSig
Bool
False
TyCon
tc ([Type] -> SumArity
forall a. [a] -> SumArity
forall (t :: * -> *) a. Foldable t => t a -> SumArity
length [Type]
tys)
= do { let full_kind :: Type
full_kind = HasDebugCallStack => Type -> Type
Type -> Type
typeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys)
; th_full_kind <- Type -> TcM Type
reifyKind Type
full_kind
; return (TH.SigT th_type th_full_kind) }
| Bool
otherwise
= Type -> TcM Type
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_type
reifyName :: NamedThing n => n -> TH.Name
reifyName :: forall n. NamedThing n => n -> Name
reifyName n
thing
| Name -> Bool
isExternalName Name
name
= String -> String -> String -> Name
mk_varg String
pkg_str String
mod_str String
occ_str
| Bool
otherwise = String -> Integer -> Name
TH.mkNameU String
occ_str (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ Unique -> Word64
getKey (Name -> Unique
forall a. Uniquable a => a -> Unique
getUnique Name
name))
where
name :: Name
name = n -> Name
forall a. NamedThing a => a -> Name
getName n
thing
mod :: GenModule Unit
mod = Bool -> GenModule Unit -> GenModule Unit
forall a. HasCallStack => Bool -> a -> a
assert (Name -> Bool
isExternalName Name
name) (GenModule Unit -> GenModule Unit)
-> GenModule Unit -> GenModule Unit
forall a b. (a -> b) -> a -> b
$ HasDebugCallStack => Name -> GenModule Unit
Name -> GenModule Unit
nameModule Name
name
pkg_str :: String
pkg_str = Unit -> String
forall u. IsUnitId u => u -> String
unitString (GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
mod)
mod_str :: String
mod_str = ModuleName -> String
moduleNameString (GenModule Unit -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName GenModule Unit
mod)
occ_str :: String
occ_str = OccName -> String
occNameString OccName
occ
occ :: OccName
occ = Name -> OccName
nameOccName Name
name
mk_varg :: String -> String -> String -> Name
mk_varg | OccName -> Bool
OccName.isDataOcc OccName
occ = String -> String -> String -> Name
TH.mkNameG_d
| OccName -> Bool
OccName.isVarOcc OccName
occ = String -> String -> String -> Name
TH.mkNameG_v
| OccName -> Bool
OccName.isTcOcc OccName
occ = String -> String -> String -> Name
TH.mkNameG_tc
| Just FastString
con_fs <- OccName -> Maybe FastString
OccName.fieldOcc_maybe OccName
occ
= \ String
pkg String
mod String
occ -> String -> String -> String -> String -> Name
TH.mkNameG_fld String
pkg String
mod (FastString -> String
unpackFS FastString
con_fs) String
occ
| Bool
otherwise = String -> SDoc -> String -> String -> String -> Name
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"reifyName" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name)
reifyFieldLabel :: FieldLabel -> TH.Name
reifyFieldLabel :: FieldLabel -> Name
reifyFieldLabel FieldLabel
fl = Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> Name -> Name
forall a b. (a -> b) -> a -> b
$ FieldLabel -> Name
flSelector FieldLabel
fl
reifyFixity :: Name -> TcM (Maybe TH.Fixity)
reifyFixity :: Name -> TcM (Maybe Fixity)
reifyFixity Name
name
= do { (found, fix) <- Name -> RnM (Bool, Fixity)
lookupFixityRn_help Name
name
; return (if found then Just (conv_fix fix) else Nothing) }
where
conv_fix :: Fixity -> Fixity
conv_fix (Hs.Fixity SourceText
_ SumArity
i FixityDirection
d) = SumArity -> FixityDirection -> Fixity
TH.Fixity SumArity
i (FixityDirection -> FixityDirection
conv_dir FixityDirection
d)
conv_dir :: FixityDirection -> FixityDirection
conv_dir FixityDirection
Hs.InfixR = FixityDirection
TH.InfixR
conv_dir FixityDirection
Hs.InfixL = FixityDirection
TH.InfixL
conv_dir FixityDirection
Hs.InfixN = FixityDirection
TH.InfixN
reifyUnpackedness :: DataCon.SrcUnpackedness -> TH.SourceUnpackedness
reifyUnpackedness :: SrcUnpackedness -> SourceUnpackedness
reifyUnpackedness SrcUnpackedness
NoSrcUnpack = SourceUnpackedness
TH.NoSourceUnpackedness
reifyUnpackedness SrcUnpackedness
SrcNoUnpack = SourceUnpackedness
TH.SourceNoUnpack
reifyUnpackedness SrcUnpackedness
SrcUnpack = SourceUnpackedness
TH.SourceUnpack
reifyStrictness :: DataCon.SrcStrictness -> TH.SourceStrictness
reifyStrictness :: SrcStrictness -> SourceStrictness
reifyStrictness SrcStrictness
NoSrcStrict = SourceStrictness
TH.NoSourceStrictness
reifyStrictness SrcStrictness
SrcStrict = SourceStrictness
TH.SourceStrict
reifyStrictness SrcStrictness
SrcLazy = SourceStrictness
TH.SourceLazy
reifySourceBang :: DataCon.HsSrcBang
-> (TH.SourceUnpackedness, TH.SourceStrictness)
reifySourceBang :: HsSrcBang -> (SourceUnpackedness, SourceStrictness)
reifySourceBang (HsSrcBang SourceText
_ SrcUnpackedness
u SrcStrictness
s) = (SrcUnpackedness -> SourceUnpackedness
reifyUnpackedness SrcUnpackedness
u, SrcStrictness -> SourceStrictness
reifyStrictness SrcStrictness
s)
reifyDecidedStrictness :: DataCon.HsImplBang -> TH.DecidedStrictness
reifyDecidedStrictness :: HsImplBang -> DecidedStrictness
reifyDecidedStrictness HsImplBang
HsLazy = DecidedStrictness
TH.DecidedLazy
reifyDecidedStrictness (HsStrict Bool
_) = DecidedStrictness
TH.DecidedStrict
reifyDecidedStrictness HsUnpack{} = DecidedStrictness
TH.DecidedUnpack
reifyTypeOfThing :: TH.Name -> TcM TH.Type
reifyTypeOfThing :: Name -> TcM Type
reifyTypeOfThing Name
th_name = do
thing <- Name -> TcM TcTyThing
getThing Name
th_name
case thing of
AGlobal (AnId TyVar
id) -> Type -> TcM Type
reifyType (TyVar -> Type
idType TyVar
id)
AGlobal (ATyCon TyCon
tc) -> Type -> TcM Type
reifyKind (TyCon -> Type
tyConKind TyCon
tc)
AGlobal (AConLike (RealDataCon DataCon
dc)) ->
Type -> TcM Type
reifyType (TyVar -> Type
idType (DataCon -> TyVar
dataConWrapId DataCon
dc))
AGlobal (AConLike (PatSynCon PatSyn
ps)) ->
([InvisTVBinder], [Type], [InvisTVBinder], [Type], [Scaled Type],
Type)
-> TcM Type
reifyPatSynType (PatSyn
-> ([InvisTVBinder], [Type], [InvisTVBinder], [Type],
[Scaled Type], Type)
patSynSigBndr PatSyn
ps)
ATcId{tct_id :: TcTyThing -> TyVar
tct_id = TyVar
id} -> ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. ZonkM a -> TcM a
liftZonkM (Type -> ZonkM Type
zonkTcType (TyVar -> Type
idType TyVar
id)) IOEnv (Env TcGblEnv TcLclEnv) Type
-> (Type -> TcM Type) -> TcM Type
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Type -> TcM Type
reifyType
ATyVar Name
_ TyVar
tctv -> ZonkM Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall a. ZonkM a -> TcM a
liftZonkM (TyVar -> ZonkM Type
zonkTcTyVar TyVar
tctv) IOEnv (Env TcGblEnv TcLclEnv) Type
-> (Type -> TcM Type) -> TcM Type
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Type -> TcM Type
reifyType
AGlobal (ACoAxiom CoAxiom Branched
_) -> String -> TcM Type
forall a. HasCallStack => String -> a
panic String
"reifyTypeOfThing: ACoAxiom"
ATcTyCon TyCon
_ -> String -> TcM Type
forall a. HasCallStack => String -> a
panic String
"reifyTypeOfThing: ATcTyCon"
APromotionErr PromotionErr
_ -> String -> TcM Type
forall a. HasCallStack => String -> a
panic String
"reifyTypeOfThing: APromotionErr"
lookupThAnnLookup :: TH.AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup :: AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup (TH.AnnLookupName Name
th_nm) = (Name -> CoreAnnTarget) -> TcM Name -> TcM CoreAnnTarget
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> CoreAnnTarget
forall name. name -> AnnTarget name
NamedTarget (Name -> TcM Name
lookupThName Name
th_nm)
lookupThAnnLookup (TH.AnnLookupModule (TH.Module PkgName
pn ModName
mn))
= CoreAnnTarget -> TcM CoreAnnTarget
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreAnnTarget -> TcM CoreAnnTarget)
-> CoreAnnTarget -> TcM CoreAnnTarget
forall a b. (a -> b) -> a -> b
$ GenModule Unit -> CoreAnnTarget
forall name. GenModule Unit -> AnnTarget name
ModuleTarget (GenModule Unit -> CoreAnnTarget)
-> GenModule Unit -> CoreAnnTarget
forall a b. (a -> b) -> a -> b
$
Unit -> ModuleName -> GenModule Unit
forall u. u -> ModuleName -> GenModule u
mkModule (String -> Unit
stringToUnit (String -> Unit) -> String -> Unit
forall a b. (a -> b) -> a -> b
$ PkgName -> String
TH.pkgString PkgName
pn) (String -> ModuleName
mkModuleName (String -> ModuleName) -> String -> ModuleName
forall a b. (a -> b) -> a -> b
$ ModName -> String
TH.modString ModName
mn)
reifyAnnotations :: Data a => TH.AnnLookup -> TcM [a]
reifyAnnotations :: forall a. Data a => AnnLookup -> TcM [a]
reifyAnnotations AnnLookup
th_name
= do { name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; topEnv <- getTopEnv
; epsHptAnns <- liftIO $ prepareAnnotations topEnv Nothing
; tcg <- getGblEnv
; let selectedEpsHptAnns = ([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
forall a.
Typeable a =>
([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
findAnns [Word8] -> a
forall a. Data a => [Word8] -> a
deserializeWithData AnnEnv
epsHptAnns CoreAnnTarget
name
; let selectedTcgAnns = ([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
forall a.
Typeable a =>
([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
findAnns [Word8] -> a
forall a. Data a => [Word8] -> a
deserializeWithData (TcGblEnv -> AnnEnv
tcg_ann_env TcGblEnv
tcg) CoreAnnTarget
name
; return (selectedEpsHptAnns ++ selectedTcgAnns) }
modToTHMod :: Module -> TH.Module
modToTHMod :: GenModule Unit -> Module
modToTHMod GenModule Unit
m = PkgName -> ModName -> Module
TH.Module (String -> PkgName
TH.PkgName (String -> PkgName) -> String -> PkgName
forall a b. (a -> b) -> a -> b
$ Unit -> String
forall u. IsUnitId u => u -> String
unitString (Unit -> String) -> Unit -> String
forall a b. (a -> b) -> a -> b
$ GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
m)
(String -> ModName
TH.ModName (String -> ModName) -> String -> ModName
forall a b. (a -> b) -> a -> b
$ ModuleName -> String
moduleNameString (ModuleName -> String) -> ModuleName -> String
forall a b. (a -> b) -> a -> b
$ GenModule Unit -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName GenModule Unit
m)
reifyModule :: TH.Module -> TcM TH.ModuleInfo
reifyModule :: Module -> TcM ModuleInfo
reifyModule (TH.Module (TH.PkgName String
pkgString) (TH.ModName String
mString)) = do
this_mod <- IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule
let reifMod = Unit -> ModuleName -> GenModule Unit
forall u. u -> ModuleName -> GenModule u
mkModule (String -> Unit
stringToUnit String
pkgString) (String -> ModuleName
mkModuleName String
mString)
if (reifMod == this_mod) then reifyThisModule else reifyFromIface reifMod
where
reifyThisModule :: TcM ModuleInfo
reifyThisModule = do
usages <- (ImportAvails -> [Module])
-> IOEnv (Env TcGblEnv TcLclEnv) ImportAvails
-> IOEnv (Env TcGblEnv TcLclEnv) [Module]
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((GenModule Unit -> Module) -> [GenModule Unit] -> [Module]
forall a b. (a -> b) -> [a] -> [b]
map GenModule Unit -> Module
modToTHMod ([GenModule Unit] -> [Module])
-> (ImportAvails -> [GenModule Unit]) -> ImportAvails -> [Module]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleEnv [ImportedBy] -> [GenModule Unit]
forall a. ModuleEnv a -> [GenModule Unit]
moduleEnvKeys (ModuleEnv [ImportedBy] -> [GenModule Unit])
-> (ImportAvails -> ModuleEnv [ImportedBy])
-> ImportAvails
-> [GenModule Unit]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImportAvails -> ModuleEnv [ImportedBy]
imp_mods) IOEnv (Env TcGblEnv TcLclEnv) ImportAvails
getImports
return $ TH.ModuleInfo usages
reifyFromIface :: GenModule Unit -> TcM ModuleInfo
reifyFromIface GenModule Unit
reifMod = do
iface <- SDoc -> GenModule Unit -> IOEnv (Env TcGblEnv TcLclEnv) ModIface
loadInterfaceForModule (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"reifying module from TH for" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> GenModule Unit -> SDoc
forall a. Outputable a => a -> SDoc
ppr GenModule Unit
reifMod) GenModule Unit
reifMod
let usages = [GenModule Unit -> Module
modToTHMod GenModule Unit
m | Usage
usage <- ModIface -> [Usage]
forall (phase :: ModIfacePhase). ModIface_ phase -> [Usage]
mi_usages ModIface
iface,
Just GenModule Unit
m <- [Unit -> Usage -> Maybe (GenModule Unit)
usageToModule (GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
reifMod) Usage
usage] ]
return $ TH.ModuleInfo usages
usageToModule :: Unit -> Usage -> Maybe Module
usageToModule :: Unit -> Usage -> Maybe (GenModule Unit)
usageToModule Unit
_ (UsageFile {}) = Maybe (GenModule Unit)
forall a. Maybe a
Nothing
usageToModule Unit
this_pkg (UsageHomeModule { usg_mod_name :: Usage -> ModuleName
usg_mod_name = ModuleName
mn }) = GenModule Unit -> Maybe (GenModule Unit)
forall a. a -> Maybe a
Just (GenModule Unit -> Maybe (GenModule Unit))
-> GenModule Unit -> Maybe (GenModule Unit)
forall a b. (a -> b) -> a -> b
$ Unit -> ModuleName -> GenModule Unit
forall u. u -> ModuleName -> GenModule u
mkModule Unit
this_pkg ModuleName
mn
usageToModule Unit
_ (UsagePackageModule { usg_mod :: Usage -> GenModule Unit
usg_mod = GenModule Unit
m }) = GenModule Unit -> Maybe (GenModule Unit)
forall a. a -> Maybe a
Just GenModule Unit
m
usageToModule Unit
_ (UsageMergedRequirement { usg_mod :: Usage -> GenModule Unit
usg_mod = GenModule Unit
m }) = GenModule Unit -> Maybe (GenModule Unit)
forall a. a -> Maybe a
Just GenModule Unit
m
usageToModule Unit
this_pkg (UsageHomeModuleInterface { usg_mod_name :: Usage -> ModuleName
usg_mod_name = ModuleName
mn }) = GenModule Unit -> Maybe (GenModule Unit)
forall a. a -> Maybe a
Just (GenModule Unit -> Maybe (GenModule Unit))
-> GenModule Unit -> Maybe (GenModule Unit)
forall a b. (a -> b) -> a -> b
$ Unit -> ModuleName -> GenModule Unit
forall u. u -> ModuleName -> GenModule u
mkModule Unit
this_pkg ModuleName
mn
mkThAppTs :: TH.Type -> [TH.Type] -> TH.Type
mkThAppTs :: Type -> [Type] -> Type
mkThAppTs Type
fun_ty [Type]
arg_tys = (Type -> Type -> Type) -> Type -> [Type] -> Type
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Type -> Type -> Type
TH.AppT Type
fun_ty [Type]
arg_tys
noTH :: UnrepresentableTypeDescr -> Type -> TcM a
noTH :: forall a. UnrepresentableTypeDescr -> Type -> TcM a
noTH UnrepresentableTypeDescr
s Type
d = TcRnMessage -> TcM a
forall a. TcRnMessage -> TcM a
failWithTc (TcRnMessage -> TcM a) -> TcRnMessage -> TcM a
forall a b. (a -> b) -> a -> b
$ THError -> TcRnMessage
TcRnTHError (THError -> TcRnMessage) -> THError -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ THReifyError -> THError
THReifyError (THReifyError -> THError) -> THReifyError -> THError
forall a b. (a -> b) -> a -> b
$ UnrepresentableTypeDescr -> Type -> THReifyError
CannotRepresentType UnrepresentableTypeDescr
s Type
d
ppr_th :: TH.Ppr a => a -> SDoc
ppr_th :: forall a. Ppr a => a -> SDoc
ppr_th a
x = String -> SDoc
forall doc. IsLine doc => String -> doc
text (a -> String
forall a. Ppr a => a -> String
TH.pprint a
x)
tcGetInterp :: TcM Interp
tcGetInterp :: TcM Interp
tcGetInterp = do
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
case hsc_interp hsc_env of
Maybe Interp
Nothing -> IO Interp -> TcM Interp
forall a. IO a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Interp -> TcM Interp) -> IO Interp -> TcM Interp
forall a b. (a -> b) -> a -> b
$ GhcException -> IO Interp
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (String -> GhcException
InstallationError String
"Template haskell requires a target code interpreter")
Just Interp
i -> Interp -> TcM Interp
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Interp
i