From 1d5cd8f7e1899c64c18a2113dffc45fa705daa6b Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Tue, 25 Aug 2015 22:21:51 +0300 Subject: [PATCH] Created LexicalWritableScope add added RedeclarationHandler for LexicalScopeImpl --- .../kotlin/resolve/scopes/LexicalScopeImpl.kt | 42 ++++-- .../resolve/scopes/LexicalWritableScope.kt | 134 ++++++++++++++++++ .../resolve/scopes/WritableScopeStorage.kt | 12 ++ .../jetbrains/kotlin/resolve/scopes/Scopes.kt | 10 +- 4 files changed, 178 insertions(+), 20 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt 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 217c6c7e6d7..15e4845ff64 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalScopeImpl.kt @@ -22,12 +22,13 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.utils.Printer -public open class LexicalScopeImpl( +public class LexicalScopeImpl jvmOverloads constructor( override val parent: LexicalScope, override val ownerDescriptor: DeclarationDescriptor, override val isOwnerDescriptorAccessibleByLabel: Boolean, override val implicitReceiver: ReceiverParameterDescriptor?, private val debugName: String, + redeclarationHandler: RedeclarationHandler = RedeclarationHandler.DO_NOTHING, initialize: LexicalScopeImpl.InitializeHandler.() -> Unit = {} ): LexicalScope, WritableScopeStorage { override val addedDescriptors: MutableList = SmartList() @@ -38,19 +39,16 @@ public open class LexicalScopeImpl( override var variablesAndClassifiersByName: MutableMap? = null init { - InitializeHandler().initialize() + InitializeHandler(redeclarationHandler).initialize() } - override fun getDeclaredDescriptors(): Collection = addedDescriptors + override fun getDeclaredDescriptors() = addedDescriptors - override fun getDeclaredClassifier(name: Name, location: LookupLocation) - = variableOrClassDescriptorByName(name) as? ClassifierDescriptor + override fun getDeclaredClassifier(name: Name, location: LookupLocation) = getDeclaredClassifier(name) - override fun getDeclaredVariables(name: Name, location: LookupLocation): Collection - = listOfNotNull(variableOrClassDescriptorByName(name) as? VariableDescriptor) + override fun getDeclaredVariables(name: Name, location: LookupLocation) = getDeclaredVariables(name) - override fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection - = functionsByName(name) ?: emptyList() + override fun getDeclaredFunctions(name: Name, location: LookupLocation) = getDeclaredFunctions(name) override fun toString(): String = debugName @@ -66,14 +64,28 @@ public open class LexicalScopeImpl( p.println("}") } - inner class InitializeHandler { - public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit - = this@LexicalScopeImpl.addVariableOrClassDescriptor(variableDescriptor) + 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 + } - public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit - = this@LexicalScopeImpl.addFunctionDescriptor(functionDescriptor) + public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit + = addVariableOrClassDescriptor(variableDescriptor) + + public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit + = super.addFunctionDescriptor(functionDescriptor) public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor): Unit - = this@LexicalScopeImpl.addVariableOrClassDescriptor(classifierDescriptor) + = 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 new file mode 100644 index 00000000000..d7ab0680f0a --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/LexicalWritableScope.kt @@ -0,0 +1,134 @@ +/* + * 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 com.intellij.util.SmartList +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.incremental.components.LookupLocation +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.utils.Printer + +class LexicalWritableScope( + override val parent: LexicalScope, + override val ownerDescriptor: DeclarationDescriptor, + override val isOwnerDescriptorAccessibleByLabel: Boolean, + override val implicitReceiver: ReceiverParameterDescriptor?, + override val redeclarationHandler: RedeclarationHandler, + private val debugName: String +) : LexicalScope, WritableScopeStorage { + override val addedDescriptors: MutableList = SmartList() + + override var functionsByName: MutableMap? = null + override var variablesAndClassifiersByName: MutableMap? = null + + private var lockLevel: WritableScope.LockLevel = WritableScope.LockLevel.WRITING + private var lastSnapshot: Snapshot? = null + + public fun changeLockLevel(lockLevel: WritableScope.LockLevel) { + if (lockLevel.ordinal() < this.lockLevel.ordinal()) { + throw IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + toString()) + } + this.lockLevel = lockLevel + } + + public fun takeSnapshot(): LexicalScope { + checkMayRead() + if (lastSnapshot == null || lastSnapshot!!.descriptorLimit != addedDescriptors.size()) { + lastSnapshot = Snapshot(addedDescriptors.size()) + } + return lastSnapshot!! + } + + public fun addVariableDescriptor(variableDescriptor: VariableDescriptor) { + checkMayWrite() + addVariableOrClassDescriptor(variableDescriptor) + } + + public override fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor) { + checkMayWrite() + super.addFunctionDescriptor(functionDescriptor) + } + + public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor) { + checkMayWrite() + addVariableOrClassDescriptor(classifierDescriptor) + } + + override fun getDeclaredDescriptors() = checkMayRead().addedDescriptors + + override fun getDeclaredClassifier(name: Name, location: LookupLocation) = checkMayRead().getDeclaredClassifier(name) + + override fun getDeclaredVariables(name: Name, location: LookupLocation) = checkMayRead().getDeclaredVariables(name) + + override fun getDeclaredFunctions(name: Name, location: LookupLocation) = checkMayRead().getDeclaredFunctions(name) + + private fun checkMayRead(): LexicalWritableScope { + if (lockLevel != WritableScope.LockLevel.READING && lockLevel != WritableScope.LockLevel.BOTH) { + throw IllegalStateException("cannot read with lock level " + lockLevel + " at " + toString()) + } + return this + } + + private fun checkMayWrite() { + if (lockLevel != WritableScope.LockLevel.WRITING && lockLevel != WritableScope.LockLevel.BOTH) { + throw IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString()) + } + } + + private inner class Snapshot(val descriptorLimit: Int) : LexicalScope { + override val parent: LexicalScope? + get() = this@LexicalWritableScope.parent + override val ownerDescriptor: DeclarationDescriptor + get() = this@LexicalWritableScope.ownerDescriptor + override val isOwnerDescriptorAccessibleByLabel: Boolean + get() = this@LexicalWritableScope.isOwnerDescriptorAccessibleByLabel + override val implicitReceiver: ReceiverParameterDescriptor? + get() = this@LexicalWritableScope.implicitReceiver + + override fun getDeclaredDescriptors() = this@LexicalWritableScope.addedDescriptors.subList(0, descriptorLimit) + + override fun getDeclaredClassifier(name: Name, location: LookupLocation) + = this@LexicalWritableScope.getDeclaredClassifier(name, descriptorLimit) + + override fun getDeclaredVariables(name: Name, location: LookupLocation) + = this@LexicalWritableScope.getDeclaredVariables(name, descriptorLimit) + + override fun getDeclaredFunctions(name: Name, location: LookupLocation) + = this@LexicalWritableScope.getDeclaredFunctions(name, descriptorLimit) + + override fun toString(): String = "Snapshot for $debugName" + + override fun printStructure(p: Printer) { + p.println("Snapshot with descriptorLimit = $descriptorLimit for scope:") + this@LexicalWritableScope.printStructure(p) + } + } + + override fun toString(): String = debugName + + override fun printStructure(p: Printer) { + p.println(javaClass.simpleName, ": ", debugName, "; for descriptor: ", ownerDescriptor.name, + " with implicitReceiver: ", implicitReceiver?.value, " {") + p.pushIndent() + + p.print("parent = ") + parent.printStructure(p.withholdIndentOnce()) + + p.popIndent() + p.println("}") + } +} \ No newline at end of file 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 720d31912db..225206bf565 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeStorage.kt @@ -16,8 +16,11 @@ package org.jetbrains.kotlin.resolve.scopes +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 @@ -108,4 +111,13 @@ public interface WritableScopeStorage { return result } + fun getDeclaredClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size()) + = variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor + + fun getDeclaredVariables(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection + = listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor) + + fun getDeclaredFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection + = functionsByName(name, descriptorLimit) ?: emptyList() + } \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt index 5f76c567dac..b10e34ce278 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt @@ -35,11 +35,11 @@ public interface LexicalScope { public fun getDeclaredDescriptors(): Collection - public fun getDeclaredClassifier(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): ClassifierDescriptor? + public fun getDeclaredClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? // need collection here because there may be extension property foo and usual property foo - public fun getDeclaredVariables(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection - public fun getDeclaredFunctions(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection + public fun getDeclaredVariables(name: Name, location: LookupLocation): Collection + public fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection public fun printStructure(p: Printer) } @@ -58,8 +58,8 @@ public interface FileScope: LexicalScope { fun getPackage(name: Name): PackageViewDescriptor? - public fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection - public fun getSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection + public fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation): Collection + public fun getSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation): Collection public fun getSyntheticExtensionProperties(receiverTypes: Collection): Collection public fun getSyntheticExtensionFunctions(receiverTypes: Collection): Collection