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...)
47 extractHsTyRdrNames, extractHsTyRdrTyVars,
48 extractHsCtxtRdrTyVars, extractGenericPatTyVars,
50 mkHsOpApp, mkClassDecl, mkClassOpSigDM,
51 mkHsNegApp, mkNPlusKPat, mkHsIntegral, mkHsFractional,
57 cvValSig, cvClassOpSig, cvInstDeclSig,
61 #include "HsVersions.h"
63 import HsSyn -- Lots of it
64 import OccName ( mkDefaultMethodOcc, mkVarOcc )
65 import RdrName ( RdrName, isRdrTyVar, mkRdrUnqual, rdrNameOcc, isRdrTyVar )
67 import BasicTypes ( RecFlag(..), FixitySig )
68 import Class ( DefMeth (..) )
72 %************************************************************************
74 \subsection{Type synonyms}
76 %************************************************************************
79 type RdrNameArithSeqInfo = ArithSeqInfo RdrName
80 type RdrNameBangType = BangType RdrName
81 type RdrNameClassOpSig = Sig RdrName
82 type RdrNameConDecl = ConDecl RdrName
83 type RdrNameConDetails = HsConDetails RdrName RdrNameBangType
84 type RdrNameContext = HsContext RdrName
85 type RdrNameHsDecl = HsDecl RdrName
86 type RdrNameDefaultDecl = DefaultDecl RdrName
87 type RdrNameForeignDecl = ForeignDecl RdrName
88 type RdrNameCoreDecl = CoreDecl RdrName
89 type RdrNameGRHS = GRHS RdrName
90 type RdrNameGRHSs = GRHSs RdrName
91 type RdrNameHsBinds = HsBinds RdrName
92 type RdrNameHsExpr = HsExpr RdrName
93 type RdrNameHsModule = HsModule RdrName
94 type RdrNameIE = IE RdrName
95 type RdrNameImportDecl = ImportDecl RdrName
96 type RdrNameInstDecl = InstDecl RdrName
97 type RdrNameMatch = Match RdrName
98 type RdrNameMonoBinds = MonoBinds RdrName
99 type RdrNamePat = InPat RdrName
100 type RdrNameHsType = HsType RdrName
101 type RdrNameHsTyVar = HsTyVarBndr RdrName
102 type RdrNameSig = Sig RdrName
103 type RdrNameStmt = Stmt RdrName
104 type RdrNameTyClDecl = TyClDecl RdrName
106 type RdrNameRuleBndr = RuleBndr RdrName
107 type RdrNameRuleDecl = RuleDecl RdrName
108 type RdrNameDeprecation = DeprecDecl RdrName
109 type RdrNameFixitySig = FixitySig RdrName
111 type RdrNameHsRecordBinds = HsRecordBinds RdrName
115 %************************************************************************
117 \subsection{A few functions over HsSyn at RdrName}
119 %************************************************************************
121 @extractHsTyRdrNames@ finds the free variables of a HsType
122 It's used when making the for-alls explicit.
125 extractHsTyRdrNames :: RdrNameHsType -> [RdrName]
126 extractHsTyRdrNames ty = nub (extract_ty ty [])
128 extractHsTyRdrTyVars :: RdrNameHsType -> [RdrName]
129 extractHsTyRdrTyVars ty = nub (filter isRdrTyVar (extract_ty ty []))
131 extractHsCtxtRdrNames :: HsContext RdrName -> [RdrName]
132 extractHsCtxtRdrNames ty = nub (extract_ctxt ty [])
133 extractHsCtxtRdrTyVars :: HsContext RdrName -> [RdrName]
134 extractHsCtxtRdrTyVars ty = filter isRdrTyVar (extractHsCtxtRdrNames ty)
136 extract_ctxt ctxt acc = foldr extract_pred acc ctxt
138 extract_pred (HsClassP cls tys) acc = foldr extract_ty (cls : acc) tys
139 extract_pred (HsIParam n ty) acc = extract_ty ty acc
141 extract_tys tys = foldr extract_ty [] tys
143 extract_ty (HsAppTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
144 extract_ty (HsListTy ty) acc = extract_ty ty acc
145 extract_ty (HsPArrTy ty) acc = extract_ty ty acc
146 extract_ty (HsTupleTy _ tys) acc = foldr extract_ty acc tys
147 extract_ty (HsFunTy ty1 ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
148 extract_ty (HsPredTy p) acc = extract_pred p acc
149 extract_ty (HsTyVar tv) acc = tv : acc
150 extract_ty (HsForAllTy Nothing cx ty) acc = extract_ctxt cx (extract_ty ty acc)
151 extract_ty (HsOpTy ty1 nam ty2) acc = extract_ty ty1 (extract_ty ty2 acc)
152 extract_ty (HsParTy ty) acc = extract_ty ty acc
154 extract_ty (HsNumTy num) acc = acc
155 extract_ty (HsKindSig ty k) acc = extract_ty ty acc
156 extract_ty (HsForAllTy (Just tvs) ctxt ty)
158 (filter (`notElem` locals) $
159 extract_ctxt ctxt (extract_ty ty []))
161 locals = hsTyVarNames tvs
163 extractGenericPatTyVars :: RdrNameMonoBinds -> [RdrName]
164 -- Get the type variables out of the type patterns in a bunch of
165 -- possibly-generic bindings in a class declaration
166 extractGenericPatTyVars binds
167 = filter isRdrTyVar (nub (get binds []))
169 get (AndMonoBinds b1 b2) acc = get b1 (get b2 acc)
170 get (FunMonoBind _ _ ms _) acc = foldr get_m acc ms
173 get_m (Match (TypePat ty : _) _ _) acc = extract_ty ty acc
174 get_m other acc = acc
178 %************************************************************************
180 \subsection{Construction functions for Rdr stuff}
182 %************************************************************************
184 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
185 by deriving them from the name of the class. We fill in the names for the
186 tycon and datacon corresponding to the class, by deriving them from the
187 name of the class itself. This saves recording the names in the interface
188 file (which would be equally good).
190 Similarly for mkConDecl, mkClassOpSig and default-method names.
192 *** See "THE NAMING STORY" in HsDecls ****
195 mkClassDecl (cxt, cname, tyvars) fds sigs mbinds loc
196 = ClassDecl { tcdCtxt = cxt, tcdName = cname, tcdTyVars = tyvars,
197 tcdFDs = fds, tcdSigs = sigs, tcdMeths = mbinds,
200 mkTyData new_or_data (context, tname, tyvars) data_cons maybe src
201 = TyData { tcdND = new_or_data, tcdCtxt = context, tcdName = tname,
202 tcdTyVars = tyvars, tcdCons = data_cons,
203 tcdDerivs = maybe, tcdLoc = src, tcdGeneric = Nothing }
205 mkClassOpSigDM op ty loc
206 = ClassOpSig op (DefMeth dm_rn) ty loc
208 dm_rn = mkRdrUnqual (mkDefaultMethodOcc (rdrNameOcc op))
212 mkHsNegApp :: RdrNameHsExpr -> RdrNameHsExpr
213 -- If the type checker sees (negate 3#) it will barf, because negate
214 -- can't take an unboxed arg. But that is exactly what it will see when
215 -- we write "-3#". So we have to do the negation right now!
217 mkHsNegApp (HsLit (HsIntPrim i)) = HsLit (HsIntPrim (-i))
218 mkHsNegApp (HsLit (HsFloatPrim i)) = HsLit (HsFloatPrim (-i))
219 mkHsNegApp (HsLit (HsDoublePrim i)) = HsLit (HsDoublePrim (-i))
220 mkHsNegApp expr = NegApp expr placeHolderName
223 A useful function for building @OpApps@. The operator is always a
224 variable, and we don't know the fixity yet.
227 mkHsOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
230 These are the bits of syntax that contain rebindable names
231 See RnEnv.lookupSyntaxName
234 mkHsIntegral i = HsIntegral i placeHolderName
235 mkHsFractional f = HsFractional f placeHolderName
236 mkNPlusKPat n k = NPlusKPatIn n k placeHolderName
237 mkHsDo ctxt stmts loc = HsDo ctxt stmts [] placeHolderType loc
241 mkHsSplice e = HsSplice unqualSplice e
243 unqualSplice = mkRdrUnqual (mkVarOcc FSLIT("splice"))
244 -- A name (uniquified later) to
245 -- identify the splice
248 %************************************************************************
250 \subsection[rdrBinding]{Bindings straight out of the parser}
252 %************************************************************************
256 = -- On input we use the Empty/And form rather than a list
258 | RdrAndBindings RdrBinding RdrBinding
260 -- Value bindings havn't been united with their
262 | RdrValBinding RdrNameMonoBinds
264 -- Signatures are mysterious; we can't
265 -- tell if its a Sig or a ClassOpSig,
266 -- so we just save the pieces:
269 -- The remainder all fit into the main HsDecl form
270 | RdrHsDecl RdrNameHsDecl
272 type SigConverter = RdrNameSig -> RdrNameSig
279 (Maybe RdrNameHsType)
283 %************************************************************************
285 \subsection[cvDecls]{Convert various top-level declarations}
287 %************************************************************************
289 We make a point not to throw any user-pragma ``sigs'' at
290 these conversion functions:
293 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
297 cvInstDeclSig sig = sig
299 cvClassOpSig (Sig var poly_ty src_loc) = mkClassOpSigDM var poly_ty src_loc
300 cvClassOpSig sig = sig
304 %************************************************************************
306 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
308 %************************************************************************
310 Function definitions are restructured here. Each is assumed to be recursive
311 initially, and non recursive definitions are discovered by the dependency
315 cvBinds :: SigConverter -> RdrBinding -> RdrNameHsBinds
316 -- The mysterious SigConverter converts Sigs to ClassOpSigs
317 -- in class declarations. Mostly it's just an identity function
319 cvBinds sig_cvtr binding
320 = case (cvMonoBindsAndSigs sig_cvtr binding) of { (mbs, sigs) ->
321 MonoBind mbs sigs Recursive
326 cvMonoBindsAndSigs :: SigConverter
328 -> (RdrNameMonoBinds, [RdrNameSig])
330 cvMonoBindsAndSigs sig_cvtr fb
331 = mangle_bind (EmptyMonoBinds, []) fb
333 mangle_bind acc RdrNullBind
336 mangle_bind acc (RdrAndBindings fb1 fb2)
337 = mangle_bind (mangle_bind acc fb1) fb2
339 mangle_bind (b_acc, s_acc) (RdrSig sig)
340 = (b_acc, sig_cvtr sig : s_acc)
342 mangle_bind (b_acc, s_acc) (RdrValBinding binding)
343 = (b_acc `AndMonoBinds` binding, s_acc)
347 %************************************************************************
349 \subsection[PrefixToHS-utils]{Utilities for conversion}
351 %************************************************************************
353 Separate declarations into all the various kinds:
356 cvTopDecls :: RdrBinding -> [RdrNameHsDecl]
359 (top_decls, mono_binds, sigs) = go ([], EmptyMonoBinds, []) bind
361 (ValD (MonoBind mono_binds sigs Recursive) : top_decls)
363 go acc RdrNullBind = acc
364 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
365 go (topds, mbs, sigs) (RdrHsDecl d) = (d : topds, mbs, sigs)
366 go (topds, mbs, sigs) (RdrSig (FixSig d)) = (FixD d : topds, mbs, sigs)
367 go (topds, mbs, sigs) (RdrSig sig) = (topds, mbs, sig:sigs)
368 go (topds, mbs, sigs) (RdrValBinding bind) = (topds, mbs `AndMonoBinds` bind, sigs)