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
144 emptyRecStmt :: StmtLR idL idR
145 mkRecStmt :: [LStmtLR idL idR] -> StmtLR idL idR
148 mkHsIntegral i = OverLit (HsIntegral i) noRebindableInfo noSyntaxExpr
149 mkHsFractional f = OverLit (HsFractional f) noRebindableInfo noSyntaxExpr
150 mkHsIsString s = OverLit (HsIsString s) noRebindableInfo noSyntaxExpr
152 noRebindableInfo :: Bool
153 noRebindableInfo = error "noRebindableInfo" -- Just another placeholder;
155 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
157 mkNPat lit neg = NPat lit neg noSyntaxExpr
158 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
160 mkTransformStmt stmts usingExpr = TransformStmt (stmts, []) usingExpr Nothing
161 mkTransformByStmt stmts usingExpr byExpr = TransformStmt (stmts, []) usingExpr (Just byExpr)
163 mkGroupUsingStmt stmts usingExpr = GroupStmt (stmts, []) (GroupByNothing usingExpr)
164 mkGroupByStmt stmts byExpr = GroupStmt (stmts, []) (GroupBySomething (Right noSyntaxExpr) byExpr)
165 mkGroupByUsingStmt stmts byExpr usingExpr = GroupStmt (stmts, []) (GroupBySomething (Left usingExpr) byExpr)
167 mkExprStmt expr = ExprStmt expr noSyntaxExpr placeHolderType
168 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
170 emptyRecStmt = RecStmt { recS_stmts = [], recS_later_ids = [], recS_rec_ids = []
171 , recS_ret_fn = noSyntaxExpr, recS_mfix_fn = noSyntaxExpr
172 , recS_bind_fn = noSyntaxExpr
173 , recS_rec_rets = [], recS_dicts = emptyLHsBinds }
175 mkRecStmt stmts = emptyRecStmt { recS_stmts = stmts }
177 -------------------------------
178 --- A useful function for building @OpApps@. The operator is always a
179 -- variable, and we don't know the fixity yet.
180 mkHsOpApp :: LHsExpr id -> id -> LHsExpr id -> HsExpr id
181 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
183 mkHsSplice :: LHsExpr RdrName -> HsSplice RdrName
184 mkHsSplice e = HsSplice unqualSplice e
186 unqualSplice :: RdrName
187 unqualSplice = mkRdrUnqual (mkVarOccFS (fsLit "splice"))
188 -- A name (uniquified later) to
189 -- identify the splice
191 mkHsQuasiQuote :: RdrName -> SrcSpan -> FastString -> HsQuasiQuote RdrName
192 mkHsQuasiQuote quoter span quote = HsQuasiQuote unqualQuasiQuote quoter span quote
194 unqualQuasiQuote :: RdrName
195 unqualQuasiQuote = mkRdrUnqual (mkVarOccFS (fsLit "quasiquote"))
196 -- A name (uniquified later) to
197 -- identify the quasi-quote
199 mkHsString :: String -> HsLit
200 mkHsString s = HsString (mkFastString s)
203 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
204 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
208 %************************************************************************
210 Constructing syntax with no location info
212 %************************************************************************
215 nlHsVar :: id -> LHsExpr id
216 nlHsVar n = noLoc (HsVar n)
218 nlHsLit :: HsLit -> LHsExpr id
219 nlHsLit n = noLoc (HsLit n)
221 nlVarPat :: id -> LPat id
222 nlVarPat n = noLoc (VarPat n)
224 nlLitPat :: HsLit -> LPat id
225 nlLitPat l = noLoc (LitPat l)
227 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
228 nlHsApp f x = noLoc (HsApp f x)
230 nlHsIntLit :: Integer -> LHsExpr id
231 nlHsIntLit n = noLoc (HsLit (HsInt n))
233 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
234 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
236 nlHsVarApps :: id -> [id] -> LHsExpr id
237 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
239 mk f a = HsApp (noLoc f) (noLoc a)
241 nlConVarPat :: id -> [id] -> LPat id
242 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
244 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
245 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
247 nlConPat :: id -> [LPat id] -> LPat id
248 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
250 nlNullaryConPat :: id -> LPat id
251 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
253 nlWildConPat :: DataCon -> LPat RdrName
254 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
255 (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
258 nlWildPat = noLoc (WildPat placeHolderType) -- Pre-typechecking
260 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
261 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
263 nlHsOpApp :: LHsExpr id -> id -> LHsExpr id -> LHsExpr id
264 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
266 nlHsLam :: LMatch id -> LHsExpr id
267 nlHsPar :: LHsExpr id -> LHsExpr id
268 nlHsIf :: LHsExpr id -> LHsExpr id -> LHsExpr id -> LHsExpr id
269 nlHsCase :: LHsExpr id -> [LMatch id] -> LHsExpr id
270 nlList :: [LHsExpr id] -> LHsExpr id
272 nlHsLam match = noLoc (HsLam (mkMatchGroup [match]))
273 nlHsPar e = noLoc (HsPar e)
274 nlHsIf cond true false = noLoc (HsIf cond true false)
275 nlHsCase expr matches = noLoc (HsCase expr (mkMatchGroup matches))
276 nlList exprs = noLoc (ExplicitList placeHolderType exprs)
278 nlHsAppTy :: LHsType name -> LHsType name -> LHsType name
279 nlHsTyVar :: name -> LHsType name
280 nlHsFunTy :: LHsType name -> LHsType name -> LHsType name
282 nlHsAppTy f t = noLoc (HsAppTy f t)
283 nlHsTyVar x = noLoc (HsTyVar x)
284 nlHsFunTy a b = noLoc (HsFunTy a b)
286 nlHsTyConApp :: name -> [LHsType name] -> LHsType name
287 nlHsTyConApp tycon tys = foldl nlHsAppTy (nlHsTyVar tycon) tys
290 Tuples. All these functions are *pre-typechecker* because they lack
294 mkLHsTupleExpr :: [LHsExpr a] -> LHsExpr a
295 -- Makes a pre-typechecker boxed tuple, deals with 1 case
296 mkLHsTupleExpr [e] = e
297 mkLHsTupleExpr es = noLoc $ ExplicitTuple (map Present es) Boxed
299 mkLHsVarTuple :: [a] -> LHsExpr a
300 mkLHsVarTuple ids = mkLHsTupleExpr (map nlHsVar ids)
302 nlTuplePat :: [LPat id] -> Boxity -> LPat id
303 nlTuplePat pats box = noLoc (TuplePat pats box placeHolderType)
305 missingTupArg :: HsTupArg a
306 missingTupArg = Missing placeHolderType
309 %************************************************************************
311 Bindings; with a location at the top
313 %************************************************************************
316 mkFunBind :: Located id -> [LMatch id] -> HsBind id
317 -- Not infix, with place holders for coercion and free vars
318 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
319 fun_co_fn = idHsWrapper, bind_fvs = placeHolderNames,
323 mkHsVarBind :: SrcSpan -> id -> LHsExpr id -> LHsBind id
324 mkHsVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
326 mkVarBind :: id -> LHsExpr id -> LHsBind id
327 mkVarBind var rhs = L (getLoc rhs) $
328 VarBind { var_id = var, var_rhs = rhs, var_inline = False }
331 mk_easy_FunBind :: SrcSpan -> id -> [LPat id]
332 -> LHsExpr id -> LHsBind id
334 mk_easy_FunBind loc fun pats expr
335 = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
338 mk_FunBind :: SrcSpan -> id
339 -> [([LPat id], LHsExpr id)]
342 mk_FunBind _ _ [] = panic "TcGenDeriv:mk_FunBind"
343 mk_FunBind loc fun pats_and_exprs
344 = L loc $ mkFunBind (L loc fun) matches
346 matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
349 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
350 mkMatch pats expr binds
351 = noLoc (Match (map paren pats) Nothing
352 (GRHSs (unguardedRHS expr) binds))
354 paren lp@(L l p) | hsPatNeedsParens p = L l (ParPat lp)
359 %************************************************************************
361 Collecting binders from HsBindGroups and HsBinds
363 %************************************************************************
365 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
373 it should return [x, y, f, a, b] (remember, order important).
376 collectLocalBinders :: HsLocalBindsLR idL idR -> [Located idL]
377 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
378 collectLocalBinders (HsIPBinds _) = []
379 collectLocalBinders EmptyLocalBinds = []
381 collectHsValBinders :: HsValBindsLR idL idR -> [Located idL]
382 collectHsValBinders (ValBindsIn binds _) = collectHsBindLocatedBinders binds
383 collectHsValBinders (ValBindsOut binds _) = foldr collect_one [] binds
385 collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
387 collectAcc :: HsBindLR idL idR -> [Located idL] -> [Located idL]
388 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
389 collectAcc (FunBind { fun_id = f }) acc = f : acc
390 collectAcc (VarBind { var_id = f }) acc = noLoc f : acc
391 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = _binds }) acc
392 = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
393 -- ++ foldr collectAcc acc binds
394 -- I don't think we want the binders from the nested binds
395 -- The only time we collect binders from a typechecked
396 -- binding (hence see AbsBinds) is in zonking in TcHsSyn
398 collectHsBindBinders :: LHsBindsLR idL idR -> [idL]
399 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
401 collectHsBindLocatedBinders :: LHsBindsLR idL idR -> [Located idL]
402 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
406 %************************************************************************
408 Getting binders from statements
410 %************************************************************************
413 collectLStmtsBinders :: [LStmtLR idL idR] -> [Located idL]
414 collectLStmtsBinders = concatMap collectLStmtBinders
416 collectStmtsBinders :: [StmtLR idL idR] -> [Located idL]
417 collectStmtsBinders = concatMap collectStmtBinders
419 collectLStmtBinders :: LStmtLR idL idR -> [Located idL]
420 collectLStmtBinders = collectStmtBinders . unLoc
422 collectStmtBinders :: StmtLR idL idR -> [Located idL]
423 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
424 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
425 collectStmtBinders (LetStmt binds) = collectLocalBinders binds
426 collectStmtBinders (ExprStmt _ _ _) = []
427 collectStmtBinders (ParStmt xs) = collectLStmtsBinders
429 collectStmtBinders (TransformStmt (stmts, _) _ _) = collectLStmtsBinders stmts
430 collectStmtBinders (GroupStmt (stmts, _) _) = collectLStmtsBinders stmts
431 collectStmtBinders (RecStmt { recS_stmts = ss }) = collectLStmtsBinders ss
435 %************************************************************************
437 %* Gathering stuff out of patterns
439 %************************************************************************
441 This function @collectPatBinders@ works with the ``collectBinders''
442 functions for @HsBinds@, etc. The order in which the binders are
443 collected is important; see @HsBinds.lhs@.
445 It collects the bounds *value* variables in renamed patterns; type variables
449 collectPatBinders :: LPat a -> [a]
450 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
452 collectLocatedPatBinders :: LPat a -> [Located a]
453 collectLocatedPatBinders pat = collectl pat []
455 collectPatsBinders :: [LPat a] -> [a]
456 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
458 collectLocatedPatsBinders :: [LPat a] -> [Located a]
459 collectLocatedPatsBinders pats = foldr collectl [] pats
461 ---------------------
462 collectl :: LPat name -> [Located name] -> [Located name]
463 collectl (L l pat) bndrs
466 go (VarPat var) = L l var : bndrs
467 go (VarPatOut var bs) = L l var : collectHsBindLocatedBinders bs
469 go (WildPat _) = bndrs
470 go (LazyPat pat) = collectl pat bndrs
471 go (BangPat pat) = collectl pat bndrs
472 go (AsPat a pat) = a : collectl pat bndrs
473 go (ViewPat _ pat _) = collectl pat bndrs
474 go (ParPat pat) = collectl pat bndrs
476 go (ListPat pats _) = foldr collectl bndrs pats
477 go (PArrPat pats _) = foldr collectl bndrs pats
478 go (TuplePat pats _ _) = foldr collectl bndrs pats
480 go (ConPatIn _ ps) = foldr collectl bndrs (hsConPatArgs ps)
481 go (ConPatOut {pat_args=ps}) = foldr collectl bndrs (hsConPatArgs ps)
482 -- See Note [Dictionary binders in ConPatOut]
483 go (LitPat _) = bndrs
484 go (NPat _ _ _) = bndrs
485 go (NPlusKPat n _ _ _) = n : bndrs
487 go (SigPatIn pat _) = collectl pat bndrs
488 go (SigPatOut pat _) = collectl pat bndrs
489 go (QuasiQuotePat _) = bndrs
490 go (TypePat _) = bndrs
491 go (CoPat _ pat _) = collectl (noLoc pat) bndrs
494 Note [Dictionary binders in ConPatOut]
495 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
496 Do *not* gather (a) dictionary and (b) dictionary bindings as binders
497 of a ConPatOut pattern. For most calls it doesn't matter, because
498 it's pre-typechecker and there are no ConPatOuts. But it does matter
499 more in the desugarer; for example, DsUtils.mkSelectorBinds uses
500 collectPatBinders. In a lazy pattern, for example f ~(C x y) = ...,
501 we want to generate bindings for x,y but not for dictionaries bound by
502 C. (The type checker ensures they would not be used.)
504 Desugaring of arrow case expressions needs these bindings (see DsArrows
505 and arrowcase1), but SPJ (Jan 2007) says it's safer for it to use its
506 own pat-binder-collector:
508 Here's the problem. Consider
511 C :: Num a => a -> Int -> T a
513 f ~(C (n+1) m) = (n,m)
515 Here, the pattern (C (n+1)) binds a hidden dictionary (d::Num a),
516 and *also* uses that dictionary to match the (n+1) pattern. Yet, the
517 variables bound by the lazy pattern are n,m, *not* the dictionary d.
518 So in mkSelectorBinds in DsUtils, we want just m,n as the variables bound.
521 collectSigTysFromPats :: [InPat name] -> [LHsType name]
522 collectSigTysFromPats pats = foldr collect_lpat [] pats
524 collectSigTysFromPat :: InPat name -> [LHsType name]
525 collectSigTysFromPat pat = collect_lpat pat []
527 collect_lpat :: InPat name -> [LHsType name] -> [LHsType name]
528 collect_lpat pat acc = collect_pat (unLoc pat) acc
530 collect_pat :: Pat name -> [LHsType name] -> [LHsType name]
531 collect_pat (SigPatIn pat ty) acc = collect_lpat pat (ty:acc)
532 collect_pat (TypePat ty) acc = ty:acc
534 collect_pat (LazyPat pat) acc = collect_lpat pat acc
535 collect_pat (BangPat pat) acc = collect_lpat pat acc
536 collect_pat (AsPat _ pat) acc = collect_lpat pat acc
537 collect_pat (ParPat pat) acc = collect_lpat pat acc
538 collect_pat (ListPat pats _) acc = foldr collect_lpat acc pats
539 collect_pat (PArrPat pats _) acc = foldr collect_lpat acc pats
540 collect_pat (TuplePat pats _ _) acc = foldr collect_lpat acc pats
541 collect_pat (ConPatIn _ ps) acc = foldr collect_lpat acc (hsConPatArgs ps)
542 collect_pat _ acc = acc -- Literals, vars, wildcard