diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java index 8d4d9f68ebc..c549364e2fb 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java @@ -798,7 +798,7 @@ public class BodyResolver { RedeclarationHandler.DO_NOTHING, new Function1() { @Override public Unit invoke(LexicalScopeImpl.InitializeHandler handler) { - handler.addVariableOrClassDescriptor(fieldDescriptor); + handler.addVariableDescriptor(fieldDescriptor); return Unit.INSTANCE$; } }); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt index c3c1b7d166f..c0eaf9afff4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.resolve.scopes -import com.intellij.util.SmartList import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name @@ -31,27 +30,19 @@ public class LexicalScopeImpl @JvmOverloads constructor( private val debugName: String, redeclarationHandler: RedeclarationHandler = RedeclarationHandler.DO_NOTHING, initialize: LexicalScopeImpl.InitializeHandler.() -> Unit = {} -): LexicalScope, WritableScopeStorage { +): LexicalScope, WritableScopeStorage(redeclarationHandler) { override val parent = parent.takeSnapshot() - override val addedDescriptors: MutableList = SmartList() - override val redeclarationHandler: RedeclarationHandler - get() = RedeclarationHandler.DO_NOTHING - - override var functionsByName: MutableMap? = null - override var variablesAndClassifiersByName: MutableMap? = null init { - InitializeHandler(redeclarationHandler).initialize() + InitializeHandler().initialize() } + override fun getContributedClassifier(name: Name, location: LookupLocation) = getClassifier(name) + override fun getContributedVariables(name: Name, location: LookupLocation) = getVariables(name) + + override fun getContributedFunctions(name: Name, location: LookupLocation) = getFunctions(name) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = addedDescriptors - override fun getContributedClassifier(name: Name, location: LookupLocation) = getDeclaredClassifier(name) - - override fun getContributedVariables(name: Name, location: LookupLocation) = getDeclaredVariables(name) - - override fun getContributedFunctions(name: Name, location: LookupLocation) = getDeclaredFunctions(name) - override fun toString(): String = debugName override fun printStructure(p: Printer) { @@ -66,28 +57,16 @@ public class LexicalScopeImpl @JvmOverloads constructor( p.println("}") } - inner class InitializeHandler(override val redeclarationHandler: RedeclarationHandler): WritableScopeStorage { - override val addedDescriptors: MutableList - get() = this@LexicalScopeImpl.addedDescriptors - override var functionsByName: MutableMap? - get() = this@LexicalScopeImpl.functionsByName - set(value) { - this@LexicalScopeImpl.functionsByName = value - } - override var variablesAndClassifiersByName: MutableMap? - get() = this@LexicalScopeImpl.variablesAndClassifiersByName - set(value) { - this@LexicalScopeImpl.variablesAndClassifiersByName = value - } + inner class InitializeHandler() { public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit - = addVariableOrClassDescriptor(variableDescriptor) + = this@LexicalScopeImpl.addVariableOrClassDescriptor(variableDescriptor) - public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit - = super.addFunctionDescriptor(functionDescriptor) + public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit + = this@LexicalScopeImpl.addFunctionDescriptorInternal(functionDescriptor) public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor): Unit - = addVariableOrClassDescriptor(classifierDescriptor) + = this@LexicalScopeImpl.addVariableOrClassDescriptor(classifierDescriptor) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt index 667d1be2e26..14a6bcbb5cd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.resolve.scopes -import com.intellij.util.SmartList import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name @@ -28,9 +27,9 @@ class LexicalWritableScope( override val ownerDescriptor: DeclarationDescriptor, override val isOwnerDescriptorAccessibleByLabel: Boolean, override val implicitReceiver: ReceiverParameterDescriptor?, - override val redeclarationHandler: RedeclarationHandler, + redeclarationHandler: RedeclarationHandler, private val debugName: String -) : LexicalScope, WritableScopeStorage { +) : LexicalScope, WritableScopeStorage(redeclarationHandler) { public enum class LockLevel { WRITING, BOTH, @@ -38,10 +37,6 @@ class LexicalWritableScope( } override val parent = parent.takeSnapshot() - override val addedDescriptors: MutableList = SmartList() - - override var functionsByName: MutableMap? = null - override var variablesAndClassifiersByName: MutableMap? = null private var lockLevel: LockLevel = LockLevel.WRITING private var lastSnapshot: Snapshot? = null @@ -66,9 +61,9 @@ class LexicalWritableScope( addVariableOrClassDescriptor(variableDescriptor) } - public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { + public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { checkMayWrite() - super.addFunctionDescriptor(functionDescriptor) + addFunctionDescriptorInternal(functionDescriptor) } public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor) { @@ -79,11 +74,11 @@ class LexicalWritableScope( override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = checkMayRead().addedDescriptors - override fun getContributedClassifier(name: Name, location: LookupLocation) = checkMayRead().getDeclaredClassifier(name) + override fun getContributedClassifier(name: Name, location: LookupLocation) = checkMayRead().getClassifier(name) - override fun getContributedVariables(name: Name, location: LookupLocation) = checkMayRead().getDeclaredVariables(name) + override fun getContributedVariables(name: Name, location: LookupLocation) = checkMayRead().getVariables(name) - override fun getContributedFunctions(name: Name, location: LookupLocation) = checkMayRead().getDeclaredFunctions(name) + override fun getContributedFunctions(name: Name, location: LookupLocation) = checkMayRead().getFunctions(name) private fun checkMayRead(): LexicalWritableScope { if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) { @@ -112,13 +107,13 @@ class LexicalWritableScope( = this@LexicalWritableScope.addedDescriptors.subList(0, descriptorLimit) override fun getContributedClassifier(name: Name, location: LookupLocation) - = this@LexicalWritableScope.getDeclaredClassifier(name, descriptorLimit) + = this@LexicalWritableScope.getClassifier(name, descriptorLimit) override fun getContributedVariables(name: Name, location: LookupLocation) - = this@LexicalWritableScope.getDeclaredVariables(name, descriptorLimit) + = this@LexicalWritableScope.getVariables(name, descriptorLimit) override fun getContributedFunctions(name: Name, location: LookupLocation) - = this@LexicalWritableScope.getDeclaredFunctions(name, descriptorLimit) + = this@LexicalWritableScope.getFunctions(name, descriptorLimit) override fun toString(): String = "Snapshot($descriptorLimit) for $debugName" diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt index de4fa7b0d1b..28465594993 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt @@ -16,26 +16,22 @@ package org.jetbrains.kotlin.resolve.scopes +import com.intellij.util.SmartList import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor -import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name -import java.util.ArrayList -import java.util.HashMap +import java.util.* -public interface WritableScopeStorage { +abstract class WritableScopeStorage(val redeclarationHandler: RedeclarationHandler) { - // must be protected, but KT-3029 Protected does not work in traits: IllegalAccessError - val addedDescriptors: MutableList - val redeclarationHandler: RedeclarationHandler + protected val addedDescriptors: MutableList = SmartList() - var functionsByName: MutableMap? // = null - var variablesAndClassifiersByName: MutableMap? // = null + private var functionsByName: MutableMap? = null + private var variablesAndClassifiersByName: MutableMap? = null - // Effectively protected: not to be used outside subclasses - fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) { + protected fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) { val name = descriptor.getName() val originalDescriptor = variableOrClassDescriptorByName(name) @@ -53,8 +49,7 @@ public interface WritableScopeStorage { } - // Effectively protected: not to be used outside subclasses - fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { + protected fun addFunctionDescriptorInternal(functionDescriptor: FunctionDescriptor) { val descriptorIndex = addDescriptor(functionDescriptor) if (functionsByName == null) { @@ -65,8 +60,7 @@ public interface WritableScopeStorage { functionsByName!![name] = functionsByName!![name] + descriptorIndex } - // Effectively protected: not to be used outside subclasses - fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? { + protected fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? { if (descriptorLimit == 0) return null var list = variablesAndClassifiersByName?.get(name) @@ -80,8 +74,7 @@ public interface WritableScopeStorage { return null } - // Effectively protected: not to be used outside subclasses - fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List? { + protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List? { if (descriptorLimit == 0) return null var list = functionsByName?.get(name) @@ -99,8 +92,7 @@ public interface WritableScopeStorage { return addedDescriptors.size() - 1 } - // Effectively protected: not to be used outside subclasses - class IntList(val last: Int, val prev: IntList?) + private class IntList(val last: Int, val prev: IntList?) private fun Int.descriptorByIndex() = addedDescriptors[this] @@ -116,13 +108,13 @@ public interface WritableScopeStorage { return result } - fun getDeclaredClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size()) + protected fun getClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size()) = variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor - fun getDeclaredVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection + protected fun getVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection = listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor) - fun getDeclaredFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection + protected fun getFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection = functionsByName(name, descriptorLimit) ?: emptyList() } \ No newline at end of file