From f038b575f1504ce5f82c94022731bfe605e63a9a Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Mon, 4 Oct 2021 13:13:29 +0300 Subject: [PATCH] FIR: Merge FirAbstractTreeTransformerWithSuperTypes and FirTypeResolveTransformer --- .../kotlin/fir/resolve/SupertypeUtils.kt | 16 ++- ...irAbstractTreeTransformerWithSuperTypes.kt | 111 ------------------ .../transformers/FirTypeResolveTransformer.kt | 79 +++++++++++-- ...irNestedClassifierScopeWithSubstitution.kt | 2 +- 4 files changed, 86 insertions(+), 122 deletions(-) delete mode 100644 compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt index ec15367a781..b116da6914f 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt @@ -11,10 +11,13 @@ import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.expandedConeType import org.jetbrains.kotlin.fir.declarations.utils.isLocal import org.jetbrains.kotlin.fir.declarations.utils.superConeTypes -import org.jetbrains.kotlin.fir.resolve.transformers.createSubstitutionForSupertype -import org.jetbrains.kotlin.fir.symbols.ensureResolved +import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic +import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind +import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor +import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.FirTypeScope +import org.jetbrains.kotlin.fir.symbols.ensureResolved import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.typeContext import org.jetbrains.kotlin.fir.types.* @@ -206,3 +209,12 @@ private fun ConeClassLikeType?.isClassBasedType( is FirRegularClassSymbol -> symbol.fir.classKind == ClassKind.CLASS } } + +fun createSubstitutionForSupertype(superType: ConeLookupTagBasedType, session: FirSession): ConeSubstitutor { + val klass = superType.lookupTag.toSymbol(session)?.fir as? FirRegularClass ?: return ConeSubstitutor.Empty + val arguments = superType.typeArguments.map { + it as? ConeKotlinType ?: ConeClassErrorType(ConeSimpleDiagnostic("illegal projection usage", DiagnosticKind.IllegalProjectionUsage)) + } + val mapping = klass.typeParameters.map { it.symbol }.zip(arguments).toMap() + return ConeSubstitutorByMap(mapping, session) +} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt deleted file mode 100644 index 999742e2cd9..00000000000 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * 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.FirSession -import org.jetbrains.kotlin.fir.declarations.* -import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic -import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind -import org.jetbrains.kotlin.fir.expressions.FirStatement -import org.jetbrains.kotlin.fir.resolve.ScopeSession -import org.jetbrains.kotlin.fir.resolve.lookupSuperTypes -import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor -import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap -import org.jetbrains.kotlin.fir.resolve.toSymbol -import org.jetbrains.kotlin.fir.scopes.FirCompositeScope -import org.jetbrains.kotlin.fir.scopes.FirScope -import org.jetbrains.kotlin.fir.scopes.getNestedClassifierScope -import org.jetbrains.kotlin.fir.scopes.impl.FirMemberTypeParameterScope -import org.jetbrains.kotlin.fir.scopes.impl.nestedClassifierScope -import org.jetbrains.kotlin.fir.scopes.impl.wrapNestedClassifierScopeWithSubstitutionForSuperType -import org.jetbrains.kotlin.fir.types.ConeClassErrorType -import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.fir.types.ConeLookupTagBasedType - -abstract class FirAbstractTreeTransformerWithSuperTypes( - phase: FirResolvePhase, - protected val scopeSession: ScopeSession -) : FirAbstractTreeTransformer(phase) { - protected val scopes = mutableListOf() - protected val classDeclarationsStack = ArrayDeque() - protected val towerScope = FirCompositeScope(scopes.asReversed()) - - protected inline fun withScopeCleanup(crossinline l: () -> T): T { - val sizeBefore = scopes.size - val result = l() - val size = scopes.size - assert(size >= sizeBefore) - repeat(size - sizeBefore) { - scopes.removeAt(scopes.lastIndex) - } - return result - } - - protected inline fun withClassDeclarationCleanup(declaration: FirRegularClass, crossinline l: () -> T): T { - withClassDeclarationCleanup(classDeclarationsStack, declaration) { - return l() - } - } - - protected fun resolveNestedClassesSupertypes( - firClass: FirClass, - data: Any? - ): FirStatement { - return withScopeCleanup { - // Otherwise annotations may try to resolve - // themselves as inner classes of the `firClass` - // if their names match - firClass.transformAnnotations(this, null) - - // ? Is it Ok to use original file session here ? - val superTypes = lookupSuperTypes( - firClass, - lookupInterfaces = false, - deep = true, - substituteTypes = true, - useSiteSession = session - ).asReversed() - for (superType in superTypes) { - superType.lookupTag.getNestedClassifierScope(session, scopeSession)?.let { nestedClassifierScope -> - val scope = nestedClassifierScope.wrapNestedClassifierScopeWithSubstitutionForSuperType(superType, session) - scopes.add(scope) - } - } - if (firClass is FirRegularClass) { - firClass.addTypeParametersScope() - val companionObject = firClass.companionObject - if (companionObject != null) { - session.nestedClassifierScope(companionObject)?.let(scopes::add) - } - } - - session.nestedClassifierScope(firClass)?.let(scopes::add) - - // Note that annotations are still visited here - // again, although there's no need in it - transformDeclarationContent(firClass, data) as FirClass - } - } - - protected fun FirMemberDeclaration.addTypeParametersScope() { - if (typeParameters.isNotEmpty()) { - scopes.add(FirMemberTypeParameterScope(this)) - } - } - - open fun transformDeclarationContent(declaration: FirDeclaration, data: Any?): FirDeclaration { - return transformElement(declaration, data) - } -} - -fun createSubstitutionForSupertype(superType: ConeLookupTagBasedType, session: FirSession): ConeSubstitutor { - val klass = superType.lookupTag.toSymbol(session)?.fir as? FirRegularClass ?: return ConeSubstitutor.Empty - val arguments = superType.typeArguments.map { - it as? ConeKotlinType ?: ConeClassErrorType(ConeSimpleDiagnostic("illegal projection usage", DiagnosticKind.IllegalProjectionUsage)) - } - val mapping = klass.typeParameters.map { it.symbol }.zip(arguments).toMap() - return ConeSubstitutorByMap(mapping, session) -} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt index d0b5cc96b05..3f7b6a58d98 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt @@ -12,8 +12,14 @@ import org.jetbrains.kotlin.fir.declarations.utils.isFromVararg import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeCyclicTypeBound +import org.jetbrains.kotlin.fir.resolve.lookupSuperTypes +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.createImportingScopes +import org.jetbrains.kotlin.fir.scopes.getNestedClassifierScope +import org.jetbrains.kotlin.fir.scopes.impl.FirMemberTypeParameterScope +import org.jetbrains.kotlin.fir.scopes.impl.nestedClassifierScope +import org.jetbrains.kotlin.fir.scopes.impl.wrapNestedClassifierScopeWithSubstitutionForSuperType import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef @@ -36,12 +42,12 @@ fun F.runTypeResolvePhaseForLocalClass( open class FirTypeResolveTransformer( final override val session: FirSession, - scopeSession: ScopeSession, + private val scopeSession: ScopeSession, initialScopes: List = emptyList() -) : FirAbstractTreeTransformerWithSuperTypes( - phase = FirResolvePhase.TYPES, - scopeSession -) { +) : FirAbstractTreeTransformer(FirResolvePhase.TYPES) { + private val classDeclarationsStack = ArrayDeque() + private val scopes = mutableListOf() + private val towerScope = FirCompositeScope(scopes.asReversed()) init { scopes.addAll(initialScopes.asReversed()) @@ -60,7 +66,7 @@ open class FirTypeResolveTransformer( } override fun transformRegularClass(regularClass: FirRegularClass, data: Any?): FirStatement { - return withClassDeclarationCleanup(regularClass) { + withClassDeclarationCleanup(classDeclarationsStack, regularClass) { withScopeCleanup { regularClass.addTypeParametersScope() regularClass.typeParameters.forEach { @@ -69,12 +75,12 @@ open class FirTypeResolveTransformer( unboundCyclesInTypeParametersSupertypes(regularClass) } - resolveNestedClassesSupertypes(regularClass, data) + return resolveClassContent(regularClass, data) } } override fun transformAnonymousObject(anonymousObject: FirAnonymousObject, data: Any?): FirStatement { - return resolveNestedClassesSupertypes(anonymousObject, data) + return resolveClassContent(anonymousObject, data) } override fun transformConstructor(constructor: FirConstructor, data: Any?): FirConstructor { @@ -208,4 +214,61 @@ open class FirTypeResolveTransformer( override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: Any?): FirStatement { return transformAnnotation(annotationCall, data) } + + private inline fun withScopeCleanup(crossinline l: () -> T): T { + val sizeBefore = scopes.size + val result = l() + val size = scopes.size + assert(size >= sizeBefore) + repeat(size - sizeBefore) { + scopes.removeAt(scopes.lastIndex) + } + return result + } + + private fun resolveClassContent( + firClass: FirClass, + data: Any? + ): FirStatement { + return withScopeCleanup { + // Otherwise annotations may try to resolve + // themselves as inner classes of the `firClass` + // if their names match + firClass.transformAnnotations(this, null) + + // ? Is it Ok to use original file session here ? + val superTypes = lookupSuperTypes( + firClass, + lookupInterfaces = false, + deep = true, + substituteTypes = true, + useSiteSession = session + ).asReversed() + for (superType in superTypes) { + superType.lookupTag.getNestedClassifierScope(session, scopeSession)?.let { nestedClassifierScope -> + val scope = nestedClassifierScope.wrapNestedClassifierScopeWithSubstitutionForSuperType(superType, session) + scopes.add(scope) + } + } + if (firClass is FirRegularClass) { + firClass.addTypeParametersScope() + val companionObject = firClass.companionObject + if (companionObject != null) { + session.nestedClassifierScope(companionObject)?.let(scopes::add) + } + } + + session.nestedClassifierScope(firClass)?.let(scopes::add) + + // Note that annotations are still visited here + // again, although there's no need in it + transformElement(firClass, data) as FirClass + } + } + + private fun FirMemberDeclaration.addTypeParametersScope() { + if (typeParameters.isNotEmpty()) { + scopes.add(FirMemberTypeParameterScope(this)) + } + } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirNestedClassifierScopeWithSubstitution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirNestedClassifierScopeWithSubstitution.kt index 265931be227..051a4846a1b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirNestedClassifierScopeWithSubstitution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirNestedClassifierScopeWithSubstitution.kt @@ -7,8 +7,8 @@ package org.jetbrains.kotlin.fir.scopes.impl import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.utils.isInner +import org.jetbrains.kotlin.fir.resolve.createSubstitutionForSupertype import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor -import org.jetbrains.kotlin.fir.resolve.transformers.createSubstitutionForSupertype import org.jetbrains.kotlin.fir.scopes.* import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.ConeClassLikeType