From 1e9c9ef7e0a2ea92a0ccc85380821644c6593716 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Wed, 29 Apr 2020 20:07:51 +0300 Subject: [PATCH] [IR] Clean up linker code --- .../serialization/IrModuleDeserializer.kt | 72 +------------------ .../common/serialization/KotlinIrLinker.kt | 7 +- .../js/lower/serialization/ir/JsIrLinker.kt | 8 +-- .../backend/jvm/serialization/JvmIrLinker.kt | 9 ++- 4 files changed, 12 insertions(+), 84 deletions(-) diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IrModuleDeserializer.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IrModuleDeserializer.kt index ed0a3aca733..a59966f4acd 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IrModuleDeserializer.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IrModuleDeserializer.kt @@ -8,15 +8,12 @@ package org.jetbrains.kotlin.backend.common.serialization import org.jetbrains.kotlin.backend.common.serialization.encodings.BinarySymbolData import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.descriptors.IrAbstractFunctionFactory import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.WrappedDeclarationDescriptor import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.util.IdSignature -import org.jetbrains.kotlin.ir.util.IrExtensionGenerator -import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.library.IrLibrary import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name @@ -205,9 +202,7 @@ class IrModuleDeserializerWithBuiltIns( open class CurrentModuleDeserializer( override val moduleFragment: IrModuleFragment, - override val moduleDependencies: Collection, - private val symbolTable: SymbolTable, - private val extensions: Collection + override val moduleDependencies: Collection ) : IrModuleDeserializer(moduleFragment.descriptor) { override fun contains(idSig: IdSignature): Boolean = false // TODO: @@ -215,68 +210,5 @@ open class CurrentModuleDeserializer( error("Unreachable execution: there could not be back-links") } - override fun declareIrSymbol(symbol: IrSymbol) { - declareIrSymbolImpl(symbol) - } - - private fun referenceParentDescriptor(descriptor: DeclarationDescriptor): IrSymbol { - return when (descriptor) { - is ClassDescriptor -> symbolTable.referenceClass(descriptor) - is PropertyDescriptor -> symbolTable.referenceProperty(descriptor) - is PackageFragmentDescriptor -> moduleFragment.files.single { it.symbol.descriptor === descriptor }.symbol - else -> error("Unexpected declaration parent $descriptor") - } - } - - private fun declareIrDeclaration(symbol: IrSymbol): IrDeclaration { - for (extension in extensions) { - extension.declare(symbol)?.let { return it } - } - - return declareIrDeclarationDefault(symbol) - } - - private fun declareIrDeclarationDefault(symbol: IrSymbol): IrDeclaration { - return when (symbol) { - is IrClassSymbol -> symbolTable.declareClass(offset, offset, IrDeclarationOrigin.DEFINED, symbol.descriptor) - is IrConstructorSymbol -> symbolTable.declareConstructor(offset, offset, IrDeclarationOrigin.DEFINED, symbol.descriptor) - is IrSimpleFunctionSymbol -> symbolTable.declareSimpleFunction(offset, offset, IrDeclarationOrigin.DEFINED, symbol.descriptor) - is IrPropertySymbol -> symbolTable.declareProperty(offset, offset, IrDeclarationOrigin.DEFINED, symbol.descriptor) - is IrTypeAliasSymbol -> TODO("Implement type alias $symbol") - is IrEnumEntrySymbol -> symbolTable.declareEnumEntry(offset, offset, IrDeclarationOrigin.DEFINED, symbol.descriptor) - else -> error("Unexpected symbol $symbol") - } - } - - private fun declareIrSymbolImpl(symbol: IrSymbol): IrSymbolOwner { - if (symbol.isBound) return symbol.owner - val descriptor = symbol.descriptor - - assert(descriptor !is WrappedDeclarationDescriptor<*>) - - val accessor = descriptor as? PropertyAccessorDescriptor - - val parent = descriptor.containingDeclaration ?: error("Expect non-root declaration $descriptor") - val parentDeclaration = declareIrSymbolImpl(referenceParentDescriptor(parent)) as IrDeclarationContainer - - val declaredDeclaration = declareIrDeclaration(symbol).also { - it.parent = parentDeclaration - if (accessor != null) { - val property = accessor.correspondingProperty - val irProperty = declareIrSymbolImpl(referenceParentDescriptor(property)) as IrProperty - val irAccessor = it as IrSimpleFunction - irAccessor.correspondingPropertySymbol = irProperty.symbol - if (accessor === property.getter) irProperty.getter = irAccessor - if (accessor === property.setter) irProperty.setter = irAccessor - } else { - parentDeclaration.declarations.add(it) - } - } - - return declaredDeclaration as IrSymbolOwner - } - - companion object { - private const val offset = UNDEFINED_OFFSET - } + override fun declareIrSymbol(symbol: IrSymbol) {} } \ No newline at end of file diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIrLinker.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIrLinker.kt index e25bfc66ad7..fff608d0631 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIrLinker.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIrLinker.kt @@ -491,6 +491,7 @@ abstract class KotlinIrLinker( moduleDeserializer.declareIrSymbol(symbol) deserializeAllReachableTopLevels() + if (!symbol.isBound) return null return descriptor } @@ -521,15 +522,15 @@ abstract class KotlinIrLinker( return symbol.owner as IrDeclaration } - protected open fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection, extensions: Collection): IrModuleDeserializer = - CurrentModuleDeserializer(moduleFragment, dependencies, symbolTable, extensions) + protected open fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection): IrModuleDeserializer = + CurrentModuleDeserializer(moduleFragment, dependencies) override fun init(moduleFragment: IrModuleFragment?, extensions: Collection) { if (moduleFragment != null) { val currentModuleDependencies = moduleFragment.descriptor.allDependencyModules.map { deserializersForModules[it] ?: error("No deserializer found for $it") } - val currentModuleDeserializer = createCurrentModuleDeserializer(moduleFragment, currentModuleDependencies, extensions) + val currentModuleDeserializer = createCurrentModuleDeserializer(moduleFragment, currentModuleDependencies) deserializersForModules[moduleFragment.descriptor] = maybeWrapWithBuiltInAndInit(moduleFragment.descriptor, currentModuleDeserializer) } diff --git a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/lower/serialization/ir/JsIrLinker.kt b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/lower/serialization/ir/JsIrLinker.kt index 549eaefec77..afe4cf25f38 100644 --- a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/lower/serialization/ir/JsIrLinker.kt +++ b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/lower/serialization/ir/JsIrLinker.kt @@ -36,12 +36,8 @@ class JsIrLinker( private inner class JsModuleDeserializer(moduleDescriptor: ModuleDescriptor, klib: IrLibrary, strategy: DeserializationStrategy) : KotlinIrLinker.BasicIrModuleDeserializer(moduleDescriptor, klib, strategy) - override fun createCurrentModuleDeserializer( - moduleFragment: IrModuleFragment, - dependencies: Collection, - extensions: Collection - ): IrModuleDeserializer { - val currentModuleDeserializer = super.createCurrentModuleDeserializer(moduleFragment, dependencies, extensions) + override fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection): IrModuleDeserializer { + val currentModuleDeserializer = super.createCurrentModuleDeserializer(moduleFragment, dependencies) icData?.let { return CurrentModuleWithICDeserializer(currentModuleDeserializer, symbolTable, builtIns, it) { lib -> JsModuleDeserializer(currentModuleDeserializer.moduleDescriptor, lib, currentModuleDeserializer.strategy) diff --git a/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/ir/backend/jvm/serialization/JvmIrLinker.kt b/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/ir/backend/jvm/serialization/JvmIrLinker.kt index 8cbdfe018fb..4ce0df6a4d0 100644 --- a/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/ir/backend/jvm/serialization/JvmIrLinker.kt +++ b/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/ir/backend/jvm/serialization/JvmIrLinker.kt @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator import org.jetbrains.kotlin.ir.util.IdSignature -import org.jetbrains.kotlin.ir.util.IrExtensionGenerator import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.library.IrLibrary import org.jetbrains.kotlin.load.java.descriptors.* @@ -76,11 +75,11 @@ class JvmIrLinker(currentModule: ModuleDescriptor?, logger: LoggingContext, buil } - override fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection, extensions: Collection): IrModuleDeserializer = - JvmCurrentModuleDeserializer(moduleFragment, dependencies, extensions) + override fun createCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection): IrModuleDeserializer = + JvmCurrentModuleDeserializer(moduleFragment, dependencies) - private inner class JvmCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection, extensions: Collection) : - CurrentModuleDeserializer(moduleFragment, dependencies, symbolTable, extensions) { + private inner class JvmCurrentModuleDeserializer(moduleFragment: IrModuleFragment, dependencies: Collection) : + CurrentModuleDeserializer(moduleFragment, dependencies) { override fun declareIrSymbol(symbol: IrSymbol) { val descriptor = symbol.descriptor