{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module TcSplice(
tcSpliceExpr, tcTypedBracket, tcUntypedBracket,
runAnnotation,
runMetaE, runMetaP, runMetaT, runMetaD, runQuasi,
tcTopSpliceExpr, lookupThName_maybe,
defaultRunMeta, runMeta', runRemoteModFinalizers,
finishTH, runTopSplice
) where
#include "HsVersions.h"
import GhcPrelude
import HsSyn
import Annotations
import Finder
import Name
import TcRnMonad
import TcType
import Outputable
import TcExpr
import SrcLoc
import THNames
import TcUnify
import TcEnv
import Coercion( etaExpandCoAxBranch )
import FileCleanup ( newTempName, TempFileLifetime(..) )
import Control.Monad
import GHCi.Message
import GHCi.RemoteTypes
import GHCi
import HscMain
import FV
import RnSplice( traceSplice, SpliceInfo(..))
import RdrName
import HscTypes
import Convert
import RnExpr
import RnEnv
import RnUtils ( HsDocContext(..) )
import RnFixity ( lookupFixityRn_help )
import RnTypes
import TcHsSyn
import TcSimplify
import Type
import NameSet
import TcMType
import TcHsType
import TcIface
import TyCoRep
import FamInst
import FamInstEnv
import InstEnv
import Inst
import NameEnv
import PrelNames
import TysWiredIn
import OccName
import Hooks
import Var
import Module
import LoadIface
import Class
import TyCon
import CoAxiom
import PatSyn
import ConLike
import DataCon
import TcEvidence( TcEvBinds(..) )
import Id
import IdInfo
import DsExpr
import DsMonad
import GHC.Serialized
import ErrUtils
import Util
import Unique
import VarSet
import Data.List ( find )
import Data.Maybe
import FastString
import BasicTypes hiding( SuccessFlag(..) )
import Maybes( MaybeErr(..) )
import DynFlags
import Panic
import Lexeme
import qualified EnumSet
import Plugins
import Bag
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH
import GHC.Desugar ( AnnotationWrapper(..) )
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 (..) )
import GHC.Exts ( unsafeCoerce# )
tcTypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcUntypedBracket :: HsExpr GhcRn -> HsBracket GhcRn -> [PendingRnSplice] -> ExpRhoType
-> TcM (HsExpr GhcTcId)
tcSpliceExpr :: HsSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
tcTypedBracket :: HsExpr GhcRn
-> HsBracket GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcTypedBracket rn_expr :: HsExpr GhcRn
rn_expr brack :: HsBracket GhcRn
brack@(TExpBr _ expr :: LHsExpr GhcRn
expr) res_ty :: ExpRhoType
res_ty
= MsgDoc -> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (HsBracket GhcRn -> MsgDoc
quotationCtxtDoc HsBracket GhcRn
brack) (TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId))
-> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
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
; (_tc_expr :: LHsExpr GhcTcId
_tc_expr, expr_ty :: TcRhoType
expr_ty) <- ThStage
-> TcM (LHsExpr GhcTcId, TcRhoType)
-> TcM (LHsExpr GhcTcId, TcRhoType)
forall a. ThStage -> TcM a -> TcM a
setStage (ThStage -> PendingStuff -> ThStage
Brack ThStage
cur_stage (IORef [PendingTcSplice] -> TcRef WantedConstraints -> PendingStuff
TcPending IORef [PendingTcSplice]
ps_ref TcRef WantedConstraints
lie_var)) (TcM (LHsExpr GhcTcId, TcRhoType)
-> TcM (LHsExpr GhcTcId, TcRhoType))
-> TcM (LHsExpr GhcTcId, TcRhoType)
-> TcM (LHsExpr GhcTcId, TcRhoType)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> TcM (LHsExpr GhcTcId, TcRhoType)
tcInferRhoNC LHsExpr GhcRn
expr
; TcRhoType
meta_ty <- TcRhoType -> TcM TcRhoType
tcTExpTy TcRhoType
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 -> TcM Id
tcLookupId Name
unsafeTExpCoerceName
; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTcId
-> TcRhoType
-> ExpRhoType
-> TcM (HsExpr GhcTcId)
tcWrapResultO (String -> CtOrigin
Shouldn'tHappenOrigin "TExpBr")
HsExpr GhcRn
rn_expr
(LHsExpr GhcTcId -> SrcSpanLess (LHsExpr GhcTcId)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (LHsExpr GhcTcId -> LHsExpr GhcTcId -> LHsExpr GhcTcId
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkHsApp (IdP GhcTcId -> [TcRhoType] -> LHsExpr GhcTcId
forall (id :: Pass).
IdP (GhcPass id) -> [TcRhoType] -> LHsExpr (GhcPass id)
nlHsTyApp Id
IdP GhcTcId
texpco [TcRhoType
expr_ty])
(SrcSpanLess (LHsExpr GhcTcId) -> LHsExpr GhcTcId
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XTcBracketOut GhcTcId
-> HsBracket GhcRn -> [PendingTcSplice] -> HsExpr GhcTcId
forall p.
XTcBracketOut p -> HsBracket GhcRn -> [PendingTcSplice] -> HsExpr p
HsTcBracketOut XTcBracketOut GhcTcId
NoExt
noExt HsBracket GhcRn
brack [PendingTcSplice]
ps'))))
TcRhoType
meta_ty ExpRhoType
res_ty }
tcTypedBracket _ other_brack :: HsBracket GhcRn
other_brack _
= String -> MsgDoc -> TcM (HsExpr GhcTcId)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "tcTypedBracket" (HsBracket GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsBracket GhcRn
other_brack)
tcUntypedBracket :: HsExpr GhcRn
-> HsBracket GhcRn
-> [PendingRnSplice]
-> ExpRhoType
-> TcM (HsExpr GhcTcId)
tcUntypedBracket rn_expr :: HsExpr GhcRn
rn_expr brack :: HsBracket GhcRn
brack ps :: [PendingRnSplice]
ps res_ty :: ExpRhoType
res_ty
= do { String -> MsgDoc -> TcRn ()
traceTc "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)
; [PendingTcSplice]
ps' <- (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 PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice [PendingRnSplice]
ps
; TcRhoType
meta_ty <- HsBracket GhcRn -> TcM TcRhoType
tcBrackTy HsBracket GhcRn
brack
; String -> MsgDoc -> TcRn ()
traceTc "tc_bracket done untyped" (TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TcRhoType
meta_ty)
; CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTcId
-> TcRhoType
-> ExpRhoType
-> TcM (HsExpr GhcTcId)
tcWrapResultO (String -> CtOrigin
Shouldn'tHappenOrigin "untyped bracket")
HsExpr GhcRn
rn_expr (XTcBracketOut GhcTcId
-> HsBracket GhcRn -> [PendingTcSplice] -> HsExpr GhcTcId
forall p.
XTcBracketOut p -> HsBracket GhcRn -> [PendingTcSplice] -> HsExpr p
HsTcBracketOut XTcBracketOut GhcTcId
NoExt
noExt HsBracket GhcRn
brack [PendingTcSplice]
ps') TcRhoType
meta_ty ExpRhoType
res_ty }
tcBrackTy :: HsBracket GhcRn -> TcM TcType
tcBrackTy :: HsBracket GhcRn -> TcM TcRhoType
tcBrackTy (VarBr {}) = Name -> TcM TcRhoType
tcMetaTy Name
nameTyConName
tcBrackTy (ExpBr {}) = Name -> TcM TcRhoType
tcMetaTy Name
expQTyConName
tcBrackTy (TypBr {}) = Name -> TcM TcRhoType
tcMetaTy Name
typeQTyConName
tcBrackTy (DecBrG {}) = Name -> TcM TcRhoType
tcMetaTy Name
decsQTyConName
tcBrackTy (PatBr {}) = Name -> TcM TcRhoType
tcMetaTy Name
patQTyConName
tcBrackTy (DecBrL {}) = String -> TcM TcRhoType
forall a. String -> a
panic "tcBrackTy: Unexpected DecBrL"
tcBrackTy (TExpBr {}) = String -> TcM TcRhoType
forall a. String -> a
panic "tcUntypedBracket: Unexpected TExpBr"
tcBrackTy (XBracket {}) = String -> TcM TcRhoType
forall a. String -> a
panic "tcUntypedBracket: Unexpected XBracket"
tcPendingSplice :: PendingRnSplice -> TcM PendingTcSplice
tcPendingSplice :: PendingRnSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
tcPendingSplice (PendingRnSplice flavour :: UntypedSpliceFlavour
flavour splice_name :: Name
splice_name expr :: LHsExpr GhcRn
expr)
= do { TcRhoType
res_ty <- Name -> TcM TcRhoType
tcMetaTy Name
meta_ty_name
; LHsExpr GhcTcId
expr' <- LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTcId)
tcMonoExpr LHsExpr GhcRn
expr (TcRhoType -> ExpRhoType
mkCheckExpType TcRhoType
res_ty)
; PendingTcSplice -> IOEnv (Env TcGblEnv TcLclEnv) PendingTcSplice
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> LHsExpr GhcTcId -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTcId
expr') }
where
meta_ty_name :: Name
meta_ty_name = case UntypedSpliceFlavour
flavour of
UntypedExpSplice -> Name
expQTyConName
UntypedPatSplice -> Name
patQTyConName
UntypedTypeSplice -> Name
typeQTyConName
UntypedDeclSplice -> Name
decsQTyConName
tcTExpTy :: TcType -> TcM TcType
tcTExpTy :: TcRhoType -> TcM TcRhoType
tcTExpTy exp_ty :: TcRhoType
exp_ty
= do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (TcRhoType -> Bool
isTauTy TcRhoType
exp_ty) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ MsgDoc -> TcRn ()
addErr (TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
err_msg TcRhoType
exp_ty)
; TyCon
q <- Name -> TcM TyCon
tcLookupTyCon Name
qTyConName
; TyCon
texp <- Name -> TcM TyCon
tcLookupTyCon Name
tExpTyConName
; TcRhoType -> TcM TcRhoType
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> [TcRhoType] -> TcRhoType
mkTyConApp TyCon
q [TyCon -> [TcRhoType] -> TcRhoType
mkTyConApp TyCon
texp [TcRhoType
exp_ty]]) }
where
err_msg :: a -> MsgDoc
err_msg ty :: a
ty
= [MsgDoc] -> MsgDoc
vcat [ String -> MsgDoc
text "Illegal polytype:" MsgDoc -> MsgDoc -> MsgDoc
<+> a -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr a
ty
, String -> MsgDoc
text "The type of a Typed Template Haskell expression must" MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text "not have any quantification." ]
quotationCtxtDoc :: HsBracket GhcRn -> SDoc
quotationCtxtDoc :: HsBracket GhcRn -> MsgDoc
quotationCtxtDoc br_body :: HsBracket GhcRn
br_body
= MsgDoc -> Int -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text "In the Template Haskell quotation")
2 (HsBracket GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsBracket GhcRn
br_body)
tcSpliceExpr :: HsSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcSpliceExpr splice :: HsSplice GhcRn
splice@(HsTypedSplice _ _ name :: IdP GhcRn
name expr :: LHsExpr GhcRn
expr) res_ty :: ExpRhoType
res_ty
= MsgDoc -> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (HsSplice GhcRn -> MsgDoc
spliceCtxtDoc HsSplice GhcRn
splice) (TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId))
-> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
SrcSpan -> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (LHsExpr GhcRn -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc LHsExpr GhcRn
expr) (TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId))
-> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$ do
{ ThStage
stage <- TcM ThStage
getStage
; case ThStage
stage of
Splice {} -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
Brack pop_stage :: ThStage
pop_stage pend :: PendingStuff
pend -> ThStage
-> PendingStuff
-> Name
-> LHsExpr GhcRn
-> ExpRhoType
-> TcM (HsExpr GhcTcId)
tcNestedSplice ThStage
pop_stage PendingStuff
pend Name
IdP GhcRn
name LHsExpr GhcRn
expr ExpRhoType
res_ty
RunSplice _ ->
String -> MsgDoc -> TcM (HsExpr GhcTcId)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic ("tcSpliceExpr: attempted to typecheck a splice when " String -> String -> String
forall a. [a] -> [a] -> [a]
++
"running another splice") (HsSplice GhcRn -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr HsSplice GhcRn
splice)
Comp -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTcId)
tcTopSplice LHsExpr GhcRn
expr ExpRhoType
res_ty
}
tcSpliceExpr splice :: HsSplice GhcRn
splice _
= String -> MsgDoc -> TcM (HsExpr GhcTcId)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "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 GhcTcId)
tcNestedSplice pop_stage :: ThStage
pop_stage (TcPending ps_var :: IORef [PendingTcSplice]
ps_var lie_var :: TcRef WantedConstraints
lie_var) splice_name :: Name
splice_name expr :: LHsExpr GhcRn
expr res_ty :: ExpRhoType
res_ty
= do { TcRhoType
res_ty <- ExpRhoType -> TcM TcRhoType
expTypeToType ExpRhoType
res_ty
; TcRhoType
meta_exp_ty <- TcRhoType -> TcM TcRhoType
tcTExpTy TcRhoType
res_ty
; LHsExpr GhcTcId
expr' <- ThStage -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a. ThStage -> TcM a -> TcM a
setStage ThStage
pop_stage (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
TcRef WantedConstraints
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a. TcRef WantedConstraints -> TcM a -> TcM a
setConstraintVar TcRef WantedConstraints
lie_var (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTcId)
tcMonoExpr LHsExpr GhcRn
expr (TcRhoType -> ExpRhoType
mkCheckExpType TcRhoType
meta_exp_ty)
; Id
untypeq <- Name -> TcM Id
tcLookupId Name
unTypeQName
; let expr'' :: LHsExpr GhcTcId
expr'' = LHsExpr GhcTcId -> LHsExpr GhcTcId -> LHsExpr GhcTcId
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkHsApp (IdP GhcTcId -> [TcRhoType] -> LHsExpr GhcTcId
forall (id :: Pass).
IdP (GhcPass id) -> [TcRhoType] -> LHsExpr (GhcPass id)
nlHsTyApp Id
IdP GhcTcId
untypeq [TcRhoType
res_ty]) LHsExpr GhcTcId
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 GhcTcId -> PendingTcSplice
PendingTcSplice Name
splice_name LHsExpr GhcTcId
expr'' PendingTcSplice -> [PendingTcSplice] -> [PendingTcSplice]
forall a. a -> [a] -> [a]
: [PendingTcSplice]
ps)
; HsExpr GhcTcId -> TcM (HsExpr GhcTcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> HsExpr GhcTcId
forall a. String -> a
panic "tcSpliceExpr") }
tcNestedSplice _ _ splice_name :: Name
splice_name _ _
= String -> MsgDoc -> TcM (HsExpr GhcTcId)
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "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 GhcTcId)
tcTopSplice expr :: LHsExpr GhcRn
expr res_ty :: ExpRhoType
res_ty
= do {
TcRhoType
res_ty <- ExpRhoType -> TcM TcRhoType
expTypeToType ExpRhoType
res_ty
; TcRhoType
meta_exp_ty <- TcRhoType -> TcM TcRhoType
tcTExpTy TcRhoType
res_ty
; LHsExpr GhcTcId
q_expr <- SpliceType -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
tcTopSpliceExpr SpliceType
Typed (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTcId)
tcMonoExpr LHsExpr GhcRn
expr (TcRhoType -> ExpRhoType
mkCheckExpType TcRhoType
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 -> TcRhoType -> LHsExpr GhcTcId -> DelayedSplice
DelayedSplice TcLclEnv
lcl_env LHsExpr GhcRn
expr TcRhoType
res_ty LHsExpr GhcTcId
q_expr
; HsExpr GhcTcId -> TcM (HsExpr GhcTcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (XSpliceE GhcTcId -> HsSplice GhcTcId -> HsExpr GhcTcId
forall p. XSpliceE p -> HsSplice p -> HsExpr p
HsSpliceE XSpliceE GhcTcId
NoExt
noExt (DelayedSplice -> HsSplice GhcTcId
forall id. DelayedSplice -> HsSplice id
HsSplicedT DelayedSplice
delayed_splice))
}
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTc)
runTopSplice :: DelayedSplice -> TcM (HsExpr GhcTcId)
runTopSplice (DelayedSplice lcl_env :: TcLclEnv
lcl_env orig_expr :: LHsExpr GhcRn
orig_expr res_ty :: TcRhoType
res_ty q_expr :: LHsExpr GhcTcId
q_expr)
= TcLclEnv -> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall lcl' gbl a lcl.
lcl' -> TcRnIf gbl lcl' a -> TcRnIf gbl lcl a
setLclEnv TcLclEnv
lcl_env (TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId))
-> TcM (HsExpr GhcTcId) -> TcM (HsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$ do {
TcRhoType
zonked_ty <- TcRhoType -> TcM TcRhoType
zonkTcType TcRhoType
res_ty
; LHsExpr GhcTcId
zonked_q_expr <- LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId)
zonkTopLExpr LHsExpr GhcTcId
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 GhcTcId -> TcM (LHsExpr GhcPs)
runMetaE LHsExpr GhcTcId
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 = "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 })
; (res :: LHsExpr GhcTcId
res, wcs :: WantedConstraints
wcs) <-
TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId, WantedConstraints))
-> TcM (LHsExpr GhcTcId)
-> TcM (LHsExpr GhcTcId, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
MsgDoc -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (LHsExpr GhcTcId -> MsgDoc
spliceResultDoc LHsExpr GhcTcId
zonked_q_expr) (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$ do
{ (exp3 :: LHsExpr GhcRn
exp3, _fvs :: FreeVars
_fvs) <- LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
expr2
; LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTcId)
tcMonoExpr LHsExpr GhcRn
exp3 (TcRhoType -> ExpRhoType
mkCheckExpType TcRhoType
zonked_ty)}
; Bag EvBind
ev <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wcs
; HsExpr GhcTcId -> TcM (HsExpr GhcTcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTcId -> TcM (HsExpr GhcTcId))
-> HsExpr GhcTcId -> TcM (HsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcTcId -> SrcSpanLess (LHsExpr GhcTcId)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (TcEvBinds -> LHsExpr GhcTcId -> LHsExpr GhcTcId
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
ev) LHsExpr GhcTcId
res)
}
spliceCtxtDoc :: HsSplice GhcRn -> SDoc
spliceCtxtDoc :: HsSplice GhcRn -> MsgDoc
spliceCtxtDoc splice :: HsSplice GhcRn
splice
= MsgDoc -> Int -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text "In the Template Haskell splice")
2 (HsSplice GhcRn -> MsgDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
HsSplice (GhcPass p) -> MsgDoc
pprSplice HsSplice GhcRn
splice)
spliceResultDoc :: LHsExpr GhcTc -> SDoc
spliceResultDoc :: LHsExpr GhcTcId -> MsgDoc
spliceResultDoc expr :: LHsExpr GhcTcId
expr
= [MsgDoc] -> MsgDoc
sep [ String -> MsgDoc
text "In the result of the splice:"
, Int -> MsgDoc -> MsgDoc
nest 2 (Char -> MsgDoc
char '$' MsgDoc -> MsgDoc -> MsgDoc
<> LHsExpr GhcTcId -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTcId
expr)
, String -> MsgDoc
text "To see what the splice expanded to, use -ddump-splices"]
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTc) -> TcM (LHsExpr GhcTc)
tcTopSpliceExpr :: SpliceType -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
tcTopSpliceExpr isTypedSplice :: SpliceType
isTypedSplice tc_action :: TcM (LHsExpr GhcTcId)
tc_action
= TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall r. TcM r -> TcM r
checkNoErrs (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
GeneralFlag -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall gbl lcl a.
GeneralFlag -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
unsetGOptM GeneralFlag
Opt_DeferTypeErrors (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
ThStage -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a. ThStage -> TcM a -> TcM a
setStage (SpliceType -> ThStage
Splice SpliceType
isTypedSplice) (TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
do {
(expr' :: LHsExpr GhcTcId
expr', wanted :: WantedConstraints
wanted) <- TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints TcM (LHsExpr GhcTcId)
tc_action
; Bag EvBind
const_binds <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wanted
; LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId))
-> LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$ TcEvBinds -> LHsExpr GhcTcId -> LHsExpr GhcTcId
mkHsDictLet (Bag EvBind -> TcEvBinds
EvBinds Bag EvBind
const_binds) LHsExpr GhcTcId
expr' }
runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
runAnnotation target :: CoreAnnTarget
target expr :: LHsExpr GhcRn
expr = do
SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
Class
data_class <- Name -> TcM Class
tcLookupClass Name
dataClassName
Id
to_annotation_wrapper_id <- Name -> TcM Id
tcLookupId Name
toAnnotationWrapperName
LHsExpr GhcTcId
zonked_wrapped_expr' <- LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId)
zonkTopLExpr (LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId))
-> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SpliceType -> TcM (LHsExpr GhcTcId) -> TcM (LHsExpr GhcTcId)
tcTopSpliceExpr SpliceType
Untyped (
do { (expr' :: LHsExpr GhcTcId
expr', expr_ty :: TcRhoType
expr_ty) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTcId, TcRhoType)
tcInferRhoNC LHsExpr GhcRn
expr
; HsWrapper
wrapper <- CtOrigin -> [TcRhoType] -> [TcRhoType] -> TcM HsWrapper
instCall CtOrigin
AnnOrigin [TcRhoType
expr_ty] [Class -> [TcRhoType] -> TcRhoType
mkClassPred Class
data_class [TcRhoType
expr_ty]]
; let specialised_to_annotation_wrapper_expr :: LHsExpr GhcTcId
specialised_to_annotation_wrapper_expr
= SrcSpan -> HsExpr GhcTcId -> LHsExpr GhcTcId
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (HsWrapper -> HsExpr GhcTcId -> HsExpr GhcTcId
forall (id :: Pass).
HsWrapper -> HsExpr (GhcPass id) -> HsExpr (GhcPass id)
mkHsWrap HsWrapper
wrapper
(XVar GhcTcId -> Located (IdP GhcTcId) -> HsExpr GhcTcId
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar XVar GhcTcId
NoExt
noExt (SrcSpan -> Id -> GenLocated SrcSpan Id
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc Id
to_annotation_wrapper_id)))
; LHsExpr GhcTcId -> TcM (LHsExpr GhcTcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> HsExpr GhcTcId -> LHsExpr GhcTcId
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XApp GhcTcId
-> LHsExpr GhcTcId -> LHsExpr GhcTcId -> HsExpr GhcTcId
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp XApp GhcTcId
NoExt
noExt
LHsExpr GhcTcId
specialised_to_annotation_wrapper_expr LHsExpr GhcTcId
expr'))
})
Serialized
serialized <- LHsExpr GhcTcId -> TcM Serialized
runMetaAW LHsExpr GhcTcId
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 fhv :: ForeignHValue
fhv = do
DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalInterpreter DynFlags
dflags
then do
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
else 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
$ DynFlags -> ForeignHValue -> IO HValue
forall a. DynFlags -> ForeignRef a -> IO a
wormhole DynFlags
dflags 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
unsafeCoerce# HValue
annotation_wrapper of
AnnotationWrapper value :: a
value | let serialized :: Serialized
serialized = (a -> [Word8]) -> a -> Serialized
forall a. Typeable a => (a -> [Word8]) -> a -> Serialized
toSerialized a -> [Word8]
forall a. Data a => a -> [Word8]
serializeWithData a
value ->
Serialized -> ()
seqSerialized Serialized
serialized () -> Serialized -> Serialized
forall a a. a -> a -> a
`seq` Serialized
serialized
seqSerialized :: Serialized -> ()
seqSerialized :: Serialized -> ()
seqSerialized (Serialized the_type :: TypeRep
the_type bytes :: [Word8]
bytes) = TypeRep
the_type TypeRep -> () -> ()
forall a a. a -> a -> a
`seq` [Word8]
bytes [Word8] -> () -> ()
forall a b. [a] -> b -> b
`seqList` ()
runQuasi :: TH.Q a -> TcM a
runQuasi :: Q a -> TcM a
runQuasi act :: Q a
act = Q a -> TcM a
forall (m :: * -> *) a. Quasi m => Q a -> m a
TH.runQ Q a
act
runRemoteModFinalizers :: ThModFinalizers -> TcM ()
runRemoteModFinalizers :: ThModFinalizers -> TcRn ()
runRemoteModFinalizers (ThModFinalizers finRefs :: [ForeignRef (Q ())]
finRefs) = do
DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let withForeignRefs :: [ForeignRef a] -> ([RemoteRef a] -> IO b) -> IO b
withForeignRefs [] f :: [RemoteRef a] -> IO b
f = [RemoteRef a] -> IO b
f []
withForeignRefs (x :: ForeignRef a
x : xs :: [ForeignRef a]
xs) f :: [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
$ \r :: 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
$ \rs :: [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)
if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalInterpreter DynFlags
dflags then do
HscEnv
hsc_env <- Env TcGblEnv TcLclEnv -> HscEnv
forall gbl lcl. Env gbl lcl -> HscEnv
env_top (Env TcGblEnv TcLclEnv -> HscEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
getEnv
HscEnv -> (IServ -> TcRn ()) -> TcRn ()
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
HscEnv -> (IServ -> m a) -> m a
withIServ HscEnv
hsc_env ((IServ -> TcRn ()) -> TcRn ()) -> (IServ -> TcRn ()) -> TcRn ()
forall a b. (a -> b) -> a -> b
$ \i :: IServ
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
Nothing -> () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just fhv :: 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
$ \st :: 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
$ \qrefs :: [RemoteRef (Q ())]
qrefs ->
IServ -> Put -> IO ()
writeIServ IServ
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))
() <- IServ -> [Messages] -> TcRn ()
runRemoteTH IServ
i []
IServ -> TcRn ()
forall a. Binary a => IServ -> TcM a
readQResult IServ
i
else 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
runQResult
:: (a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult :: (a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult show_th :: a -> String
show_th f :: SrcSpan -> a -> b
f runQ :: ForeignHValue -> TcM a
runQ expr_span :: SrcSpan
expr_span hval :: ForeignHValue
hval
= do { a
th_result <- ForeignHValue -> TcM a
runQ ForeignHValue
hval
; String -> MsgDoc -> TcRn ()
traceTc "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 (SrcSpan -> a -> b
f SrcSpan
expr_span a
th_result) }
runMeta :: (MetaHook TcM -> LHsExpr GhcTc -> TcM hs_syn)
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta :: (MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta unwrap :: MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn
unwrap e :: LHsExpr GhcTcId
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 GhcTcId -> TcM hs_syn
unwrap MetaHook TcM
h LHsExpr GhcTcId
e }
defaultRunMeta :: MetaHook TcM
defaultRunMeta :: MetaHook TcM
defaultRunMeta (MetaE r :: 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 GhcTcId -> TcM (LHsExpr GhcPs))
-> LHsExpr GhcTcId
-> 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 GhcTcId
-> TcM (LHsExpr GhcPs)
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> TcM hs_syn
runMeta' Bool
True LHsExpr GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Exp -> String)
-> (SrcSpan -> Exp -> Either MsgDoc (LHsExpr GhcPs))
-> (ForeignHValue -> TcM Exp)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (LHsExpr GhcPs))
forall a b.
(a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Exp -> String
forall a. Ppr a => a -> String
TH.pprint SrcSpan -> Exp -> Either MsgDoc (LHsExpr GhcPs)
convertToHsExpr ForeignHValue -> TcM Exp
runTHExp)
defaultRunMeta (MetaP r :: LPat GhcPs -> MetaResult
r)
= (LPat GhcPs -> MetaResult)
-> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LPat GhcPs -> MetaResult
r (IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult)
-> (LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs))
-> LHsExpr GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) MetaResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> (LPat GhcPs -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc (LPat GhcPs)))
-> LHsExpr GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> TcM hs_syn
runMeta' Bool
True LPat GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Pat -> String)
-> (SrcSpan -> Pat -> Either MsgDoc (LPat GhcPs))
-> (ForeignHValue -> TcM Pat)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (LPat GhcPs))
forall a b.
(a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Pat -> String
forall a. Ppr a => a -> String
TH.pprint SrcSpan -> Pat -> Either MsgDoc (LPat GhcPs)
convertToPat ForeignHValue -> TcM Pat
runTHPat)
defaultRunMeta (MetaT r :: 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 GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs))
-> LHsExpr GhcTcId
-> 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 GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> TcM hs_syn
runMeta' Bool
True LHsType GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr ((Type -> String)
-> (SrcSpan -> Type -> Either MsgDoc (LHsType GhcPs))
-> (ForeignHValue -> TcM Type)
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc (LHsType GhcPs))
forall a b.
(a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult Type -> String
forall a. Ppr a => a -> String
TH.pprint SrcSpan -> Type -> Either MsgDoc (LHsType GhcPs)
convertToHsType ForeignHValue -> TcM Type
runTHType)
defaultRunMeta (MetaD r :: [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 GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs])
-> LHsExpr GhcTcId
-> 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 GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> TcM hs_syn
runMeta' Bool
True [LHsDecl GhcPs] -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr (([Dec] -> String)
-> (SrcSpan -> [Dec] -> Either MsgDoc [LHsDecl GhcPs])
-> (ForeignHValue -> TcM [Dec])
-> SrcSpan
-> ForeignHValue
-> TcM (Either MsgDoc [LHsDecl GhcPs])
forall a b.
(a -> String)
-> (SrcSpan -> a -> b)
-> (ForeignHValue -> TcM a)
-> SrcSpan
-> ForeignHValue
-> TcM b
runQResult [Dec] -> String
forall a. Ppr a => a -> String
TH.pprint SrcSpan -> [Dec] -> Either MsgDoc [LHsDecl GhcPs]
convertToHsDecls ForeignHValue -> TcM [Dec]
runTHDec)
defaultRunMeta (MetaAW r :: 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 GhcTcId -> TcM Serialized)
-> LHsExpr GhcTcId
-> 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 GhcTcId
-> TcM Serialized
forall hs_syn.
Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> 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 GhcTcId -> TcM Serialized
runMetaAW = (MetaHook TcM -> LHsExpr GhcTcId -> TcM Serialized)
-> LHsExpr GhcTcId -> TcM Serialized
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta MetaHook TcM -> LHsExpr GhcTcId -> TcM Serialized
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTcId -> f Serialized
metaRequestAW
runMetaE :: LHsExpr GhcTc
-> TcM (LHsExpr GhcPs)
runMetaE :: LHsExpr GhcTcId -> TcM (LHsExpr GhcPs)
runMetaE = (MetaHook TcM -> LHsExpr GhcTcId -> TcM (LHsExpr GhcPs))
-> LHsExpr GhcTcId -> TcM (LHsExpr GhcPs)
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta MetaHook TcM -> LHsExpr GhcTcId -> TcM (LHsExpr GhcPs)
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTcId -> f (LHsExpr GhcPs)
metaRequestE
runMetaP :: LHsExpr GhcTc
-> TcM (LPat GhcPs)
runMetaP :: LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
runMetaP = (MetaHook TcM
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs))
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LPat GhcPs)
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTcId -> f (LPat GhcPs)
metaRequestP
runMetaT :: LHsExpr GhcTc
-> TcM (LHsType GhcPs)
runMetaT :: LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
runMetaT = (MetaHook TcM
-> LHsExpr GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs))
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTcId -> f (LHsType GhcPs)
metaRequestT
runMetaD :: LHsExpr GhcTc
-> TcM [LHsDecl GhcPs]
runMetaD :: LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
runMetaD = (MetaHook TcM
-> LHsExpr GhcTcId
-> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs])
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall hs_syn.
(MetaHook TcM -> LHsExpr GhcTcId -> TcM hs_syn)
-> LHsExpr GhcTcId -> TcM hs_syn
runMeta MetaHook TcM
-> LHsExpr GhcTcId -> IOEnv (Env TcGblEnv TcLclEnv) [LHsDecl GhcPs]
forall (f :: * -> *).
Functor f =>
MetaHook f -> LHsExpr GhcTcId -> f [LHsDecl GhcPs]
metaRequestD
runMeta' :: Bool
-> (hs_syn -> SDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTc
-> TcM hs_syn
runMeta' :: Bool
-> (hs_syn -> MsgDoc)
-> (SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn))
-> LHsExpr GhcTcId
-> TcM hs_syn
runMeta' show_code :: Bool
show_code ppr_hs :: hs_syn -> MsgDoc
ppr_hs run_and_convert :: SrcSpan -> ForeignHValue -> TcM (Either MsgDoc hs_syn)
run_and_convert expr :: LHsExpr GhcTcId
expr
= do { String -> MsgDoc -> TcRn ()
traceTc "About to run" (LHsExpr GhcTcId -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTcId
expr)
; TcRn ()
recordThSpliceUse
; TcRn ()
failIfErrsM
; HscEnv
hsc_env <- IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
; LHsExpr GhcTcId
expr' <- DynFlags
-> PluginOperation TcM (LHsExpr GhcTcId)
-> LHsExpr GhcTcId
-> TcM (LHsExpr GhcTcId)
forall (m :: * -> *) a.
Monad m =>
DynFlags -> PluginOperation m a -> a -> m a
withPlugins (HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env) PluginOperation TcM (LHsExpr GhcTcId)
spliceRunAction LHsExpr GhcTcId
expr
; CoreExpr
ds_expr <- DsM CoreExpr -> TcM CoreExpr
forall a. DsM a -> TcM a
initDsTc (LHsExpr GhcTcId -> DsM CoreExpr
dsLExpr LHsExpr GhcTcId
expr')
; SrcSpan
src_span <- TcRn SrcSpan
getSrcSpanM
; String -> MsgDoc -> TcRn ()
traceTc "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
HscMain.hscCompileCoreExpr HscEnv
hsc_env SrcSpan
src_span CoreExpr
ds_expr
; case Either IOEnvFailure ForeignHValue
either_hval of {
Left exn :: IOEnvFailure
exn -> String -> IOEnvFailure -> TcM hs_syn
forall e a. Exception e => String -> e -> TcM a
fail_with_exn "compile and link" IOEnvFailure
exn ;
Right hval :: ForeignHValue
hval -> do
{
let expr_span :: SrcSpan
expr_span = LHsExpr GhcTcId -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc LHsExpr GhcTcId
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 err :: MsgDoc
err -> MsgDoc -> TcM hs_syn
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
err
Right result :: hs_syn
result -> do { String -> MsgDoc -> TcRn ()
traceTc "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 v :: hs_syn
v -> hs_syn -> TcM hs_syn
forall (m :: * -> *) a. Monad m => a -> m a
return hs_syn
v
Left se :: SomeException
se -> case SomeException -> Maybe IOEnvFailure
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se of
Just IOEnvFailure -> TcM hs_syn
forall env a. IOEnv env a
failM
_ -> String -> SomeException -> TcM hs_syn
forall e a. Exception e => String -> e -> TcM a
fail_with_exn "run" SomeException
se
}}}
where
fail_with_exn :: Exception e => String -> e -> TcM a
fail_with_exn :: String -> e -> TcM a
fail_with_exn phase :: String
phase exn :: 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 "Exception when trying to" MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text String
phase MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text "compile-time code:",
Int -> MsgDoc -> MsgDoc
nest 2 (String -> MsgDoc
text String
exn_msg),
if Bool
show_code then String -> MsgDoc
text "Code:" MsgDoc -> MsgDoc -> MsgDoc
<+> LHsExpr GhcTcId -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr LHsExpr GhcTcId
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 s :: String
s = do { Unique
u <- TcRnIf TcGblEnv TcLclEnv Unique
forall gbl lcl. TcRnIf gbl lcl Unique
newUnique
; let i :: Int
i = Unique -> Int
getKey Unique
u
; Name -> TcM Name
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Int -> Name
TH.mkNameU String
s Int
i) }
qReport :: Bool -> String -> TcRn ()
qReport True msg :: 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 False msg :: 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 { Module
m <- IOEnv (Env TcGblEnv TcLclEnv) Module
forall (m :: * -> *). HasModule m => m Module
getModule
; SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
; RealSrcSpan
r <- case SrcSpan
l of
UnhelpfulSpan _ -> String -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) RealSrcSpan
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "qLocation: Unhelpful location"
(SrcSpan -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr SrcSpan
l)
RealSrcSpan s :: RealSrcSpan
s -> 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 (Module -> ModuleName
moduleName Module
m)
, loc_package :: String
TH.loc_package = UnitId -> String
unitIdString (Module -> UnitId
moduleUnitId Module
m)
, loc_start :: CharPos
TH.loc_start = (RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
r, RealSrcSpan -> Int
srcSpanStartCol RealSrcSpan
r)
, loc_end :: CharPos
TH.loc_end = (RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
r, RealSrcSpan -> Int
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 nm :: 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
qReifyInstances :: Name -> [Type] -> TcM [Dec]
qReifyInstances = Name -> [Type] -> TcM [Dec]
reifyInstances
qReifyRoles :: Name -> TcM [Role]
qReifyRoles = Name -> TcM [Role]
reifyRoles
qReifyAnnotations :: 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 nm :: 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 :: TcM a -> TcM a -> TcM a
qRecover recover :: TcM a
recover main :: 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 fp :: 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 suffix :: 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 thds :: [Dec]
thds = do
SrcSpan
l <- TcRn SrcSpan
getSrcSpanM
let either_hval :: Either MsgDoc [LHsDecl GhcPs]
either_hval = SrcSpan -> [Dec] -> Either MsgDoc [LHsDecl GhcPs]
convertToHsDecls SrcSpan
l [Dec]
thds
[LHsDecl GhcPs]
ds <- case Either MsgDoc [LHsDecl GhcPs]
either_hval of
Left exn :: 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 -> Int -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text "Error in a declaration passed to addTopDecls:")
2 MsgDoc
exn
Right ds :: [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 a. HasSrcSpan a => a -> SrcSpanLess a
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 (\topds :: [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 _ binds :: 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.
(SrcSpanLess (LPat p) ~ LPat p, HasSrcSpan (LPat p)) =>
HsBindLR p idR -> [IdP p]
collectHsBindBinders HsBind GhcPs
binds)
checkTopDecl (SigD _ _)
= () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (AnnD _ _)
= () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTopDecl (ForD _ (ForeignImport { fd_name :: forall pass. ForeignDecl pass -> Located (IdP pass)
fd_name = L _ name :: IdP GhcPs
name }))
= RdrName -> TcRn ()
bindName RdrName
IdP GhcPs
name
checkTopDecl _
= MsgDoc -> TcRn ()
addErr (MsgDoc -> TcRn ()) -> MsgDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$ String -> MsgDoc
text "Only function, value, annotation, and foreign import declarations may be added with addTopDecl"
bindName :: RdrName -> TcM ()
bindName :: RdrName -> TcRn ()
bindName (Exact n :: 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 (\ns :: FreeVars
ns -> FreeVars -> Name -> FreeVars
extendNameSet FreeVars
ns Name
n)
}
bindName name :: RdrName
name =
MsgDoc -> TcRn ()
addErr (MsgDoc -> TcRn ()) -> MsgDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
MsgDoc -> Int -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text "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 "is not a NameU."))
2 (String -> MsgDoc
text "Probable cause: you used mkName instead of newName to generate a binding.")
qAddForeignFilePath :: ForeignSrcLang -> String -> TcRn ()
qAddForeignFilePath lang :: ForeignSrcLang
lang fp :: 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 fin :: 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 plugin :: String
plugin = do
HscEnv
hsc_env <- Env TcGblEnv TcLclEnv -> HscEnv
forall gbl lcl. Env gbl lcl -> HscEnv
env_top (Env TcGblEnv TcLclEnv -> HscEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
getEnv
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 -> Int -> MsgDoc -> MsgDoc
hang
(String -> MsgDoc
text "addCorePlugin: invalid plugin module "
MsgDoc -> MsgDoc -> MsgDoc
<+> String -> MsgDoc
text (String -> String
forall a. Show a => a -> String
show String
plugin)
)
2
(String -> MsgDoc
text "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
_ -> () -> 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 :: 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 :: a -> TcRn ()
qPutQ x :: 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 (\m :: Map TypeRep Dynamic
m -> TypeRep -> Dynamic -> Map TypeRep Dynamic -> Map TypeRep Dynamic
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (a -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf a
x) (a -> Dynamic
forall a. Typeable a => a -> Dynamic
toDyn a
x) Map TypeRep Dynamic
m)
qIsExtEnabled :: Extension -> TcM Bool
qIsExtEnabled = Extension -> TcM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM
qExtsEnabled :: TcM [Extension]
qExtsEnabled =
EnumSet Extension -> [Extension]
forall a. Enum a => EnumSet a -> [a]
EnumSet.toList (EnumSet Extension -> [Extension])
-> (HscEnv -> EnumSet Extension) -> HscEnv -> [Extension]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> EnumSet Extension
extensionFlags (DynFlags -> EnumSet Extension)
-> (HscEnv -> DynFlags) -> HscEnv -> EnumSet Extension
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> DynFlags
hsc_dflags (HscEnv -> [Extension])
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv -> TcM [Extension]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM ()
addModFinalizerRef :: ForeignRef (Q ()) -> TcRn ()
addModFinalizerRef finRef :: ForeignRef (Q ())
finRef = do
ThStage
th_stage <- TcM ThStage
getStage
case ThStage
th_stage of
RunSplice th_modfinalizers_var :: 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]
:)
_ ->
String -> MsgDoc -> TcRn ()
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "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
DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalInterpreter DynFlags
dflags) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ 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 :: THResultType -> ForeignHValue -> TcM a
runTH ty :: THResultType
ty fhv :: ForeignHValue
fhv = do
HscEnv
hsc_env <- Env TcGblEnv TcLclEnv -> HscEnv
forall gbl lcl. Env gbl lcl -> HscEnv
env_top (Env TcGblEnv TcLclEnv -> HscEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
getEnv
DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
if Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalInterpreter DynFlags
dflags)
then 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
$ DynFlags -> ForeignHValue -> IO HValue
forall a. DynFlags -> ForeignRef a -> IO a
wormhole DynFlags
dflags ForeignHValue
fhv
a
r <- Q a -> TcM a
forall a. Q a -> TcM a
runQuasi (HValue -> Q a
unsafeCoerce# HValue
hv :: TH.Q a)
a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
else
HscEnv -> (IServ -> TcM a) -> TcM a
forall (m :: * -> *) a.
(MonadIO m, ExceptionMonad m) =>
HscEnv -> (IServ -> m a) -> m a
withIServ HscEnv
hsc_env ((IServ -> TcM a) -> TcM a) -> (IServ -> TcM a) -> TcM a
forall a b. (a -> b) -> a -> b
$ \i :: IServ
i -> do
ForeignRef (IORef QState)
rstate <- IServ -> TcM (ForeignRef (IORef QState))
getTHState IServ
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
$ \state_hv :: 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
$ \q_hv :: RemoteRef HValue
q_hv ->
IServ -> Put -> IO ()
writeIServ IServ
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)))
IServ -> [Messages] -> TcRn ()
runRemoteTH IServ
i []
ByteString
bs <- IServ -> TcM ByteString
forall a. Binary a => IServ -> TcM a
readQResult IServ
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
:: IServ
-> [Messages]
-> TcM ()
runRemoteTH :: IServ -> [Messages] -> TcRn ()
runRemoteTH iserv :: IServ
iserv recovers :: [Messages]
recovers = do
THMsg msg :: 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
$ IServ -> Get THMsg -> IO THMsg
forall a. IServ -> Get a -> IO a
readIServ IServ
iserv Get THMsg
getTHMessage
case THMessage a
msg of
RunTHDone -> () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
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
IServ -> [Messages] -> TcRn ()
runRemoteTH IServ
iserv (Messages
msgs Messages -> [Messages] -> [Messages]
forall a. a -> [a] -> [a]
: [Messages]
recovers)
EndRecover caught_error :: Bool
caught_error -> do
let (prev_msgs :: Messages
prev_msgs@(prev_warns :: WarningMessages
prev_warns,prev_errs :: WarningMessages
prev_errs), rest :: [Messages]
rest) = case [Messages]
recovers of
[] -> String -> (Messages, [Messages])
forall a. String -> a
panic "EndRecover"
a :: Messages
a : b :: [Messages]
b -> (Messages
a,[Messages]
b)
TcRef Messages
v <- TcRn (TcRef Messages)
getErrsVar
(warn_msgs :: WarningMessages
warn_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 -> 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)
IServ -> [Messages] -> TcRn ()
runRemoteTH IServ
iserv [Messages]
rest
_other :: 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
$ IServ -> Put -> IO ()
writeIServ IServ
iserv (a -> Put
forall t. Binary t => t -> Put
put a
r)
IServ -> [Messages] -> TcRn ()
runRemoteTH IServ
iserv [Messages]
recovers
readQResult :: Binary a => IServ -> TcM a
readQResult :: IServ -> TcM a
readQResult i :: IServ
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
$ IServ -> Get (QResult a) -> IO (QResult a)
forall a. IServ -> Get a -> IO a
readIServ IServ
i Get (QResult a)
forall t. Binary t => Get t
get
case QResult a
qr of
QDone a :: a
a -> a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
QException str :: 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 str :: String
str -> String -> TcM a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
str
getTHState :: IServ -> TcM (ForeignRef (IORef QState))
getTHState :: IServ -> TcM (ForeignRef (IORef QState))
getTHState i :: IServ
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 rhv :: ForeignRef (IORef QState)
rhv -> ForeignRef (IORef QState) -> TcM (ForeignRef (IORef QState))
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignRef (IORef QState)
rhv
Nothing -> do
HscEnv
hsc_env <- Env TcGblEnv TcLclEnv -> HscEnv
forall gbl lcl. Env gbl lcl -> HscEnv
env_top (Env TcGblEnv TcLclEnv -> HscEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
getEnv
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
=<< IServ
-> Message (RemoteRef (IORef QState))
-> IO (RemoteRef (IORef QState))
forall a. Binary a => IServ -> Message a -> IO a
iservCall IServ
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 :: TcM a -> TcM (THResult a)
wrapTHResult tcm :: 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 e :: 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
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 :: THMessage a -> TcM a
handleTHMessage msg :: THMessage a
msg = case THMessage a
msg of
NewName a :: String
a -> TcM Name -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Name -> TcM a) -> TcM Name -> TcM a
forall a b. (a -> b) -> a -> b
$ String -> TcM Name
forall (m :: * -> *). Quasi m => String -> m Name
TH.qNewName String
a
Report b :: Bool
b str :: String
str -> TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
forall a b. (a -> b) -> a -> b
$ Bool -> String -> TcRn ()
forall (m :: * -> *). Quasi m => Bool -> String -> m ()
TH.qReport Bool
b String
str
LookupName b :: Bool
b str :: String
str -> TcM (Maybe Name) -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Name) -> TcM a) -> TcM (Maybe Name) -> TcM a
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 n :: Name
n -> TcM Info -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Info -> TcM a) -> TcM Info -> TcM a
forall a b. (a -> b) -> a -> b
$ Name -> TcM Info
forall (m :: * -> *). Quasi m => Name -> m Info
TH.qReify Name
n
ReifyFixity n :: Name
n -> TcM (Maybe Fixity) -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM (Maybe Fixity) -> TcM a) -> TcM (Maybe Fixity) -> TcM a
forall a b. (a -> b) -> a -> b
$ Name -> TcM (Maybe Fixity)
forall (m :: * -> *). Quasi m => Name -> m (Maybe Fixity)
TH.qReifyFixity Name
n
ReifyInstances n :: Name
n ts :: [Type]
ts -> TcM [Dec] -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Dec] -> TcM a) -> TcM [Dec] -> TcM a
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 n :: Name
n -> TcM [Role] -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Role] -> TcM a) -> TcM [Role] -> TcM a
forall a b. (a -> b) -> a -> b
$ Name -> TcM [Role]
forall (m :: * -> *). Quasi m => Name -> m [Role]
TH.qReifyRoles Name
n
ReifyAnnotations lookup :: AnnLookup
lookup tyrep :: TypeRep
tyrep ->
TcM [ByteString] -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [ByteString] -> TcM a) -> TcM [ByteString] -> TcM a
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 m :: Module
m -> TcM ModuleInfo -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM ModuleInfo -> TcM a) -> TcM ModuleInfo -> TcM a
forall a b. (a -> b) -> a -> b
$ Module -> TcM ModuleInfo
forall (m :: * -> *). Quasi m => Module -> m ModuleInfo
TH.qReifyModule Module
m
ReifyConStrictness nm :: Name
nm -> TcM [DecidedStrictness] -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [DecidedStrictness] -> TcM a)
-> TcM [DecidedStrictness] -> TcM a
forall a b. (a -> b) -> a -> b
$ Name -> TcM [DecidedStrictness]
forall (m :: * -> *). Quasi m => Name -> m [DecidedStrictness]
TH.qReifyConStrictness Name
nm
AddDependentFile f :: String
f -> TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddDependentFile String
f
AddTempFile s :: String
s -> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (IOEnv (Env TcGblEnv TcLclEnv) String -> TcM a)
-> IOEnv (Env TcGblEnv TcLclEnv) String -> TcM a
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 r :: RemoteRef (Q ())
r -> do
HscEnv
hsc_env <- Env TcGblEnv TcLclEnv -> HscEnv
forall gbl lcl. Env gbl lcl -> HscEnv
env_top (Env TcGblEnv TcLclEnv -> HscEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
getEnv
TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
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 str :: String
str -> TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
forall a b. (a -> b) -> a -> b
$ String -> TcRn ()
forall (m :: * -> *). Quasi m => String -> m ()
TH.qAddCorePlugin String
str
AddTopDecls decs :: [Dec]
decs -> TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
forall a b. (a -> b) -> a -> b
$ [Dec] -> TcRn ()
forall (m :: * -> *). Quasi m => [Dec] -> m ()
TH.qAddTopDecls [Dec]
decs
AddForeignFilePath lang :: ForeignSrcLang
lang str :: String
str -> TcRn () -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcRn () -> TcM a) -> TcRn () -> TcM a
forall a b. (a -> b) -> a -> b
$ ForeignSrcLang -> String -> TcRn ()
forall (m :: * -> *). Quasi m => ForeignSrcLang -> String -> m ()
TH.qAddForeignFilePath ForeignSrcLang
lang String
str
IsExtEnabled ext :: Extension
ext -> TcM Bool -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM Bool -> TcM a) -> TcM Bool -> TcM a
forall a b. (a -> b) -> a -> b
$ Extension -> TcM Bool
forall (m :: * -> *). Quasi m => Extension -> m Bool
TH.qIsExtEnabled Extension
ext
ExtsEnabled -> TcM [Extension] -> TcM a
forall a. TcM a -> TcM (THResult a)
wrapTHResult (TcM [Extension] -> TcM a) -> TcM [Extension] -> TcM a
forall a b. (a -> b) -> a -> b
$ TcM [Extension]
forall (m :: * -> *). Quasi m => m [Extension]
TH.qExtsEnabled
FailIfErrs -> TcRn () -> TcM (THResult ())
forall a. TcM a -> TcM (THResult a)
wrapTHResult TcRn ()
failIfErrsM
_ -> String -> TcM a
forall a. String -> a
panic ("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 th_name :: AnnLookup
th_name tyrep :: TypeRep
tyrep
= do { CoreAnnTarget
name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; HscEnv
topEnv <- IOEnv (Env 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 th_nm :: Name
th_nm th_tys :: [Type]
th_tys
= MsgDoc -> TcM [Dec] -> TcM [Dec]
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt (String -> MsgDoc
text "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
; LHsType GhcPs
rdr_ty <- SrcSpan -> Type -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
cvt SrcSpan
loc (Type -> [Type] -> Type
mkThAppTs (Name -> Type
TH.ConT Name
th_nm) [Type]
th_tys)
; let tv_rdrs :: [Located RdrName]
tv_rdrs = FreeKiTyVars -> [Located RdrName]
freeKiTyVarsAllVars (LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
rdr_ty)
; ((tv_names :: [Name]
tv_names, rn_ty :: LHsType GhcRn
rn_ty), _fvs :: 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
$
[Located RdrName]
-> ([Name] -> TcM (([Name], LHsType GhcRn), FreeVars))
-> TcM (([Name], LHsType GhcRn), FreeVars)
forall a.
[Located RdrName]
-> ([Name] -> RnM (a, FreeVars)) -> RnM (a, FreeVars)
bindLRdrNames [Located RdrName]
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
$ \ tv_names :: [Name]
tv_names ->
do { (rn_ty :: LHsType GhcRn
rn_ty, fvs :: 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) }
; (_tvs :: [Id]
_tvs, ty :: TcRhoType
ty)
<- TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType)
forall r. TcM r -> TcM r
pushTcLevelM_ (TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType))
-> TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType)
forall a b. (a -> b) -> a -> b
$
TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType)
forall r. TcM r -> TcM r
solveEqualities (TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType))
-> TcM ([Id], TcRhoType) -> TcM ([Id], TcRhoType)
forall a b. (a -> b) -> a -> b
$
[Name] -> TcM TcRhoType -> TcM ([Id], TcRhoType)
forall a. [Name] -> TcM a -> TcM ([Id], a)
bindImplicitTKBndrs_Skol [Name]
tv_names (TcM TcRhoType -> TcM ([Id], TcRhoType))
-> TcM TcRhoType -> TcM ([Id], TcRhoType)
forall a b. (a -> b) -> a -> b
$
(TcRhoType, TcRhoType) -> TcRhoType
forall a b. (a, b) -> a
fst ((TcRhoType, TcRhoType) -> TcRhoType)
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRhoType, TcRhoType)
-> TcM TcRhoType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LHsType GhcRn
-> IOEnv (Env TcGblEnv TcLclEnv) (TcRhoType, TcRhoType)
tcLHsType LHsType GhcRn
rn_ty
; TcRhoType
ty <- TcRhoType -> TcM TcRhoType
zonkTcTypeToType TcRhoType
ty
; String -> MsgDoc -> TcRn ()
traceTc "reifyInstances" (TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TcRhoType
ty MsgDoc -> MsgDoc -> MsgDoc
$$ TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr (HasDebugCallStack => TcRhoType -> TcRhoType
TcRhoType -> TcRhoType
tcTypeKind TcRhoType
ty))
; case HasDebugCallStack => TcRhoType -> Maybe (TyCon, [TcRhoType])
TcRhoType -> Maybe (TyCon, [TcRhoType])
splitTyConApp_maybe TcRhoType
ty of
Just (tc :: TyCon
tc, tys :: [TcRhoType]
tys)
| Just cls :: Class
cls <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
-> do { InstEnvs
inst_envs <- TcM InstEnvs
tcGetInstEnvs
; let (matches :: [InstMatch]
matches, unifies :: [ClsInst]
unifies, _) = Bool
-> InstEnvs
-> Class
-> [TcRhoType]
-> ([InstMatch], [ClsInst], [InstMatch])
lookupInstEnv Bool
False InstEnvs
inst_envs Class
cls [TcRhoType]
tys
; String -> MsgDoc -> TcRn ()
traceTc "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 -> [TcRhoType] -> [FamInstMatch]
lookupFamInstEnv FamInstEnvs
inst_envs TyCon
tc [TcRhoType]
tys
; String -> MsgDoc -> TcRn ()
traceTc "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) }
_ -> MsgDoc -> TcM [Dec]
forall a. MsgDoc -> TcM a
bale_out (MsgDoc -> Int -> MsgDoc -> MsgDoc
hang (String -> MsgDoc
text "reifyInstances:" MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
quotes (TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TcRhoType
ty))
2 (String -> MsgDoc
text "is not a class constraint or type family application")) }
where
doc :: HsDocContext
doc = HsDocContext
ClassInstanceCtx
bale_out :: MsgDoc -> TcM a
bale_out msg :: MsgDoc
msg = MsgDoc -> TcM a
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
cvt :: SrcSpan -> TH.Type -> TcM (LHsType GhcPs)
cvt :: SrcSpan -> Type -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
cvt loc :: SrcSpan
loc th_ty :: Type
th_ty = case SrcSpan -> Type -> Either MsgDoc (LHsType GhcPs)
convertToHsType SrcSpan
loc Type
th_ty of
Left msg :: MsgDoc
msg -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) (LHsType GhcPs)
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
Right ty :: 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 is_type_name :: Bool
is_type_name s :: String
s
= do { LocalRdrEnv
lcl_env <- RnM LocalRdrEnv
getLocalRdrEnv
; case LocalRdrEnv -> RdrName -> Maybe Name
lookupLocalRdrEnv LocalRdrEnv
lcl_env RdrName
rdr_name of
Just n :: 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))
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
Nothing -> OccName -> RdrName
mkRdrUnqual OccName
occ
Just mod :: String
mod -> ModuleName -> OccName -> RdrName
mkRdrQual (String -> ModuleName
mkModuleName String
mod) OccName
occ
getThing :: TH.Name -> TcM TcTyThing
getThing :: Name -> TcM TcTyThing
getThing th_name :: 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 "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 _ (TH.NameG TH.DataName _pkg :: PkgName
_pkg _mod :: ModName
_mod)) = String -> MsgDoc
text "data"
ppr_ns (TH.Name _ (TH.NameG TH.TcClsName _pkg :: PkgName
_pkg _mod :: ModName
_mod)) = String -> MsgDoc
text "tc"
ppr_ns (TH.Name _ (TH.NameG TH.VarName _pkg :: PkgName
_pkg _mod :: ModName
_mod)) = String -> MsgDoc
text "var"
ppr_ns _ = String -> MsgDoc
forall a. String -> a
panic "reify/ppr_ns"
reify :: TH.Name -> TcM TH.Info
reify :: Name -> TcM Info
reify th_name :: Name
th_name
= do { String -> MsgDoc -> TcRn ()
traceTc "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 "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 th_name :: Name
th_name = do
Maybe Name
mb_name <- Name -> RnM (Maybe Name)
lookupThName_maybe Name
th_name
case Maybe Name
mb_name of
Nothing -> MsgDoc -> TcM Name
forall a. MsgDoc -> TcM a
failWithTc (Name -> MsgDoc
notInScope Name
th_name)
Just name :: 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 th_name :: 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 rdr_name :: 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
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)
Nothing -> RdrName -> RnM (Maybe Name)
lookupGlobalOccRn_maybe RdrName
rdr_name }
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh :: Name -> TcM TcTyThing
tcLookupTh name :: Name
name
= do { (gbl_env :: TcGblEnv
gbl_env, lcl_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 thing :: TcTyThing
thing -> TcTyThing -> TcM TcTyThing
forall (m :: * -> *) a. Monad m => a -> m a
return TcTyThing
thing;
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 thing :: TyThing
thing -> TcTyThing -> TcM TcTyThing
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing);
Nothing ->
if Module -> Name -> Bool
nameIsLocalOrFrom (TcGblEnv -> Module
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 thing :: TyThing
thing -> TcTyThing -> TcM TcTyThing
forall (m :: * -> *) a. Monad m => a -> m a
return (TyThing -> TcTyThing
AGlobal TyThing
thing)
Failed msg :: MsgDoc
msg -> MsgDoc -> TcM TcTyThing
forall a. MsgDoc -> TcM a
failWithTc MsgDoc
msg
}}}}
notInScope :: TH.Name -> SDoc
notInScope :: Name -> MsgDoc
notInScope th_name :: 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 "is not in scope at a reify"
notInEnv :: Name -> SDoc
notInEnv :: Name -> MsgDoc
notInEnv name :: Name
name = MsgDoc -> MsgDoc
quotes (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
name) MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text "is not in the type environment at a reify"
reifyRoles :: TH.Name -> TcM [TH.Role]
reifyRoles :: Name -> TcM [Role]
reifyRoles th_name :: Name
th_name
= do { TcTyThing
thing <- Name -> TcM TcTyThing
getThing Name
th_name
; case TcTyThing
thing of
AGlobal (ATyCon tc :: 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))
_ -> MsgDoc -> TcM [Role]
forall a. MsgDoc -> TcM a
failWithTc (String -> MsgDoc
text "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 Nominal = Role
TH.NominalR
reify_role Representational = Role
TH.RepresentationalR
reify_role Phantom = Role
TH.PhantomR
reifyThing :: TcTyThing -> TcM TH.Info
reifyThing :: TcTyThing -> TcM Info
reifyThing (AGlobal (AnId id :: Id
id))
= do { Type
ty <- TcRhoType -> TcM Type
reifyType (Id -> TcRhoType
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 cls :: 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 tc :: 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)
_ -> 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 tc :: TyCon
tc)) = TyCon -> TcM Info
reifyTyCon TyCon
tc
reifyThing (AGlobal (AConLike (RealDataCon dc :: DataCon
dc)))
= do { let name :: Name
name = DataCon -> Name
dataConName DataCon
dc
; Type
ty <- TcRhoType -> TcM Type
reifyType (Id -> TcRhoType
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 ps :: PatSyn
ps)))
= do { let name :: Name
name = PatSyn -> Name
forall n. NamedThing n => n -> Name
reifyName PatSyn
ps
; Type
ty <- ([Id], [TcRhoType], [Id], [TcRhoType], [TcRhoType], TcRhoType)
-> TcM Type
reifyPatSynType (PatSyn
-> ([Id], [TcRhoType], [Id], [TcRhoType], [TcRhoType], TcRhoType)
patSynSig 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 { TcRhoType
ty1 <- TcRhoType -> TcM TcRhoType
zonkTcType (Id -> TcRhoType
idType Id
id)
; Type
ty2 <- TcRhoType -> TcM Type
reifyType TcRhoType
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 tv :: Name
tv tv1 :: Id
tv1)
= do { TcRhoType
ty1 <- Id -> TcM TcRhoType
zonkTcTyVar Id
tv1
; Type
ty2 <- TcRhoType -> TcM Type
reifyType TcRhoType
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 thing :: TcTyThing
thing = String -> MsgDoc -> TcM Info
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "reifyThing" (TcTyThing -> MsgDoc
pprTcTyThingCategory TcTyThing
thing)
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TH.TySynEqn
reifyAxBranch :: TyCon -> CoAxBranch -> TcM TySynEqn
reifyAxBranch fam_tc :: TyCon
fam_tc (CoAxBranch { cab_tvs :: CoAxBranch -> [Id]
cab_tvs = [Id]
tvs
, cab_lhs :: CoAxBranch -> [TcRhoType]
cab_lhs = [TcRhoType]
lhs
, cab_rhs :: CoAxBranch -> TcRhoType
cab_rhs = TcRhoType
rhs })
= do { Maybe [TyVarBndr]
tvs' <- [Id] -> TcM (Maybe [TyVarBndr])
reifyTyVarsToMaybe [Id]
tvs
; let lhs_types_only :: [TcRhoType]
lhs_types_only = TyCon -> [TcRhoType] -> [TcRhoType]
filterOutInvisibleTypes TyCon
fam_tc [TcRhoType]
lhs
; [Type]
lhs' <- [TcRhoType] -> TcM [Type]
reifyTypes [TcRhoType]
lhs_types_only
; [Type]
annot_th_lhs <- (Bool -> TcRhoType -> Type -> TcM Type)
-> [Bool] -> [TcRhoType] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> TcRhoType -> Type -> TcM Type
annotThType ([Id] -> [Bool]
mkIsPolyTvs [Id]
fam_tvs)
[TcRhoType]
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' <- TcRhoType -> TcM Type
reifyType TcRhoType
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') }
where
fam_tvs :: [Id]
fam_tvs = TyCon -> [Id]
tyConVisibleTyVars TyCon
fam_tc
reifyTyCon :: TyCon -> TcM TH.Info
reifyTyCon :: TyCon -> TcM Info
reifyTyCon tc :: TyCon
tc
| Just cls :: 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 -> Int -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) 2 Bool
False)
| TyCon -> Bool
isPrimTyCon TyCon
tc
= Info -> TcM Info
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Int -> Bool -> Info
TH.PrimTyConI (TyCon -> Name
forall n. NamedThing n => n -> Name
reifyName TyCon
tc) (TyCon -> Int
tyConArity TyCon
tc) (TyCon -> Bool
isUnliftedTyCon TyCon
tc))
| TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
= do { let tvs :: [Id]
tvs = TyCon -> [Id]
tyConTyVars TyCon
tc
res_kind :: TcRhoType
res_kind = TyCon -> TcRhoType
tyConResKind TyCon
tc
resVar :: Maybe Name
resVar = TyCon -> Maybe Name
famTcResVar TyCon
tc
; Type
kind' <- TcRhoType -> TcM Type
reifyKind TcRhoType
res_kind
; let (resultSig :: FamilyResultSig
resultSig, injectivity :: Maybe InjectivityAnn
injectivity) =
case Maybe Name
resVar of
Nothing -> (Type -> FamilyResultSig
TH.KindSig Type
kind', Maybe InjectivityAnn
forall a. Maybe a
Nothing)
Just name :: 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
TH.KindedTV Name
thName Type
kind')
inj :: Maybe InjectivityAnn
inj = case Injectivity
injAnnot of
NotInjective -> Maybe InjectivityAnn
forall a. Maybe a
Nothing
Injective ms :: [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 ax :: 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
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 :: TcRhoType
res_kind = TyCon -> TcRhoType
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 (TcRhoType -> TcM Type
reifyKind TcRhoType
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 (_, rhs :: TcRhoType
rhs) <- TyCon -> Maybe ([Id], TcRhoType)
synTyConDefn_maybe TyCon
tc
= do { Type
rhs' <- TcRhoType -> TcM Type
reifyType TcRhoType
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 <- [TcRhoType] -> TcM [Type]
reifyCxt (TyCon -> [TcRhoType]
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 -> [TcRhoType] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadt ([Id] -> [TcRhoType]
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 -> [TcRhoType] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon isGadtDataCon :: Bool
isGadtDataCon tys :: [TcRhoType]
tys dc :: DataCon
dc
= do { let
(ex_tvs :: [Id]
ex_tvs, theta :: [TcRhoType]
theta, arg_tys :: [TcRhoType]
arg_tys)
= DataCon -> [TcRhoType] -> ([Id], [TcRhoType], [TcRhoType])
dataConInstSig DataCon
dc [TcRhoType]
tys
g_user_tvs' :: [Id]
g_user_tvs' = DataCon -> [Id]
dataConUserTyVars DataCon
dc
(g_univ_tvs :: [Id]
g_univ_tvs, _, g_eq_spec :: [EqSpec]
g_eq_spec, g_theta' :: [TcRhoType]
g_theta', g_arg_tys' :: [TcRhoType]
g_arg_tys', g_res_ty' :: TcRhoType
g_res_ty')
= DataCon
-> ([Id], [Id], [EqSpec], [TcRhoType], [TcRhoType], TcRhoType)
dataConFullSig DataCon
dc
(srcUnpks :: [SourceUnpackedness]
srcUnpks, srcStricts :: [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)
; (univ_subst :: TCvSubst
univ_subst, _)
<- [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 (tvb_subst :: TCvSubst
tvb_subst, g_user_tvs :: [Id]
g_user_tvs) = HasCallStack => TCvSubst -> [Id] -> (TCvSubst, [Id])
TCvSubst -> [Id] -> (TCvSubst, [Id])
substTyVarBndrs TCvSubst
univ_subst [Id]
g_user_tvs'
g_theta :: [TcRhoType]
g_theta = HasCallStack => TCvSubst -> [TcRhoType] -> [TcRhoType]
TCvSubst -> [TcRhoType] -> [TcRhoType]
substTys TCvSubst
tvb_subst [TcRhoType]
g_theta'
g_arg_tys :: [TcRhoType]
g_arg_tys = HasCallStack => TCvSubst -> [TcRhoType] -> [TcRhoType]
TCvSubst -> [TcRhoType] -> [TcRhoType]
substTys TCvSubst
tvb_subst [TcRhoType]
g_arg_tys'
g_res_ty :: TcRhoType
g_res_ty = HasCallStack => TCvSubst -> TcRhoType -> TcRhoType
TCvSubst -> TcRhoType -> TcRhoType
substTy TCvSubst
tvb_subst TcRhoType
g_res_ty'
; [Type]
r_arg_tys <- [TcRhoType] -> TcM [Type]
reifyTypes (if Bool
isGadtDataCon then [TcRhoType]
g_arg_tys else [TcRhoType]
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 <- TcRhoType -> TcM Type
reifyType TcRhoType
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( 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 <- TcRhoType -> TcM Type
reifyType TcRhoType
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 (ex_tvs' :: [Id]
ex_tvs', theta' :: [TcRhoType]
theta') | Bool
isGadtDataCon = ([Id]
g_user_tvs, [TcRhoType]
g_theta)
| Bool
otherwise = ASSERT( all isTyVar ex_tvs )
([Id]
ex_tvs, [TcRhoType]
theta)
ret_con :: IOEnv (Env TcGblEnv TcLclEnv) Con
ret_con | [Id] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
ex_tvs' Bool -> Bool -> Bool
&& [TcRhoType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcRhoType]
theta' = Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return Con
main_con
| Bool
otherwise = do
{ [Type]
cxt <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
theta'
; [TyVarBndr]
ex_tvs'' <- [Id] -> TcM [TyVarBndr]
reifyTyVars [Id]
ex_tvs'
; Con -> IOEnv (Env TcGblEnv TcLclEnv) Con
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyVarBndr] -> [Type] -> Con -> Con
TH.ForallC [TyVarBndr]
ex_tvs'' [Type]
cxt Con
main_con) }
; ASSERT( arg_tys `equalLength` dcdBangs )
IOEnv (Env TcGblEnv TcLclEnv) Con
ret_con }
reifyClass :: Class -> TcM TH.Info
reifyClass :: Class -> TcM Info
reifyClass cls :: Class
cls
= do { [Type]
cxt <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
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 TcRhoType)) -> TcM [Dec])
-> [(Id, Maybe (Name, DefMethSpec TcRhoType))] -> TcM [Dec]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM (Id, Maybe (Name, DefMethSpec TcRhoType)) -> TcM [Dec]
forall a. (Id, Maybe (a, DefMethSpec TcRhoType)) -> TcM [Dec]
reify_op [(Id, Maybe (Name, DefMethSpec TcRhoType))]
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
(_, fds :: [FunDep Id]
fds, theta :: [TcRhoType]
theta, _, ats :: [ClassATItem]
ats, op_stuff :: [(Id, Maybe (Name, DefMethSpec TcRhoType))]
op_stuff) = Class
-> ([Id], [FunDep Id], [TcRhoType], [Id], [ClassATItem],
[(Id, Maybe (Name, DefMethSpec TcRhoType))])
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 TcRhoType)) -> TcM [Dec]
reify_op (op :: Id
op, def_meth :: Maybe (a, DefMethSpec TcRhoType)
def_meth)
= do { let (_, _, ty :: TcRhoType
ty) = TcRhoType -> ([Id], TcRhoType, TcRhoType)
tcSplitMethodTy (Id -> TcRhoType
idType Id
op)
; Type
ty' <- TcRhoType -> TcM Type
reifyType TcRhoType
ty
; let nm' :: Name
nm' = Id -> Name
forall n. NamedThing n => n -> Name
reifyName Id
op
; case Maybe (a, DefMethSpec TcRhoType)
def_meth of
Just (_, GenericDM gdm_ty :: TcRhoType
gdm_ty) ->
do { Type
gdm_ty' <- TcRhoType -> TcM Type
reifyType TcRhoType
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'] }
_ -> [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
tycon def :: Maybe (TcRhoType, SrcSpan)
def) = do
Info
tycon' <- TyCon -> TcM Info
reifyTyCon TyCon
tycon
case Info
tycon' of
TH.FamilyI dec :: Dec
dec _ -> do
let (tyName :: Name
tyName, tyArgs :: [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]
-> ((TcRhoType, SrcSpan) -> TcM [Dec])
-> Maybe (TcRhoType, SrcSpan)
-> 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])
-> ((TcRhoType, SrcSpan) -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> (TcRhoType, SrcSpan)
-> TcM [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> [Name] -> TcRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyDefImpl Name
tyName [Name]
tyArgs (TcRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Dec)
-> ((TcRhoType, SrcSpan) -> TcRhoType)
-> (TcRhoType, SrcSpan)
-> IOEnv (Env TcGblEnv TcLclEnv) Dec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TcRhoType, SrcSpan) -> TcRhoType
forall a b. (a, b) -> a
fst)
Maybe (TcRhoType, SrcSpan)
def
_ -> String -> MsgDoc -> TcM [Dec]
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "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] -> TcRhoType -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyDefImpl n :: Name
n args :: [Name]
args ty :: TcRhoType
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
<$> TcRhoType -> TcM Type
reifyType TcRhoType
ty
tfNames :: TH.Dec -> (TH.Name, [TH.Name])
tfNames :: Dec -> (Name, [Name])
tfNames (TH.OpenTypeFamilyD (TH.TypeFamilyHead n :: Name
n args :: [TyVarBndr]
args _ _))
= (Name
n, (TyVarBndr -> Name) -> [TyVarBndr] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVarBndr -> Name
bndrName [TyVarBndr]
args)
tfNames d :: Dec
d = String -> MsgDoc -> (Name, [Name])
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "tfNames" (String -> MsgDoc
text (Dec -> String
forall a. Show a => a -> String
show Dec
d))
bndrName :: TH.TyVarBndr -> TH.Name
bndrName :: TyVarBndr -> Name
bndrName (TH.PlainTV n :: Name
n) = Name
n
bndrName (TH.KindedTV n :: Name
n _) = Name
n
annotThType :: Bool
-> TyCoRep.Type -> TH.Type -> TcM TH.Type
annotThType :: Bool -> TcRhoType -> Type -> TcM Type
annotThType _ _ th_ty :: Type
th_ty@(TH.SigT {}) = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_ty
annotThType True ty :: TcRhoType
ty th_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
$ TcRhoType -> VarSet
tyCoVarsOfType TcRhoType
ty
= do { let ki :: TcRhoType
ki = HasDebugCallStack => TcRhoType -> TcRhoType
TcRhoType -> TcRhoType
tcTypeKind TcRhoType
ty
; Type
th_ki <- TcRhoType -> TcM Type
reifyKind TcRhoType
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 _ _ th_ty :: Type
th_ty = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
th_ty
mkIsPolyTvs :: [TyVar] -> [Bool]
mkIsPolyTvs :: [Id] -> [Bool]
mkIsPolyTvs = (Id -> Bool) -> [Id] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Bool
is_poly_tv
where
is_poly_tv :: Id -> Bool
is_poly_tv tv :: Id
tv = 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
$
TcRhoType -> VarSet
tyCoVarsOfType (TcRhoType -> VarSet) -> TcRhoType -> VarSet
forall a b. (a -> b) -> a -> b
$
Id -> TcRhoType
tyVarKind Id
tv
reifyClassInstances :: Class -> [ClsInst] -> TcM [TH.Dec]
reifyClassInstances :: Class -> [ClsInst] -> TcM [Dec]
reifyClassInstances cls :: Class
cls insts :: [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 ([Id] -> [Bool]
mkIsPolyTvs [Id]
tvs)) [ClsInst]
insts
where
tvs :: [Id]
tvs = TyCon -> [Id]
tyConVisibleTyVars (Class -> TyCon
classTyCon Class
cls)
reifyClassInstance :: [Bool]
-> ClsInst -> TcM TH.Dec
reifyClassInstance :: [Bool] -> ClsInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyClassInstance is_poly_tvs :: [Bool]
is_poly_tvs i :: ClsInst
i
= do { [Type]
cxt <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
theta
; let vis_types :: [TcRhoType]
vis_types = TyCon -> [TcRhoType] -> [TcRhoType]
filterOutInvisibleTypes TyCon
cls_tc [TcRhoType]
types
; [Type]
thtypes <- [TcRhoType] -> TcM [Type]
reifyTypes [TcRhoType]
vis_types
; [Type]
annot_thtypes <- (Bool -> TcRhoType -> Type -> TcM Type)
-> [Bool] -> [TcRhoType] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> TcRhoType -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [TcRhoType]
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
(_tvs :: [Id]
_tvs, theta :: [TcRhoType]
theta, cls :: Class
cls, types :: [TcRhoType]
types) = TcRhoType -> ([Id], [TcRhoType], Class, [TcRhoType])
tcSplitDFunTy (Id -> TcRhoType
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 _ -> Maybe Overlap
forall a. Maybe a
Nothing
Overlappable _ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlappable
Overlapping _ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlapping
Overlaps _ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Overlaps
Incoherent _ -> Overlap -> Maybe Overlap
forall a. a -> Maybe a
Just Overlap
TH.Incoherent
reifyFamilyInstances :: TyCon -> [FamInst] -> TcM [TH.Dec]
reifyFamilyInstances :: TyCon -> [FamInst] -> TcM [Dec]
reifyFamilyInstances fam_tc :: TyCon
fam_tc fam_insts :: [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 ([Id] -> [Bool]
mkIsPolyTvs [Id]
fam_tvs)) [FamInst]
fam_insts
where
fam_tvs :: [Id]
fam_tvs = TyCon -> [Id]
tyConVisibleTyVars TyCon
fam_tc
reifyFamilyInstance :: [Bool]
-> FamInst -> TcM TH.Dec
reifyFamilyInstance :: [Bool] -> FamInst -> IOEnv (Env TcGblEnv TcLclEnv) Dec
reifyFamilyInstance is_poly_tvs :: [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 -> [TcRhoType]
cab_lhs = [TcRhoType]
lhs, cab_rhs :: CoAxBranch -> TcRhoType
cab_rhs = TcRhoType
rhs } <- CoAxBranch
branch
= case FamFlavor
flavor of
SynFamilyInst ->
do { Maybe [TyVarBndr]
th_tvs <- [Id] -> TcM (Maybe [TyVarBndr])
reifyTyVarsToMaybe [Id]
tvs
; let lhs_types_only :: [TcRhoType]
lhs_types_only = TyCon -> [TcRhoType] -> [TcRhoType]
filterOutInvisibleTypes TyCon
fam_tc [TcRhoType]
lhs
; [Type]
th_lhs <- [TcRhoType] -> TcM [Type]
reifyTypes [TcRhoType]
lhs_types_only
; [Type]
annot_th_lhs <- (Bool -> TcRhoType -> Type -> TcM Type)
-> [Bool] -> [TcRhoType] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> TcRhoType -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [TcRhoType]
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 <- TcRhoType -> TcM Type
reifyType TcRhoType
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 rep_tc :: TyCon
rep_tc ->
do { let
(ee_tvs :: [Id]
ee_tvs, ee_lhs :: [TcRhoType]
ee_lhs, _) = CoAxBranch -> ([Id], [TcRhoType], TcRhoType)
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 -> [TcRhoType] -> DataCon -> IOEnv (Env TcGblEnv TcLclEnv) Con
reifyDataCon Bool
isGadt ([Id] -> [TcRhoType]
mkTyVarTys [Id]
ee_tvs)) [DataCon]
dataCons
; let types_only :: [TcRhoType]
types_only = TyCon -> [TcRhoType] -> [TcRhoType]
filterOutInvisibleTypes TyCon
fam_tc [TcRhoType]
ee_lhs
; [Type]
th_tys <- [TcRhoType] -> TcM [Type]
reifyTypes [TcRhoType]
types_only
; [Type]
annot_th_tys <- (Bool -> TcRhoType -> Type -> TcM Type)
-> [Bool] -> [TcRhoType] -> [Type] -> TcM [Type]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> TcRhoType -> Type -> TcM Type
annotThType [Bool]
is_poly_tvs [TcRhoType]
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
; 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
forall a. Maybe a
Nothing ([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
forall a. Maybe a
Nothing [Con]
cons []
}
reifyType :: TyCoRep.Type -> TcM TH.Type
reifyType :: TcRhoType -> TcM Type
reifyType ty :: TcRhoType
ty | TcRhoType -> Bool
tcIsLiftedTypeKind TcRhoType
ty = Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TH.StarT
reifyType ty :: TcRhoType
ty@(ForAllTy {}) = TcRhoType -> TcM Type
reify_for_all TcRhoType
ty
reifyType (LitTy t :: 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 tv :: 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 tc :: TyCon
tc tys :: [TcRhoType]
tys) = TyCon -> [TcRhoType] -> TcM Type
reify_tc_app TyCon
tc [TcRhoType]
tys
reifyType ty :: TcRhoType
ty@(AppTy {}) = do
let (ty_head :: TcRhoType
ty_head, ty_args :: [TcRhoType]
ty_args) = TcRhoType -> (TcRhoType, [TcRhoType])
splitAppTys TcRhoType
ty
Type
ty_head' <- TcRhoType -> TcM Type
reifyType TcRhoType
ty_head
[Type]
ty_args' <- [TcRhoType] -> TcM [Type]
reifyTypes (TcRhoType -> [TcRhoType] -> [TcRhoType]
filter_out_invisible_args TcRhoType
ty_head [TcRhoType]
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 :: TcRhoType -> [TcRhoType] -> [TcRhoType]
filter_out_invisible_args ty_head :: TcRhoType
ty_head ty_args :: [TcRhoType]
ty_args =
[Bool] -> [TcRhoType] -> [TcRhoType]
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
$ TcRhoType -> [TcRhoType] -> [ArgFlag]
appTyArgFlags TcRhoType
ty_head [TcRhoType]
ty_args)
[TcRhoType]
ty_args
reifyType ty :: TcRhoType
ty@(FunTy t1 :: TcRhoType
t1 t2 :: TcRhoType
t2)
| TcRhoType -> Bool
isPredTy TcRhoType
t1 = TcRhoType -> TcM Type
reify_for_all TcRhoType
ty
| Bool
otherwise = do { [r1 :: Type
r1,r2 :: Type
r2] <- [TcRhoType] -> TcM [Type]
reifyTypes [TcRhoType
t1,TcRhoType
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 (CastTy t :: TcRhoType
t _) = TcRhoType -> TcM Type
reifyType TcRhoType
t
reifyType ty :: TcRhoType
ty@(CoercionTy {})= PtrString -> MsgDoc -> TcM Type
forall a. PtrString -> MsgDoc -> TcM a
noTH (String -> PtrString
sLit "coercions in types") (TcRhoType -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr TcRhoType
ty)
reify_for_all :: TyCoRep.Type -> TcM TH.Type
reify_for_all :: TcRhoType -> TcM Type
reify_for_all ty :: TcRhoType
ty
= do { [Type]
cxt' <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
cxt;
; Type
tau' <- TcRhoType -> TcM Type
reifyType TcRhoType
tau
; [TyVarBndr]
tvs' <- [Id] -> TcM [TyVarBndr]
reifyTyVars [Id]
tvs
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyVarBndr] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr]
tvs' [Type]
cxt' Type
tau') }
where
(tvs :: [Id]
tvs, cxt :: [TcRhoType]
cxt, tau :: TcRhoType
tau) = TcRhoType -> ([Id], [TcRhoType], TcRhoType)
tcSplitSigmaTy TcRhoType
ty
reifyTyLit :: TyCoRep.TyLit -> TcM TH.TyLit
reifyTyLit :: TyLit -> TcM TyLit
reifyTyLit (NumTyLit n :: Integer
n) = TyLit -> TcM TyLit
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> TyLit
TH.NumTyLit Integer
n)
reifyTyLit (StrTyLit s :: 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 :: [TcRhoType] -> TcM [Type]
reifyTypes = (TcRhoType -> TcM Type) -> [TcRhoType] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TcRhoType -> TcM Type
reifyType
reifyPatSynType
:: ([TyVar], ThetaType, [TyVar], ThetaType, [Type], Type) -> TcM TH.Type
reifyPatSynType :: ([Id], [TcRhoType], [Id], [TcRhoType], [TcRhoType], TcRhoType)
-> TcM Type
reifyPatSynType (univTyVars :: [Id]
univTyVars, req :: [TcRhoType]
req, exTyVars :: [Id]
exTyVars, prov :: [TcRhoType]
prov, argTys :: [TcRhoType]
argTys, resTy :: TcRhoType
resTy)
= do { [TyVarBndr]
univTyVars' <- [Id] -> TcM [TyVarBndr]
reifyTyVars [Id]
univTyVars
; [Type]
req' <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
req
; [TyVarBndr]
exTyVars' <- [Id] -> TcM [TyVarBndr]
reifyTyVars [Id]
exTyVars
; [Type]
prov' <- [TcRhoType] -> TcM [Type]
reifyCxt [TcRhoType]
prov
; Type
tau' <- TcRhoType -> TcM Type
reifyType ([TcRhoType] -> TcRhoType -> TcRhoType
mkFunTys [TcRhoType]
argTys TcRhoType
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] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr]
univTyVars' [Type]
req'
(Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ [TyVarBndr] -> [Type] -> Type -> Type
TH.ForallT [TyVarBndr]
exTyVars' [Type]
prov' Type
tau' }
reifyKind :: Kind -> TcM TH.Kind
reifyKind :: TcRhoType -> TcM Type
reifyKind = TcRhoType -> TcM Type
reifyType
reifyCxt :: [PredType] -> TcM [TH.Pred]
reifyCxt :: [TcRhoType] -> TcM [Type]
reifyCxt = (TcRhoType -> TcM Type) -> [TcRhoType] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TcRhoType -> TcM Type
reifyType
reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
reifyFunDep :: FunDep Id -> FunDep
reifyFunDep (xs :: [Id]
xs, ys :: [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)
reifyTyVars :: [TyVar] -> TcM [TH.TyVarBndr]
reifyTyVars :: [Id] -> TcM [TyVarBndr]
reifyTyVars tvs :: [Id]
tvs = (Id -> IOEnv (Env TcGblEnv TcLclEnv) TyVarBndr)
-> [Id] -> TcM [TyVarBndr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Id -> IOEnv (Env TcGblEnv TcLclEnv) TyVarBndr
reify_tv [Id]
tvs
where
reify_tv :: Id -> IOEnv (Env TcGblEnv TcLclEnv) TyVarBndr
reify_tv tv :: Id
tv = Name -> Type -> TyVarBndr
TH.KindedTV Name
name (Type -> TyVarBndr)
-> TcM Type -> IOEnv (Env TcGblEnv TcLclEnv) TyVarBndr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRhoType -> TcM Type
reifyKind TcRhoType
kind
where
kind :: TcRhoType
kind = Id -> TcRhoType
tyVarKind Id
tv
name :: Name
name = Id -> Name
forall n. NamedThing n => n -> Name
reifyName 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 tys :: [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 -> [TcRhoType] -> TcM Type
reify_tc_app tc :: TyCon
tc tys :: [TcRhoType]
tys
= do { [Type]
tys' <- [TcRhoType] -> TcM [Type]
reifyTypes (TyCon -> [TcRhoType] -> [TcRhoType]
filterOutInvisibleTypes TyCon
tc [TcRhoType]
tys)
; Type -> TcM Type
maybe_sig_t (Type -> [Type] -> Type
mkThAppTs Type
r_tc [Type]
tys') }
where
arity :: Int
arity = TyCon -> Int
tyConArity TyCon
tc
tc_binders :: [TyConBinder]
tc_binders = TyCon -> [TyConBinder]
tyConBinders TyCon
tc
tc_res_kind :: TcRhoType
tc_res_kind = TyCon -> TcRhoType
tyConResKind TyCon
tc
r_tc :: Type
r_tc | TyCon -> Bool
isUnboxedSumTyCon TyCon
tc = Int -> Type
TH.UnboxedSumT (Int
arity Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
| TyCon -> Bool
isUnboxedTupleTyCon TyCon
tc = Int -> Type
TH.UnboxedTupleT (Int
arity Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
| TyCon -> Bool
isPromotedTupleTyCon TyCon
tc = Int -> Type
TH.PromotedTupleT (Int
arity Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
| TyCon -> Bool
isTupleTyCon TyCon
tc = if TyCon -> Bool
isPromotedDataCon TyCon
tc
then Int -> Type
TH.PromotedTupleT Int
arity
else Int -> Type
TH.TupleT Int
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
funTyConKey = 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 th_type :: Type
th_type
| Bool
needs_kind_sig
= do { let full_kind :: TcRhoType
full_kind = HasDebugCallStack => TcRhoType -> TcRhoType
TcRhoType -> TcRhoType
tcTypeKind (TyCon -> [TcRhoType] -> TcRhoType
mkTyConApp TyCon
tc [TcRhoType]
tys)
; Type
th_full_kind <- TcRhoType -> TcM Type
reifyKind TcRhoType
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
needs_kind_sig :: Bool
needs_kind_sig
| Ordering
GT <- [TcRhoType] -> [TyConBinder] -> Ordering
forall a b. [a] -> [b] -> Ordering
compareLength [TcRhoType]
tys [TyConBinder]
tc_binders
= Bool
False
| Bool
otherwise
= let (dropped_binders :: [TyConBinder]
dropped_binders, remaining_binders :: [TyConBinder]
remaining_binders)
= [TcRhoType] -> [TyConBinder] -> ([TyConBinder], [TyConBinder])
forall b a. [b] -> [a] -> ([a], [a])
splitAtList [TcRhoType]
tys [TyConBinder]
tc_binders
result_kind :: TcRhoType
result_kind = [TyConBinder] -> TcRhoType -> TcRhoType
mkTyConKind [TyConBinder]
remaining_binders TcRhoType
tc_res_kind
result_vars :: VarSet
result_vars = TcRhoType -> VarSet
tyCoVarsOfType TcRhoType
result_kind
dropped_vars :: VarSet
dropped_vars = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$
(TyConBinder -> FV) -> [TyConBinder] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV TyConBinder -> FV
injectiveVarsOfBinder [TyConBinder]
dropped_binders
in Bool -> Bool
not (VarSet -> VarSet -> Bool
subVarSet VarSet
result_vars VarSet
dropped_vars)
reifyName :: NamedThing n => n -> TH.Name
reifyName :: n -> Name
reifyName thing :: 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 -> Int -> Name
TH.mkNameU String
occ_str (Unique -> Int
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 :: Module
mod = ASSERT( isExternalName name ) nameModule name
pkg_str :: String
pkg_str = UnitId -> String
unitIdString (Module -> UnitId
moduleUnitId Module
mod)
mod_str :: String
mod_str = ModuleName -> String
moduleNameString (Module -> ModuleName
moduleName Module
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 "reifyName" (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
name)
reifyFieldLabel :: FieldLabel -> TH.Name
reifyFieldLabel :: FieldLabel -> Name
reifyFieldLabel fl :: 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 :: Module
mod = ASSERT( isExternalName name ) nameModule name
pkg_str :: String
pkg_str = UnitId -> String
unitIdString (Module -> UnitId
moduleUnitId Module
mod)
mod_str :: String
mod_str = ModuleName -> String
moduleNameString (Module -> ModuleName
moduleName Module
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
id tc :: 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 fl :: FieldLabel
fl -> FieldLabel -> Name
reifyFieldLabel FieldLabel
fl
Nothing -> String -> MsgDoc -> Name
forall a. HasCallStack => String -> MsgDoc -> a
pprPanic "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
name
= do { (found :: Bool
found, fix :: 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 _ i :: Int
i d :: FixityDirection
d) = Int -> FixityDirection -> Fixity
TH.Fixity Int
i (FixityDirection -> FixityDirection
conv_dir FixityDirection
d)
conv_dir :: FixityDirection -> FixityDirection
conv_dir BasicTypes.InfixR = FixityDirection
TH.InfixR
conv_dir BasicTypes.InfixL = FixityDirection
TH.InfixL
conv_dir BasicTypes.InfixN = FixityDirection
TH.InfixN
reifyUnpackedness :: DataCon.SrcUnpackedness -> TH.SourceUnpackedness
reifyUnpackedness :: SrcUnpackedness -> SourceUnpackedness
reifyUnpackedness NoSrcUnpack = SourceUnpackedness
TH.NoSourceUnpackedness
reifyUnpackedness SrcNoUnpack = SourceUnpackedness
TH.SourceNoUnpack
reifyUnpackedness SrcUnpack = SourceUnpackedness
TH.SourceUnpack
reifyStrictness :: DataCon.SrcStrictness -> TH.SourceStrictness
reifyStrictness :: SrcStrictness -> SourceStrictness
reifyStrictness NoSrcStrict = SourceStrictness
TH.NoSourceStrictness
reifyStrictness SrcStrict = SourceStrictness
TH.SourceStrict
reifyStrictness SrcLazy = SourceStrictness
TH.SourceLazy
reifySourceBang :: DataCon.HsSrcBang
-> (TH.SourceUnpackedness, TH.SourceStrictness)
reifySourceBang :: HsSrcBang -> (SourceUnpackedness, SourceStrictness)
reifySourceBang (HsSrcBang _ u :: SrcUnpackedness
u s :: SrcStrictness
s) = (SrcUnpackedness -> SourceUnpackedness
reifyUnpackedness SrcUnpackedness
u, SrcStrictness -> SourceStrictness
reifyStrictness SrcStrictness
s)
reifyDecidedStrictness :: DataCon.HsImplBang -> TH.DecidedStrictness
reifyDecidedStrictness :: HsImplBang -> DecidedStrictness
reifyDecidedStrictness HsLazy = DecidedStrictness
TH.DecidedLazy
reifyDecidedStrictness HsStrict = DecidedStrictness
TH.DecidedStrict
reifyDecidedStrictness HsUnpack{} = DecidedStrictness
TH.DecidedUnpack
lookupThAnnLookup :: TH.AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup :: AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup (TH.AnnLookupName th_nm :: 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 pn :: PkgName
pn mn :: 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
$ Module -> CoreAnnTarget
forall name. Module -> AnnTarget name
ModuleTarget (Module -> CoreAnnTarget) -> Module -> CoreAnnTarget
forall a b. (a -> b) -> a -> b
$
UnitId -> ModuleName -> Module
mkModule (String -> UnitId
stringToUnitId (String -> UnitId) -> String -> UnitId
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 :: AnnLookup -> TcM [a]
reifyAnnotations th_name :: AnnLookup
th_name
= do { CoreAnnTarget
name <- AnnLookup -> TcM CoreAnnTarget
lookupThAnnLookup AnnLookup
th_name
; HscEnv
topEnv <- IOEnv (Env 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 :: Module -> Module
modToTHMod m :: Module
m = PkgName -> ModName -> Module
TH.Module (String -> PkgName
TH.PkgName (String -> PkgName) -> String -> PkgName
forall a b. (a -> b) -> a -> b
$ UnitId -> String
unitIdString (UnitId -> String) -> UnitId -> String
forall a b. (a -> b) -> a -> b
$ Module -> UnitId
moduleUnitId Module
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
$ Module -> ModuleName
moduleName Module
m)
reifyModule :: TH.Module -> TcM TH.ModuleInfo
reifyModule :: Module -> TcM ModuleInfo
reifyModule (TH.Module (TH.PkgName pkgString :: String
pkgString) (TH.ModName mString :: String
mString)) = do
Module
this_mod <- IOEnv (Env TcGblEnv TcLclEnv) Module
forall (m :: * -> *). HasModule m => m Module
getModule
let reifMod :: Module
reifMod = UnitId -> ModuleName -> Module
mkModule (String -> UnitId
stringToUnitId String
pkgString) (String -> ModuleName
mkModuleName String
mString)
if (Module
reifMod Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== Module
this_mod) then TcM ModuleInfo
reifyThisModule else Module -> TcM ModuleInfo
reifyFromIface Module
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 ((Module -> Module) -> [Module] -> [Module]
forall a b. (a -> b) -> [a] -> [b]
map Module -> Module
modToTHMod ([Module] -> [Module])
-> (ImportAvails -> [Module]) -> ImportAvails -> [Module]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleEnv [ImportedBy] -> [Module]
forall a. ModuleEnv a -> [Module]
moduleEnvKeys (ModuleEnv [ImportedBy] -> [Module])
-> (ImportAvails -> ModuleEnv [ImportedBy])
-> ImportAvails
-> [Module]
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 :: Module -> TcM ModuleInfo
reifyFromIface reifMod :: Module
reifMod = do
ModIface
iface <- MsgDoc -> Module -> TcRn ModIface
loadInterfaceForModule (String -> MsgDoc
text "reifying module from TH for" MsgDoc -> MsgDoc -> MsgDoc
<+> Module -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Module
reifMod) Module
reifMod
let usages :: [Module]
usages = [Module -> Module
modToTHMod Module
m | Usage
usage <- ModIface -> [Usage]
mi_usages ModIface
iface,
Just m :: Module
m <- [UnitId -> Usage -> Maybe Module
usageToModule (Module -> UnitId
moduleUnitId Module
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 :: UnitId -> Usage -> Maybe Module
usageToModule :: UnitId -> Usage -> Maybe Module
usageToModule _ (UsageFile {}) = Maybe Module
forall a. Maybe a
Nothing
usageToModule this_pkg :: UnitId
this_pkg (UsageHomeModule { usg_mod_name :: Usage -> ModuleName
usg_mod_name = ModuleName
mn }) = Module -> Maybe Module
forall a. a -> Maybe a
Just (Module -> Maybe Module) -> Module -> Maybe Module
forall a b. (a -> b) -> a -> b
$ UnitId -> ModuleName -> Module
mkModule UnitId
this_pkg ModuleName
mn
usageToModule _ (UsagePackageModule { usg_mod :: Usage -> Module
usg_mod = Module
m }) = Module -> Maybe Module
forall a. a -> Maybe a
Just Module
m
usageToModule _ (UsageMergedRequirement { usg_mod :: Usage -> Module
usg_mod = Module
m }) = Module -> Maybe Module
forall a. a -> Maybe a
Just Module
m
mkThAppTs :: TH.Type -> [TH.Type] -> TH.Type
mkThAppTs :: Type -> [Type] -> Type
mkThAppTs fun_ty :: Type
fun_ty arg_tys :: [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 :: PtrString -> MsgDoc -> TcM a
noTH s :: PtrString
s d :: MsgDoc
d = MsgDoc -> TcM a
forall a. MsgDoc -> TcM a
failWithTc ([MsgDoc] -> MsgDoc
hsep [String -> MsgDoc
text "Can't represent" MsgDoc -> MsgDoc -> MsgDoc
<+> PtrString -> MsgDoc
ptext PtrString
s MsgDoc -> MsgDoc -> MsgDoc
<+>
String -> MsgDoc
text "in Template Haskell:",
Int -> MsgDoc -> MsgDoc
nest 2 MsgDoc
d])
ppr_th :: TH.Ppr a => a -> SDoc
ppr_th :: a -> MsgDoc
ppr_th x :: a
x = String -> MsgDoc
text (a -> String
forall a. Ppr a => a -> String
TH.pprint a
x)