X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FDsExpr.lhs;h=f447d9d52e6048420e8eefc43d1f1dc00b20bec8;hb=1f5e55804b97d2b9a77207d568d602ba88d8855d;hp=0cf2b9712d78728c0fc2ccb6de85cd1835d084b2;hpb=dbc254c3dcd64761015a3d1c191ac742caafbf4c;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/DsExpr.lhs b/ghc/compiler/deSugar/DsExpr.lhs index 0cf2b97..f447d9d 100644 --- a/ghc/compiler/deSugar/DsExpr.lhs +++ b/ghc/compiler/deSugar/DsExpr.lhs @@ -13,9 +13,11 @@ import Match ( matchWrapper, matchSimply ) import MatchLit ( dsLit ) 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, + mkCoreTupTy, selectMatchVar, + dsReboundNames, lookupReboundName ) +import DsArrows ( dsProcExpr ) import DsMonad #ifdef GHCI @@ -23,10 +25,10 @@ import DsMonad import DsMeta ( dsBracket ) #endif -import HsSyn ( failureFreePat, - HsExpr(..), Pat(..), HsLit(..), ArithSeqInfo(..), +import HsSyn ( HsExpr(..), Pat(..), ArithSeqInfo(..), Stmt(..), HsMatchContext(..), HsStmtContext(..), Match(..), HsBinds(..), MonoBinds(..), HsConDetails(..), + ReboundNames, mkSimpleMatch, isDoExpr ) import TcHsSyn ( TypecheckedHsExpr, TypecheckedHsBinds, TypecheckedStmt, hsPatType ) @@ -45,14 +47,17 @@ 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, mkTupleTy ) +import TysWiredIn ( tupleCon ) import BasicTypes ( RecFlag(..), Boxity(..), ipNameName ) -import PrelNames ( toPName ) +import PrelNames ( toPName, + returnMName, bindMName, thenMName, failMName, + mfixName ) import SrcLoc ( noSrcLoc ) import Util ( zipEqual, zipWithEqual ) import Outputable @@ -87,6 +92,13 @@ dsLet (ThenBinds b1 b2) body = dsLet b2 body `thenDs` \ body' -> dsLet b1 body' +dsLet (IPBinds binds) 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. @@ -102,8 +114,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) @@ -223,16 +235,18 @@ dsExpr (SectionR op expr) returnDs (bindNonRec y_id y_core $ Lam x_id (mkApps core_op [Var x_id, Var y_id])) -dsExpr (HsCCall lbl args may_gc is_asm result_ty) - = mapDs dsExpr args `thenDs` \ core_args -> - dsCCall lbl core_args may_gc is_asm result_ty - -- dsCCall does all the unboxification, etc. - dsExpr (HsSCC cc expr) = dsExpr expr `thenDs` \ core_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) @@ -258,14 +272,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. -- @@ -339,7 +345,7 @@ dsExpr (ExplicitPArr ty xs) returnDs (mkApps (Var toP) [Type ty, coreList]) dsExpr (ExplicitTuple expr_list boxity) - = mapDs dsExpr expr_list `thenDs` \ core_exprs -> + = mappM dsExpr expr_list `thenDs` \ core_exprs -> returnDs (mkConApp (tupleCon boxity (length expr_list)) (map (Type . exprType) core_exprs ++ core_exprs)) @@ -427,8 +433,8 @@ dsExpr (RecordConOut data_con con_expr rbinds) in (if null labels - then mapDs unlabelled_bottom arg_tys - else mapDs mk_arg (zipEqual "dsExpr:RecordCon" arg_tys labels)) + then mappM unlabelled_bottom arg_tys + else mappM mk_arg (zipEqual "dsExpr:RecordCon" arg_tys labels)) `thenDs` \ con_args -> returnDs (mkApps con_expr' con_args) @@ -499,7 +505,7 @@ dsExpr expr@(RecordUpdOut record_expr record_in_ty record_out_ty rbinds) -- and the right hand sides with applications of the wrapper Id -- so that everything works when we are doing fancy unboxing on the -- constructor aguments. - mapDs mk_alt cons_to_upd `thenDs` \ alts -> + mappM mk_alt cons_to_upd `thenDs` \ alts -> matchWrapper RecUpd alts `thenDs` \ ([discrim_var], matching_code) -> returnDs (bindNonRec discrim_var record_expr' matching_code) @@ -549,9 +555,11 @@ 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 (HsSplice n e _) = pprPanic "dsExpr:splice" (ppr e) #endif +-- Arrow notation extension +dsExpr (HsProc pat cmd src_loc) = dsProcExpr pat cmd src_loc \end{code} @@ -571,15 +579,20 @@ dsExpr (PArrSeqIn _) = panic "dsExpr:PArrSeqIn" Basically does the translation given in the Haskell~1.3 report: \begin{code} -dsDo :: HsStmtContext +dsDo :: HsStmtContext Name -> [TypecheckedStmt] - -> [Id] -- id for: [return,fail,>>=,>>] and possibly mfixName - -> Type -- Element type; the whole expression has type (m t) + -> ReboundNames 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 result_ty - = let - (return_id : fail_id : bind_id : then_id : _) = ids + = dsReboundNames ids `thenDs` \ (meth_binds, ds_meths) -> + let + return_id = lookupReboundName ds_meths returnMName + fail_id = lookupReboundName ds_meths failMName + bind_id = lookupReboundName ds_meths bindMName + then_id = lookupReboundName ds_meths thenMName + (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 @@ -591,13 +604,13 @@ dsDo do_or_lc stmts ids result_ty go [ResultStmt expr locn] | is_do = do_expr expr locn | otherwise = do_expr expr locn `thenDs` \ expr2 -> - returnDs (mkApps (Var return_id) [Type b_ty, expr2]) + returnDs (mkApps return_id [Type b_ty, expr2]) go (ExprStmt expr a_ty locn : stmts) | is_do -- Do expression = do_expr expr locn `thenDs` \ expr2 -> go stmts `thenDs` \ rest -> - returnDs (mkApps (Var then_id) [Type a_ty, Type b_ty, expr2, rest]) + returnDs (mkApps then_id [Type a_ty, Type b_ty, expr2, rest]) | otherwise -- List comprehension = do_expr expr locn `thenDs` \ expr2 -> @@ -607,44 +620,40 @@ dsDo do_or_lc stmts ids result_ty in mkStringLit msg `thenDs` \ core_msg -> returnDs (mkIfThenElse expr2 rest - (App (App (Var fail_id) (Type b_ty)) core_msg)) + (App (App fail_id (Type b_ty)) core_msg)) - go (LetStmt binds : stmts ) + go (LetStmt binds : stmts) = go stmts `thenDs` \ rest -> 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 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 (StmtCtxt 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 bind_id [Type a_ty, Type b_ty, rhs, Lam var match_code]) - go (RecStmt rec_vars rec_stmts : stmts) + go (RecStmt rec_stmts later_vars rec_vars rec_rets : stmts) = go (bind_stmt : stmts) where - bind_stmt = dsRecStmt m_ty ids rec_vars rec_stmts + bind_stmt = dsRecStmt m_ty ds_meths rec_stmts later_vars rec_vars rec_rets in - go stmts + go stmts `thenDs` \ stmts_code -> + returnDs (foldr Let stmts_code meth_binds) 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: @@ -656,30 +665,36 @@ We turn (RecStmt [v1,..vn] stmts) into: \begin{code} dsRecStmt :: Type -- Monad type constructor :: * -> * - -> [Id] -- Ids for: [return,fail,>>=,>>,mfix] - -> [Id] -> [TypecheckedStmt] -- Guts of the RecStmt + -> [(Name,Id)] -- Rebound Ids + -> [TypecheckedStmt] + -> [Id] -> [Id] -> [TypecheckedHsExpr] -> TypecheckedStmt -dsRecStmt m_ty ids@[return_id, _, _, _, mfix_id] vars stmts - = BindStmt tup_pat mfix_app noSrcLoc +dsRecStmt m_ty ds_meths stmts later_vars rec_vars rec_rets + = ASSERT( length vars == length rets ) + BindStmt tup_pat mfix_app noSrcLoc where - (var1:rest) = vars -- Always at least one - one_var = null rest + vars@(var1:rest) = later_vars ++ rec_vars -- Always at least one + rets@(ret1:_) = map HsVar later_vars ++ rec_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 = HsVar var1 - | otherwise = ExplicitTuple (map HsVar vars) Boxed - tup_ty | one_var = idType var1 - | otherwise = mkTupleTy Boxed (length vars) (map idType vars) + tup_expr | one_var = ret1 + | otherwise = ExplicitTuple rets Boxed + tup_ty = mkCoreTupTy (map idType vars) + -- Deals with singleton case 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 + [(n, HsVar id) | (n,id) <- ds_meths] -- A bit of a hack (mkAppTy m_ty tup_ty) noSrcLoc + Var return_id = lookupReboundName ds_meths returnMName + Var mfix_id = lookupReboundName ds_meths mfixName + return_stmt = ResultStmt return_app noSrcLoc return_app = HsApp (TyApp (HsVar return_id) [tup_ty]) tup_expr \end{code}