[low level api] rework components structure, add ScopeSession caches

This commit is contained in:
Ilya Kirillov
2022-04-16 00:05:54 +02:00
parent 079400796a
commit 9a18598606
51 changed files with 542 additions and 435 deletions
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LLFirModuleResolveSta
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LowLevelFirApiFacadeForResolveOnAir
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.platform.TargetPlatform
@@ -116,6 +117,8 @@ private constructor(
val firSymbolProvider: FirSymbolProvider get() = rootModuleSession.symbolProvider
val targetPlatform: TargetPlatform get() = rootModuleSession.moduleData.platform
fun getScopeSessionFor(session: FirSession): ScopeSession = firResolveState.getScopeSessionFor(session)
companion object {
@InvalidWayOfUsingAnalysisSession
internal fun createAnalysisSessionByResolveState(
@@ -929,7 +929,11 @@ internal class KtFirCallResolver(
}
private fun FirClassSymbol<*>.getEqualsSymbol(equalsSymbolInAny: FirNamedFunctionSymbol): FirNamedFunctionSymbol {
val scope = unsubstitutedScope(analysisSession.rootModuleSession, ScopeSession(), false)
val scope = unsubstitutedScope(
analysisSession.rootModuleSession,
analysisSession.getScopeSessionFor(analysisSession.rootModuleSession),
false
)
var equalsSymbol: FirNamedFunctionSymbol? = null
scope.processFunctionsByName(EQUALS) { equalsSymbolFromScope ->
if (equalsSymbol != null) return@processFunctionsByName
@@ -66,7 +66,7 @@ internal class KtFirReferenceShortener(
override val token: ValidityToken,
override val firResolveState: LLFirModuleResolveState,
) : KtReferenceShortener(), KtFirAnalysisSessionComponent {
private val context = FirShorteningContext(firResolveState)
private val context = FirShorteningContext(analysisSession)
override fun collectShortenings(
file: KtFile,
@@ -163,7 +163,8 @@ private data class AvailableSymbol<out T>(
val importKind: ImportKind,
)
private class FirShorteningContext(val firResolveState: LLFirModuleResolveState) {
private class FirShorteningContext(val analysisSession: KtFirAnalysisSession) {
private val firResolveState = analysisSession.firResolveState
private val firSession: FirSession
get() = firResolveState.rootModuleSession
@@ -229,7 +230,11 @@ private class FirShorteningContext(val firResolveState: LLFirModuleResolveState)
val resolvedNewImports = newImports.mapNotNull { createFakeResolvedImport(it) }
if (resolvedNewImports.isEmpty()) return null
return FirExplicitSimpleImportingScope(resolvedNewImports, firSession, ScopeSession())
return FirExplicitSimpleImportingScope(
resolvedNewImports,
firSession,
analysisSession.getScopeSessionFor(firSession),
)
}
private fun createFakeResolvedImport(fqNameToImport: FqName): FirResolvedImport? {
@@ -28,16 +28,18 @@ internal class KtFirSamResolver(
override fun getSamConstructor(ktClassLikeSymbol: KtClassLikeSymbol): KtSamConstructorSymbol? {
val classId = ktClassLikeSymbol.classIdIfNonLocal ?: return null
val owner = analysisSession.getClassLikeSymbol(classId) as? FirRegularClass ?: return null
val resolver = LocalSamResolver(analysisSession.rootModuleSession)
val resolver = LocalSamResolver(analysisSession, analysisSession.rootModuleSession)
return resolver.getSamConstructor(owner)?.let {
analysisSession.firSymbolBuilder.functionLikeBuilder.buildSamConstructorSymbol(it.symbol)
}
}
private class LocalSamResolver(
analysisSession: KtFirAnalysisSession,
private val firSession: FirSession,
) {
private val scopeSession = ScopeSession()
private val scopeSession = analysisSession.getScopeSessionFor(firSession)
// TODO: This transformer is not intended for actual transformations and
// created here only to simplify access to SAM resolver in body resolve components
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.utils.delegateFields
import org.jetbrains.kotlin.fir.expressions.FirAnonymousObjectExpression
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticPropertiesScope
import org.jetbrains.kotlin.fir.resolve.scope
import org.jetbrains.kotlin.fir.scopes.*
@@ -53,6 +52,10 @@ internal class KtFirScopeProvider(
firResolveState: LLFirModuleResolveState,
override val token: ValidityToken,
) : KtScopeProvider(), ValidityTokenOwner {
// KtFirScopeProvider is thread local, so it's okay to use the same session here
private val scopeSession = analysisSession.getScopeSessionFor(analysisSession.rootModuleSession)
override val analysisSession: KtFirAnalysisSession by weakRef(analysisSession)
private val builder by weakRef(builder)
private val firResolveState by weakRef(firResolveState)
@@ -89,7 +92,7 @@ internal class KtFirScopeProvider(
val firSession = analysisSession.rootModuleSession
fir.unsubstitutedScope(
firSession,
ScopeSession(),
scopeSession,
withForcedTypeCalculator = false
)
} ?: return@getOrPut getEmptyScope()
@@ -100,7 +103,12 @@ internal class KtFirScopeProvider(
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope {
val firScope = symbol.withFirForScope { fir ->
fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession())
val firSession = analysisSession.rootModuleSession
fir.scopeProvider.getStaticScope(
fir,
firSession,
scopeSession,
)
} ?: return getEmptyScope()
return KtFirDelegatingScope(firScope, builder, token)
}
@@ -122,9 +130,10 @@ internal class KtFirScopeProvider(
val firScope = classSymbol.withFirForScope { fir ->
val delegateFields = fir.delegateFields
if (delegateFields.isNotEmpty()) {
val firSession = analysisSession.rootModuleSession
FirDelegatedMemberScope(
analysisSession.rootModuleSession,
ScopeSession(),
firSession,
scopeSession,
fir,
declaredScope,
delegateFields
@@ -170,7 +179,7 @@ internal class KtFirScopeProvider(
val firSession = firResolveState.rootModuleSession
val firTypeScope = type.coneType.scope(
firSession,
ScopeSession(),
scopeSession,
FakeOverrideTypeCalculator.Forced
) ?: return null
return getCompositeScope(
@@ -117,9 +117,10 @@ internal class KtFirSymbolDeclarationOverridesProvider(
val firContainer = containingDeclaration.firSymbol.fir
val firCallableDeclaration = callableSymbol.firSymbol.fir
val firSession = firContainer.moduleData.session
val firTypeScope = firContainer.unsubstitutedScope(
firContainer.moduleData.session,
ScopeSession(),
firSession,
analysisSession.getScopeSessionFor(firSession),
withForcedTypeCalculator = false
)
firTypeScope.processCallableByName(firCallableDeclaration)
@@ -25,7 +25,11 @@ internal class KtFirTypeInfoProvider(
override fun isFunctionalInterfaceType(type: KtType): Boolean {
val coneType = (type as KtFirType).coneType
val samResolver = FirSamResolverImpl(analysisSession.rootModuleSession, ScopeSession())
val firSession = analysisSession.rootModuleSession
val samResolver = FirSamResolverImpl(
firSession,
analysisSession.getScopeSessionFor(firSession),
)
return samResolver.getFunctionTypeForPossibleSamType(coneType) != null
}
@@ -54,7 +54,10 @@ internal class KtFirOverrideInfoProvider(
val parentClassFir = parentClassSymbol.firSymbol.fir as? FirClass ?: return null
return memberFir.symbol.getImplementationStatus(
SessionHolderImpl(rootModuleSession, ScopeSession()),
SessionHolderImpl(
rootModuleSession,
analysisSession.getScopeSessionFor(analysisSession.rootModuleSession),
),
parentClassFir.symbol
)
}
@@ -24,9 +24,10 @@ internal abstract class KtFirMemberSymbolPointer<S : KtSymbol>(
require(analysisSession is KtFirAnalysisSession)
val owner = analysisSession.getClassLikeSymbol(ownerClassId) as? FirRegularClass
?: return null
val firSession = analysisSession.rootModuleSession
val scope = owner.unsubstitutedScope(
analysisSession.firResolveState.rootModuleSession,
ScopeSession(),
firSession,
analysisSession.getScopeSessionFor(firSession),
withForcedTypeCalculator = false
)
return analysisSession.chooseCandidateAndCreateSymbol(scope, owner.moduleData.session)