3 % (c) The University of Glasgow, 1992-2006
6 Here we collect a variety of helper functions that construct or
7 analyse HsSyn. All these functions deal with generic HsSyn; functions
8 which deal with the intantiated versions are located elsewhere:
10 Parameterised by Module
11 ---------------- -------------
12 RdrName parser/RdrHsSyn
40 %************************************************************************
42 Some useful helpers for constructing syntax
44 %************************************************************************
46 These functions attempt to construct a not-completely-useless SrcSpan
47 from their components, compared with the nl* functions below which
48 just attach noSrcSpan to everything.
51 mkHsPar :: LHsExpr id -> LHsExpr id
52 mkHsPar e = L (getLoc e) (HsPar e)
54 mkSimpleMatch :: [LPat id] -> LHsExpr id -> LMatch id
55 mkSimpleMatch pats rhs
57 Match pats Nothing (unguardedGRHSs rhs)
61 (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
63 unguardedGRHSs :: LHsExpr id -> GRHSs id
64 unguardedGRHSs rhs = GRHSs (unguardedRHS rhs) emptyLocalBinds
66 unguardedRHS :: LHsExpr id -> [LGRHS id]
67 unguardedRHS rhs@(L loc _) = [L loc (GRHS [] rhs)]
69 mkHsAppTy :: LHsType name -> LHsType name -> LHsType name
70 mkHsAppTy t1 t2 = addCLoc t1 t2 (HsAppTy t1 t2)
72 mkHsApp :: LHsExpr name -> LHsExpr name -> LHsExpr name
73 mkHsApp e1 e2 = addCLoc e1 e2 (HsApp e1 e2)
75 nlHsTyApp :: name -> [Type] -> LHsExpr name
76 nlHsTyApp fun_id tys = noLoc (HsWrap (mkWpTyApps tys) (HsVar fun_id))
78 mkLHsWrap :: HsWrapper -> LHsExpr id -> LHsExpr id
79 mkLHsWrap co_fn (L loc e) = L loc (mkHsWrap co_fn e)
81 mkHsWrap :: HsWrapper -> HsExpr id -> HsExpr id
82 mkHsWrap co_fn e | isIdHsWrapper co_fn = e
83 | otherwise = HsWrap co_fn e
85 mkHsWrapCoI :: CoercionI -> HsExpr id -> HsExpr id
86 mkHsWrapCoI IdCo e = e
87 mkHsWrapCoI (ACo co) e = mkHsWrap (WpCast co) e
89 coiToHsWrapper :: CoercionI -> HsWrapper
90 coiToHsWrapper IdCo = idHsWrapper
91 coiToHsWrapper (ACo co) = WpCast co
93 mkHsLam :: [LPat id] -> LHsExpr id -> LHsExpr id
94 mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches))
96 matches = mkMatchGroup [mkSimpleMatch pats body]
98 mkMatchGroup :: [LMatch id] -> MatchGroup id
99 mkMatchGroup matches = MatchGroup matches placeHolderType
101 mkHsDictLet :: LHsBinds Id -> LHsExpr Id -> LHsExpr Id
102 -- Used for the dictionary bindings gotten from TcSimplify
103 -- We make them recursive to be on the safe side
104 mkHsDictLet binds expr
105 | isEmptyLHsBinds binds = expr
106 | otherwise = L (getLoc expr) (HsLet (HsValBinds val_binds) expr)
108 val_binds = ValBindsOut [(Recursive, binds)] []
110 mkHsConApp :: DataCon -> [Type] -> [HsExpr Id] -> LHsExpr Id
111 -- Used for constructing dictionary terms etc, so no locations
112 mkHsConApp data_con tys args
113 = foldl mk_app (nlHsTyApp (dataConWrapId data_con) tys) args
115 mk_app f a = noLoc (HsApp f (noLoc a))
117 mkSimpleHsAlt :: LPat id -> LHsExpr id -> LMatch id
118 -- A simple lambda with a single pattern, no binds, no guards; pre-typechecking
119 mkSimpleHsAlt pat expr
120 = mkSimpleMatch [pat] expr
122 -------------------------------
123 -- These are the bits of syntax that contain rebindable names
124 -- See RnEnv.lookupSyntaxName
126 mkHsIntegral :: Integer -> PostTcType -> HsOverLit id
127 mkHsFractional :: Rational -> PostTcType -> HsOverLit id
128 mkHsIsString :: FastString -> PostTcType -> HsOverLit id
129 mkHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> HsExpr id
131 mkNPat :: HsOverLit id -> Maybe (SyntaxExpr id) -> Pat id
132 mkNPlusKPat :: Located id -> HsOverLit id -> Pat id
134 mkTransformStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
135 mkTransformByStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
137 mkGroupUsingStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
138 mkGroupByStmt :: [LStmt idL] -> LHsExpr idR -> StmtLR idL idR
139 mkGroupByUsingStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
141 mkExprStmt :: LHsExpr idR -> StmtLR idL idR
142 mkBindStmt :: LPat idL -> LHsExpr idR -> StmtLR idL idR
143 mkRecStmt :: [LStmtLR idL idR] -> StmtLR idL idR
146 mkHsIntegral i = OverLit (HsIntegral i) noRebindableInfo noSyntaxExpr
147 mkHsFractional f = OverLit (HsFractional f) noRebindableInfo noSyntaxExpr
148 mkHsIsString s = OverLit (HsIsString s) noRebindableInfo noSyntaxExpr
150 noRebindableInfo :: Bool
151 noRebindableInfo = error "noRebindableInfo" -- Just another placeholder;
153 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
155 mkNPat lit neg = NPat lit neg noSyntaxExpr
156 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
158 mkTransformStmt stmts usingExpr = TransformStmt (stmts, []) usingExpr Nothing
159 mkTransformByStmt stmts usingExpr byExpr = TransformStmt (stmts, []) usingExpr (Just byExpr)
161 mkGroupUsingStmt stmts usingExpr = GroupStmt (stmts, []) (GroupByNothing usingExpr)
162 mkGroupByStmt stmts byExpr = GroupStmt (stmts, []) (GroupBySomething (Right noSyntaxExpr) byExpr)
163 mkGroupByUsingStmt stmts byExpr usingExpr = GroupStmt (stmts, []) (GroupBySomething (Left usingExpr) byExpr)
165 mkExprStmt expr = ExprStmt expr noSyntaxExpr placeHolderType
166 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
167 mkRecStmt stmts = RecStmt stmts [] [] [] emptyLHsBinds
169 -------------------------------
170 --- A useful function for building @OpApps@. The operator is always a
171 -- variable, and we don't know the fixity yet.
172 mkHsOpApp :: LHsExpr id -> id -> LHsExpr id -> HsExpr id
173 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
175 mkHsSplice :: LHsExpr RdrName -> HsSplice RdrName
176 mkHsSplice e = HsSplice unqualSplice e
178 unqualSplice :: RdrName
179 unqualSplice = mkRdrUnqual (mkVarOccFS (fsLit "splice"))
180 -- A name (uniquified later) to
181 -- identify the splice
183 mkHsQuasiQuote :: RdrName -> SrcSpan -> FastString -> HsQuasiQuote RdrName
184 mkHsQuasiQuote quoter span quote = HsQuasiQuote unqualQuasiQuote quoter span quote
186 unqualQuasiQuote :: RdrName
187 unqualQuasiQuote = mkRdrUnqual (mkVarOccFS (fsLit "quasiquote"))
188 -- A name (uniquified later) to
189 -- identify the quasi-quote
191 mkHsString :: String -> HsLit
192 mkHsString s = HsString (mkFastString s)
195 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
196 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
200 %************************************************************************
202 Constructing syntax with no location info
204 %************************************************************************
207 nlHsVar :: id -> LHsExpr id
208 nlHsVar n = noLoc (HsVar n)
210 nlHsLit :: HsLit -> LHsExpr id
211 nlHsLit n = noLoc (HsLit n)
213 nlVarPat :: id -> LPat id
214 nlVarPat n = noLoc (VarPat n)
216 nlLitPat :: HsLit -> LPat id
217 nlLitPat l = noLoc (LitPat l)
219 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
220 nlHsApp f x = noLoc (HsApp f x)
222 nlHsIntLit :: Integer -> LHsExpr id
223 nlHsIntLit n = noLoc (HsLit (HsInt n))
225 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
226 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
228 nlHsVarApps :: id -> [id] -> LHsExpr id
229 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
231 mk f a = HsApp (noLoc f) (noLoc a)
233 nlConVarPat :: id -> [id] -> LPat id
234 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
236 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
237 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
239 nlConPat :: id -> [LPat id] -> LPat id
240 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
242 nlNullaryConPat :: id -> LPat id
243 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
245 nlWildConPat :: DataCon -> LPat RdrName
246 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
247 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
249 nlTuplePat :: [LPat id] -> Boxity -> LPat id
250 nlTuplePat pats box = noLoc (TuplePat pats box placeHolderType)
253 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
255 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
256 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
258 nlHsOpApp :: LHsExpr id -> id -> LHsExpr id -> LHsExpr id
259 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
261 nlHsLam :: LMatch id -> LHsExpr id
262 nlHsPar :: LHsExpr id -> LHsExpr id
263 nlHsIf :: LHsExpr id -> LHsExpr id -> LHsExpr id -> LHsExpr id
264 nlHsCase :: LHsExpr id -> [LMatch id] -> LHsExpr id
265 nlTuple :: [LHsExpr id] -> Boxity -> LHsExpr id
266 nlList :: [LHsExpr id] -> LHsExpr id
268 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
269 nlHsPar e = noLoc (HsPar e)
270 nlHsIf cond true false = noLoc (HsIf cond true false)
271 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
272 nlTuple exprs box = noLoc (ExplicitTuple exprs box)
273 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
275 nlHsAppTy :: LHsType name -> LHsType name -> LHsType name
276 nlHsTyVar :: name -> LHsType name
277 nlHsFunTy :: LHsType name -> LHsType name -> LHsType name
279 nlHsAppTy f t = noLoc (HsAppTy f t)
280 nlHsTyVar x = noLoc (HsTyVar x)
281 nlHsFunTy a b = noLoc (HsFunTy a b)
283 nlHsTyConApp :: name -> [LHsType name] -> LHsType name
284 nlHsTyConApp tycon tys = foldl nlHsAppTy (nlHsTyVar tycon) tys
289 %************************************************************************
291 Bindings; with a location at the top
293 %************************************************************************
296 mkFunBind :: Located id -> [LMatch id] -> HsBind id
297 -- Not infix, with place holders for coercion and free vars
298 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
299 fun_co_fn = idHsWrapper, bind_fvs = placeHolderNames,
303 mkHsVarBind :: SrcSpan -> id -> LHsExpr id -> LHsBind id
304 mkHsVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
306 mkVarBind :: id -> LHsExpr id -> LHsBind id
307 mkVarBind var rhs = L (getLoc rhs) $
308 VarBind { var_id = var, var_rhs = rhs, var_inline = False }
311 mk_easy_FunBind :: SrcSpan -> id -> [LPat id]
312 -> LHsExpr id -> LHsBind id
314 mk_easy_FunBind loc fun pats expr
315 = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
318 mk_FunBind :: SrcSpan -> id
319 -> [([LPat id], LHsExpr id)]
322 mk_FunBind _ _ [] = panic "TcGenDeriv:mk_FunBind"
323 mk_FunBind loc fun pats_and_exprs
324 = L loc $ mkFunBind (L loc fun) matches
326 matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
329 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
330 mkMatch pats expr binds
331 = noLoc (Match (map paren pats) Nothing
332 (GRHSs (unguardedRHS expr) binds))
336 L l _ -> L l (ParPat p)
340 %************************************************************************
342 Collecting binders from HsBindGroups and HsBinds
344 %************************************************************************
346 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
354 it should return [x, y, f, a, b] (remember, order important).
357 collectLocalBinders :: HsLocalBindsLR idL idR -> [Located idL]
358 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
359 collectLocalBinders (HsIPBinds _) = []
360 collectLocalBinders EmptyLocalBinds = []
362 collectHsValBinders :: HsValBindsLR idL idR -> [Located idL]
363 collectHsValBinders (ValBindsIn binds _) = collectHsBindLocatedBinders binds
364 collectHsValBinders (ValBindsOut binds _) = foldr collect_one [] binds
366 collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
368 collectAcc :: HsBindLR idL idR -> [Located idL] -> [Located idL]
369 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
370 collectAcc (FunBind { fun_id = f }) acc = f : acc
371 collectAcc (VarBind { var_id = f }) acc = noLoc f : acc
372 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = _binds }) acc
373 = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
374 -- ++ foldr collectAcc acc binds
375 -- I don't think we want the binders from the nested binds
376 -- The only time we collect binders from a typechecked
377 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
379 collectHsBindBinders :: LHsBindsLR idL idR -> [idL]
380 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
382 collectHsBindLocatedBinders :: LHsBindsLR idL idR -> [Located idL]
383 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
387 %************************************************************************
389 Getting binders from statements
391 %************************************************************************
394 collectLStmtsBinders :: [LStmtLR idL idR] -> [Located idL]
395 collectLStmtsBinders = concatMap collectLStmtBinders
397 collectStmtsBinders :: [StmtLR idL idR] -> [Located idL]
398 collectStmtsBinders = concatMap collectStmtBinders
400 collectLStmtBinders :: LStmtLR idL idR -> [Located idL]
401 collectLStmtBinders = collectStmtBinders . unLoc
403 collectStmtBinders :: StmtLR idL idR -> [Located idL]
404 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
405 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
406 collectStmtBinders (LetStmt binds) = collectLocalBinders binds
407 collectStmtBinders (ExprStmt _ _ _) = []
408 collectStmtBinders (ParStmt xs) = collectLStmtsBinders
410 collectStmtBinders (TransformStmt (stmts, _) _ _) = collectLStmtsBinders stmts
411 collectStmtBinders (GroupStmt (stmts, _) _) = collectLStmtsBinders stmts
412 collectStmtBinders (RecStmt ss _ _ _ _) = collectLStmtsBinders ss
416 %************************************************************************
418 %* Gathering stuff out of patterns
420 %************************************************************************
422 This function @collectPatBinders@ works with the ``collectBinders''
423 functions for @HsBinds@, etc. The order in which the binders are
424 collected is important; see @HsBinds.lhs@.
426 It collects the bounds *value* variables in renamed patterns; type variables
430 collectPatBinders :: LPat a -> [a]
431 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
433 collectLocatedPatBinders :: LPat a -> [Located a]
434 collectLocatedPatBinders pat = collectl pat []
436 collectPatsBinders :: [LPat a] -> [a]
437 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
439 collectLocatedPatsBinders :: [LPat a] -> [Located a]
440 collectLocatedPatsBinders pats = foldr collectl [] pats
442 ---------------------
443 collectl :: LPat name -> [Located name] -> [Located name]
444 collectl (L l pat) bndrs
447 go (VarPat var) = L l var : bndrs
448 go (VarPatOut var bs) = L l var : collectHsBindLocatedBinders bs
450 go (WildPat _) = bndrs
451 go (LazyPat pat) = collectl pat bndrs
452 go (BangPat pat) = collectl pat bndrs
453 go (AsPat a pat) = a : collectl pat bndrs
454 go (ViewPat _ pat _) = collectl pat bndrs
455 go (ParPat pat) = collectl pat bndrs
457 go (ListPat pats _) = foldr collectl bndrs pats
458 go (PArrPat pats _) = foldr collectl bndrs pats
459 go (TuplePat pats _ _) = foldr collectl bndrs pats
461 go (ConPatIn _ ps) = foldr collectl bndrs (hsConPatArgs ps)
462 go (ConPatOut {pat_args=ps}) = foldr collectl bndrs (hsConPatArgs ps)
463 -- See Note [Dictionary binders in ConPatOut]
464 go (LitPat _) = bndrs
465 go (NPat _ _ _) = bndrs
466 go (NPlusKPat n _ _ _) = n : bndrs
468 go (SigPatIn pat _) = collectl pat bndrs
469 go (SigPatOut pat _) = collectl pat bndrs
470 go (QuasiQuotePat _) = bndrs
471 go (TypePat _) = bndrs
472 go (CoPat _ pat _) = collectl (noLoc pat) bndrs
475 Note [Dictionary binders in ConPatOut]
476 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477 Do *not* gather (a) dictionary and (b) dictionary bindings as binders
478 of a ConPatOut pattern. For most calls it doesn't matter, because
479 it's pre-typechecker and there are no ConPatOuts. But it does matter
480 more in the desugarer; for example, DsUtils.mkSelectorBinds uses
481 collectPatBinders. In a lazy pattern, for example f ~(C x y) = ...,
482 we want to generate bindings for x,y but not for dictionaries bound by
483 C. (The type checker ensures they would not be used.)
485 Desugaring of arrow case expressions needs these bindings (see DsArrows
486 and arrowcase1), but SPJ (Jan 2007) says it's safer for it to use its
487 own pat-binder-collector:
489 Here's the problem. Consider
492 C :: Num a => a -> Int -> T a
494 f ~(C (n+1) m) = (n,m)
496 Here, the pattern (C (n+1)) binds a hidden dictionary (d::Num a),
497 and *also* uses that dictionary to match the (n+1) pattern. Yet, the
498 variables bound by the lazy pattern are n,m, *not* the dictionary d.
499 So in mkSelectorBinds in DsUtils, we want just m,n as the variables bound.
502 collectSigTysFromPats :: [InPat name] -> [LHsType name]
503 collectSigTysFromPats pats = foldr collect_lpat [] pats
505 collectSigTysFromPat :: InPat name -> [LHsType name]
506 collectSigTysFromPat pat = collect_lpat pat []
508 collect_lpat :: InPat name -> [LHsType name] -> [LHsType name]
509 collect_lpat pat acc = collect_pat (unLoc pat) acc
511 collect_pat :: Pat name -> [LHsType name] -> [LHsType name]
512 collect_pat (SigPatIn pat ty) acc = collect_lpat pat (ty:acc)
513 collect_pat (TypePat ty) acc = ty:acc
515 collect_pat (LazyPat pat) acc = collect_lpat pat acc
516 collect_pat (BangPat pat) acc = collect_lpat pat acc
517 collect_pat (AsPat _ pat) acc = collect_lpat pat acc
518 collect_pat (ParPat pat) acc = collect_lpat pat acc
519 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
520 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
521 collect_pat (TuplePat pats _ _) acc = foldr collect_lpat acc pats
522 collect_pat (ConPatIn _ ps) acc = foldr collect_lpat acc (hsConPatArgs ps)
523 collect_pat _ acc = acc -- Literals, vars, wildcard