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...)
44 RdrNameClassOpPragmas,
48 RdrNameInstancePragmas,
50 extractPatsTyVars, extractRuleBndrsTyVars,
52 mkOpApp, mkClassDecl, mkClassOpSig,
57 cvValSig, cvClassOpSig, cvInstDeclSig
60 #include "HsVersions.h"
63 import Name ( mkClassTyConOcc, mkClassDataConOcc )
64 import OccName ( mkClassTyConOcc, mkClassDataConOcc,
65 mkSuperDictSelOcc, mkDefaultMethodOcc
67 import RdrName ( RdrName, isRdrTyVar, mkRdrUnqual, rdrNameOcc )
68 import Util ( thenCmp )
71 import BasicTypes ( RecFlag(..) )
76 %************************************************************************
78 \subsection{Type synonyms}
80 %************************************************************************
83 type RdrNameArithSeqInfo = ArithSeqInfo RdrName RdrNamePat
84 type RdrNameBangType = BangType RdrName
85 type RdrNameClassOpSig = Sig RdrName
86 type RdrNameConDecl = ConDecl RdrName
87 type RdrNameContext = Context RdrName
88 type RdrNameHsDecl = HsDecl RdrName RdrNamePat
89 type RdrNameSpecDataSig = SpecDataSig RdrName
90 type RdrNameDefaultDecl = DefaultDecl RdrName
91 type RdrNameForeignDecl = ForeignDecl RdrName
92 type RdrNameGRHS = GRHS RdrName RdrNamePat
93 type RdrNameGRHSs = GRHSs RdrName RdrNamePat
94 type RdrNameHsBinds = HsBinds RdrName RdrNamePat
95 type RdrNameHsExpr = HsExpr RdrName RdrNamePat
96 type RdrNameHsModule = HsModule RdrName RdrNamePat
97 type RdrNameIE = IE RdrName
98 type RdrNameImportDecl = ImportDecl RdrName
99 type RdrNameInstDecl = InstDecl RdrName RdrNamePat
100 type RdrNameMatch = Match RdrName RdrNamePat
101 type RdrNameMonoBinds = MonoBinds RdrName RdrNamePat
102 type RdrNamePat = InPat RdrName
103 type RdrNameHsType = HsType RdrName
104 type RdrNameHsTyVar = HsTyVar RdrName
105 type RdrNameSig = Sig RdrName
106 type RdrNameStmt = Stmt RdrName RdrNamePat
107 type RdrNameTyClDecl = TyClDecl RdrName RdrNamePat
108 type RdrNameRuleBndr = RuleBndr RdrName
109 type RdrNameRuleDecl = RuleDecl RdrName RdrNamePat
111 type RdrNameHsRecordBinds = HsRecordBinds RdrName RdrNamePat
113 type RdrNameClassOpPragmas = ClassOpPragmas RdrName
114 type RdrNameClassPragmas = ClassPragmas RdrName
115 type RdrNameDataPragmas = DataPragmas RdrName
116 type RdrNameGenPragmas = GenPragmas RdrName
117 type RdrNameInstancePragmas = InstancePragmas RdrName
121 %************************************************************************
123 \subsection{A few functions over HsSyn at RdrName}
125 %************************************************************************
127 @extractHsTyRdrNames@ finds the free variables of a HsType
128 It's used when making the for-alls explicit.
131 extractHsTyRdrNames :: HsType RdrName -> [RdrName]
132 extractHsTyRdrNames ty = nub (extract_ty ty [])
134 extractRuleBndrsTyVars :: [RuleBndr RdrName] -> [RdrName]
135 extractRuleBndrsTyVars bndrs = filter isRdrTyVar (nub (foldr go [] bndrs))
137 go (RuleBndr _) acc = acc
138 go (RuleBndrSig _ ty) acc = extract_ty ty acc
140 extractHsCtxtRdrNames :: Context RdrName -> [RdrName]
141 extractHsCtxtRdrNames ty = nub (extract_ctxt ty [])
143 extract_ctxt ctxt acc = foldr extract_ass acc ctxt
145 extract_ass (cls, tys) acc = foldr extract_ty (cls : acc) tys
147 extract_ty (MonoTyApp ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
148 extract_ty (MonoListTy ty) acc = extract_ty ty acc
149 extract_ty (MonoTupleTy tys _) acc = foldr extract_ty acc tys
150 extract_ty (MonoFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
151 extract_ty (MonoDictTy cls tys) acc = foldr extract_ty (cls : acc) tys
152 extract_ty (MonoUsgTy usg ty) acc = extract_ty ty acc
153 extract_ty (MonoTyVar tv) acc = tv : acc
154 extract_ty (HsForAllTy (Just tvs) ctxt ty)
156 (filter (`notElem` locals) $
157 extract_ctxt ctxt (extract_ty ty []))
159 locals = map getTyVarName tvs
162 extractPatsTyVars :: [RdrNamePat] -> [RdrName]
163 extractPatsTyVars pats = nub (foldr extract_pat [] pats)
165 extract_pat (SigPatIn pat ty) acc = extract_ty ty acc
166 extract_pat WildPatIn acc = acc
167 extract_pat (VarPatIn var) acc = acc
168 extract_pat (LitPatIn _) acc = acc
169 extract_pat (LazyPatIn pat) acc = extract_pat pat acc
170 extract_pat (AsPatIn a pat) acc = extract_pat pat acc
171 extract_pat (NPlusKPatIn n _) acc = acc
172 extract_pat (ConPatIn c pats) acc = foldr extract_pat acc pats
173 extract_pat (ConOpPatIn p1 c f p2) acc = extract_pat p1 (extract_pat p2 acc)
174 extract_pat (NegPatIn pat) acc = extract_pat pat acc
175 extract_pat (ParPatIn pat) acc = extract_pat pat acc
176 extract_pat (ListPatIn pats) acc = foldr extract_pat acc pats
177 extract_pat (TuplePatIn pats _) acc = foldr extract_pat acc pats
178 extract_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> extract_pat pat acc) acc fields
181 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
182 by deriving them from the name of the class. We fill in the names for the
183 tycon and datacon corresponding to the class, by deriving them from the
184 name of the class itself. This saves recording the names in the interface
185 file (which would be equally good).
187 Similarly for mkClassOpSig and default-method names.
190 mkClassDecl cxt cname tyvars sigs mbinds prags loc
191 = ClassDecl cxt cname tyvars sigs mbinds prags tname dname sc_sel_names loc
193 cls_occ = rdrNameOcc cname
194 dname = mkRdrUnqual (mkClassDataConOcc cls_occ)
195 tname = mkRdrUnqual (mkClassTyConOcc cls_occ)
196 sc_sel_names = [ mkRdrUnqual (mkSuperDictSelOcc n cls_occ)
197 | n <- [1..length cxt]]
198 -- We number off the superclass selectors, 1, 2, 3 etc so that we
199 -- can construct names for the selectors. Thus
200 -- class (C a, C b) => D a b where ...
201 -- gives superclass selectors
203 -- (We used to call them D_C, but now we can have two different
204 -- superclasses both called C!)
206 mkClassOpSig has_default_method op ty loc
207 | not has_default_method = ClassOpSig op Nothing ty loc
208 | otherwise = ClassOpSig op (Just dm_rn) ty loc
210 dm_rn = mkRdrUnqual (mkDefaultMethodOcc (rdrNameOcc op))
213 A useful function for building @OpApps@. The operator is always a variable,
214 and we don't know the fixity yet.
217 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
220 %************************************************************************
222 \subsection[rdrBinding]{Bindings straight out of the parser}
224 %************************************************************************
228 = -- On input we use the Empty/And form rather than a list
230 | RdrAndBindings RdrBinding RdrBinding
232 -- Value bindings havn't been united with their
234 | RdrValBinding RdrNameMonoBinds
236 -- Signatures are mysterious; we can't
237 -- tell if its a Sig or a ClassOpSig,
238 -- so we just save the pieces:
241 -- The remainder all fit into the main HsDecl form
242 | RdrHsDecl RdrNameHsDecl
244 type SigConverter = RdrNameSig -> RdrNameSig
251 (Maybe RdrNameHsType)
255 %************************************************************************
257 \subsection[cvDecls]{Convert various top-level declarations}
259 %************************************************************************
261 We make a point not to throw any user-pragma ``sigs'' at
262 these conversion functions:
265 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
269 cvInstDeclSig sig = sig
271 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
272 cvClassOpSig sig = sig
276 %************************************************************************
278 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
280 %************************************************************************
282 Function definitions are restructured here. Each is assumed to be recursive
283 initially, and non recursive definitions are discovered by the dependency
287 cvBinds :: SigConverter -> RdrBinding -> RdrNameHsBinds
288 -- The mysterious SigConverter converts Sigs to ClassOpSigs
289 -- in class declarations. Mostly it's just an identity function
291 cvBinds sig_cvtr binding
292 = case (cvMonoBindsAndSigs sig_cvtr binding) of { (mbs, sigs) ->
293 MonoBind mbs sigs Recursive
298 cvMonoBindsAndSigs :: SigConverter
300 -> (RdrNameMonoBinds, [RdrNameSig])
302 cvMonoBindsAndSigs sig_cvtr fb
303 = mangle_bind (EmptyMonoBinds, []) fb
305 mangle_bind acc RdrNullBind
308 mangle_bind acc (RdrAndBindings fb1 fb2)
309 = mangle_bind (mangle_bind acc fb1) fb2
311 mangle_bind (b_acc, s_acc) (RdrSig sig)
312 = (b_acc, sig_cvtr sig : s_acc)
314 mangle_bind (b_acc, s_acc) (RdrValBinding binding)
315 = (b_acc `AndMonoBinds` binding, s_acc)
319 %************************************************************************
321 \subsection[PrefixToHS-utils]{Utilities for conversion}
323 %************************************************************************
325 Separate declarations into all the various kinds:
328 cvTopDecls :: RdrBinding -> [RdrNameHsDecl]
331 (top_decls, mono_binds, sigs) = go ([], EmptyMonoBinds, []) bind
333 (ValD (MonoBind mono_binds sigs Recursive) : top_decls)
335 go acc RdrNullBind = acc
336 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
337 go (topds, mbs, sigs) (RdrHsDecl d) = (d : topds, mbs, sigs)
338 go (topds, mbs, sigs) (RdrSig (FixSig d)) = (FixD d : topds, mbs, sigs)
339 go (topds, mbs, sigs) (RdrSig sig) = (topds, mbs, sig:sigs)
340 go (topds, mbs, sigs) (RdrValBinding bind) = (topds, mbs `AndMonoBinds` bind, sigs)