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, mkRdrQual, mkRdrUnqual, lookupLocalRdrEnv, isSrcRdrName )
26 import RnTypes ( rnLHsType )
27 import TcExpr ( tcCheckRho, tcMonoExpr )
28 import TcHsSyn ( mkHsLet, 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, zonkTcTyVar )
34 import TcHsType ( tcHsSigType, kcHsType )
35 import TcIface ( tcImportDecl )
36 import TypeRep ( Type(..), PredType(..), TyThing(..) ) -- For reification
37 import Name ( Name, NamedThing(..), nameOccName, nameModule, isExternalName,
38 mkInternalName, nameIsLocalOrFrom )
39 import NameEnv ( lookupNameEnv )
40 import HscTypes ( lookupType, ExternalPackageState(..), emptyModDetails )
42 import Var ( Id, TyVar, idType )
43 import Module ( moduleUserString, mkModule )
45 import IfaceEnv ( lookupOrig )
46 import Class ( Class, classExtraBigSig )
47 import TyCon ( TyCon, AlgTyConRhs(..), tyConTyVars, getSynTyConDefn,
48 isSynTyCon, isNewTyCon, tyConDataCons, algTyConRhs, isPrimTyCon, isFunTyCon,
49 tyConArity, tyConStupidTheta, isUnLiftedTyCon )
50 import DataCon ( DataCon, dataConTyCon, dataConOrigArgTys, dataConStrictMarks,
51 dataConName, dataConFieldLabels, dataConWrapId, dataConIsInfix,
53 import Id ( idName, globalIdDetails )
54 import IdInfo ( GlobalIdDetails(..) )
55 import TysWiredIn ( mkListTy )
56 import DsMeta ( expQTyConName, typeQTyConName, decTyConName, qTyConName, nameTyConName )
57 import ErrUtils ( Message )
58 import SrcLoc ( noLoc, unLoc, getLoc, noSrcLoc )
60 import Unique ( Unique, Uniquable(..), getKey, mkUniqueGrimily )
62 import BasicTypes ( StrictnessMark(..), Fixity(..), FixityDirection(..) )
63 import Panic ( showException )
64 import FastString ( LitString )
66 import GHC.Base ( unsafeCoerce#, Int#, Int(..) ) -- Should have a better home in the module hierarchy
67 import Monad ( liftM )
68 import Maybes ( orElse )
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
130 -- Result type is Var (not Q-monadic)
132 tc_bracket (ExpBr expr)
133 = newTyFlexiVarTy liftedTypeKind `thenM` \ any_ty ->
134 tcCheckRho expr any_ty `thenM_`
135 tcMetaTy expQTyConName
136 -- Result type is Expr (= Q Exp)
138 tc_bracket (TypBr typ)
139 = tcHsSigType ExprSigCtxt typ `thenM_`
140 tcMetaTy typeQTyConName
141 -- Result type is Type (= Q Typ)
143 tc_bracket (DecBr decls)
144 = tcTopSrcDecls emptyModDetails decls `thenM_`
145 -- Typecheck the declarations, dicarding the result
146 -- We'll get all that stuff later, when we splice it in
148 tcMetaTy decTyConName `thenM` \ decl_ty ->
149 tcMetaTy qTyConName `thenM` \ q_ty ->
150 returnM (mkAppTy q_ty (mkListTy decl_ty))
151 -- 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 zonked_q_expr `thenM` \ simple_expr ->
213 -- simple_expr :: TH.Exp
215 expr2 :: LHsExpr RdrName
216 expr2 = convertToHsExpr simple_expr
218 traceTc (text "Got result" <+> ppr expr2) `thenM_`
220 showSplice "expression"
221 zonked_q_expr (ppr expr2) `thenM_`
223 -- Rename it, but bale out if there are errors
224 -- otherwise the type checker just gives more spurious errors
225 checkNoErrs (rnLExpr expr2) `thenM` \ (exp3, fvs) ->
227 tcMonoExpr exp3 res_ty
230 tcTopSpliceExpr :: LHsExpr Name -> TcType -> TcM (LHsExpr Id)
231 -- Type check an expression that is the body of a top-level splice
232 -- (the caller will compile and run it)
233 tcTopSpliceExpr expr meta_ty
234 = checkNoErrs $ -- checkNoErrs: must not try to run the thing
235 -- if the type checker fails!
237 setStage topSpliceStage $ do
240 do { recordThUse -- Record that TH is used (for pkg depdendency)
242 -- Typecheck the expression
243 ; (expr', lie) <- getLIE (tcCheckRho expr meta_ty)
245 -- Solve the constraints
246 ; const_binds <- tcSimplifyTop lie
249 ; zonkTopLExpr (mkHsLet const_binds expr') }
253 %************************************************************************
257 %************************************************************************
259 Very like splicing an expression, but we don't yet share code.
262 kcSpliceType (HsSplice name hs_expr)
263 = setSrcSpan (getLoc hs_expr) $ do
265 ; case spliceOK level of {
266 Nothing -> failWithTc (illegalSplice level) ;
267 Just next_level -> do
270 Comp -> do { (t,k) <- kcTopSpliceType hs_expr
271 ; return (unLoc t, k) } ;
272 Brack _ ps_var lie_var -> do
274 { -- A splice inside brackets
275 ; meta_ty <- tcMetaTy typeQTyConName
276 ; expr' <- setStage (Splice next_level) $
278 tcCheckRho hs_expr meta_ty
280 -- Write the pending splice into the bucket
281 ; ps <- readMutVar ps_var
282 ; writeMutVar ps_var ((name,expr') : ps)
284 -- e.g. [| Int -> $(h 4) |]
285 -- Here (h 4) :: Q Type
286 -- but $(h 4) :: forall a.a i.e. any kind
288 ; returnM (panic "kcSpliceType", kind) -- The returned type is ignored
291 kcTopSpliceType :: LHsExpr Name -> TcM (LHsType Name, TcKind)
293 = do { meta_ty <- tcMetaTy typeQTyConName
295 -- Typecheck the expression
296 ; zonked_q_expr <- tcTopSpliceExpr expr meta_ty
298 -- Run the expression
299 ; traceTc (text "About to run" <+> ppr zonked_q_expr)
300 ; simple_ty <- runMetaT zonked_q_expr
302 ; let -- simple_ty :: TH.Type
303 hs_ty2 :: LHsType RdrName
304 hs_ty2 = convertToHsType simple_ty
306 ; traceTc (text "Got result" <+> ppr hs_ty2)
308 ; showSplice "type" zonked_q_expr (ppr hs_ty2)
310 -- Rename it, but bale out if there are errors
311 -- otherwise the type checker just gives more spurious errors
312 ; let doc = ptext SLIT("In the spliced type") <+> ppr hs_ty2
313 ; hs_ty3 <- checkNoErrs (rnLHsType doc hs_ty2)
318 %************************************************************************
320 \subsection{Splicing an expression}
322 %************************************************************************
325 -- Always at top level
327 = do { meta_dec_ty <- tcMetaTy decTyConName
328 ; meta_q_ty <- tcMetaTy qTyConName
329 ; let list_q = mkAppTy meta_q_ty (mkListTy meta_dec_ty)
330 ; zonked_q_expr <- tcTopSpliceExpr expr list_q
332 -- Run the expression
333 ; traceTc (text "About to run" <+> ppr zonked_q_expr)
334 ; simple_expr <- runMetaD zonked_q_expr
336 -- simple_expr :: [TH.Dec]
337 -- decls :: [RdrNameHsDecl]
338 ; decls <- handleErrors (convertToHsDecls simple_expr)
339 ; traceTc (text "Got result" <+> vcat (map ppr decls))
340 ; showSplice "declarations"
341 zonked_q_expr (vcat (map ppr decls))
344 where handleErrors :: [Either a Message] -> TcM [a]
345 handleErrors [] = return []
346 handleErrors (Left x:xs) = liftM (x:) (handleErrors xs)
347 handleErrors (Right m:xs) = do addErrTc m
352 %************************************************************************
354 \subsection{Running an expression}
356 %************************************************************************
359 runMetaE :: LHsExpr Id -- Of type (Q Exp)
360 -> TcM TH.Exp -- Of type Exp
361 runMetaE e = runMeta e
363 runMetaT :: LHsExpr Id -- Of type (Q Type)
364 -> TcM TH.Type -- Of type Type
365 runMetaT e = runMeta e
367 runMetaD :: LHsExpr Id -- Of type Q [Dec]
368 -> TcM [TH.Dec] -- Of type [Dec]
369 runMetaD e = runMeta e
371 runMeta :: LHsExpr Id -- Of type X
372 -> TcM t -- Of type t
374 = do { hsc_env <- getTopEnv
375 ; tcg_env <- getGblEnv
376 ; this_mod <- getModule
377 ; let type_env = tcg_type_env tcg_env
378 rdr_env = tcg_rdr_env tcg_env
379 -- Wrap the compile-and-run in an exception-catcher
380 -- Compiling might fail if linking fails
381 -- Running might fail if it throws an exception
382 ; either_tval <- tryM $ do
384 hval <- ioToTcRn (HscMain.compileExpr
386 rdr_env type_env expr)
387 -- Coerce it to Q t, and run it
388 ; TH.runQ (unsafeCoerce# hval) }
390 ; case either_tval of
391 Left exn -> failWithTc (vcat [text "Exception when trying to run compile-time code:",
392 nest 4 (vcat [text "Code:" <+> ppr expr,
393 text ("Exn: " ++ Panic.showException exn)])])
394 Right v -> returnM v }
397 To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
400 instance TH.Quasi (IOEnv (Env TcGblEnv TcLclEnv)) where
401 qNewName s = do { u <- newUnique
403 ; return (TH.mkNameU s i) }
405 qReport True msg = addErr (text msg)
406 qReport False msg = addReport (text msg)
408 qCurrentModule = do { m <- getModule; return (moduleUserString m) }
412 qRunIO io = ioToTcRn io
416 %************************************************************************
418 \subsection{Errors and contexts}
420 %************************************************************************
423 showSplice :: String -> LHsExpr Id -> SDoc -> TcM ()
424 showSplice what before after
425 = getSrcSpanM `thenM` \ loc ->
426 traceSplice (vcat [ppr loc <> colon <+> text "Splicing" <+> text what,
427 nest 2 (sep [nest 2 (ppr before),
432 = ptext SLIT("Illegal bracket at level") <+> ppr level
435 = ptext SLIT("Illegal splice at level") <+> ppr level
441 %************************************************************************
445 %************************************************************************
449 reify :: TH.Name -> TcM TH.Info
451 = do { name <- lookupThName th_name
452 ; thing <- tcLookupTh name
453 -- ToDo: this tcLookup could fail, which would give a
454 -- rather unhelpful error message
455 ; traceIf (text "reify" <+> text (show th_name) <+> brackets (ppr_ns th_name) <+> ppr name)
459 ppr_ns (TH.Name _ (TH.NameG TH.DataName mod)) = text "data"
460 ppr_ns (TH.Name _ (TH.NameG TH.TcClsName mod)) = text "tc"
461 ppr_ns (TH.Name _ (TH.NameG TH.VarName mod)) = text "var"
463 lookupThName :: TH.Name -> TcM Name
465 = do { let rdr_name = thRdrName guessed_ns th_name
467 -- Repeat much of lookupOccRn, becase we want
468 -- to report errors in a TH-relevant way
469 ; rdr_env <- getLocalRdrEnv
470 ; case lookupLocalRdrEnv rdr_env rdr_name of
471 Just name -> return name
472 Nothing | not (isSrcRdrName rdr_name) -- Exact, Orig
473 -> lookupImportedName rdr_name
474 | otherwise -- Unqual, Qual
476 mb_name <- lookupSrcOcc_maybe rdr_name
478 Just name -> return name
479 Nothing -> failWithTc (notInScope th_name) }
482 -- guessed_ns is the name space guessed from looking at the TH name
483 guessed_ns | isLexCon occ_fs = OccName.dataName
484 | otherwise = OccName.varName
485 occ_fs = mkFastString (TH.nameBase th_name)
487 tcLookupTh :: Name -> TcM TcTyThing
488 -- This is a specialised version of TcEnv.tcLookup; specialised mainly in that
489 -- it gives a reify-related error message on failure, whereas in the normal
490 -- tcLookup, failure is a bug.
492 = do { (gbl_env, lcl_env) <- getEnvs
493 ; case lookupNameEnv (tcl_env lcl_env) name of {
494 Just thing -> returnM thing;
496 { if nameIsLocalOrFrom (tcg_mod gbl_env) name
497 then -- It's defined in this module
498 case lookupNameEnv (tcg_type_env gbl_env) name of
499 Just thing -> return (AGlobal thing)
500 Nothing -> failWithTc (notInEnv name)
502 else do -- It's imported
503 { (eps,hpt) <- getEpsAndHpt
504 ; case lookupType hpt (eps_PTE eps) name of
505 Just thing -> return (AGlobal thing)
506 Nothing -> do { thing <- tcImportDecl name
507 ; return (AGlobal thing) }
508 -- Imported names should always be findable;
509 -- if not, we fail hard in tcImportDecl
512 mk_uniq :: Int# -> Unique
513 mk_uniq u = mkUniqueGrimily (I# u)
515 notInScope :: TH.Name -> SDoc
516 notInScope th_name = quotes (text (TH.pprint th_name)) <+>
517 ptext SLIT("is not in scope at a reify")
518 -- Ugh! Rather an indirect way to display the name
520 notInEnv :: Name -> SDoc
521 notInEnv name = quotes (ppr name) <+>
522 ptext SLIT("is not in the type environment at a reify")
524 ------------------------------
525 reifyThing :: TcTyThing -> TcM TH.Info
526 -- The only reason this is monadic is for error reporting,
527 -- which in turn is mainly for the case when TH can't express
528 -- some random GHC extension
530 reifyThing (AGlobal (AnId id))
531 = do { ty <- reifyType (idType id)
532 ; fix <- reifyFixity (idName id)
533 ; let v = reifyName id
534 ; case globalIdDetails id of
535 ClassOpId cls -> return (TH.ClassOpI v ty (reifyName cls) fix)
536 other -> return (TH.VarI v ty Nothing fix)
539 reifyThing (AGlobal (ATyCon tc)) = reifyTyCon tc
540 reifyThing (AGlobal (AClass cls)) = reifyClass cls
541 reifyThing (AGlobal (ADataCon dc))
542 = do { let name = dataConName dc
543 ; ty <- reifyType (idType (dataConWrapId dc))
544 ; fix <- reifyFixity name
545 ; return (TH.DataConI (reifyName name) ty (reifyName (dataConTyCon dc)) fix) }
547 reifyThing (ATcId id _)
548 = do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
549 -- though it may be incomplete
550 ; ty2 <- reifyType ty1
551 ; fix <- reifyFixity (idName id)
552 ; return (TH.VarI (reifyName id) ty2 Nothing fix) }
554 reifyThing (ATyVar tv ty)
555 = do { ty1 <- zonkTcType ty
556 ; ty2 <- reifyType ty1
557 ; return (TH.TyVarI (reifyName tv) ty2) }
559 ------------------------------
560 reifyTyCon :: TyCon -> TcM TH.Info
562 | isFunTyCon tc = return (TH.PrimTyConI (reifyName tc) 2 False)
563 | isPrimTyCon tc = return (TH.PrimTyConI (reifyName tc) (tyConArity tc) (isUnLiftedTyCon tc))
565 = do { let (tvs, rhs) = getSynTyConDefn tc
566 ; rhs' <- reifyType rhs
567 ; return (TH.TyConI $ TH.TySynD (reifyName tc) (reifyTyVars tvs) rhs') }
570 = case algTyConRhs tc of
571 NewTyCon data_con _ _
572 -> do { cxt <- reifyCxt (tyConStupidTheta tc)
573 ; con <- reifyDataCon data_con
574 ; return (TH.TyConI $ TH.NewtypeD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
575 con [{- Don't know about deriving -}]) }
578 -> do { cxt <- reifyCxt (tyConStupidTheta tc)
579 ; cons <- mapM reifyDataCon (tyConDataCons tc)
580 ; return (TH.TyConI $ TH.DataD cxt (reifyName tc) (reifyTyVars (tyConTyVars tc))
581 cons [{- Don't know about deriving -}]) }
583 reifyDataCon :: DataCon -> TcM TH.Con
585 | isVanillaDataCon dc
586 = do { arg_tys <- reifyTypes (dataConOrigArgTys dc)
587 ; let stricts = map reifyStrict (dataConStrictMarks dc)
588 fields = dataConFieldLabels dc
592 ; ASSERT( length arg_tys == length stricts )
593 if not (null fields) then
594 return (TH.RecC name (zip3 (map reifyName fields) stricts arg_tys))
596 if dataConIsInfix dc then
597 ASSERT( length arg_tys == 2 )
598 return (TH.InfixC (s1,a1) name (s1,a2))
600 return (TH.NormalC name (stricts `zip` arg_tys)) }
602 = failWithTc (ptext SLIT("Can't reify a non-Haskell-98 data constructor:")
605 ------------------------------
606 reifyClass :: Class -> TcM TH.Info
608 = do { cxt <- reifyCxt theta
609 ; ops <- mapM reify_op op_stuff
610 ; return (TH.ClassI $ TH.ClassD cxt (reifyName cls) (reifyTyVars tvs) fds' ops) }
612 (tvs, fds, theta, _, op_stuff) = classExtraBigSig cls
613 fds' = map reifyFunDep fds
614 reify_op (op, _) = do { ty <- reifyType (idType op)
615 ; return (TH.SigD (reifyName op) ty) }
617 ------------------------------
618 reifyType :: TypeRep.Type -> TcM TH.Type
619 reifyType (TyVarTy tv) = return (TH.VarT (reifyName tv))
620 reifyType (TyConApp tc tys) = reify_tc_app (reifyName tc) tys
621 reifyType (NoteTy _ ty) = reifyType ty
622 reifyType (AppTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (r1 `TH.AppT` r2) }
623 reifyType (FunTy t1 t2) = do { [r1,r2] <- reifyTypes [t1,t2] ; return (TH.ArrowT `TH.AppT` r1 `TH.AppT` r2) }
624 reifyType ty@(ForAllTy _ _) = do { cxt' <- reifyCxt cxt;
625 ; tau' <- reifyType tau
626 ; return (TH.ForallT (reifyTyVars tvs) cxt' tau') }
628 (tvs, cxt, tau) = tcSplitSigmaTy ty
629 reifyTypes = mapM reifyType
630 reifyCxt = mapM reifyPred
632 reifyFunDep :: ([TyVar], [TyVar]) -> TH.FunDep
633 reifyFunDep (xs, ys) = TH.FunDep (map reifyName xs) (map reifyName ys)
635 reifyTyVars :: [TyVar] -> [TH.Name]
636 reifyTyVars = map reifyName
638 reify_tc_app :: TH.Name -> [TypeRep.Type] -> TcM TH.Type
639 reify_tc_app tc tys = do { tys' <- reifyTypes tys
640 ; return (foldl TH.AppT (TH.ConT tc) tys') }
642 reifyPred :: TypeRep.PredType -> TcM TH.Type
643 reifyPred (ClassP cls tys) = reify_tc_app (reifyName cls) tys
644 reifyPred p@(IParam _ _) = noTH SLIT("implicit parameters") (ppr p)
647 ------------------------------
648 reifyName :: NamedThing n => n -> TH.Name
650 | isExternalName name = mk_varg mod occ_str
651 | otherwise = TH.mkNameU occ_str (getKey (getUnique name))
652 -- Many of the things we reify have local bindings, and
653 -- NameL's aren't supposed to appear in binding positions, so
654 -- we use NameU. When/if we start to reify nested things, that
655 -- have free variables, we may need to generate NameL's for them.
658 mod = moduleUserString (nameModule name)
659 occ_str = occNameUserString occ
660 occ = nameOccName name
661 mk_varg | OccName.isDataOcc occ = TH.mkNameG_d
662 | OccName.isVarOcc occ = TH.mkNameG_v
663 | OccName.isTcOcc occ = TH.mkNameG_tc
664 | otherwise = pprPanic "reifyName" (ppr name)
666 ------------------------------
667 reifyFixity :: Name -> TcM TH.Fixity
669 = do { fix <- lookupFixityRn name
670 ; return (conv_fix fix) }
672 conv_fix (BasicTypes.Fixity i d) = TH.Fixity i (conv_dir d)
673 conv_dir BasicTypes.InfixR = TH.InfixR
674 conv_dir BasicTypes.InfixL = TH.InfixL
675 conv_dir BasicTypes.InfixN = TH.InfixN
677 reifyStrict :: BasicTypes.StrictnessMark -> TH.Strict
678 reifyStrict MarkedStrict = TH.IsStrict
679 reifyStrict MarkedUnboxed = TH.IsStrict
680 reifyStrict NotMarkedStrict = TH.NotStrict
682 ------------------------------
683 noTH :: LitString -> SDoc -> TcM a
684 noTH s d = failWithTc (hsep [ptext SLIT("Can't represent") <+> ptext s <+>
685 ptext SLIT("in Template Haskell:"),