Minor. Make WritableScopeStorage abstract class

This commit is contained in:
Stanislav Erokhin
2015-10-31 08:19:53 +03:00
parent 5c6134af81
commit 40146a0df8
4 changed files with 36 additions and 70 deletions
@@ -798,7 +798,7 @@ public class BodyResolver {
RedeclarationHandler.DO_NOTHING, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() { RedeclarationHandler.DO_NOTHING, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override @Override
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) { public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
handler.addVariableOrClassDescriptor(fieldDescriptor); handler.addVariableDescriptor(fieldDescriptor);
return Unit.INSTANCE$; return Unit.INSTANCE$;
} }
}); });
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.scopes package org.jetbrains.kotlin.resolve.scopes
import com.intellij.util.SmartList
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -31,27 +30,19 @@ public class LexicalScopeImpl @JvmOverloads constructor(
private val debugName: String, private val debugName: String,
redeclarationHandler: RedeclarationHandler = RedeclarationHandler.DO_NOTHING, redeclarationHandler: RedeclarationHandler = RedeclarationHandler.DO_NOTHING,
initialize: LexicalScopeImpl.InitializeHandler.() -> Unit = {} initialize: LexicalScopeImpl.InitializeHandler.() -> Unit = {}
): LexicalScope, WritableScopeStorage { ): LexicalScope, WritableScopeStorage(redeclarationHandler) {
override val parent = parent.takeSnapshot() override val parent = parent.takeSnapshot()
override val addedDescriptors: MutableList<DeclarationDescriptor> = SmartList()
override val redeclarationHandler: RedeclarationHandler
get() = RedeclarationHandler.DO_NOTHING
override var functionsByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
override var variablesAndClassifiersByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
init { 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 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 toString(): String = debugName
override fun printStructure(p: Printer) { override fun printStructure(p: Printer) {
@@ -66,28 +57,16 @@ public class LexicalScopeImpl @JvmOverloads constructor(
p.println("}") p.println("}")
} }
inner class InitializeHandler(override val redeclarationHandler: RedeclarationHandler): WritableScopeStorage { inner class InitializeHandler() {
override val addedDescriptors: MutableList<DeclarationDescriptor>
get() = this@LexicalScopeImpl.addedDescriptors
override var functionsByName: MutableMap<Name, WritableScopeStorage.IntList>?
get() = this@LexicalScopeImpl.functionsByName
set(value) {
this@LexicalScopeImpl.functionsByName = value
}
override var variablesAndClassifiersByName: MutableMap<Name, WritableScopeStorage.IntList>?
get() = this@LexicalScopeImpl.variablesAndClassifiersByName
set(value) {
this@LexicalScopeImpl.variablesAndClassifiersByName = value
}
public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit
= addVariableOrClassDescriptor(variableDescriptor) = this@LexicalScopeImpl.addVariableOrClassDescriptor(variableDescriptor)
public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit
= super.addFunctionDescriptor(functionDescriptor) = this@LexicalScopeImpl.addFunctionDescriptorInternal(functionDescriptor)
public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor): Unit public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor): Unit
= addVariableOrClassDescriptor(classifierDescriptor) = this@LexicalScopeImpl.addVariableOrClassDescriptor(classifierDescriptor)
} }
} }
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.scopes package org.jetbrains.kotlin.resolve.scopes
import com.intellij.util.SmartList
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -28,9 +27,9 @@ class LexicalWritableScope(
override val ownerDescriptor: DeclarationDescriptor, override val ownerDescriptor: DeclarationDescriptor,
override val isOwnerDescriptorAccessibleByLabel: Boolean, override val isOwnerDescriptorAccessibleByLabel: Boolean,
override val implicitReceiver: ReceiverParameterDescriptor?, override val implicitReceiver: ReceiverParameterDescriptor?,
override val redeclarationHandler: RedeclarationHandler, redeclarationHandler: RedeclarationHandler,
private val debugName: String private val debugName: String
) : LexicalScope, WritableScopeStorage { ) : LexicalScope, WritableScopeStorage(redeclarationHandler) {
public enum class LockLevel { public enum class LockLevel {
WRITING, WRITING,
BOTH, BOTH,
@@ -38,10 +37,6 @@ class LexicalWritableScope(
} }
override val parent = parent.takeSnapshot() override val parent = parent.takeSnapshot()
override val addedDescriptors: MutableList<DeclarationDescriptor> = SmartList()
override var functionsByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
override var variablesAndClassifiersByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
private var lockLevel: LockLevel = LockLevel.WRITING private var lockLevel: LockLevel = LockLevel.WRITING
private var lastSnapshot: Snapshot? = null private var lastSnapshot: Snapshot? = null
@@ -66,9 +61,9 @@ class LexicalWritableScope(
addVariableOrClassDescriptor(variableDescriptor) addVariableOrClassDescriptor(variableDescriptor)
} }
public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) {
checkMayWrite() checkMayWrite()
super<WritableScopeStorage>.addFunctionDescriptor(functionDescriptor) addFunctionDescriptorInternal(functionDescriptor)
} }
public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor) { public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor) {
@@ -79,11 +74,11 @@ class LexicalWritableScope(
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= checkMayRead().addedDescriptors = 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 { private fun checkMayRead(): LexicalWritableScope {
if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) { if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) {
@@ -112,13 +107,13 @@ class LexicalWritableScope(
= this@LexicalWritableScope.addedDescriptors.subList(0, descriptorLimit) = this@LexicalWritableScope.addedDescriptors.subList(0, descriptorLimit)
override fun getContributedClassifier(name: Name, location: LookupLocation) override fun getContributedClassifier(name: Name, location: LookupLocation)
= this@LexicalWritableScope.getDeclaredClassifier(name, descriptorLimit) = this@LexicalWritableScope.getClassifier(name, descriptorLimit)
override fun getContributedVariables(name: Name, location: LookupLocation) override fun getContributedVariables(name: Name, location: LookupLocation)
= this@LexicalWritableScope.getDeclaredVariables(name, descriptorLimit) = this@LexicalWritableScope.getVariables(name, descriptorLimit)
override fun getContributedFunctions(name: Name, location: LookupLocation) 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" override fun toString(): String = "Snapshot($descriptorLimit) for $debugName"
@@ -16,26 +16,22 @@
package org.jetbrains.kotlin.resolve.scopes package org.jetbrains.kotlin.resolve.scopes
import com.intellij.util.SmartList
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import java.util.ArrayList import java.util.*
import java.util.HashMap
public interface WritableScopeStorage { abstract class WritableScopeStorage(val redeclarationHandler: RedeclarationHandler) {
// must be protected, but KT-3029 Protected does not work in traits: IllegalAccessError protected val addedDescriptors: MutableList<DeclarationDescriptor> = SmartList()
val addedDescriptors: MutableList<DeclarationDescriptor>
val redeclarationHandler: RedeclarationHandler
var functionsByName: MutableMap<Name, IntList>? // = null private var functionsByName: MutableMap<Name, IntList>? = null
var variablesAndClassifiersByName: MutableMap<Name, IntList>? // = null private var variablesAndClassifiersByName: MutableMap<Name, IntList>? = null
// Effectively protected: not to be used outside subclasses protected fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) {
fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) {
val name = descriptor.getName() val name = descriptor.getName()
val originalDescriptor = variableOrClassDescriptorByName(name) val originalDescriptor = variableOrClassDescriptorByName(name)
@@ -53,8 +49,7 @@ public interface WritableScopeStorage {
} }
// Effectively protected: not to be used outside subclasses protected fun addFunctionDescriptorInternal(functionDescriptor: FunctionDescriptor) {
fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) {
val descriptorIndex = addDescriptor(functionDescriptor) val descriptorIndex = addDescriptor(functionDescriptor)
if (functionsByName == null) { if (functionsByName == null) {
@@ -65,8 +60,7 @@ public interface WritableScopeStorage {
functionsByName!![name] = functionsByName!![name] + descriptorIndex functionsByName!![name] = functionsByName!![name] + descriptorIndex
} }
// Effectively protected: not to be used outside subclasses protected fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? {
fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? {
if (descriptorLimit == 0) return null if (descriptorLimit == 0) return null
var list = variablesAndClassifiersByName?.get(name) var list = variablesAndClassifiersByName?.get(name)
@@ -80,8 +74,7 @@ public interface WritableScopeStorage {
return null return null
} }
// Effectively protected: not to be used outside subclasses protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List<FunctionDescriptor>? {
fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List<FunctionDescriptor>? {
if (descriptorLimit == 0) return null if (descriptorLimit == 0) return null
var list = functionsByName?.get(name) var list = functionsByName?.get(name)
@@ -99,8 +92,7 @@ public interface WritableScopeStorage {
return addedDescriptors.size() - 1 return addedDescriptors.size() - 1
} }
// Effectively protected: not to be used outside subclasses private class IntList(val last: Int, val prev: IntList?)
class IntList(val last: Int, val prev: IntList?)
private fun Int.descriptorByIndex() = addedDescriptors[this] private fun Int.descriptorByIndex() = addedDescriptors[this]
@@ -116,13 +108,13 @@ public interface WritableScopeStorage {
return result return result
} }
fun getDeclaredClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size()) protected fun getClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size())
= variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor = variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor
fun getDeclaredVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<VariableDescriptor> protected fun getVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<VariableDescriptor>
= listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor) = listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor)
fun getDeclaredFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<FunctionDescriptor> protected fun getFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<FunctionDescriptor>
= functionsByName(name, descriptorLimit) ?: emptyList() = functionsByName(name, descriptorLimit) ?: emptyList()
} }