[IR] Clean up linker interface

This commit is contained in:
Roman Artemev
2020-05-06 10:39:54 +03:00
committed by romanart
parent 1e9c9ef7e0
commit 9f99094780
8 changed files with 16 additions and 46 deletions
@@ -78,8 +78,7 @@ object JvmBackendFacade {
stubGenerator.setIrProviders(irProviders)
val irModuleFragment =
psi2ir.generateModuleFragment(psi2irContext, files, irProviders, expectDescriptorToSymbol = null, pluginExtensions)
val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files, irProviders, expectDescriptorToSymbol = null)
irLinker.postProcess()
stubGenerator.unboundSymbolGeneration = true
@@ -74,8 +74,7 @@ class Psi2IrTranslator(
context: GeneratorContext,
ktFiles: Collection<KtFile>,
irProviders: List<IrProvider>,
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>? = null,
pluginExtensions: Collection<IrExtensionGenerator> = emptyList()
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>? = null
): IrModuleFragment {
val moduleGenerator = ModuleGenerator(context)
val irModule = moduleGenerator.generateModuleFragmentWithoutDependencies(ktFiles)
@@ -84,7 +83,7 @@ class Psi2IrTranslator(
expectDescriptorToSymbol?.let { referenceExpectsForUsedActuals(it, context.symbolTable, irModule) }
postprocess(context, irModule)
irProviders.filterIsInstance<IrDeserializer>().forEach { it.init(irModule, emptyList()) }
irProviders.filterIsInstance<IrDeserializer>().forEach { it.init(irModule) }
moduleGenerator.generateUnboundSymbolsAsDependencies(irProviders)
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
class IrSyntheticDeclarationGenerator(context: GeneratorContext) : IrElementVisitorVoid {
private val descriptorGenerator = SyntheticDeclarationsGenerator(context, emptyList())
private val descriptorGenerator = SyntheticDeclarationsGenerator(context)
private val symbolTable = context.symbolTable
override fun visitElement(element: IrElement) {
@@ -9,13 +9,9 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.IrExtensionGenerator
import org.jetbrains.kotlin.resolve.DescriptorUtils
class SyntheticDeclarationsGenerator(
private val context: GeneratorContext,
private val pluginProviders: Collection<IrExtensionGenerator>
) : DeclarationDescriptorVisitor<Unit, IrDeclarationContainer?> {
class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDescriptorVisitor<Unit, IrDeclarationContainer?> {
private val generator = StandaloneDeclarationGenerator(context)
private val symbolTable = context.symbolTable
@@ -30,17 +26,6 @@ class SyntheticDeclarationsGenerator(
return this
}
private inline fun <S : IrSymbol, R : IrDeclaration> generateOrDefault(symbol: S, onDefault: (S) -> R): R {
for (p in pluginProviders) {
p.declare(symbol)?.let {
@Suppress("UNCHECKED_CAST")
return it as R
}
}
return onDefault(symbol)
}
override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, data: IrDeclarationContainer?) {
error("Unexpected declaration descriptor $descriptor")
}
@@ -54,9 +39,7 @@ class SyntheticDeclarationsGenerator(
}
private fun createFunctionStub(descriptor: FunctionDescriptor, symbol: IrSimpleFunctionSymbol): IrSimpleFunction {
return generateOrDefault(symbol) {
generator.generateSimpleFunction(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateSimpleFunction(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: IrDeclarationContainer?) {
@@ -74,16 +57,13 @@ class SyntheticDeclarationsGenerator(
private fun createClassStub(descriptor: ClassDescriptor, symbol: IrClassSymbol): IrClass {
assert(!DescriptorUtils.isEnumEntry(descriptor))
return generateOrDefault(symbol) {
generator.generateClass(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateClass(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
private fun createEnumEntruStub(descriptor: ClassDescriptor, symbol: IrEnumEntrySymbol): IrEnumEntry {
assert(DescriptorUtils.isEnumEntry(descriptor))
return generateOrDefault(symbol) {
generator.generateEnumEntry(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateEnumEntry(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
override fun visitClassDescriptor(descriptor: ClassDescriptor, data: IrDeclarationContainer?) {
@@ -101,9 +81,7 @@ class SyntheticDeclarationsGenerator(
}
private fun declareTypeAliasStub(descriptor: TypeAliasDescriptor, symbol: IrTypeAliasSymbol): IrTypeAlias {
return generateOrDefault(symbol) {
generator.generateTypeAlias(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateTypeAlias(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
override fun visitTypeAliasDescriptor(descriptor: TypeAliasDescriptor, data: IrDeclarationContainer?) {
@@ -119,9 +97,7 @@ class SyntheticDeclarationsGenerator(
}
private fun createConstructorStub(descriptor: ClassConstructorDescriptor, symbol: IrConstructorSymbol): IrConstructor {
return generateOrDefault(symbol) {
generator.generateConstructor(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateConstructor(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor, data: IrDeclarationContainer?) {
@@ -137,9 +113,7 @@ class SyntheticDeclarationsGenerator(
}
private fun createPropertyStub(descriptor: PropertyDescriptor, symbol: IrPropertySymbol): IrProperty {
return generateOrDefault(symbol) {
generator.generateProperty(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
return generator.generateProperty(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
}
private fun declareAccessor(accessorDescriptor: PropertyAccessorDescriptor, property: IrProperty): IrSimpleFunction {
@@ -52,7 +52,7 @@ interface IrExtensionGenerator {
}
interface IrDeserializer : IrProvider {
fun init(moduleFragment: IrModuleFragment?, extensions: Collection<IrExtensionGenerator>) {}
fun init(moduleFragment: IrModuleFragment?) {}
}
interface ReferenceSymbolTable {
@@ -525,7 +525,7 @@ abstract class KotlinIrLinker(
protected open fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection<IrModuleDeserializer>): IrModuleDeserializer =
CurrentModuleDeserializer(moduleFragment, dependencies)
override fun init(moduleFragment: IrModuleFragment?, extensions: Collection<IrExtensionGenerator>) {
override fun init(moduleFragment: IrModuleFragment?) {
if (moduleFragment != null) {
val currentModuleDependencies = moduleFragment.descriptor.allDependencyModules.map {
deserializersForModules[it] ?: error("No deserializer found for $it")
@@ -259,7 +259,7 @@ fun loadIr(
val moduleFragment = deserializedModuleFragments.last()
irLinker.init(null, emptyList())
irLinker.init(null)
ExternalDependenciesGenerator(symbolTable, listOf(irLinker), configuration.languageVersionSettings).generateUnboundSymbolsAsDependencies()
irLinker.postProcess()
@@ -311,7 +311,7 @@ fun GeneratorContext.generateModuleFragmentWithPlugins(
}
}
return psi2Ir.generateModuleFragment(this, files, listOf(irLinker), expectDescriptorToSymbol, extensions)
return psi2Ir.generateModuleFragment(this, files, listOf(irLinker), expectDescriptorToSymbol)
}
private fun createBuiltIns(storageManager: StorageManager) = object : KotlinBuiltIns(storageManager) {}
@@ -14,8 +14,6 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.descriptors.IrAbstractFunctionFactory
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
import org.jetbrains.kotlin.ir.util.IrExtensionGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.library.IrLibrary
import org.jetbrains.kotlin.library.SerializedIrFile