org.jetbrains.kotlin.fir.backend.Fir2IrPluginContext.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2023 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.FirIncompatiblePluginAPI
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.ir.BuiltinSymbolsBase
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.fir.backend.utils.unsubstitutedScope
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.impl.*
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.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
import org.jetbrains.kotlin.utils.addToStdlib.shouldNotBeCalled
class Fir2IrPluginContext(
private val c: Fir2IrComponents,
override val irBuiltIns: IrBuiltIns,
@property:ObsoleteDescriptorBasedAPI override val moduleDescriptor: ModuleDescriptor,
@property:ObsoleteDescriptorBasedAPI override val symbolTable: ReferenceSymbolTable,
) : IrPluginContext {
companion object {
private const val ERROR_MESSAGE = "This API is not supported for K2"
}
@ObsoleteDescriptorBasedAPI
@FirIncompatiblePluginAPI
override val bindingContext: BindingContext
get() = error(ERROR_MESSAGE)
@ObsoleteDescriptorBasedAPI
@FirIncompatiblePluginAPI
override val typeTranslator: TypeTranslator
get() = error(ERROR_MESSAGE)
override val afterK2: Boolean = true
override val languageVersionSettings: LanguageVersionSettings
get() = c.session.languageVersionSettings
override val platform: TargetPlatform
get() = c.session.moduleData.platform
override val symbols: BuiltinSymbolsBase = BuiltinSymbolsBase(irBuiltIns, symbolTable)
private val symbolProvider: FirSymbolProvider
get() = c.session.symbolProvider
override val metadataDeclarationRegistrar: Fir2IrIrGeneratedDeclarationsRegistrar
get() = c.annotationsFromPluginRegistrar
override fun referenceClass(classId: ClassId): IrClassSymbol? {
val firSymbol = symbolProvider.getClassLikeSymbolByClassId(classId) as? FirClassSymbol<*> ?: return null
return c.classifierStorage.getIrClassSymbol(firSymbol)
}
override fun referenceTypeAlias(classId: ClassId): IrTypeAliasSymbol? {
val firSymbol = symbolProvider.getClassLikeSymbolByClassId(classId) as? FirTypeAliasSymbol ?: return null
return c.classifierStorage.getIrTypeAliasSymbol(firSymbol)
}
override fun referenceConstructors(classId: ClassId): Collection {
return referenceCallableSymbols(
classId,
getCallablesFromScope = { getDeclaredConstructors() },
getCallablesFromProvider = { shouldNotBeCalled() },
Fir2IrDeclarationStorage::getIrConstructorSymbol
)
}
override fun referenceFunctions(callableId: CallableId): Collection {
return referenceCallableSymbols(
callableId.classId,
getCallablesFromScope = { getFunctions(callableId.callableName) },
getCallablesFromProvider = { getTopLevelFunctionSymbols(callableId.packageName, callableId.callableName) },
Fir2IrDeclarationStorage::getIrFunctionSymbol
)
}
override fun referenceProperties(callableId: CallableId): Collection {
return referenceCallableSymbols(
callableId.classId,
getCallablesFromScope = { getProperties(callableId.callableName).filterIsInstance() },
getCallablesFromProvider = { getTopLevelPropertySymbols(callableId.packageName, callableId.callableName) },
Fir2IrDeclarationStorage::getIrPropertySymbol
)
}
private inline fun , S : IrSymbol, reified R : S> 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(c.session)
?: return emptyList()
expandedClass.unsubstitutedScope(c).getCallablesFromScope()
} else {
symbolProvider.getCallablesFromProvider()
}
return callables.mapNotNull { c.declarationStorage.irExtractor(it) }.filterIsInstance()
}
override fun createDiagnosticReporter(pluginId: String): MessageCollector {
error(ERROR_MESSAGE)
}
@FirIncompatiblePluginAPI
override fun referenceClass(fqName: FqName): IrClassSymbol? {
error(ERROR_MESSAGE)
}
@FirIncompatiblePluginAPI
override fun referenceTypeAlias(fqName: FqName): IrTypeAliasSymbol? {
error(ERROR_MESSAGE)
}
@FirIncompatiblePluginAPI
override fun referenceConstructors(classFqn: FqName): Collection {
error(ERROR_MESSAGE)
}
@FirIncompatiblePluginAPI
override fun referenceFunctions(fqName: FqName): Collection {
error(ERROR_MESSAGE)
}
@FirIncompatiblePluginAPI
override fun referenceProperties(fqName: FqName): Collection {
error(ERROR_MESSAGE)
}
override fun referenceTopLevel(
signature: IdSignature,
kind: IrDeserializer.TopLevelSymbolKind,
moduleDescriptor: ModuleDescriptor
): IrSymbol? {
error(ERROR_MESSAGE)
}
}