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>() {
@Override
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
handler.addVariableOrClassDescriptor(fieldDescriptor);
handler.addVariableDescriptor(fieldDescriptor);
return Unit.INSTANCE$;
}
});
@@ -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<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 {
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<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
}
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)
}
}
@@ -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<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 lastSnapshot: Snapshot? = null
@@ -66,9 +61,9 @@ class LexicalWritableScope(
addVariableOrClassDescriptor(variableDescriptor)
}
public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) {
public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) {
checkMayWrite()
super<WritableScopeStorage>.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"
@@ -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<DeclarationDescriptor>
val redeclarationHandler: RedeclarationHandler
protected val addedDescriptors: MutableList<DeclarationDescriptor> = SmartList()
var functionsByName: MutableMap<Name, IntList>? // = null
var variablesAndClassifiersByName: MutableMap<Name, IntList>? // = null
private var functionsByName: MutableMap<Name, IntList>? = null
private var variablesAndClassifiersByName: MutableMap<Name, IntList>? = 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<FunctionDescriptor>? {
protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List<FunctionDescriptor>? {
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<VariableDescriptor>
protected fun getVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<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()
}