2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 This module converts Template Haskell syntax into HsSyn
9 module Convert( convertToHsExpr, convertToHsDecls, convertToHsType, thRdrName ) where
11 #include "HsVersions.h"
13 import Language.Haskell.TH as TH hiding (sigP)
14 import Language.Haskell.TH.Syntax as TH
17 import qualified Class (FunDep)
18 import RdrName ( RdrName, mkRdrUnqual, mkRdrQual, mkOrig, getRdrName, nameRdrName )
19 import Name ( mkInternalName )
20 import Module ( Module, mkModule )
21 import RdrHsSyn ( mkClassDecl, mkTyData )
22 import qualified OccName
23 import SrcLoc ( Located(..), SrcSpan )
25 import TysWiredIn ( unitTyCon, tupleTyCon, trueDataCon )
26 import BasicTypes( Boxity(..) )
27 import ForeignCall ( Safety(..), CCallConv(..), CCallTarget(..),
29 import Char ( isAscii, isAlphaNum, isAlpha )
30 import List ( partition )
31 import Unique ( Unique, mkUniqueGrimily )
32 import ErrUtils ( Message )
33 import GLAEXTS ( Int(..), Int# )
34 import SrcLoc ( noSrcLoc )
35 import Bag ( listToBag )
41 -------------------------------------------------------------------
42 -- The external interface
44 convertToHsDecls :: SrcSpan -> [TH.Dec] -> Either Message [LHsDecl RdrName]
45 convertToHsDecls loc ds = initCvt loc (mapM cvtTop ds)
47 convertToHsExpr :: SrcSpan -> TH.Exp -> Either Message (LHsExpr RdrName)
48 convertToHsExpr loc e = initCvt loc (cvtl e)
50 convertToHsType :: SrcSpan -> TH.Type -> Either Message (LHsType RdrName)
51 convertToHsType loc t = initCvt loc (cvtType t)
54 -------------------------------------------------------------------
55 newtype CvtM a = CvtM { unCvtM :: SrcSpan -> Either Message a }
56 -- Push down the source location;
57 -- Can fail, with a single error message
59 -- NB: If the conversion succeeds with (Right x), there should
60 -- be no exception values hiding in x
61 -- Reason: so a (head []) in TH code doesn't subsequently
62 -- make GHC crash when it tries to walk the generated tree
64 -- Use the loc everywhere, for lack of anything better
65 -- In particular, we want it on binding locations, so that variables bound in
66 -- the spliced-in declarations get a location that at least relates to the splice point
68 instance Monad CvtM where
69 return x = CvtM $ \loc -> Right x
70 (CvtM m) >>= k = CvtM $ \loc -> case m loc of
72 Right v -> unCvtM (k v) loc
74 initCvt :: SrcSpan -> CvtM a -> Either Message a
75 initCvt loc (CvtM m) = m loc
78 force a = a `seq` return a
80 failWith :: Message -> CvtM a
81 failWith m = CvtM (\loc -> Left full_msg)
83 full_msg = m $$ ptext SLIT("When splicing generated code into the program")
85 returnL :: a -> CvtM (Located a)
86 returnL x = CvtM (\loc -> Right (L loc x))
88 wrapL :: CvtM a -> CvtM (Located a)
89 wrapL (CvtM m) = CvtM (\loc -> case m loc of
91 Right v -> Right (L loc v))
93 -------------------------------------------------------------------
94 cvtTop :: TH.Dec -> CvtM (LHsDecl RdrName)
95 cvtTop d@(TH.ValD _ _ _) = do { L loc d' <- cvtBind d; return (L loc $ Hs.ValD d') }
96 cvtTop d@(TH.FunD _ _) = do { L loc d' <- cvtBind d; return (L loc $ Hs.ValD d') }
97 cvtTop (TH.SigD nm typ) = do { nm' <- vNameL nm
99 ; returnL $ Hs.SigD (TypeSig nm' ty') }
101 cvtTop (TySynD tc tvs rhs)
102 = do { tc' <- tconNameL tc
104 ; rhs' <- cvtType rhs
105 ; returnL $ TyClD (TySynonym tc' tvs' rhs') }
107 cvtTop (DataD ctxt tc tvs constrs derivs)
108 = do { stuff <- cvt_tycl_hdr ctxt tc tvs
109 ; cons' <- mapM cvtConstr constrs
110 ; derivs' <- cvtDerivs derivs
111 ; returnL $ TyClD (mkTyData DataType stuff Nothing cons' derivs') }
114 cvtTop (NewtypeD ctxt tc tvs constr derivs)
115 = do { stuff <- cvt_tycl_hdr ctxt tc tvs
116 ; con' <- cvtConstr constr
117 ; derivs' <- cvtDerivs derivs
118 ; returnL $ TyClD (mkTyData NewType stuff Nothing [con'] derivs') }
120 cvtTop (ClassD ctxt cl tvs fds decs)
121 = do { stuff <- cvt_tycl_hdr ctxt cl tvs
122 ; fds' <- mapM cvt_fundep fds
123 ; (binds', sigs') <- cvtBindsAndSigs decs
124 ; returnL $ TyClD $ mkClassDecl stuff fds' sigs' binds' }
126 cvtTop (InstanceD tys ty decs)
127 = do { (binds', sigs') <- cvtBindsAndSigs decs
128 ; ctxt' <- cvtContext tys
129 ; L loc pred' <- cvtPred ty
130 ; inst_ty' <- returnL $ mkImplicitHsForAllTy ctxt' (L loc (HsPredTy pred'))
131 ; returnL $ InstD (InstDecl inst_ty' binds' sigs') }
133 cvtTop (ForeignD ford) = do { ford' <- cvtForD ford; returnL $ ForD ford' }
135 cvt_tycl_hdr cxt tc tvs
136 = do { cxt' <- cvtContext cxt
137 ; tc' <- tconNameL tc
139 ; return (cxt', tc', tvs') }
141 ---------------------------------------------------
143 -- Can't handle GADTs yet
144 ---------------------------------------------------
146 cvtConstr (NormalC c strtys)
147 = do { c' <- cNameL c
149 ; tys' <- mapM cvt_arg strtys
150 ; returnL $ ConDecl c' Explicit noExistentials cxt' (PrefixCon tys') ResTyH98 }
152 cvtConstr (RecC c varstrtys)
153 = do { c' <- cNameL c
155 ; args' <- mapM cvt_id_arg varstrtys
156 ; returnL $ ConDecl c' Explicit noExistentials cxt' (RecCon args') ResTyH98 }
158 cvtConstr (InfixC st1 c st2)
159 = do { c' <- cNameL c
161 ; st1' <- cvt_arg st1
162 ; st2' <- cvt_arg st2
163 ; returnL $ ConDecl c' Explicit noExistentials cxt' (InfixCon st1' st2') ResTyH98 }
165 cvtConstr (ForallC tvs ctxt (ForallC tvs' ctxt' con'))
166 = cvtConstr (ForallC (tvs ++ tvs') (ctxt ++ ctxt') con')
168 cvtConstr (ForallC tvs ctxt con)
169 = do { L _ con' <- cvtConstr con
171 ; ctxt' <- cvtContext ctxt
173 ConDecl l _ [] (L _ []) x ResTyH98
174 -> returnL $ ConDecl l Explicit tvs' ctxt' x ResTyH98
175 c -> panic "ForallC: Can't happen" }
177 cvt_arg (IsStrict, ty) = do { ty' <- cvtType ty; returnL $ HsBangTy HsStrict ty' }
178 cvt_arg (NotStrict, ty) = cvtType ty
180 cvt_id_arg (i, str, ty) = do { i' <- vNameL i
181 ; ty' <- cvt_arg (str,ty)
184 cvtDerivs [] = return Nothing
185 cvtDerivs cs = do { cs' <- mapM cvt_one cs
186 ; return (Just cs') }
188 cvt_one c = do { c' <- tconName c
189 ; returnL $ HsPredTy $ HsClassP c' [] }
191 cvt_fundep :: FunDep -> CvtM (Located (Class.FunDep RdrName))
192 cvt_fundep (FunDep xs ys) = do { xs' <- mapM tName xs; ys' <- mapM tName ys; returnL (xs', ys') }
196 ------------------------------------------
197 -- Foreign declarations
198 ------------------------------------------
200 cvtForD :: Foreign -> CvtM (ForeignDecl RdrName)
201 cvtForD (ImportF callconv safety from nm ty)
202 | Just (c_header, cis) <- parse_ccall_impent (TH.nameBase nm) from
203 = do { nm' <- vNameL nm
205 ; let i = CImport (cvt_conv callconv) safety' c_header nilFS cis
206 ; return $ ForeignImport nm' ty' i False }
209 = failWith $ text (show from)<+> ptext SLIT("is not a valid ccall impent")
211 safety' = case safety of
213 Safe -> PlaySafe False
214 Threadsafe -> PlaySafe True
216 cvtForD (ExportF callconv as nm ty)
217 = do { nm' <- vNameL nm
219 ; let e = CExport (CExportStatic (mkFastString as) (cvt_conv callconv))
220 ; return $ ForeignExport nm' ty' e False }
222 cvt_conv CCall = CCallConv
223 cvt_conv StdCall = StdCallConv
225 parse_ccall_impent :: String -> String -> Maybe (FastString, CImportSpec)
226 parse_ccall_impent nm s
227 = case lex_ccall_impent s of
228 Just ["dynamic"] -> Just (nilFS, CFunction DynamicTarget)
229 Just ["wrapper"] -> Just (nilFS, CWrapper)
230 Just ("static":ts) -> parse_ccall_impent_static nm ts
231 Just ts -> parse_ccall_impent_static nm ts
234 parse_ccall_impent_static :: String
236 -> Maybe (FastString, CImportSpec)
237 parse_ccall_impent_static nm ts
238 = let ts' = case ts of
239 [ "&", cid] -> [ cid]
240 [fname, "&" ] -> [fname ]
241 [fname, "&", cid] -> [fname, cid]
244 [ cid] | is_cid cid -> Just (nilFS, mk_cid cid)
245 [fname, cid] | is_cid cid -> Just (mkFastString fname, mk_cid cid)
246 [ ] -> Just (nilFS, mk_cid nm)
247 [fname ] -> Just (mkFastString fname, mk_cid nm)
249 where is_cid :: String -> Bool
250 is_cid x = all (/= '.') x && (isAlpha (head x) || head x == '_')
251 mk_cid :: String -> CImportSpec
252 mk_cid = CFunction . StaticTarget . mkFastString
254 lex_ccall_impent :: String -> Maybe [String]
255 lex_ccall_impent "" = Just []
256 lex_ccall_impent ('&':xs) = fmap ("&":) $ lex_ccall_impent xs
257 lex_ccall_impent (' ':xs) = lex_ccall_impent xs
258 lex_ccall_impent ('\t':xs) = lex_ccall_impent xs
259 lex_ccall_impent xs = case span is_valid xs of
261 (t, xs') -> fmap (t:) $ lex_ccall_impent xs'
262 where is_valid :: Char -> Bool
263 is_valid c = isAscii c && (isAlphaNum c || c `elem` "._")
266 ---------------------------------------------------
268 ---------------------------------------------------
270 cvtDecs :: [TH.Dec] -> CvtM (HsLocalBinds RdrName)
271 cvtDecs [] = return EmptyLocalBinds
272 cvtDecs ds = do { (binds,sigs) <- cvtBindsAndSigs ds
273 ; return (HsValBinds (ValBindsIn binds sigs)) }
276 = do { binds' <- mapM cvtBind binds; sigs' <- mapM cvtSig sigs
277 ; return (listToBag binds', sigs') }
279 (sigs, binds) = partition is_sig ds
281 is_sig (TH.SigD _ _) = True
284 cvtSig (TH.SigD nm ty)
285 = do { nm' <- vNameL nm; ty' <- cvtType ty; returnL (Hs.TypeSig nm' ty') }
287 cvtBind :: TH.Dec -> CvtM (LHsBind RdrName)
288 -- Used only for declarations in a 'let/where' clause,
289 -- not for top level decls
290 cvtBind (TH.ValD (TH.VarP s) body ds)
291 = do { s' <- vNameL s
292 ; cl' <- cvtClause (Clause [] body ds)
293 ; returnL $ FunBind s' False (mkMatchGroup [cl']) placeHolderNames }
295 cvtBind (TH.FunD nm cls)
296 = do { nm' <- vNameL nm
297 ; cls' <- mapM cvtClause cls
298 ; returnL $ FunBind nm' False (mkMatchGroup cls') placeHolderNames }
300 cvtBind (TH.ValD p body ds)
301 = do { p' <- cvtPat p
302 ; g' <- cvtGuard body
304 ; returnL $ PatBind p' (GRHSs g' ds') void placeHolderNames }
307 = failWith (sep [ptext SLIT("Illegal kind of declaration in where clause"),
308 nest 2 (text (TH.pprint d))])
311 cvtClause :: TH.Clause -> CvtM (Hs.LMatch RdrName)
312 cvtClause (Clause ps body wheres)
313 = do { ps' <- cvtPats ps
314 ; g' <- cvtGuard body
315 ; ds' <- cvtDecs wheres
316 ; returnL $ Hs.Match ps' Nothing (GRHSs g' ds') }
319 -------------------------------------------------------------------
321 -------------------------------------------------------------------
323 cvtl :: TH.Exp -> CvtM (LHsExpr RdrName)
324 cvtl e = wrapL (cvt e)
326 cvt (VarE s) = do { s' <- vName s; return $ HsVar s' }
327 cvt (ConE s) = do { s' <- cName s; return $ HsVar s' }
329 | overloadedLit l = do { l' <- cvtOverLit l; return $ HsOverLit l' }
330 | otherwise = do { l' <- cvtLit l; return $ HsLit l' }
332 cvt (AppE x y) = do { x' <- cvtl x; y' <- cvtl y; return $ HsApp x' y' }
333 cvt (LamE ps e) = do { ps' <- cvtPats ps; e' <- cvtl e
334 ; return $ HsLam (mkMatchGroup [mkSimpleMatch ps' e']) }
335 cvt (TupE [e]) = cvt e
336 cvt (TupE es) = do { es' <- mapM cvtl es; return $ ExplicitTuple es' Boxed }
337 cvt (CondE x y z) = do { x' <- cvtl x; y' <- cvtl y; z' <- cvtl z
338 ; return $ HsIf x' y' z' }
339 cvt (LetE ds e) = do { ds' <- cvtDecs ds; e' <- cvtl e; return $ HsLet ds' e' }
340 cvt (CaseE e ms) = do { e' <- cvtl e; ms' <- mapM cvtMatch ms
341 ; return $ HsCase e' (mkMatchGroup ms') }
342 cvt (DoE ss) = cvtHsDo DoExpr ss
343 cvt (CompE ss) = cvtHsDo ListComp ss
344 cvt (ArithSeqE dd) = do { dd' <- cvtDD dd; return $ ArithSeq noPostTcExpr dd' }
345 cvt (ListE xs) = do { xs' <- mapM cvtl xs; return $ ExplicitList void xs' }
346 cvt (InfixE (Just x) s (Just y)) = do { x' <- cvtl x; s' <- cvtl s; y' <- cvtl y
347 ; e' <- returnL $ OpApp x' s' undefined y'
348 ; return $ HsPar e' }
349 cvt (InfixE Nothing s (Just y)) = do { s' <- cvtl s; y' <- cvtl y
350 ; return $ SectionR s' y' }
351 cvt (InfixE (Just x) s Nothing ) = do { x' <- cvtl x; s' <- cvtl s
352 ; return $ SectionL x' s' }
353 cvt (InfixE Nothing s Nothing ) = cvt s -- Can I indicate this is an infix thing?
355 cvt (SigE e t) = do { e' <- cvtl e; t' <- cvtType t
356 ; return $ ExprWithTySig e' t' }
357 cvt (RecConE c flds) = do { c' <- cNameL c
358 ; flds' <- mapM cvtFld flds
359 ; return $ RecordCon c' noPostTcExpr flds' }
360 cvt (RecUpdE e flds) = do { e' <- cvtl e
361 ; flds' <- mapM cvtFld flds
362 ; return $ RecordUpd e' flds' placeHolderType placeHolderType }
364 cvtFld (v,e) = do { v' <- vNameL v; e' <- cvtl e; return (v',e') }
366 cvtDD :: Range -> CvtM (ArithSeqInfo RdrName)
367 cvtDD (FromR x) = do { x' <- cvtl x; return $ From x' }
368 cvtDD (FromThenR x y) = do { x' <- cvtl x; y' <- cvtl y; return $ FromThen x' y' }
369 cvtDD (FromToR x y) = do { x' <- cvtl x; y' <- cvtl y; return $ FromTo x' y' }
370 cvtDD (FromThenToR x y z) = do { x' <- cvtl x; y' <- cvtl y; z' <- cvtl z; return $ FromThenTo x' y' z' }
372 -------------------------------------
373 -- Do notation and statements
374 -------------------------------------
376 cvtHsDo do_or_lc stmts
377 = do { stmts' <- cvtStmts stmts
378 ; let body = case last stmts' of
379 L _ (ExprStmt body _ _) -> body
380 ; return $ HsDo do_or_lc (init stmts') body void }
382 cvtStmts = mapM cvtStmt
384 cvtStmt :: TH.Stmt -> CvtM (Hs.LStmt RdrName)
385 cvtStmt (NoBindS e) = do { e' <- cvtl e; returnL $ mkExprStmt e' }
386 cvtStmt (TH.BindS p e) = do { p' <- cvtPat p; e' <- cvtl e; returnL $ mkBindStmt p' e' }
387 cvtStmt (TH.LetS ds) = do { ds' <- cvtDecs ds; returnL $ LetStmt ds' }
388 cvtStmt (TH.ParS dss) = do { dss' <- mapM cvt_one dss; returnL $ ParStmt dss' }
390 cvt_one ds = do { ds' <- cvtStmts ds; return (ds', undefined) }
392 cvtMatch :: TH.Match -> CvtM (Hs.LMatch RdrName)
393 cvtMatch (TH.Match p body decs)
394 = do { p' <- cvtPat p
395 ; g' <- cvtGuard body
396 ; decs' <- cvtDecs decs
397 ; returnL $ Hs.Match [p'] Nothing (GRHSs g' decs') }
399 cvtGuard :: TH.Body -> CvtM [LGRHS RdrName]
400 cvtGuard (GuardedB pairs) = mapM cvtpair pairs
401 cvtGuard (NormalB e) = do { e' <- cvtl e; g' <- returnL $ GRHS [] e'; return [g'] }
403 cvtpair :: (TH.Guard, TH.Exp) -> CvtM (LGRHS RdrName)
404 cvtpair (NormalG ge,rhs) = do { ge' <- cvtl ge; rhs' <- cvtl rhs
405 ; g' <- returnL $ mkBindStmt truePat ge'
406 ; returnL $ GRHS [g'] rhs' }
407 cvtpair (PatG gs,rhs) = do { gs' <- cvtStmts gs; rhs' <- cvtl rhs
408 ; returnL $ GRHS gs' rhs' }
410 cvtOverLit :: Lit -> CvtM (HsOverLit RdrName)
411 cvtOverLit (IntegerL i) = do { force i; return $ mkHsIntegral i }
412 cvtOverLit (RationalL r) = do { force r; return $ mkHsFractional r }
413 -- An Integer is like an an (overloaded) '3' in a Haskell source program
414 -- Similarly 3.5 for fractionals
416 cvtLit :: Lit -> CvtM HsLit
417 cvtLit (IntPrimL i) = do { force i; return $ HsIntPrim i }
418 cvtLit (FloatPrimL f) = do { force f; return $ HsFloatPrim f }
419 cvtLit (DoublePrimL f) = do { force f; return $ HsDoublePrim f }
420 cvtLit (CharL c) = do { force c; return $ HsChar c }
421 cvtLit (StringL s) = do { let { s' = mkFastString s }; force s'; return $ HsString s' }
423 cvtPats :: [TH.Pat] -> CvtM [Hs.LPat RdrName]
424 cvtPats pats = mapM cvtPat pats
426 cvtPat :: TH.Pat -> CvtM (Hs.LPat RdrName)
427 cvtPat pat = wrapL (cvtp pat)
429 cvtp :: TH.Pat -> CvtM (Hs.Pat RdrName)
431 | overloadedLit l = do { l' <- cvtOverLit l
432 ; return (mkNPat l' Nothing) }
433 -- Not right for negative patterns;
434 -- need to think about that!
435 | otherwise = do { l' <- cvtLit l; return $ Hs.LitPat l' }
436 cvtp (TH.VarP s) = do { s' <- vName s; return $ Hs.VarPat s' }
437 cvtp (TupP [p]) = cvtp p
438 cvtp (TupP ps) = do { ps' <- cvtPats ps; return $ TuplePat ps' Boxed }
439 cvtp (ConP s ps) = do { s' <- cNameL s; ps' <- cvtPats ps; return $ ConPatIn s' (PrefixCon ps') }
440 cvtp (InfixP p1 s p2) = do { s' <- cNameL s; p1' <- cvtPat p1; p2' <- cvtPat p2
441 ; return $ ConPatIn s' (InfixCon p1' p2') }
442 cvtp (TildeP p) = do { p' <- cvtPat p; return $ LazyPat p' }
443 cvtp (TH.AsP s p) = do { s' <- vNameL s; p' <- cvtPat p; return $ AsPat s' p' }
444 cvtp TH.WildP = return $ WildPat void
445 cvtp (RecP c fs) = do { c' <- cNameL c; fs' <- mapM cvtPatFld fs
446 ; return $ ConPatIn c' $ Hs.RecCon fs' }
447 cvtp (ListP ps) = do { ps' <- cvtPats ps; return $ ListPat ps' void }
448 cvtp (SigP p t) = do { p' <- cvtPat p; t' <- cvtType t; return $ SigPatIn p' t' }
450 cvtPatFld (s,p) = do { s' <- vNameL s; p' <- cvtPat p; return (s',p') }
452 -----------------------------------------------------------
453 -- Types and type variables
455 cvtTvs :: [TH.Name] -> CvtM [LHsTyVarBndr RdrName]
456 cvtTvs tvs = mapM cvt_tv tvs
458 cvt_tv tv = do { tv' <- tName tv; returnL $ UserTyVar tv' }
460 cvtContext :: Cxt -> CvtM (LHsContext RdrName)
461 cvtContext tys = do { preds' <- mapM cvtPred tys; returnL preds' }
463 cvtPred :: TH.Type -> CvtM (LHsPred RdrName)
465 = do { (head, tys') <- split_ty_app ty
467 ConT tc -> do { tc' <- tconName tc; returnL $ HsClassP tc' tys' }
468 VarT tv -> do { tv' <- tName tv; returnL $ HsClassP tv' tys' }
469 other -> failWith (ptext SLIT("Malformed predicate") <+> text (TH.pprint ty)) }
471 cvtType :: TH.Type -> CvtM (LHsType RdrName)
472 cvtType ty = do { (head, tys') <- split_ty_app ty
474 TupleT n | length tys' == n -> returnL (HsTupleTy Boxed tys')
475 | n == 0 -> mk_apps (HsTyVar (getRdrName unitTyCon)) tys'
476 | otherwise -> mk_apps (HsTyVar (getRdrName (tupleTyCon Boxed n))) tys'
477 ArrowT | [x',y'] <- tys' -> returnL (HsFunTy x' y')
478 ListT | [x'] <- tys' -> returnL (HsListTy x')
479 VarT nm -> do { nm' <- tName nm; mk_apps (HsTyVar nm') tys' }
480 ConT nm -> do { nm' <- tconName nm; mk_apps (HsTyVar nm') tys' }
482 ForallT tvs cxt ty | null tys' -> do { tvs' <- cvtTvs tvs
483 ; cxt' <- cvtContext cxt
485 ; returnL $ mkExplicitHsForAllTy tvs' cxt' ty' }
486 otherwise -> failWith (ptext SLIT("Malformed type") <+> text (show ty))
489 mk_apps head [] = returnL head
490 mk_apps head (ty:tys) = do { head' <- returnL head; mk_apps (HsAppTy head' ty) tys }
492 split_ty_app :: TH.Type -> CvtM (TH.Type, [LHsType RdrName])
493 split_ty_app ty = go ty []
495 go (AppT f a) as' = do { a' <- cvtType a; go f (a':as') }
496 go f as = return (f,as)
498 -----------------------------------------------------------
501 -----------------------------------------------------------
502 -- some useful things
504 truePat = nlConPat (getRdrName trueDataCon) []
506 overloadedLit :: Lit -> Bool
507 -- True for literals that Haskell treats as overloaded
508 overloadedLit (IntegerL l) = True
509 overloadedLit (RationalL l) = True
510 overloadedLit l = False
513 void = placeHolderType
515 --------------------------------------------------------------------
516 -- Turning Name back into RdrName
517 --------------------------------------------------------------------
520 vNameL, cNameL, tconNameL :: TH.Name -> CvtM (Located RdrName)
521 vName, cName, tName, tconName :: TH.Name -> CvtM RdrName
523 vNameL n = wrapL (vName n)
524 vName n = force (thRdrName OccName.varName n)
526 -- Constructor function names; this is Haskell source, hence srcDataName
527 cNameL n = wrapL (cName n)
528 cName n = force (thRdrName OccName.srcDataName n)
530 -- Type variable names
531 tName n = force (thRdrName OccName.tvName n)
533 -- Type Constructor names
534 tconNameL n = wrapL (tconName n)
535 tconName n = force (thRdrName OccName.tcName n)
537 thRdrName :: OccName.NameSpace -> TH.Name -> RdrName
538 -- This turns a Name into a RdrName
539 -- The passed-in name space tells what the context is expecting;
540 -- use it unless the TH name knows what name-space it comes
541 -- from, in which case use the latter
543 -- The strict applications ensure that any buried exceptions get forced
544 thRdrName ctxt_ns (TH.Name occ (TH.NameG th_ns mod)) = (mkOrig $! (mk_mod mod)) $! (mk_occ (mk_ghc_ns th_ns) occ)
545 thRdrName ctxt_ns (TH.Name occ (TH.NameL uniq)) = nameRdrName $! (((mkInternalName $! (mk_uniq uniq)) $! (mk_occ ctxt_ns occ)) noSrcLoc)
546 thRdrName ctxt_ns (TH.Name occ (TH.NameQ mod)) = (mkRdrQual $! (mk_mod mod)) $! (mk_occ ctxt_ns occ)
547 thRdrName ctxt_ns (TH.Name occ TH.NameS) = mkRdrUnqual $! (mk_occ ctxt_ns occ)
548 thRdrName ctxt_ns (TH.Name occ (TH.NameU uniq)) = mkRdrUnqual $! (mk_uniq_occ ctxt_ns occ uniq)
550 mk_uniq_occ :: OccName.NameSpace -> TH.OccName -> Int# -> OccName.OccName
551 mk_uniq_occ ns occ uniq
552 = OccName.mkOccName ns (TH.occString occ ++ '[' : shows (mk_uniq uniq) "]")
553 -- The idea here is to make a name that
554 -- a) the user could not possibly write, and
555 -- b) cannot clash with another NameU
556 -- Previously I generated an Exact RdrName with mkInternalName.
557 -- This works fine for local binders, but does not work at all for
558 -- top-level binders, which must have External Names, since they are
559 -- rapidly baked into data constructors and the like. Baling out
560 -- and generating an unqualified RdrName here is the simple solution
562 mk_ghc_ns :: TH.NameSpace -> OccName.NameSpace
563 mk_ghc_ns DataName = OccName.dataName
564 mk_ghc_ns TH.TcClsName = OccName.tcClsName
565 mk_ghc_ns TH.VarName = OccName.varName
567 -- The packing and unpacking is rather turgid :-(
568 mk_occ :: OccName.NameSpace -> TH.OccName -> OccName.OccName
569 mk_occ ns occ = OccName.mkOccFS ns (mkFastString (TH.occString occ))
571 mk_mod :: TH.ModName -> Module
572 mk_mod mod = mkModule (TH.modString mod)
574 mk_uniq :: Int# -> Unique
575 mk_uniq u = mkUniqueGrimily (I# u)