{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.Tc.Gen.Splice(
tcSpliceExpr, tcTypedBracket, tcUntypedBracket,
runAnnotation,
runMetaE, runMetaP, runMetaT, runMetaD, runQuasi,
tcTopSpliceExpr, lookupThName_maybe,
defaultRunMeta, runMeta', runRemoteModFinalizers,
finishTH, runTopSplice
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Hs
import GHC.Types.Annotations
import GHC.Driver.Finder
import GHC.Types.Name
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.TcType
import GHC.Core.Multiplicity
import GHC.Utils.Outputable
import GHC.Tc.Gen.Expr
import GHC.Types.SrcLoc
import GHC.Builtin.Names.TH
import GHC.Tc.Utils.Unify
import GHC.Tc.Utils.Env
import GHC.Tc.Types.Origin
import GHC.Core.Coercion( etaExpandCoAxBranch )
import GHC.SysTools.FileCleanup ( newTempName, TempFileLifetime(..) )
import Control.Monad
import GHCi.Message
import GHCi.RemoteTypes
import GHC.Runtime.Interpreter
import GHC.Runtime.Interpreter.Types
import GHC.Driver.Main
import GHC.Rename.Splice( traceSplice, SpliceInfo(..))
import GHC.Types.Name.Reader
import GHC.Driver.Types
import GHC.ThToHs
import GHC.Rename.Expr
import GHC.Rename.Env
import GHC.Rename.Utils ( HsDocContext(..) )
import GHC.Rename.Fixity ( lookupFixityRn_help )
import GHC.Rename.HsType
import GHC.Tc.Utils.Zonk
import GHC.Tc.Solver
import GHC.Core.Type as Type
import GHC.Types.Name.Set
import GHC.Tc.Utils.TcMType
import GHC.Tc.Gen.HsType
import GHC.IfaceToCore
import GHC.Core.TyCo.Rep as TyCoRep
import GHC.Tc.Instance.Family
import GHC.Core.FamInstEnv
import GHC.Core.InstEnv as InstEnv
import GHC.Tc.Utils.Instantiate
import GHC.Types.Name.Env
import GHC.Builtin.Names
import GHC.Builtin.Types
import GHC.Types.Name.Occurrence as OccName
import GHC.Driver.Hooks
import GHC.Types.Var
import GHC.Unit.Module
import GHC.Iface.Load
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.Tc.Types.Evidence
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.HsToCore.Expr
import GHC.HsToCore.Monad
import GHC.Serialized
import GHC.Utils.Error
import GHC.Utils.Misc
import GHC.Types.Unique
import GHC.Types.Var.Set
import Data.List ( find )
import Data.Maybe
import GHC.Data.FastString
import GHC.Types.Basic as BasicTypes hiding( SuccessFlag(..) )
import GHC.Data.Maybe( MaybeErr(..) )
import GHC.Driver.Session
import GHC.Utils.Panic as Panic
import GHC.Utils.Lexeme
import qualified GHC.Data.EnumSet as EnumSet
import GHC.Driver.Plugins
import GHC.Data.Bag
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.Exception
import Data.Binary
import Data.Binary.Get
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Dynamic ( fromDynamic, toDyn )
import qualified Data.Map as Map
import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep )
import Data.Data (Data)
import Data.Proxy ( Proxy (..) )
tcTypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcUntypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> [PendingRnSplice] -> ExpRhoType
-> TcM (HsExpr GhcTc)
tcSpliceExpr :: HsSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
tcTypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedBracket HsExpr GhcRn
rn_expr brack :: HsBracket GhcRn
brack@(TExpBr XTExpBr GhcRn
_ LHsExpr GhcRn
expr) ExpRhoType
res_ty
= MsgDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (HsBracket GhcRn -> MsgDoc
quotationCtxtDoc HsBracket GhcRn
brack) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
do { ThStage
cur_stage <- TcM ThStage
getStage
; IORef [PendingTcSplice]
ps_ref <- [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) (IORef [PendingTcSplice])
forall a env. a -> IOEnv env (IORef a)
newMutVar []
; TcRef WantedConstraints
lie_var <- TcM (TcRef WantedConstraints)
getConstraintVar
; Type
m_var <- Id -> Type
mkTyVarTy (Id -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Id
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar
; Id
ev_var <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var
; let wrapper :: QuoteWrapper
wrapper = Id -> Type -> QuoteWrapper
QuoteWrapper Id
ev_var Type
m_var
; (LHsExpr GhcTc
_tc_expr, Type
expr_ty) <- ThStage -> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a. ThStage -> TcM a -> TcM a
setStage (ThStage -> PendingStuff -> ThStage
Brack ThStage
cur_stage (IORef [PendingTcSplice]
-> TcRef WantedConstraints -> QuoteWrapper -> PendingStuff
TcPending IORef [PendingTcSplice]
ps_ref TcRef WantedConstraints
lie_var QuoteWrapper
wrapper)) (TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type))
-> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a b. (a -> b) -> a -> b
$
Type -> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
Many (TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type))
-> TcM (LHsExpr GhcTc, Type) -> TcM (LHsExpr GhcTc, Type)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRhoNC LHsExpr GhcRn
expr
; let rep :: Type
rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
expr_ty
; Type
meta_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_var Type
expr_ty
; [PendingTcSplice]
ps' <- IORef [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a env. IORef a -> IOEnv env a
readMutVar IORef [PendingTcSplice]
ps_ref
; Id
texpco <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
unsafeCodeCoerceName
; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTc
-> Type
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcWrapResultO (String -> CtOrigin
Shouldn'tHappenOrigin String
"TExpBr")
HsExpr GhcRn
rn_expr
(LHsExpr GhcTc -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc (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
wrapper)
(Id -> [Type] -> LHsExpr GhcTc
nlHsTyApp Id
texpco [Type
rep, Type
expr_ty]))
(HsExpr GhcTc -> LHsExpr GhcTc
forall e. e -> Located e
noLoc (XTcBracketOut GhcTc
-> Maybe QuoteWrapper
-> HsBracket GhcRn
-> [PendingTcSplice]
-> HsExpr GhcTc
forall p.
XTcBracketOut p
-> Maybe QuoteWrapper
-> HsBracket GhcRn
-> [PendingTcSplice]
-> HsExpr p
HsTcBracketOut NoExtField
XTcBracketOut GhcTc
noExtField (QuoteWrapper -> Maybe QuoteWrapper
forall a. a -> Maybe a
Just QuoteWrapper
wrapper) HsBracket GhcRn
brack [PendingTcSplice]
ps'))))
Type
meta_ty ExpRhoType
res_ty }
tcTypedBracket HsExpr GhcRn
_ HsBracket GhcRn
other_brack ExpRhoType
_
= String -> MsgDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"tcTypedBracket" (HsBracket GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsBracket GhcRn
other_brack)
tcUntypedBracket :: HsExpr GhcRn
-> HsBracket GhcRn
-> [PendingRnSplice]
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcUntypedBracket HsExpr GhcRn
rn_expr HsBracket GhcRn
brack [PendingRnSplice]
ps ExpRhoType
res_ty
= do { String -> MsgDoc -> TcRn ()
traceTc String
"tc_bracket untyped" (HsBracket GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsBracket GhcRn
brack MsgDoc -> MsgDoc -> MsgDoc
$$ [PendingRnSplice] -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr [PendingRnSplice]
ps)
; (Maybe QuoteWrapper
brack_info, Type
expected_type) <- HsBracket GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsBracket GhcRn
brack
; [PendingTcSplice]
ps' <- case QuoteWrapper -> Type
quoteWrapperTyVarTy (QuoteWrapper -> Type) -> Maybe QuoteWrapper -> DFunInstType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe QuoteWrapper
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)
mapM (Type
-> PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice Type
m_var) [PendingRnSplice]
ps
DFunInstType
Nothing -> ASSERT(null ps) return []
; String -> MsgDoc -> TcRn ()
traceTc String
"tc_bracket done untyped" (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Type
expected_type)
; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTc
-> Type
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcWrapResultO CtOrigin
BracketOrigin HsExpr GhcRn
rn_expr
(XTcBracketOut GhcTc
-> Maybe QuoteWrapper
-> HsBracket GhcRn
-> [PendingTcSplice]
-> HsExpr GhcTc
forall p.
XTcBracketOut p
-> Maybe QuoteWrapper
-> HsBracket GhcRn
-> [PendingTcSplice]
-> HsExpr p
HsTcBracketOut NoExtField
XTcBracketOut GhcTc
noExtField Maybe QuoteWrapper
brack_info HsBracket GhcRn
brack [PendingTcSplice]
ps')
Type
expected_type ExpRhoType
res_ty
}
mkMetaTyVar :: TcM TyVar
mkMetaTyVar :: IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar =
FastString -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
newNamedFlexiTyVar (String -> FastString
fsLit String
"m") (Type -> Type -> Type
mkVisFunTyMany Type
liftedTypeKind Type
liftedTypeKind)
emitQuoteWanted :: Type -> TcM EvVar
emitQuoteWanted :: Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var = do
TyCon
quote_con <- Name -> TcM TyCon
tcLookupTyCon Name
quoteClassName
CtOrigin -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitWantedEvVar CtOrigin
BracketOrigin (Type -> IOEnv (Env TcGblEnv TcLclEnv) Id)
-> Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
forall a b. (a -> b) -> a -> b
$
TyCon -> [Type] -> Type
mkTyConApp TyCon
quote_con [Type
m_var]
brackTy :: HsBracket GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy :: HsBracket GhcRn -> TcM (Maybe QuoteWrapper, Type)
brackTy HsBracket GhcRn
b =
let mkTy :: Name -> TcM (Maybe QuoteWrapper, Type)
mkTy Name
n = do
Type
m_var <- Id -> Type
mkTyVarTy (Id -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Id
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) Id
mkMetaTyVar
Id
ev_var <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Id
emitQuoteWanted Type
m_var
Type
final_ty <- Type -> Type -> Type
mkAppTy Type
m_var (Type -> Type)
-> IOEnv (Env TcGblEnv TcLclEnv) Type
-> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
n
let wrapper :: QuoteWrapper
wrapper = Id -> Type -> QuoteWrapper
QuoteWrapper Id
ev_var Type
m_var
(Maybe QuoteWrapper, Type) -> TcM (Maybe QuoteWrapper, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (QuoteWrapper -> Maybe QuoteWrapper
forall a. a -> Maybe a
Just QuoteWrapper
wrapper, Type
final_ty)
in
case HsBracket 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. String -> a
panic String
"tcBrackTy: Unexpected DecBrL"
(TExpBr {}) -> String -> TcM (Maybe QuoteWrapper, Type)
forall a. String -> a
panic String
"tcUntypedBracket: Unexpected TExpBr"
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 { Type
meta_ty <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
meta_ty_name
; let expected_type :: Type
expected_type = Type -> Type -> Type
mkAppTy Type
m_var Type
meta_ty
; LHsExpr GhcTc
expr' <- Type -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
Many (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr Type
expected_type
; PendingTcSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> LHsExpr GhcTc -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTc
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
$ MsgDoc -> TcRn ()
addErr (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
err_msg Type
exp_ty)
; TyCon
codeCon <- Name -> TcM TyCon
tcLookupTyCon Name
codeTyConName
; let rep :: Type
rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
exp_ty
; Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> [Type] -> Type
mkTyConApp TyCon
codeCon [Type
rep, Type
m_ty, Type
exp_ty]) }
where
err_msg :: a -> MsgDoc
err_msg a
ty
= [MsgDoc] -> MsgDoc
vcat [ String -> MsgDoc
text String
"Illegal polytype:" MsgDoc -> MsgDoc -> MsgDoc
<+> a -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr a
ty
, String -> MsgDoc
text String
"The type of a Typed Template Haskell expression must" MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text String
"not have any quantification." ]
quotationCtxtDoc :: HsBracket GhcRn -> SDoc
quotationCtxtDoc :: HsBracket GhcRn -> MsgDoc
quotationCtxtDoc HsBracket GhcRn
br_body
= MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text String
"In the Template Haskell quotation")
Arity
2 (HsBracket GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsBracket GhcRn
br_body)
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin :: TcM Origin
getThSpliceOrigin = do
Bool
warn <- GeneralFlag -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. GeneralFlag -> TcRnIf gbl lcl Bool
goptM GeneralFlag
Opt_EnableThSpliceWarnings
if Bool
warn then Origin -> TcM Origin
forall (m :: * -> *) a. Monad m => a -> m a
return Origin
FromSource else Origin -> TcM Origin
forall (m :: * -> *) a. Monad m => a -> m a
return Origin
Generated
tcSpliceExpr :: HsSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcSpliceExpr splice :: HsSplice GhcRn
splice@(HsTypedSplice XTypedSplice GhcRn
_ SpliceDecoration
_ IdP GhcRn
name LHsExpr GhcRn
expr) ExpRhoType
res_ty
= MsgDoc -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (HsSplice GhcRn -> MsgDoc
spliceCtxtDoc HsSplice GhcRn
splice) (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 (LHsExpr GhcRn -> SrcSpan
forall l e. GenLocated l e -> l
getLoc LHsExpr GhcRn
expr) (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ do
{ ThStage
stage <- TcM ThStage
getStage
; case ThStage
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
IdP GhcRn
name LHsExpr GhcRn
expr ExpRhoType
res_ty
RunSplice TcRef [ForeignRef (Q ())]
_ ->
String -> MsgDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic (String
"tcSpliceExpr: attempted to typecheck a splice when " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"running another splice") (HsSplice GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsSplice GhcRn
splice)
ThStage
Comp -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
}
tcSpliceExpr HsSplice GhcRn
splice ExpRhoType
_
= String -> MsgDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"tcSpliceExpr" (HsSplice GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsSplice GhcRn
splice)
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 Id
_ Type
m_var)) Name
splice_name LHsExpr GhcRn
expr ExpRhoType
res_ty
= do { Type
res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
; let rep :: Type
rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
res_ty
; Type
meta_exp_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
m_var Type
res_ty
; LHsExpr GhcTc
expr' <- ThStage -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. ThStage -> TcM a -> TcM a
setStage ThStage
pop_stage (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
TcRef WantedConstraints
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. TcRef WantedConstraints -> TcM a -> TcM a
setConstraintVar TcRef WantedConstraints
lie_var (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
expr Type
meta_exp_ty
; Id
untype_code <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
unTypeCodeName
; let expr'' :: LHsExpr GhcTc
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)
(Id -> [Type] -> LHsExpr GhcTc
nlHsTyApp Id
untype_code [Type
rep, Type
res_ty])) LHsExpr GhcTc
expr'
; [PendingTcSplice]
ps <- IORef [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a env. IORef a -> IOEnv env a
readMutVar IORef [PendingTcSplice]
ps_var
; IORef [PendingTcSplice] -> [PendingTcSplice] -> TcRn ()
forall a env. IORef a -> a -> IOEnv env ()
writeMutVar IORef [PendingTcSplice]
ps_var (Name -> LHsExpr GhcTc -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTc
expr'' PendingTcSplice -> [PendingTcSplice] -> [PendingTcSplice]
forall a. a -> [a] -> [a]
: [PendingTcSplice]
ps)
; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (XSpliceE GhcTc -> HsSplice GhcTc -> HsExpr GhcTc
forall p. XSpliceE p -> HsSplice p -> HsExpr p
HsSpliceE NoExtField
XSpliceE GhcTc
noExtField (HsSplice GhcTc -> HsExpr GhcTc) -> HsSplice GhcTc -> HsExpr GhcTc
forall a b. (a -> b) -> a -> b
$
XSpliced GhcTc
-> ThModFinalizers -> HsSplicedThing GhcTc -> HsSplice GhcTc
forall id.
XSpliced id -> ThModFinalizers -> HsSplicedThing id -> HsSplice id
HsSpliced NoExtField
XSpliced GhcTc
noExtField ([ForeignRef (Q ())] -> ThModFinalizers
ThModFinalizers []) (HsSplicedThing GhcTc -> HsSplice GhcTc)
-> HsSplicedThing GhcTc -> HsSplice GhcTc
forall a b. (a -> b) -> a -> b
$
HsExpr GhcTc -> HsSplicedThing GhcTc
forall id. HsExpr id -> HsSplicedThing id
HsSplicedExpr (LHsExpr GhcTc -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcTc
expr'')) }
tcNestedSplice ThStage
_ PendingStuff
_ Name
splice_name LHsExpr GhcRn
_ ExpRhoType
_
= String -> MsgDoc -> TcM (HsExpr GhcTc)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"tcNestedSplice: rename stage found" (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
splice_name)
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
= do {
Type
res_ty <- ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Type
expTypeToType ExpRhoType
res_ty
; Type
q_type <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcMetaTy Name
qTyConName
; Type
meta_exp_ty <- Type -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcTExpTy Type
q_type Type
res_ty
; LHsExpr GhcTc
q_expr <- SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
Typed (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
expr Type
meta_exp_ty
; TcLclEnv
lcl_env <- TcRnIf TcGblEnv TcLclEnv TcLclEnv
forall gbl lcl. TcRnIf gbl lcl lcl
getLclEnv
; let delayed_splice :: DelayedSplice
delayed_splice
= TcLclEnv -> LHsExpr GhcRn -> Type -> LHsExpr GhcTc -> DelayedSplice
DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
expr Type
res_ty LHsExpr GhcTc
q_expr
; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (XSpliceE GhcTc -> HsSplice GhcTc -> HsExpr GhcTc
forall p. XSpliceE p -> HsSplice p -> HsExpr p
HsSpliceE NoExtField
XSpliceE GhcTc
noExtField (XXSplice GhcTc -> HsSplice GhcTc
forall id. XXSplice id -> HsSplice id
XSplice (DelayedSplice -> HsSplicedT
HsSplicedT DelayedSplice
delayed_splice)))
}
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)
= do
TcRef Messages
errs_var <- TcRn (TcRef Messages)
getErrsVar
TcLclEnv -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall lcl' gbl a lcl.
lcl' -> TcRnIf gbl lcl' a -> TcRnIf gbl lcl a
setLclEnv TcLclEnv
lcl_env (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ TcRef Messages -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. TcRef Messages -> TcRn a -> TcRn a
setErrsVar TcRef Messages
errs_var (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ do {
Type
zonked_ty <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcType Type
res_ty
; LHsExpr GhcTc
zonked_q_expr <- LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
zonkTopLExpr LHsExpr GhcTc
q_expr
; TcRef [ForeignRef (Q ())]
modfinalizers_ref <- [ForeignRef (Q ())]
-> TcRnIf TcGblEnv TcLclEnv (TcRef [ForeignRef (Q ())])
forall a gbl lcl. a -> TcRnIf gbl lcl (TcRef a)
newTcRef []
; LHsExpr GhcPs
expr2 <- ThStage -> TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs)
forall a. ThStage -> TcM a -> TcM a
setStage (TcRef [ForeignRef (Q ())] -> ThStage
RunSplice TcRef [ForeignRef (Q ())]
modfinalizers_ref) (TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs))
-> TcM (LHsExpr GhcPs) -> TcM (LHsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
runMetaE LHsExpr GhcTc
zonked_q_expr
; [ForeignRef (Q ())]
mod_finalizers <- TcRef [ForeignRef (Q ())]
-> TcRnIf TcGblEnv TcLclEnv [ForeignRef (Q ())]
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef [ForeignRef (Q ())]
modfinalizers_ref
; ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv (ThModFinalizers -> TcRn ()) -> ThModFinalizers -> TcRn ()
forall a b. (a -> b) -> a -> b
$ [ForeignRef (Q ())] -> ThModFinalizers
ThModFinalizers [ForeignRef (Q ())]
mod_finalizers
; SpliceInfo -> TcRn ()
traceSplice (SpliceInfo :: String -> Maybe (LHsExpr GhcRn) -> Bool -> MsgDoc -> SpliceInfo
SpliceInfo { spliceDescription :: String
spliceDescription = String
"expression"
, spliceIsDecl :: Bool
spliceIsDecl = Bool
False
, spliceSource :: Maybe (LHsExpr GhcRn)
spliceSource = LHsExpr GhcRn -> Maybe (LHsExpr GhcRn)
forall a. a -> Maybe a
Just LHsExpr GhcRn
orig_expr
, spliceGenerated :: MsgDoc
spliceGenerated = LHsExpr GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcPs
expr2 })
; (LHsExpr GhcTc
res, WantedConstraints
wcs) <-
TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc, WantedConstraints))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
MsgDoc -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (LHsExpr GhcTc -> MsgDoc
spliceResultDoc LHsExpr GhcTc
zonked_q_expr) (TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ do
{ (LHsExpr GhcRn
exp3, FreeVars
_fvs) <- LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
expr2
; LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
exp3 Type
zonked_ty }
; Bag EvBind
ev <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wcs
; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc -> TcM (HsExpr GhcTc))
-> HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcTc -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc (TcEvBinds -> LHsExpr GhcTc -> LHsExpr GhcTc
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
ev) LHsExpr GhcTc
res)
}
spliceCtxtDoc :: HsSplice GhcRn -> SDoc
spliceCtxtDoc :: HsSplice GhcRn -> MsgDoc
spliceCtxtDoc HsSplice GhcRn
splice
= MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text String
"In the Template Haskell splice")
Arity
2 (HsSplice GhcRn -> MsgDoc
forall (p :: Pass).
OutputableBndrId p =>
HsSplice (GhcPass p) -> MsgDoc
pprSplice HsSplice GhcRn
splice)
spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc :: LHsExpr GhcTc -> MsgDoc
spliceResultDoc LHsExpr GhcTc
expr
= [MsgDoc] -> MsgDoc
sep [ String -> MsgDoc
text String
"In the result of the splice:"
, Arity -> MsgDoc -> MsgDoc
nest Arity
2 (Char -> MsgDoc
char Char
'$' MsgDoc -> MsgDoc -> MsgDoc
<> LHsExpr GhcTc -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTc
expr)
, String -> MsgDoc
text String
"To see what the splice expanded to, use -ddump-splices"]
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
$
GeneralFlag -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall gbl lcl a.
GeneralFlag -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
unsetGOptM GeneralFlag
Opt_DeferTypeErrors (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 {
(Maybe (LHsExpr GhcTc)
mb_expr', WantedConstraints
wanted) <- TcM (LHsExpr GhcTc)
-> TcM (Maybe (LHsExpr GhcTc), WantedConstraints)
forall a. TcM a -> TcM (Maybe a, WantedConstraints)
tryCaptureConstraints TcM (LHsExpr GhcTc)
tc_action
; Bag EvBind
const_binds <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wanted
; case Maybe (LHsExpr GhcTc)
mb_expr' of
Maybe (LHsExpr GhcTc)
Nothing -> TcM (LHsExpr GhcTc)
forall env a. IOEnv env a
failM
Just LHsExpr GhcTc
expr' -> LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
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
expr' }
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
runAnnotation CoreAnnTarget
target LHsExpr GhcRn
expr = do
SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
Class
data_class <- Name -> TcM Class
tcLookupClass Name
dataClassName
Id
to_annotation_wrapper_id <- Name -> IOEnv (Env TcGblEnv TcLclEnv) Id
tcLookupId Name
toAnnotationWrapperName
LHsExpr GhcTc
zonked_wrapped_expr' <- LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
zonkTopLExpr (LHsExpr GhcTc -> TcM (LHsExpr GhcTc))
-> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr SpliceType
Untyped (
do { (LHsExpr GhcTc
expr', Type
expr_ty) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRhoNC LHsExpr GhcRn
expr
; HsWrapper
wrapper <- CtOrigin -> [Type] -> [Type] -> TcM HsWrapper
instCall CtOrigin
AnnOrigin [Type
expr_ty] [Class -> [Type] -> Type
mkClassPred Class
data_class [Type
expr_ty]]
; let specialised_to_annotation_wrapper_expr :: LHsExpr GhcTc
specialised_to_annotation_wrapper_expr
= SrcSpan -> HsExpr GhcTc -> LHsExpr GhcTc
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrapper
(XVar GhcTc -> Located (IdP GhcTc) -> HsExpr GhcTc
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar NoExtField
XVar GhcTc
noExtField (SrcSpan -> Id -> GenLocated SrcSpan Id
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc Id
to_annotation_wrapper_id)))
; LHsExpr GhcTc -> TcM (LHsExpr GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> HsExpr GhcTc -> LHsExpr GhcTc
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XApp GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp NoExtField
XApp GhcTc
noExtField
LHsExpr GhcTc
specialised_to_annotation_wrapper_expr LHsExpr GhcTc
expr'))
})
Serialized
serialized <- LHsExpr GhcTc -> TcM Serialized
runMetaAW LHsExpr GhcTc
zonked_wrapped_expr'
Annotation -> TcM Annotation
forall (m :: * -> *) a. Monad m => a -> m a
return Annotation :: CoreAnnTarget -> Serialized -> Annotation
Annotation {
ann_target :: CoreAnnTarget
ann_target = CoreAnnTarget
target,
ann_value :: Serialized
ann_value = Serialized
serialized
}
convertAnnotationWrapper :: ForeignHValue -> TcM (Either MsgDoc Serialized)
convertAnnotationWrapper :: ForeignHValue -> TcM (Either MsgDoc Serialized)
convertAnnotationWrapper ForeignHValue
fhv = do
Interp
interp <- TcM Interp
tcGetInterp
case Interp
interp of
ExternalInterp {} -> Serialized -> Either MsgDoc Serialized
forall a b. b -> Either a b
Right (Serialized -> Either MsgDoc Serialized)
-> TcM Serialized -> TcM (Either MsgDoc Serialized)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> THResultType -> ForeignHValue -> TcM Serialized
forall a. Binary a => THResultType -> ForeignHValue -> TcM a
runTH THResultType
THAnnWrapper ForeignHValue
fhv
#if defined(HAVE_INTERNAL_INTERPRETER)
Interp
InternalInterp -> do
HValue
annotation_wrapper <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
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
InternalInterp ForeignHValue
fhv
Either MsgDoc Serialized -> TcM (Either MsgDoc Serialized)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either MsgDoc Serialized -> TcM (Either MsgDoc Serialized))
-> Either MsgDoc Serialized -> TcM (Either MsgDoc Serialized)
forall a b. (a -> b) -> a -> b
$ Serialized -> Either MsgDoc Serialized
forall a b. b -> Either a b
Right (Serialized -> Either MsgDoc Serialized)
-> Serialized -> Either MsgDoc Serialized
forall a b. (a -> b) -> a -> b
$
case HValue -> AnnotationWrapper
forall a b. a -> b
unsafeCoerce HValue
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
`seq` Serialized
serialized
seqSerialized :: Serialized -> ()
seqSerialized :: Serialized -> ()
seqSerialized (Serialized TypeRep
the_type [Word8]
bytes) = TypeRep
the_type TypeRep -> () -> ()
`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
interp <- TcM Interp
tcGetInterp
case Interp
interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
Interp
InternalInterp -> do
[Q ()]
qs <- IO [Q ()] -> IOEnv (Env TcGblEnv TcLclEnv) [Q ()]
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)
mapM RemoteRef (Q ()) -> IO (Q ())
forall a. RemoteRef a -> IO a
localRef)
Q () -> TcRn ()
forall a. Q a -> TcM a
runQuasi (Q () -> TcRn ()) -> Q () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ [Q ()] -> Q ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [Q ()]
qs
#endif
ExternalInterp IServConfig
conf IServ
iserv -> IServConfig -> IServ -> (IServInstance -> TcRn ()) -> TcRn ()
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
IServConfig -> IServ -> (IServInstance -> m a) -> m a
withIServ_ IServConfig
conf IServ
iserv ((IServInstance -> TcRn ()) -> TcRn ())
-> (IServInstance -> TcRn ()) -> TcRn ()
forall a b. (a -> b) -> a -> b
$ \IServInstance
i -> do
TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
Maybe (ForeignRef (IORef QState))
th_state <- TcRef (Maybe (ForeignRef (IORef QState)))
-> TcRnIf TcGblEnv TcLclEnv (Maybe (ForeignRef (IORef QState)))
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg)
case Maybe (ForeignRef (IORef QState))
th_state of
Maybe (ForeignRef (IORef QState))
Nothing -> () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just ForeignRef (IORef QState)
fhv -> do
IO () -> TcRn ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ ForeignRef (IORef QState)
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef (IORef QState)
fhv ((RemoteRef (IORef QState) -> IO ()) -> IO ())
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef (IORef QState)
st ->
[ForeignRef (Q ())] -> ([RemoteRef (Q ())] -> IO ()) -> IO ()
forall {a} {b}. [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [ForeignRef (Q ())]
finRefs (([RemoteRef (Q ())] -> IO ()) -> IO ())
-> ([RemoteRef (Q ())] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[RemoteRef (Q ())]
qrefs ->
IServInstance -> Put -> IO ()
writeIServ IServInstance
i (Message (QResult ()) -> Put
forall a. Message a -> Put
putMessage (RemoteRef (IORef QState)
-> [RemoteRef (Q ())] -> Message (QResult ())
RunModFinalizers RemoteRef (IORef QState)
st [RemoteRef (Q ())]
qrefs))
() <- IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
i []
IServInstance -> TcRn ()
forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i
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 { a
th_result <- ForeignHValue -> TcM a
runQ ForeignHValue
hval
; Origin
th_origin <- TcM Origin
getThSpliceOrigin
; String -> MsgDoc -> TcRn ()
traceTc String
"Got TH result:" (String -> MsgDoc
text (a -> String
show_th a
th_result))
; b -> TcM b
forall (m :: * -> *) a. Monad m => a -> m a
return (Origin -> SrcSpan -> a -> b
f Origin
th_origin SrcSpan
expr_span a
th_result) }
runMeta :: (MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta :: forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn
unwrap LHsExpr GhcTc
e
= do { MetaHook TcM
h <- (Hooks -> Maybe (MetaHook TcM))
-> MetaHook TcM -> IOEnv (Env TcGblEnv TcLclEnv) (MetaHook TcM)
forall (f :: * -> *) a.
(Functor f, HasDynFlags f) =>
(Hooks -> Maybe a) -> a -> f a
getHooked Hooks -> Maybe (MetaHook TcM)
runMetaHook MetaHook TcM
defaultRunMeta
; MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn
unwrap MetaHook TcM
h LHsExpr GhcTc
e }
defaultRunMeta :: MetaHook TcM
defaultRunMeta :: MetaHook TcM
defaultRunMeta (MetaE LHsExpr GhcPs -> MetaResult
r)
= (LHsExpr GhcPs -> MetaResult)
-> TcM (LHsExpr GhcPs) -> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsExpr GhcPs -> MetaResult
r (TcM (LHsExpr GhcPs) -> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTc -> TcM (LHsExpr GhcPs))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (LHsExpr GhcPs -> MsgDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either MsgDoc (LHsExpr GhcPs)))
-> LHsExpr GhcTc
-> TcM (LHsExpr GhcPs)
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True LHsExpr GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Exp -> String)
-> (Origin -> SrcSpan -> Exp -> Either MsgDoc (LHsExpr GhcPs))
-> (ForeignHValue -> TcM Exp)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (LHsExpr 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 MsgDoc (LHsExpr GhcPs)
convertToHsExpr ForeignHValue -> TcM Exp
runTHExp)
defaultRunMeta (MetaP LPat GhcPs -> MetaResult
r)
= (Located (Pat GhcPs) -> MetaResult)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs))
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Located (Pat GhcPs) -> MetaResult
LPat GhcPs -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs))
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs)))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (Located (Pat GhcPs) -> MsgDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either MsgDoc (Located (Pat GhcPs))))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs))
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True Located (Pat GhcPs) -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Pat -> String)
-> (Origin
-> SrcSpan -> Pat -> Either MsgDoc (Located (Pat GhcPs)))
-> (ForeignHValue -> TcM Pat)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (Located (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 MsgDoc (Located (Pat GhcPs))
Origin -> SrcSpan -> Pat -> Either MsgDoc (LPat GhcPs)
convertToPat ForeignHValue -> TcM Pat
runTHPat)
defaultRunMeta (MetaT LHsType GhcPs -> MetaResult
r)
= (LHsType GhcPs -> MetaResult)
-> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsType GhcPs -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (LHsType GhcPs -> MsgDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either MsgDoc (LHsType GhcPs)))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True LHsType GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Type -> String)
-> (Origin -> SrcSpan -> Type -> Either MsgDoc (LHsType GhcPs))
-> (ForeignHValue -> TcM Type)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (LHsType 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 MsgDoc (LHsType GhcPs)
convertToHsType ForeignHValue -> TcM Type
runTHType)
defaultRunMeta (MetaD [LHsDecl GhcPs] -> MetaResult
r)
= ([LHsDecl GhcPs] -> MetaResult)
-> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [LHsDecl GhcPs] -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs])
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ([LHsDecl GhcPs] -> MsgDoc)
-> (SrcSpan
-> ForeignHValue -> TcM (Either MsgDoc [LHsDecl GhcPs]))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
True [LHsDecl GhcPs] -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr (([Dec] -> String)
-> (Origin -> SrcSpan -> [Dec] -> Either MsgDoc [LHsDecl GhcPs])
-> (ForeignHValue -> TcM [Dec])
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc [LHsDecl 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 MsgDoc [LHsDecl GhcPs]
convertToHsDecls ForeignHValue -> TcM [Dec]
runTHDec)
defaultRunMeta (MetaAW Serialized -> MetaResult
r)
= (Serialized -> MetaResult)
-> TcM Serialized -> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Serialized -> MetaResult
r (TcM Serialized -> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTc -> TcM Serialized)
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (Serialized -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc Serialized))
-> LHsExpr GhcTc
-> TcM Serialized
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
False (MsgDoc -> Serialized -> MsgDoc
forall a b. a -> b -> a
const MsgDoc
empty) ((ForeignHValue -> TcM (Either MsgDoc Serialized))
-> SrcSpan -> ForeignHValue -> TcM (Either MsgDoc Serialized)
forall a b. a -> b -> a
const ForeignHValue -> TcM (Either MsgDoc Serialized)
convertAnnotationWrapper)
runMetaAW :: LHsExpr GhcTc
-> TcM Serialized
runMetaAW :: LHsExpr GhcTc -> TcM Serialized
runMetaAW = (MetaHook TcM -> LHsExpr GhcTc -> TcM Serialized)
-> LHsExpr GhcTc -> TcM Serialized
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM -> 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 TcM -> LHsExpr GhcTc -> TcM (LHsExpr GhcPs))
-> LHsExpr GhcTc -> TcM (LHsExpr GhcPs)
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM -> LHsExpr GhcTc -> TcM (LHsExpr 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 TcM
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs)))
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs))
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTc
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcPs))
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LPat GhcPs)
metaRequestP
runMetaT :: LHsExpr GhcTc
-> TcM (LHsType GhcPs)
runMetaT :: LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
runMetaT = (MetaHook TcM
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs))
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f (LHsType GhcPs)
metaRequestT
runMetaD :: LHsExpr GhcTc
-> TcM [LHsDecl GhcPs]
runMetaD :: LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
runMetaD = (MetaHook TcM
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs])
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTc -> f [LHsDecl GhcPs]
metaRequestD
runMeta' :: Bool
-> (hs_syn -> SDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' :: forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' Bool
show_code hs_syn -> MsgDoc
ppr_hs SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn)
run_and_convert LHsExpr GhcTc
expr
= do { String -> MsgDoc -> TcRn ()
traceTc String
"About to run" (LHsExpr GhcTc -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTc
expr)
; TcRn ()
recordThSpliceUse
; TcRn ()
failIfErrsM
; HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
; LHsExpr GhcTc
expr' <- DynFlags
-> PluginOperation TcM (LHsExpr GhcTc)
-> LHsExpr GhcTc
-> TcM (LHsExpr GhcTc)
forall (m :: * -> *) a.
Monad m =>
DynFlags -> PluginOperation m a -> a -> m a
withPlugins (HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env) PluginOperation TcM (LHsExpr GhcTc)
spliceRunAction LHsExpr GhcTc
expr
; CoreExpr
ds_expr <- DsM CoreExpr -> TcM CoreExpr
forall a. DsM a -> TcM a
initDsTc (LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr')
; SrcSpan
src_span <- TcRn SrcSpan
getSrcSpanM
; String -> MsgDoc -> TcRn ()
traceTc String
"About to run (desugared)" (CoreExpr -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr CoreExpr
ds_expr)
; Either IOEnvFailure ForeignHValue
either_hval <- IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue
-> IOEnv
(Env TcGblEnv TcLclEnv) (Either IOEnvFailure ForeignHValue)
forall env r. IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM (IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue
-> IOEnv
(Env TcGblEnv TcLclEnv) (Either IOEnvFailure ForeignHValue))
-> IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue
-> IOEnv
(Env TcGblEnv TcLclEnv) (Either IOEnvFailure ForeignHValue)
forall a b. (a -> b) -> a -> b
$ IO ForeignHValue -> IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ForeignHValue -> IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue)
-> IO ForeignHValue -> IOEnv (Env TcGblEnv TcLclEnv) ForeignHValue
forall a b. (a -> b) -> a -> b
$
HscEnv -> SrcSpan -> CoreExpr -> IO ForeignHValue
GHC.Driver.Main.hscCompileCoreExpr HscEnv
hsc_env SrcSpan
src_span CoreExpr
ds_expr
; case Either IOEnvFailure ForeignHValue
either_hval of {
Left IOEnvFailure
exn -> String -> IOEnvFailure -> TcM hs_syn
forall e a. Exception e => String -> e -> TcM a
fail_with_exn String
"compile and link" IOEnvFailure
exn ;
Right ForeignHValue
hval -> do
{
let expr_span :: SrcSpan
expr_span = LHsExpr GhcTc -> SrcSpan
forall l e. GenLocated l e -> l
getLoc LHsExpr GhcTc
expr
; Either SomeException hs_syn
either_tval <- TcM hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall env r. IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (TcM hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn))
-> TcM hs_syn
-> IOEnv (Env TcGblEnv TcLclEnv) (Either SomeException hs_syn)
forall a b. (a -> b) -> a -> b
$
SrcSpan -> TcM hs_syn -> TcM hs_syn
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
expr_span (TcM hs_syn -> TcM hs_syn) -> TcM hs_syn -> TcM hs_syn
forall a b. (a -> b) -> a -> b
$
do { Either MsgDoc hs_syn
mb_result <- SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn)
run_and_convert SrcSpan
expr_span ForeignHValue
hval
; case Either MsgDoc hs_syn
mb_result of
Left MsgDoc
err -> MsgDoc -> TcM hs_syn
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
err
Right hs_syn
result -> do { String -> MsgDoc -> TcRn ()
traceTc String
"Got HsSyn result:" (hs_syn -> MsgDoc
ppr_hs hs_syn
result)
; hs_syn -> TcM hs_syn
forall (m :: * -> *) a. Monad m => a -> m a
return (hs_syn -> TcM hs_syn) -> hs_syn -> TcM hs_syn
forall a b. (a -> b) -> a -> b
$! hs_syn
result } }
; case Either SomeException hs_syn
either_tval of
Right hs_syn
v -> hs_syn -> TcM hs_syn
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 -> TcM hs_syn
forall env a. IOEnv env a
failM
Maybe IOEnvFailure
_ -> String -> SomeException -> TcM hs_syn
forall e a. Exception e => String -> e -> TcM a
fail_with_exn String
"run" SomeException
se
}}}
where
fail_with_exn :: Exception e => String -> e -> TcM a
fail_with_exn :: forall e a. Exception e => String -> e -> TcM a
fail_with_exn String
phase e
exn = do
String
exn_msg <- IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
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
let msg :: MsgDoc
msg = [MsgDoc] -> MsgDoc
vcat [String -> MsgDoc
text String
"Exception when trying to" MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text String
phase MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text String
"compile-time code:",
Arity -> MsgDoc -> MsgDoc
nest Arity
2 (String -> MsgDoc
text String
exn_msg),
if Bool
show_code then String -> MsgDoc
text String
"Code:" MsgDoc -> MsgDoc -> MsgDoc
<+> LHsExpr GhcTc -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTc
expr else MsgDoc
empty]
MsgDoc -> TcM a
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
instance TH.Quasi TcM where
qNewName :: String -> TcM Name
qNewName String
s = do { Unique
u <- TcRnIf TcGblEnv TcLclEnv Unique
forall gbl lcl. TcRnIf gbl lcl Unique
newUnique
; let i :: Integer
i = Arity -> Integer
forall a. Integral a => a -> Integer
toInteger (Unique -> Arity
getKey Unique
u)
; Name -> TcM Name
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Integer -> Name
TH.mkNameU String
s Integer
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
$ MsgDoc -> TcRn ()
addErr (String -> MsgDoc
text 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
$ WarnReason -> MsgDoc -> TcRn ()
addWarn WarnReason
NoReason (String -> MsgDoc
text String
msg)
qLocation :: TcM Loc
qLocation = do { GenModule Unit
m <- IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule
; SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
; RealSrcSpan
r <- case SrcSpan
l of
UnhelpfulSpan UnhelpfulSpanReason
_ -> String -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"qLocation: Unhelpful location"
(SrcSpan -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr SrcSpan
l)
RealSrcSpan RealSrcSpan
s Maybe BufSpan
_ -> RealSrcSpan -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall (m :: * -> *) a. Monad m => a -> m a
return RealSrcSpan
s
; Loc -> TcM Loc
forall (m :: * -> *) a. Monad m => a -> m a
return (Loc :: String -> String -> String -> CharPos -> CharPos -> Loc
TH.Loc { loc_filename :: String
TH.loc_filename = FastString -> String
unpackFS (RealSrcSpan -> FastString
srcSpanFile RealSrcSpan
r)
, loc_module :: String
TH.loc_module = ModuleName -> String
moduleNameString (GenModule Unit -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName GenModule Unit
m)
, loc_package :: String
TH.loc_package = Unit -> String
unitString (GenModule Unit -> Unit
forall unit. GenModule unit -> unit
moduleUnit GenModule Unit
m)
, loc_start :: CharPos
TH.loc_start = (RealSrcSpan -> Arity
srcSpanStartLine RealSrcSpan
r, RealSrcSpan -> Arity
srcSpanStartCol RealSrcSpan
r)
, loc_end :: CharPos
TH.loc_end = (RealSrcSpan -> Arity
srcSpanEndLine RealSrcSpan
r, RealSrcSpan -> Arity
srcSpanEndCol RealSrcSpan
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 (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 { Name
nm' <- Name -> TcM Name
lookupThName Name
nm
; DataCon
dc <- Name -> TcM DataCon
tcLookupDataCon Name
nm'
; let bangs :: [HsImplBang]
bangs = DataCon -> [HsImplBang]
dataConImplBangs DataCon
dc
; [DecidedStrictness] -> TcM [DecidedStrictness]
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsImplBang -> DecidedStrictness)
-> [HsImplBang] -> [DecidedStrictness]
forall a b. (a -> b) -> [a] -> [b]
map HsImplBang -> DecidedStrictness
reifyDecidedStrictness [HsImplBang]
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
qAddDependentFile :: String -> TcRn ()
qAddDependentFile String
fp = do
TcRef [String]
ref <- (TcGblEnv -> TcRef [String])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [String])
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
[String]
dep_files <- TcRef [String] -> TcRnIf TcGblEnv TcLclEnv [String]
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef [String]
ref
TcRef [String] -> [String] -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef [String]
ref (String
fpString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
dep_files)
qAddTempFile :: String -> IOEnv (Env TcGblEnv TcLclEnv) String
qAddTempFile String
suffix = do
DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
IO String -> IOEnv (Env TcGblEnv TcLclEnv) String
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
$ DynFlags -> TempFileLifetime -> String -> IO String
newTempName DynFlags
dflags TempFileLifetime
TFL_GhcSession String
suffix
qAddTopDecls :: [Dec] -> TcRn ()
qAddTopDecls [Dec]
thds = do
SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
Origin
th_origin <- TcM Origin
getThSpliceOrigin
let either_hval :: Either MsgDoc [LHsDecl GhcPs]
either_hval = Origin -> SrcSpan -> [Dec] -> Either MsgDoc [LHsDecl GhcPs]
convertToHsDecls Origin
th_origin SrcSpan
l [Dec]
thds
[LHsDecl GhcPs]
ds <- case Either MsgDoc [LHsDecl GhcPs]
either_hval of
Left MsgDoc
exn -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall a. MsgDoc -> TcM a
failWithTc (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs])
-> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall a b. (a -> b) -> a -> b
$
MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text String
"Error in a declaration passed to addTopDecls:")
Arity
2 MsgDoc
exn
Right [LHsDecl GhcPs]
ds -> [LHsDecl GhcPs] -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall (m :: * -> *) a. Monad m => a -> m a
return [LHsDecl GhcPs]
ds
(LHsDecl GhcPs -> TcRn ()) -> [LHsDecl GhcPs] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (HsDecl GhcPs -> TcRn ()
checkTopDecl (HsDecl GhcPs -> TcRn ())
-> (LHsDecl GhcPs -> HsDecl GhcPs) -> LHsDecl GhcPs -> TcRn ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsDecl GhcPs -> HsDecl GhcPs
forall l e. GenLocated l e -> e
unLoc) [LHsDecl GhcPs]
ds
TcRef [LHsDecl GhcPs]
th_topdecls_var <- (TcGblEnv -> TcRef [LHsDecl GhcPs])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [LHsDecl GhcPs])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> TcRef [LHsDecl GhcPs]
tcg_th_topdecls TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
TcRef [LHsDecl GhcPs]
-> ([LHsDecl GhcPs] -> [LHsDecl GhcPs]) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [LHsDecl GhcPs]
th_topdecls_var (\[LHsDecl GhcPs]
topds -> [LHsDecl GhcPs]
ds [LHsDecl GhcPs] -> [LHsDecl GhcPs] -> [LHsDecl GhcPs]
forall a. [a] -> [a] -> [a]
++ [LHsDecl 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 (HsBind GhcPs -> [IdP GhcPs]
forall p idR. CollectPass p => HsBindLR p idR -> [IdP p]
collectHsBindBinders HsBind GhcPs
binds)
checkTopDecl (SigD XSigD GhcPs
_ Sig GhcPs
_)
= () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (AnnD XAnnD GhcPs
_ AnnDecl GhcPs
_)
= () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (ForD XForD GhcPs
_ (ForeignImport { fd_name :: forall pass. ForeignDecl pass -> Located (IdP pass)
fd_name = L SrcSpan
_ IdP GhcPs
name }))
= RdrName -> TcRn ()
bindName RdrName
IdP GhcPs
name
checkTopDecl HsDecl GhcPs
_
= MsgDoc -> TcRn ()
addErr (MsgDoc -> TcRn ()) -> MsgDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$ String -> MsgDoc
text String
"Only function, value, annotation, and foreign import declarations may be added with addTopDecl"
bindName :: RdrName -> TcM ()
bindName :: RdrName -> TcRn ()
bindName (Exact Name
n)
= do { TcRef FreeVars
th_topnames_var <- (TcGblEnv -> TcRef FreeVars)
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef FreeVars)
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
; TcRef FreeVars -> (FreeVars -> FreeVars) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef FreeVars
th_topnames_var (\FreeVars
ns -> FreeVars -> Name -> FreeVars
extendNameSet FreeVars
ns Name
n)
}
bindName RdrName
name =
MsgDoc -> TcRn ()
addErr (MsgDoc -> TcRn ()) -> MsgDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text String
"The binder" MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
quotes (RdrName -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr RdrName
name) MsgDoc -> MsgDoc -> MsgDoc
<+> PtrString -> MsgDoc
ptext (String -> PtrString
sLit String
"is not a NameU."))
Arity
2 (String -> MsgDoc
text String
"Probable cause: you used mkName instead of newName to generate a binding.")
qAddForeignFilePath :: ForeignSrcLang -> String -> TcRn ()
qAddForeignFilePath ForeignSrcLang
lang String
fp = do
TcRef [(ForeignSrcLang, String)]
var <- (TcGblEnv -> TcRef [(ForeignSrcLang, String)])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [(ForeignSrcLang, String)])
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
TcRef [(ForeignSrcLang, String)]
-> ([(ForeignSrcLang, String)] -> [(ForeignSrcLang, String)])
-> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [(ForeignSrcLang, String)]
var ((ForeignSrcLang
lang, String
fp) (ForeignSrcLang, String)
-> [(ForeignSrcLang, String)] -> [(ForeignSrcLang, String)]
forall a. a -> [a] -> [a]
:)
qAddModFinalizer :: Q () -> TcRn ()
qAddModFinalizer Q ()
fin = do
RemoteRef (Q ())
r <- IO (RemoteRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (RemoteRef (Q ()))
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
ForeignRef (Q ())
fref <- IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ())))
-> IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall a b. (a -> b) -> a -> b
$ RemoteRef (Q ()) -> IO () -> IO (ForeignRef (Q ()))
forall a. RemoteRef a -> IO () -> IO (ForeignRef a)
mkForeignRef RemoteRef (Q ())
r (RemoteRef (Q ()) -> IO ()
forall a. RemoteRef a -> IO ()
freeRemoteRef RemoteRef (Q ())
r)
ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef ForeignRef (Q ())
fref
qAddCorePlugin :: String -> TcRn ()
qAddCorePlugin String
plugin = do
HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
FindResult
r <- IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult)
-> IO FindResult -> IOEnv (Env TcGblEnv TcLclEnv) FindResult
forall a b. (a -> b) -> a -> b
$ HscEnv -> ModuleName -> IO FindResult
findHomeModule HscEnv
hsc_env (String -> ModuleName
mkModuleName String
plugin)
let err :: MsgDoc
err = MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang
(String -> MsgDoc
text String
"addCorePlugin: invalid plugin module "
MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text (String -> String
forall a. Show a => a -> String
show String
plugin)
)
Arity
2
(String -> MsgDoc
text String
"Plugins in the current package can't be specified.")
case FindResult
r of
Found {} -> MsgDoc -> TcRn ()
addErr MsgDoc
err
FoundMultiple {} -> MsgDoc -> TcRn ()
addErr MsgDoc
err
FindResult
_ -> () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
TcRef [String]
th_coreplugins_var <- TcGblEnv -> TcRef [String]
tcg_th_coreplugins (TcGblEnv -> TcRef [String])
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
TcRef [String] -> ([String] -> [String]) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [String]
th_coreplugins_var (String
pluginString -> [String] -> [String]
forall a. a -> [a] -> [a]
:)
qGetQ :: forall a. Typeable a => TcM (Maybe a)
qGetQ :: forall a. Typeable a => TcM (Maybe a)
qGetQ = do
TcRef (Map TypeRep Dynamic)
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
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
Map TypeRep Dynamic
th_state <- TcRef (Map TypeRep Dynamic)
-> TcRnIf TcGblEnv TcLclEnv (Map TypeRep Dynamic)
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef (Map TypeRep Dynamic)
th_state_var
Maybe a -> TcM (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeRep -> Map TypeRep Dynamic -> Maybe Dynamic
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)) Map TypeRep Dynamic
th_state Maybe Dynamic -> (Dynamic -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Dynamic -> Maybe a
forall a. Typeable a => Dynamic -> Maybe a
fromDynamic)
qPutQ :: forall a. Typeable a => a -> TcRn ()
qPutQ a
x = do
TcRef (Map TypeRep Dynamic)
th_state_var <- (TcGblEnv -> TcRef (Map TypeRep Dynamic))
-> TcRnIf TcGblEnv TcLclEnv TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRef (Map TypeRep Dynamic))
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
TcRef (Map TypeRep Dynamic)
-> (Map TypeRep Dynamic -> Map TypeRep Dynamic) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef (Map TypeRep Dynamic)
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 -> TcRnIf TcGblEnv TcLclEnv Bool
qIsExtEnabled = Extension -> TcRnIf TcGblEnv TcLclEnv 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
addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM ()
addModFinalizerRef :: ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef ForeignRef (Q ())
finRef = do
ThStage
th_stage <- TcM ThStage
getStage
case ThStage
th_stage of
RunSplice TcRef [ForeignRef (Q ())]
th_modfinalizers_var -> TcRef [ForeignRef (Q ())]
-> ([ForeignRef (Q ())] -> [ForeignRef (Q ())]) -> TcRn ()
forall a gbl lcl. TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
updTcRef TcRef [ForeignRef (Q ())]
th_modfinalizers_var (ForeignRef (Q ())
finRef ForeignRef (Q ()) -> [ForeignRef (Q ())] -> [ForeignRef (Q ())]
forall a. a -> [a] -> [a]
:)
ThStage
_ ->
String -> MsgDoc -> TcRn ()
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"addModFinalizer was called when no finalizers were collected"
(ThStage -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ThStage
th_stage)
finishTH :: TcM ()
finishTH :: TcRn ()
finishTH = do
HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
case HscEnv -> Maybe Interp
hsc_interp HscEnv
hsc_env of
Maybe Interp
Nothing -> () -> TcRn ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#if defined(HAVE_INTERNAL_INTERPRETER)
Just Interp
InternalInterp -> () -> TcRn ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
#endif
Just (ExternalInterp {}) -> do
TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
TcRef (Maybe (ForeignRef (IORef QState)))
-> Maybe (ForeignRef (IORef QState)) -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg) Maybe (ForeignRef (IORef QState))
forall a. Maybe a
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
interp <- TcM Interp
tcGetInterp
case Interp
interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
Interp
InternalInterp -> do
HValue
hv <- IO HValue -> IOEnv (Env TcGblEnv TcLclEnv) HValue
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
InternalInterp ForeignHValue
fhv
a
r <- Q a -> TcM a
forall a. Q a -> TcM a
runQuasi (HValue -> Q a
forall a b. a -> b
unsafeCoerce HValue
hv :: TH.Q a)
a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
#endif
ExternalInterp IServConfig
conf IServ
iserv ->
IServConfig -> IServ -> (IServInstance -> TcM a) -> TcM a
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
IServConfig -> IServ -> (IServInstance -> m a) -> m a
withIServ_ IServConfig
conf IServ
iserv ((IServInstance -> TcM a) -> TcM a)
-> (IServInstance -> TcM a) -> TcM a
forall a b. (a -> b) -> a -> b
$ \IServInstance
i -> do
ForeignRef (IORef QState)
rstate <- IServInstance -> TcM (ForeignRef (IORef QState))
getTHState IServInstance
i
Loc
loc <- TcM Loc
forall (m :: * -> *). Quasi m => m Loc
TH.qLocation
IO () -> TcRn ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
ForeignRef (IORef QState)
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignRef (IORef QState)
rstate ((RemoteRef (IORef QState) -> IO ()) -> IO ())
-> (RemoteRef (IORef QState) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef (IORef QState)
state_hv ->
ForeignHValue -> (RemoteRef HValue -> IO ()) -> IO ()
forall a b. ForeignRef a -> (RemoteRef a -> IO b) -> IO b
withForeignRef ForeignHValue
fhv ((RemoteRef HValue -> IO ()) -> IO ())
-> (RemoteRef HValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRef HValue
q_hv ->
IServInstance -> Put -> IO ()
writeIServ IServInstance
i (Message (QResult ByteString) -> Put
forall a. Message a -> Put
putMessage (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)))
IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
i []
ByteString
bs <- IServInstance -> TcM ByteString
forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i
a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> TcM a) -> a -> TcM a
forall a b. (a -> b) -> a -> b
$! Get a -> ByteString -> a
forall a. Get a -> ByteString -> a
runGet Get a
forall t. Binary t => Get t
get (ByteString -> ByteString
LB.fromStrict ByteString
bs)
runRemoteTH
:: IServInstance
-> [Messages]
-> TcM ()
runRemoteTH :: IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages]
recovers = do
THMsg THMessage a
msg <- IO THMsg -> IOEnv (Env TcGblEnv TcLclEnv) THMsg
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
$ IServInstance -> Get THMsg -> IO THMsg
forall a. IServInstance -> Get a -> IO a
readIServ IServInstance
iserv Get THMsg
getTHMessage
case THMessage a
msg of
THMessage a
RunTHDone -> () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
THMessage a
StartRecover -> do
TcRef Messages
v <- TcRn (TcRef Messages)
getErrsVar
Messages
msgs <- TcRef Messages -> TcRnIf TcGblEnv TcLclEnv Messages
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef Messages
v
TcRef Messages -> Messages -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef Messages
v Messages
emptyMessages
IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
iserv (Messages
msgs Messages -> [Messages] -> [Messages]
forall a. a -> [a] -> [a]
: [Messages]
recovers)
EndRecover Bool
caught_error -> do
let (prev_msgs :: Messages
prev_msgs@(WarningMessages
prev_warns,WarningMessages
prev_errs), [Messages]
rest) = case [Messages]
recovers of
[] -> String -> (Messages, [Messages])
forall a. String -> a
panic String
"EndRecover"
Messages
a : [Messages]
b -> (Messages
a,[Messages]
b)
TcRef Messages
v <- TcRn (TcRef Messages)
getErrsVar
(WarningMessages
warn_msgs,WarningMessages
_) <- TcRef Messages -> TcRnIf TcGblEnv TcLclEnv Messages
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef TcRef Messages
v
TcRef Messages -> Messages -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef TcRef Messages
v (Messages -> TcRn ()) -> Messages -> TcRn ()
forall a b. (a -> b) -> a -> b
$ if Bool
caught_error
then Messages
prev_msgs
else (WarningMessages
prev_warns WarningMessages -> WarningMessages -> WarningMessages
forall a. Bag a -> Bag a -> Bag a
`unionBags` WarningMessages
warn_msgs, WarningMessages
prev_errs)
IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages]
rest
THMessage a
_other -> do
a
r <- THMessage a -> TcM a
forall a. THMessage a -> TcM a
handleTHMessage THMessage a
msg
IO () -> TcRn ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> TcRn ()) -> IO () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ IServInstance -> Put -> IO ()
writeIServ IServInstance
iserv (a -> Put
forall t. Binary t => t -> Put
put a
r)
IServInstance -> [Messages] -> TcRn ()
runRemoteTH IServInstance
iserv [Messages]
recovers
readQResult :: Binary a => IServInstance -> TcM a
readQResult :: forall a. Binary a => IServInstance -> TcM a
readQResult IServInstance
i = do
QResult a
qr <- IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a))
-> IO (QResult a) -> IOEnv (Env TcGblEnv TcLclEnv) (QResult a)
forall a b. (a -> b) -> a -> b
$ IServInstance -> Get (QResult a) -> IO (QResult a)
forall a. IServInstance -> Get a -> IO a
readIServ IServInstance
i Get (QResult a)
forall t. Binary t => Get t
get
case QResult a
qr of
QDone a
a -> a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
QException String
str -> IO a -> TcM 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. Exception e => e -> IO a
throwIO (String -> ErrorCall
ErrorCall String
str)
QFail String
str -> String -> TcM a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
str
getTHState :: IServInstance -> TcM (ForeignRef (IORef QState))
getTHState :: IServInstance -> TcM (ForeignRef (IORef QState))
getTHState IServInstance
i = do
TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
Maybe (ForeignRef (IORef QState))
th_state <- TcRef (Maybe (ForeignRef (IORef QState)))
-> TcRnIf TcGblEnv TcLclEnv (Maybe (ForeignRef (IORef QState)))
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
readTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg)
case Maybe (ForeignRef (IORef QState))
th_state of
Just ForeignRef (IORef QState)
rhv -> ForeignRef (IORef QState) -> TcM (ForeignRef (IORef QState))
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
rhv
Maybe (ForeignRef (IORef QState))
Nothing -> do
HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
ForeignRef (IORef QState)
fhv <- IO (ForeignRef (IORef QState)) -> TcM (ForeignRef (IORef QState))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ForeignRef (IORef QState)) -> TcM (ForeignRef (IORef QState)))
-> IO (ForeignRef (IORef QState))
-> TcM (ForeignRef (IORef QState))
forall a b. (a -> b) -> a -> b
$ HscEnv
-> RemoteRef (IORef QState) -> IO (ForeignRef (IORef QState))
forall a. HscEnv -> RemoteRef a -> IO (ForeignRef a)
mkFinalizedHValue HscEnv
hsc_env (RemoteRef (IORef QState) -> IO (ForeignRef (IORef QState)))
-> IO (RemoteRef (IORef QState)) -> IO (ForeignRef (IORef QState))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IServInstance
-> Message (RemoteRef (IORef QState))
-> IO (RemoteRef (IORef QState))
forall a. Binary a => IServInstance -> Message a -> IO a
iservCall IServInstance
i Message (RemoteRef (IORef QState))
StartTH
TcRef (Maybe (ForeignRef (IORef QState)))
-> Maybe (ForeignRef (IORef QState)) -> TcRn ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
writeTcRef (TcGblEnv -> TcRef (Maybe (ForeignRef (IORef QState)))
tcg_th_remote_state TcGblEnv
tcg) (ForeignRef (IORef QState) -> Maybe (ForeignRef (IORef QState))
forall a. a -> Maybe a
Just ForeignRef (IORef QState)
fhv)
ForeignRef (IORef QState) -> TcM (ForeignRef (IORef QState))
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
fhv
wrapTHResult :: TcM a -> TcM (THResult a)
wrapTHResult :: forall a. TcM a -> TcM (THResult a)
wrapTHResult TcM a
tcm = do
Either IOEnvFailure a
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 Either IOEnvFailure a
e of
Left IOEnvFailure
e -> THResult a -> TcM (THResult 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 -> TcM (THResult 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
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
HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM (THResult ())) -> TcRn () -> TcM (THResult ())
forall a b. (a -> b) -> a -> b
$ IO (ForeignRef (Q ()))
-> IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (HscEnv -> RemoteRef (Q ()) -> IO (ForeignRef (Q ()))
forall a. HscEnv -> RemoteRef a -> IO (ForeignRef a)
mkFinalizedHValue HscEnv
hsc_env RemoteRef (Q ())
r) IOEnv (Env TcGblEnv TcLclEnv) (ForeignRef (Q ()))
-> (ForeignRef (Q ()) -> TcRn ()) -> TcRn ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ForeignRef (Q ()) -> TcRn ()
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 -> TcRnIf TcGblEnv TcLclEnv Bool -> TcM (THResult Bool)
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRnIf TcGblEnv TcLclEnv Bool -> TcM (THResult Bool))
-> TcRnIf TcGblEnv TcLclEnv Bool -> TcM (THResult Bool)
forall a b. (a -> b) -> a -> b
$ Extension -> TcRnIf TcGblEnv TcLclEnv 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
THMessage a
FailIfErrs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult TcRn ()
failIfErrsM
THMessage a
_ -> String -> TcM a
forall a. 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 { CoreAnnTarget
name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; HscEnv
topEnv <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
; AnnEnv
epsHptAnns <- IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv)
-> IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall a b. (a -> b) -> a -> b
$ HscEnv -> Maybe ModGuts -> IO AnnEnv
prepareAnnotations HscEnv
topEnv Maybe ModGuts
forall a. Maybe a
Nothing
; TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; let selectedEpsHptAnns :: [[Word8]]
selectedEpsHptAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep AnnEnv
epsHptAnns CoreAnnTarget
name TypeRep
tyrep
; let selectedTcgAnns :: [[Word8]]
selectedTcgAnns = AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
findAnnsByTypeRep (TcGblEnv -> AnnEnv
tcg_ann_env TcGblEnv
tcg) CoreAnnTarget
name TypeRep
tyrep
; [[Word8]] -> IOEnv (Env TcGblEnv TcLclEnv) [[Word8]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[Word8]]
selectedEpsHptAnns [[Word8]] -> [[Word8]] -> [[Word8]]
forall a. [a] -> [a] -> [a]
++ [[Word8]]
selectedTcgAnns) }
reifyInstances :: TH.Name -> [TH.Type] -> TcM [TH.Dec]
reifyInstances :: Name -> [Type] -> TcM [Dec]
reifyInstances Name
th_nm [Type]
th_tys
= MsgDoc -> TcM [Dec] -> TcM [Dec]
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (String -> MsgDoc
text String
"In the argument of reifyInstances:"
MsgDoc -> MsgDoc -> MsgDoc
<+> Name -> MsgDoc
forall a. Ppr a => a -> MsgDoc
ppr_th Name
th_nm MsgDoc -> MsgDoc -> MsgDoc
<+> [MsgDoc] -> MsgDoc
sep ((Type -> MsgDoc) -> [Type] -> [MsgDoc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> MsgDoc
forall a. Ppr a => a -> MsgDoc
ppr_th [Type]
th_tys)) (TcM [Dec] -> TcM [Dec]) -> TcM [Dec] -> TcM [Dec]
forall a b. (a -> b) -> a -> b
$
do { SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
; Origin
th_origin <- TcM Origin
getThSpliceOrigin
; LHsType GhcPs
rdr_ty <- Origin
-> SrcSpan -> Type -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
cvt Origin
th_origin SrcSpan
loc (Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
th_nm) [Type]
th_tys)
; let tv_rdrs :: FreeKiTyVars
tv_rdrs = LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
rdr_ty
; (([Name]
tv_names, LHsType GhcRn
rn_ty), FreeVars
_fvs)
<- TcM (([Name], LHsType GhcRn), FreeVars)
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall r. TcM r -> TcM r
checkNoErrs (TcM (([Name], LHsType GhcRn), FreeVars)
-> TcM (([Name], LHsType GhcRn), FreeVars))
-> TcM (([Name], LHsType GhcRn), FreeVars)
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall a b. (a -> b) -> a -> b
$
Maybe Any
-> FreeKiTyVars
-> ([Name] -> TcM (([Name], LHsType GhcRn), FreeVars))
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitBndrs Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
tv_rdrs (([Name] -> TcM (([Name], LHsType GhcRn), FreeVars))
-> TcM (([Name], LHsType GhcRn), FreeVars))
-> ([Name] -> TcM (([Name], LHsType GhcRn), FreeVars))
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Name]
tv_names ->
do { (LHsType GhcRn
rn_ty, FreeVars
fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
doc LHsType GhcPs
rdr_ty
; (([Name], LHsType GhcRn), FreeVars)
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (([Name]
tv_names, LHsType GhcRn
rn_ty), FreeVars
fvs) }
; ([Id]
_tvs, Type
ty)
<- TcM ([Id], Type) -> TcM ([Id], Type)
forall r. TcM r -> TcM r
pushTcLevelM_ (TcM ([Id], Type) -> TcM ([Id], Type))
-> TcM ([Id], Type) -> TcM ([Id], Type)
forall a b. (a -> b) -> a -> b
$
TcM ([Id], Type) -> TcM ([Id], Type)
forall r. TcM r -> TcM r
solveEqualities (TcM ([Id], Type) -> TcM ([Id], Type))
-> TcM ([Id], Type) -> TcM ([Id], Type)
forall a b. (a -> b) -> a -> b
$
[Name] -> IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type)
forall a. [Name] -> TcM a -> TcM ([Id], a)
bindImplicitTKBndrs_Skol [Name]
tv_names (IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type))
-> IOEnv (Env TcGblEnv TcLclEnv) Type -> TcM ([Id], Type)
forall a b. (a -> b) -> a -> b
$
LHsType GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) Type
tcInferLHsType LHsType GhcRn
rn_ty
; Type
ty <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcTypeToType Type
ty
; String -> MsgDoc -> TcRn ()
traceTc String
"reifyInstances" (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Type
ty MsgDoc -> MsgDoc -> MsgDoc
$$ Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
ty))
; case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type]
tys)
| Just Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
-> do { InstEnvs
inst_envs <- TcM InstEnvs
tcGetInstEnvs
; let ([InstMatch]
matches, [ClsInst]
unifies, [InstMatch]
_) = Bool
-> InstEnvs
-> Class
-> [Type]
-> ([InstMatch], [ClsInst], [InstMatch])
lookupInstEnv Bool
False InstEnvs
inst_envs Class
cls [Type]
tys
; String -> MsgDoc -> TcRn ()
traceTc String
"reifyInstances1" ([InstMatch] -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr [InstMatch]
matches)
; Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances Class
cls ((InstMatch -> ClsInst) -> [InstMatch] -> [ClsInst]
forall a b. (a -> b) -> [a] -> [b]
map InstMatch -> ClsInst
forall a b. (a, b) -> a
fst [InstMatch]
matches [ClsInst] -> [ClsInst] -> [ClsInst]
forall a. [a] -> [a] -> [a]
++ [ClsInst]
unifies) }
| TyCon -> Bool
isOpenFamilyTyCon TyCon
tc
-> do { FamInstEnvs
inst_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
; let matches :: [FamInstMatch]
matches = FamInstEnvs -> TyCon -> [Type] -> [FamInstMatch]
lookupFamInstEnv FamInstEnvs
inst_envs TyCon
tc [Type]
tys
; String -> MsgDoc -> TcRn ()
traceTc String
"reifyInstances2" ([FamInstMatch] -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr [FamInstMatch]
matches)
; TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc ((FamInstMatch -> FamInst) -> [FamInstMatch] -> [FamInst]
forall a b. (a -> b) -> [a] -> [b]
map FamInstMatch -> FamInst
fim_instance [FamInstMatch]
matches) }
Maybe (TyCon, [Type])
_ -> MsgDoc -> TcM [Dec]
forall a. MsgDoc -> TcM a
bale_out (MsgDoc -> Arity -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text String
"reifyInstances:" MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
quotes (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Type
ty))
Arity
2 (String -> MsgDoc
text String
"is not a class constraint or type family application")) }
where
doc :: HsDocContext
doc = HsDocContext
ClassInstanceCtx
bale_out :: MsgDoc -> TcM a
bale_out MsgDoc
msg = MsgDoc -> TcM a
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
cvt :: Origin -> SrcSpan -> TH.Type -> TcM (LHsType GhcPs)
cvt :: Origin
-> SrcSpan -> Type -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
cvt Origin
origin SrcSpan
loc Type
th_ty = case Origin -> SrcSpan -> Type -> Either MsgDoc (LHsType GhcPs)
convertToHsType Origin
origin SrcSpan
loc Type
th_ty of
Left MsgDoc
msg -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
Right LHsType GhcPs
ty -> LHsType GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall (m :: * -> *) a. Monad m => a -> m a
return LHsType GhcPs
ty
lookupName :: Bool
-> String -> TcM (Maybe TH.Name)
lookupName :: Bool -> String -> TcM (Maybe Name)
lookupName Bool
is_type_name String
s
= do { LocalRdrEnv
lcl_env <- RnM LocalRdrEnv
getLocalRdrEnv
; case LocalRdrEnv -> RdrName -> Maybe Name
lookupLocalRdrEnv LocalRdrEnv
lcl_env RdrName
rdr_name of
Just Name
n -> Maybe Name -> TcM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Maybe Name
forall a. a -> Maybe a
Just (Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
n))
Maybe Name
Nothing -> do { Maybe Name
mb_nm <- RdrName -> RnM (Maybe Name)
lookupGlobalOccRn_maybe RdrName
rdr_name
; Maybe Name -> TcM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Name -> Name) -> Maybe Name -> Maybe Name
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Name
forall n. NamedThing n => n -> Name
reifyName Maybe Name
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
getThing :: TH.Name -> TcM TcTyThing
getThing :: Name -> TcM TcTyThing
getThing Name
th_name
= do { Name
name <- Name -> TcM Name
lookupThName Name
th_name
; MsgDoc -> TcRn ()
forall m n. MsgDoc -> TcRnIf m n ()
traceIf (String -> MsgDoc
text String
"reify" MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text (Name -> String
forall a. Show a => a -> String
show Name
th_name) MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
brackets (Name -> MsgDoc
ppr_ns Name
th_name) MsgDoc -> MsgDoc -> MsgDoc
<+> Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
name)
; Name -> TcM TcTyThing
tcLookupTh Name
name }
where
ppr_ns :: Name -> MsgDoc
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.DataName PkgName
_pkg ModName
_mod)) = String -> MsgDoc
text String
"data"
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.TcClsName PkgName
_pkg ModName
_mod)) = String -> MsgDoc
text String
"tc"
ppr_ns (TH.Name OccName
_ (TH.NameG NameSpace
TH.VarName PkgName
_pkg ModName
_mod)) = String -> MsgDoc
text String
"var"
ppr_ns Name
_ = String -> MsgDoc
forall a. String -> a
panic String
"reify/ppr_ns"
reify :: TH.Name -> TcM TH.Info
reify :: Name -> TcM Info
reify Name
th_name
= do { String -> MsgDoc -> TcRn ()
traceTc String
"reify 1" (String -> MsgDoc
text (Name -> String
TH.showName Name
th_name))
; TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
; String -> MsgDoc -> TcRn ()
traceTc String
"reify 2" (TcTyThing -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TcTyThing
thing)
; TcTyThing -> TcM Info
reifyThing TcTyThing
thing }
lookupThName :: TH.Name -> TcM Name
lookupThName :: Name -> TcM Name
lookupThName Name
th_name = do
Maybe Name
mb_name <- Name -> RnM (Maybe Name)
lookupThName_maybe Name
th_name
case Maybe Name
mb_name of
Maybe Name
Nothing -> MsgDoc -> TcM Name
forall a. MsgDoc -> TcM a
failWithTc (Name -> MsgDoc
notInScope Name
th_name)
Just Name
name -> Name -> TcM Name
forall (m :: * -> *) a. Monad m => a -> m a
return Name
name
lookupThName_maybe :: TH.Name -> TcM (Maybe Name)
lookupThName_maybe :: Name -> RnM (Maybe Name)
lookupThName_maybe Name
th_name
= do { [Name]
names <- (RdrName -> RnM (Maybe Name))
-> [RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) [Name]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM RdrName -> RnM (Maybe Name)
lookup (Name -> [RdrName]
thRdrNameGuesses Name
th_name)
; Maybe Name -> RnM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Name] -> Maybe Name
forall a. [a] -> Maybe a
listToMaybe [Name]
names) }
where
lookup :: RdrName -> RnM (Maybe Name)
lookup RdrName
rdr_name
= do {
; LocalRdrEnv
rdr_env <- RnM LocalRdrEnv
getLocalRdrEnv
; case LocalRdrEnv -> RdrName -> Maybe Name
lookupLocalRdrEnv LocalRdrEnv
rdr_env RdrName
rdr_name of
Just Name
name -> Maybe Name -> RnM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
name)
Maybe Name
Nothing -> RdrName -> RnM (Maybe Name)
lookupGlobalOccRn_maybe RdrName
rdr_name }
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh Name
name
= do { (TcGblEnv
gbl_env, TcLclEnv
lcl_env) <- TcRnIf TcGblEnv TcLclEnv (TcGblEnv, TcLclEnv)
forall gbl lcl. TcRnIf gbl lcl (gbl, lcl)
getEnvs
; case NameEnv TcTyThing -> Name -> Maybe TcTyThing
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv (TcLclEnv -> NameEnv TcTyThing
tcl_env TcLclEnv
lcl_env) Name
name of {
Just TcTyThing
thing -> TcTyThing -> TcM TcTyThing
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 (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
MsgDoc -> TcM TcTyThing
forall a. MsgDoc -> TcM a
failWithTc (Name -> MsgDoc
notInEnv Name
name)
else
do { MaybeErr MsgDoc TyThing
mb_thing <- Name -> TcM (MaybeErr MsgDoc TyThing)
tcLookupImported_maybe Name
name
; case MaybeErr MsgDoc TyThing
mb_thing of
Succeeded TyThing
thing -> TcTyThing -> TcM TcTyThing
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing)
Failed MsgDoc
msg -> MsgDoc -> TcM TcTyThing
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
}}}}
notInScope :: TH.Name -> SDoc
notInScope :: Name -> MsgDoc
notInScope Name
th_name = MsgDoc -> MsgDoc
quotes (String -> MsgDoc
text (Name -> String
forall a. Ppr a => a -> String
TH.pprint Name
th_name)) MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text String
"is not in scope at a reify"
notInEnv :: Name -> SDoc
notInEnv :: Name -> MsgDoc
notInEnv Name
name = MsgDoc -> MsgDoc
quotes (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
name) MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text String
"is not in the type environment at a reify"
reifyRoles :: TH.Name -> TcM [TH.Role]
reifyRoles :: Name -> TcM [Role]
reifyRoles Name
th_name
= do { TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
; case TcTyThing
thing of
AGlobal (ATyCon TyCon
tc) -> [Role] -> TcM [Role]
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
_ -> MsgDoc -> TcM [Role]
forall a. MsgDoc -> TcM a
failWithTc (String -> MsgDoc
text String
"No roles associated with" MsgDoc -> MsgDoc -> MsgDoc
<+> (TcTyThing -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr 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 Id
id))
= do { Type
ty <- Type -> TcM Type
reifyType (Id -> Type
idType Id
id)
; let v :: Name
v = Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
id
; case Id -> IdDetails
idDetails Id
id of
ClassOpId Class
cls -> Info -> TcM Info
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))
RecSelId{sel_tycon :: IdDetails -> RecSelParent
sel_tycon=RecSelData TyCon
tc}
-> Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI (Id -> TyCon -> Name
reifySelector Id
id TyCon
tc) Type
ty Maybe Dec
forall a. Maybe a
Nothing)
IdDetails
_ -> Info -> TcM Info
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)))
= do { let name :: Name
name = DataCon -> Name
dataConName DataCon
dc
; Type
ty <- Type -> TcM Type
reifyType (Id -> Type
idType (DataCon -> Id
dataConWrapId DataCon
dc))
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Name -> Info
TH.DataConI (Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
name) Type
ty
(TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName (DataCon -> TyCon
dataConOrigTyCon DataCon
dc)))
}
reifyThing (AGlobal (AConLike (PatSynCon PatSyn
ps)))
= do { let name :: Name
name = PatSyn -> Name
forall n. NamedThing n => n -> Name
reifyName PatSyn
ps
; Type
ty <- ([InvisTVBinder], [Type], [InvisTVBinder], [Type], [Scaled Type],
Type)
-> TcM Type
reifyPatSynType (PatSyn
-> ([InvisTVBinder], [Type], [InvisTVBinder], [Type],
[Scaled Type], Type)
patSynSigBndr PatSyn
ps)
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Info
TH.PatSynI Name
name Type
ty) }
reifyThing (ATcId {tct_id :: TcTyThing -> Id
tct_id = Id
id})
= do { Type
ty1 <- Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcType (Id -> Type
idType Id
id)
; Type
ty2 <- Type -> TcM Type
reifyType Type
ty1
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Maybe Dec -> Info
TH.VarI (Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
id) Type
ty2 Maybe Dec
forall a. Maybe a
Nothing) }
reifyThing (ATyVar Name
tv Id
tv1)
= do { Type
ty1 <- Id -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcTyVar Id
tv1
; Type
ty2 <- Type -> TcM Type
reifyType Type
ty1
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type -> Info
TH.TyVarI (Name -> Name
forall n. NamedThing n => n -> Name
reifyName Name
tv) Type
ty2) }
reifyThing TcTyThing
thing = String -> MsgDoc -> TcM Info
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"reifyThing" (TcTyThing -> MsgDoc
pprTcTyThingCategory TcTyThing
thing)
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TH.TySynEqn
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch TyCon
fam_tc (CoAxBranch { cab_tvs :: CoAxBranch -> [Id]
cab_tvs = [Id]
tvs
, cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs
, cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs })
= do { Maybe [TyVarBndr ()]
tvs' <- [Id] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [Id]
tvs
; let lhs_types_only :: [Type]
lhs_types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
lhs
; [Type]
lhs' <- [Type] -> TcM [Type]
reifyTypes [Type]
lhs_types_only
; [Type]
annot_th_lhs <- (Bool -> Type -> Type -> TcM Type)
-> [Bool] -> [Type] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Type -> Type -> TcM Type
annotThType (TyCon -> [Bool]
tyConArgsPolyKinded TyCon
fam_tc)
[Type]
lhs_types_only [Type]
lhs'
; let lhs_type :: Type
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
; Type
rhs' <- Type -> TcM Type
reifyType Type
rhs
; TySynEqn -> TcM TySynEqn
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TH.TySynEqn Maybe [TyVarBndr ()]
tvs' Type
lhs_type 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
isFunTyCon TyCon
tc
= Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Arity -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) Arity
2 Bool
False)
| TyCon -> Bool
isPrimTyCon TyCon
tc
= Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Arity -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) ([Id] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc))
(TyCon -> Bool
isUnliftedTyCon TyCon
tc))
| TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
= do { let tvs :: [Id]
tvs = TyCon -> [Id]
tyConTyVars TyCon
tc
res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc
resVar :: Maybe Name
resVar = TyCon -> Maybe Name
famTcResVar TyCon
tc
; Type
kind' <- Type -> TcM Type
reifyKind Type
res_kind
; let (FamilyResultSig
resultSig, Maybe InjectivityAnn
injectivity) =
case Maybe Name
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 = (Id -> Name) -> [Id] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> (Id -> Name) -> Id -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Name
tyVarName)
([Bool] -> [Id] -> [Id]
forall a. [Bool] -> [a] -> [a]
filterByList [Bool]
ms [Id]
tvs)
in (FamilyResultSig
sig, Maybe InjectivityAnn
inj)
; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
; let tfHead :: TypeFamilyHead
tfHead =
Name
-> [TyVarBndr ()]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> TypeFamilyHead
TH.TypeFamilyHead (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' FamilyResultSig
resultSig Maybe InjectivityAnn
injectivity
; if TyCon -> Bool
isOpenTypeFamilyTyCon TyCon
tc
then do { FamInstEnvs
fam_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
; [Dec]
instances <- TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc
(FamInstEnvs -> TyCon -> [FamInst]
familyInstances FamInstEnvs
fam_envs TyCon
tc)
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI (TypeFamilyHead -> Dec
TH.OpenTypeFamilyD TypeFamilyHead
tfHead) [Dec]
instances) }
else do { [TySynEqn]
eqns <-
case TyCon -> Maybe (CoAxiom Branched)
isClosedSynFamilyTyConWithAxiom_maybe TyCon
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)
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 (m :: * -> *) a. Monad m => a -> m a
return []
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI (TypeFamilyHead -> [TySynEqn] -> Dec
TH.ClosedTypeFamilyD TypeFamilyHead
tfHead [TySynEqn]
eqns)
[]) } }
| TyCon -> Bool
isDataFamilyTyCon TyCon
tc
= do { let res_kind :: Type
res_kind = TyCon -> Type
tyConResKind TyCon
tc
; Maybe Type
kind' <- (Type -> Maybe Type)
-> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
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)
; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
; FamInstEnvs
fam_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
; [Dec]
instances <- TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances TyCon
tc (FamInstEnvs -> TyCon -> [FamInst]
familyInstances FamInstEnvs
fam_envs TyCon
tc)
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.FamilyI
(Name -> [TyVarBndr ()] -> Maybe Type -> Dec
TH.DataFamilyD (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' Maybe Type
kind') [Dec]
instances) }
| Just ([Id]
_, Type
rhs) <- TyCon -> Maybe ([Id], Type)
synTyConDefn_maybe TyCon
tc
= do { Type
rhs' <- Type -> TcM Type
reifyType Type
rhs
; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> Info
TH.TyConI
(Name -> [TyVarBndr ()] -> Type -> Dec
TH.TySynD (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) [TyVarBndr ()]
tvs' Type
rhs'))
}
| Bool
otherwise
= do { [Type]
cxt <- [Type] -> TcM [Type]
reifyCxt (TyCon -> [Type]
tyConStupidTheta TyCon
tc)
; let tvs :: [Id]
tvs = TyCon -> [Id]
tyConTyVars TyCon
tc
dataCons :: [DataCon]
dataCons = TyCon -> [DataCon]
tyConDataCons TyCon
tc
isGadt :: Bool
isGadt = TyCon -> Bool
isGadtSyntaxTyCon TyCon
tc
; [Con]
cons <- (DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> [DataCon] -> IOEnv (Env TcGblEnv TcLclEnv) [Con]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool -> [Type] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadt ([Id] -> [Type]
mkTyVarTys [Id]
tvs)) [DataCon]
dataCons
; [TyVarBndr ()]
r_tvs <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars TyCon
tc)
; let name :: Name
name = TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc
deriv :: [a]
deriv = []
decl :: Dec
decl | TyCon -> Bool
isNewTyCon TyCon
tc =
[Type]
-> Name
-> [TyVarBndr ()]
-> Maybe Type
-> Con
-> [DerivClause]
-> Dec
TH.NewtypeD [Type]
cxt Name
name [TyVarBndr ()]
r_tvs Maybe Type
forall a. Maybe a
Nothing ([Con] -> Con
forall a. [a] -> a
head [Con]
cons) [DerivClause]
forall a. [a]
deriv
| Bool
otherwise =
[Type]
-> Name
-> [TyVarBndr ()]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
TH.DataD [Type]
cxt Name
name [TyVarBndr ()]
r_tvs Maybe Type
forall a. Maybe a
Nothing [Con]
cons [DerivClause]
forall a. [a]
deriv
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> Info
TH.TyConI Dec
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
([Id]
ex_tvs, [Type]
theta, [Type]
arg_tys)
= DataCon -> [Type] -> ([Id], [Type], [Type])
dataConInstSig DataCon
dc [Type]
tys
g_user_tvs' :: [InvisTVBinder]
g_user_tvs' = DataCon -> [InvisTVBinder]
dataConUserTyVarBinders DataCon
dc
([Id]
g_univ_tvs, [Id]
_, [EqSpec]
g_eq_spec, [Type]
g_theta', [Scaled Type]
g_arg_tys', Type
g_res_ty')
= DataCon -> ([Id], [Id], [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 = [Id] -> VarSet
mkVarSet ((EqSpec -> Id) -> [EqSpec] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map EqSpec -> Id
eqSpecTyVar [EqSpec]
g_eq_spec)
; (TCvSubst
univ_subst, [Id]
_)
<- [Id] -> TcM (TCvSubst, [Id])
freshenTyVarBndrs ([Id] -> TcM (TCvSubst, [Id])) -> [Id] -> TcM (TCvSubst, [Id])
forall a b. (a -> b) -> a -> b
$
(Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (Id -> VarSet -> Bool
`elemVarSet` VarSet
eq_spec_tvs) [Id]
g_univ_tvs
; let (TCvSubst
tvb_subst, [InvisTVBinder]
g_user_tvs) = TCvSubst -> [InvisTVBinder] -> (TCvSubst, [InvisTVBinder])
forall {argf}.
TCvSubst -> [VarBndr Id argf] -> (TCvSubst, [VarBndr Id argf])
subst_tv_binders TCvSubst
univ_subst [InvisTVBinder]
g_user_tvs'
g_theta :: [Type]
g_theta = HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
substTys TCvSubst
tvb_subst [Type]
g_theta'
g_arg_tys :: [Type]
g_arg_tys = HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
substTys TCvSubst
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 :: Type
g_res_ty = HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy TCvSubst
tvb_subst Type
g_res_ty'
; [Type]
r_arg_tys <- [Type] -> TcM [Type]
reifyTypes (if Bool
isGadtDataCon then [Type]
g_arg_tys else [Type]
arg_tys)
; Con
main_con <-
if | Bool -> Bool
not ([FieldLabel] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
fields) Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isGadtDataCon ->
Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ Name -> [VarBangType] -> Con
TH.RecC Name
name ([Name] -> [Bang] -> [Type] -> [VarBangType]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 ((FieldLabel -> Name) -> [FieldLabel] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> Name
reifyFieldLabel [FieldLabel]
fields)
[Bang]
dcdBangs [Type]
r_arg_tys)
| Bool -> Bool
not ([FieldLabel] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
fields) -> do
{ Type
res_ty <- Type -> TcM Type
reifyType Type
g_res_ty
; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ [Name] -> [VarBangType] -> Type -> Con
TH.RecGadtC [Name
name]
([Name] -> [Bang] -> [Type] -> [VarBangType]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 ((FieldLabel -> Name) -> [FieldLabel] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Name
forall n. NamedThing n => n -> Name
reifyName (Name -> Name) -> (FieldLabel -> Name) -> FieldLabel -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldLabel -> Name
forall a. FieldLbl a -> a
flSelector) [FieldLabel]
fields)
[Bang]
dcdBangs [Type]
r_arg_tys) Type
res_ty }
| DataCon -> Bool
dataConIsInfix DataCon
dc Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
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) }
| Bool
isGadtDataCon -> do
{ Type
res_ty <- Type -> TcM Type
reifyType Type
g_res_ty
; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ [Name] -> [BangType] -> Type -> Con
TH.GadtC [Name
name] ([Bang]
dcdBangs [Bang] -> [Type] -> [BangType]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
r_arg_tys) Type
res_ty }
| Bool
otherwise ->
Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Con -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall a b. (a -> b) -> a -> b
$ Name -> [BangType] -> Con
TH.NormalC Name
name ([Bang]
dcdBangs [Bang] -> [Type] -> [BangType]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
r_arg_tys)
; let ([InvisTVBinder]
ex_tvs', [Type]
theta') | Bool
isGadtDataCon = ([InvisTVBinder]
g_user_tvs, [Type]
g_theta)
| Bool
otherwise = ASSERT( all isTyVar ex_tvs )
((Id -> InvisTVBinder) -> [Id] -> [InvisTVBinder]
forall a b. (a -> b) -> [a] -> [b]
map Id -> InvisTVBinder
forall {var}. var -> VarBndr var Specificity
mk_specified [Id]
ex_tvs, [Type]
theta)
ret_con :: IOEnv (Env TcGblEnv TcLclEnv) Con
ret_con | [InvisTVBinder] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [InvisTVBinder]
ex_tvs' Bool -> Bool -> Bool
&& [Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
theta' = Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return Con
main_con
| Bool
otherwise = do
{ [Type]
cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta'
; [TyVarBndr Specificity]
ex_tvs'' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
ex_tvs'
; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyVarBndr Specificity] -> [Type] -> Con -> Con
TH.ForallC [TyVarBndr Specificity]
ex_tvs'' [Type]
cxt Con
main_con) }
; ASSERT( r_arg_tys `equalLength` dcdBangs )
IOEnv (Env TcGblEnv TcLclEnv) Con
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 :: TCvSubst -> [VarBndr Id argf] -> (TCvSubst, [VarBndr Id argf])
subst_tv_binders TCvSubst
subst [VarBndr Id argf]
tv_bndrs =
let tvs :: [Id]
tvs = [VarBndr Id argf] -> [Id]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr Id argf]
tv_bndrs
flags :: [argf]
flags = (VarBndr Id argf -> argf) -> [VarBndr Id argf] -> [argf]
forall a b. (a -> b) -> [a] -> [b]
map VarBndr Id argf -> argf
forall tv argf. VarBndr tv argf -> argf
binderArgFlag [VarBndr Id argf]
tv_bndrs
(TCvSubst
subst', [Id]
tvs') = HasCallStack => TCvSubst -> [Id] -> (TCvSubst, [Id])
TCvSubst -> [Id] -> (TCvSubst, [Id])
substTyVarBndrs TCvSubst
subst [Id]
tvs
tv_bndrs' :: [VarBndr Id argf]
tv_bndrs' = ((Id, argf) -> VarBndr Id argf)
-> [(Id, argf)] -> [VarBndr Id argf]
forall a b. (a -> b) -> [a] -> [b]
map (\(Id
tv,argf
fl) -> Id -> argf -> VarBndr Id argf
forall var argf. var -> argf -> VarBndr var argf
Bndr Id
tv argf
fl) ([Id] -> [argf] -> [(Id, argf)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Id]
tvs' [argf]
flags)
in (TCvSubst
subst', [VarBndr Id argf]
tv_bndrs')
reifyClass :: Class -> TcM TH.Info
reifyClass :: Class -> TcM Info
reifyClass Class
cls
= do { [Type]
cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta
; InstEnvs
inst_envs <- TcM InstEnvs
tcGetInstEnvs
; [Dec]
insts <- Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances Class
cls (InstEnvs -> Class -> [ClsInst]
InstEnv.classInstances InstEnvs
inst_envs Class
cls)
; [Dec]
assocTys <- (ClassATItem -> TcM [Dec]) -> [ClassATItem] -> TcM [Dec]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM ClassATItem -> TcM [Dec]
reifyAT [ClassATItem]
ats
; [Dec]
ops <- ((Id, Maybe (Name, DefMethSpec Type)) -> TcM [Dec])
-> [(Id, Maybe (Name, DefMethSpec Type))] -> TcM [Dec]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM (Id, Maybe (Name, DefMethSpec Type)) -> TcM [Dec]
forall {a}. (Id, Maybe (a, DefMethSpec Type)) -> TcM [Dec]
reify_op [(Id, Maybe (Name, DefMethSpec Type))]
op_stuff
; [TyVarBndr ()]
tvs' <- [Id] -> TcM [TyVarBndr ()]
reifyTyVars (TyCon -> [Id]
tyConVisibleTyVars (Class -> TyCon
classTyCon Class
cls))
; let dec :: Dec
dec = [Type] -> Name -> [TyVarBndr ()] -> [FunDep] -> [Dec] -> Dec
TH.ClassD [Type]
cxt (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls) [TyVarBndr ()]
tvs' [FunDep]
fds' ([Dec]
assocTys [Dec] -> [Dec] -> [Dec]
forall a. [a] -> [a] -> [a]
++ [Dec]
ops)
; Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> [Dec] -> Info
TH.ClassI Dec
dec [Dec]
insts) }
where
([Id]
_, [FunDep Id]
fds, [Type]
theta, [Id]
_, [ClassATItem]
ats, [(Id, Maybe (Name, DefMethSpec Type))]
op_stuff) = Class
-> ([Id], [FunDep Id], [Type], [Id], [ClassATItem],
[(Id, Maybe (Name, DefMethSpec Type))])
classExtraBigSig Class
cls
fds' :: [FunDep]
fds' = (FunDep Id -> FunDep) -> [FunDep Id] -> [FunDep]
forall a b. (a -> b) -> [a] -> [b]
map FunDep Id -> FunDep
reifyFunDep [FunDep Id]
fds
reify_op :: (Id, Maybe (a, DefMethSpec Type)) -> TcM [Dec]
reify_op (Id
op, Maybe (a, DefMethSpec Type)
def_meth)
= do { let ([Id]
_, Type
_, Type
ty) = Type -> ([Id], Type, Type)
tcSplitMethodTy (Id -> Type
idType Id
op)
; Type
ty' <- Type -> TcM Type
reifyType Type
ty
; let nm' :: Name
nm' = Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
op
; case Maybe (a, DefMethSpec Type)
def_meth of
Just (a
_, GenericDM Type
gdm_ty) ->
do { Type
gdm_ty' <- Type -> TcM Type
reifyType Type
gdm_ty
; [Dec] -> TcM [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return [Name -> Type -> Dec
TH.SigD Name
nm' Type
ty', Name -> Type -> Dec
TH.DefaultSigD Name
nm' Type
gdm_ty'] }
Maybe (a, DefMethSpec Type)
_ -> [Dec] -> TcM [Dec]
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, ATValidityInfo)
def) = do
Info
tycon' <- TyCon -> TcM Info
reifyTyCon TyCon
tycon
case Info
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, ATValidityInfo) -> TcM [Dec])
-> Maybe (Type, ATValidityInfo)
-> TcM [Dec]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Dec] -> TcM [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return [])
((Dec -> [Dec]) -> IOEnv (Env TcGblEnv TcLclEnv) Dec -> TcM [Dec]
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, ATValidityInfo) -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> (Type, ATValidityInfo)
-> 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, ATValidityInfo) -> Type)
-> (Type, ATValidityInfo)
-> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Type, ATValidityInfo) -> Type
forall a b. (a, b) -> a
fst)
Maybe (Type, ATValidityInfo)
def
Info
_ -> String -> MsgDoc -> TcM [Dec]
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"reifyAT" (String -> MsgDoc
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 ()]
args FamilyResultSig
_ Maybe InjectivityAnn
_))
= (Name
n, (TyVarBndr () -> Name) -> [TyVarBndr ()] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVarBndr () -> Name
forall flag. TyVarBndr flag -> Name
bndrName [TyVarBndr ()]
args)
tfNames Dec
d = String -> MsgDoc -> (Name, [Name])
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"tfNames" (String -> MsgDoc
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 (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
$ (Id -> Bool) -> VarSet -> VarSet
filterVarSet Id -> 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
tcTypeKind Type
ty
; Type
th_ki <- Type -> TcM Type
reifyKind Type
ki
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
TH.SigT Type
th_ty Type
th_ki) }
annotThType Bool
_ Type
_ Type
th_ty = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_ty
tyConArgsPolyKinded :: TyCon -> [Bool]
tyConArgsPolyKinded :: TyCon -> [Bool]
tyConArgsPolyKinded TyCon
tc =
(Id -> Bool) -> [Id] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Bool
is_poly_ty (Type -> Bool) -> (Id -> Type) -> Id -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
tyVarKind) [Id]
tc_vis_tvs
[Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ (TyCoBinder -> Bool) -> [TyCoBinder] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Bool
is_poly_ty (Type -> Bool) -> (TyCoBinder -> Type) -> TyCoBinder -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCoBinder -> Type
tyCoBinderType) [TyCoBinder]
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
$
(Id -> Bool) -> VarSet -> VarSet
filterVarSet Id -> 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 :: [Id]
tc_vis_tvs = TyCon -> [Id]
tyConVisibleTyVars TyCon
tc
tc_res_kind_vis_bndrs :: [TyCoBinder]
tc_res_kind_vis_bndrs :: [TyCoBinder]
tc_res_kind_vis_bndrs = (TyCoBinder -> Bool) -> [TyCoBinder] -> [TyCoBinder]
forall a. (a -> Bool) -> [a] -> [a]
filter TyCoBinder -> Bool
isVisibleBinder ([TyCoBinder] -> [TyCoBinder]) -> [TyCoBinder] -> [TyCoBinder]
forall a b. (a -> b) -> a -> b
$ ([TyCoBinder], Type) -> [TyCoBinder]
forall a b. (a, b) -> a
fst (([TyCoBinder], Type) -> [TyCoBinder])
-> ([TyCoBinder], Type) -> [TyCoBinder]
forall a b. (a -> b) -> a -> b
$ Type -> ([TyCoBinder], Type)
splitPiTys (Type -> ([TyCoBinder], Type)) -> Type -> ([TyCoBinder], 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)
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 { [Type]
cxt <- [Type] -> TcM [Type]
reifyCxt [Type]
theta
; let vis_types :: [Type]
vis_types = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
cls_tc [Type]
types
; [Type]
thtypes <- [Type] -> TcM [Type]
reifyTypes [Type]
vis_types
; [Type]
annot_thtypes <- (Bool -> Type -> Type -> TcM Type)
-> [Bool] -> [Type] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Type -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [Type]
vis_types [Type]
thtypes
; let head_ty :: Type
head_ty = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT (Class -> Name
forall n. NamedThing n => n -> Name
reifyName Class
cls)) [Type]
annot_thtypes
; Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall a b. (a -> b) -> a -> b
$ (Maybe Overlap -> [Type] -> Type -> [Dec] -> Dec
TH.InstanceD Maybe Overlap
over [Type]
cxt Type
head_ty []) }
where
([Id]
_tvs, [Type]
theta, Class
cls, [Type]
types) = Type -> ([Id], [Type], Class, [Type])
tcSplitDFunTy (Id -> Type
idType Id
dfun)
cls_tc :: TyCon
cls_tc = Class -> TyCon
classTyCon Class
cls
dfun :: Id
dfun = ClsInst -> Id
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
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)
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 -> [Id]
cab_tvs = [Id]
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 { Maybe [TyVarBndr ()]
th_tvs <- [Id] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [Id]
tvs
; let lhs_types_only :: [Type]
lhs_types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
lhs
; [Type]
th_lhs <- [Type] -> TcM [Type]
reifyTypes [Type]
lhs_types_only
; [Type]
annot_th_lhs <- (Bool -> Type -> Type -> TcM Type)
-> [Bool] -> [Type] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Type -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [Type]
lhs_types_only
[Type]
th_lhs
; let lhs_type :: Type
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
; Type
th_rhs <- Type -> TcM Type
reifyType Type
rhs
; Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall (m :: * -> *) a. Monad m => a -> m a
return (TySynEqn -> Dec
TH.TySynInstD (Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TH.TySynEqn Maybe [TyVarBndr ()]
th_tvs Type
lhs_type Type
th_rhs)) }
DataFamilyInst TyCon
rep_tc ->
do { let
([Id]
ee_tvs, [Type]
ee_lhs, Type
_) = CoAxBranch -> ([Id], [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
; Maybe [TyVarBndr ()]
th_tvs <- [Id] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [Id]
ee_tvs
; [Con]
cons <- (DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con)
-> [DataCon] -> IOEnv (Env TcGblEnv TcLclEnv) [Con]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool -> [Type] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadt ([Id] -> [Type]
mkTyVarTys [Id]
ee_tvs)) [DataCon]
dataCons
; let types_only :: [Type]
types_only = TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
fam_tc [Type]
ee_lhs
; [Type]
th_tys <- [Type] -> TcM [Type]
reifyTypes [Type]
types_only
; [Type]
annot_th_tys <- (Bool -> Type -> Type -> TcM Type)
-> [Bool] -> [Type] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Type -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [Type]
types_only [Type]
th_tys
; let lhs_type :: Type
lhs_type = Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
fam') [Type]
annot_th_tys
; Maybe Type
mb_sig <-
if ([Con] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Con]
cons Bool -> Bool -> Bool
|| TyCon -> Bool
isGadtSyntaxTyCon TyCon
rep_tc)
Bool -> Bool -> Bool
&& Bool -> TyCon -> Arity -> Bool
tyConAppNeedsKindSig Bool
False TyCon
fam_tc ([Type] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Type]
ee_lhs)
then do { let full_kind :: Type
full_kind = HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
fam_tc [Type]
ee_lhs)
; Type
th_full_kind <- Type -> TcM Type
reifyKind Type
full_kind
; Maybe Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type))
-> Maybe Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
forall a b. (a -> b) -> a -> b
$ Type -> Maybe Type
forall a. a -> Maybe a
Just Type
th_full_kind }
else Maybe Type -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe Type)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Type
forall a. Maybe a
Nothing
; Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> Dec -> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall a b. (a -> b) -> a -> b
$
if TyCon -> Bool
isNewTyCon TyCon
rep_tc
then [Type]
-> Maybe [TyVarBndr ()]
-> Type
-> Maybe Type
-> Con
-> [DerivClause]
-> Dec
TH.NewtypeInstD [] Maybe [TyVarBndr ()]
th_tvs Type
lhs_type Maybe Type
mb_sig ([Con] -> Con
forall a. [a] -> a
head [Con]
cons) []
else [Type]
-> Maybe [TyVarBndr ()]
-> Type
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
TH.DataInstD [] Maybe [TyVarBndr ()]
th_tvs Type
lhs_type Maybe Type
mb_sig [Con]
cons []
}
reifyType :: TyCoRep.Type -> TcM TH.Type
reifyType :: Type -> TcM Type
reifyType Type
ty | Type -> Bool
tcIsLiftedTypeKind Type
ty = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TH.StarT
reifyType ty :: Type
ty@(ForAllTy (Bndr Id
_ ArgFlag
argf) Type
_)
= ArgFlag -> Type -> TcM Type
reify_for_all ArgFlag
argf Type
ty
reifyType (LitTy TyLit
t) = do { TyLit
r <- TyLit -> TcM TyLit
reifyTyLit TyLit
t; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (TyLit -> Type
TH.LitT TyLit
r) }
reifyType (TyVarTy Id
tv) = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
TH.VarT (Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
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) = Type -> (Type, [Type])
splitAppTys Type
ty
Type
ty_head' <- Type -> TcM Type
reifyType Type
ty_head
[Type]
ty_args' <- [Type] -> TcM [Type]
reifyTypes (Type -> [Type] -> [Type]
filter_out_invisible_args Type
ty_head [Type]
ty_args)
Type -> TcM Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type -> TcM Type) -> Type -> TcM Type
forall a b. (a -> b) -> a -> b
$ Type -> [Type] -> Type
mkThAppTs Type
ty_head' [Type]
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 ((ArgFlag -> Bool) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map ArgFlag -> Bool
isVisibleArgFlag ([ArgFlag] -> [Bool]) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> a -> b
$ Type -> [Type] -> [ArgFlag]
appTyArgFlags Type
ty_head [Type]
ty_args)
[Type]
ty_args
reifyType ty :: Type
ty@(FunTy { ft_af :: Type -> AnonArgFlag
ft_af = AnonArgFlag
af, ft_mult :: Type -> Type
ft_mult = Type
Many, ft_arg :: Type -> Type
ft_arg = Type
t1, ft_res :: Type -> Type
ft_res = Type
t2 })
| AnonArgFlag
InvisArg <- AnonArgFlag
af = ArgFlag -> Type -> TcM Type
reify_for_all ArgFlag
Inferred Type
ty
| Bool
otherwise = do { [Type
r1,Type
r2] <- [Type] -> TcM [Type]
reifyTypes [Type
t1,Type
t2]
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
TH.ArrowT Type -> Type -> Type
`TH.AppT` Type
r1 Type -> Type -> Type
`TH.AppT` Type
r2) }
reifyType ty :: Type
ty@(FunTy { ft_af :: Type -> AnonArgFlag
ft_af = AnonArgFlag
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 })
| AnonArgFlag
InvisArg <- AnonArgFlag
af = PtrString -> MsgDoc -> TcM Type
forall a. PtrString -> MsgDoc -> TcM a
noTH (String -> PtrString
sLit String
"linear invisible argument") (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Type
ty)
| Bool
otherwise = do { [Type
rm,Type
r1,Type
r2] <- [Type] -> TcM [Type]
reifyTypes [Type
tm,Type
t1,Type
t2]
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
TH.MulArrowT Type -> Type -> Type
`TH.AppT` Type
rm Type -> Type -> Type
`TH.AppT` Type
r1 Type -> Type -> Type
`TH.AppT` Type
r2) }
reifyType (CastTy Type
t KindCoercion
_) = Type -> TcM Type
reifyType Type
t
reifyType ty :: Type
ty@(CoercionTy {})= PtrString -> MsgDoc -> TcM Type
forall a. PtrString -> MsgDoc -> TcM a
noTH (String -> PtrString
sLit String
"coercions in types") (Type -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Type
ty)
reify_for_all :: TyCoRep.ArgFlag -> TyCoRep.Type -> TcM TH.Type
reify_for_all :: ArgFlag -> Type -> TcM Type
reify_for_all ArgFlag
argf Type
ty
| ArgFlag -> Bool
isVisibleArgFlag ArgFlag
argf
= do let ([TcReqTVBinder]
req_bndrs, Type
phi) = Type -> ([TcReqTVBinder], Type)
tcSplitForAllTysReq Type
ty
[TyVarBndr ()]
tvbndrs' <- [TcReqTVBinder] -> TcM [TyVarBndr ()]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [TcReqTVBinder]
req_bndrs
Type
phi' <- Type -> TcM Type
reifyType Type
phi
Type -> TcM Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type -> TcM Type) -> Type -> TcM Type
forall a b. (a -> b) -> a -> b
$ [TyVarBndr ()] -> Type -> Type
TH.ForallVisT [TyVarBndr ()]
tvbndrs' Type
phi'
| Bool
otherwise
= do let ([InvisTVBinder]
inv_bndrs, Type
phi) = Type -> ([InvisTVBinder], Type)
tcSplitForAllTysInvis Type
ty
[TyVarBndr Specificity]
tvbndrs' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
inv_bndrs
let ([Type]
cxt, Type
tau) = Type -> ([Type], Type)
tcSplitPhiTy Type
phi
[Type]
cxt' <- [Type] -> TcM [Type]
reifyCxt [Type]
cxt
Type
tau' <- Type -> TcM Type
reifyType Type
tau
Type -> TcM Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type -> TcM Type) -> Type -> TcM Type
forall a b. (a -> b) -> a -> b
$ [TyVarBndr Specificity] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr Specificity]
tvbndrs' [Type]
cxt' Type
tau'
reifyTyLit :: TyCoRep.TyLit -> TcM TH.TyLit
reifyTyLit :: TyLit -> TcM TyLit
reifyTyLit (NumTyLit Integer
n) = TyLit -> TcM TyLit
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> TyLit
TH.NumTyLit Integer
n)
reifyTyLit (StrTyLit FastString
s) = TyLit -> TcM TyLit
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> TyLit
TH.StrTyLit (FastString -> String
unpackFS FastString
s))
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)
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 { [TyVarBndr Specificity]
univTyVars' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
univTyVars
; [Type]
req' <- [Type] -> TcM [Type]
reifyCxt [Type]
req
; [TyVarBndr Specificity]
exTyVars' <- [InvisTVBinder] -> TcM [TyVarBndr Specificity]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs [InvisTVBinder]
exTyVars
; [Type]
prov' <- [Type] -> TcM [Type]
reifyCxt [Type]
prov
; Type
tau' <- Type -> TcM Type
reifyType ([Scaled Type] -> Type -> Type
mkVisFunTys [Scaled Type]
argTys Type
resTy)
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcM Type) -> Type -> TcM Type
forall a b. (a -> b) -> a -> b
$ [TyVarBndr Specificity] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr Specificity]
univTyVars' [Type]
req'
(Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ [TyVarBndr Specificity] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr Specificity]
exTyVars' [Type]
prov' Type
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)
mapM Type -> TcM Type
reifyType
reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
reifyFunDep :: FunDep Id -> FunDep
reifyFunDep ([Id]
xs, [Id]
ys) = [Name] -> [Name] -> FunDep
TH.FunDep ((Id -> Name) -> [Id] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Name
forall n. NamedThing n => n -> Name
reifyName [Id]
xs) ((Id -> Name) -> [Id] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Name
forall n. NamedThing n => n -> Name
reifyName [Id]
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
reifyTyVars :: [TyVar] -> TcM [TH.TyVarBndr ()]
reifyTyVars :: [Id] -> TcM [TyVarBndr ()]
reifyTyVars = [TcReqTVBinder] -> TcM [TyVarBndr ()]
forall flag flag'.
ReifyFlag flag flag' =>
[VarBndr Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs ([TcReqTVBinder] -> TcM [TyVarBndr ()])
-> ([Id] -> [TcReqTVBinder]) -> [Id] -> TcM [TyVarBndr ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Id -> TcReqTVBinder) -> [Id] -> [TcReqTVBinder]
forall a b. (a -> b) -> [a] -> [b]
map Id -> 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 Id flag] -> TcM [TyVarBndr flag']
reifyTyVarBndrs = (VarBndr Id flag
-> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag'))
-> [VarBndr Id flag]
-> IOEnv (Env TcGblEnv TcLclEnv) [TyVarBndr flag']
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM VarBndr Id flag -> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag')
forall {flag} {flag}.
ReifyFlag flag flag =>
VarBndr Id flag -> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag)
reify_tvbndr
where
reify_tvbndr :: VarBndr Id flag -> IOEnv (Env TcGblEnv TcLclEnv) (TyVarBndr flag)
reify_tvbndr (Bndr Id
tv flag
fl) = Name -> flag -> Type -> TyVarBndr flag
forall flag. Name -> flag -> Type -> TyVarBndr flag
TH.KindedTV (Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
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 (Id -> Type
tyVarKind Id
tv)
reifyTyVarsToMaybe :: [TyVar] -> TcM (Maybe [TH.TyVarBndr ()])
reifyTyVarsToMaybe :: [Id] -> TcM (Maybe [TyVarBndr ()])
reifyTyVarsToMaybe [] = Maybe [TyVarBndr ()] -> TcM (Maybe [TyVarBndr ()])
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing
reifyTyVarsToMaybe [Id]
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
<$> [Id] -> TcM [TyVarBndr ()]
reifyTyVars [Id]
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 { [Type]
tys' <- [Type] -> TcM [Type]
reifyTypes (TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
tc [Type]
tys)
; Type -> TcM Type
maybe_sig_t (Type -> [Type] -> Type
mkThAppTs Type
r_tc [Type]
tys') }
where
arity :: Arity
arity = TyCon -> Arity
tyConArity TyCon
tc
r_tc :: Type
r_tc | TyCon -> Bool
isUnboxedSumTyCon TyCon
tc = Arity -> Type
TH.UnboxedSumT (Arity
arity Arity -> Arity -> Arity
forall a. Integral a => a -> a -> a
`div` Arity
2)
| TyCon -> Bool
isUnboxedTupleTyCon TyCon
tc = Arity -> Type
TH.UnboxedTupleT (Arity
arity Arity -> Arity -> Arity
forall a. Integral a => a -> a -> a
`div` Arity
2)
| TyCon -> Bool
isPromotedTupleTyCon TyCon
tc = Arity -> Type
TH.PromotedTupleT (Arity
arity Arity -> Arity -> Arity
forall a. Integral a => a -> a -> a
`div` Arity
2)
| TyCon -> Bool
isTupleTyCon TyCon
tc = if TyCon -> Bool
isPromotedDataCon TyCon
tc
then Arity -> Type
TH.PromotedTupleT Arity
arity
else Arity -> Type
TH.TupleT Arity
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
isPromotedDataCon 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 -> Arity -> Bool
tyConAppNeedsKindSig
Bool
False
TyCon
tc ([Type] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Type]
tys)
= do { let full_kind :: Type
full_kind = HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys)
; Type
th_full_kind <- Type -> TcM Type
reifyKind Type
full_kind
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
TH.SigT Type
th_type Type
th_full_kind) }
| Bool
otherwise
= Type -> TcM Type
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 (Arity -> Integer
forall a. Integral a => a -> Integer
toInteger (Arity -> Integer) -> Arity -> Integer
forall a b. (a -> b) -> a -> b
$ Unique -> Arity
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 = ASSERT( isExternalName name ) nameModule name
pkg_str :: String
pkg_str = Unit -> 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
| Bool
otherwise = String -> MsgDoc -> String -> String -> String -> Name
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"reifyName" (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
name)
reifyFieldLabel :: FieldLabel -> TH.Name
reifyFieldLabel :: FieldLabel -> Name
reifyFieldLabel FieldLabel
fl
| FieldLabel -> Bool
forall a. FieldLbl a -> Bool
flIsOverloaded FieldLabel
fl
= OccName -> NameFlavour -> Name
TH.Name (String -> OccName
TH.mkOccName String
occ_str) (ModName -> NameFlavour
TH.NameQ (String -> ModName
TH.mkModName String
mod_str))
| Bool
otherwise = String -> String -> String -> Name
TH.mkNameG_v String
pkg_str String
mod_str String
occ_str
where
name :: Name
name = FieldLabel -> Name
forall a. FieldLbl a -> a
flSelector FieldLabel
fl
mod :: GenModule Unit
mod = ASSERT( isExternalName name ) nameModule name
pkg_str :: String
pkg_str = Unit -> 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 = FastString -> String
unpackFS (FieldLabel -> FastString
forall a. FieldLbl a -> FastString
flLabel FieldLabel
fl)
reifySelector :: Id -> TyCon -> TH.Name
reifySelector :: Id -> TyCon -> Name
reifySelector Id
id TyCon
tc
= case (FieldLabel -> Bool) -> [FieldLabel] -> Maybe FieldLabel
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Id -> Name
idName Id
id Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
==) (Name -> Bool) -> (FieldLabel -> Name) -> FieldLabel -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldLabel -> Name
forall a. FieldLbl a -> a
flSelector) (TyCon -> [FieldLabel]
tyConFieldLabels TyCon
tc) of
Just FieldLabel
fl -> FieldLabel -> Name
reifyFieldLabel FieldLabel
fl
Maybe FieldLabel
Nothing -> String -> MsgDoc -> Name
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic String
"reifySelector: missing field" (Id -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Id
id MsgDoc -> MsgDoc -> MsgDoc
$$ TyCon -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TyCon
tc)
reifyFixity :: Name -> TcM (Maybe TH.Fixity)
reifyFixity :: Name -> TcM (Maybe Fixity)
reifyFixity Name
name
= do { (Bool
found, Fixity
fix) <- Name -> RnM (Bool, Fixity)
lookupFixityRn_help Name
name
; Maybe Fixity -> TcM (Maybe Fixity)
forall (m :: * -> *) a. Monad m => a -> m a
return (if Bool
found then Fixity -> Maybe Fixity
forall a. a -> Maybe a
Just (Fixity -> Fixity
conv_fix Fixity
fix) else Maybe Fixity
forall a. Maybe a
Nothing) }
where
conv_fix :: Fixity -> Fixity
conv_fix (BasicTypes.Fixity SourceText
_ Arity
i FixityDirection
d) = Arity -> FixityDirection -> Fixity
TH.Fixity Arity
i (FixityDirection -> FixityDirection
conv_dir FixityDirection
d)
conv_dir :: FixityDirection -> FixityDirection
conv_dir FixityDirection
BasicTypes.InfixR = FixityDirection
TH.InfixR
conv_dir FixityDirection
BasicTypes.InfixL = FixityDirection
TH.InfixL
conv_dir FixityDirection
BasicTypes.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 HsImplBang
HsStrict = DecidedStrictness
TH.DecidedStrict
reifyDecidedStrictness HsUnpack{} = DecidedStrictness
TH.DecidedUnpack
reifyTypeOfThing :: TH.Name -> TcM TH.Type
reifyTypeOfThing :: Name -> TcM Type
reifyTypeOfThing Name
th_name = do
TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
case TcTyThing
thing of
AGlobal (AnId Id
id) -> Type -> TcM Type
reifyType (Id -> Type
idType Id
id)
AGlobal (ATyCon TyCon
tc) -> Type -> TcM Type
reifyKind (TyCon -> Type
tyConKind TyCon
tc)
AGlobal (AConLike (RealDataCon DataCon
dc)) ->
Type -> TcM Type
reifyType (Id -> Type
idType (DataCon -> Id
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 -> Id
tct_id = Id
id} -> Type -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcType (Id -> Type
idType Id
id) IOEnv (Env TcGblEnv TcLclEnv) Type
-> (Type -> TcM Type) -> TcM Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Type -> TcM Type
reifyType
ATyVar Name
_ Id
tctv -> Id -> IOEnv (Env TcGblEnv TcLclEnv) Type
zonkTcTyVar Id
tctv IOEnv (Env TcGblEnv TcLclEnv) Type
-> (Type -> TcM Type) -> TcM Type
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. String -> a
panic String
"reifyTypeOfThing: ACoAxiom"
ATcTyCon TyCon
_ -> String -> TcM Type
forall a. String -> a
panic String
"reifyTypeOfThing: ATcTyCon"
APromotionErr PromotionErr
_ -> String -> TcM Type
forall a. 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 (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 (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 { CoreAnnTarget
name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; HscEnv
topEnv <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
; AnnEnv
epsHptAnns <- IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv)
-> IO AnnEnv -> IOEnv (Env TcGblEnv TcLclEnv) AnnEnv
forall a b. (a -> b) -> a -> b
$ HscEnv -> Maybe ModGuts -> IO AnnEnv
prepareAnnotations HscEnv
topEnv Maybe ModGuts
forall a. Maybe a
Nothing
; TcGblEnv
tcg <- TcRnIf TcGblEnv TcLclEnv TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
; let selectedEpsHptAnns :: [a]
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 :: [a]
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
; [a] -> TcM [a]
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
selectedEpsHptAnns [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
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
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
GenModule Unit
this_mod <- IOEnv (Env TcGblEnv TcLclEnv) (GenModule Unit)
forall (m :: * -> *). HasModule m => m (GenModule Unit)
getModule
let reifMod :: GenModule Unit
reifMod = Unit -> ModuleName -> GenModule Unit
forall u. u -> ModuleName -> GenModule u
mkModule (String -> Unit
stringToUnit String
pkgString) (String -> ModuleName
mkModuleName String
mString)
if (GenModule Unit
reifMod GenModule Unit -> GenModule Unit -> Bool
forall a. Eq a => a -> a -> Bool
== GenModule Unit
this_mod) then TcM ModuleInfo
reifyThisModule else GenModule Unit -> TcM ModuleInfo
reifyFromIface GenModule Unit
reifMod
where
reifyThisModule :: TcM ModuleInfo
reifyThisModule = do
[Module]
usages <- (ImportAvails -> [Module])
-> IOEnv (Env TcGblEnv TcLclEnv) ImportAvails
-> IOEnv (Env TcGblEnv TcLclEnv) [Module]
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
ModuleInfo -> TcM ModuleInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (ModuleInfo -> TcM ModuleInfo) -> ModuleInfo -> TcM ModuleInfo
forall a b. (a -> b) -> a -> b
$ [Module] -> ModuleInfo
TH.ModuleInfo [Module]
usages
reifyFromIface :: GenModule Unit -> TcM ModuleInfo
reifyFromIface GenModule Unit
reifMod = do
ModIface
iface <- MsgDoc -> GenModule Unit -> TcRn ModIface
loadInterfaceForModule (String -> MsgDoc
text String
"reifying module from TH for" MsgDoc -> MsgDoc -> MsgDoc
<+> GenModule Unit -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr GenModule Unit
reifMod) GenModule Unit
reifMod
let usages :: [Module]
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] ]
ModuleInfo -> TcM ModuleInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (ModuleInfo -> TcM ModuleInfo) -> ModuleInfo -> TcM ModuleInfo
forall a b. (a -> b) -> a -> b
$ [Module] -> ModuleInfo
TH.ModuleInfo [Module]
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
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 (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 :: PtrString -> SDoc -> TcM a
noTH :: forall a. PtrString -> MsgDoc -> TcM a
noTH PtrString
s MsgDoc
d = MsgDoc -> TcM a
forall a. MsgDoc -> TcM a
failWithTc ([MsgDoc] -> MsgDoc
hsep [String -> MsgDoc
text String
"Can't represent" MsgDoc -> MsgDoc -> MsgDoc
<+> PtrString -> MsgDoc
ptext PtrString
s MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text String
"in Template Haskell:",
Arity -> MsgDoc -> MsgDoc
nest Arity
2 MsgDoc
d])
ppr_th :: TH.Ppr a => a -> SDoc
ppr_th :: forall a. Ppr a => a -> MsgDoc
ppr_th a
x = String -> MsgDoc
text (a -> String
forall a. Ppr a => a -> String
TH.pprint a
x)
tcGetInterp :: TcM Interp
tcGetInterp :: TcM Interp
tcGetInterp = do
HscEnv
hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
case HscEnv -> Maybe Interp
hsc_interp HscEnv
hsc_env of
Maybe Interp
Nothing -> IO Interp -> TcM Interp
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. Exception e => e -> IO a
throwIO (String -> GhcException
InstallationError String
"Template haskell requires a target code interpreter")
Just Interp
i -> Interp -> TcM Interp
forall (f :: * -> *) a. Applicative f => a -> f a
pure Interp
i