Extract FirAbstractTreeTransformer and *WithSuperTypes
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.resolve.transformers
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
||||
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
||||
import org.jetbrains.kotlin.fir.visitors.compose
|
||||
|
||||
abstract class FirAbstractTreeTransformer : FirTransformer<Nothing?>() {
|
||||
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return (element.transformChildren(this, data) as E).compose()
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.resolve.transformers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.FirCompositeScope
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeTypeAliasSymbol
|
||||
import org.jetbrains.kotlin.fir.types.ConeAbbreviatedType
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassErrorType
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
|
||||
abstract class FirAbstractTreeTransformerWithSuperTypes(reversedScopePriority: Boolean) : FirAbstractTreeTransformer() {
|
||||
protected var towerScope = FirCompositeScope(mutableListOf(), reversedPriority = reversedScopePriority)
|
||||
|
||||
protected inline fun <T> withScopeCleanup(crossinline l: () -> T): T {
|
||||
val scopeBefore = towerScope
|
||||
val scopes = towerScope.scopes
|
||||
val sizeBefore = scopes.size
|
||||
val result = l()
|
||||
towerScope = scopeBefore
|
||||
assert(scopes.size >= sizeBefore)
|
||||
scopes.subList(sizeBefore + 1, scopes.size).clear()
|
||||
return result
|
||||
}
|
||||
|
||||
protected fun lookupSuperTypes(klass: FirRegularClass): List<ConeClassLikeType> {
|
||||
return mutableListOf<ConeClassLikeType>().also { klass.symbol.collectSuperTypes(it) }
|
||||
}
|
||||
|
||||
private tailrec fun ConeClassLikeType.computePartialExpansion(): ConeClassLikeType? {
|
||||
return when (this) {
|
||||
is ConeAbbreviatedType -> directExpansion.takeIf { it !is ConeClassErrorType }?.computePartialExpansion()
|
||||
else -> return this
|
||||
}
|
||||
}
|
||||
|
||||
private tailrec fun ConeClassLikeSymbol.collectSuperTypes(list: MutableList<ConeClassLikeType>) {
|
||||
when (this) {
|
||||
is ConeClassSymbol -> {
|
||||
val superClassType =
|
||||
this.superTypes
|
||||
.map { it.computePartialExpansion() }
|
||||
.firstOrNull {
|
||||
it !is ConeClassErrorType && (it?.symbol as? ConeClassSymbol)?.kind == ClassKind.CLASS
|
||||
} ?: return
|
||||
list += superClassType
|
||||
superClassType.symbol.collectSuperTypes(list)
|
||||
}
|
||||
is ConeTypeAliasSymbol -> {
|
||||
val expansion = expansionType?.computePartialExpansion() ?: return
|
||||
expansion.symbol.collectSuperTypes(list)
|
||||
}
|
||||
else -> error("?!id:1")
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -13,12 +13,11 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedImportImpl
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedPackageStarImport
|
||||
import org.jetbrains.kotlin.fir.resolve.FirSymbolProvider
|
||||
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
||||
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
||||
import org.jetbrains.kotlin.fir.visitors.compose
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
class FirImportResolveTransformer() : FirTransformer<Nothing?>() {
|
||||
class FirImportResolveTransformer() : FirAbstractTreeTransformer() {
|
||||
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
|
||||
return element.compose()
|
||||
}
|
||||
|
||||
+1
-8
@@ -9,19 +9,12 @@ import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
|
||||
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
||||
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
||||
import org.jetbrains.kotlin.fir.visitors.compose
|
||||
|
||||
class FirStatusResolveTransformer : FirTransformer<Nothing?>() {
|
||||
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return (element.transformChildren(this, data) as E).compose()
|
||||
}
|
||||
|
||||
class FirStatusResolveTransformer : FirAbstractTreeTransformer() {
|
||||
private val declarationsWithStatuses = mutableListOf<FirDeclaration>()
|
||||
|
||||
private val classes = mutableListOf<FirRegularClass>()
|
||||
|
||||
+16
-69
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.resolve.transformers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
|
||||
@@ -27,33 +26,20 @@ import org.jetbrains.kotlin.fir.visitors.compose
|
||||
|
||||
open class FirTypeResolveTransformer(
|
||||
private val traversedClassifiers: Set<FirMemberDeclaration> = setOf()
|
||||
) : FirTransformer<Nothing?>() {
|
||||
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return (element.transformChildren(this, data) as E).compose()
|
||||
}
|
||||
|
||||
lateinit var scope: FirCompositeScope
|
||||
|
||||
) : FirAbstractTreeTransformerWithSuperTypes(reversedScopePriority = false) {
|
||||
override fun transformFile(file: FirFile, data: Nothing?): CompositeTransformResult<FirFile> {
|
||||
val session = file.session
|
||||
scope = FirCompositeScope(
|
||||
mutableListOf(
|
||||
// from high priority to low priority
|
||||
FirExplicitSimpleImportingScope(file.imports, session),
|
||||
FirSelfImportingScope(file.packageFqName, session),
|
||||
FirDefaultSimpleImportingScope(session),
|
||||
FirExplicitStarImportingScope(file.imports, session),
|
||||
FirDefaultStarImportingScope(session)
|
||||
)
|
||||
towerScope.scopes += listOf(
|
||||
// from high priority to low priority
|
||||
FirExplicitSimpleImportingScope(file.imports, session),
|
||||
FirSelfImportingScope(file.packageFqName, session),
|
||||
FirDefaultSimpleImportingScope(session),
|
||||
FirExplicitStarImportingScope(file.imports, session),
|
||||
FirDefaultStarImportingScope(session)
|
||||
)
|
||||
return super.transformFile(file, data)
|
||||
}
|
||||
|
||||
private fun lookupSuperTypes(klass: FirRegularClass): List<ConeClassLikeType> {
|
||||
return mutableListOf<ConeClassLikeType>().also { klass.symbol.collectSuperTypes(it) }
|
||||
}
|
||||
|
||||
private fun resolveSuperTypesAndExpansions(element: FirMemberDeclaration) {
|
||||
try {
|
||||
element.transformChildren(SuperTypeResolver(traversedClassifiers + listOfNotNull(element)), null)
|
||||
@@ -70,13 +56,13 @@ open class FirTypeResolveTransformer(
|
||||
|
||||
val firProvider = FirProvider.getInstance(regularClass.session)
|
||||
val classId = regularClass.symbol.classId
|
||||
scope.scopes += FirNestedClassifierScope(classId, firProvider)
|
||||
towerScope.scopes += FirNestedClassifierScope(classId, firProvider)
|
||||
val companionObjects = regularClass.declarations.filterIsInstance<FirRegularClass>().filter { it.isCompanion }
|
||||
for (companionObject in companionObjects) {
|
||||
scope.scopes += FirNestedClassifierScope(companionObject.symbol.classId, firProvider)
|
||||
towerScope.scopes += FirNestedClassifierScope(companionObject.symbol.classId, firProvider)
|
||||
}
|
||||
|
||||
lookupSuperTypes(regularClass).mapTo(scope.scopes) {
|
||||
lookupSuperTypes(regularClass).mapTo(towerScope.scopes) {
|
||||
val symbol = it.symbol
|
||||
if (symbol is FirBasedSymbol<*>) {
|
||||
FirNestedClassifierScope(symbol.classId, FirProvider.getInstance(symbol.fir.session))
|
||||
@@ -100,7 +86,7 @@ open class FirTypeResolveTransformer(
|
||||
|
||||
|
||||
private inline fun <T> FirMemberDeclaration.withTypeParametersScope(crossinline l: () -> T): T {
|
||||
val scopes = scope.scopes
|
||||
val scopes = towerScope.scopes
|
||||
if (typeParameters.isNotEmpty()) {
|
||||
scopes += FirMemberTypeParameterScope(this)
|
||||
}
|
||||
@@ -111,17 +97,6 @@ open class FirTypeResolveTransformer(
|
||||
return result
|
||||
}
|
||||
|
||||
private inline fun <T> withScopeCleanup(crossinline l: () -> T): T {
|
||||
val scopeBefore = scope
|
||||
val scopes = scope.scopes
|
||||
val sizeBefore = scopes.size
|
||||
val result = l()
|
||||
scope = scopeBefore
|
||||
assert(scopes.size >= sizeBefore)
|
||||
scopes.subList(sizeBefore + 1, scopes.size).clear()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun transformProperty(property: FirProperty, data: Nothing?): CompositeTransformResult<FirDeclaration> {
|
||||
return property.withTypeParametersScope {
|
||||
super.transformProperty(property, data)
|
||||
@@ -137,7 +112,7 @@ open class FirTypeResolveTransformer(
|
||||
override fun transformType(type: FirType, data: Nothing?): CompositeTransformResult<FirType> {
|
||||
val typeResolver = FirTypeResolver.getInstance(type.session)
|
||||
type.transformChildren(this, null)
|
||||
return transformType(type, typeResolver.resolveType(type, scope, position = FirPosition.OTHER))
|
||||
return transformType(type, typeResolver.resolveType(type, towerScope, position = FirPosition.OTHER))
|
||||
}
|
||||
|
||||
override fun transformFunctionType(functionType: FirFunctionType, data: Nothing?): CompositeTransformResult<FirType> {
|
||||
@@ -151,7 +126,7 @@ open class FirTypeResolveTransformer(
|
||||
functionType.receiverType,
|
||||
functionType.valueParameters as MutableList<FirValueParameter>,
|
||||
functionType.returnType,
|
||||
typeResolver.resolveType(functionType, scope, FirPosition.OTHER)
|
||||
typeResolver.resolveType(functionType, towerScope, FirPosition.OTHER)
|
||||
).compose()
|
||||
}
|
||||
|
||||
@@ -230,7 +205,7 @@ open class FirTypeResolveTransformer(
|
||||
|
||||
override fun transformType(type: FirType, data: Nothing?): CompositeTransformResult<FirType> {
|
||||
val typeResolver = FirTypeResolver.getInstance(type.session)
|
||||
val symbol = typeResolver.resolveToSymbol(type, scope, position = FirPosition.SUPER_TYPE_OR_EXPANSION)
|
||||
val symbol = typeResolver.resolveToSymbol(type, towerScope, position = FirPosition.SUPER_TYPE_OR_EXPANSION)
|
||||
val myTransformer = this@FirTypeResolveTransformer
|
||||
|
||||
if (symbol != null) {
|
||||
@@ -245,35 +220,7 @@ open class FirTypeResolveTransformer(
|
||||
|
||||
|
||||
type.transformChildren(myTransformer, null)
|
||||
return myTransformer.transformType(type, typeResolver.resolveUserType(type, symbol, scope))
|
||||
return myTransformer.transformType(type, typeResolver.resolveUserType(type, symbol, towerScope))
|
||||
}
|
||||
}
|
||||
|
||||
private tailrec fun ConeClassLikeType.computePartialExpansion(): ConeClassLikeType? {
|
||||
return when (this) {
|
||||
is ConeAbbreviatedType -> directExpansion.takeIf { it !is ConeClassErrorType }?.computePartialExpansion()
|
||||
else -> return this
|
||||
}
|
||||
}
|
||||
|
||||
private tailrec fun ConeClassLikeSymbol.collectSuperTypes(list: MutableList<ConeClassLikeType>) {
|
||||
when (this) {
|
||||
is ConeClassSymbol -> {
|
||||
val superClassType =
|
||||
this.superTypes
|
||||
.map { it.computePartialExpansion() }
|
||||
.firstOrNull {
|
||||
it !is ConeClassErrorType && (it?.symbol as? ConeClassSymbol)?.kind == ClassKind.CLASS
|
||||
} ?: return
|
||||
list += superClassType
|
||||
superClassType.symbol.collectSuperTypes(list)
|
||||
}
|
||||
is ConeTypeAliasSymbol -> {
|
||||
val expansion = expansionType?.computePartialExpansion() ?: return
|
||||
expansion.symbol.collectSuperTypes(list)
|
||||
}
|
||||
else -> error("?!id:1")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,12 +11,16 @@ import org.jetbrains.kotlin.fir.scopes.FirTypeParameterScope
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeSymbol
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class FirCompositeScope(val scopes: MutableList<FirScope>) : FirScope {
|
||||
class FirCompositeScope(
|
||||
val scopes: MutableList<FirScope>,
|
||||
private val reversedPriority: Boolean = false
|
||||
) : FirScope {
|
||||
override fun processClassifiersByName(
|
||||
name: Name,
|
||||
position: FirPosition,
|
||||
processor: (ConeSymbol) -> Boolean
|
||||
): Boolean {
|
||||
val scopes = if (reversedPriority) scopes.asReversed() else scopes
|
||||
for (scope in scopes) {
|
||||
if (!position.allowTypeParameters && scope is FirTypeParameterScope) continue
|
||||
if (!scope.processClassifiersByName(name, position, processor)) {
|
||||
|
||||
Reference in New Issue
Block a user