From 8c9ace7d4096c319b2cc3848ba2a98dc33d73b62 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 20 Jun 2017 18:55:44 +0300 Subject: [PATCH] Get rid of Guava in DelegatingDataFlowInfo (except Multimap things) --- .../smartcasts/DelegatingDataFlowInfo.kt | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt index a6200d42c56..a173c07e38b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/smartcasts/DelegatingDataFlowInfo.kt @@ -16,7 +16,8 @@ package org.jetbrains.kotlin.resolve.calls.smartcasts -import com.google.common.collect.* +import com.google.common.collect.SetMultimap +import com.google.common.collect.LinkedHashMultimap import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageVersionSettings @@ -31,7 +32,7 @@ import java.util.* internal class DelegatingDataFlowInfo private constructor( private val parent: DataFlowInfo?, - private val nullabilityInfo: ImmutableMap, + private val nullabilityInfo: Map, // Also immutable private val typeInfo: SetMultimap, /** @@ -41,11 +42,11 @@ internal class DelegatingDataFlowInfo private constructor( private val valueWithGivenTypeInfo: DataFlowValue? ) : DataFlowInfo { - constructor(): this(null, ImmutableMap.of(), newTypeInfo(), null) + constructor(): this(null, emptyMap(), newTypeInfo(), null) override val completeNullabilityInfo: Map get() { - val result = Maps.newHashMap() + val result = hashMapOf() var info: DelegatingDataFlowInfo? = this while (info != null) { for ((key, value) in info.nullabilityInfo) { @@ -145,13 +146,13 @@ internal class DelegatingDataFlowInfo private constructor( * @param value */ override fun clearValueInfo(value: DataFlowValue, languageVersionSettings: LanguageVersionSettings): DataFlowInfo { - val builder = Maps.newHashMap() + val builder = hashMapOf() putNullability(builder, value, value.immanentNullability, languageVersionSettings) - return create(this, ImmutableMap.copyOf(builder), EMPTY_TYPE_INFO, value) + return create(this, builder.toMap(), EMPTY_TYPE_INFO, value) } override fun assign(a: DataFlowValue, b: DataFlowValue, languageVersionSettings: LanguageVersionSettings): DataFlowInfo { - val nullability = Maps.newHashMap() + val nullability = hashMapOf() val nullabilityOfB = getStableNullability(b) putNullability(nullability, a, nullabilityOfB, languageVersionSettings, affectReceiver = false) @@ -166,13 +167,13 @@ internal class DelegatingDataFlowInfo private constructor( } newTypeInfo.putAll(a, typesForB) - return create(this, ImmutableMap.copyOf(nullability), if (newTypeInfo.isEmpty) EMPTY_TYPE_INFO else newTypeInfo, a) + return create(this, nullability.toMap(), if (newTypeInfo.isEmpty) EMPTY_TYPE_INFO else newTypeInfo, a) } override fun equate( a: DataFlowValue, b: DataFlowValue, identityEquals: Boolean, languageVersionSettings: LanguageVersionSettings ): DataFlowInfo { - val builder = Maps.newHashMap() + val builder = hashMapOf() val nullabilityOfA = getStableNullability(a) val nullabilityOfB = getStableNullability(b) @@ -200,7 +201,7 @@ internal class DelegatingDataFlowInfo private constructor( this } else { - create(this, ImmutableMap.copyOf(builder), if (newTypeInfo.isEmpty) EMPTY_TYPE_INFO else newTypeInfo) + create(this, builder.toMap(), if (newTypeInfo.isEmpty) EMPTY_TYPE_INFO else newTypeInfo) } } @@ -225,13 +226,13 @@ internal class DelegatingDataFlowInfo private constructor( override fun disequate( a: DataFlowValue, b: DataFlowValue, languageVersionSettings: LanguageVersionSettings ): DataFlowInfo { - val builder = Maps.newHashMap() + val builder = hashMapOf() val nullabilityOfA = getStableNullability(a) val nullabilityOfB = getStableNullability(b) val changed = putNullability(builder, a, nullabilityOfA.refine(nullabilityOfB.invert()), languageVersionSettings) or putNullability(builder, b, nullabilityOfB.refine(nullabilityOfA.invert()), languageVersionSettings) - return if (changed) create(this, ImmutableMap.copyOf(builder), EMPTY_TYPE_INFO) else this + return if (changed) create(this, builder.toMap(), EMPTY_TYPE_INFO) else this } override fun establishSubtyping( @@ -242,11 +243,11 @@ internal class DelegatingDataFlowInfo private constructor( if (!value.type.isFlexible() && value.type.isSubtypeOf(type)) return this val newTypeInfo = newTypeInfo() newTypeInfo.put(value, type) - val builder = Maps.newHashMap() + val builder = hashMapOf() if (!type.isMarkedNullable) { putNullability(builder, value, NOT_NULL, languageVersionSettings) } - val newNullabilityInfo = if (type.isMarkedNullable) EMPTY_NULLABILITY_INFO else ImmutableMap.copyOf(builder) + val newNullabilityInfo = if (type.isMarkedNullable) emptyMap() else builder.toMap() return create(this, newNullabilityInfo, newTypeInfo) } @@ -257,7 +258,7 @@ internal class DelegatingDataFlowInfo private constructor( assert(other is DelegatingDataFlowInfo) { "Unknown DataFlowInfo type: " + other } - val nullabilityMapBuilder = Maps.newHashMap() + val nullabilityMapBuilder = hashMapOf() for ((key, otherFlags) in other.completeNullabilityInfo) { val thisFlags = getCollectedNullability(key) val flags = thisFlags.and(otherFlags) @@ -272,16 +273,16 @@ internal class DelegatingDataFlowInfo private constructor( return this } - return create(this, ImmutableMap.copyOf(nullabilityMapBuilder), otherTypeInfo) + return create(this, nullabilityMapBuilder.toMap(), otherTypeInfo) } private fun Set.containsNothing() = any { KotlinBuiltIns.isNothing(it) } - private fun Set.intersect(other: Set) = + private fun Set.intersectConsideringNothing(other: Set) = when { other.containsNothing() -> this this.containsNothing() -> other - else -> Sets.intersection(this, other) + else -> this.intersect(other) } override fun or(other: DataFlowInfo): DataFlowInfo { @@ -291,7 +292,7 @@ internal class DelegatingDataFlowInfo private constructor( assert(other is DelegatingDataFlowInfo) { "Unknown DataFlowInfo type: " + other } - val nullabilityMapBuilder = Maps.newHashMap() + val nullabilityMapBuilder = hashMapOf() for ((key, otherFlags) in other.completeNullabilityInfo) { val thisFlags = getCollectedNullability(key) nullabilityMapBuilder.put(key, thisFlags.or(otherFlags)) @@ -301,17 +302,17 @@ internal class DelegatingDataFlowInfo private constructor( val otherTypeInfo = other.completeTypeInfo val newTypeInfo = newTypeInfo() - for (key in Sets.intersection(myTypeInfo.keySet(), otherTypeInfo.keySet())) { - newTypeInfo.putAll(key, myTypeInfo[key].intersect(otherTypeInfo[key])) + for (key in myTypeInfo.keySet()) { + if (key in otherTypeInfo.keySet()) { + newTypeInfo.putAll(key, myTypeInfo[key].intersectConsideringNothing(otherTypeInfo[key])) + } } - - return create(null, ImmutableMap.copyOf(nullabilityMapBuilder), newTypeInfo) + return create(null, nullabilityMapBuilder.toMap(), newTypeInfo) } override fun toString() = if (typeInfo.isEmpty && nullabilityInfo.isEmpty()) "EMPTY" else "Non-trivial DataFlowInfo" companion object { - private val EMPTY_NULLABILITY_INFO = ImmutableMap.of() private val EMPTY_TYPE_INFO = newTypeInfo() private fun containsAll(first: SetMultimap, second: SetMultimap) = @@ -320,7 +321,7 @@ internal class DelegatingDataFlowInfo private constructor( fun newTypeInfo(): SetMultimap = LinkedHashMultimap.create() private fun create(parent: DataFlowInfo?, - nullabilityInfo: ImmutableMap, + nullabilityInfo: Map, // NB: typeInfo must be mutable here! typeInfo: SetMultimap, valueWithGivenTypeInfo: DataFlowValue? = null