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 (Just tvs) ctxt ty)
126 (filter (`notElem` locals) $
127 extract_ctxt ctxt (extract_ty ty []))
129 locals = map getTyVarName tvs
131 insertTV name acc | isRdrTyVar name = name : acc
132 insertTV other acc = acc
134 extractPatsTyVars :: [RdrNamePat] -> [RdrName]
135 extractPatsTyVars pats = nub (foldr extract_pat [] pats)
137 extract_pat (SigPatIn pat ty) acc = extract_ty ty acc
138 extract_pat WildPatIn acc = acc
139 extract_pat (VarPatIn var) acc = acc
140 extract_pat (LitPatIn _) acc = acc
141 extract_pat (LazyPatIn pat) acc = extract_pat pat acc
142 extract_pat (AsPatIn a pat) acc = extract_pat pat acc
143 extract_pat (NPlusKPatIn n _) acc = acc
144 extract_pat (ConPatIn c pats) acc = foldr extract_pat acc pats
145 extract_pat (ConOpPatIn p1 c f p2) acc = extract_pat p1 (extract_pat p2 acc)
146 extract_pat (NegPatIn pat) acc = extract_pat pat acc
147 extract_pat (ParPatIn pat) acc = extract_pat pat acc
148 extract_pat (ListPatIn pats) acc = foldr extract_pat acc pats
149 extract_pat (TuplePatIn pats _) acc = foldr extract_pat acc pats
150 extract_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> extract_pat pat acc) acc fields
154 A useful function for building @OpApps@. The operator is always a variable,
155 and we don't know the fixity yet.
158 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
161 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
162 by deriving them from the name of the class. We fill in the names for the
163 tycon and datacon corresponding to the class, by deriving them from the
164 name of the class itself. This saves recording the names in the interface
165 file (which would be equally godd).
168 mkClassDecl cxt cname tyvars sigs mbinds prags loc
169 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname loc
171 cls_occ = rdrNameOcc cname
172 dname = mkRdrUnqual (mkClassDataConOcc cls_occ)
173 tname = mkRdrUnqual (mkClassTyConOcc cls_occ)