|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import Contexts._, Symbols._, Types._, Flags._, Decorators._, StdNames._, Constants._, Phases._ |
| 6 | +import TreeTransforms._ |
| 7 | +import ast.Trees._ |
| 8 | +import collection.mutable |
| 9 | + |
| 10 | +object NonLocalReturns { |
| 11 | + import ast.tpd._ |
| 12 | + def isNonLocalReturn(ret: Return)(implicit ctx: Context) = |
| 13 | + ret.from.symbol != ctx.owner.enclosingMethod || ctx.owner.is(Lazy) |
| 14 | +} |
| 15 | + |
| 16 | +/** Implement non-local returns using NonLocalReturnControl exceptions. |
| 17 | + */ |
| 18 | +class NonLocalReturns extends MiniPhaseTransform { thisTransformer => |
| 19 | + override def phaseName = "nonLocalReturns" |
| 20 | + |
| 21 | + import NonLocalReturns._ |
| 22 | + import ast.tpd._ |
| 23 | + |
| 24 | + override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[ElimByName]) |
| 25 | + |
| 26 | + private def ensureConforms(tree: Tree, pt: Type)(implicit ctx: Context) = |
| 27 | + if (tree.tpe <:< pt) tree |
| 28 | + else Erasure.Boxing.adaptToType(tree, pt) |
| 29 | + |
| 30 | + /** The type of a non-local return expression with given argument type */ |
| 31 | + private def nonLocalReturnExceptionType(argtype: Type)(implicit ctx: Context) = |
| 32 | + defn.NonLocalReturnControlClass.typeRef.appliedTo(argtype) |
| 33 | + |
| 34 | + /** A hashmap from method symbols to non-local return keys */ |
| 35 | + private val nonLocalReturnKeys = mutable.Map[Symbol, TermSymbol]() |
| 36 | + |
| 37 | + /** Return non-local return key for given method */ |
| 38 | + private def nonLocalReturnKey(meth: Symbol)(implicit ctx: Context) = |
| 39 | + nonLocalReturnKeys.getOrElseUpdate(meth, |
| 40 | + ctx.newSymbol( |
| 41 | + meth, ctx.freshName("nonLocalReturnKey").toTermName, Synthetic, defn.ObjectType, coord = meth.pos)) |
| 42 | + |
| 43 | + /** Generate a non-local return throw with given return expression from given method. |
| 44 | + * I.e. for the method's non-local return key, generate: |
| 45 | + * |
| 46 | + * throw new NonLocalReturnControl(key, expr) |
| 47 | + * todo: maybe clone a pre-existing exception instead? |
| 48 | + * (but what to do about exceptions that miss their targets?) |
| 49 | + */ |
| 50 | + private def nonLocalReturnThrow(expr: Tree, meth: Symbol)(implicit ctx: Context) = |
| 51 | + Throw( |
| 52 | + New( |
| 53 | + defn.NonLocalReturnControlClass.typeRef, |
| 54 | + ref(nonLocalReturnKey(meth)) :: expr.ensureConforms(defn.ObjectType) :: Nil)) |
| 55 | + |
| 56 | + /** Transform (body, key) to: |
| 57 | + * |
| 58 | + * { |
| 59 | + * val key = new Object() |
| 60 | + * try { |
| 61 | + * body |
| 62 | + * } catch { |
| 63 | + * case ex: NonLocalReturnControl => |
| 64 | + * if (ex.key().eq(key)) ex.value().asInstanceOf[T] |
| 65 | + * else throw ex |
| 66 | + * } |
| 67 | + * } |
| 68 | + */ |
| 69 | + private def nonLocalReturnTry(body: Tree, key: TermSymbol, meth: Symbol)(implicit ctx: Context) = { |
| 70 | + val keyDef = ValDef(key, New(defn.ObjectType, Nil)) |
| 71 | + val nonLocalReturnControl = defn.NonLocalReturnControlClass.typeRef |
| 72 | + val ex = ctx.newSymbol(meth, nme.ex, EmptyFlags, nonLocalReturnControl, coord = body.pos) |
| 73 | + val pat = BindTyped(ex, nonLocalReturnControl) |
| 74 | + val rhs = If( |
| 75 | + ref(ex).select(nme.key).appliedToNone.select(nme.eq).appliedTo(ref(key)), |
| 76 | + ref(ex).select(nme.value).ensureConforms(meth.info.finalResultType), |
| 77 | + Throw(ref(ex))) |
| 78 | + val catches = CaseDef(pat, EmptyTree, rhs) :: Nil |
| 79 | + val tryCatch = Try(body, catches, EmptyTree) |
| 80 | + Block(keyDef :: Nil, tryCatch) |
| 81 | + } |
| 82 | + |
| 83 | + override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree = |
| 84 | + nonLocalReturnKeys.remove(tree.symbol) match { |
| 85 | + case Some(key) => cpy.DefDef(tree)(rhs = nonLocalReturnTry(tree.rhs, key, tree.symbol)) |
| 86 | + case _ => tree |
| 87 | + } |
| 88 | + |
| 89 | + override def transformReturn(tree: Return)(implicit ctx: Context, info: TransformerInfo): Tree = |
| 90 | + if (isNonLocalReturn(tree)) nonLocalReturnThrow(tree.expr, tree.from.symbol).withPos(tree.pos) |
| 91 | + else tree |
| 92 | +} |
0 commit comments