diff options
author | Szabolcs Berecz <szabolcs.berecz@gmail.com> | 2012-01-29 01:24:28 +0100 |
---|---|---|
committer | Szabolcs Berecz <szabolcs.berecz@gmail.com> | 2012-02-16 21:36:58 +0100 |
commit | 01ee3de96dcae707fb8fee28e93af0515519c603 (patch) | |
tree | e4dcd0e7ba8ab1036abd92dbfaecc2cb9fa14175 /src/compiler/scala/tools/nsc/transform/TailCalls.scala | |
parent | b8b19b1ca1de01083cf0975226a34424bd729a5b (diff) | |
download | scala-01ee3de96dcae707fb8fee28e93af0515519c603.tar.gz scala-01ee3de96dcae707fb8fee28e93af0515519c603.tar.bz2 scala-01ee3de96dcae707fb8fee28e93af0515519c603.zip |
replace methods containing a synchronized body with synchronized methods
The following:
def f = synchronized { ... }
will be rewritten to:
<synchronized> def f = ...
which is then emitted to the classfile with the synchronized flag set.
Inlining of such transformed methods are disabled for now
This transformation is not done on methods defined in a trait.
Diffstat (limited to 'src/compiler/scala/tools/nsc/transform/TailCalls.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/TailCalls.scala | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/TailCalls.scala b/src/compiler/scala/tools/nsc/transform/TailCalls.scala index 1655ad09c4..97b9caaa73 100644 --- a/src/compiler/scala/tools/nsc/transform/TailCalls.scala +++ b/src/compiler/scala/tools/nsc/transform/TailCalls.scala @@ -173,6 +173,29 @@ abstract class TailCalls extends Transform { trees map (t => transform(t, nctx)) } + /** + * Transforms methods with synchronized body into synchronized methods + */ + private def transformSynchronizedMethods(tree: Tree): Tree = { + def isSelfSynchronized(body: Apply) = body.fun match { + case TypeApply(fun @ Select(This(_), _), List(TypeTree())) + if (fun.symbol == Object_synchronized && + fun.qualifier.symbol == tree.symbol.enclClass && + !tree.symbol.enclClass.isTrait) => true + case _ => false + } + + tree match { + case DefDef(mods, name, tparams, vparamss, tpt, rhs @ Apply(_, List(body))) + if (isSelfSynchronized(rhs)) => + val res = treeCopy.DefDef(tree, mods, name, tparams, vparamss, tpt, body) + res.symbol withAnnotation AnnotationInfo(SynchronizedAttr.tpe, Nil, Nil) + res + case _ => + tree + } + } + override def transform(tree: Tree): Tree = { /** A possibly polymorphic apply to be considered for tail call transformation. */ @@ -222,7 +245,7 @@ abstract class TailCalls extends Transform { else rewriteTailCall(receiver) } - tree match { + transformSynchronizedMethods(tree) match { case dd @ DefDef(mods, name, tparams, vparams, tpt, rhs) => val newCtx = new Context(dd) |