diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt index bfa80f769f5..0232276b987 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt @@ -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, 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 { - error("Should not be called") - } - - override fun referenceFunctions(fqName: FqName): Collection { - error("Should not be called") - } - - override fun referenceProperties(fqName: FqName): Collection { - error("Should not be called") - } - - override fun referenceTopLevel( - signature: IdSignature, - kind: IrDeserializer.TopLevelSymbolKind, - moduleDescriptor: ModuleDescriptor - ): IrSymbol? { - error("Should not be called") - } - } } private class WrappedDescriptorSignatureComposer( diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrPluginContext.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrPluginContext.kt new file mode 100644 index 00000000000..0ac55e83a52 --- /dev/null +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrPluginContext.kt @@ -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 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 { + return referenceCallableSymbols( + classId, + getCallablesFromScope = { getDeclaredConstructors() }, + getCallablesFromProvider = { error("should not be called") }, + Fir2IrDeclarationStorage::getIrConstructorSymbol + ) + } + + override fun referenceFunctions(callableId: CallableId): Collection { + return referenceCallableSymbols( + callableId.classId, + getCallablesFromScope = { getFunctions(callableId.callableName) }, + getCallablesFromProvider = { getTopLevelFunctionSymbols(callableId.packageName, callableId.callableName) }, + Fir2IrDeclarationStorage::getIrFunctionSymbol + ).filterIsInstance() + } + + override fun referenceProperties(callableId: CallableId): Collection { + return referenceCallableSymbols( + callableId.classId, + getCallablesFromScope = { getProperties(callableId.callableName).filterIsInstance() }, + getCallablesFromProvider = { getTopLevelPropertySymbols(callableId.packageName, callableId.callableName) }, + Fir2IrDeclarationStorage::getIrPropertySymbol + ).filterIsInstance() + } + + private inline fun referenceCallableSymbols( + classId: ClassId?, + getCallablesFromScope: FirTypeScope.() -> Collection, + getCallablesFromProvider: FirSymbolProvider.() -> Collection, + irExtractor: Fir2IrDeclarationStorage.(F) -> S?, + ): Collection { + 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 { + error("Should not be called") + } + + override fun referenceFunctions(fqName: FqName): Collection { + error("Should not be called") + } + + override fun referenceProperties(fqName: FqName): Collection { + error("Should not be called") + } + + override fun referenceTopLevel( + signature: IdSignature, + kind: IrDeserializer.TopLevelSymbolKind, + moduleDescriptor: ModuleDescriptor + ): IrSymbol? { + error("Should not be called") + } +} diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContext.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContext.kt index d90625d9d91..a2fafe9ee4f 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContext.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContext.kt @@ -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 fun referenceProperties(fqName: FqName): Collection + // This one is experimental too + fun referenceClass(classId: ClassId): IrClassSymbol? + fun referenceTypeAlias(classId: ClassId): IrTypeAliasSymbol? + fun referenceConstructors(classId: ClassId): Collection + fun referenceFunctions(callableId: CallableId): Collection + fun referenceProperties(callableId: CallableId): Collection + // temporary solution to load synthetic top-level declaration fun referenceTopLevel(signature: IdSignature, kind: IrDeserializer.TopLevelSymbolKind, moduleDescriptor: ModuleDescriptor): IrSymbol? } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContextImpl.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContextImpl.kt index ad0406d65f9..53c314c8b4e 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContextImpl.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/extensions/IrPluginContextImpl.kt @@ -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 { + return referenceConstructors(classId.asSingleFqName()) + } + + override fun referenceFunctions(callableId: CallableId): Collection { + return referenceFunctions(callableId.asSingleFqName()) + } + + override fun referenceProperties(callableId: CallableId): Collection { + return referenceProperties(callableId.asSingleFqName()) + } + override fun referenceTopLevel( signature: IdSignature, kind: IrDeserializer.TopLevelSymbolKind,