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 nlHsTyApp :: name -> [Type] -> LHsExpr name
75 nlHsTyApp fun_id tys = noLoc (HsCoerce (mkCoTyApps tys) (HsVar fun_id))
77 mkLHsCoerce :: ExprCoFn -> LHsExpr id -> LHsExpr id
78 mkLHsCoerce co_fn (L loc e) = L loc (mkHsCoerce co_fn e)
80 mkHsCoerce :: ExprCoFn -> HsExpr id -> HsExpr id
81 mkHsCoerce co_fn e | isIdCoercion co_fn = e
82 | otherwise = HsCoerce co_fn e
84 mkHsLam :: [LPat id] -> LHsExpr id -> LHsExpr id
85 mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches))
87 matches = mkMatchGroup [mkSimpleMatch pats body]
89 mkMatchGroup :: [LMatch id] -> MatchGroup id
90 mkMatchGroup matches = MatchGroup matches placeHolderType
92 mkHsDictLet :: LHsBinds Id -> LHsExpr Id -> LHsExpr Id
93 -- Used for the dictionary bindings gotten from TcSimplify
94 -- We make them recursive to be on the safe side
95 mkHsDictLet binds expr
96 | isEmptyLHsBinds binds = expr
97 | otherwise = L (getLoc expr) (HsLet (HsValBinds val_binds) expr)
99 val_binds = ValBindsOut [(Recursive, binds)] []
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 (nlHsTyApp (dataConWrapId data_con) 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 -------------------------------
114 -- These are the bits of syntax that contain rebindable names
115 -- See RnEnv.lookupSyntaxName
117 mkHsIntegral i = HsIntegral i noSyntaxExpr
118 mkHsFractional f = HsFractional f noSyntaxExpr
119 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
121 mkNPat lit neg = NPat lit neg noSyntaxExpr placeHolderType
122 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
124 mkExprStmt expr = ExprStmt expr noSyntaxExpr placeHolderType
125 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
126 mkRecStmt stmts = RecStmt stmts [] [] [] emptyLHsBinds
128 -------------------------------
129 --- A useful function for building @OpApps@. The operator is always a
130 -- variable, and we don't know the fixity yet.
131 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
133 mkHsSplice e = HsSplice unqualSplice e
135 unqualSplice = mkRdrUnqual (mkVarOccFS FSLIT("splice"))
136 -- A name (uniquified later) to
137 -- identify the splice
139 mkHsString s = HsString (mkFastString s)
142 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
143 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
147 %************************************************************************
149 Constructing syntax with no location info
151 %************************************************************************
154 nlHsVar :: id -> LHsExpr id
155 nlHsVar n = noLoc (HsVar n)
157 nlHsLit :: HsLit -> LHsExpr id
158 nlHsLit n = noLoc (HsLit n)
160 nlVarPat :: id -> LPat id
161 nlVarPat n = noLoc (VarPat n)
163 nlLitPat :: HsLit -> LPat id
164 nlLitPat l = noLoc (LitPat l)
166 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
167 nlHsApp f x = noLoc (HsApp f x)
169 nlHsIntLit n = noLoc (HsLit (HsInt n))
171 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
172 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
174 nlHsVarApps :: id -> [id] -> LHsExpr id
175 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
177 mk f a = HsApp (noLoc f) (noLoc a)
179 nlConVarPat :: id -> [id] -> LPat id
180 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
182 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
183 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
185 nlConPat :: id -> [LPat id] -> LPat id
186 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
188 nlNullaryConPat :: id -> LPat id
189 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
191 nlWildConPat :: DataCon -> LPat RdrName
192 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
193 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
195 nlTuplePat pats box = noLoc (TuplePat pats box placeHolderType)
196 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
198 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
199 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
201 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
203 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
204 nlHsPar e = noLoc (HsPar e)
205 nlHsIf cond true false = noLoc (HsIf cond true false)
206 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
207 nlTuple exprs box = noLoc (ExplicitTuple exprs box)
208 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
210 nlHsAppTy f t = noLoc (HsAppTy f t)
211 nlHsTyVar x = noLoc (HsTyVar x)
212 nlHsFunTy a b = noLoc (HsFunTy a b)
217 %************************************************************************
219 Bindings; with a location at the top
221 %************************************************************************
224 mkFunBind :: Located id -> [LMatch id] -> HsBind id
225 -- Not infix, with place holders for coercion and free vars
226 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
227 fun_co_fn = idCoercion, bind_fvs = placeHolderNames }
230 mkVarBind :: SrcSpan -> RdrName -> LHsExpr RdrName -> LHsBind RdrName
231 mkVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
234 mk_easy_FunBind :: SrcSpan -> RdrName -> [LPat RdrName]
235 -> LHsExpr RdrName -> LHsBind RdrName
237 mk_easy_FunBind loc fun pats expr
238 = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
241 mk_FunBind :: SrcSpan -> RdrName
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 $ mkFunBind (L loc fun) matches
249 matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
252 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
253 mkMatch pats expr binds
254 = noLoc (Match (map paren pats) Nothing
255 (GRHSs (unguardedRHS expr) binds))
259 L l _ -> L l (ParPat p)
263 %************************************************************************
265 Collecting binders from HsBindGroups and HsBinds
267 %************************************************************************
269 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
277 it should return [x, y, f, a, b] (remember, order important).
280 collectLocalBinders :: HsLocalBinds name -> [Located name]
281 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
282 collectLocalBinders (HsIPBinds _) = []
283 collectLocalBinders EmptyLocalBinds = []
285 collectHsValBinders :: HsValBinds name -> [Located name]
286 collectHsValBinders (ValBindsIn binds sigs) = collectHsBindLocatedBinders binds
287 collectHsValBinders (ValBindsOut binds sigs) = foldr collect_one [] binds
289 collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
291 collectAcc :: HsBind name -> [Located name] -> [Located name]
292 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
293 collectAcc (FunBind { fun_id = f }) acc = f : acc
294 collectAcc (VarBind { var_id = f }) acc = noLoc f : acc
295 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = binds }) acc
296 = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
297 -- ++ foldr collectAcc acc binds
298 -- I don't think we want the binders from the nested binds
299 -- The only time we collect binders from a typechecked
300 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
302 collectHsBindBinders :: LHsBinds name -> [name]
303 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
305 collectHsBindLocatedBinders :: LHsBinds name -> [Located name]
306 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
310 %************************************************************************
312 Getting binders from statements
314 %************************************************************************
317 collectLStmtsBinders :: [LStmt id] -> [Located id]
318 collectLStmtsBinders = concatMap collectLStmtBinders
320 collectStmtsBinders :: [Stmt id] -> [Located id]
321 collectStmtsBinders = concatMap collectStmtBinders
323 collectLStmtBinders :: LStmt id -> [Located id]
324 collectLStmtBinders = collectStmtBinders . unLoc
326 collectStmtBinders :: Stmt id -> [Located id]
327 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
328 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
329 collectStmtBinders (LetStmt binds) = collectLocalBinders binds
330 collectStmtBinders (ExprStmt _ _ _) = []
331 collectStmtBinders (RecStmt ss _ _ _ _) = collectLStmtsBinders ss
332 collectStmtBinders other = panic "collectStmtBinders"
336 %************************************************************************
338 %* Gathering stuff out of patterns
340 %************************************************************************
342 This function @collectPatBinders@ works with the ``collectBinders''
343 functions for @HsBinds@, etc. The order in which the binders are
344 collected is important; see @HsBinds.lhs@.
346 It collects the bounds *value* variables in renamed patterns; type variables
350 collectPatBinders :: LPat a -> [a]
351 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
353 collectLocatedPatBinders :: LPat a -> [Located a]
354 collectLocatedPatBinders pat = collectl pat []
356 collectPatsBinders :: [LPat a] -> [a]
357 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
359 collectLocatedPatsBinders :: [LPat a] -> [Located a]
360 collectLocatedPatsBinders pats = foldr collectl [] pats
362 ---------------------
363 collectl (L l pat) bndrs
366 go (VarPat var) = L l var : bndrs
367 go (VarPatOut var bs) = L l var : collectHsBindLocatedBinders bs
369 go (WildPat _) = bndrs
370 go (LazyPat pat) = collectl pat bndrs
371 go (BangPat pat) = collectl pat bndrs
372 go (AsPat a pat) = a : collectl pat bndrs
373 go (ParPat pat) = collectl pat bndrs
375 go (ListPat pats _) = foldr collectl bndrs pats
376 go (PArrPat pats _) = foldr collectl bndrs pats
377 go (TuplePat pats _ _) = foldr collectl bndrs pats
379 go (ConPatIn c ps) = foldr collectl bndrs (hsConArgs ps)
380 go (ConPatOut { pat_dicts = ds,
381 pat_binds = bs, pat_args = ps })
383 ++ collectHsBindLocatedBinders bs
384 ++ foldr collectl bndrs (hsConArgs ps)
385 go (LitPat _) = bndrs
386 go (NPat _ _ _ _) = bndrs
387 go (NPlusKPat n _ _ _) = n : bndrs
389 go (SigPatIn pat _) = collectl pat bndrs
390 go (SigPatOut pat _) = collectl pat bndrs
391 go (TypePat ty) = bndrs
392 go (DictPat ids1 ids2) = map noLoc ids1 ++ map noLoc ids2
394 go (CoPat _ pat ty) = collectl (noLoc pat) bndrs
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 (BangPat pat) acc = collect_lpat pat acc
411 collect_pat (AsPat a pat) acc = collect_lpat pat acc
412 collect_pat (ParPat pat) acc = collect_lpat pat acc
413 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
414 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
415 collect_pat (TuplePat pats _ _) acc = foldr collect_lpat acc pats
416 collect_pat (ConPatIn c ps) acc = foldr collect_lpat acc (hsConArgs ps)
417 collect_pat other acc = acc -- Literals, vars, wildcard