Extend import resolution for library-to-source analysis

Use composite importing scope for references when resolution anchors are enabled.
Composite scope provides additional descriptors from scope of resolution anchor module.
Overriding old importing scope with a new one is not possible as it breaks library
dependencies on other libraries, which are inaccessible in anchor scope (scope for sources).

KT-24309 In Progress
This commit is contained in:
Pavel Kirpichenkov
2020-06-15 15:36:53 +03:00
parent 8c876e4621
commit 5892bdf3f4
8 changed files with 94 additions and 2 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
// see utils/ScopeUtils.kt
@@ -169,3 +170,51 @@ abstract class BaseImportingScope(parent: ImportingScope?) : BaseHierarchicalSco
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> = emptyList()
}
class CompositePrioritizedImportingScope(
private val primaryScope: ImportingScope,
private val secondaryScope: ImportingScope,
) : ImportingScope {
override val parent: ImportingScope?
get() = primaryScope.parent ?: secondaryScope.parent
override fun getContributedPackage(name: Name): PackageViewDescriptor? {
return primaryScope.getContributedPackage(name) ?: secondaryScope.getContributedPackage(name)
}
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> {
return primaryScope.getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased)
.ifEmpty { secondaryScope.getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased) }
}
override fun computeImportedNames(): Set<Name>? {
val primaryNames = primaryScope.computeImportedNames()
val secondaryNames = secondaryScope.computeImportedNames()
return primaryNames?.union(secondaryNames.orEmpty()) ?: secondaryNames
}
override fun printStructure(p: Printer) {
p.println(primaryScope::class.java.simpleName)
}
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
return primaryScope.getContributedClassifier(name, location) ?: secondaryScope.getContributedClassifier(name, location)
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> {
return primaryScope.getContributedVariables(name, location).ifEmpty {
secondaryScope.getContributedVariables(name, location)
}
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
return primaryScope.getContributedFunctions(name, location).ifEmpty {
secondaryScope.getContributedFunctions(name, location)
}
}
}