[Analysis API] introduce base KtTypeScope -> KtScope converter

This commit is contained in:
Ilya Kirillov
2022-06-08 13:39:57 +02:00
parent c52d33d215
commit 7b1765cc8b
8 changed files with 84 additions and 2 deletions
@@ -57,6 +57,7 @@ class KtFe10AnalysisSession(
override val analysisScopeProviderImpl: KtAnalysisScopeProvider = KtAnalysisScopeProviderImpl(this, token)
override val referenceResolveProviderImpl: KtReferenceResolveProvider = KtFe10ReferenceResolveProvider(this)
override val substitutionProviderImpl: KtSignatureSubsitutor = KtFe10SignatureSubsitutor(this)
override val scopeSubstitutionImpl: KtScopeSubstitution = KtFe10ScopeSubstitution(this)
override fun createContextDependentCopy(originalKtFile: KtFile, elementToReanalyze: KtElement): KtAnalysisSession =
withValidityAssertion {
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2022 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.analysis.api.descriptors.components
import org.jetbrains.kotlin.analysis.api.components.KtScopeSubstitution
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtTypeScope
internal class KtFe10ScopeSubstitution(
override val analysisSession: KtFe10AnalysisSession,
) : KtScopeSubstitution(), Fe10KtAnalysisSessionComponent {
override fun getDeclarationScope(scope: KtTypeScope): KtScope {
TODO()
}
}
@@ -105,6 +105,8 @@ private constructor(
override val substitutionProviderImpl: KtSignatureSubsitutor = KtFirSignatureSubsitutor(this)
override val scopeSubstitutionImpl: KtScopeSubstitution = KtFirScopeSubstitution(this)
@Suppress("AnalysisApiMissingLifetimeCheck")
override fun createContextDependentCopy(originalKtFile: KtFile, elementToReanalyze: KtElement): KtAnalysisSession {
check(mode == AnalysisSessionMode.REGULAR) {
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 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.analysis.api.fir.components
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
import org.jetbrains.kotlin.analysis.api.components.KtScopeSubstitution
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingScope
import org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingTypeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeTypeScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtTypeScope
import org.jetbrains.kotlin.analysis.utils.errors.unexpectedElementError
internal class KtFirScopeSubstitution(
override val analysisSession: KtFirAnalysisSession,
) : KtScopeSubstitution(), KtFirAnalysisSessionComponent {
@OptIn(KtAnalysisApiInternals::class)
override fun getDeclarationScope(scope: KtTypeScope): KtScope {
return when (scope) {
is KtFirDelegatingTypeScope -> KtFirDelegatingScope(scope.firScope, analysisSession.firSymbolBuilder, token)
is KtCompositeTypeScope -> KtCompositeScope(scope.subScopes.map(::getDeclarationScope), token)
else -> unexpectedElementError<KtTypeScope>(scope)
}
}
}
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.name.Name
@KtAnalysisApiInternals
class KtCompositeTypeScope(
private val subScopes: List<KtTypeScope>,
val subScopes: List<KtTypeScope>,
override val token: KtLifetimeToken
) : KtTypeScope {
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion {
@@ -58,7 +58,8 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
KtInheritorsProviderMixIn,
KtTypeCreatorMixIn,
KtAnalysisScopeProviderMixIn,
KtSignatureSubsitutorMixIn {
KtSignatureSubsitutorMixIn,
KtScopeSubstitutionMixIn {
public abstract val useSiteModule: KtModule
@@ -147,6 +148,9 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
internal val substitutionProvider: KtSignatureSubsitutor get() = substitutionProviderImpl
protected abstract val substitutionProviderImpl: KtSignatureSubsitutor
internal val scopeSubstitution: KtScopeSubstitution get() = scopeSubstitutionImpl
protected abstract val scopeSubstitutionImpl: KtScopeSubstitution
@PublishedApi
internal val typesCreator: KtTypeCreator
get() = typesCreatorImpl
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2020 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.analysis.api.components
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtTypeScope
public abstract class KtScopeSubstitution : KtAnalysisSessionComponent() {
public abstract fun getDeclarationScope(scope: KtTypeScope): KtScope
}
public interface KtScopeSubstitutionMixIn : KtAnalysisSessionMixIn {
public fun KtTypeScope.getDeclarationScope(): KtScope =
withValidityAssertion { analysisSession.scopeSubstitution.getDeclarationScope(this) }
}
@@ -7,4 +7,8 @@ package org.jetbrains.kotlin.analysis.utils.errors
public fun unexpectedElementError(elementName: String, element: Any?): Nothing {
error("Unexpected $elementName ${element?.let { it::class.simpleName }}")
}
public inline fun <reified ELEMENT> unexpectedElementError(element: Any?): Nothing {
unexpectedElementError(ELEMENT::class.simpleName ?: ELEMENT::class.java.name, element)
}