2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 This module converts Template Haskell syntax into HsSyn
9 module Convert( convertToHsExpr, convertToHsDecls ) where
11 #include "HsVersions.h"
13 import Language.Haskell.THSyntax as Meta
16 ( HsExpr(..), HsLit(..), ArithSeqInfo(..),
18 Match(..), GRHSs(..), GRHS(..), HsPred(..),
19 HsDecl(..), InstDecl(..), ConDecl(..),
20 Stmt(..), HsBinds(..), MonoBinds(..), Sig(..),
21 Pat(..), HsConDetails(..), HsOverLit, BangType(..),
22 placeHolderType, HsType(..), HsTupCon(..),
23 HsTyVarBndr(..), HsContext,
27 import RdrName ( RdrName, mkRdrUnqual, mkRdrQual, mkOrig )
28 import Module ( mkModuleName )
29 import RdrHsSyn ( mkHsIntegral, mkHsFractional, mkClassDecl, mkTyData )
31 import SrcLoc ( SrcLoc, generatedSrcLoc )
32 import TyCon ( DataConDetails(..) )
34 import BasicTypes( Boxity(..), RecFlag(Recursive),
35 NewOrData(..), StrictnessMark(..) )
36 import FastString( mkFastString )
37 import Char ( ord, isAlphaNum )
38 import List ( partition )
42 -------------------------------------------------------------------
43 convertToHsDecls :: [Meta.Dec] -> [HsDecl RdrName]
44 convertToHsDecls ds = map cvt_top ds
47 cvt_top d@(Val _ _ _) = ValD (cvtd d)
48 cvt_top d@(Fun _ _) = ValD (cvtd d)
50 cvt_top (Data tc tvs constrs derivs)
51 = TyClD (mkTyData DataType
52 (noContext, tconName tc, cvt_tvs tvs)
53 (DataCons (map mk_con constrs))
54 (mk_derivs derivs) loc0)
57 = ConDecl (cName c) noExistentials noContext
58 (PrefixCon (map mk_arg tys)) loc0
60 mk_arg ty = BangType NotMarkedStrict (cvtType ty)
62 mk_derivs [] = Nothing
63 mk_derivs cs = Just [HsClassP (tconName c) [] | c <- cs]
65 cvt_top (Class ctxt cl tvs decs)
66 = TyClD (mkClassDecl (cvt_context ctxt, tconName cl, cvt_tvs tvs)
68 sigs (Just binds) loc0)
70 (binds,sigs) = cvtBindsAndSigs decs
72 cvt_top (Instance tys ty decs)
73 = InstD (InstDecl inst_ty binds sigs Nothing loc0)
75 (binds, sigs) = cvtBindsAndSigs decs
76 inst_ty = HsForAllTy Nothing
78 (HsPredTy (cvt_pred ty))
80 cvt_top (Proto nm typ) = SigD (Sig (vName nm) (cvtType typ) loc0)
86 -------------------------------------------------------------------
87 convertToHsExpr :: Meta.Exp -> HsExpr RdrName
90 cvt (Var s) = HsVar(vName s)
91 cvt (Con s) = HsVar(cName s)
93 | overloadedLit l = HsOverLit (cvtOverLit l)
94 | otherwise = HsLit (cvtLit l)
96 cvt (App x y) = HsApp (cvt x) (cvt y)
97 cvt (Lam ps e) = HsLam (mkSimpleMatch (map cvtp ps) (cvt e) void loc0)
98 cvt (Tup es) = ExplicitTuple(map cvt es) Boxed
99 cvt (Cond x y z) = HsIf (cvt x) (cvt y) (cvt z) loc0
100 cvt (Let ds e) = HsLet (cvtdecs ds) (cvt e)
101 cvt (Case e ms) = HsCase (cvt e) (map cvtm ms) loc0
102 cvt (Do ss) = HsDo DoExpr (cvtstmts ss) [] void loc0
103 cvt (Comp ss) = HsDo ListComp (cvtstmts ss) [] void loc0
104 cvt (ArithSeq dd) = ArithSeqIn (cvtdd dd)
105 cvt (ListExp xs) = ExplicitList void (map cvt xs)
106 cvt (Infix (Just x) s (Just y)) = OpApp (cvt x) (HsVar(vName s)) undefined (cvt y)
107 cvt (Infix Nothing s (Just y)) = SectionR (HsVar(vName s)) (cvt y)
108 cvt (Infix (Just x) s Nothing ) = SectionL (cvt x) (HsVar(vName s))
109 cvt (Infix Nothing s Nothing ) = HsVar(vName s) -- Can I indicate this is an infix thing?
110 cvt (SigExp e t) = ExprWithTySig (cvt e) (cvtType t)
112 cvtdecs :: [Meta.Dec] -> HsBinds RdrName
113 cvtdecs [] = EmptyBinds
114 cvtdecs ds = MonoBind binds sigs Recursive
116 (binds, sigs) = cvtBindsAndSigs ds
119 = (cvtds non_sigs, map cvtSig sigs)
121 (sigs, non_sigs) = partition sigP ds
123 cvtSig (Proto nm typ) = Sig (vName nm) (cvtType typ) loc0
125 cvtds :: [Meta.Dec] -> MonoBinds RdrName
126 cvtds [] = EmptyMonoBinds
127 cvtds (d:ds) = AndMonoBinds (cvtd d) (cvtds ds)
129 cvtd :: Meta.Dec -> MonoBinds RdrName
130 -- Used only for declarations in a 'let/where' clause,
131 -- not for top level decls
132 cvtd (Val (Pvar s) body ds) = FunMonoBind (vName s) False
133 (panic "what now?") loc0
134 cvtd (Fun nm cls) = FunMonoBind (vName nm) False (map cvtclause cls) loc0
135 cvtd (Val p body ds) = PatMonoBind (cvtp p) (GRHSs (cvtguard body)
138 cvtd x = panic "Illegal kind of declaration in where clause"
141 cvtclause :: Meta.Clause (Meta.Pat) (Meta.Exp) (Meta.Dec) -> Hs.Match RdrName
142 cvtclause (ps,body,wheres) = Match (map cvtp ps) Nothing
143 (GRHSs (cvtguard body) (cvtdecs wheres) void)
147 cvtdd :: Meta.DDt -> ArithSeqInfo RdrName
148 cvtdd (Meta.From x) = (Hs.From (cvt x))
149 cvtdd (Meta.FromThen x y) = (Hs.FromThen (cvt x) (cvt y))
150 cvtdd (Meta.FromTo x y) = (Hs.FromTo (cvt x) (cvt y))
151 cvtdd (Meta.FromThenTo x y z) = (Hs.FromThenTo (cvt x) (cvt y) (cvt z))
154 cvtstmts :: [Meta.Stm] -> [Hs.Stmt RdrName]
155 cvtstmts [] = [] -- this is probably an error as every [stmt] should end with ResultStmt
156 cvtstmts [NoBindSt e] = [ResultStmt (cvt e) loc0] -- when its the last element use ResultStmt
157 cvtstmts (NoBindSt e : ss) = ExprStmt (cvt e) void loc0 : cvtstmts ss
158 cvtstmts (BindSt p e : ss) = BindStmt (cvtp p) (cvt e) loc0 : cvtstmts ss
159 cvtstmts (LetSt ds : ss) = LetStmt (cvtdecs ds) : cvtstmts ss
160 cvtstmts (ParSt dss : ss) = ParStmt(map cvtstmts dss) : cvtstmts ss
163 cvtm :: Meta.Mat -> Hs.Match RdrName
164 cvtm (p,body,wheres) = Match [cvtp p] Nothing
165 (GRHSs (cvtguard body) (cvtdecs wheres) void)
167 cvtguard :: Meta.Rhs -> [GRHS RdrName]
168 cvtguard (Guarded pairs) = map cvtpair pairs
169 cvtguard (Normal e) = [GRHS [ ResultStmt (cvt e) loc0 ] loc0]
171 cvtpair :: (Meta.Exp,Meta.Exp) -> GRHS RdrName
172 cvtpair (x,y) = GRHS [BindStmt truePat (cvt x) loc0,
173 ResultStmt (cvt y) loc0] loc0
175 cvtOverLit :: Lit -> HsOverLit
176 cvtOverLit (Int i) = mkHsIntegral (fromInt i)
177 cvtOverLit (Rational r) = mkHsFractional r
178 -- An Int is like an an (overloaded) '3' in a Haskell source program
179 -- Similarly 3.5 for fractionals
181 cvtLit :: Lit -> HsLit
182 cvtLit (Char c) = HsChar (ord c)
183 cvtLit (String s) = HsString (mkFastString s)
185 cvtp :: Meta.Pat -> Hs.Pat RdrName
187 | overloadedLit l = NPatIn (cvtOverLit l) Nothing -- Not right for negative
188 -- patterns; need to think
190 | otherwise = LitPat (cvtLit l)
191 cvtp (Pvar s) = VarPat(vName s)
192 cvtp (Ptup ps) = TuplePat (map cvtp ps) Boxed
193 cvtp (Pcon s ps) = ConPatIn (cName s) (PrefixCon (map cvtp ps))
194 cvtp (Ptilde p) = LazyPat (cvtp p)
195 cvtp (Paspat s p) = AsPat (vName s) (cvtp p)
196 cvtp Pwild = WildPat void
198 -----------------------------------------------------------
199 -- Types and type variables
201 cvt_tvs :: [String] -> [HsTyVarBndr RdrName]
202 cvt_tvs tvs = map (UserTyVar . tName) tvs
204 cvt_context :: Cxt -> HsContext RdrName
205 cvt_context tys = map cvt_pred tys
207 cvt_pred :: Typ -> HsPred RdrName
208 cvt_pred ty = case split_ty_app ty of
209 (Tvar tc, tys) -> HsClassP (tconName tc) (map cvtType tys)
210 other -> panic "Malformed predicate"
212 cvtType :: Meta.Typ -> HsType RdrName
213 cvtType ty = trans (root ty [])
214 where root (Tapp a b) zs = root a (cvtType b : zs)
217 trans (Tcon (Tuple n),args) | length args == n
218 = HsTupleTy (HsTupCon Boxed n) args
219 trans (Tcon Arrow, [x,y]) = HsFunTy x y
220 trans (Tcon List, [x]) = HsListTy x
222 trans (Tvar nm, args) = foldl HsAppTy (HsTyVar (tName nm)) args
223 trans (Tcon tc, args) = foldl HsAppTy (HsTyVar (tc_name tc)) args
225 tc_name (TconName nm) = tconName nm
226 tc_name Arrow = tconName "->"
227 tc_name List = tconName "[]"
228 tc_name (Tuple 0) = tconName "()"
229 tc_name (Tuple n) = tconName ("(" ++ replicate (n-1) ',' ++ ")")
231 split_ty_app :: Typ -> (Typ, [Typ])
232 split_ty_app ty = go ty []
234 go (Tapp f a) as = go f (a:as)
237 -----------------------------------------------------------
239 sigP (Proto _ _) = True
243 -----------------------------------------------------------
244 -- some useful things
246 truePat = ConPatIn (cName "True") (PrefixCon [])
247 falsePat = ConPatIn (cName "False") (PrefixCon [])
249 overloadedLit :: Lit -> Bool
250 -- True for literals that Haskell treats as overloaded
251 overloadedLit (Int l) = True
252 overloadedLit l = False
255 void = placeHolderType
258 loc0 = generatedSrcLoc
260 fromInt :: Int -> Integer
261 fromInt x = toInteger x
264 vName :: String -> RdrName
265 vName = mkName varName
267 -- Constructor function names
268 cName :: String -> RdrName
269 cName = mkName dataName
271 -- Type variable names
272 tName :: String -> RdrName
273 tName = mkName tvName
275 -- Type Constructor names
276 tconName = mkName tcName
278 mkName :: NameSpace -> String -> RdrName
279 -- Parse the string to see if it has a "." or ":" in it
280 -- so we know whether to generate a qualified or original name
281 -- It's a bit tricky because we need to parse
282 -- Foo.Baz.x as Qual Foo.Baz x
283 -- So we parse it from back to front
286 = split [] (reverse str)
288 split occ [] = mkRdrUnqual (mk_occ occ)
289 split occ (c:d:rev) -- 'd' is the last char before the separator
290 | is_sep c -- E.g. Fo.x d='o'
291 && isAlphaNum d -- Fo.+: d='+' perhaps
292 = mk_qual (reverse (d:rev)) c occ
293 split occ (c:rev) = split (c:occ) rev
295 mk_qual mod '.' occ = mkRdrQual (mk_mod mod) (mk_occ occ)
296 mk_qual mod ':' occ = mkOrig (mk_mod mod) (mk_occ occ)
298 mk_occ occ = mkOccFS ns (mkFastString occ)
299 mk_mod mod = mkModuleName mod