2 % (c) The University of Glasgow, 1992-2006
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
39 %************************************************************************
41 Some useful helpers for constructing syntax
43 %************************************************************************
45 These functions attempt to construct a not-completely-useless SrcSpan
46 from their components, compared with the nl* functions below which
47 just attach noSrcSpan to everything.
50 mkHsPar :: LHsExpr id -> LHsExpr id
51 mkHsPar e = L (getLoc e) (HsPar e)
53 mkSimpleMatch :: [LPat id] -> LHsExpr id -> LMatch id
54 mkSimpleMatch pats rhs
56 Match pats Nothing (unguardedGRHSs rhs)
60 (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
62 unguardedGRHSs :: LHsExpr id -> GRHSs id
63 unguardedGRHSs rhs = GRHSs (unguardedRHS rhs) emptyLocalBinds
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 (HsWrap (mkWpTyApps tys) (HsVar fun_id))
77 mkLHsWrap :: HsWrapper -> LHsExpr id -> LHsExpr id
78 mkLHsWrap co_fn (L loc e) = L loc (mkHsWrap co_fn e)
80 mkHsWrap :: HsWrapper -> HsExpr id -> HsExpr id
81 mkHsWrap co_fn e | isIdHsWrapper co_fn = e
82 | otherwise = HsWrap co_fn e
84 mkHsWrapCoI :: CoercionI -> HsExpr id -> HsExpr id
85 mkHsWrapCoI IdCo e = e
86 mkHsWrapCoI (ACo co) e = mkHsWrap (WpCast co) e
88 coiToHsWrapper :: CoercionI -> HsWrapper
89 coiToHsWrapper IdCo = idHsWrapper
90 coiToHsWrapper (ACo co) = WpCast co
92 mkHsLam :: [LPat id] -> LHsExpr id -> LHsExpr id
93 mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches))
95 matches = mkMatchGroup [mkSimpleMatch pats body]
97 mkMatchGroup :: [LMatch id] -> MatchGroup id
98 mkMatchGroup matches = MatchGroup matches placeHolderType
100 mkHsDictLet :: LHsBinds Id -> LHsExpr Id -> LHsExpr Id
101 -- Used for the dictionary bindings gotten from TcSimplify
102 -- We make them recursive to be on the safe side
103 mkHsDictLet binds expr
104 | isEmptyLHsBinds binds = expr
105 | otherwise = L (getLoc expr) (HsLet (HsValBinds val_binds) expr)
107 val_binds = ValBindsOut [(Recursive, binds)] []
109 mkHsConApp :: DataCon -> [Type] -> [HsExpr Id] -> LHsExpr Id
110 -- Used for constructing dictionary terms etc, so no locations
111 mkHsConApp data_con tys args
112 = foldl mk_app (nlHsTyApp (dataConWrapId data_con) tys) args
114 mk_app f a = noLoc (HsApp f (noLoc a))
116 mkSimpleHsAlt :: LPat id -> LHsExpr id -> LMatch id
117 -- A simple lambda with a single pattern, no binds, no guards; pre-typechecking
118 mkSimpleHsAlt pat expr
119 = mkSimpleMatch [pat] expr
121 -------------------------------
122 -- These are the bits of syntax that contain rebindable names
123 -- See RnEnv.lookupSyntaxName
125 mkHsIntegral :: Integer -> PostTcType -> HsOverLit id
126 mkHsFractional :: Rational -> PostTcType -> HsOverLit id
127 mkHsIsString :: FastString -> PostTcType -> HsOverLit id
128 mkHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> HsExpr id
130 mkNPat :: HsOverLit id -> Maybe (SyntaxExpr id) -> Pat id
131 mkNPlusKPat :: Located id -> HsOverLit id -> Pat id
133 mkTransformStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
134 mkTransformByStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
136 mkGroupUsingStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
137 mkGroupByStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
138 mkGroupByUsingStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
140 mkExprStmt :: LHsExpr idR -> StmtLR idL idR
141 mkBindStmt :: LPat idL -> LHsExpr idR -> StmtLR idL idR
142 mkRecStmt :: [LStmtLR idL idR] -> StmtLR idL idR
145 mkHsIntegral i = HsIntegral i noSyntaxExpr
146 mkHsFractional f = HsFractional f noSyntaxExpr
147 mkHsIsString s = HsIsString s noSyntaxExpr
148 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
150 mkNPat lit neg = NPat lit neg noSyntaxExpr
151 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
153 mkTransformStmt stmts usingExpr = TransformStmt (stmts, []) usingExpr Nothing
154 mkTransformByStmt stmts usingExpr byExpr = TransformStmt (stmts, []) usingExpr (Just byExpr)
156 mkGroupUsingStmt stmts usingExpr = GroupStmt (stmts, []) (GroupByNothing usingExpr)
157 mkGroupByStmt stmts byExpr = GroupStmt (stmts, []) (GroupBySomething (Right noSyntaxExpr) byExpr)
158 mkGroupByUsingStmt stmts byExpr usingExpr = GroupStmt (stmts, []) (GroupBySomething (Left usingExpr) byExpr)
160 mkExprStmt expr = ExprStmt expr noSyntaxExpr placeHolderType
161 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
162 mkRecStmt stmts = RecStmt stmts [] [] [] emptyLHsBinds
164 -------------------------------
165 --- A useful function for building @OpApps@. The operator is always a
166 -- variable, and we don't know the fixity yet.
167 mkHsOpApp :: LHsExpr id -> id -> LHsExpr id -> HsExpr id
168 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
170 mkHsSplice :: LHsExpr RdrName -> HsSplice RdrName
171 mkHsSplice e = HsSplice unqualSplice e
173 unqualSplice :: RdrName
174 unqualSplice = mkRdrUnqual (mkVarOccFS (fsLit "splice"))
175 -- A name (uniquified later) to
176 -- identify the splice
178 mkHsQuasiQuote :: RdrName -> SrcSpan -> FastString -> HsQuasiQuote RdrName
179 mkHsQuasiQuote quoter span quote = HsQuasiQuote unqualQuasiQuote quoter span quote
181 unqualQuasiQuote :: RdrName
182 unqualQuasiQuote = mkRdrUnqual (mkVarOccFS (fsLit "quasiquote"))
183 -- A name (uniquified later) to
184 -- identify the quasi-quote
186 mkHsString :: String -> HsLit
187 mkHsString s = HsString (mkFastString s)
190 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
191 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
195 %************************************************************************
197 Constructing syntax with no location info
199 %************************************************************************
202 nlHsVar :: id -> LHsExpr id
203 nlHsVar n = noLoc (HsVar n)
205 nlHsLit :: HsLit -> LHsExpr id
206 nlHsLit n = noLoc (HsLit n)
208 nlVarPat :: id -> LPat id
209 nlVarPat n = noLoc (VarPat n)
211 nlLitPat :: HsLit -> LPat id
212 nlLitPat l = noLoc (LitPat l)
214 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
215 nlHsApp f x = noLoc (HsApp f x)
217 nlHsIntLit :: Integer -> LHsExpr id
218 nlHsIntLit n = noLoc (HsLit (HsInt n))
220 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
221 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
223 nlHsVarApps :: id -> [id] -> LHsExpr id
224 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
226 mk f a = HsApp (noLoc f) (noLoc a)
228 nlConVarPat :: id -> [id] -> LPat id
229 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
231 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
232 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
234 nlConPat :: id -> [LPat id] -> LPat id
235 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
237 nlNullaryConPat :: id -> LPat id
238 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
240 nlWildConPat :: DataCon -> LPat RdrName
241 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
242 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
244 nlTuplePat :: [LPat id] -> Boxity -> LPat id
245 nlTuplePat pats box = noLoc (TuplePat pats box placeHolderType)
248 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
250 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
251 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
253 nlHsOpApp :: LHsExpr id -> id -> LHsExpr id -> LHsExpr id
254 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
256 nlHsLam :: LMatch id -> LHsExpr id
257 nlHsPar :: LHsExpr id -> LHsExpr id
258 nlHsIf :: LHsExpr id -> LHsExpr id -> LHsExpr id -> LHsExpr id
259 nlHsCase :: LHsExpr id -> [LMatch id] -> LHsExpr id
260 nlTuple :: [LHsExpr id] -> Boxity -> LHsExpr id
261 nlList :: [LHsExpr id] -> LHsExpr id
263 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
264 nlHsPar e = noLoc (HsPar e)
265 nlHsIf cond true false = noLoc (HsIf cond true false)
266 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
267 nlTuple exprs box = noLoc (ExplicitTuple exprs box)
268 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
270 nlHsAppTy :: LHsType name -> LHsType name -> LHsType name
271 nlHsTyVar :: name -> LHsType name
272 nlHsFunTy :: LHsType name -> LHsType name -> LHsType name
274 nlHsAppTy f t = noLoc (HsAppTy f t)
275 nlHsTyVar x = noLoc (HsTyVar x)
276 nlHsFunTy a b = noLoc (HsFunTy a b)
278 nlHsTyConApp :: name -> [LHsType name] -> LHsType name
279 nlHsTyConApp tycon tys = foldl nlHsAppTy (nlHsTyVar tycon) tys
284 %************************************************************************
286 Bindings; with a location at the top
288 %************************************************************************
291 mkFunBind :: Located id -> [LMatch id] -> HsBind id
292 -- Not infix, with place holders for coercion and free vars
293 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
294 fun_co_fn = idHsWrapper, bind_fvs = placeHolderNames,
298 mkVarBind :: SrcSpan -> id -> LHsExpr id -> LHsBind id
299 mkVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
302 mk_easy_FunBind :: SrcSpan -> id -> [LPat id]
303 -> LHsExpr id -> LHsBind id
305 mk_easy_FunBind loc fun pats expr
306 = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
309 mk_FunBind :: SrcSpan -> id
310 -> [([LPat id], LHsExpr id)]
313 mk_FunBind _ _ [] = panic "TcGenDeriv:mk_FunBind"
314 mk_FunBind loc fun pats_and_exprs
315 = L loc $ mkFunBind (L loc fun) matches
317 matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
320 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
321 mkMatch pats expr binds
322 = noLoc (Match (map paren pats) Nothing
323 (GRHSs (unguardedRHS expr) binds))
327 L l _ -> L l (ParPat p)
331 %************************************************************************
333 Collecting binders from HsBindGroups and HsBinds
335 %************************************************************************
337 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
345 it should return [x, y, f, a, b] (remember, order important).
348 collectLocalBinders :: HsLocalBindsLR idL idR -> [Located idL]
349 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
350 collectLocalBinders (HsIPBinds _) = []
351 collectLocalBinders EmptyLocalBinds = []
353 collectHsValBinders :: HsValBindsLR idL idR -> [Located idL]
354 collectHsValBinders (ValBindsIn binds _) = collectHsBindLocatedBinders binds
355 collectHsValBinders (ValBindsOut binds _) = foldr collect_one [] binds
357 collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
359 collectAcc :: HsBindLR idL idR -> [Located idL] -> [Located idL]
360 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
361 collectAcc (FunBind { fun_id = f }) acc = f : acc
362 collectAcc (VarBind { var_id = f }) acc = noLoc f : acc
363 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = _binds }) acc
364 = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
365 -- ++ foldr collectAcc acc binds
366 -- I don't think we want the binders from the nested binds
367 -- The only time we collect binders from a typechecked
368 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
370 collectHsBindBinders :: LHsBindsLR idL idR -> [idL]
371 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
373 collectHsBindLocatedBinders :: LHsBindsLR idL idR -> [Located idL]
374 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
378 %************************************************************************
380 Getting binders from statements
382 %************************************************************************
385 collectLStmtsBinders :: [LStmtLR idL idR] -> [Located idL]
386 collectLStmtsBinders = concatMap collectLStmtBinders
388 collectStmtsBinders :: [StmtLR idL idR] -> [Located idL]
389 collectStmtsBinders = concatMap collectStmtBinders
391 collectLStmtBinders :: LStmtLR idL idR -> [Located idL]
392 collectLStmtBinders = collectStmtBinders . unLoc
394 collectStmtBinders :: StmtLR idL idR -> [Located idL]
395 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
396 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
397 collectStmtBinders (LetStmt binds) = collectLocalBinders binds
398 collectStmtBinders (ExprStmt _ _ _) = []
399 collectStmtBinders (ParStmt xs) = collectLStmtsBinders
401 collectStmtBinders (TransformStmt (stmts, _) _ _) = collectLStmtsBinders stmts
402 collectStmtBinders (GroupStmt (stmts, _) _) = collectLStmtsBinders stmts
403 collectStmtBinders (RecStmt ss _ _ _ _) = collectLStmtsBinders ss
407 %************************************************************************
409 %* Gathering stuff out of patterns
411 %************************************************************************
413 This function @collectPatBinders@ works with the ``collectBinders''
414 functions for @HsBinds@, etc. The order in which the binders are
415 collected is important; see @HsBinds.lhs@.
417 It collects the bounds *value* variables in renamed patterns; type variables
421 collectPatBinders :: LPat a -> [a]
422 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
424 collectLocatedPatBinders :: LPat a -> [Located a]
425 collectLocatedPatBinders pat = collectl pat []
427 collectPatsBinders :: [LPat a] -> [a]
428 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
430 collectLocatedPatsBinders :: [LPat a] -> [Located a]
431 collectLocatedPatsBinders pats = foldr collectl [] pats
433 ---------------------
434 collectl :: LPat name -> [Located name] -> [Located name]
435 collectl (L l pat) bndrs
438 go (VarPat var) = L l var : bndrs
439 go (VarPatOut var bs) = L l var : collectHsBindLocatedBinders bs
441 go (WildPat _) = bndrs
442 go (LazyPat pat) = collectl pat bndrs
443 go (BangPat pat) = collectl pat bndrs
444 go (AsPat a pat) = a : collectl pat bndrs
445 go (ViewPat _ pat _) = collectl pat bndrs
446 go (ParPat pat) = collectl pat bndrs
448 go (ListPat pats _) = foldr collectl bndrs pats
449 go (PArrPat pats _) = foldr collectl bndrs pats
450 go (TuplePat pats _ _) = foldr collectl bndrs pats
452 go (ConPatIn _ ps) = foldr collectl bndrs (hsConPatArgs ps)
453 go (ConPatOut {pat_args=ps}) = foldr collectl bndrs (hsConPatArgs ps)
454 -- See Note [Dictionary binders in ConPatOut]
455 go (LitPat _) = bndrs
456 go (NPat _ _ _) = bndrs
457 go (NPlusKPat n _ _ _) = n : bndrs
459 go (SigPatIn pat _) = collectl pat bndrs
460 go (SigPatOut pat _) = collectl pat bndrs
461 go (QuasiQuotePat _) = bndrs
462 go (TypePat _) = bndrs
463 go (CoPat _ pat _) = collectl (noLoc pat) bndrs
466 Note [Dictionary binders in ConPatOut]
467 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
468 Do *not* gather (a) dictionary and (b) dictionary bindings as binders
469 of a ConPatOut pattern. For most calls it doesn't matter, because
470 it's pre-typechecker and there are no ConPatOuts. But it does matter
471 more in the desugarer; for example, DsUtils.mkSelectorBinds uses
472 collectPatBinders. In a lazy pattern, for example f ~(C x y) = ...,
473 we want to generate bindings for x,y but not for dictionaries bound by
474 C. (The type checker ensures they would not be used.)
476 Desugaring of arrow case expressions needs these bindings (see DsArrows
477 and arrowcase1), but SPJ (Jan 2007) says it's safer for it to use its
478 own pat-binder-collector:
480 Here's the problem. Consider
483 C :: Num a => a -> Int -> T a
485 f ~(C (n+1) m) = (n,m)
487 Here, the pattern (C (n+1)) binds a hidden dictionary (d::Num a),
488 and *also* uses that dictionary to match the (n+1) pattern. Yet, the
489 variables bound by the lazy pattern are n,m, *not* the dictionary d.
490 So in mkSelectorBinds in DsUtils, we want just m,n as the variables bound.
493 collectSigTysFromPats :: [InPat name] -> [LHsType name]
494 collectSigTysFromPats pats = foldr collect_lpat [] pats
496 collectSigTysFromPat :: InPat name -> [LHsType name]
497 collectSigTysFromPat pat = collect_lpat pat []
499 collect_lpat :: InPat name -> [LHsType name] -> [LHsType name]
500 collect_lpat pat acc = collect_pat (unLoc pat) acc
502 collect_pat :: Pat name -> [LHsType name] -> [LHsType name]
503 collect_pat (SigPatIn pat ty) acc = collect_lpat pat (ty:acc)
504 collect_pat (TypePat ty) acc = ty:acc
506 collect_pat (LazyPat pat) acc = collect_lpat pat acc
507 collect_pat (BangPat pat) acc = collect_lpat pat acc
508 collect_pat (AsPat _ pat) acc = collect_lpat pat acc
509 collect_pat (ParPat pat) acc = collect_lpat pat acc
510 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
511 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
512 collect_pat (TuplePat pats _ _) acc = foldr collect_lpat acc pats
513 collect_pat (ConPatIn _ ps) acc = foldr collect_lpat acc (hsConPatArgs ps)
514 collect_pat _ acc = acc -- Literals, vars, wildcard