[IR] Introduce new functions for referencing declarations from IrPluginContext

Those functions take `ClassId` and `CallableId`, which is more correct
  than using just `FqName`.

Also implement those functions for `Fir2IrPluginContext`
This commit is contained in:
Dmitriy Novozhilov
2022-06-15 15:58:12 +03:00
committed by teamcity
parent a30216c655
commit 76622097e4
4 changed files with 195 additions and 70 deletions
@@ -8,11 +8,8 @@ package org.jetbrains.kotlin.fir.backend
import org.jetbrains.kotlin.KtPsiSourceFileLinesMapping
import org.jetbrains.kotlin.KtSourceFileLinesMappingFromLineStartOffsets
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.ir.BuiltinSymbolsBase
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.backend.generators.*
import org.jetbrains.kotlin.fir.backend.generators.DataClassMembersGenerator
@@ -24,13 +21,11 @@ import org.jetbrains.kotlin.fir.extensions.declarationGenerators
import org.jetbrains.kotlin.fir.extensions.extensionService
import org.jetbrains.kotlin.fir.extensions.generatedMembers
import org.jetbrains.kotlin.fir.extensions.generatedNestedClassifiers
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.packageFqName
import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.signaturer.FirBasedSignatureComposer
import org.jetbrains.kotlin.fir.signaturer.FirMangler
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.PsiIrFileEntry
import org.jetbrains.kotlin.ir.declarations.*
@@ -39,14 +34,10 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
class Fir2IrConverter(
private val moduleDescriptor: FirModuleDescriptor,
@@ -63,7 +54,6 @@ class Fir2IrConverter(
irModuleFragment: IrModuleFragmentImpl,
irGenerationExtensions: Collection<IrGenerationExtension>,
fir2irVisitor: Fir2IrVisitor,
languageVersionSettings: LanguageVersionSettings,
fir2IrExtensions: Fir2IrExtensions,
) {
for (firFile in allFirFiles) {
@@ -106,13 +96,7 @@ class Fir2IrConverter(
evaluateConstants(irModuleFragment)
if (irGenerationExtensions.isNotEmpty()) {
val pluginContext = Fir2IrPluginContext(
languageVersionSettings,
BuiltinSymbolsBase(irBuiltIns, symbolTable),
session.moduleData.platform,
irBuiltIns,
symbolTable
)
val pluginContext = Fir2IrPluginContext(components)
for (extension in irGenerationExtensions) {
extension.generate(irModuleFragment, pluginContext)
}
@@ -474,64 +458,12 @@ class Fir2IrConverter(
}
converter.runSourcesConversion(
allFirFiles, irModuleFragment, irGenerationExtensions, fir2irVisitor, languageVersionSettings, fir2IrExtensions
allFirFiles, irModuleFragment, irGenerationExtensions, fir2irVisitor, fir2IrExtensions
)
return Fir2IrResult(irModuleFragment, components)
}
}
private class Fir2IrPluginContext(
override val languageVersionSettings: LanguageVersionSettings,
override val symbols: BuiltinSymbolsBase,
override val platform: TargetPlatform?,
override val irBuiltIns: IrBuiltIns,
override val symbolTable: SymbolTable
) : IrPluginContext {
@ObsoleteDescriptorBasedAPI
override val moduleDescriptor: ModuleDescriptor
get() = error("Should not be called")
@ObsoleteDescriptorBasedAPI
override val bindingContext: BindingContext
get() = error("Should not be called")
@ObsoleteDescriptorBasedAPI
override val typeTranslator: TypeTranslator
get() = error("Should not be called")
override fun createDiagnosticReporter(pluginId: String): IrMessageLogger {
error("Should not be called")
}
override fun referenceClass(fqName: FqName): IrClassSymbol? {
error("Should not be called")
}
override fun referenceTypeAlias(fqName: FqName): IrTypeAliasSymbol? {
error("Should not be called")
}
override fun referenceConstructors(classFqn: FqName): Collection<IrConstructorSymbol> {
error("Should not be called")
}
override fun referenceFunctions(fqName: FqName): Collection<IrSimpleFunctionSymbol> {
error("Should not be called")
}
override fun referenceProperties(fqName: FqName): Collection<IrPropertySymbol> {
error("Should not be called")
}
override fun referenceTopLevel(
signature: IdSignature,
kind: IrDeserializer.TopLevelSymbolKind,
moduleDescriptor: ModuleDescriptor
): IrSymbol? {
error("Should not be called")
}
}
}
private class WrappedDescriptorSignatureComposer(
@@ -0,0 +1,162 @@
/*
* 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.fir.backend
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.ir.BuiltinSymbolsBase
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.fir.declarations.fullyExpandedClass
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.scopes.*
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.BindingContext
class Fir2IrPluginContext(private val components: Fir2IrComponents) : IrPluginContext {
@ObsoleteDescriptorBasedAPI
override val moduleDescriptor: ModuleDescriptor
get() = error("Should not be called")
@ObsoleteDescriptorBasedAPI
override val bindingContext: BindingContext
get() = error("Should not be called")
@ObsoleteDescriptorBasedAPI
override val typeTranslator: TypeTranslator
get() = error("Should not be called")
override val languageVersionSettings: LanguageVersionSettings
get() = components.session.languageVersionSettings
override val platform: TargetPlatform
get() = components.session.moduleData.platform
override val symbolTable: ReferenceSymbolTable
get() = components.symbolTable
override val symbols: BuiltinSymbolsBase = BuiltinSymbolsBase(irBuiltIns, symbolTable)
override val irBuiltIns: IrBuiltIns
get() = components.irBuiltIns
private val symbolProvider: FirSymbolProvider
get() = components.session.symbolProvider
override fun referenceClass(classId: ClassId): IrClassSymbol? {
return referenceClassLikeSymbol(classId, symbolProvider::getClassLikeSymbolByClassId, symbolTable::referenceClass)
}
override fun referenceTypeAlias(classId: ClassId): IrTypeAliasSymbol? {
return referenceClassLikeSymbol(classId, symbolProvider::getClassLikeSymbolByClassId, symbolTable::referenceTypeAlias)
}
private inline fun <R> referenceClassLikeSymbol(
id: ClassId,
firSymbolExtractor: (ClassId) -> FirBasedSymbol<*>?,
irSymbolExtractor: (IdSignature) -> R
): R? {
val firSymbol = firSymbolExtractor(id) ?: return null
val signature = components.signatureComposer.composeSignature(firSymbol.fir) ?: return null
return irSymbolExtractor(signature)
}
override fun referenceConstructors(classId: ClassId): Collection<IrConstructorSymbol> {
return referenceCallableSymbols(
classId,
getCallablesFromScope = { getDeclaredConstructors() },
getCallablesFromProvider = { error("should not be called") },
Fir2IrDeclarationStorage::getIrConstructorSymbol
)
}
override fun referenceFunctions(callableId: CallableId): Collection<IrSimpleFunctionSymbol> {
return referenceCallableSymbols(
callableId.classId,
getCallablesFromScope = { getFunctions(callableId.callableName) },
getCallablesFromProvider = { getTopLevelFunctionSymbols(callableId.packageName, callableId.callableName) },
Fir2IrDeclarationStorage::getIrFunctionSymbol
).filterIsInstance<IrSimpleFunctionSymbol>()
}
override fun referenceProperties(callableId: CallableId): Collection<IrPropertySymbol> {
return referenceCallableSymbols(
callableId.classId,
getCallablesFromScope = { getProperties(callableId.callableName).filterIsInstance<FirPropertySymbol>() },
getCallablesFromProvider = { getTopLevelPropertySymbols(callableId.packageName, callableId.callableName) },
Fir2IrDeclarationStorage::getIrPropertySymbol
).filterIsInstance<IrPropertySymbol>()
}
private inline fun <F, S> referenceCallableSymbols(
classId: ClassId?,
getCallablesFromScope: FirTypeScope.() -> Collection<F>,
getCallablesFromProvider: FirSymbolProvider.() -> Collection<F>,
irExtractor: Fir2IrDeclarationStorage.(F) -> S?,
): Collection<S> {
val callables = if (classId != null) {
val expandedClass = symbolProvider.getClassLikeSymbolByClassId(classId)
?.fullyExpandedClass(components.session)
?: return emptyList()
expandedClass
.unsubstitutedScope(components.session, components.scopeSession, withForcedTypeCalculator = true)
.getCallablesFromScope()
} else {
symbolProvider.getCallablesFromProvider()
}
return callables.mapNotNull {
components.declarationStorage.irExtractor(it)
}
}
override fun createDiagnosticReporter(pluginId: String): IrMessageLogger {
error("Should not be called")
}
override fun referenceClass(fqName: FqName): IrClassSymbol? {
error("Should not be called")
}
override fun referenceTypeAlias(fqName: FqName): IrTypeAliasSymbol? {
error("Should not be called")
}
override fun referenceConstructors(classFqn: FqName): Collection<IrConstructorSymbol> {
error("Should not be called")
}
override fun referenceFunctions(fqName: FqName): Collection<IrSimpleFunctionSymbol> {
error("Should not be called")
}
override fun referenceProperties(fqName: FqName): Collection<IrPropertySymbol> {
error("Should not be called")
}
override fun referenceTopLevel(
signature: IdSignature,
kind: IrDeserializer.TopLevelSymbolKind,
moduleDescriptor: ModuleDescriptor
): IrSymbol? {
error("Should not be called")
}
}
@@ -16,6 +16,8 @@ import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.BindingContext
@@ -53,6 +55,13 @@ interface IrPluginContext : IrGeneratorContext {
fun referenceFunctions(fqName: FqName): Collection<IrSimpleFunctionSymbol>
fun referenceProperties(fqName: FqName): Collection<IrPropertySymbol>
// This one is experimental too
fun referenceClass(classId: ClassId): IrClassSymbol?
fun referenceTypeAlias(classId: ClassId): IrTypeAliasSymbol?
fun referenceConstructors(classId: ClassId): Collection<IrConstructorSymbol>
fun referenceFunctions(callableId: CallableId): Collection<IrSimpleFunctionSymbol>
fun referenceProperties(callableId: CallableId): Collection<IrPropertySymbol>
// temporary solution to load synthetic top-level declaration
fun referenceTopLevel(signature: IdSignature, kind: IrDeserializer.TopLevelSymbolKind, moduleDescriptor: ModuleDescriptor): IrSymbol?
}
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.BindingContext
@@ -140,6 +142,26 @@ open class IrPluginContextImpl constructor(
}
}
override fun referenceClass(classId: ClassId): IrClassSymbol? {
return referenceClass(classId.asSingleFqName())
}
override fun referenceTypeAlias(classId: ClassId): IrTypeAliasSymbol? {
return referenceTypeAlias(classId.asSingleFqName())
}
override fun referenceConstructors(classId: ClassId): Collection<IrConstructorSymbol> {
return referenceConstructors(classId.asSingleFqName())
}
override fun referenceFunctions(callableId: CallableId): Collection<IrSimpleFunctionSymbol> {
return referenceFunctions(callableId.asSingleFqName())
}
override fun referenceProperties(callableId: CallableId): Collection<IrPropertySymbol> {
return referenceProperties(callableId.asSingleFqName())
}
override fun referenceTopLevel(
signature: IdSignature,
kind: IrDeserializer.TopLevelSymbolKind,