From aa3be395a48e9337f6cffaad403cbbc5d2bfdc98 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Sun, 23 Aug 2015 14:45:52 +0300 Subject: [PATCH] Extract WritableScopeStorage from WritableScopeImpl --- .../resolve/scopes/WritableScopeImpl.kt | 101 ++-------------- .../resolve/scopes/WritableScopeStorage.kt | 111 ++++++++++++++++++ 2 files changed, 124 insertions(+), 88 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt index 303806f2676..e0ae0a09298 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt @@ -22,8 +22,7 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.util.collectionUtils.concatInOrder import org.jetbrains.kotlin.utils.Printer -import java.util.ArrayList -import java.util.HashMap +import java.util.* // Reads from: // 1. Maps @@ -34,18 +33,18 @@ import java.util.HashMap public class WritableScopeImpl @jvmOverloads constructor( outerScope: JetScope, private val ownerDeclarationDescriptor: DeclarationDescriptor, - private val redeclarationHandler: RedeclarationHandler, + override val redeclarationHandler: RedeclarationHandler, private val debugName: String, implicitReceiver: ReceiverParameterDescriptor? = null, private val labeledDeclaration: DeclarationDescriptor? = null -) : AbstractScopeAdapter(), WritableScope { +) : AbstractScopeAdapter(), WritableScope, WritableScopeStorage { override val workerScope: JetScope = if (outerScope is WritableScope) outerScope.takeSnapshot() else outerScope - private val addedDescriptors = SmartList() + override val addedDescriptors = SmartList() - private var functionsByName: MutableMap? = null - private var variablesAndClassifiersByName: MutableMap? = null + override var functionsByName: MutableMap? = null + override var variablesAndClassifiersByName: MutableMap? = null private val implicitReceiverHierarchy = if (implicitReceiver != null) listOf(implicitReceiver) + super.getImplicitReceiversHierarchy() @@ -54,10 +53,10 @@ public class WritableScopeImpl @jvmOverloads constructor( private var lastSnapshot: Snapshot? = null - private var lockLevel: WritableScope.LockLevel = WritableScope.LockLevel.WRITING - override fun getContainingDeclaration() = ownerDeclarationDescriptor + private var lockLevel: WritableScope.LockLevel = WritableScope.LockLevel.WRITING + override fun changeLockLevel(lockLevel: WritableScope.LockLevel) { if (lockLevel.ordinal() < this.lockLevel.ordinal()) { throw IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + toString()) @@ -65,13 +64,13 @@ public class WritableScopeImpl @jvmOverloads constructor( this.lockLevel = lockLevel } - protected fun checkMayRead() { + private fun checkMayRead() { if (lockLevel != WritableScope.LockLevel.READING && lockLevel != WritableScope.LockLevel.BOTH) { throw IllegalStateException("cannot read with lock level " + lockLevel + " at " + toString()) } } - protected fun checkMayWrite() { + private fun checkMayWrite() { if (lockLevel != WritableScope.LockLevel.WRITING && lockLevel != WritableScope.LockLevel.BOTH) { throw IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()) } @@ -106,27 +105,8 @@ public class WritableScopeImpl @jvmOverloads constructor( superResult } - private fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) { - checkMayWrite() - - val name = descriptor.getName() - - val originalDescriptor = variableOrClassDescriptorByName(name) - if (originalDescriptor != null) { - redeclarationHandler.handleRedeclaration(originalDescriptor, descriptor) - } - - val descriptorIndex = addDescriptor(descriptor) - - if (variablesAndClassifiersByName == null) { - variablesAndClassifiersByName = HashMap() - } - //TODO: could not use += because of KT-8050 - variablesAndClassifiersByName!![name] = variablesAndClassifiersByName!![name] + descriptorIndex - - } - override fun addVariableDescriptor(variableDescriptor: VariableDescriptor) { + checkMayWrite() addVariableOrClassDescriptor(variableDescriptor) } @@ -143,15 +123,7 @@ public class WritableScopeImpl @jvmOverloads constructor( override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { checkMayWrite() - - val descriptorIndex = addDescriptor(functionDescriptor) - - if (functionsByName == null) { - functionsByName = HashMap(1) - } - val name = functionDescriptor.getName() - //TODO: could not use += because of KT-8050 - functionsByName!![name] = functionsByName!![name] + descriptorIndex + super.addFunctionDescriptor(functionDescriptor) } override fun getFunctions(name: Name, location: LookupLocation): Collection { @@ -161,6 +133,7 @@ public class WritableScopeImpl @jvmOverloads constructor( } override fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor) { + checkMayWrite() addVariableOrClassDescriptor(classifierDescriptor) } @@ -175,40 +148,6 @@ public class WritableScopeImpl @jvmOverloads constructor( override fun getOwnDeclaredDescriptors(): Collection = addedDescriptors - private fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? { - if (descriptorLimit == 0) return null - - var list = variablesAndClassifiersByName?.get(name) - while (list != null) { - val descriptorIndex = list.last - if (descriptorIndex < descriptorLimit) { - return descriptorIndex.descriptorByIndex() - } - list = list.prev - } - return null - } - - private fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List? { - if (descriptorLimit == 0) return null - - var list = functionsByName?.get(name) - while (list != null) { - if (list.last < descriptorLimit) { - return list.toDescriptors() - } - list = list.prev - } - return null - } - - private fun addDescriptor(descriptor: DeclarationDescriptor): Int { - addedDescriptors.add(descriptor) - return addedDescriptors.size() - 1 - } - - private fun Int.descriptorByIndex() = addedDescriptors[this] - override fun toString(): String { return javaClass.getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(this)) + " " + debugName + " for " + getContainingDeclaration() } @@ -226,20 +165,6 @@ public class WritableScopeImpl @jvmOverloads constructor( p.println("}") } - private class IntList(val last: Int, val prev: IntList?) - - private fun IntList?.plus(value: Int) = IntList(value, this) - - private fun IntList.toDescriptors(): List { - val result = ArrayList(1) - var rest: IntList? = this - do { - result.add(rest!!.last.descriptorByIndex() as TDescriptor) - rest = rest.prev - } while (rest != null) - return result - } - private inner class Snapshot(val descriptorLimit: Int) : JetScope by this@WritableScopeImpl { override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection { checkMayRead() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt new file mode 100644 index 00000000000..720d31912db --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt @@ -0,0 +1,111 @@ +/* + * 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.resolve.scopes + +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.name.Name +import java.util.ArrayList +import java.util.HashMap + +public interface WritableScopeStorage { + + // must be protected, but KT-3029 Protected does not work in traits: IllegalAccessError + val addedDescriptors: MutableList + val redeclarationHandler: RedeclarationHandler + + var functionsByName: MutableMap? // = null + var variablesAndClassifiersByName: MutableMap? // = null + + protected fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) { + val name = descriptor.getName() + + val originalDescriptor = variableOrClassDescriptorByName(name) + if (originalDescriptor != null) { + redeclarationHandler.handleRedeclaration(originalDescriptor, descriptor) + } + + val descriptorIndex = addDescriptor(descriptor) + + if (variablesAndClassifiersByName == null) { + variablesAndClassifiersByName = HashMap() + } + //TODO: could not use += because of KT-8050 + variablesAndClassifiersByName!![name] = variablesAndClassifiersByName!![name] + descriptorIndex + + } + + protected fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { + val descriptorIndex = addDescriptor(functionDescriptor) + + if (functionsByName == null) { + functionsByName = HashMap(1) + } + val name = functionDescriptor.getName() + //TODO: could not use += because of KT-8050 + functionsByName!![name] = functionsByName!![name] + descriptorIndex + } + + protected fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): DeclarationDescriptor? { + if (descriptorLimit == 0) return null + + var list = variablesAndClassifiersByName?.get(name) + while (list != null) { + val descriptorIndex = list.last + if (descriptorIndex < descriptorLimit) { + return descriptorIndex.descriptorByIndex() + } + list = list.prev + } + return null + } + + protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size()): List? { + if (descriptorLimit == 0) return null + + var list = functionsByName?.get(name) + while (list != null) { + if (list.last < descriptorLimit) { + return list.toDescriptors() + } + list = list.prev + } + return null + } + + private fun addDescriptor(descriptor: DeclarationDescriptor): Int { + addedDescriptors.add(descriptor) + return addedDescriptors.size() - 1 + } + + protected class IntList(val last: Int, val prev: IntList?) + + private fun Int.descriptorByIndex() = addedDescriptors[this] + + private fun IntList?.plus(value: Int) = IntList(value, this) + + private fun IntList.toDescriptors(): List { + val result = ArrayList(1) + var rest: IntList? = this + do { + result.add(rest!!.last.descriptorByIndex() as TDescriptor) + rest = rest.prev + } while (rest != null) + return result + } + +} \ No newline at end of file