2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RdrHsSyn]{Specialisations of the @HsSyn@ syntax for the reader}
6 (Well, really, for specialisations involving @RdrName@s, even if
7 they are used somewhat later on in the compiler...)
36 RdrNameClassOpPragmas,
40 RdrNameInstancePragmas,
41 extractHsTyVars, extractHsCtxtTyVars, extractPatsTyVars,
46 #include "HsVersions.h"
49 import Name ( mkClassTyConOcc, mkClassDataConOcc )
50 import RdrName ( RdrName, isRdrTyVar, mkRdrUnqual, rdrNameOcc )
51 import Util ( thenCmp )
52 import HsPragmas ( GenPragmas, ClassPragmas, DataPragmas, ClassOpPragmas, InstancePragmas )
58 %************************************************************************
60 \subsection{Type synonyms}
62 %************************************************************************
65 type RdrNameArithSeqInfo = ArithSeqInfo RdrName RdrNamePat
66 type RdrNameBangType = BangType RdrName
67 type RdrNameClassOpSig = Sig RdrName
68 type RdrNameConDecl = ConDecl RdrName
69 type RdrNameContext = Context RdrName
70 type RdrNameHsDecl = HsDecl RdrName RdrNamePat
71 type RdrNameSpecDataSig = SpecDataSig RdrName
72 type RdrNameDefaultDecl = DefaultDecl RdrName
73 type RdrNameForeignDecl = ForeignDecl RdrName
74 type RdrNameGRHS = GRHS RdrName RdrNamePat
75 type RdrNameGRHSs = GRHSs RdrName RdrNamePat
76 type RdrNameHsBinds = HsBinds RdrName RdrNamePat
77 type RdrNameHsExpr = HsExpr RdrName RdrNamePat
78 type RdrNameHsModule = HsModule RdrName RdrNamePat
79 type RdrNameIE = IE RdrName
80 type RdrNameImportDecl = ImportDecl RdrName
81 type RdrNameInstDecl = InstDecl RdrName RdrNamePat
82 type RdrNameMatch = Match RdrName RdrNamePat
83 type RdrNameMonoBinds = MonoBinds RdrName RdrNamePat
84 type RdrNamePat = InPat RdrName
85 type RdrNameHsType = HsType RdrName
86 type RdrNameSig = Sig RdrName
87 type RdrNameStmt = Stmt RdrName RdrNamePat
88 type RdrNameTyClDecl = TyClDecl RdrName RdrNamePat
90 type RdrNameClassOpPragmas = ClassOpPragmas RdrName
91 type RdrNameClassPragmas = ClassPragmas RdrName
92 type RdrNameDataPragmas = DataPragmas RdrName
93 type RdrNameGenPragmas = GenPragmas RdrName
94 type RdrNameInstancePragmas = InstancePragmas RdrName
98 %************************************************************************
100 \subsection{A few functions over HsSyn at RdrName}
102 %************************************************************************
104 @extractHsTyVars@ looks just for things that could be type variables.
105 It's used when making the for-alls explicit.
108 extractHsTyVars :: HsType RdrName -> [RdrName]
109 extractHsTyVars ty = nub (extract_ty ty [])
111 extractHsCtxtTyVars :: Context RdrName -> [RdrName]
112 extractHsCtxtTyVars ty = nub (extract_ctxt ty [])
114 extract_ctxt ctxt acc = foldr extract_ass acc ctxt
116 extract_ass (cls, tys) acc = foldr extract_ty acc tys
118 extract_ty (MonoTyApp ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
119 extract_ty (MonoListTy ty) acc = extract_ty ty acc
120 extract_ty (MonoTupleTy tys _) acc = foldr extract_ty acc tys
121 extract_ty (MonoFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
122 extract_ty (MonoDictTy cls tys) acc = foldr extract_ty acc tys
123 extract_ty (MonoTyVar tv) acc = insertTV tv acc
124 extract_ty (HsForAllTy tvs ctxt ty) acc = acc ++
125 (filter (`notElem` locals) $
126 extract_ctxt ctxt (extract_ty ty []))
128 locals = map getTyVarName tvs
130 insertTV name acc | isRdrTyVar name = name : acc
131 insertTV other acc = acc
133 extractPatsTyVars :: [RdrNamePat] -> [RdrName]
134 extractPatsTyVars pats = nub (foldr extract_pat [] pats)
136 extract_pat (SigPatIn pat ty) acc = extract_ty ty acc
137 extract_pat WildPatIn acc = acc
138 extract_pat (VarPatIn var) acc = acc
139 extract_pat (LitPatIn _) acc = acc
140 extract_pat (LazyPatIn pat) acc = extract_pat pat acc
141 extract_pat (AsPatIn a pat) acc = extract_pat pat acc
142 extract_pat (NPlusKPatIn n _) acc = acc
143 extract_pat (ConPatIn c pats) acc = foldr extract_pat acc pats
144 extract_pat (ConOpPatIn p1 c f p2) acc = extract_pat p1 (extract_pat p2 acc)
145 extract_pat (NegPatIn pat) acc = extract_pat pat acc
146 extract_pat (ParPatIn pat) acc = extract_pat pat acc
147 extract_pat (ListPatIn pats) acc = foldr extract_pat acc pats
148 extract_pat (TuplePatIn pats _) acc = foldr extract_pat acc pats
149 extract_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> extract_pat pat acc) acc fields
153 A useful function for building @OpApps@. The operator is always a variable,
154 and we don't know the fixity yet.
157 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
160 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
161 by deriving them from the name of the class. We fill in the names for the
162 tycon and datacon corresponding to the class, by deriving them from the
163 name of the class itself. This saves recording the names in the interface
164 file (which would be equally godd).
167 mkClassDecl cxt cname tyvars sigs mbinds prags loc
168 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname loc
170 cls_occ = rdrNameOcc cname
171 dname = mkRdrUnqual (mkClassDataConOcc cls_occ)
172 tname = mkRdrUnqual (mkClassTyConOcc cls_occ)