X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FDsExpr.lhs;h=3a31d90c817ed32bd7e474498fe9ce4ea3901082;hb=802b299f16593e95deb6cc2bd5d457444ed92fd1;hp=bc8a1f58f0041c3b2cdd5ce8c7f91f3bf10e0f01;hpb=9af77fa423926fbda946b31e174173d0ec5ebac8;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/DsExpr.lhs b/ghc/compiler/deSugar/DsExpr.lhs index bc8a1f5..3a31d90 100644 --- a/ghc/compiler/deSugar/DsExpr.lhs +++ b/ghc/compiler/deSugar/DsExpr.lhs @@ -15,19 +15,18 @@ import DsBinds ( dsMonoBinds, AutoScc(..) ) import DsGRHSs ( dsGuarded ) import DsCCall ( dsCCall ) import DsListComp ( dsListComp, dsPArrComp ) -import DsUtils ( mkErrorAppDs, mkStringLit, mkConsExpr, mkNilExpr) +import DsUtils ( mkErrorAppDs, mkStringLit, mkConsExpr, mkNilExpr, selectMatchVar ) import DsMonad #ifdef GHCI -- Template Haskell stuff iff bootstrapped -import DsMeta ( dsBracket ) +import DsMeta ( dsBracket, dsReify ) #endif -import HsSyn ( failureFreePat, - HsExpr(..), Pat(..), HsLit(..), ArithSeqInfo(..), - Stmt(..), HsMatchContext(..), HsDoContext(..), +import HsSyn ( HsExpr(..), Pat(..), HsLit(..), ArithSeqInfo(..), + Stmt(..), HsMatchContext(..), HsStmtContext(..), Match(..), HsBinds(..), MonoBinds(..), HsConDetails(..), - mkSimpleMatch + mkSimpleMatch, isDoExpr ) import TcHsSyn ( TypecheckedHsExpr, TypecheckedHsBinds, TypecheckedStmt, hsPatType ) @@ -37,21 +36,25 @@ import TcHsSyn ( TypecheckedHsExpr, TypecheckedHsBinds, TypecheckedStmt, hsPatT -- Sigh. This is a pain. import TcType ( tcSplitAppTy, tcSplitFunTys, tcTyConAppArgs, - tcSplitTyConApp, isUnLiftedType, Type ) + tcSplitTyConApp, isUnLiftedType, Type, + mkAppTy ) import Type ( splitFunTys ) import CoreSyn +import Literal ( Literal(..) ) import CoreUtils ( exprType, mkIfThenElse, bindNonRec ) import FieldLabel ( FieldLabel, fieldLabelTyCon ) import CostCentre ( mkUserCC ) -import Id ( Id, idType, recordSelectorFieldLabel ) +import Id ( Id, idType, idName, recordSelectorFieldLabel ) import PrelInfo ( rEC_CON_ERROR_ID, iRREFUT_PAT_ERROR_ID ) import DataCon ( DataCon, dataConWrapId, dataConFieldLabels, dataConInstOrigArgTys ) import DataCon ( isExistentialDataCon ) +import Name ( Name ) import TyCon ( tyConDataCons ) -import TysWiredIn ( tupleCon ) +import TysWiredIn ( tupleCon, mkTupleTy ) import BasicTypes ( RecFlag(..), Boxity(..), ipNameName ) import PrelNames ( toPName ) +import SrcLoc ( noSrcLoc ) import Util ( zipEqual, zipWithEqual ) import Outputable import FastString @@ -85,6 +88,13 @@ dsLet (ThenBinds b1 b2) body = dsLet b2 body `thenDs` \ body' -> dsLet b1 body' +dsLet (IPBinds binds is_with) body + = foldlDs dsIPBind body binds + where + dsIPBind body (n, e) + = dsExpr e `thenDs` \ e' -> + returnDs (Let (NonRec (ipNameName n) e') body) + -- Special case for bindings which bind unlifted variables -- We need to do a case right away, rather than building -- a tuple and doing selections. @@ -100,8 +110,8 @@ dsLet bind@(MonoBind (AbsBinds [] [] exports inlines binds) sigs is_rec) body -- below. Then pattern-match would fail. Urk.) case binds of FunMonoBind fun _ matches loc - -> putSrcLocDs loc $ - matchWrapper (FunRhs fun) matches `thenDs` \ (args, rhs) -> + -> putSrcLocDs loc $ + matchWrapper (FunRhs (idName fun)) matches `thenDs` \ (args, rhs) -> ASSERT( null args ) -- Functions aren't lifted returnDs (bindNonRec fun rhs body_w_exports) @@ -231,6 +241,13 @@ dsExpr (HsSCC cc expr) getModuleDs `thenDs` \ mod_name -> returnDs (Note (SCC (mkUserCC cc mod_name)) core_expr) + +-- hdaume: core annotation + +dsExpr (HsCoreAnn fs expr) + = dsExpr expr `thenDs` \ core_expr -> + returnDs (Note (CoreNote $ unpackFS fs) core_expr) + -- special case to handle unboxed tuple patterns. dsExpr (HsCase discrim matches src_loc) @@ -256,14 +273,6 @@ dsExpr (HsLet binds body) = dsExpr body `thenDs` \ body' -> dsLet binds body' -dsExpr (HsWith expr binds is_with) - = dsExpr expr `thenDs` \ expr' -> - foldlDs dsIPBind expr' binds - where - dsIPBind body (n, e) - = dsExpr e `thenDs` \ e' -> - returnDs (Let (NonRec (ipNameName n) e') body) - -- We need the `ListComp' form to use `deListComp' (rather than the "do" form) -- because the interpretation of `stmts' depends on what sort of thing it is. -- @@ -274,9 +283,10 @@ dsExpr (HsDo ListComp stmts _ result_ty src_loc) where (_, [elt_ty]) = tcSplitTyConApp result_ty -dsExpr (HsDo DoExpr stmts ids result_ty src_loc) +dsExpr (HsDo do_or_lc stmts ids result_ty src_loc) + | isDoExpr do_or_lc = putSrcLocDs src_loc $ - dsDo DoExpr stmts ids result_ty + dsDo do_or_lc stmts ids result_ty dsExpr (HsDo PArrComp stmts _ result_ty src_loc) = -- Special case for array comprehensions @@ -546,7 +556,8 @@ Here is where we desugar the Template Haskell brackets and escapes #ifdef GHCI /* Only if bootstrapping */ dsExpr (HsBracketOut x ps) = dsBracket x ps -dsExpr (HsSplice n e) = pprPanic "dsExpr:splice" (ppr e) +dsExpr (HsReify r) = dsReify r +dsExpr (HsSplice n e _) = pprPanic "dsExpr:splice" (ppr e) #endif \end{code} @@ -568,18 +579,17 @@ dsExpr (PArrSeqIn _) = panic "dsExpr:PArrSeqIn" Basically does the translation given in the Haskell~1.3 report: \begin{code} -dsDo :: HsDoContext +dsDo :: HsStmtContext Name -> [TypecheckedStmt] - -> [Id] -- id for: [return,fail,>>=,>>] + -> [Id] -- id for: [return,fail,>>=,>>] and possibly mfixName -> Type -- Element type; the whole expression has type (m t) -> DsM CoreExpr -dsDo do_or_lc stmts ids@[return_id, fail_id, bind_id, then_id] result_ty +dsDo do_or_lc stmts ids result_ty = let - (_, b_ty) = tcSplitAppTy result_ty -- result_ty must be of the form (m b) - is_do = case do_or_lc of - DoExpr -> True - _ -> False + (return_id : fail_id : bind_id : then_id : _) = ids + (m_ty, b_ty) = tcSplitAppTy result_ty -- result_ty must be of the form (m b) + is_do = isDoExpr do_or_lc -- True for both MDo and Do -- For ExprStmt, see the comments near HsExpr.Stmt about -- exactly what ExprStmts mean! @@ -612,29 +622,69 @@ dsDo do_or_lc stmts ids@[return_id, fail_id, bind_id, then_id] result_ty dsLet binds rest go (BindStmt pat expr locn : stmts) - = putSrcLocDs locn $ - dsExpr expr `thenDs` \ expr2 -> + = go stmts `thenDs` \ body -> + putSrcLocDs locn $ -- Rest is associated with this location + dsExpr expr `thenDs` \ rhs -> + mkStringLit (mk_msg locn) `thenDs` \ core_msg -> let + -- In a do expression, pattern-match failure just calls + -- the monadic 'fail' rather than throwing an exception + fail_expr = mkApps (Var fail_id) [Type b_ty, core_msg] a_ty = hsPatType pat - fail_expr = HsApp (TyApp (HsVar fail_id) [b_ty]) - (HsLit (HsString (mkFastString msg))) - msg = "Pattern match failure in do expression, " ++ showSDoc (ppr locn) - main_match = mkSimpleMatch [pat] - (HsDo do_or_lc stmts ids result_ty locn) - result_ty locn - the_matches - | failureFreePat pat = [main_match] - | otherwise = - [ main_match - , mkSimpleMatch [WildPat a_ty] fail_expr result_ty locn - ] in - matchWrapper (DoCtxt do_or_lc) the_matches `thenDs` \ (binders, matching_code) -> - returnDs (mkApps (Var bind_id) [Type a_ty, Type b_ty, expr2, - mkLams binders matching_code]) + selectMatchVar pat `thenDs` \ var -> + matchSimply (Var var) (StmtCtxt do_or_lc) pat + body fail_expr `thenDs` \ match_code -> + returnDs (mkApps (Var bind_id) [Type a_ty, Type b_ty, rhs, Lam var match_code]) + + go (RecStmt rec_vars rec_stmts rec_rets : stmts) + = go (bind_stmt : stmts) + where + bind_stmt = dsRecStmt m_ty ids rec_vars rec_stmts rec_rets + in go stmts where do_expr expr locn = putSrcLocDs locn (dsExpr expr) + mk_msg locn = "Pattern match failure in do expression at " ++ showSDoc (ppr locn) +\end{code} + +Translation for RecStmt's: +----------------------------- +We turn (RecStmt [v1,..vn] stmts) into: + + (v1,..,vn) <- mfix (\~(v1,..vn). do stmts + return (v1,..vn)) + +\begin{code} +dsRecStmt :: Type -- Monad type constructor :: * -> * + -> [Id] -- Ids for: [return,fail,>>=,>>,mfix] + -> [Id] -> [TypecheckedStmt] -> [TypecheckedHsExpr] -- Guts of the RecStmt + -> TypecheckedStmt +dsRecStmt m_ty ids@[return_id, _, _, _, mfix_id] vars stmts rets + = ASSERT( length vars == length rets ) + BindStmt tup_pat mfix_app noSrcLoc + where + (var1:rest) = vars -- Always at least one + (ret1:_) = rets + one_var = null rest + + mfix_app = HsApp (TyApp (HsVar mfix_id) [tup_ty]) mfix_arg + mfix_arg = HsLam (mkSimpleMatch [tup_pat] body tup_ty noSrcLoc) + + tup_expr | one_var = ret1 + | otherwise = ExplicitTuple rets Boxed + tup_ty | one_var = idType var1 + | otherwise = mkTupleTy Boxed (length vars) (map idType vars) + tup_pat | one_var = VarPat var1 + | otherwise = LazyPat (TuplePat (map VarPat vars) Boxed) + + body = HsDo DoExpr (stmts ++ [return_stmt]) + ids -- Don't need the mfix, but it does no harm + (mkAppTy m_ty tup_ty) + noSrcLoc + + return_stmt = ResultStmt return_app noSrcLoc + return_app = HsApp (TyApp (HsVar return_id) [tup_ty]) tup_expr \end{code}