Fix after review KT-CR-4441 of cc5382b3
Simplify API of InlineConstTracker In ConstLowering: Move transformer logic to another class to avoid mutable state. Avoid marking all files in module as module. Support of inner classes. #KT-46506 Fixed
This commit is contained in:
committed by
teamcityserver
parent
c2715d26f1
commit
47a1dd27dd
@@ -6,15 +6,17 @@
|
|||||||
package org.jetbrains.kotlin.incremental
|
package org.jetbrains.kotlin.incremental
|
||||||
|
|
||||||
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
||||||
import org.jetbrains.kotlin.incremental.components.ConstantRef
|
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
class InlineConstTrackerImpl : InlineConstTracker {
|
class InlineConstTrackerImpl : InlineConstTracker {
|
||||||
private val inlineConst = hashMapOf<String, MutableSet<ConstantRef>>()
|
private val inlineConst = hashMapOf<String, MutableSet<ConstantRef>>()
|
||||||
|
|
||||||
val inlineConstMap: Map<String, Collection<ConstantRef>>
|
val inlineConstMap: Map<String, Collection<ConstantRef>>
|
||||||
get() = inlineConst
|
get() = inlineConst
|
||||||
|
|
||||||
override fun report(filePath: String, cRefs: Collection<ConstantRef>) {
|
override fun report(filePath: String, owner: String, name: String, constType: String) {
|
||||||
inlineConst.getOrPut(filePath) { hashSetOf() }.addAll(cRefs)
|
inlineConst.getOrPut(filePath) { hashSetOf() }.add(ConstantRef(owner, name, constType))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class ConstantRef(var owner: String, var name: String, var constType: String)
|
||||||
|
|||||||
+2
-3
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.daemon.client
|
|||||||
import org.jetbrains.kotlin.daemon.common.*
|
import org.jetbrains.kotlin.daemon.common.*
|
||||||
import org.jetbrains.kotlin.incremental.components.ExpectActualTracker
|
import org.jetbrains.kotlin.incremental.components.ExpectActualTracker
|
||||||
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
||||||
import org.jetbrains.kotlin.incremental.components.ConstantRef
|
|
||||||
import org.jetbrains.kotlin.incremental.components.LookupInfo
|
import org.jetbrains.kotlin.incremental.components.LookupInfo
|
||||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||||
import org.jetbrains.kotlin.incremental.js.IncrementalDataProvider
|
import org.jetbrains.kotlin.incremental.js.IncrementalDataProvider
|
||||||
@@ -121,8 +120,8 @@ open class CompilerCallbackServicesFacadeServer(
|
|||||||
expectActualTracker!!.report(File(expectedFilePath), File(actualFilePath))
|
expectActualTracker!!.report(File(expectedFilePath), File(actualFilePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun inlineConstTracker_report(className: String, cRefs: Collection<ConstantRef>) {
|
override fun inlineConstTracker_report(filePath: String, owner: String, name: String, constType: String) {
|
||||||
inlineConstTracker?.report(className, cRefs) ?: throw NullPointerException("inlineConstTracker was not initialized")
|
inlineConstTracker?.report(filePath, owner, name, constType) ?: throw NullPointerException("inlineConstTracker was not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun incrementalResultsConsumer_processHeader(headerMetadata: ByteArray) {
|
override fun incrementalResultsConsumer_processHeader(headerMetadata: ByteArray) {
|
||||||
|
|||||||
+1
-2
@@ -17,7 +17,6 @@
|
|||||||
package org.jetbrains.kotlin.daemon.common
|
package org.jetbrains.kotlin.daemon.common
|
||||||
|
|
||||||
import org.jetbrains.kotlin.incremental.components.LookupInfo
|
import org.jetbrains.kotlin.incremental.components.LookupInfo
|
||||||
import org.jetbrains.kotlin.incremental.components.ConstantRef
|
|
||||||
import org.jetbrains.kotlin.incremental.js.JsInlineFunctionHash
|
import org.jetbrains.kotlin.incremental.js.JsInlineFunctionHash
|
||||||
import org.jetbrains.kotlin.load.kotlin.incremental.components.JvmPackagePartProto
|
import org.jetbrains.kotlin.load.kotlin.incremental.components.JvmPackagePartProto
|
||||||
import org.jetbrains.kotlin.modules.TargetId
|
import org.jetbrains.kotlin.modules.TargetId
|
||||||
@@ -108,7 +107,7 @@ interface CompilerCallbackServicesFacade : Remote {
|
|||||||
// ---------------------------------------------------
|
// ---------------------------------------------------
|
||||||
// InlineConstTracker
|
// InlineConstTracker
|
||||||
@Throws(RemoteException::class)
|
@Throws(RemoteException::class)
|
||||||
fun inlineConstTracker_report(className: String, cRefs: Collection<ConstantRef>)
|
fun inlineConstTracker_report(filePath: String, owner: String, name: String, constType: String)
|
||||||
|
|
||||||
// ---------------------------------------------------
|
// ---------------------------------------------------
|
||||||
// IncrementalResultsConsumer (js)
|
// IncrementalResultsConsumer (js)
|
||||||
|
|||||||
@@ -9,15 +9,14 @@ import org.jetbrains.kotlin.daemon.common.DummyProfiler
|
|||||||
import org.jetbrains.kotlin.daemon.common.Profiler
|
import org.jetbrains.kotlin.daemon.common.Profiler
|
||||||
import org.jetbrains.kotlin.daemon.common.withMeasure
|
import org.jetbrains.kotlin.daemon.common.withMeasure
|
||||||
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
||||||
import org.jetbrains.kotlin.incremental.components.ConstantRef
|
|
||||||
|
|
||||||
class RemoteInlineConstTracker(
|
class RemoteInlineConstTracker(
|
||||||
@Suppress("DEPRECATION") val facade: org.jetbrains.kotlin.daemon.common.CompilerCallbackServicesFacade,
|
@Suppress("DEPRECATION") val facade: org.jetbrains.kotlin.daemon.common.CompilerCallbackServicesFacade,
|
||||||
val profiler: Profiler = DummyProfiler()
|
val profiler: Profiler = DummyProfiler()
|
||||||
): InlineConstTracker {
|
): InlineConstTracker {
|
||||||
override fun report(filePath: String, cRefs: Collection<ConstantRef>) {
|
override fun report(filePath: String, owner: String, name: String, constType: String) {
|
||||||
profiler.withMeasure(this) {
|
profiler.withMeasure(this) {
|
||||||
facade.inlineConstTracker_report(filePath, cRefs)
|
facade.inlineConstTracker_report(filePath, owner, name, constType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+24
-35
@@ -9,21 +9,16 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
|||||||
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
||||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||||
import org.jetbrains.kotlin.backend.jvm.ir.constantValue
|
import org.jetbrains.kotlin.backend.jvm.ir.constantValue
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||||
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
|
||||||
import org.jetbrains.kotlin.incremental.components.ConstantRef
|
|
||||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||||
import org.jetbrains.kotlin.descriptors.SourceFile
|
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
|
||||||
import org.jetbrains.kotlin.ir.descriptors.toIrBasedDescriptor
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.ir.util.classId
|
||||||
import org.jetbrains.kotlin.ir.util.IdSignature
|
import org.jetbrains.kotlin.ir.util.parentAsClass
|
||||||
import org.jetbrains.kotlin.load.kotlin.toSourceElement
|
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||||
|
|
||||||
internal val constPhase1 = makeIrFilePhase(
|
internal val constPhase1 = makeIrFilePhase(
|
||||||
::ConstLowering,
|
::ConstLowering,
|
||||||
@@ -37,16 +32,22 @@ internal val constPhase2 = makeIrFilePhase(
|
|||||||
description = "Substitute calls to const properties with constant values"
|
description = "Substitute calls to const properties with constant values"
|
||||||
)
|
)
|
||||||
|
|
||||||
class ConstLowering(val context: JvmBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
|
class ConstLowering(val context: JvmBackendContext) : FileLoweringPass {
|
||||||
val inlineConstTracker =
|
val inlineConstTracker =
|
||||||
context.state.configuration[CommonConfigurationKeys.INLINE_CONST_TRACKER] ?: InlineConstTracker.DoNothing
|
context.state.configuration[CommonConfigurationKeys.INLINE_CONST_TRACKER]
|
||||||
|
|
||||||
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid()
|
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid(ConstTransformer(irFile, context, inlineConstTracker))
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ConstTransformer(
|
||||||
|
val irFile: IrFile,
|
||||||
|
val context: JvmBackendContext,
|
||||||
|
val inlineConstTracker: InlineConstTracker?
|
||||||
|
) : IrElementTransformerVoid() {
|
||||||
private fun IrExpression.lowerConstRead(receiver: IrExpression?, field: IrField?): IrExpression? {
|
private fun IrExpression.lowerConstRead(receiver: IrExpression?, field: IrField?): IrExpression? {
|
||||||
val value = field?.constantValue() ?: return null
|
val value = field?.constantValue() ?: return null
|
||||||
transformChildrenVoid()
|
transformChildrenVoid()
|
||||||
reportInlineConst(this@lowerConstRead, field)
|
reportInlineConst(field, value)
|
||||||
|
|
||||||
val resultExpression = if (context.state.shouldInlineConstVals)
|
val resultExpression = if (context.state.shouldInlineConstVals)
|
||||||
value.copyWithOffsets(startOffset, endOffset)
|
value.copyWithOffsets(startOffset, endOffset)
|
||||||
@@ -62,28 +63,16 @@ class ConstLowering(val context: JvmBackendContext) : IrElementTransformerVoid()
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reportInlineConst(irExpression: IrExpression, field: IrField?) {
|
private fun reportInlineConst(field: IrField, value: IrConst<*>) {
|
||||||
if (inlineConstTracker == InlineConstTracker.DoNothing) return
|
if (inlineConstTracker == null) return
|
||||||
val value = field?.constantValue() ?: return
|
if (field.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB) return
|
||||||
|
|
||||||
if (!field.isFinal || !field.isStatic) return
|
val path = irFile.path
|
||||||
|
val owner = field.parentAsClass.classId?.asString()?.replace(".", "$")?.replace("/", ".") ?: return
|
||||||
|
val name = field.name.asString()
|
||||||
|
val constType = value.kind.asString
|
||||||
|
|
||||||
val sourceFile = field.toIrBasedDescriptor().containingDeclaration.toSourceElement.containingFile
|
inlineConstTracker.report(path, owner, name, constType)
|
||||||
if (sourceFile == SourceFile.NO_SOURCE_FILE || sourceFile.toString().lowercase().endsWith(".kt")) return
|
|
||||||
|
|
||||||
for (file: KtFile in context.state.files) {
|
|
||||||
val fileName = file.virtualFilePath
|
|
||||||
val owner =
|
|
||||||
((irExpression as? IrGetFieldImpl)?.symbol?.signature as? IdSignature.CompositeSignature)?.container?.asPublic()?.firstNameSegment
|
|
||||||
?: continue
|
|
||||||
val name = field.name.asString()
|
|
||||||
val constType = value.kind.toString()
|
|
||||||
|
|
||||||
inlineConstTracker.report(
|
|
||||||
fileName,
|
|
||||||
listOf(ConstantRef(owner, name, constType))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun IrExpression.shouldDropConstReceiver() =
|
private fun IrExpression.shouldDropConstReceiver() =
|
||||||
@@ -99,4 +88,4 @@ class ConstLowering(val context: JvmBackendContext) : IrElementTransformerVoid()
|
|||||||
|
|
||||||
override fun visitGetField(expression: IrGetField): IrExpression =
|
override fun visitGetField(expression: IrGetField): IrExpression =
|
||||||
expression.lowerConstRead(expression.receiver, expression.symbol.owner) ?: super.visitGetField(expression)
|
expression.lowerConstRead(expression.receiver, expression.symbol.owner) ?: super.visitGetField(expression)
|
||||||
}
|
}
|
||||||
+18
-8
@@ -1,16 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.incremental.components
|
package org.jetbrains.kotlin.incremental.components
|
||||||
|
|
||||||
import org.jetbrains.kotlin.container.DefaultImplementation
|
/**
|
||||||
import java.io.Serializable
|
* InlineConstTracker is used to track Java constants used in Kotlin for correct build scope expansion in IC during JPS build.
|
||||||
|
*/
|
||||||
@DefaultImplementation(InlineConstTracker.DoNothing::class)
|
|
||||||
interface InlineConstTracker {
|
interface InlineConstTracker {
|
||||||
fun report(filePath: String, cRefs: Collection<ConstantRef>)
|
|
||||||
|
/**
|
||||||
|
* Report Java constant, which is defined as [name] in [owner] java class.
|
||||||
|
* This constant is used in Kotlin file [filePath].
|
||||||
|
* [constType] is one of Kotlin's [Byte, Short, Int, Long, Float, Double, Boolean, Char, String],
|
||||||
|
* that correspond to the eight primitive Java types or String
|
||||||
|
* Format of [owner] class is "package.Outer$Inner"
|
||||||
|
*/
|
||||||
|
fun report(filePath: String, owner: String, name: String, constType: String)
|
||||||
|
|
||||||
object DoNothing : InlineConstTracker {
|
object DoNothing : InlineConstTracker {
|
||||||
override fun report(filePath: String, cRefs: Collection<ConstantRef>) {
|
override fun report(filePath: String, owner: String, name: String, constType: String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ConstantRef(var owner: String, var name: String, var constType: String) : Serializable
|
|
||||||
|
|||||||
Reference in New Issue
Block a user