From 166c496d5784d0be3611b270d9eba39f1273e048 Mon Sep 17 00:00:00 2001 From: Tiark Rompf Date: Tue, 16 Mar 2010 08:19:59 +0000 Subject: added support for continuations in try/catch bl... added support for continuations in try/catch blocks. review by community. --- .../tools/selectivecps/CPSAnnotationChecker.scala | 15 ++++++++- .../tools/selectivecps/SelectiveANFTransform.scala | 11 +++---- .../tools/selectivecps/SelectiveCPSTransform.scala | 37 +++++++++++++++++++++- 3 files changed, 55 insertions(+), 8 deletions(-) (limited to 'src/continuations/plugin') diff --git a/src/continuations/plugin/scala/tools/selectivecps/CPSAnnotationChecker.scala b/src/continuations/plugin/scala/tools/selectivecps/CPSAnnotationChecker.scala index dc7c297b92..14135a24ae 100644 --- a/src/continuations/plugin/scala/tools/selectivecps/CPSAnnotationChecker.scala +++ b/src/continuations/plugin/scala/tools/selectivecps/CPSAnnotationChecker.scala @@ -422,7 +422,20 @@ abstract class CPSAnnotationChecker extends CPSUtils { case Match(select, cases) => - transChildrenInOrder(tree, tpe, List(select), cases:::cases map { case CaseDef(_, _, body) => body }) + // TODO: can there be cases that are not CaseDefs?? check partialMap vs map! + transChildrenInOrder(tree, tpe, List(select), cases:::(cases partialMap { case CaseDef(_, _, body) => body })) + + case Try(block, catches, finalizer) => + val tpe1 = transChildrenInOrder(tree, tpe, Nil, block::catches:::(catches partialMap { case CaseDef(_, _, body) => body })) + + val annots = filterAttribs(tpe1, MarkerCPSTypes) + if (annots.nonEmpty) { + val ann = single(annots) + val atp0::atp1::Nil = ann.atp.normalize.typeArgs + if (!(atp0 =:= atp1)) + throw new TypeError("only simple cps types allowed in try/catch blocks (found: " + tpe1 + ")") + } + tpe1 case Block(stms, expr) => // if any stm has annotation, so does block diff --git a/src/continuations/plugin/scala/tools/selectivecps/SelectiveANFTransform.scala b/src/continuations/plugin/scala/tools/selectivecps/SelectiveANFTransform.scala index 2b6ce67ef4..a09772d236 100644 --- a/src/continuations/plugin/scala/tools/selectivecps/SelectiveANFTransform.scala +++ b/src/continuations/plugin/scala/tools/selectivecps/SelectiveANFTransform.scala @@ -206,17 +206,16 @@ abstract class SelectiveANFTransform extends PluginComponent with Transform with case Try(block, catches, finalizer) => - // no cps code allowed in try/catch/finally! - val blockVal = transExpr(block, None, None) + val blockVal = transExpr(block, cpsA, cpsR) val catchVals = for { cd @ CaseDef(pat, guard, body) <- catches - val bodyVal = transExpr(body, None, None) + val bodyVal = transExpr(body, cpsA, cpsR) } yield { treeCopy.CaseDef(cd, transform(pat), transform(guard), bodyVal) } - val finallyVal = transExpr(finalizer, None, None) + val finallyVal = transExpr(finalizer, None, None) // for now, no cps in finally (Nil, updateSynthFlag(treeCopy.Try(tree, blockVal, catchVals, finallyVal)), cpsA) @@ -297,7 +296,7 @@ abstract class SelectiveANFTransform extends PluginComponent with Transform with } else if (!cpsR.isDefined && bot.isDefined) { // error! log("cps type error: " + expr) - println("cps type error: " + expr + "/" + expr.tpe + "/" + getAnswerTypeAnn(expr.tpe)) + //println("cps type error: " + expr + "/" + expr.tpe + "/" + getAnswerTypeAnn(expr.tpe)) println(cpsR + "/" + spc + "/" + bot) @@ -306,7 +305,7 @@ abstract class SelectiveANFTransform extends PluginComponent with Transform with // all is well if (expr.tpe.hasAnnotation(MarkerCPSAdaptPlus)) { - unit.warning(tree.pos, "expression " + expr + "/" + expr.tpe + " should not have cps-plus annotation") + unit.warning(tree.pos, "expression " + expr + " of type " + expr.tpe + " is not expected to have a cps type") expr.setType(removeAllCPSAnnotations(expr.tpe)) } diff --git a/src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSTransform.scala b/src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSTransform.scala index b906c12568..7f1610e8cc 100644 --- a/src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSTransform.scala +++ b/src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSTransform.scala @@ -116,7 +116,42 @@ abstract class SelectiveCPSTransform extends PluginComponent with ).setType(transformCPSType(tree.tpe)) } - case Block(stms, expr) => + case Try(block, catches, finalizer) => + // currently duplicates the catch block into a partial function + // this is kinda risky, but we don't expect there will be lots + // of try/catches inside catch blocks (exp. blowup) + + val block1 = transform(block) + val catches1 = transformCaseDefs(catches) + val finalizer1 = transform(finalizer) + + if (block1.tpe.typeSymbol.tpe <:< Context.tpe) { + //println("CPS Transform: " + tree) + + val pos = catches.head.pos + + val (stms, expr) = block1 match { + case Block(stms, expr) => (stms, expr) + case expr => (Nil, expr) + } + + val arg = currentOwner.newValueParameter(pos, "$ex").setInfo(ThrowableClass.tpe) + val catches2 = catches1 map (duplicateTree(_).asInstanceOf[CaseDef]) + val rhs = Match(Ident(arg), catches2) + val fun = Function(List(ValDef(arg)), rhs) + val expr2 = localTyper.typed(atPos(pos) { Apply(Select(expr, expr.tpe.member("flatCat")), List(fun)) }) + val block2 = treeCopy.Block(block1, stms, expr2) + + arg.owner = fun.symbol + val chown = new ChangeOwnerTraverser(currentOwner, fun.symbol) + chown.traverse(rhs) + + treeCopy.Try(tree, block2, catches1, finalizer1) + } else { + treeCopy.Try(tree, block1, catches1, finalizer1) + } + + case Block(stms, expr) => val (stms1, expr1) = transBlock(stms, expr) treeCopy.Block(tree, stms1, expr1) -- cgit v1.2.3