From b6a4c279a4a34a19fa80109bd5a76b9987b1c4a2 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Thu, 5 Nov 2020 10:19:31 -0800 Subject: [PATCH] FIR checker: refactor ControlFlowInfos whose value is EventOccurrencesRange --- .../kotlin/fir/analysis/cfa/CfaUtils.kt | 31 ++++++++++++------- .../fir/analysis/cfa/ControlFlowInfo.kt | 4 ++- .../analysis/cfa/FirCallsEffectAnalyzer.kt | 14 ++------- .../checkers/extended/UnusedChecker.kt | 2 +- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/CfaUtils.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/CfaUtils.kt index ed8f129be1b..ff3c5f8968d 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/CfaUtils.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/CfaUtils.kt @@ -13,18 +13,13 @@ import org.jetbrains.kotlin.fir.resolve.dfa.cfg.* import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol import java.lang.IllegalStateException -class PropertyInitializationInfo( - map: PersistentMap = persistentMapOf() -) : ControlFlowInfo(map) { - companion object { - val EMPTY = PropertyInitializationInfo() - } +abstract class EventOccurrencesRangeInfo, K : Any>( + map: PersistentMap = persistentMapOf() +) : ControlFlowInfo(map) { - override val constructor: (PersistentMap) -> PropertyInitializationInfo = - ::PropertyInitializationInfo - - fun merge(other: PropertyInitializationInfo): PropertyInitializationInfo { - var result = this + override fun merge(other: E): E { + @Suppress("UNCHECKED_CAST") + var result = this as E for (symbol in keys.union(other.keys)) { val kind1 = this[symbol] ?: EventOccurrencesRange.ZERO val kind2 = other[symbol] ?: EventOccurrencesRange.ZERO @@ -34,6 +29,18 @@ class PropertyInitializationInfo( } } +class PropertyInitializationInfo( + map: PersistentMap = persistentMapOf() +) : EventOccurrencesRangeInfo(map) { + companion object { + val EMPTY = PropertyInitializationInfo() + } + + override val constructor: (PersistentMap) -> PropertyInitializationInfo = + ::PropertyInitializationInfo + +} + class LocalPropertyCollector private constructor() : ControlFlowGraphVisitorVoid() { companion object { fun collect(graph: ControlFlowGraph): MutableSet { @@ -105,7 +112,7 @@ class PathAwarePropertyInitializationInfo( } } - fun merge(other: PathAwarePropertyInitializationInfo): PathAwarePropertyInitializationInfo { + override fun merge(other: PathAwarePropertyInitializationInfo): PathAwarePropertyInitializationInfo { var resultMap = persistentMapOf() for (label in keys.union(other.keys)) { // disjoint merging to preserve paths. i.e., merge the property initialization info if and only if both have the key. diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/ControlFlowInfo.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/ControlFlowInfo.kt index f045b5bc0d1..bc79dfbbbcc 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/ControlFlowInfo.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/ControlFlowInfo.kt @@ -28,4 +28,6 @@ abstract class ControlFlowInfo, K : Any, V : Any> p override fun put(key: K, value: V): S { return constructor(map.put(key, value)) } -} \ No newline at end of file + + abstract fun merge(other: S): S +} diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt index 36fbc34e629..c79971066b7 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt @@ -205,8 +205,7 @@ object FirCallsEffectAnalyzer : FirControlFlowChecker() { class LambdaInvocationInfo( map: PersistentMap, EventOccurrencesRange> = persistentMapOf(), - ) : ControlFlowInfo, EventOccurrencesRange>(map) { - + ) : EventOccurrencesRangeInfo>(map) { companion object { val EMPTY = LambdaInvocationInfo() } @@ -214,15 +213,6 @@ object FirCallsEffectAnalyzer : FirControlFlowChecker() { override val constructor: (PersistentMap, EventOccurrencesRange>) -> LambdaInvocationInfo = ::LambdaInvocationInfo - fun merge(other: LambdaInvocationInfo): LambdaInvocationInfo { - var result = this - for (symbol in keys.union(other.keys)) { - val kind1 = this[symbol] ?: EventOccurrencesRange.ZERO - val kind2 = other[symbol] ?: EventOccurrencesRange.ZERO - result = result.put(symbol, kind1 or kind2) - } - return result - } } class PathAwareLambdaInvocationInfo( @@ -278,7 +268,7 @@ object FirCallsEffectAnalyzer : FirControlFlowChecker() { } } - fun merge(other: PathAwareLambdaInvocationInfo): PathAwareLambdaInvocationInfo { + override fun merge(other: PathAwareLambdaInvocationInfo): PathAwareLambdaInvocationInfo { var resultMap = persistentMapOf() for (label in keys.union(other.keys)) { // disjoint merging to preserve paths. i.e., merge the property initialization info if and only if both have the key. diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt index 0a75a5fe852..d8c7a7773f2 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt @@ -106,7 +106,7 @@ object UnusedChecker : FirControlFlowChecker() { override val constructor: (PersistentMap) -> VariableStatusInfo = ::VariableStatusInfo - fun merge(other: VariableStatusInfo): VariableStatusInfo { + override fun merge(other: VariableStatusInfo): VariableStatusInfo { var result = this for (symbol in keys.union(other.keys)) { val kind1 = this[symbol] ?: VariableStatus.UNUSED