8000 Use LazyVar for CoreBTypes · scala/scala@cd861fd · GitHub
[go: up one dir, main page]

Skip to content

Commit cd861fd

Browse files
committed
Use LazyVar for CoreBTypes
Remove implicit conversion from LazyVar[T] to T
1 parent de9236c commit cd861fd

11 files changed

+249
-281
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ abstract class BCodeHelpers extends BCodeIdiomatic {
956956
val bType = mirrorClassClassBType(moduleClass)
957957
val mirrorClass = new asm.tree.ClassNode
958958
mirrorClass.visit(
959-
backendUtils.classfileVersion,
959+
backendUtils.classfileVersion.get,
960960
bType.info.get.flags,
961961
bType.internalName,
962962
null /* no java-generic-signature */,
@@ -1000,7 +1000,7 @@ abstract class BCodeHelpers extends BCodeIdiomatic {
10001000

10011001
val beanInfoClass = new asm.tree.ClassNode
10021002
beanInfoClass.visit(
1003-
backendUtils.classfileVersion,
1003+
backendUtils.classfileVersion.get,
10041004
beanInfoType.info.get.flags,
10051005
beanInfoType.internalName,
10061006
null, // no java-generic-signature

src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ abstract class BCodeIdiomatic {
188188
* can-multi-thread
189189
*/
190190
def genConcat(elemType: BType, pos: Position): Unit = {
191-
val paramType = elemType match {
191+
val paramType: BType = elemType match {
192192
case ct: ClassBType if ct.isSubtypeOf(StringRef).get => StringRef
193193
case ct: ClassBType if ct.isSubtypeOf(jlStringBufferRef).get => jlStringBufferRef
194194
case ct: ClassBType if ct.isSubtypeOf(jlCharSequenceRef).get => jlCharSequenceRef

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
129129
val flags = javaFlags(claszSymbol)
130130

131131
val thisSignature = getGenericSignature(claszSymbol, claszSymbol.owner)
132-
cnode.visit(backendUtils.classfileVersion, flags,
132+
cnode.visit(backendUtils.classfileVersion.get, flags,
133133
thisBType.internalName, thisSignature,
134134
superClass, interfaceNames.toArray)
135135

src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ abstract class BTypes {
2828
val postProcessorFrontendAccess: PostProcessorFrontendAccess
2929
import postProcessorFrontendAccess.{frontendSynch, recordPerRunCache}
3030

31-
// Some core BTypes are required here, in class BType, where no Global instance is available.
32-
// The Global is only available in the subclass BTypesFromSymbols. We cannot depend on the actual
33-
// implementation (CoreBTypesProxy) here because it has members t 57AE hat refer to global.Symbol.
34-
val coreBTypes: CoreBTypesProxyGlobalIndependent[this.type]
31+
val coreBTypes: CoreBTypes { val bTypes: BTypes.this.type }
3532
import coreBTypes._
3633

3734
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class BTypesFromSymbols[G <: Global](val global: G, val postProcessorFrontendAcc
3030
import codeGen.CodeGenImpl._
3131
import postProcessor.{bTypesFromClassfile, byteCodeRepository}
3232

33-
// Why the proxy, see documentation of class [[CoreBTypes]].
34-
val coreBTypes = new CoreBTypesProxy[this.type](this)
33+
val coreBTypes = new CoreBTypesFromSymbols[G] {
34+
val bTypes: BTypesFromSymbols.this.type = BTypesFromSymbols.this
35+
}
3536
import coreBTypes._
3637

3738
final def initialize(): Unit = {

src/compiler/scala/tools/nsc/backend/jvm/CodeGen.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class CodeGen[G <: Global](val global: G) extends PerRunLazy {
1212
private val caseInsensitively = perRunCaches.newMap[String, Symbol]()
1313

1414
// TODO: do we really need a new instance per run? Is there state that depends on the compiler frontend (symbols, types, settings)?
15-
private val mirrorCodeGen: LazyVar[CodeGenImpl.JMirrorBuilder] = perRunLazy(new CodeGenImpl.JMirrorBuilder())
15+
private[this] lazy val mirrorCodeGen: LazyVar[CodeGenImpl.JMirrorBuilder] = perRunLazy(new CodeGenImpl.JMirrorBuilder())
1616

17-
private val beanInfoCodeGen: LazyVar[CodeGenImpl.JBeanInfoBuilder] = perRunLazy(new CodeGenImpl.JBeanInfoBuilder())
17+
private[this] lazy val beanInfoCodeGen: LazyVar[CodeGenImpl.JBeanInfoBuilder] = perRunLazy(new CodeGenImpl.JBeanInfoBuilder())
1818

1919
def genUnit(unit: CompilationUnit): Unit = {
2020
import genBCode.postProcessor.generatedClasses
@@ -56,12 +56,12 @@ abstract class CodeGen[G <: Global](val global: G) extends PerRunLazy {
5656
}
5757

5858
def genMirrorClass(classSym: Symbol, unit: CompilationUnit): ClassNode = {
59-
mirrorCodeGen.genMirrorClass(classSym, unit)
59+
mirrorCodeGen.get.genMirrorClass(classSym, unit)
6060
}
6161

6262
def genBeanInfoClass(cd: ClassDef, unit: CompilationUnit): ClassNode = {
6363
val sym = cd.symbol
64-
beanInfoCodeGen.genBeanInfoClass(sym, unit, CodeGenImpl.fieldSymbols(sym), CodeGenImpl.methodSymbols(cd))
64+
beanInfoCodeGen.get.genBeanInfoClass(sym, unit, CodeGenImpl.fieldSymbols(sym), CodeGenImpl.methodSymbols(cd))
6565
}
6666

6767
private def warnCaseInsensitiveOverwrite(cd: ClassDef): Unit = {

src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala

Lines changed: 215 additions & 240 deletions
Large diffs are not rendered by default.

src/compiler/scala/tools/nsc/backend/jvm/PerRunLazy.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait PerRunLazy {
2020
r
2121
}
2222

23-
def initialize(): Unit = ls.foreach(_.reInit())
23+
def initialize(): Unit = ls.foreach(_.reInitialize())
2424
}
2525

2626
/**
@@ -30,7 +30,7 @@ class LazyVar[T](init: () => T) {
3030
@volatile private[this] var isInit: Boolean = false
3131
private[this] var v: T = _
3232

33-
def get = {
33+
def get: T = {
3434
if (isInit) v
3535
else synchronized {
3636
if (!isInit) v = init()
@@ -39,10 +39,5 @@ class LazyVar[T](init: () => T) {
3939
}
4040
}
4141

42-
def reInit(): Unit = synchronized(isInit = false)
42+
def reInitialize(): Unit = synchronized(isInit = false)
4343
}
44-
45-
object LazyVar {
46-
import language.implicitConversions
47-
implicit def lGet[T](l: LazyVar[T]): T = l.get
48-
}

src/compiler/scala/tools/nsc/backend/jvm/PostProcessor.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ abstract class PostProcessor(val frontendAccess: PostProcessorFrontendAccess) ex
3030
val bTypesFromClassfile = new { val postProcessor: PostProcessor.this.type = PostProcessor.this } with BTypesFromClassfile
3131

3232
// re-initialized per run because it reads compiler settings that might change
33-
val classfileWriter: LazyVar[ClassfileWriter] = perRunLazy(new ClassfileWriter(frontendAccess))
33+
lazy val classfileWriter: LazyVar[ClassfileWriter] = perRunLazy(new ClassfileWriter(frontendAccess))
3434

35-
val generatedClasses = recordPerRunCache(new ListBuffer[GeneratedClass])
35+
lazy val generatedClasses = recordPerRunCache(new ListBuffer[GeneratedClass])
3636

3737
override def initialize(): Unit = {
3838
super.initialize()
@@ -68,11 +68,11 @@ abstract class PostProcessor(val frontendAccess: PostProcessorFrontendAccess) ex
6868
if (AsmUtils.traceSerializedClassEnabled && classNode.name.contains(AsmUtils.traceSerializedClassPattern))
6969
AsmUtils.traceClass(bytes)
7070

71-
classfileWriter.write(classNode.name, bytes, sourceFile)
71+
classfileWriter.get.write(classNode.name, bytes, sourceFile)
7272
}
7373
}
7474

75-
classfileWriter.close()
75+
classfileWriter.get.close()
7676
}
7777

7878
def runGlobalOptimizations(): Unit = {
@@ -101,7 +101,7 @@ abstract class PostProcessor(val frontendAccess: PostProcessorFrontendAccess) ex
101101
}
102102

103103
def serializeClass(classNode: ClassNode): Array[Byte] = {
104-
val cw = new ClassWriterWithBTypeLub(backendUtils.extraProc)
104+
val cw = new ClassWriterWithBTypeLub(backendUtils.extraProc.get)
105105
classNode.accept(cw)
106106
cw.toByteArray
107107
}
@@ -214,9 +214,9 @@ object PostProcessorFrontendAccess {
214214
class PostProcessorFrontendAccessImpl(global: Global) extends PostProcessorFrontendAccess with PerRunLazy {
215215
import global._
216216

217-
private[this] val _compilerSettings: LazyVar[CompilerSettings] = perRunLazy(buildCompilerSettings())
217+
private[this] lazy val _compilerSettings: LazyVar[CompilerSettings] = perRunLazy(buildCompilerSettings())
218218

219-
def compilerSettings: CompilerSettings = _compilerSettings
219+
def compilerSettings: CompilerSettings = _compilerSettings.get
220220

221221
private def buildCompilerSettings(): CompilerSettings = new CompilerSettings {
222222
import global.{settings => s}

src/compiler/scala/tools/nsc/backend/jvm/analysis/BackendUtils.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class BackendUtils extends PerRunLazy {
3838
import frontendAccess.compilerSettings
3939

4040
// unused objects created by these constructors are eliminated by pushPop
41-
private val sideEffectFreeConstructors: LazyVar[Set[(String, String)]] = perRunLazy {
41+
private[this] lazy val sideEffectFreeConstructors: LazyVar[Set[(String, String)]] = perRunLazy {
4242
val ownerDesc = (p: (InternalName, MethodNameAndType)) => (p._1, p._2.methodType.descriptor)
4343
primitiveBoxConstructors.map(ownerDesc).toSet ++
4444
srRefConstructors.map(ownerDesc) ++
@@ -49,20 +49,20 @@ abstract class BackendUtils extends PerRunLazy {
4949
(StringRef.internalName, MethodBType(List(ArrayBType(CHAR)), UNIT).descriptor))
5050
}
5151

52-
private val classesOfSideEffectFreeConstructors: LazyVar[Set[String]] = perRunLazy(sideEffectFreeConstructors.map(_._1))
52+
private[this] lazy val classesOfSideEffectFreeConstructors: LazyVar[Set[String]] = perRunLazy(sideEffectFreeConstructors.get.map(_._1))
5353

54-
val classfileVersion: LazyVar[Int] = perRunLazy(compilerSettings.target match {
54+
lazy val classfileVersion: LazyVar[Int] = perRunLazy(compilerSettings.target match {
5555
case "jvm-1.8" => asm.Opcodes.V1_8
5656
})
5757

5858

59-
val majorVersion: LazyVar[Int] = perRunLazy(classfileVersion & 0xFF)
59+
lazy val majorVersion: LazyVar[Int] = perRunLazy(classfileVersion.get & 0xFF)
6060

61-
val emitStackMapFrame: LazyVar[Boolean] = perRunLazy(majorVersion >= 50)
61+
lazy val emitStackMapFrame: LazyVar[Boolean] = perRunLazy(majorVersion.get >= 50)
6262

63-
val extraProc: LazyVar[Int] = perRunLazy(GenBCode.mkFlags(
63+
lazy val extraProc: LazyVar[Int] = perRunLazy(GenBCode.mkFlags(
6464
asm.ClassWriter.COMPUTE_MAXS,
65-
if (emitStackMapFrame) asm.ClassWriter.COMPUTE_FRAMES else 0
65+
if (emitStackMapFrame.get) asm.ClassWriter.COMPUTE_FRAMES else 0
6666
))
6767

6868
/**
@@ -293,13 +293,13 @@ abstract class BackendUtils extends PerRunLazy {
293293

294294

295295
def isSideEffectFreeConstructorCall(insn: MethodInsnNode): Boolean = {
296-
insn.name == INSTANCE_CONSTRUCTOR_NAME && sideEffectFreeConstructors((insn.owner, insn.desc))
296+
insn.name == INSTANCE_CONSTRUCTOR_NAME && sideEffectFreeConstructors.get((insn.owner, insn.desc))
297297
}
298298

299299
def isNewForSideEffectFreeConstructor(insn: AbstractInsnNode) = {
300300
insn.getOpcode == NEW && {
301301
val ti = insn.asInstanceOf[TypeInsnNode]
302-
classesOfSideEffectFreeConstructors.contains(ti.desc)
302+
classesOfSideEffectFreeConstructors.get.contains(ti.desc)
303303
}
304304
}
305305

src/compiler/scala/tools/nsc/backend/jvm/opt/InlinerHeuristics.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class InlinerHeuristics extends PerRunLazy {
2626
import callGraph._
2727
import frontendAccess.{backendReporting, compilerSettings}
2828

29-
val inlineSourceMatcher: LazyVar[InlineSourceMatcher] = perRunLazy(new InlineSourceMatcher(compilerSettings.optInlineFrom))
29+
lazy val inlineSourceMatcher: LazyVar[InlineSourceMatcher] = perRunLazy(new InlineSourceMatcher(compilerSettings.optInlineFrom))
3030

3131
final case class InlineRequest(callsite: Callsite, post: List[InlineRequest], reason: String) {
3232
// invariant: all post inline requests denote callsites in the callee of the main callsite
@@ -36,8 +36,8 @@ abstract class InlinerHeuristics extends PerRunLazy {
3636
def canInlineFromSource(sourceFilePath: Option[String], calleeDeclarationClass: InternalName) = {
3737
compilerSettings.optLClasspath ||
3838
compilerSettings.optLProject && sourceFilePath.isDefined ||
39-
inlineSourceMatcher.allowFromSources && sourceFilePath.isDefined ||
40-
inlineSourceMatcher.allow(calleeDeclarationClass)
39+
inlineSourceMatcher.get.allowFromSources && sourceFilePath.isDefined ||
40+
inlineSourceMatcher.get.allow(calleeDeclarationClass)
4141
}
4242

4343
/**

0 commit comments

Comments
 (0)
0