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...)
45 RdrNameClassOpPragmas,
49 RdrNameInstancePragmas,
53 extractRuleBndrsTyVars,
55 mkOpApp, mkClassDecl, mkClassOpSig,
60 cvValSig, cvClassOpSig, cvInstDeclSig
63 #include "HsVersions.h"
66 import Name ( mkClassTyConOcc, mkClassDataConOcc )
67 import OccName ( mkClassTyConOcc, mkClassDataConOcc,
68 mkSuperDictSelOcc, mkDefaultMethodOcc
70 import RdrName ( RdrName, isRdrTyVar, mkRdrUnqual, rdrNameOcc )
71 import Util ( thenCmp )
74 import BasicTypes ( RecFlag(..) )
79 %************************************************************************
81 \subsection{Type synonyms}
83 %************************************************************************
86 type RdrNameArithSeqInfo = ArithSeqInfo RdrName RdrNamePat
87 type RdrNameBangType = BangType RdrName
88 type RdrNameClassOpSig = Sig RdrName
89 type RdrNameConDecl = ConDecl RdrName
90 type RdrNameConDetails = ConDetails RdrName
91 type RdrNameContext = Context RdrName
92 type RdrNameHsDecl = HsDecl RdrName RdrNamePat
93 type RdrNameSpecDataSig = SpecDataSig RdrName
94 type RdrNameDefaultDecl = DefaultDecl RdrName
95 type RdrNameForeignDecl = ForeignDecl RdrName
96 type RdrNameGRHS = GRHS RdrName RdrNamePat
97 type RdrNameGRHSs = GRHSs RdrName RdrNamePat
98 type RdrNameHsBinds = HsBinds RdrName RdrNamePat
99 type RdrNameHsExpr = HsExpr RdrName RdrNamePat
100 type RdrNameHsModule = HsModule RdrName RdrNamePat
101 type RdrNameIE = IE RdrName
102 type RdrNameImportDecl = ImportDecl RdrName
103 type RdrNameInstDecl = InstDecl RdrName RdrNamePat
104 type RdrNameMatch = Match RdrName RdrNamePat
105 type RdrNameMonoBinds = MonoBinds RdrName RdrNamePat
106 type RdrNamePat = InPat RdrName
107 type RdrNameHsType = HsType RdrName
108 type RdrNameHsTyVar = HsTyVar RdrName
109 type RdrNameSig = Sig RdrName
110 type RdrNameStmt = Stmt RdrName RdrNamePat
111 type RdrNameTyClDecl = TyClDecl RdrName RdrNamePat
112 type RdrNameRuleBndr = RuleBndr RdrName
113 type RdrNameRuleDecl = RuleDecl RdrName RdrNamePat
115 type RdrNameHsRecordBinds = HsRecordBinds RdrName RdrNamePat
117 type RdrNameClassOpPragmas = ClassOpPragmas RdrName
118 type RdrNameClassPragmas = ClassPragmas RdrName
119 type RdrNameDataPragmas = DataPragmas RdrName
120 type RdrNameGenPragmas = GenPragmas RdrName
121 type RdrNameInstancePragmas = InstancePragmas RdrName
125 %************************************************************************
127 \subsection{A few functions over HsSyn at RdrName}
129 %************************************************************************
131 @extractHsTyRdrNames@ finds the free variables of a HsType
132 It's used when making the for-alls explicit.
135 extractHsTyRdrNames :: HsType RdrName -> [RdrName]
136 extractHsTyRdrNames ty = nub (extract_ty ty [])
138 extractHsTyRdrTyVars :: RdrNameHsType -> [RdrName]
139 extractHsTyRdrTyVars ty = filter isRdrTyVar (extractHsTyRdrNames ty)
141 extractRuleBndrsTyVars :: [RuleBndr RdrName] -> [RdrName]
142 extractRuleBndrsTyVars bndrs = filter isRdrTyVar (nub (foldr go [] bndrs))
144 go (RuleBndr _) acc = acc
145 go (RuleBndrSig _ ty) acc = extract_ty ty acc
147 extractHsCtxtRdrNames :: Context RdrName -> [RdrName]
148 extractHsCtxtRdrNames ty = nub (extract_ctxt ty [])
150 extract_ctxt ctxt acc = foldr extract_ass acc ctxt
152 extract_ass (cls, tys) acc = foldr extract_ty (cls : acc) tys
154 extract_ty (MonoTyApp ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
155 extract_ty (MonoListTy ty) acc = extract_ty ty acc
156 extract_ty (MonoTupleTy tys _) acc = foldr extract_ty acc tys
157 extract_ty (MonoFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
158 extract_ty (MonoDictTy cls tys) acc = foldr extract_ty (cls : acc) tys
159 extract_ty (MonoUsgTy usg ty) acc = extract_ty ty acc
160 extract_ty (MonoTyVar tv) acc = tv : acc
161 extract_ty (HsForAllTy Nothing ctxt ty) acc = extract_ctxt ctxt (extract_ty ty acc)
162 extract_ty (HsForAllTy (Just tvs) ctxt ty)
164 (filter (`notElem` locals) $
165 extract_ctxt ctxt (extract_ty ty []))
167 locals = map getTyVarName tvs
170 extractPatsTyVars :: [RdrNamePat] -> [RdrName]
171 extractPatsTyVars pats = filter isRdrTyVar (nub (foldr extract_pat [] pats))
173 extract_pat (SigPatIn pat ty) acc = extract_ty ty acc
174 extract_pat WildPatIn acc = acc
175 extract_pat (VarPatIn var) acc = acc
176 extract_pat (LitPatIn _) acc = acc
177 extract_pat (LazyPatIn pat) acc = extract_pat pat acc
178 extract_pat (AsPatIn a pat) acc = extract_pat pat acc
179 extract_pat (NPlusKPatIn n _) acc = acc
180 extract_pat (ConPatIn c pats) acc = foldr extract_pat acc pats
181 extract_pat (ConOpPatIn p1 c f p2) acc = extract_pat p1 (extract_pat p2 acc)
182 extract_pat (NegPatIn pat) acc = extract_pat pat acc
183 extract_pat (ParPatIn pat) acc = extract_pat pat acc
184 extract_pat (ListPatIn pats) acc = foldr extract_pat acc pats
185 extract_pat (TuplePatIn pats _) acc = foldr extract_pat acc pats
186 extract_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> extract_pat pat acc) acc fields
189 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
190 by deriving them from the name of the class. We fill in the names for the
191 tycon and datacon corresponding to the class, by deriving them from the
192 name of the class itself. This saves recording the names in the interface
193 file (which would be equally good).
195 Similarly for mkClassOpSig and default-method names.
198 mkClassDecl cxt cname tyvars sigs mbinds prags loc
199 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname sc_sel_names loc
201 cls_occ = rdrNameOcc cname
202 dname = mkRdrUnqual (mkClassDataConOcc cls_occ)
203 tname = mkRdrUnqual (mkClassTyConOcc cls_occ)
204 sc_sel_names = [ mkRdrUnqual (mkSuperDictSelOcc n cls_occ)
205 | n <- [1..length cxt]]
206 -- We number off the superclass selectors, 1, 2, 3 etc so that we
207 -- can construct names for the selectors. Thus
208 -- class (C a, C b) => D a b where ...
209 -- gives superclass selectors
211 -- (We used to call them D_C, but now we can have two different
212 -- superclasses both called C!)
214 mkClassOpSig has_default_method op ty loc
215 | not has_default_method = ClassOpSig op Nothing ty loc
216 | otherwise = ClassOpSig op (Just dm_rn) ty loc
218 dm_rn = mkRdrUnqual (mkDefaultMethodOcc (rdrNameOcc op))
221 A useful function for building @OpApps@. The operator is always a variable,
222 and we don't know the fixity yet.
225 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
228 %************************************************************************
230 \subsection[rdrBinding]{Bindings straight out of the parser}
232 %************************************************************************
236 = -- On input we use the Empty/And form rather than a list
238 | RdrAndBindings RdrBinding RdrBinding
240 -- Value bindings havn't been united with their
242 | RdrValBinding RdrNameMonoBinds
244 -- Signatures are mysterious; we can't
245 -- tell if its a Sig or a ClassOpSig,
246 -- so we just save the pieces:
249 -- The remainder all fit into the main HsDecl form
250 | RdrHsDecl RdrNameHsDecl
252 type SigConverter = RdrNameSig -> RdrNameSig
259 (Maybe RdrNameHsType)
263 %************************************************************************
265 \subsection[cvDecls]{Convert various top-level declarations}
267 %************************************************************************
269 We make a point not to throw any user-pragma ``sigs'' at
270 these conversion functions:
273 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
277 cvInstDeclSig sig = sig
279 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
280 cvClassOpSig sig = sig
284 %************************************************************************
286 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
288 %************************************************************************
290 Function definitions are restructured here. Each is assumed to be recursive
291 initially, and non recursive definitions are discovered by the dependency
295 cvBinds :: SigConverter -> RdrBinding -> RdrNameHsBinds
296 -- The mysterious SigConverter converts Sigs to ClassOpSigs
297 -- in class declarations. Mostly it's just an identity function
299 cvBinds sig_cvtr binding
300 = case (cvMonoBindsAndSigs sig_cvtr binding) of { (mbs, sigs) ->
301 MonoBind mbs sigs Recursive
306 cvMonoBindsAndSigs :: SigConverter
308 -> (RdrNameMonoBinds, [RdrNameSig])
310 cvMonoBindsAndSigs sig_cvtr fb
311 = mangle_bind (EmptyMonoBinds, []) fb
313 mangle_bind acc RdrNullBind
316 mangle_bind acc (RdrAndBindings fb1 fb2)
317 = mangle_bind (mangle_bind acc fb1) fb2
319 mangle_bind (b_acc, s_acc) (RdrSig sig)
320 = (b_acc, sig_cvtr sig : s_acc)
322 mangle_bind (b_acc, s_acc) (RdrValBinding binding)
323 = (b_acc `AndMonoBinds` binding, s_acc)
327 %************************************************************************
329 \subsection[PrefixToHS-utils]{Utilities for conversion}
331 %************************************************************************
333 Separate declarations into all the various kinds:
336 cvTopDecls :: RdrBinding -> [RdrNameHsDecl]
339 (top_decls, mono_binds, sigs) = go ([], EmptyMonoBinds, []) bind
341 (ValD (MonoBind mono_binds sigs Recursive) : top_decls)
343 go acc RdrNullBind = acc
344 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
345 go (topds, mbs, sigs) (RdrHsDecl d) = (d : topds, mbs, sigs)
346 go (topds, mbs, sigs) (RdrSig (FixSig d)) = (FixD d : topds, mbs, sigs)
347 go (topds, mbs, sigs) (RdrSig sig) = (topds, mbs, sig:sigs)
348 go (topds, mbs, sigs) (RdrValBinding bind) = (topds, mbs `AndMonoBinds` bind, sigs)