From ad3e03bdbd07b56ff500d9958582bd80d1f49dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Fri, 10 May 2019 11:51:55 +0200 Subject: [PATCH] Disable type mapper name mangling for inline classes in the JVM_IR backend --- .../kotlin/codegen/state/KotlinTypeMapper.kt | 81 ++++++++++--------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.kt index fd36f7c861f..1e0071179d0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.kt @@ -384,7 +384,7 @@ class KotlinTypeMapper @JvmOverloads constructor( resolvedCall: ResolvedCall<*>? = null ): CallableMethod { // we generate constructors of inline classes as usual functions - if (descriptor is ConstructorDescriptor && kind !== OwnerKind.ERASED_INLINE_CLASS) { + if (descriptor is ConstructorDescriptor && (kind !== OwnerKind.ERASED_INLINE_CLASS || isIrBackend)) { val method = mapSignatureSkipGeneric(descriptor.original) val owner = mapOwner(descriptor) val originalDescriptor = descriptor.original @@ -454,8 +454,10 @@ class KotlinTypeMapper @JvmOverloads constructor( } } } else { - val toInlinedErasedClass = - functionParent.isInline && (!isAccessor(functionDescriptor) || isInlineClassConstructorAccessor(functionDescriptor)) + // the IR backend handles inline classes by lowering + val toInlinedErasedClass = !isIrBackend && functionParent.isInline && + (!isAccessor(functionDescriptor) || isInlineClassConstructorAccessor(functionDescriptor)) + if (toInlinedErasedClass) { functionDescriptor = descriptor } @@ -616,34 +618,37 @@ class KotlinTypeMapper @JvmOverloads constructor( return name } - // Special methods for inline classes. - if (InlineClassDescriptorResolver.isSynthesizedBoxMethod(descriptor)) { - return BOX_JVM_METHOD_NAME - } - if (InlineClassDescriptorResolver.isSynthesizedUnboxMethod(descriptor)) { - return UNBOX_JVM_METHOD_NAME - } - if (InlineClassDescriptorResolver.isSpecializedEqualsMethod(descriptor)) { - return name - } - + // Mangle inline class methods outside of the Ir backend. var newName = name - // Constructor: - // either a constructor method for inline class (should be mangled), - // or should stay as it is (''). - if (descriptor is ConstructorDescriptor) { - if (kind === OwnerKind.ERASED_INLINE_CLASS) { - newName = JvmAbi.ERASED_INLINE_CONSTRUCTOR_NAME - } else { + if (!isIrBackend) { + // Special methods for inline classes. + if (InlineClassDescriptorResolver.isSynthesizedBoxMethod(descriptor)) { + return BOX_JVM_METHOD_NAME + } + if (InlineClassDescriptorResolver.isSynthesizedUnboxMethod(descriptor)) { + return UNBOX_JVM_METHOD_NAME + } + if (InlineClassDescriptorResolver.isSpecializedEqualsMethod(descriptor)) { return name } - } - val suffix = getInlineClassSignatureManglingSuffix(descriptor) - if (suffix != null) { - newName += suffix - } else if (kind === OwnerKind.ERASED_INLINE_CLASS) { - newName += JvmAbi.IMPL_SUFFIX_FOR_INLINE_CLASS_MEMBERS + // Constructor: + // either a constructor method for inline class (should be mangled), + // or should stay as it is (''). + if (descriptor is ConstructorDescriptor) { + if (kind === OwnerKind.ERASED_INLINE_CLASS) { + newName = JvmAbi.ERASED_INLINE_CONSTRUCTOR_NAME + } else { + return name + } + } + + val suffix = getInlineClassSignatureManglingSuffix(descriptor) + if (suffix != null) { + newName += suffix + } else if (kind === OwnerKind.ERASED_INLINE_CLASS) { + newName += JvmAbi.IMPL_SUFFIX_FOR_INLINE_CLASS_MEMBERS + } } newName = sanitizeNameIfNeeded(newName, languageVersionSettings) @@ -662,7 +667,6 @@ class KotlinTypeMapper @JvmOverloads constructor( ) { InternalNameMapper.mangleInternalName(newName, getModuleName(descriptor)) } else newName - } private fun getModuleName(descriptor: CallableMemberDescriptor): String { @@ -811,13 +815,13 @@ class KotlinTypeMapper @JvmOverloads constructor( } } else { val directMember = DescriptorUtils.getDirectMember(f) - val thisIfNeeded: KotlinType? = when (kind) { - OwnerKind.DEFAULT_IMPLS -> { + val thisIfNeeded: KotlinType? = when { + kind == OwnerKind.DEFAULT_IMPLS -> { val receiverTypeAndTypeParameters = patchTypeParametersForDefaultImplMethod(directMember) writeFormalTypeParameters(receiverTypeAndTypeParameters.typeParameters + directMember.typeParameters, sw) receiverTypeAndTypeParameters.receiverType } - OwnerKind.ERASED_INLINE_CLASS -> { + !isIrBackend && kind == OwnerKind.ERASED_INLINE_CLASS -> { (directMember.containingDeclaration as ClassDescriptor).defaultType } else -> { @@ -942,6 +946,14 @@ class KotlinTypeMapper @JvmOverloads constructor( getAllOverriddenDescriptors(descriptor).any { !isJvmPrimitive(it.returnType!!) } } + private fun isBoxMethodForInlineClass(descriptor: FunctionDescriptor): Boolean { + val containingDeclaration = descriptor.containingDeclaration + return containingDeclaration.isInlineClass() && + descriptor.kind == CallableMemberDescriptor.Kind.SYNTHESIZED && + (descriptor.name == InlineClassDescriptorResolver.BOX_METHOD_NAME || + (isIrBackend && descriptor.name.asString() == BOX_JVM_METHOD_NAME)) + } + private fun isJvmPrimitive(kotlinType: KotlinType): Boolean { if (KotlinBuiltIns.isPrimitiveType(kotlinType)) return true @@ -1613,13 +1625,6 @@ class KotlinTypeMapper @JvmOverloads constructor( return callableDescriptor is ClassConstructorDescriptor && callableDescriptor.containingDeclaration.isInlineClass() } - private fun isBoxMethodForInlineClass(descriptor: FunctionDescriptor): Boolean { - val containingDeclaration = descriptor.containingDeclaration - return containingDeclaration.isInlineClass() && - descriptor.kind == CallableMemberDescriptor.Kind.SYNTHESIZED && - descriptor.name == InlineClassDescriptorResolver.BOX_METHOD_NAME - } - private fun writeVoidReturn(sw: JvmSignatureWriter) { sw.writeReturnType() sw.writeAsmType(Type.VOID_TYPE)