diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-08-02 11:43:36 -0700 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-08-15 14:34:51 +1000 |
commit | 40f7fce0af1da614d99048b024e1ff579635f0f2 (patch) | |
tree | e0471e7302da2291edb14a43a32bb0073e25aee4 /src | |
parent | 804133f60dd3c78909dc9e557e91b5c9923240ff (diff) | |
download | scala-40f7fce0af1da614d99048b024e1ff579635f0f2.tar.gz scala-40f7fce0af1da614d99048b024e1ff579635f0f2.tar.bz2 scala-40f7fce0af1da614d99048b024e1ff579635f0f2.zip |
SD-192 Change scheme for trait super accessors
Rather than putting the code of a trait method body into a static method,
leave it in the default method. The static method (needed as the target
of the super calls) now uses `invokespecial` to exactly call that method.
Diffstat (limited to 'src')
6 files changed, 29 insertions, 17 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala index 6f9682f434..bac84a4959 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala @@ -657,9 +657,13 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder { } else if (isPrimitive(sym)) { // primitive method call generatedType = genPrimitiveOp(app, expectedType) } else { // normal method call + def isTraitSuperAccessorBodyCall = app.hasAttachment[UseInvokeSpecial.type] val invokeStyle = - if (sym.isStaticMember) InvokeStyle.Static + if (sym.isStaticMember) + InvokeStyle.Static else if (sym.isPrivate || sym.isClassConstructor) InvokeStyle.Special + else if (isTraitSuperAccessorBodyCall) + InvokeStyle.Special else InvokeStyle.Virtual if (invokeStyle.hasInstance) genLoadQualifier(fun) @@ -1077,7 +1081,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder { assert(receiverClass == methodOwner, s"for super call, expecting $receiverClass == $methodOwner") if (receiverClass.isTrait && !receiverClass.isJavaDefined) { val staticDesc = MethodBType(typeToBType(method.owner.info) :: bmType.argumentTypes, bmType.returnType).descriptor - val staticName = traitImplMethodName(method).toString + val staticName = traitSuperAccessorName(method).toString bc.invokestatic(receiverName, staticName, staticDesc, isInterface, pos) } else { if (receiverClass.isTraitOrInterface) { diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala index e1decaba3e..18e7500172 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala @@ -52,7 +52,7 @@ abstract class BCodeHelpers extends BCodeIdiomatic with BytecodeWriters { def needsStaticImplMethod(sym: Symbol) = sym.hasAttachment[global.mixer.NeedStaticImpl.type] - final def traitImplMethodName(sym: Symbol): Name = { + final def traitSuperAccessorName(sym: Symbol): Name = { val name = sym.javaSimpleName if (sym.isMixinConstructor) name else name.append(nme.NAME_JOIN_STRING) diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala index d4d532f4df..481f71cb5d 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala @@ -8,12 +8,12 @@ package scala.tools.nsc package backend package jvm -import scala.collection.{ mutable, immutable } +import scala.collection.{immutable, mutable} import scala.tools.nsc.symtab._ - import scala.tools.asm import GenBCode._ import BackendReporting._ +import scala.tools.nsc.backend.jvm.BCodeHelpers.InvokeStyle /* * @@ -483,18 +483,23 @@ abstract class BCodeSkelBuilder extends BCodeHelpers { case dd : DefDef => val sym = dd.symbol if (needsStaticImplMethod(sym)) { - val staticDefDef = global.gen.mkStatic(dd, traitImplMethodName(sym), _.cloneSymbol) - val forwarderDefDef = { - val forwarderBody = Apply(global.gen.mkAttributedRef(staticDefDef.symbol), This(sym.owner).setType(sym.owner.typeConstructor) :: dd.vparamss.head.map(p => global.gen.mkAttributedIdent(p.symbol))).setType(sym.info.resultType) - // we don't want to the optimizer to inline the static method into the forwarder. Instead, - // the backend has a special case to transitively inline into a callsite of the forwarder - // when the forwarder itself is inlined. - forwarderBody.updateAttachment(NoInlineCallsiteAttachment) - deriveDefDef(dd)(_ => global.atPos(dd.pos)(forwarderBody)) - } - genDefDef(staticDefDef) - if (!sym.isMixinConstructor) + if (sym.isMixinConstructor) { + val statified = global.gen.mkStatic(dd, sym.name, _.cloneSymbol) + genDefDef(statified) + } else { + val forwarderDefDef = { + val dd1 = global.gen.mkStatic(deriveDefDef(dd)(_ => EmptyTree), traitSuperAccessorName(sym), _.cloneSymbol) + dd1.symbol.setFlag(Flags.ARTIFACT).resetFlag(Flags.OVERRIDE) + val selfParam :: realParams = dd1.vparamss.head.map(_.symbol) + deriveDefDef(dd1)(_ => + atPos(dd1.pos)( + Apply(Select(global.gen.mkAttributedIdent(selfParam).setType(sym.owner.typeConstructor), dd.symbol), + realParams.map(global.gen.mkAttributedIdent)).updateAttachment(UseInvokeSpecial)) + ) + } genDefDef(forwarderDefDef) + genDefDef(dd) + } } else genDefDef(dd) case Template(_, _, body) => body foreach gen diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala index 477afaa91b..09e82de89b 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala @@ -596,7 +596,7 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes { annotatedNoInline = methodSym.hasAnnotation(ScalaNoInlineClass)) if (needsStaticImplMethod(methodSym)) { - val staticName = traitImplMethodName(methodSym).toString + val staticName = traitSuperAccessorName(methodSym).toString val selfParam = methodSym.newSyntheticValueParam(methodSym.owner.typeConstructor, nme.SELF) val staticMethodType = methodSym.info match { case mt @ MethodType(params, res) => copyMethodType(mt, selfParam :: params, res) diff --git a/src/reflect/scala/reflect/internal/StdAttachments.scala b/src/reflect/scala/reflect/internal/StdAttachments.scala index 76e34153c9..78f360409d 100644 --- a/src/reflect/scala/reflect/internal/StdAttachments.scala +++ b/src/reflect/scala/reflect/internal/StdAttachments.scala @@ -76,4 +76,6 @@ trait StdAttachments { * in place of the outer parameter, can help callers to avoid capturing the outer instance. */ case object OuterArgCanBeElided extends PlainAttachment + + case object UseInvokeSpecial extends PlainAttachment } diff --git a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala index caef5535b4..f55b33959a 100644 --- a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala +++ b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala @@ -46,6 +46,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse => this.NoInlineCallsiteAttachment this.InlineCallsiteAttachment this.OuterArgCanBeElided + this.UseInvokeSpecial this.noPrint this.typeDebug this.Range |