Minor. Added some util methods for LexicalScope

This commit is contained in:
Stanislav Erokhin
2015-08-28 17:47:50 +03:00
parent d1efadf168
commit 1b1fae8899
@@ -73,20 +73,36 @@ public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<Decl
} }
} }
// Result is guaranteed to be filtered by kind and name.
public fun LexicalScope.getDescriptorsFiltered(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = { true }
): Collection<DeclarationDescriptor> {
if (kindFilter.kindMask == 0) return listOf()
return collectAllFromMeAndParent {
if (it is FileScope) {
it.getDescriptors(kindFilter, nameFilter)
} else {
it.getDeclaredDescriptors()
}
}.filter { kindFilter.accepts(it) && nameFilter(it.name) }
}
@deprecated("Use getOwnProperties instead") @deprecated("Use getOwnProperties instead")
public fun LexicalScope.getLocalVariable(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): VariableDescriptor? { public fun LexicalScope.getLocalVariable(name: Name): VariableDescriptor? {
processForMeAndParent { processForMeAndParent {
if (it is MemberScopeToFileScopeAdapter) { // todo remove hack if (it is MemberScopeToFileScopeAdapter) { // todo remove hack
return it.memberScope.getLocalVariable(name) return it.memberScope.getLocalVariable(name)
} }
else if (it !is FileScope) { // todo check this else if (it !is FileScope) { // todo check this
it.getDeclaredVariables(name, location).singleOrNull()?.let { return it } it.getDeclaredVariables(name, NoLookupLocation.UNSORTED).singleOrNull()?.let { return it }
} }
} }
return null return null
} }
public fun LexicalScope.getClassifier(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): ClassifierDescriptor? { public fun LexicalScope.getClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
processForMeAndParent { processForMeAndParent {
it.getDeclaredClassifier(name, location)?.let { return it } it.getDeclaredClassifier(name, location)?.let { return it }
} }
@@ -99,9 +115,17 @@ public fun LexicalScope.asJetScope(): JetScope {
return LexicalToJetScopeAdapter(this) return LexicalToJetScopeAdapter(this)
} }
@deprecated("Remove this method after scope refactoring")
public fun JetScope.memberScopeAsFileScope(): FileScope = MemberScopeToFileScopeAdapter(this) public fun JetScope.memberScopeAsFileScope(): FileScope = MemberScopeToFileScopeAdapter(this)
@deprecated("Remove this method after scope refactoring")
public fun JetScope.asLexicalScope(): LexicalScope
= if (this is LexicalToJetScopeAdapter) {
lexicalScope
}
else {
memberScopeAsFileScope()
}
private class LexicalToJetScopeAdapter(val lexicalScope: LexicalScope): JetScope { private class LexicalToJetScopeAdapter(val lexicalScope: LexicalScope): JetScope {
override fun getClassifier(name: Name, location: LookupLocation) = lexicalScope.getClassifier(name, location) override fun getClassifier(name: Name, location: LookupLocation) = lexicalScope.getClassifier(name, location)
@@ -191,9 +215,7 @@ private class MemberScopeToFileScopeAdapter(val memberScope: JetScope) : FileSco
override fun getDeclaredClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(name, location) override fun getDeclaredClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(name, location)
override fun getDeclaredVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> { override fun getDeclaredVariables(name: Name, location: LookupLocation) = memberScope.getProperties(name, location)
throw IllegalStateException()
}
override fun getDeclaredFunctions(name: Name, location: LookupLocation) = memberScope.getFunctions(name, location) override fun getDeclaredFunctions(name: Name, location: LookupLocation) = memberScope.getFunctions(name, location)