Created LexicalWritableScope add added RedeclarationHandler for LexicalScopeImpl

This commit is contained in:
Stanislav Erokhin
2015-08-25 22:21:51 +03:00
parent 04ccb4cf90
commit 1d5cd8f7e1
4 changed files with 178 additions and 20 deletions
@@ -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<DeclarationDescriptor> = SmartList()
@@ -38,19 +39,16 @@ public open class LexicalScopeImpl(
override var variablesAndClassifiersByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
init {
InitializeHandler().initialize()
InitializeHandler(redeclarationHandler).initialize()
}
override fun getDeclaredDescriptors(): Collection<DeclarationDescriptor> = 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<VariableDescriptor>
= listOfNotNull(variableOrClassDescriptorByName(name) as? VariableDescriptor)
override fun getDeclaredVariables(name: Name, location: LookupLocation) = getDeclaredVariables(name)
override fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
= 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<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 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)
}
}
@@ -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<DeclarationDescriptor> = SmartList()
override var functionsByName: MutableMap<Name, WritableScopeStorage.IntList>? = null
override var variablesAndClassifiersByName: MutableMap<Name, WritableScopeStorage.IntList>? = 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<WritableScopeStorage>.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("}")
}
}
@@ -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<VariableDescriptor>
= listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor)
fun getDeclaredFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size()): Collection<FunctionDescriptor>
= functionsByName(name, descriptorLimit) ?: emptyList()
}
@@ -35,11 +35,11 @@ public interface LexicalScope {
public fun getDeclaredDescriptors(): Collection<DeclarationDescriptor>
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<VariableDescriptor>
public fun getDeclaredFunctions(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection<FunctionDescriptor>
public fun getDeclaredVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor>
public fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
public fun printStructure(p: Printer)
}
@@ -58,8 +58,8 @@ public interface FileScope: LexicalScope {
fun getPackage(name: Name): PackageViewDescriptor?
public fun getSyntheticExtensionProperties(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection<PropertyDescriptor>
public fun getSyntheticExtensionFunctions(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): Collection<FunctionDescriptor>
public fun getSyntheticExtensionProperties(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor>
public fun getSyntheticExtensionFunctions(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor>
public fun getSyntheticExtensionProperties(receiverTypes: Collection<JetType>): Collection<PropertyDescriptor>
public fun getSyntheticExtensionFunctions(receiverTypes: Collection<JetType>): Collection<FunctionDescriptor>