From 4e258df26b897866d1d178a90ed78283206d836c Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Wed, 14 Sep 2016 16:24:27 +0300 Subject: [PATCH] Extract super class info calculation to utility class, make 'signature' static --- .../codegen/ImplementationBodyCodegen.java | 42 ++++++++------- .../kotlin/codegen/SuperClassInfo.kt | 51 +++++++++++++++++++ 2 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 compiler/backend/src/org/jetbrains/kotlin/codegen/SuperClassInfo.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index 4949197f9e4..8303986acb3 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil; import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter; import org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter; import org.jetbrains.kotlin.codegen.state.GenerationState; +import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; import org.jetbrains.kotlin.lexer.KtTokens; @@ -91,8 +92,8 @@ import static org.jetbrains.org.objectweb.asm.Opcodes.*; public class ImplementationBodyCodegen extends ClassBodyCodegen { private static final String ENUM_VALUES_FIELD_NAME = "$VALUES"; private Type superClassAsmType; - @Nullable // null means java/lang/Object - private KotlinType superClassType; + @NotNull + private SuperClassInfo superClassInfo; private final Type classAsmType; private final boolean isLocal; @@ -285,17 +286,27 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { @NotNull private JvmClassSignature signature() { + return signature(descriptor, classAsmType, superClassInfo, typeMapper); + } + + @NotNull + public static JvmClassSignature signature( + @NotNull ClassDescriptor descriptor, + @NotNull Type classAsmType, + @NotNull SuperClassInfo superClassInfo, + @NotNull KotlinTypeMapper typeMapper + ) { JvmSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS); typeMapper.writeFormalTypeParameters(descriptor.getDeclaredTypeParameters(), sw); sw.writeSuperclass(); - if (superClassType == null) { - sw.writeClassBegin(superClassAsmType); + if (superClassInfo.getKotlinType() == null) { + sw.writeClassBegin(superClassInfo.getType()); sw.writeClassEnd(); } else { - typeMapper.mapSupertype(superClassType, sw); + typeMapper.mapSupertype(superClassInfo.getKotlinType(), sw); } sw.writeSuperclassEnd(); @@ -326,26 +337,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { superInterfaces.addAll(kotlinMarkerInterfaces); - return new JvmClassSignature(classAsmType.getInternalName(), superClassAsmType.getInternalName(), + return new JvmClassSignature(classAsmType.getInternalName(), superClassInfo.getType().getInternalName(), new ArrayList(superInterfaces), sw.makeJavaGenericSignature()); } - protected void getSuperClass() { - superClassAsmType = OBJECT_TYPE; - superClassType = null; - - if (descriptor.getKind() == ClassKind.INTERFACE) { - return; - } - - for (KotlinType supertype : descriptor.getTypeConstructor().getSupertypes()) { - ClassifierDescriptor superClass = supertype.getConstructor().getDeclarationDescriptor(); - if (superClass != null && !isJvmInterface(superClass)) { - superClassAsmType = typeMapper.mapClass(superClass); - superClassType = supertype; - return; - } - } + private void getSuperClass() { + superClassInfo = SuperClassInfo.getSuperClassInfo(descriptor, typeMapper); + superClassAsmType = superClassInfo.getType(); } @Override diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/SuperClassInfo.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/SuperClassInfo.kt new file mode 100644 index 00000000000..4d178d5325d --- /dev/null +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/SuperClassInfo.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.codegen + +import org.jetbrains.kotlin.codegen.JvmCodegenUtil.isJvmInterface +import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.org.objectweb.asm.Type + +class SuperClassInfo( + val type: Type, + // null means java/lang/Object + val kotlinType: KotlinType? +) { + + companion object { + @JvmStatic + fun getSuperClassInfo(descriptor: ClassDescriptor, typeMapper: KotlinTypeMapper): SuperClassInfo { + if (descriptor.kind == ClassKind.INTERFACE) { + return SuperClassInfo(OBJECT_TYPE, null) + } + + for (supertype in descriptor.typeConstructor.supertypes) { + val superClass = supertype.constructor.declarationDescriptor + if (superClass != null && !isJvmInterface(superClass)) { + return SuperClassInfo(typeMapper.mapClass(superClass), supertype) + } + } + + return SuperClassInfo(OBJECT_TYPE, null) + } + } + +} \ No newline at end of file