+\begin{code}
+rnStmts :: HsStmtContext Name -> [RdrNameStmt] -> RnM ([RenamedStmt], FreeVars)
+
+rnStmts MDoExpr stmts = rnMDoStmts stmts
+rnStmts ctxt stmts = rnNormalStmts ctxt stmts
+
+rnNormalStmts :: HsStmtContext Name -> [RdrNameStmt] -> RnM ([RenamedStmt], FreeVars)
+-- Used for cases *other* than recursive mdo
+-- Implements nested scopes
+
+rnNormalStmts ctxt [] = returnM ([], emptyFVs)
+ -- Happens at the end of the sub-lists of a ParStmts
+
+rnNormalStmts ctxt (ExprStmt expr _ src_loc : stmts)
+ = addSrcLoc src_loc $
+ rnExpr expr `thenM` \ (expr', fv_expr) ->
+ rnNormalStmts ctxt stmts `thenM` \ (stmts', fvs) ->
+ returnM (ExprStmt expr' placeHolderType src_loc : stmts',
+ fv_expr `plusFV` fvs)
+
+rnNormalStmts ctxt [ResultStmt expr src_loc]
+ = addSrcLoc src_loc $
+ rnExpr expr `thenM` \ (expr', fv_expr) ->
+ returnM ([ResultStmt expr' src_loc], fv_expr)
+
+rnNormalStmts ctxt (BindStmt pat expr src_loc : stmts)
+ = addSrcLoc src_loc $
+ rnExpr expr `thenM` \ (expr', fv_expr) ->
+ -- The binders do not scope over the expression
+
+ let
+ reportUnused =
+ case ctxt of
+ ParStmtCtxt{} -> False
+ _ -> True
+ in
+ rnPatsAndThen (StmtCtxt ctxt) reportUnused [pat] $ \ [pat'] ->
+ rnNormalStmts ctxt stmts `thenM` \ (stmts', fvs) ->
+ returnM (BindStmt pat' expr' src_loc : stmts',
+ fv_expr `plusFV` fvs) -- fv_expr shouldn't really be filtered by
+ -- the rnPatsAndThen, but it does not matter
+
+rnNormalStmts ctxt (LetStmt binds : stmts)
+ = checkErr (ok ctxt binds) (badIpBinds binds) `thenM_`
+ rnBindsAndThen binds ( \ binds' ->
+ rnNormalStmts ctxt stmts `thenM` \ (stmts', fvs) ->
+ returnM (LetStmt binds' : stmts', fvs))
+ where
+ -- We do not allow implicit-parameter bindings in a parallel
+ -- list comprehension. I'm not sure what it might mean.
+ ok (ParStmtCtxt _) (IPBinds _) = False
+ ok _ _ = True
+
+rnNormalStmts ctxt (ParStmt stmtss : stmts)
+ = doptM Opt_GlasgowExts `thenM` \ opt_GlasgowExts ->
+ checkM opt_GlasgowExts parStmtErr `thenM_`
+ mapFvRn rn_branch stmtss `thenM` \ (stmtss', fv_stmtss) ->
+ let
+ bndrss :: [[Name]] -- NB: Name, not RdrName
+ bndrss = map collectStmtsBinders stmtss'
+ (bndrs, dups) = removeDups cmpByOcc (concat bndrss)
+ in
+ mappM dupErr dups `thenM` \ _ ->
+ bindLocalNamesFV bndrs $
+ -- Note: binders are returned in scope order, so one may
+ -- shadow the next; e.g. x <- xs; x <- ys
+ rnNormalStmts ctxt stmts `thenM` \ (stmts', fvs) ->
+
+ -- Cut down the exported binders to just the ones needed in the body
+ let
+ used_bndrs_s = map (filter (`elemNameSet` fvs)) bndrss
+ unused_bndrs = filter (not . (`elemNameSet` fvs)) bndrs
+ in
+ -- With processing of the branches and the tail of comprehension done,
+ -- we can finally compute&report any unused ParStmt binders.
+ warnUnusedMatches unused_bndrs `thenM_`
+ returnM (ParStmt (stmtss' `zip` used_bndrs_s) : stmts',
+ fv_stmtss `plusFV` fvs)
+ where
+ rn_branch (stmts, _) = rnNormalStmts (ParStmtCtxt ctxt) stmts
+
+ cmpByOcc n1 n2 = nameOccName n1 `compare` nameOccName n2
+ dupErr (v:_) = addErr (ptext SLIT("Duplicate binding in parallel list comprehension for:")
+ <+> quotes (ppr v))
+
+rnNormalStmts ctxt (RecStmt rec_stmts _ _ _ : stmts)
+ = bindLocalsRn doc (collectStmtsBinders rec_stmts) $ \ _ ->
+ rn_rec_stmts rec_stmts `thenM` \ segs ->
+ rnNormalStmts ctxt stmts `thenM` \ (stmts', fvs) ->
+ let
+ segs_w_fwd_refs = addFwdRefs segs
+ (ds, us, fs, rec_stmts') = unzip4 segs_w_fwd_refs
+ later_vars = nameSetToList (plusFVs ds `intersectNameSet` fvs)
+ fwd_vars = nameSetToList (plusFVs fs)
+ uses = plusFVs us
+ in
+ returnM (RecStmt rec_stmts' later_vars fwd_vars [] : stmts', uses `plusFV` fvs)
+ where
+ doc = text "In a recursive do statement"
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsubsection{mdo expressions}
+%* *
+%************************************************************************