Created LexicalChainedScope and LexicalScopeImpl
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.ReceiverParameterDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch
|
||||
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
|
||||
public class LexicalChainedScope(
|
||||
override val parent: LexicalScope,
|
||||
override val ownerDescriptor: DeclarationDescriptor,
|
||||
override val isOwnerDescriptorAccessibleByLabel: Boolean,
|
||||
override val implicitReceiver: ReceiverParameterDescriptor?,
|
||||
private val debugName: String,
|
||||
vararg memberScopes: JetScope // todo JetScope -> MemberScope
|
||||
): LexicalScope {
|
||||
private val scopeChain = memberScopes.clone()
|
||||
|
||||
override fun getDeclaredDescriptors() = getFromAllScopes(scopeChain) { it.getAllDescriptors() }
|
||||
|
||||
override fun getDeclaredClassifier(name: Name, location: LookupLocation) = getFirstMatch(scopeChain) { it.getClassifier(name, location) }
|
||||
|
||||
override fun getDeclaredVariables(name: Name, location: LookupLocation) = getFromAllScopes(scopeChain) { it.getProperties(name, location) }
|
||||
|
||||
override fun getDeclaredFunctions(name: Name, location: LookupLocation) = getFromAllScopes(scopeChain) { it.getFunctions(name, location) }
|
||||
|
||||
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()
|
||||
|
||||
for (scope in scopeChain) {
|
||||
scope.printScopeStructure(p)
|
||||
}
|
||||
|
||||
p.print("parent = ")
|
||||
parent.printStructure(p.withholdIndentOnce())
|
||||
|
||||
p.popIndent()
|
||||
p.println("}")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
public open class LexicalScopeImpl(
|
||||
override val parent: LexicalScope,
|
||||
override val ownerDescriptor: DeclarationDescriptor,
|
||||
override val isOwnerDescriptorAccessibleByLabel: Boolean,
|
||||
override val implicitReceiver: ReceiverParameterDescriptor?,
|
||||
private val debugName: String,
|
||||
initialize: LexicalScopeImpl.InitializeHandler.() -> Unit = {}
|
||||
): LexicalScope, WritableScopeStorage {
|
||||
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().initialize()
|
||||
}
|
||||
|
||||
override fun getDeclaredDescriptors(): Collection<DeclarationDescriptor> = addedDescriptors
|
||||
|
||||
override fun getDeclaredClassifier(name: Name, location: LookupLocation)
|
||||
= variableOrClassDescriptorByName(name) as? ClassifierDescriptor
|
||||
|
||||
override fun getDeclaredVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor>
|
||||
= listOfNotNull(variableOrClassDescriptorByName(name) as? VariableDescriptor)
|
||||
|
||||
override fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
|
||||
= functionsByName(name) ?: emptyList()
|
||||
|
||||
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("}")
|
||||
}
|
||||
|
||||
inner class InitializeHandler {
|
||||
public fun addVariableDescriptor(variableDescriptor: VariableDescriptor): Unit
|
||||
= this@LexicalScopeImpl.addVariableOrClassDescriptor(variableDescriptor)
|
||||
|
||||
public fun addFunctionDescriptor(functionDescriptor: FunctionDescriptor): Unit
|
||||
= this@LexicalScopeImpl.addFunctionDescriptor(functionDescriptor)
|
||||
|
||||
public fun addClassifierDescriptor(classifierDescriptor: ClassifierDescriptor): Unit
|
||||
= this@LexicalScopeImpl.addVariableOrClassDescriptor(classifierDescriptor)
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,8 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
import org.jetbrains.kotlin.util.collectionUtils.concat
|
||||
import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch
|
||||
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -32,50 +33,32 @@ public open class ChainedScope(
|
||||
private val scopeChain = scopes.clone()
|
||||
private var implicitReceiverHierarchy: List<ReceiverParameterDescriptor>? = null
|
||||
|
||||
private inline fun getFirstMatch<T>(callback: (JetScope) -> T): T {
|
||||
// NOTE: This is performance-sensitive; please don't replace with map().firstOrNull()
|
||||
for (scope in scopeChain) {
|
||||
val result = callback(scope)
|
||||
if (result != null) return result
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private inline fun getFromAllScopes<T>(callback: (JetScope) -> Collection<T>): Collection<T> {
|
||||
if (scopeChain.isEmpty()) return emptySet()
|
||||
var result: Collection<T>? = null
|
||||
for (scope in scopeChain) {
|
||||
result = result.concat(callback(scope))
|
||||
}
|
||||
return result ?: emptySet()
|
||||
}
|
||||
|
||||
override fun getClassifier(name: Name, location: LookupLocation): ClassifierDescriptor?
|
||||
= getFirstMatch { it.getClassifier(name, location) }
|
||||
= getFirstMatch(scopeChain) { it.getClassifier(name, location) }
|
||||
|
||||
override fun getPackage(name: Name): PackageViewDescriptor?
|
||||
= getFirstMatch { it.getPackage(name) }
|
||||
= getFirstMatch(scopeChain) { it.getPackage(name) }
|
||||
|
||||
override fun getProperties(name: Name, location: LookupLocation): Collection<VariableDescriptor>
|
||||
= getFromAllScopes { it.getProperties(name, location) }
|
||||
= getFromAllScopes(scopeChain) { it.getProperties(name, location) }
|
||||
|
||||
override fun getLocalVariable(name: Name): VariableDescriptor?
|
||||
= getFirstMatch { it.getLocalVariable(name) }
|
||||
= getFirstMatch(scopeChain) { it.getLocalVariable(name) }
|
||||
|
||||
override fun getFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
|
||||
= getFromAllScopes { it.getFunctions(name, location) }
|
||||
= getFromAllScopes(scopeChain) { it.getFunctions(name, location) }
|
||||
|
||||
override fun getSyntheticExtensionProperties(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor>
|
||||
= getFromAllScopes { it.getSyntheticExtensionProperties(receiverTypes, name, location) }
|
||||
= getFromAllScopes(scopeChain) { it.getSyntheticExtensionProperties(receiverTypes, name, location) }
|
||||
|
||||
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<JetType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor>
|
||||
= getFromAllScopes { it.getSyntheticExtensionFunctions(receiverTypes, name, location) }
|
||||
= getFromAllScopes(scopeChain) { it.getSyntheticExtensionFunctions(receiverTypes, name, location) }
|
||||
|
||||
override fun getSyntheticExtensionProperties(receiverTypes: Collection<JetType>): Collection<PropertyDescriptor>
|
||||
= getFromAllScopes { it.getSyntheticExtensionProperties(receiverTypes) }
|
||||
= getFromAllScopes(scopeChain) { it.getSyntheticExtensionProperties(receiverTypes) }
|
||||
|
||||
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<JetType>): Collection<FunctionDescriptor>
|
||||
= getFromAllScopes { it.getSyntheticExtensionFunctions(receiverTypes) }
|
||||
= getFromAllScopes(scopeChain) { it.getSyntheticExtensionFunctions(receiverTypes) }
|
||||
|
||||
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
|
||||
if (implicitReceiverHierarchy == null) {
|
||||
@@ -92,7 +75,7 @@ public open class ChainedScope(
|
||||
override fun getDeclarationsByLabel(labelName: Name) = scopeChain.flatMap { it.getDeclarationsByLabel(labelName) }
|
||||
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
|
||||
= getFromAllScopes { it.getDescriptors(kindFilter, nameFilter) }
|
||||
= getFromAllScopes(scopeChain) { it.getDescriptors(kindFilter, nameFilter) }
|
||||
|
||||
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> {
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
+20
-2
@@ -22,7 +22,7 @@ import java.util.*
|
||||
* Concatenates the contents of this collection with the given collection, avoiding allocations if possible.
|
||||
* Can modify `this` if it is a mutable collection.
|
||||
*/
|
||||
fun <T> Collection<T>?.concat(collection: Collection<T>): Collection<T>? {
|
||||
public fun <T> Collection<T>?.concat(collection: Collection<T>): Collection<T>? {
|
||||
if (collection.isEmpty()) {
|
||||
return this
|
||||
}
|
||||
@@ -39,7 +39,7 @@ fun <T> Collection<T>?.concat(collection: Collection<T>): Collection<T>? {
|
||||
return result
|
||||
}
|
||||
|
||||
fun concatInOrder<T>(c1: Collection<T>?, c2: Collection<T>?): Collection<T> {
|
||||
public fun concatInOrder<T>(c1: Collection<T>?, c2: Collection<T>?): Collection<T> {
|
||||
val result = if (c1 == null || c1.isEmpty())
|
||||
c2
|
||||
else if (c2 == null || c2.isEmpty())
|
||||
@@ -52,3 +52,21 @@ fun concatInOrder<T>(c1: Collection<T>?, c2: Collection<T>?): Collection<T> {
|
||||
}
|
||||
return result ?: emptySet()
|
||||
}
|
||||
|
||||
public inline fun getFromAllScopes<Scope, T>(scopes: Array<out Scope>, callback: (Scope) -> Collection<T>): Collection<T> {
|
||||
if (scopes.isEmpty()) return emptySet()
|
||||
var result: Collection<T>? = null
|
||||
for (scope in scopes) {
|
||||
result = result.concat(callback(scope))
|
||||
}
|
||||
return result ?: emptySet()
|
||||
}
|
||||
|
||||
public inline fun getFirstMatch<Scope, T>(scopes: Array<out Scope>, callback: (Scope) -> T): T {
|
||||
// NOTE: This is performance-sensitive; please don't replace with map().firstOrNull()
|
||||
for (scope in scopes) {
|
||||
val result = callback(scope)
|
||||
if (result != null) return result
|
||||
}
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user