Skip to content

Commit 5795716

Browse files
committed
Create symExtensions
1 parent 3d7dc45 commit 5795716

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.core.Symbols._
1111
*/
1212
final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
1313
import interface._
14+
import interface.symExtensions
1415

1516
/**
1617
* True if `classSym` is an anonymous class or a local class. I.e., false if `classSym` is a
@@ -24,7 +25,7 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
2425
// always top-level. However, SI-8900 shows an example where the weak name-based implementation
2526
// of isDelambdafyFunction failed (for a function declared in a package named "lambda").
2627
classSym.isAnonymousClass || {
27-
val originalOwnerLexicallyEnclosingClass = symHelper(classSym.originalOwner).originalLexicallyEnclosingClass
28+
val originalOwnerLexicallyEnclosingClass = classSym.originalOwner.originalLexicallyEnclosingClass
2829
originalOwnerLexicallyEnclosingClass != NoSymbol && !originalOwnerLexicallyEnclosingClass.isClass
2930
}
3031
}
@@ -57,9 +58,9 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
5758
def enclosingMethod(sym: Symbol): Option[Symbol] = {
5859
if (sym.isClass || sym == NoSymbol) None
5960
else if (sym.is(Flags.Method)) Some(sym)
60-
else enclosingMethod(symHelper(sym.originalOwner).originalLexicallyEnclosingClass)
61+
else enclosingMethod(sym.originalOwner.originalLexicallyEnclosingClass)
6162
}
62-
enclosingMethod(symHelper(classSym.originalOwner).originalLexicallyEnclosingClass)
63+
enclosingMethod(classSym.originalOwner.originalLexicallyEnclosingClass)
6364
}
6465

6566
/**
@@ -70,9 +71,9 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
7071
assert(classSym.isClass, classSym)
7172
def enclosingClass(sym: Symbol): Symbol = {
7273
if (sym.isClass) sym
73-
else enclosingClass(symHelper(sym.originalOwner).originalLexicallyEnclosingClass)
74+
else enclosingClass(sym.originalOwner.originalLexicallyEnclosingClass)
7475
}
75-
enclosingClass(symHelper(classSym.originalOwner).originalLexicallyEnclosingClass)
76+
enclosingClass(classSym.originalOwner.originalLexicallyEnclosingClass)
7677
}
7778

7879
/*final*/ case class EnclosingMethodEntry(owner: String, name: String, methodDescriptor: String)

compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
3030
// import definitions._
3131
import tpd._
3232
import int._
33+
import int.symExtensions
3334
import bTypes._
3435
import coreBTypes._
3536
import BCodeBodyBuilder._
@@ -70,7 +71,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
7071

7172
tree match {
7273
case Assign(lhs @ SelectBI(qual, _), rhs) =>
73-
val isStatic = symHelper(lhs.symbol).isStaticMember
74+
val isStatic = lhs.symbol.isStaticMember
7475
if (!isStatic) { genLoadQualifier(lhs) }
7576
genLoad(rhs, symInfoTK(lhs.symbol))
7677
lineNumber(tree)
@@ -331,7 +332,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
331332
case t @ Ident(_) => (t, Nil)
332333
}
333334

334-
if (!symHelper(fun.symbol).isStaticMember) {
335+
if (!fun.symbol.isStaticMember) {
335336
// load receiver of non-static implementation of lambda
336337

337338
// darkdimius: I haven't found in spec `this` reference should go
@@ -378,7 +379,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
378379
if (sym.is(Flags.Module)) {
379380
genLoadQualUnlessElidable()
380381
genLoadModule(tree)
381-
} else if (symHelper(sym).isStaticMember) {
382+
} else if (sym.isStaticMember) {
382383
genLoadQualUnlessElidable()
383384
fieldLoad(sym, receiverClass)
384385
} else {
@@ -466,7 +467,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
466467
val owner = internalName(if (useSpecificReceiver) specificReceiver else field.owner)
467468
val fieldJName = field.name.mangledString.toString
468469
val fieldDescr = symInfoTK(field).descriptor
469-
val isStatic = symHelper(field).isStaticMember
470+
val isStatic = field.isStaticMember
470471
val opc =
471472
if (isLoad) { if (isStatic) asm.Opcodes.GETSTATIC else asm.Opcodes.GETFIELD }
472473
else { if (isStatic) asm.Opcodes.PUTSTATIC else asm.Opcodes.PUTFIELD }
@@ -702,7 +703,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
702703
if (!isModuleInitialized &&
703704
jMethodName == INSTANCE_CONSTRUCTOR_NAME &&
704705
fun.symbol.name.mangledString.toString == INSTANCE_CONSTRUCTOR_NAME &&
705-
symHelper(claszSymbol).isStaticModuleClass) {
706+
claszSymbol.isStaticModuleClass) {
706707
isModuleInitialized = true
707708
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
708709
mnode.visitFieldInsn(
@@ -771,7 +772,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
771772
generatedType = genPrimitiveOp(app, expectedType)
772773
} else { // normal method call
773774
val invokeStyle =
774-
if (symHelper(sym).isStaticMember) InvokeStyle.Static
775+
if (sym.isStaticMember) InvokeStyle.Static
775776
else if (sym.is(Flags.Private) || sym.isClassConstructor) InvokeStyle.Special
776777
else InvokeStyle.Virtual
777778

@@ -1039,7 +1040,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
10391040
}
10401041

10411042
def genLoadModule(module: Symbol): Unit = {
1042-
def inStaticMethod = methSymbol != null && symHelper(methSymbol).isStaticMember
1043+
def inStaticMethod = methSymbol != null && methSymbol.isStaticMember
10431044
if (claszSymbol == module.moduleClass && jMethodName != "readResolve" && !inStaticMethod) {
10441045
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
10451046
} else {
@@ -1436,7 +1437,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
14361437
val isSerializable = functionalInterface.isSerializable || defn.isFunctionClass(functionalInterface)
14371438
val isInterface = symHelper(lambdaTarget.owner).isEmittedInterface
14381439
val invokeStyle =
1439-
if (symHelper(lambdaTarget).isStaticMember) asm.Opcodes.H_INVOKESTATIC
1440+
if (lambdaTarget.isStaticMember) asm.Opcodes.H_INVOKESTATIC
14401441
else if (lambdaTarget.is(Flags.Private) || lambdaTarget.isClassConstructor) asm.Opcodes.H_INVOKESPECIAL
14411442
else if (isInterface) asm.Opcodes.H_INVOKEINTERFACE
14421443
else asm.Opcodes.H_INVOKEVIRTUAL

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import dotty.tools.dotc.util.Spans._
2727
*/
2828
trait BCodeSkelBuilder extends BCodeHelpers {
2929
import int._
30+
import int.symExtensions
3031
import tpd._
3132
import bTypes._
3233
import coreBTypes._
@@ -96,15 +97,15 @@ trait BCodeSkelBuilder extends BCodeHelpers {
9697

9798
claszSymbol = cd.symbol
9899
isCZParcelable = isAndroidParcelableClass(claszSymbol)
99-
isCZStaticModule = symHelper(claszSymbol).isStaticModuleClass
100+
isCZStaticModule = claszSymbol.isStaticModuleClass
100101
thisName = internalName(claszSymbol)
101102

102103
cnode = new ClassNode1()
103104

104105
initJClass(cnode)
105106

106107
val methodSymbols = for (f <- cd.symbol.info.decls.toList if f.is(Flags.Method) && f.isTerm && !f.is(Flags.Module)) yield f
107-
val hasStaticCtor = methodSymbols exists (symHelper(_).isStaticConstructor)
108+
val hasStaticCtor = methodSymbols exists (_.isStaticConstructor)
108109
if (!hasStaticCtor) {
109110
// but needs one ...
110111
if (isCZStaticModule || isCZParcelable) {
@@ -181,7 +182,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
181182

182183
} else {
183184

184-
val skipStaticForwarders = (symHelper(claszSymbol).isInterface || claszSymbol.is(Flags.Module) || ctx.settings.XnoForwarders.value)
185+
val skipStaticForwarders = (claszSymbol.isInterface || claszSymbol.is(Flags.Module) || ctx.settings.XnoForwarders.value)
185186
if (!skipStaticForwarders) {
186187
val lmoc = claszSymbol.companionModule
187188
// add static forwarders if there are no name conflicts; see bugs #363 and #1735
@@ -255,7 +256,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
255256
val javagensig = getGenericSignature(f, claszSymbol)
256257
val flags = javaFieldFlags(f)
257258

258-
assert(!symHelper(f).isStaticMember || !symHelper(claszSymbol).isInterface || !f.is(Flags.Mutable),
259+
assert(!f.isStaticMember || !claszSymbol.isInterface || !f.is(Flags.Mutable),
259260
s"interface $claszSymbol cannot have non-final static field $f")
260261

261262
val jfield = new asm.tree.FieldNode(
@@ -470,7 +471,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
470471
// on entering a method
471472
def resetMethodBookkeeping(dd: DefDef) = {
472473
val rhs = dd.rhs
473-
locals.reset(isStaticMethod = symHelper(methSymbol).isStaticMember)
474+
locals.reset(isStaticMethod = methSymbol.isStaticMember)
474475
jumpDest = immutable.Map.empty[ /* LabelDef */ Symbol, asm.Label ]
475476

476477
// check previous invocation of genDefDef exited as many varsInScope as it entered.
@@ -544,7 +545,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
544545
methSymbol = dd.symbol
545546
jMethodName = methSymbol.name.mangledString.toString
546547
returnType = asmMethodType(dd.symbol).returnType
547-
isMethSymStaticCtor = symHelper(methSymbol).isStaticConstructor
548+
isMethSymStaticCtor = methSymbol.isStaticConstructor
548549

549550
resetMethodBookkeeping(dd)
550551

@@ -562,7 +563,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
562563
}
563564

564565
val isNative = methSymbol.hasAnnotation(NativeAttr)
565-
val isAbstractMethod = (methSymbol.is(Flags.Deferred) || (symHelper(methSymbol.owner).isInterface && ((methSymbol.is(Flags.Deferred)) || methSymbol.isClassConstructor)))
566+
val isAbstractMethod = (methSymbol.is(Flags.Deferred) || (methSymbol.owner.isInterface && ((methSymbol.is(Flags.Deferred)) || methSymbol.isClassConstructor)))
566567
val flags = GenBCodeOps.mkFlags(
567568
javaFlags(methSymbol),
568569
if (isAbstractMethod) asm.Opcodes.ACC_ABSTRACT else 0,

compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import dotty.tools.dotc.transform.SymUtils._
2323
*/
2424
class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
2525
import int._
26+
import int.symExtensions
2627

2728
val bCodeAsmCommon: BCodeAsmCommon[int.type ] = new BCodeAsmCommon(int)
2829
import bCodeAsmCommon._
@@ -80,11 +81,11 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
8081
assert(
8182
if (classSym == defn.ObjectClass)
8283
superClassSym == NoSymbol
83-
else if (symHelper(classSym).isInterface)
84+
else if (classSym.isInterface)
8485
superClassSym == defn.ObjectClass
8586
else
8687
// A ClassBType for a primitive class (scala.Boolean et al) is only created when compiling these classes.
87-
((superClassSym != NoSymbol) && !symHelper(superClassSym).isInterface) || (isCompilingPrimitive && primitiveTypeMap.contains(classSym)),
88+
((superClassSym != NoSymbol) && !superClassSym.isInterface) || (isCompilingPrimitive && primitiveTypeMap.contains(classSym)),
8889
s"Bad superClass for $classSym: $superClassSym"
8990
)
9091
val superClass = if (superClassSym == NoSymbol) None
@@ -178,11 +179,11 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
178179
private def buildNestedInfo(innerClassSym: Symbol): Option[NestedInfo] = {
179180
assert(innerClassSym.isClass, s"Cannot build NestedInfo for non-class symbol $innerClassSym")
180181

181-
val isNested = !symHelper(innerClassSym.originalOwner).originalLexicallyEnclosingClass.is(Flags.PackageClass)
182+
val isNested = !innerClassSym.originalOwner.originalLexicallyEnclosingClass.is(Flags.PackageClass)
182183
if (!isNested) None
183184
else {
184185
// See comment in BTypes, when is a class marked static in the InnerClass table.
185-
val isStaticNestedClass = symHelper(innerClassSym.originalOwner).originalLexicallyEnclosingClass.isOriginallyStaticOwner
186+
val isStaticNestedClass = innerClassSym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner
186187

187188
// After lambdalift (which is where we are), the rawowoner field contains the enclosing class.
188189
val enclosingClassSym = {
@@ -199,12 +200,12 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
199200
if (isAnonymousOrLocalClass(innerClassSym)) {
200201
None
201202
} else {
202-
val outerName = symHelper(innerClassSym.originalOwner).originalLexicallyEnclosingClass.fullName.mangledString.replace('.', '/')
203+
val outerName = innerClassSym.originalOwner.originalLexicallyEnclosingClass.fullName.mangledString.replace('.', '/')
203204
def dropModule(str: String): String =
204205
if (!str.isEmpty && str.last == '$') str.take(str.length - 1) else str
205206
// Java compatibility. See the big comment in BTypes that summarizes the InnerClass spec.
206207
val outerNameModule =
207-
if (symHelper(symHelper(innerClassSym.originalOwner).originalLexicallyEnclosingClass).isTopLevelModuleClass) dropModule(outerName)
208+
if (symHelper(innerClassSym.originalOwner.originalLexicallyEnclosingClass).isTopLevelModuleClass) dropModule(outerName)
208209
else outerName
209210
Some(outerNameModule.toString)
210211
}
@@ -232,7 +233,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
232233
* the owner of U is T, so UModuleClass.isStatic is true. Phase travel does not help here.
233234
*/
234235
private def (sym: Symbol).isOriginallyStaticOwner: Boolean =
235-
sym.is(Flags.PackageClass) || sym.is(Flags.ModuleClass) && symHelper(sym.originalOwner).originalLexicallyEnclosingClass.isOriginallyStaticOwner
236+
sym.is(Flags.PackageClass) || sym.is(Flags.ModuleClass) && sym.originalOwner.originalLexicallyEnclosingClass.isOriginallyStaticOwner
236237

237238
/**
238239
* Return the Java modifiers for the given symbol.
@@ -263,7 +264,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
263264
GenBCodeOps.mkFlags(
264265
if (privateFlag) ACC_PRIVATE else ACC_PUBLIC,
265266
if (sym.is(Flags.Deferred) || sym.isOneOf(Flags.AbstractOrTrait)) ACC_ABSTRACT else 0,
266-
if (symHelper(sym).isInterface) ACC_INTERFACE else 0,
267+
if (sym.isInterface) ACC_INTERFACE else 0,
267268

268269
if (finalFlag &&
269270
// Primitives are "abstract final" to prohibit instantiation
@@ -274,10 +275,10 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
274275
// Mixin forwarders are bridges and can be final, but final bridges confuse some frameworks
275276
!sym.is(Flags.Bridge))
276277
ACC_FINAL else 0,
277-
if (symHelper(sym).isStaticMember) ACC_STATIC else 0,
278+
if (sym.isStaticMember) ACC_STATIC else 0,
278279
if (sym.is(Flags.Bridge)) ACC_BRIDGE | ACC_SYNTHETIC else 0,
279280
if (sym.is(Flags.Artifact)) ACC_SYNTHETIC else 0,
280-
if (sym.isClass && !symHelper(sym).isInterface) ACC_SUPER else 0,
281+
if (sym.isClass && !sym.isInterface) ACC_SUPER else 0,
281282
if (sym.isAllOf(Flags.JavaEnumTrait)) ACC_ENUM else 0,
282283
if (sym.is(Flags.JavaVarargs)) ACC_VARARGS else 0,
283284
if (sym.is(Flags.Synchronized)) ACC_SYNCHRONIZED else 0,

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,11 @@ class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap
205205
}
206206
}
207207

208-
def symHelper(sym: Symbol): SymbolHelper = new SymbolHelper(sym)
209-
210-
class SymbolHelper(sym: Symbol) {
208+
extension symExtensions on (sym: Symbol) {
211209

212-
// tests
213-
def isPublic: Boolean = !sym.flags.isOneOf(Flags.Private | Flags.Protected)
214210
def isInterface: Boolean = (sym.is(Flags.PureInterface)) || sym.is(Flags.Trait)
215211

216-
/** Does this symbol actually correspond to an interface that will be emitted?
217-
* In the backend, this should be preferred over `isInterface` because it
218-
* also returns true for the symbols of the fake companion objects we
219-
* create for Java-defined classes as well as for Java annotations
220-
* which we represent as classes.
221-
*/
222-
def isEmittedInterface: Boolean = isInterface ||
223-
sym.is(Flags.JavaDefined) && (toDenot(sym).isAnnotation || sym.is(Flags.ModuleClass) && (sym.companionClass.is(Flags.PureInterface)) || sym.companionClass.is(Flags.Trait))
212+
def isStaticConstructor: Boolean = (sym.isStaticMember && sym.isClassConstructor) || (sym.name eq nme.STATIC_CONSTRUCTOR)
224213

225214
def isStaticMember: Boolean = (sym ne NoSymbol) &&
226215
(sym.is(Flags.JavaStatic) || sym.hasAnnotation(ctx.definitions.ScalaStaticAnnot))
@@ -241,11 +230,8 @@ class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap
241230
toDenot(sym)(shiftedContext).isStatic(shiftedContext)
242231
}
243232

244-
def isStaticConstructor: Boolean = (isStaticMember && sym.isClassConstructor) || (sym.name eq nme.STATIC_CONSTRUCTOR)
245233

246234

247-
// navigation
248-
249235
def originalLexicallyEnclosingClass: Symbol =
250236
// used to populate the EnclosingMethod attribute.
251237
// it is very tricky in presence of classes(and annonymous classes) defined inside supper calls.
@@ -255,6 +241,27 @@ class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap
255241
toDenot(sym)(shiftedContext).lexicallyEnclosingClass(shiftedContext)
256242
} else NoSymbol
257243

244+
}
245+
246+
def symHelper(sym: Symbol): SymbolHelper = new SymbolHelper(sym)
247+
248+
class SymbolHelper(sym: Symbol) {
249+
250+
// tests
251+
252+
/** Does this symbol actually correspond to an interface that will be emitted?
253+
* In the backend, this should be preferred over `isInterface` because it
254+
* also returns true for the symbols of the fake companion objects we
255+
* create for Java-defined classes as well as for Java annotations
256+
* which we represent as classes.
257+
*/
258+
def isEmittedInterface: Boolean = sym.isInterface ||
259+
sym.is(Flags.JavaDefined) && (toDenot(sym).isAnnotation || sym.is(Flags.ModuleClass) && (sym.companionClass.is(Flags.PureInterface)) || sym.companionClass.is(Flags.Trait))
260+
261+
262+
263+
// navigation
264+
258265
// members
259266

260267
/** For currently compiled classes: All locally defined classes including local classes.

0 commit comments

Comments
 (0)