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 BasicTypes ( RecFlag(..) )
31 import OccName ( mkVarOcc )
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) [])
63 (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
65 unguardedRHS :: LHsExpr id -> [LGRHS id]
66 unguardedRHS rhs@(L loc _) = [L loc (GRHS [L loc (ResultStmt 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 mkHsLet :: LHsBinds name -> LHsExpr name -> LHsExpr name
98 | isEmptyLHsBinds binds = expr
99 | otherwise = L (getLoc expr) (HsLet [HsBindGroup binds [] Recursive] expr)
101 mkHsConApp :: DataCon -> [Type] -> [HsExpr Id] -> LHsExpr Id
102 -- Used for constructing dictinoary terms etc, so no locations
103 mkHsConApp data_con tys args
104 = foldl mk_app (noLoc (HsVar (dataConWrapId data_con)) `mkHsTyApp` tys) args
106 mk_app f a = noLoc (HsApp f (noLoc a))
108 mkSimpleHsAlt :: LPat id -> LHsExpr id -> LMatch id
109 -- A simple lambda with a single pattern, no binds, no guards; pre-typechecking
110 mkSimpleHsAlt pat expr
111 = mkSimpleMatch [pat] expr
113 glueBindsOnGRHSs :: HsBindGroup id -> GRHSs id -> GRHSs id
115 glueBindsOnGRHSs binds1 (GRHSs grhss binds2)
116 = GRHSs grhss (binds1 : binds2)
118 -- These are the bits of syntax that contain rebindable names
119 -- See RnEnv.lookupSyntaxName
121 mkHsIntegral i = HsIntegral i placeHolderName
122 mkHsFractional f = HsFractional f placeHolderName
123 mkNPlusKPat n k = NPlusKPatIn n k placeHolderName
124 mkHsDo ctxt stmts = HsDo ctxt stmts [] placeHolderType
126 --- A useful function for building @OpApps@. The operator is always a
127 -- variable, and we don't know the fixity yet.
128 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
130 mkHsSplice e = HsSplice unqualSplice e
132 unqualSplice = mkRdrUnqual (mkVarOcc FSLIT("splice"))
133 -- A name (uniquified later) to
134 -- identify the splice
136 mkHsString s = HsString (mkFastString s)
140 %************************************************************************
142 Constructing syntax with no location info
144 %************************************************************************
147 nlHsVar :: id -> LHsExpr id
148 nlHsVar n = noLoc (HsVar n)
150 nlHsLit :: HsLit -> LHsExpr id
151 nlHsLit n = noLoc (HsLit n)
153 nlVarPat :: id -> LPat id
154 nlVarPat n = noLoc (VarPat n)
156 nlLitPat :: HsLit -> LPat id
157 nlLitPat l = noLoc (LitPat l)
159 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
160 nlHsApp f x = noLoc (HsApp f x)
162 nlHsIntLit n = noLoc (HsLit (HsInt n))
164 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
165 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
167 nlHsVarApps :: id -> [id] -> LHsExpr id
168 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
170 mk f a = HsApp (noLoc f) (noLoc a)
172 nlConVarPat :: id -> [id] -> LPat id
173 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
175 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
176 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
178 nlConPat :: id -> [LPat id] -> LPat id
179 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
181 nlNullaryConPat :: id -> LPat id
182 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
184 nlWildConPat :: DataCon -> LPat RdrName
185 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
186 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
188 nlTuplePat pats box = noLoc (TuplePat pats box)
189 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
191 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id
192 nlHsDo ctxt stmts = noLoc (mkHsDo ctxt stmts)
194 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
196 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
197 nlHsPar e = noLoc (HsPar e)
198 nlHsIf cond true false = noLoc (HsIf cond true false)
199 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
200 nlTuple exprs box = noLoc (ExplicitTuple exprs box)
201 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
203 nlHsAppTy f t = noLoc (HsAppTy f t)
204 nlHsTyVar x = noLoc (HsTyVar x)
205 nlHsFunTy a b = noLoc (HsFunTy a b)
207 nlExprStmt expr = noLoc (ExprStmt expr placeHolderType)
208 nlBindStmt pat expr = noLoc (BindStmt pat expr)
209 nlLetStmt binds = noLoc (LetStmt binds)
210 nlResultStmt expr = noLoc (ResultStmt expr)
211 nlParStmt stuff = noLoc (ParStmt stuff)
216 %************************************************************************
218 Bindings; with a location at the top
220 %************************************************************************
223 mkVarBind :: SrcSpan -> RdrName -> LHsExpr RdrName -> LHsBind RdrName
224 mkVarBind loc var rhs = mk_easy_FunBind loc var [] emptyLHsBinds rhs
226 mk_easy_FunBind :: SrcSpan -> RdrName -> [LPat RdrName]
227 -> LHsBinds RdrName -> LHsExpr RdrName
230 mk_easy_FunBind loc fun pats binds expr
231 = L loc (FunBind (L loc fun) False{-not infix-}
232 (mkMatchGroup [mk_easy_Match pats binds expr]))
234 mk_easy_Match pats binds expr
235 = mkMatch pats expr [HsBindGroup binds [] Recursive]
236 -- The renamer expects everything in its input to be a
237 -- "recursive" MonoBinds, and it is its job to sort things out
240 mk_FunBind :: SrcSpan
242 -> [([LPat RdrName], LHsExpr RdrName)]
245 mk_FunBind loc fun [] = panic "TcGenDeriv:mk_FunBind"
246 mk_FunBind loc fun pats_and_exprs
247 = L loc (FunBind (L loc fun) False{-not infix-}
248 (mkMatchGroup [mkMatch p e [] | (p,e) <-pats_and_exprs]))
250 mkMatch :: [LPat id] -> LHsExpr id -> [HsBindGroup id] -> LMatch id
251 mkMatch pats expr binds
252 = noLoc (Match (map paren pats) Nothing
254 (GRHSs (unguardedRHS expr) binds))
258 L l _ -> L l (ParPat p)
262 %************************************************************************
264 Collecting binders from HsBindGroups and HsBinds
266 %************************************************************************
268 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
276 it should return [x, y, f, a, b] (remember, order important).
279 collectGroupBinders :: [HsBindGroup name] -> [Located name]
280 collectGroupBinders groups = foldr collect_group [] groups
282 collect_group (HsBindGroup bag sigs is_rec) acc
283 = foldrBag (collectAcc . unLoc) acc bag
284 collect_group (HsIPBinds _) acc = acc
287 collectAcc :: HsBind name -> [Located name] -> [Located name]
288 collectAcc (PatBind pat _ _) acc = collectLocatedPatBinders pat ++ acc
289 collectAcc (FunBind f _ _) acc = f : acc
290 collectAcc (VarBind f _) acc = noLoc f : acc
291 collectAcc (AbsBinds _ _ dbinds _ binds) acc
292 = [noLoc dp | (_,dp,_) <- dbinds] ++ acc
293 -- ++ foldr collectAcc acc binds
294 -- I don't think we want the binders from the nested binds
295 -- The only time we collect binders from a typechecked
296 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
298 collectHsBindBinders :: Bag (LHsBind name) -> [name]
299 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
301 collectHsBindLocatedBinders :: Bag (LHsBind name) -> [Located name]
302 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
306 %************************************************************************
308 Getting pattern signatures out of bindings
310 %************************************************************************
312 Get all the pattern type signatures out of a bunch of bindings
315 collectSigTysFromHsBinds :: [LHsBind name] -> [LHsType name]
316 collectSigTysFromHsBinds binds = concat (map collectSigTysFromHsBind binds)
318 collectSigTysFromHsBind :: LHsBind name -> [LHsType name]
319 collectSigTysFromHsBind bind
323 = collectSigTysFromPat pat
324 go (FunBind f _ (MatchGroup ms _))
325 = [sig | L _ (Match [] (Just sig) _) <- ms]
326 -- A binding like x :: a = f y
327 -- is parsed as FunMonoBind, but for this purpose we
328 -- want to treat it as a pattern binding
331 %************************************************************************
333 Getting binders from statements
335 %************************************************************************
338 collectStmtsBinders :: [LStmt id] -> [Located id]
339 collectStmtsBinders = concatMap collectLStmtBinders
341 collectLStmtBinders = collectStmtBinders . unLoc
343 collectStmtBinders :: Stmt id -> [Located id]
344 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
345 collectStmtBinders (BindStmt pat _) = collectLocatedPatBinders pat
346 collectStmtBinders (LetStmt binds) = collectGroupBinders binds
347 collectStmtBinders (ExprStmt _ _) = []
348 collectStmtBinders (ResultStmt _) = []
349 collectStmtBinders (RecStmt ss _ _ _) = collectStmtsBinders ss
350 collectStmtBinders other = panic "collectStmtBinders"
354 %************************************************************************
356 %* Gathering stuff out of patterns
358 %************************************************************************
360 This function @collectPatBinders@ works with the ``collectBinders''
361 functions for @HsBinds@, etc. The order in which the binders are
362 collected is important; see @HsBinds.lhs@.
364 It collects the bounds *value* variables in renamed patterns; type variables
368 collectPatBinders :: LPat a -> [a]
369 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
371 collectLocatedPatBinders :: LPat a -> [Located a]
372 collectLocatedPatBinders pat = collectl pat []
374 collectPatsBinders :: [LPat a] -> [a]
375 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
377 collectLocatedPatsBinders :: [LPat a] -> [Located a]
378 collectLocatedPatsBinders pats = foldr collectl [] pats
380 ---------------------
381 collectl (L l (VarPat var)) bndrs = L l var : bndrs
382 collectl (L l (VarPatOut var bs)) bndrs = L l var : collectHsBindLocatedBinders bs
384 collectl (L l pat) bndrs = collect pat bndrs
386 ---------------------
387 collect (WildPat _) bndrs = bndrs
388 collect (LazyPat pat) bndrs = collectl pat bndrs
389 collect (AsPat a pat) bndrs = a : collectl pat bndrs
390 collect (ParPat pat) bndrs = collectl pat bndrs
392 collect (ListPat pats _) bndrs = foldr collectl bndrs pats
393 collect (PArrPat pats _) bndrs = foldr collectl bndrs pats
394 collect (TuplePat pats _) bndrs = foldr collectl bndrs pats
396 collect (ConPatIn c ps) bndrs = foldr collectl bndrs (hsConArgs ps)
397 collect (ConPatOut c _ ds bs ps _) bndrs = map noLoc ds
398 ++ collectHsBindLocatedBinders bs
399 ++ foldr collectl bndrs (hsConArgs ps)
400 collect (LitPat _) bndrs = bndrs
401 collect (NPatIn _ _) bndrs = bndrs
402 collect (NPatOut _ _ _) bndrs = bndrs
404 collect (NPlusKPatIn n _ _) bndrs = n : bndrs
405 collect (NPlusKPatOut n _ _ _) bndrs = n : bndrs
407 collect (SigPatIn pat _) bndrs = collectl pat bndrs
408 collect (SigPatOut pat _) bndrs = collectl pat bndrs
409 collect (TypePat ty) bndrs = bndrs
410 collect (DictPat ids1 ids2) bndrs = map noLoc ids1 ++ map noLoc ids2
415 collectSigTysFromPats :: [InPat name] -> [LHsType name]
416 collectSigTysFromPats pats = foldr collect_lpat [] pats
418 collectSigTysFromPat :: InPat name -> [LHsType name]
419 collectSigTysFromPat pat = collect_lpat pat []
421 collect_lpat pat acc = collect_pat (unLoc pat) acc
423 collect_pat (SigPatIn pat ty) acc = collect_lpat pat (ty:acc)
424 collect_pat (TypePat ty) acc = ty:acc
426 collect_pat (LazyPat pat) acc = collect_lpat pat acc
427 collect_pat (AsPat a pat) acc = collect_lpat pat acc
428 collect_pat (ParPat pat) acc = collect_lpat pat acc
429 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
430 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
431 collect_pat (TuplePat pats _) acc = foldr collect_lpat acc pats
432 collect_pat (ConPatIn c ps) acc = foldr collect_lpat acc (hsConArgs ps)
433 collect_pat other acc = acc -- Literals, vars, wildcard