From 1b1fae8899317862e35fa0bedc01f24cfa5f987a Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Fri, 28 Aug 2015 17:47:50 +0300 Subject: [PATCH] Minor. Added some util methods for LexicalScope --- .../kotlin/resolve/scopes/utils/ScopeUtils.kt | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt index d87b449cc9b..a75aafcd3fc 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt @@ -73,20 +73,36 @@ public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection Boolean = { true } +): Collection { + 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") -public fun LexicalScope.getLocalVariable(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): VariableDescriptor? { +public fun LexicalScope.getLocalVariable(name: Name): VariableDescriptor? { processForMeAndParent { if (it is MemberScopeToFileScopeAdapter) { // todo remove hack return it.memberScope.getLocalVariable(name) } 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 } -public fun LexicalScope.getClassifier(name: Name, location: LookupLocation = NoLookupLocation.UNSORTED): ClassifierDescriptor? { +public fun LexicalScope.getClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? { processForMeAndParent { it.getDeclaredClassifier(name, location)?.let { return it } } @@ -99,9 +115,17 @@ public fun LexicalScope.asJetScope(): JetScope { return LexicalToJetScopeAdapter(this) } -@deprecated("Remove this method after scope refactoring") 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 { 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 getDeclaredVariables(name: Name, location: LookupLocation): Collection { - throw IllegalStateException() - } + override fun getDeclaredVariables(name: Name, location: LookupLocation) = memberScope.getProperties(name, location) override fun getDeclaredFunctions(name: Name, location: LookupLocation) = memberScope.getFunctions(name, location)