From f61072873fa850e272f3b76eddb778928e3a5134 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Wed, 9 Jan 2019 18:56:09 +0300 Subject: [PATCH] remove descriptors from createInnerClassConstructorWithOuterThisParameter --- .../kotlin/backend/jvm/JvmBackendContext.kt | 2 +- .../jvm/descriptors/JvmDeclarationFactory.kt | 96 ++++++++----------- 2 files changed, 40 insertions(+), 58 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index 9c492cf5b90..8fbe2d9cd59 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -38,7 +38,7 @@ class JvmBackendContext( irModuleFragment: IrModuleFragment, symbolTable: SymbolTable ) : CommonBackendContext { override val builtIns = state.module.builtIns - override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(state, symbolTable) + override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(state) override val sharedVariablesManager = JvmSharedVariablesManager(builtIns, irBuiltIns) // TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt index 7c1bc9c3b47..317e7b984f1 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt @@ -5,8 +5,14 @@ package org.jetbrains.kotlin.backend.jvm.descriptors +import org.jetbrains.kotlin.backend.common.deepCopyWithWrappedDescriptors +import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassConstructorDescriptor import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassDescriptor +import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor import org.jetbrains.kotlin.backend.common.ir.DeclarationFactory +import org.jetbrains.kotlin.backend.common.ir.copyTo +import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom +import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.lower.createStaticFunctionWithReceivers import org.jetbrains.kotlin.builtins.CompanionObjectMapping.isMappedIntrinsicCompanionObject @@ -14,7 +20,6 @@ import org.jetbrains.kotlin.codegen.OwnerKind import org.jetbrains.kotlin.codegen.state.GenerationState import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.declarations.* @@ -27,7 +32,6 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.toIrType import org.jetbrains.kotlin.ir.types.toKotlinType import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.load.java.JavaVisibilities @@ -37,8 +41,7 @@ import org.jetbrains.org.objectweb.asm.Opcodes import java.util.* class JvmDeclarationFactory( - private val state: GenerationState, - private val symbolTable: SymbolTable + private val state: GenerationState ) : DeclarationFactory { private val singletonFieldDeclarations = HashMap() private val outerThisDeclarations = HashMap() @@ -63,7 +66,7 @@ class JvmDeclarationFactory( if (!innerClass.isInner) throw AssertionError("Class is not inner: ${innerClass.dump()}") else outerThisDeclarations.getOrPut(innerClass) { val outerClass = innerClass.parent as? IrClass - ?: throw AssertionError("No containing class for inner class ${innerClass.dump()}") + ?: throw AssertionError("No containing class for inner class ${innerClass.dump()}") val symbol = IrFieldSymbolImpl( JvmPropertyDescriptorImpl.createFinalField( @@ -86,60 +89,38 @@ class JvmDeclarationFactory( } private fun createInnerClassConstructorWithOuterThisParameter(oldConstructor: IrConstructor): IrConstructor { - val oldDescriptor = oldConstructor.descriptor - val classDescriptor = oldDescriptor.containingDeclaration - val outerThisType = (classDescriptor.containingDeclaration as ClassDescriptor).defaultType - - val newDescriptor = ClassConstructorDescriptorImpl.createSynthesized( - classDescriptor, oldDescriptor.annotations, oldDescriptor.isPrimary, oldDescriptor.source - ) - - val outerThisValueParameter = newDescriptor.createValueParameter(0, "\$outer", outerThisType) - - val newValueParameters = - listOf(outerThisValueParameter) + - oldDescriptor.valueParameters.map { it.copy(newDescriptor, it.name, it.index + 1) } - // Call the long version of `initialize()`, because otherwise default implementation inserts - // an unwanted `dispatchReceiverParameter` - newDescriptor.initialize( - oldDescriptor.extensionReceiverParameter?.copy(newDescriptor), - null, - oldDescriptor.typeParameters, - newValueParameters, - oldDescriptor.returnType, - oldDescriptor.modality, - oldDescriptor.visibility - ) - val symbol = IrConstructorSymbolImpl(newDescriptor) + val newDescriptor = WrappedClassConstructorDescriptor(oldConstructor.descriptor.annotations) return IrConstructorImpl( - oldConstructor.startOffset, - oldConstructor.endOffset, - oldConstructor.origin, - symbol, - oldConstructor.returnType - ).also { constructor -> - newValueParameters.mapIndexedTo(constructor.valueParameters) { i, v -> - constructor.parent = oldConstructor.parent - if (i == 0) { - IrValueParameterImpl( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, - JvmLoweredDeclarationOrigin.FIELD_FOR_OUTER_THIS, - IrValueParameterSymbolImpl(v), - outerThisType.toIrType(symbolTable)!!, - null - ) - } else { - val oldParameter = oldConstructor.valueParameters[i - 1] - IrValueParameterImpl( - oldParameter.startOffset, oldParameter.endOffset, oldParameter.origin, - IrValueParameterSymbolImpl(v), oldParameter.type, oldParameter.varargElementType - ).also { - it.defaultValue = oldParameter.defaultValue - } - } - } + oldConstructor.startOffset, oldConstructor.endOffset, oldConstructor.origin, + IrConstructorSymbolImpl(newDescriptor), + oldConstructor.name, oldConstructor.visibility, oldConstructor.returnType, + oldConstructor.isInline, oldConstructor.isExternal, oldConstructor.isPrimary + ).apply { + newDescriptor.bind(this) + annotations.addAll(oldConstructor.annotations.map { it.deepCopyWithWrappedDescriptors(this) }) + parent = oldConstructor.parent + returnType = oldConstructor.returnType + copyTypeParametersFrom(oldConstructor) - constructor.metadata = oldConstructor.metadata + val outerThisType = oldConstructor.parentAsClass.parentAsClass.defaultType + val outerThisDescriptor = WrappedValueParameterDescriptor() + val outerThisValueParameter = IrValueParameterImpl( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_OUTER_THIS, + IrValueParameterSymbolImpl(outerThisDescriptor), + Name.identifier("\$outer"), + 0, + type = outerThisType, + varargElementType = null, + isCrossinline = false, + isNoinline = false + ).also { + outerThisDescriptor.bind(it) + it.parent = this + } + valueParameters.add(outerThisValueParameter) + + oldConstructor.valueParameters.mapTo(valueParameters) { it.copyTo(this, index = it.index + 1) } + metadata = oldConstructor.metadata } } @@ -223,6 +204,7 @@ class JvmDeclarationFactory( ).apply { descriptor.bind(this) parent = interfaceClass + createImplicitParameterDeclarationWithWrappedDescriptor() } } }