Type resolve transformer: reorder tower scope

This commit prioritizes inner scopes above outer scopes
(before this commit, inner scopes had lowest priority).
NB: really it's not precise enough too,
because explicit importing scope should have higher priority
than inner scopes.
Also fixes problem with total Kotlin resolve test introduced
in previous commit.
This commit is contained in:
Mikhail Glukhikh
2019-01-29 18:31:19 +03:00
parent bec62acf5b
commit 84b3a17e2b
2 changed files with 46 additions and 43 deletions
@@ -26,18 +26,21 @@ import org.jetbrains.kotlin.fir.visitors.compose
open class FirTypeResolveTransformer( open class FirTypeResolveTransformer(
private val traversedClassifiers: Set<FirMemberDeclaration> = setOf() private val traversedClassifiers: Set<FirMemberDeclaration> = setOf()
) : FirAbstractTreeTransformerWithSuperTypes(reversedScopePriority = false) { ) : FirAbstractTreeTransformerWithSuperTypes(reversedScopePriority = true) {
override fun transformFile(file: FirFile, data: Nothing?): CompositeTransformResult<FirFile> { override fun transformFile(file: FirFile, data: Nothing?): CompositeTransformResult<FirFile> {
val session = file.session val session = file.session
return withScopeCleanup {
towerScope.scopes += listOf( towerScope.scopes += listOf(
// from high priority to low priority // from low priority to high priority
FirExplicitSimpleImportingScope(file.imports, session), FirDefaultStarImportingScope(session),
FirSelfImportingScope(file.packageFqName, session),
FirDefaultSimpleImportingScope(session),
FirExplicitStarImportingScope(file.imports, session), FirExplicitStarImportingScope(file.imports, session),
FirDefaultStarImportingScope(session) FirDefaultSimpleImportingScope(session),
FirSelfImportingScope(file.packageFqName, session),
// TODO: explicit simple importing scope should have highest priority (higher than inner scopes added in process)
FirExplicitSimpleImportingScope(file.imports, session)
) )
return super.transformFile(file, data) super.transformFile(file, data)
}
} }
private fun resolveSuperTypesAndExpansions(element: FirMemberDeclaration) { private fun resolveSuperTypesAndExpansions(element: FirMemberDeclaration) {
@@ -50,19 +53,17 @@ open class FirTypeResolveTransformer(
} }
override fun transformRegularClass(regularClass: FirRegularClass, data: Nothing?): CompositeTransformResult<FirDeclaration> { override fun transformRegularClass(regularClass: FirRegularClass, data: Nothing?): CompositeTransformResult<FirDeclaration> {
return withScopeCleanup { withScopeCleanup {
regularClass.withTypeParametersScope { regularClass.addTypeParametersScope()
resolveSuperTypesAndExpansions(regularClass) resolveSuperTypesAndExpansions(regularClass)
regularClass.typeParameters.forEach {
it.accept(this, data)
}
}
return withScopeCleanup {
val firProvider = FirProvider.getInstance(regularClass.session) val firProvider = FirProvider.getInstance(regularClass.session)
val classId = regularClass.symbol.classId val classId = regularClass.symbol.classId
towerScope.scopes += FirNestedClassifierScope(classId, firProvider) lookupSuperTypes(regularClass).asReversed().mapTo(towerScope.scopes) {
val companionObjects = regularClass.declarations.filterIsInstance<FirRegularClass>().filter { it.isCompanion }
for (companionObject in companionObjects) {
towerScope.scopes += FirNestedClassifierScope(companionObject.symbol.classId, firProvider)
}
lookupSuperTypes(regularClass).mapTo(towerScope.scopes) {
val symbol = it.symbol val symbol = it.symbol
if (symbol is FirBasedSymbol<*>) { if (symbol is FirBasedSymbol<*>) {
FirNestedClassifierScope(symbol.classId, FirProvider.getInstance(symbol.fir.session)) FirNestedClassifierScope(symbol.classId, FirProvider.getInstance(symbol.fir.session))
@@ -70,41 +71,43 @@ open class FirTypeResolveTransformer(
FirNestedClassifierScope(symbol.classId, FirSymbolProvider.getInstance(regularClass.session)) FirNestedClassifierScope(symbol.classId, FirSymbolProvider.getInstance(regularClass.session))
} }
} }
super.transformRegularClass(regularClass, data) val companionObjects = regularClass.declarations.filterIsInstance<FirRegularClass>().filter { it.isCompanion }
for (companionObject in companionObjects) {
towerScope.scopes += FirNestedClassifierScope(companionObject.symbol.classId, firProvider)
} }
towerScope.scopes += FirNestedClassifierScope(classId, firProvider)
regularClass.addTypeParametersScope()
super.transformRegularClass(regularClass, data)
} }
} }
override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Nothing?): CompositeTransformResult<FirDeclaration> { override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Nothing?): CompositeTransformResult<FirDeclaration> {
// TODO: Remove comment when KT-23742 fixed return withScopeCleanup {
// Warning: boxing inline class here () typeAlias.addTypeParametersScope()
return typeAlias.withTypeParametersScope {
resolveSuperTypesAndExpansions(typeAlias) resolveSuperTypesAndExpansions(typeAlias)
super.transformTypeAlias(typeAlias, data) super.transformTypeAlias(typeAlias, data)
} }
} }
private inline fun <T> FirMemberDeclaration.withTypeParametersScope(crossinline l: () -> T): T { private fun FirMemberDeclaration.addTypeParametersScope() {
val scopes = towerScope.scopes val scopes = towerScope.scopes
if (typeParameters.isNotEmpty()) { if (typeParameters.isNotEmpty()) {
scopes += FirMemberTypeParameterScope(this) scopes += FirMemberTypeParameterScope(this)
} }
val result = l()
if (typeParameters.isNotEmpty()) {
scopes.removeAt(scopes.lastIndex)
}
return result
} }
override fun transformProperty(property: FirProperty, data: Nothing?): CompositeTransformResult<FirDeclaration> { override fun transformProperty(property: FirProperty, data: Nothing?): CompositeTransformResult<FirDeclaration> {
return property.withTypeParametersScope { return withScopeCleanup {
property.addTypeParametersScope()
super.transformProperty(property, data) super.transformProperty(property, data)
} }
} }
override fun transformNamedFunction(namedFunction: FirNamedFunction, data: Nothing?): CompositeTransformResult<FirDeclaration> { override fun transformNamedFunction(namedFunction: FirNamedFunction, data: Nothing?): CompositeTransformResult<FirDeclaration> {
return namedFunction.withTypeParametersScope { return withScopeCleanup {
namedFunction.addTypeParametersScope()
super.transformNamedFunction(namedFunction, data) super.transformNamedFunction(namedFunction, data)
} }
} }
+1 -1
View File
@@ -23,7 +23,7 @@ FILE: jvm.kt
public final function test(): R|kotlin/Unit| { public final function test(): R|kotlin/Unit| {
R|/A.foo|() R|/A.foo|()
<Unresolved name: bar>#() R|/A.bar|()
} }
} }