org.jetbrains.kotlin.storage.LockBasedLazyResolveStorageManager.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.storage
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice
import org.jetbrains.kotlin.util.slicedMap.WritableSlice
import org.jetbrains.kotlin.resolve.TraceEntryFilter
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.types.KotlinType
class LockBasedLazyResolveStorageManager(private val storageManager: StorageManager): StorageManager by storageManager, LazyResolveStorageManager {
override fun createSoftlyRetainedMemoizedFunction(compute: Function1) =
storageManager.createMemoizedFunction(compute, ContainerUtil.createConcurrentSoftValueMap())
override fun createSoftlyRetainedMemoizedFunctionWithNullableValues(compute: Function1) =
storageManager.createMemoizedFunctionWithNullableValues(compute, ContainerUtil.createConcurrentSoftValueMap())
override fun createSafeTrace(originalTrace: BindingTrace): BindingTrace =
LockProtectedTrace(storageManager, originalTrace)
private class LockProtectedContext(private val storageManager: StorageManager, private val context: BindingContext) : BindingContext {
override fun getType(expression: KtExpression): KotlinType? = storageManager.compute { context.getType(expression) }
override fun getDiagnostics(): Diagnostics = storageManager.compute { context.diagnostics }
override fun get(slice: ReadOnlySlice, key: K) = storageManager.compute { context.get(slice, key) }
override fun getKeys(slice: WritableSlice) = storageManager.compute { context.getKeys(slice) }
override fun addOwnDataTo(trace: BindingTrace, commitDiagnostics: Boolean) {
storageManager.compute { context.addOwnDataTo(trace, commitDiagnostics) }
}
@TestOnly
override fun getSliceContents(slice: ReadOnlySlice) = storageManager.compute { context.getSliceContents(slice) }
}
private class LockProtectedTrace(private val storageManager: StorageManager, private val trace: BindingTrace) : BindingTrace {
override fun recordType(expression: KtExpression, type: KotlinType?) {
storageManager.compute { trace.recordType(expression, type) }
}
override fun getType(expression: KtExpression): KotlinType? = storageManager.compute { trace.getType(expression) }
private val context: BindingContext = LockProtectedContext(storageManager, trace.bindingContext)
override fun getBindingContext() = context
override fun record(slice: WritableSlice, key: K, value: V) {
storageManager.compute { trace.record(slice, key, value) }
}
override fun record(slice: WritableSlice, key: K) {
storageManager.compute { trace.record(slice, key) }
}
override fun get(slice: ReadOnlySlice, key: K): V? = storageManager.compute { trace.get(slice, key) }
override fun getKeys(slice: WritableSlice): Collection = storageManager.compute { trace.getKeys(slice) }
override fun report(diagnostic: Diagnostic) {
storageManager.compute { trace.report(diagnostic) }
}
}
}