2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcSplice]{Template Haskell splices}
7 module TcSplice( tcSpliceExpr, tcSpliceDecls, tcBracket ) where
9 #include "HsVersions.h"
11 import HscMain ( compileExpr )
12 import TcRnDriver ( tcTopSrcDecls )
13 -- These imports are the reason that TcSplice
14 -- is very high up the module hierarchy
16 import qualified Language.Haskell.TH as TH
17 -- THSyntax gives access to internal functions and data types
18 import qualified Language.Haskell.TH.Syntax as TH
20 import HsSyn ( HsBracket(..), HsExpr(..), HsSplice(..), LHsExpr, LHsDecl,
22 import Convert ( convertToHsExpr, convertToHsDecls, convertToHsType, thRdrName )
23 import RnExpr ( rnLExpr )
24 import RnEnv ( lookupFixityRn, lookupSrcOcc_maybe, lookupImportedName )
25 import RdrName ( RdrName, lookupLocalRdrEnv, isSrcRdrName )
26 import RnTypes ( rnLHsType )
27 import TcExpr ( tcCheckRho, tcMonoExpr )
28 import TcHsSyn ( mkHsDictLet, zonkTopLExpr )
29 import TcSimplify ( tcSimplifyTop, tcSimplifyBracket )
30 import TcUnify ( Expected, zapExpectedTo, zapExpectedType )
31 import TcType ( TcType, TcKind, liftedTypeKind, mkAppTy, tcSplitSigmaTy )
32 import TcEnv ( spliceOK, tcMetaTy, bracketOK )
33 import TcMType ( newTyFlexiVarTy, newKindVar, UserTypeCtxt(ExprSigCtxt), zonkTcType )
34 import TcHsType ( tcHsSigType, kcHsType )
35 import TcIface ( tcImportDecl )
36 import TypeRep ( Type(..), PredType(..), TyThing(..) ) -- For reification
37 import PrelNames ( thFAKE )
38 import Name ( Name, NamedThing(..), nameOccName, nameModule, isExternalName,
40 import NameEnv ( lookupNameEnv )
41 import HscTypes ( lookupType, ExternalPackageState(..), emptyModDetails )
43 import Var ( Id, TyVar, idType )
44 import Module ( moduleUserString )
46 import IfaceEnv ( lookupOrig )
47 import Class ( Class, classExtraBigSig )
48 import TyCon ( TyCon, tyConTyVars, getSynTyConDefn,
49 isSynTyCon, isNewTyCon, tyConDataCons, isPrimTyCon, isFunTyCon,
50 tyConArity, tyConStupidTheta, isUnLiftedTyCon )
51 import DataCon ( DataCon, dataConTyCon, dataConOrigArgTys, dataConStrictMarks,
52 dataConName, dataConFieldLabels, dataConWrapId, dataConIsInfix,
54 import Id ( idName, globalIdDetails )
55 import IdInfo ( GlobalIdDetails(..) )
56 import TysWiredIn ( mkListTy )
57 import DsMeta ( expQTyConName, typeQTyConName, decTyConName, qTyConName, nameTyConName )
58 import ErrUtils ( Message )
59 import SrcLoc ( SrcSpan, noLoc, unLoc, getLoc )
61 import Unique ( Unique, Uniquable(..), getKey, mkUniqueGrimily )
63 import BasicTypes ( StrictnessMark(..), Fixity(..), FixityDirection(..) )
64 import Panic ( showException )
65 import FastString ( LitString )
67 import GHC.Base ( unsafeCoerce#, Int#, Int(..) ) -- Should have a better home in the module hierarchy
68 import Monad ( liftM )
71 import FastString ( mkFastString )
76 %************************************************************************
78 \subsection{Main interface + stubs for the non-GHCI case
80 %************************************************************************
83 tcSpliceDecls :: LHsExpr Name -> TcM [LHsDecl RdrName]
84 tcSpliceExpr :: HsSplice Name -> Expected TcType -> TcM (HsExpr TcId)
85 kcSpliceType :: HsSplice Name -> TcM (HsType Name, TcKind)
88 tcSpliceExpr n e ty = pprPanic "Cant do tcSpliceExpr without GHCi" (ppr e)
89 tcSpliceDecls e = pprPanic "Cant do tcSpliceDecls without GHCi" (ppr e)
93 %************************************************************************
95 \subsection{Quoting an expression}
97 %************************************************************************
100 tcBracket :: HsBracket Name -> Expected TcType -> TcM (LHsExpr Id)
101 tcBracket brack res_ty
102 = getStage `thenM` \ level ->
103 case bracketOK level of {
104 Nothing -> failWithTc (illegalBracket level) ;
107 -- Typecheck expr to make sure it is valid,
108 -- but throw away the results. We'll type check
109 -- it again when we actually use it.
111 newMutVar [] `thenM` \ pending_splices ->
112 getLIEVar `thenM` \ lie_var ->
114 setStage (Brack next_level pending_splices lie_var) (
115 getLIE (tc_bracket brack)
116 ) `thenM` \ (meta_ty, lie) ->
117 tcSimplifyBracket lie `thenM_`
119 -- Make the expected type have the right shape
120 zapExpectedTo res_ty meta_ty `thenM_`
122 -- Return the original expression, not the type-decorated one
123 readMutVar pending_splices `thenM` \ pendings ->
124 returnM (noLoc (HsBracketOut brack pendings))
127 tc_bracket :: HsBracket Name -> TcM TcType
129 = tcMetaTy nameTyConName -- Result type is Var (not Q-monadic)
131 tc_bracket (ExpBr expr)
132 = newTyFlexiVarTy liftedTypeKind `thenM` \ any_ty ->
133 tcCheckRho expr any_ty `thenM_`
134 tcMetaTy expQTyConName
135 -- Result type is Expr (= Q Exp)
137 tc_bracket (TypBr typ)
138 = tcHsSigType ExprSigCtxt typ `thenM_`
139 tcMetaTy typeQTyConName
140 -- Result type is Type (= Q Typ)
142 tc_bracket (DecBr decls)
143 = do { tcTopSrcDecls emptyModDetails decls
144 -- Typecheck the declarations, dicarding the result
145 -- We'll get all that stuff later, when we splice it in
147 ; decl_ty <- tcMetaTy decTyConName
148 ; q_ty <- tcMetaTy qTyConName
149 ; return (mkAppTy q_ty (mkListTy decl_ty))
150 -- Result type is Q [Dec]
155 %************************************************************************
157 \subsection{Splicing an expression}
159 %************************************************************************
162 tcSpliceExpr (HsSplice name expr) res_ty
163 = setSrcSpan (getLoc expr) $
164 getStage `thenM` \ level ->
165 case spliceOK level of {
166 Nothing -> failWithTc (illegalSplice level) ;
170 Comp -> do { e <- tcTopSplice expr res_ty
171 ; returnM (unLoc e) } ;
172 Brack _ ps_var lie_var ->
174 -- A splice inside brackets
175 -- NB: ignore res_ty, apart from zapping it to a mono-type
176 -- e.g. [| reverse $(h 4) |]
177 -- Here (h 4) :: Q Exp
178 -- but $(h 4) :: forall a.a i.e. anything!
180 zapExpectedType res_ty liftedTypeKind `thenM_`
181 tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
182 setStage (Splice next_level) (
184 tcCheckRho expr meta_exp_ty
187 -- Write the pending splice into the bucket
188 readMutVar ps_var `thenM` \ ps ->
189 writeMutVar ps_var ((name,expr') : ps) `thenM_`
191 returnM (panic "tcSpliceExpr") -- The returned expression is ignored
194 -- tcTopSplice used to have this:
195 -- Note that we do not decrement the level (to -1) before
196 -- typechecking the expression. For example:
197 -- f x = $( ...$(g 3) ... )
198 -- The recursive call to tcMonoExpr will simply expand the
199 -- inner escape before dealing with the outer one
201 tcTopSplice :: LHsExpr Name -> Expected TcType -> TcM (LHsExpr Id)
202 tcTopSplice expr res_ty
203 = tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
205 -- Typecheck the expression
206 tcTopSpliceExpr expr meta_exp_ty `thenM` \ zonked_q_expr ->
208 -- Run the expression
209 traceTc (text "About to run" <+> ppr zonked_q_expr) `thenM_`
210 runMetaE convertToHsExpr zonked_q_expr `thenM` \ expr2 ->
212 traceTc (text "Got result" <+> ppr expr2) `thenM_`
214 showSplice "expression"
215 zonked_q_expr (ppr expr2) `thenM_`
217 -- Rename it, but bale out if there are errors
218 -- otherwise the type checker just gives more spurious errors
219 checkNoErrs (rnLExpr expr2) `thenM` \ (exp3, fvs) ->
221 tcMonoExpr exp3 res_ty
224 tcTopSpliceExpr :: LHsExpr Name -> TcType -> TcM (LHsExpr Id)
225 -- Type check an expression that is the body of a top-level splice
226 -- (the caller will compile and run it)
227 tcTopSpliceExpr expr meta_ty
228 = checkNoErrs $ -- checkNoErrs: must not try to run the thing
229 -- if the type checker fails!
231 setStage topSpliceStage $ do
234 do { recordThUse -- Record that TH is used (for pkg depdendency)
236 -- Typecheck the expression
237 ; (expr', lie) <- getLIE (tcCheckRho expr meta_ty)
239 -- Solve the constraints
240 ; const_binds <- tcSimplifyTop lie
243 ; zonkTopLExpr (mkHsDictLet const_binds expr') }
247 %************************************************************************
251 %************************************************************************
253 Very like splicing an expression, but we don't yet share code.
256 kcSpliceType (HsSplice name hs_expr)
257 = setSrcSpan (getLoc hs_expr) $ do
259 ; case spliceOK level of {
260 Nothing -> failWithTc (illegalSplice level) ;
261 Just next_level -> do
264 Comp -> do { (t,k) <- kcTopSpliceType hs_expr
265 ; return (unLoc t, k) } ;
266 Brack _ ps_var lie_var -> do
268 { -- A splice inside brackets
269 ; meta_ty <- tcMetaTy typeQTyConName
270 ; expr' <- setStage (Splice next_level) $
272 tcCheckRho hs_expr meta_ty
274 -- Write the pending splice into the bucket
275 ; ps <- readMutVar ps_var
276 ; writeMutVar ps_var ((name,expr') : ps)
278 -- e.g. [| Int -> $(h 4) |]
279 -- Here (h 4) :: Q Type
280 -- but $(h 4) :: forall a.a i.e. any kind
282 ; returnM (panic "kcSpliceType", kind) -- The returned type is ignored
285 kcTopSpliceType :: LHsExpr Name -> TcM (LHsType Name, TcKind)
287 = do { meta_ty <- tcMetaTy typeQTyConName
289 -- Typecheck the expression
290 ; zonked_q_expr <- tcTopSpliceExpr expr meta_ty
292 -- Run the expression
293 ; traceTc (text "About to run" <+> ppr zonked_q_expr)
294 ; hs_ty2 <- runMetaT convertToHsType zonked_q_expr
296 ; traceTc (text "Got result" <+> ppr hs_ty2)
298 ; showSplice "type" zonked_q_expr (ppr hs_ty2)
300 -- Rename it, but bale out if there are errors
301 -- otherwise the type checker just gives more spurious errors
302 ; let doc = ptext SLIT("In the spliced type") <+> ppr hs_ty2
303 ; hs_ty3 <- checkNoErrs (rnLHsType doc hs_ty2)
308 %************************************************************************
310 \subsection{Splicing an expression}
312 %************************************************************************
315 -- Always at top level
316 -- Type sig at top of file:
317 -- tcSpliceDecls :: LHsExpr Name -> TcM [LHsDecl RdrName]
319 = do { meta_dec_ty <- tcMetaTy decTyConName
320 ; meta_q_ty <- tcMetaTy qTyConName
321 ; let list_q = mkAppTy meta_q_ty (mkListTy meta_dec_ty)
322 ; zonked_q_expr <- tcTopSpliceExpr expr list_q
324 -- Run the expression
325 ; traceTc (text "About to run" <+> ppr zonked_q_expr)
326 ; decls <- runMetaD convertToHsDecls zonked_q_expr
328 ; traceTc (text "Got result" <+> vcat (map ppr decls))
329 ; showSplice "declarations"
331 (ppr (getLoc expr) $$ (vcat (map ppr decls)))
334 where handleErrors :: [Either a Message] -> TcM [a]
335 handleErrors [] = return []
336 handleErrors (Left x:xs) = liftM (x:) (handleErrors xs)
337 handleErrors (Right m:xs) = do addErrTc m
342 %************************************************************************
344 \subsection{Running an expression}
346 %************************************************************************
349 runMetaE :: (SrcSpan -> TH.Exp -> Either Message (LHsExpr RdrName))
350 -> LHsExpr Id -- Of type (Q Exp)
351 -> TcM (LHsExpr RdrName)
354 runMetaT :: (SrcSpan -> TH.Type -> Either Message (LHsType RdrName))
355 -> LHsExpr Id -- Of type (Q Type)
356 -> TcM (LHsType RdrName)
359 runMetaD :: (SrcSpan -> [TH.Dec] -> Either Message [LHsDecl RdrName])
360 -> LHsExpr Id -- Of type Q [Dec]
361 -> TcM [LHsDecl RdrName]
364 runMeta :: (SrcSpan -> th_syn -> Either Message hs_syn)
365 -> LHsExpr Id -- Of type X
366 -> TcM hs_syn -- Of type t
368 = do { hsc_env <- getTopEnv
369 ; tcg_env <- getGblEnv
370 ; this_mod <- getModule
371 ; let type_env = tcg_type_env tcg_env
372 rdr_env = tcg_rdr_env tcg_env
374 -- Compile and link it; might fail if linking fails
375 ; either_hval <- tryM $ ioToTcRn $
378 rdr_env type_env expr
379 ; case either_hval of {
380 Left exn -> failWithTc (mk_msg "compile and link" exn) ;
383 { -- Coerce it to Q t, and run it
384 -- Running might fail if it throws an exception of any kind (hence tryAllM)
385 -- including, say, a pattern-match exception in the code we are running
387 -- We also do the TH -> HS syntax conversion inside the same
388 -- exception-cacthing thing so that if there are any lurking
389 -- exceptions in the data structure returned by hval, we'll
390 -- encounter them inside the tryALlM
391 either_tval <- tryAllM $ do
392 { th_syn <- TH.runQ (unsafeCoerce# hval)
393 ; case convert (getLoc expr) th_syn of
394 Left err -> do { addErrTc err; return Nothing }
395 Right hs_syn -> return (Just hs_syn) }
397 ; case either_tval of
398 Right (Just v) -> return v
399 Right Nothing -> failM -- Error already in Tc monad
400 Left exn -> failWithTc (mk_msg "run" exn) -- Exception
403 mk_msg s exn = vcat [text "Exception when trying to" <+> text s <+> text "compile-time code:",
404 nest 2 (text (Panic.showException exn)),
405 nest 2 (text "Code:" <+> ppr expr)]
408 To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
411 instance TH.Quasi (IOEnv (Env TcGblEnv TcLclEnv)) where
412 qNewName s = do { u <- newUnique
414 ; return (TH.mkNameU s i) }
416 qReport True msg = addErr (text msg)
417 qReport False msg = addReport (text msg)
419 qCurrentModule = do { m <- getModule; return (moduleUserString m) }
423 qRunIO io = ioToTcRn io
427 %************************************************************************
429 \subsection{Errors and contexts}
431 %************************************************************************
434 showSplice :: String -> LHsExpr Id -> SDoc -> TcM ()
435 showSplice what before after
436 = getSrcSpanM `thenM` \ loc ->
437 traceSplice (vcat [ppr loc <> colon <+> text "Splicing" <+> text what,
438 nest 2 (sep [nest 2 (ppr before),
443 = ptext SLIT("Illegal bracket at level") <+> ppr level
446 = ptext SLIT("Illegal splice at level") <+> ppr level
452 %************************************************************************
456 %************************************************************************
460 reify :: TH.Name -> TcM TH.Info
462 = do { name <- lookupThName th_name
463 ; thing <- tcLookupTh name
464 -- ToDo: this tcLookup could fail, which would give a
465 -- rather unhelpful error message
466 ; traceIf (text "reify" <+> text (show th_name) <+> brackets (ppr_ns th_name) <+> ppr name)
470 ppr_ns (TH.Name _ (TH.NameG TH.DataName mod)) = text "data"
471 ppr_ns (TH.Name _ (TH.NameG TH.TcClsName mod)) = text "tc"
472 ppr_ns (TH.Name _ (TH.NameG TH.VarName mod)) = text "var"
474 lookupThName :: TH.Name -> TcM Name
475 lookupThName th_name@(TH.Name occ flavour)
476 = do { let rdr_name = thRdrName guessed_ns occ_str flavour
478 -- Repeat much of lookupOccRn, becase we want
479 -- to report errors in a TH-relevant way
480 ; rdr_env <- getLocalRdrEnv
481 ; case lookupLocalRdrEnv rdr_env rdr_name of
482 Just name -> return name
483 Nothing | not (isSrcRdrName rdr_name) -- Exact, Orig
484 -> lookupImportedName rdr_name
485 | otherwise -- Unqual, Qual
487 mb_name <- lookupSrcOcc_maybe rdr_name
489 Just name -> return name
490 Nothing -> failWithTc (notInScope th_name) }
493 -- guessed_ns is the name space guessed from looking at the TH name
494 guessed_ns | isLexCon (mkFastString occ_str) = OccName.dataName
495 | otherwise = OccName.varName
496 occ_str = TH.occString occ
498 tcLookupTh :: Name -> TcM TcTyThing
499 -- This is a specialised version of TcEnv.tcLookup; specialised mainly in that
500 -- it gives a reify-related error message on failure, whereas in the normal
501 -- tcLookup, failure is a bug.
503 = do { (gbl_env, lcl_env) <- getEnvs
504 ; case lookupNameEnv (tcl_env lcl_env) name of {
505 Just thing -> returnM thing;
507 { if nameIsLocalOrFrom (tcg_mod gbl_env) name
508 then -- It's defined in this module
509 case lookupNameEnv (tcg_type_env gbl_env) name of
510 Just thing -> return (AGlobal thing)
511 Nothing -> failWithTc (notInEnv name)
513 else do -- It's imported
514 { (eps,hpt) <- getEpsAndHpt
515 ; case lookupType hpt (eps_PTE eps) name of
516 Just thing -> return (AGlobal thing)
517 Nothing -> do { thing <- tcImportDecl name
518 ; return (AGlobal thing) }
519 -- Imported names should always be findable;
520 -- if not, we fail hard in tcImportDecl
523 notInScope :: TH.Name -> SDoc
524 notInScope th_name = quotes (text (TH.pprint th_name)) <+>
525 ptext SLIT("is not in scope at a reify")
526 -- Ugh! Rather an indirect way to display the name
528 notInEnv :: Name -> SDoc
529 notInEnv name = quotes (ppr name) <+>
530 ptext SLIT("is not in the type environment at a reify")
532 ------------------------------
533 reifyThing :: TcTyThing -> TcM TH.Info
534 -- The only reason this is monadic is for error reporting,
535 -- which in turn is mainly for the case when TH can't express
536 -- some random GHC extension
538 reifyThing (AGlobal (AnId id))
539 = do { ty <- reifyType (idType id)
540 ; fix <- reifyFixity (idName id)
541 ; let v = reifyName id
542 ; case globalIdDetails id of
543 ClassOpId cls -> return (TH.ClassOpI v ty (reifyName cls) fix)
544 other -> return (TH.VarI v ty Nothing fix)
547 reifyThing (AGlobal (ATyCon tc)) = reifyTyCon tc
548 reifyThing (AGlobal (AClass cls)) = reifyClass cls
549 reifyThing (AGlobal (ADataCon dc))
550 = do { let name = dataConName dc
551 ; ty <- reifyType (idType (dataConWrapId dc))
552 ; fix <- reifyFixity name
553 ; return (TH.DataConI (reifyName name) ty (reifyName (dataConTyCon dc)) fix) }
555 reifyThing (ATcId id _)
556 = do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
557 -- though it may be incomplete
558 ; ty2 <- reifyType ty1
559 ; fix <- reifyFixity (idName id)
560 ; return (TH.VarI (reifyName id) ty2 Nothing fix) }
562 reifyThing (ATyVar tv ty)
563 = do { ty1 <- zonkTcType ty
564 ; ty2 <- reifyType ty1
565 ; return (TH.TyVarI (reifyName tv) ty2) }
567 ------------------------------
568 reifyTyCon :: TyCon -> TcM TH.Info
570 | isFunTyCon tc = return (TH.PrimTyConI (reifyName tc) 2 False)
571 | isPrimTyCon tc = return (TH.PrimTyConI (reifyName tc) (tyConArity tc) (isUnLiftedTyCon tc))
573 = do { let (tvs, rhs) = getSynTyConDefn tc
574 ; rhs' <- reifyType rhs
575 ; return (TH.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
578 = do { cxt <- reifyCxt (tyConStupidTheta tc)
579 ; cons <- mapM reifyDataCon (tyConDataCons tc)
580 ; let name = reifyName tc
581 tvs = reifyTyVars (tyConTyVars tc)
582 deriv = [] -- Don't know about deriving
583 decl | isNewTyCon tc = TH.NewtypeD cxt name tvs (head cons) deriv
584 | otherwise = TH.DataD cxt name tvs cons deriv
585 ; return (TH.TyConI decl) }
587 reifyDataCon :: DataCon -> TcM TH.Con
589 | isVanillaDataCon dc
590 = do { arg_tys <- reifyTypes (dataConOrigArgTys dc)
591 ; let stricts = map reifyStrict (dataConStrictMarks dc)
592 fields = dataConFieldLabels dc
596 ; ASSERT( length arg_tys == length stricts )
597 if not (null fields) then
598 return (TH.RecC name (zip3 (map reifyName fields) stricts arg_tys))
600 if dataConIsInfix dc then
601 ASSERT( length arg_tys == 2 )
602 return (TH.InfixC (s1,a1) name (s2,a2))
604 return (TH.NormalC name (stricts `zip` arg_tys)) }
606 = failWithTc (ptext SLIT("Can't reify a non-Haskell-98 data constructor:")
609 ------------------------------
610 reifyClass :: Class -> TcM TH.Info
612 = do { cxt <- reifyCxt theta
613 ; ops <- mapM reify_op op_stuff
614 ; return (TH.ClassI $ TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
616 (tvs, fds, theta, _, op_stuff) = classExtraBigSig cls
617 fds' = map reifyFunDep fds
618 reify_op (op, _) = do { ty <- reifyType (idType op)
619 ; return (TH.SigD (reifyName op) ty) }
621 ------------------------------
622 reifyType :: TypeRep.Type -> TcM TH.Type
623 reifyType (TyVarTy tv) = return (TH.VarT (reifyName tv))
624 reifyType (TyConApp tc tys) = reify_tc_app (reifyName tc) tys
625 reifyType (NoteTy _ ty) = reifyType ty
626 reifyType (AppTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (r1 `TH.AppT` r2) }
627 reifyType (FunTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (TH.ArrowT `TH.AppT` r1 `TH.AppT` r2) }
628 reifyType ty@(ForAllTy _ _) = do { cxt' <- reifyCxt cxt;
629 ; tau' <- reifyType tau
630 ; return (TH.ForallT (reifyTyVars tvs) cxt' tau') }
632 (tvs, cxt, tau) = tcSplitSigmaTy ty
633 reifyTypes = mapM reifyType
634 reifyCxt = mapM reifyPred
636 reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
637 reifyFunDep (xs, ys) = TH.FunDep (map reifyName xs) (map reifyName ys)
639 reifyTyVars :: [TyVar] -> [TH.Name]
640 reifyTyVars = map reifyName
642 reify_tc_app :: TH.Name -> [TypeRep.Type] -> TcM TH.Type
643 reify_tc_app tc tys = do { tys' <- reifyTypes tys
644 ; return (foldl TH.AppT (TH.ConT tc) tys') }
646 reifyPred :: TypeRep.PredType -> TcM TH.Type
647 reifyPred (ClassP cls tys) = reify_tc_app (reifyName cls) tys
648 reifyPred p@(IParam _ _) = noTH SLIT("implicit parameters") (ppr p)
651 ------------------------------
652 reifyName :: NamedThing n => n -> TH.Name
654 | isExternalName name = mk_varg mod occ_str
655 | otherwise = TH.mkNameU occ_str (getKey (getUnique name))
656 -- Many of the things we reify have local bindings, and
657 -- NameL's aren't supposed to appear in binding positions, so
658 -- we use NameU. When/if we start to reify nested things, that
659 -- have free variables, we may need to generate NameL's for them.
662 mod = moduleUserString (nameModule name)
663 occ_str = occNameUserString occ
664 occ = nameOccName name
665 mk_varg | OccName.isDataOcc occ = TH.mkNameG_d
666 | OccName.isVarOcc occ = TH.mkNameG_v
667 | OccName.isTcOcc occ = TH.mkNameG_tc
668 | otherwise = pprPanic "reifyName" (ppr name)
670 ------------------------------
671 reifyFixity :: Name -> TcM TH.Fixity
673 = do { fix <- lookupFixityRn name
674 ; return (conv_fix fix) }
676 conv_fix (BasicTypes.Fixity i d) = TH.Fixity i (conv_dir d)
677 conv_dir BasicTypes.InfixR = TH.InfixR
678 conv_dir BasicTypes.InfixL = TH.InfixL
679 conv_dir BasicTypes.InfixN = TH.InfixN
681 reifyStrict :: BasicTypes.StrictnessMark -> TH.Strict
682 reifyStrict MarkedStrict = TH.IsStrict
683 reifyStrict MarkedUnboxed = TH.IsStrict
684 reifyStrict NotMarkedStrict = TH.NotStrict
686 ------------------------------
687 noTH :: LitString -> SDoc -> TcM a
688 noTH s d = failWithTc (hsep [ptext SLIT("Can't represent") <+> ptext s <+>
689 ptext SLIT("in Template Haskell:"),