2 % (c) The University of Glasgow, 1992-2003
5 Here we collect a variety of helper functions that construct or
6 analyse HsSyn. All these functions deal with generic HsSyn; functions
7 which deal with the intantiated versions are located elsewhere:
9 Parameterised by Module
10 ---------------- -------------
11 RdrName parser/RdrHsSyn
18 #include "HsVersions.h"
26 import RdrName ( RdrName, getRdrName, mkRdrUnqual )
29 import DataCon ( DataCon, dataConWrapId, dataConSourceArity )
30 import OccName ( mkVarOccFS )
32 import BasicTypes ( RecFlag(..) )
34 import FastString ( mkFastString )
36 import Util ( nOfThem )
41 %************************************************************************
43 Some useful helpers for constructing syntax
45 %************************************************************************
47 These functions attempt to construct a not-completely-useless SrcSpan
48 from their components, compared with the nl* functions below which
49 just attach noSrcSpan to everything.
52 mkHsPar :: LHsExpr id -> LHsExpr id
53 mkHsPar e = L (getLoc e) (HsPar e)
56 mkSimpleMatch :: [LPat id] -> LHsExpr id -> LMatch id
57 mkSimpleMatch pats rhs
59 Match pats Nothing (GRHSs (unguardedRHS rhs) emptyLocalBinds)
63 (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
65 unguardedRHS :: LHsExpr id -> [LGRHS id]
66 unguardedRHS rhs@(L loc _) = [L loc (GRHS [] rhs)]
68 mkHsAppTy :: LHsType name -> LHsType name -> LHsType name
69 mkHsAppTy t1 t2 = addCLoc t1 t2 (HsAppTy t1 t2)
71 mkHsApp :: LHsExpr name -> LHsExpr name -> LHsExpr name
72 mkHsApp e1 e2 = addCLoc e1 e2 (HsApp e1 e2)
74 mkHsTyApp :: LHsExpr name -> [Type] -> LHsExpr name
75 mkHsTyApp expr [] = expr
76 mkHsTyApp expr tys = L (getLoc expr) (TyApp expr tys)
78 mkHsDictApp :: LHsExpr name -> [name] -> LHsExpr name
79 mkHsDictApp expr [] = expr
80 mkHsDictApp expr dict_vars = L (getLoc expr) (DictApp expr dict_vars)
82 mkHsLam :: [LPat id] -> LHsExpr id -> LHsExpr id
83 mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches))
85 matches = mkMatchGroup [mkSimpleMatch pats body]
87 mkMatchGroup :: [LMatch id] -> MatchGroup id
88 mkMatchGroup matches = MatchGroup matches placeHolderType
90 mkHsTyLam [] expr = expr
91 mkHsTyLam tyvars expr = L (getLoc expr) (TyLam tyvars expr)
93 mkHsDictLam [] expr = expr
94 mkHsDictLam dicts expr = L (getLoc expr) (DictLam dicts expr)
96 mkHsDictLet :: LHsBinds Id -> LHsExpr Id -> LHsExpr Id
97 -- Used for the dictionary bindings gotten from TcSimplify
98 -- We make them recursive to be on the safe side
99 mkHsDictLet binds expr
100 | isEmptyLHsBinds binds = expr
101 | otherwise = L (getLoc expr) (HsLet (HsValBinds val_binds) expr)
103 val_binds = ValBindsOut [(Recursive, binds)] []
105 mkHsConApp :: DataCon -> [Type] -> [HsExpr Id] -> LHsExpr Id
106 -- Used for constructing dictinoary terms etc, so no locations
107 mkHsConApp data_con tys args
108 = foldl mk_app (noLoc (HsVar (dataConWrapId data_con)) `mkHsTyApp` tys) args
110 mk_app f a = noLoc (HsApp f (noLoc a))
112 mkSimpleHsAlt :: LPat id -> LHsExpr id -> LMatch id
113 -- A simple lambda with a single pattern, no binds, no guards; pre-typechecking
114 mkSimpleHsAlt pat expr
115 = mkSimpleMatch [pat] expr
117 -------------------------------
118 -- These are the bits of syntax that contain rebindable names
119 -- See RnEnv.lookupSyntaxName
121 mkHsIntegral i = HsIntegral i noSyntaxExpr
122 mkHsFractional f = HsFractional f noSyntaxExpr
123 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
125 mkNPat lit neg = NPat lit neg noSyntaxExpr placeHolderType
126 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
128 mkExprStmt expr = ExprStmt expr noSyntaxExpr placeHolderType
129 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
130 mkRecStmt stmts = RecStmt stmts [] [] [] emptyLHsBinds
132 -------------------------------
133 --- A useful function for building @OpApps@. The operator is always a
134 -- variable, and we don't know the fixity yet.
135 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
137 mkHsSplice e = HsSplice unqualSplice e
139 unqualSplice = mkRdrUnqual (mkVarOccFS FSLIT("splice"))
140 -- A name (uniquified later) to
141 -- identify the splice
143 mkHsString s = HsString (mkFastString s)
146 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
147 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
151 %************************************************************************
153 Constructing syntax with no location info
155 %************************************************************************
158 nlHsVar :: id -> LHsExpr id
159 nlHsVar n = noLoc (HsVar n)
161 nlHsLit :: HsLit -> LHsExpr id
162 nlHsLit n = noLoc (HsLit n)
164 nlVarPat :: id -> LPat id
165 nlVarPat n = noLoc (VarPat n)
167 nlLitPat :: HsLit -> LPat id
168 nlLitPat l = noLoc (LitPat l)
170 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
171 nlHsApp f x = noLoc (HsApp f x)
173 nlHsIntLit n = noLoc (HsLit (HsInt n))
175 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
176 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
178 nlHsVarApps :: id -> [id] -> LHsExpr id
179 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
181 mk f a = HsApp (noLoc f) (noLoc a)
183 nlConVarPat :: id -> [id] -> LPat id
184 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
186 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
187 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
189 nlConPat :: id -> [LPat id] -> LPat id
190 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
192 nlNullaryConPat :: id -> LPat id
193 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
195 nlWildConPat :: DataCon -> LPat RdrName
196 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
197 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
199 nlTuplePat pats box = noLoc (TuplePat pats box)
200 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
202 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
203 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
205 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
207 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
208 nlHsPar e = noLoc (HsPar e)
209 nlHsIf cond true false = noLoc (HsIf cond true false)
210 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
211 nlTuple exprs box = noLoc (ExplicitTuple exprs box)
212 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
214 nlHsAppTy f t = noLoc (HsAppTy f t)
215 nlHsTyVar x = noLoc (HsTyVar x)
216 nlHsFunTy a b = noLoc (HsFunTy a b)
221 %************************************************************************
223 Bindings; with a location at the top
225 %************************************************************************
228 mkFunBind :: Located id -> [LMatch id] -> HsBind id
229 -- Not infix, with place holders for coercion and free vars
230 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
231 fun_co_fn = idCoercion, bind_fvs = placeHolderNames }
234 mkVarBind :: SrcSpan -> RdrName -> LHsExpr RdrName -> LHsBind RdrName
235 mkVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
238 mk_easy_FunBind :: SrcSpan -> RdrName -> [LPat RdrName]
239 -> LHsExpr RdrName -> LHsBind RdrName
241 mk_easy_FunBind loc fun pats expr
242 = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
245 mk_FunBind :: SrcSpan -> RdrName
246 -> [([LPat RdrName], LHsExpr RdrName)]
249 mk_FunBind loc fun [] = panic "TcGenDeriv:mk_FunBind"
250 mk_FunBind loc fun pats_and_exprs
251 = L loc $ mkFunBind (L loc fun) matches
253 matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
256 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
257 mkMatch pats expr binds
258 = noLoc (Match (map paren pats) Nothing
259 (GRHSs (unguardedRHS expr) binds))
263 L l _ -> L l (ParPat p)
267 %************************************************************************
269 Collecting binders from HsBindGroups and HsBinds
271 %************************************************************************
273 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
281 it should return [x, y, f, a, b] (remember, order important).
284 collectLocalBinders :: HsLocalBinds name -> [Located name]
285 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
286 collectLocalBinders (HsIPBinds _) = []
287 collectLocalBinders EmptyLocalBinds = []
289 collectHsValBinders :: HsValBinds name -> [Located name]
290 collectHsValBinders (ValBindsIn binds sigs) = collectHsBindLocatedBinders binds
291 collectHsValBinders (ValBindsOut binds sigs) = foldr collect_one [] binds
293 collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
295 collectAcc :: HsBind name -> [Located name] -> [Located name]
296 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
297 collectAcc (FunBind { fun_id = f }) acc = f : acc
298 collectAcc (VarBind { var_id = f }) acc = noLoc f : acc
299 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = binds }) acc
300 = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
301 -- ++ foldr collectAcc acc binds
302 -- I don't think we want the binders from the nested binds
303 -- The only time we collect binders from a typechecked
304 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
306 collectHsBindBinders :: LHsBinds name -> [name]
307 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
309 collectHsBindLocatedBinders :: LHsBinds name -> [Located name]
310 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
314 %************************************************************************
316 Getting binders from statements
318 %************************************************************************
321 collectLStmtsBinders :: [LStmt id] -> [Located id]
322 collectLStmtsBinders = concatMap collectLStmtBinders
324 collectStmtsBinders :: [Stmt id] -> [Located id]
325 collectStmtsBinders = concatMap collectStmtBinders
327 collectLStmtBinders :: LStmt id -> [Located id]
328 collectLStmtBinders = collectStmtBinders . unLoc
330 collectStmtBinders :: Stmt id -> [Located id]
331 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
332 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
333 collectStmtBinders (LetStmt binds) = collectLocalBinders binds
334 collectStmtBinders (ExprStmt _ _ _) = []
335 collectStmtBinders (RecStmt ss _ _ _ _) = collectLStmtsBinders ss
336 collectStmtBinders other = panic "collectStmtBinders"
340 %************************************************************************
342 %* Gathering stuff out of patterns
344 %************************************************************************
346 This function @collectPatBinders@ works with the ``collectBinders''
347 functions for @HsBinds@, etc. The order in which the binders are
348 collected is important; see @HsBinds.lhs@.
350 It collects the bounds *value* variables in renamed patterns; type variables
354 collectPatBinders :: LPat a -> [a]
355 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
357 collectLocatedPatBinders :: LPat a -> [Located a]
358 collectLocatedPatBinders pat = collectl pat []
360 collectPatsBinders :: [LPat a] -> [a]
361 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
363 collectLocatedPatsBinders :: [LPat a] -> [Located a]
364 collectLocatedPatsBinders pats = foldr collectl [] pats
366 ---------------------
367 collectl (L l pat) bndrs
370 go (VarPat var) = L l var : bndrs
371 go (VarPatOut var bs) = L l var : collectHsBindLocatedBinders bs
373 go (WildPat _) = bndrs
374 go (LazyPat pat) = collectl pat bndrs
375 go (AsPat a pat) = a : collectl pat bndrs
376 go (ParPat pat) = collectl pat bndrs
378 go (ListPat pats _) = foldr collectl bndrs pats
379 go (PArrPat pats _) = foldr collectl bndrs pats
380 go (TuplePat pats _) = foldr collectl bndrs pats
382 go (ConPatIn c ps) = foldr collectl bndrs (hsConArgs ps)
383 go (ConPatOut c _ ds bs ps _) = map noLoc ds
384 ++ collectHsBindLocatedBinders bs
385 ++ foldr collectl bndrs (hsConArgs ps)
386 go (LitPat _) = bndrs
387 go (NPat _ _ _ _) = bndrs
388 go (NPlusKPat n _ _ _) = n : bndrs
390 go (SigPatIn pat _) = collectl pat bndrs
391 go (SigPatOut pat _) = collectl pat bndrs
392 go (TypePat ty) = bndrs
393 go (DictPat ids1 ids2) = map noLoc ids1 ++ map noLoc ids2
398 collectSigTysFromPats :: [InPat name] -> [LHsType name]
399 collectSigTysFromPats pats = foldr collect_lpat [] pats
401 collectSigTysFromPat :: InPat name -> [LHsType name]
402 collectSigTysFromPat pat = collect_lpat pat []
404 collect_lpat pat acc = collect_pat (unLoc pat) acc
406 collect_pat (SigPatIn pat ty) acc = collect_lpat pat (ty:acc)
407 collect_pat (TypePat ty) acc = ty:acc
409 collect_pat (LazyPat pat) acc = collect_lpat pat acc
410 collect_pat (AsPat a pat) acc = collect_lpat pat acc
411 collect_pat (ParPat pat) acc = collect_lpat pat acc
412 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
413 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
414 collect_pat (TuplePat pats _) acc = foldr collect_lpat acc pats
415 collect_pat (ConPatIn c ps) acc = foldr collect_lpat acc (hsConArgs ps)
416 collect_pat other acc = acc -- Literals, vars, wildcard