Implement LazyImportResolver::definitelyDoesNotContainName

This commit is contained in:
Denis Zharkov
2017-08-15 19:07:45 +07:00
parent 6e766634c1
commit 3322ed6e04
9 changed files with 47 additions and 12 deletions
@@ -177,6 +177,7 @@ class FileScopeFactory(
parentScope: ImportingScope
): ImportingScope {
val scope = packageView.memberScope
val names by lazy(LazyThreadSafetyMode.PUBLICATION) { scope.computeAllNames () }
val packageName = packageView.fqName
val excludedNames = aliasImportNames.mapNotNull { if (it.parent() == packageName) it.shortName() else null }
@@ -219,6 +220,12 @@ class FileScopeFactory(
override fun computeImportedNames() = packageView.memberScope.computeAllNames()
override fun definitelyDoesNotContainName(name: Name, location: LookupLocation): Boolean {
if (names?.let { name in it } != false) return false
packageView.memberScope.recordLookup(name, location)
return true
}
override fun toString() = "Scope for current package (${filteringKind.name})"
override fun printStructure(p: Printer) {
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.util.collectionUtils.concat
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.flatMapToNullable
import java.util.*
interface IndexedImports {
@@ -81,7 +82,7 @@ class LazyImportResolver(
val indexedImports: IndexedImports,
excludedImportNames: Collection<FqName>,
private val traceForImportResolve: BindingTrace,
private val packageFragment: PackageFragmentDescriptor,
private val packageFragment: PackageFragmentDescriptor?,
val deprecationResolver: DeprecationResolver
) : ImportResolver {
private val importedScopesProvider = storageManager.createMemoizedFunctionWithNullableValues {
@@ -191,6 +192,18 @@ class LazyImportResolver(
fun getImportScope(directive: KtImportDirective): ImportingScope {
return importedScopesProvider(directive) ?: ImportingScope.Empty
}
val allNames: Set<Name>? by lazy(LazyThreadSafetyMode.PUBLICATION) {
indexedImports.imports.flatMapToNullable(hashSetOf()) { getImportScope(it).computeImportedNames() }
}
fun definitelyDoesNotContainName(name: Name, location: LookupLocation): Boolean {
if (allNames?.let { name in it } != false) return false
indexedImports.importsForName(name).forEach {
getImportScope(it).recordLookup(name, location)
}
return true
}
}
class LazyImportScope(
@@ -269,4 +282,7 @@ class LazyImportScope(
p.popIndent()
p.println("}")
}
override fun definitelyDoesNotContainName(name: Name, location: LookupLocation) = importResolver.definitelyDoesNotContainName(name, location)
override fun computeImportedNames() = importResolver.allNames
}
@@ -219,8 +219,6 @@ protected constructor(
return result.toList()
}
abstract fun recordLookup(name: Name, from: LookupLocation)
// Do not change this, override in concrete subclasses:
// it is very easy to compromise laziness of this class, and fail all the debugging
// a generic implementation can't do this properly
@@ -178,7 +178,7 @@ class TowerResolver {
}
}
}
else {
else if (scope.mayFitForName(name, location)) {
// functions with no receiver or extension for explicit receiver
TowerData.TowerLevel(ImportingScopeBasedTowerLevel(this, scope as ImportingScope)).process()?.let { return it }
}