From f9afb4f95bfda91125940fc92bd19863f7908e2b Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 26 Jun 2015 21:25:20 +0300 Subject: [PATCH] Make CLASS default class kind in binary metadata Don't write the kind to the class file if it's CLASS to save some bytes --- .../codegen/ImplementationBodyCodegen.java | 22 ++++++++++++++----- ...eadKotlinClassHeaderAnnotationVisitor.java | 5 +++++ .../src/kotlin/jvm/internal/KotlinClass.java | 4 ++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index 118d894d66f..8e43cb0b89b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -239,13 +239,20 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { kind = KotlinClass.Kind.ANONYMOUS_OBJECT; } else if (isTopLevelOrInnerClass(descriptor)) { - kind = KotlinClass.Kind.CLASS; + // Default value is Kind.CLASS + kind = null; } else { // LOCAL_CLASS is also written to inner classes of local classes kind = KotlinClass.Kind.LOCAL_CLASS; } + // Temporarily write class kind anyway because old compiler may not expect its absence + // TODO: remove after M13 + if (kind == null) { + kind = KotlinClass.Kind.CLASS; + } + DescriptorSerializer serializer = DescriptorSerializer.create(descriptor, new JvmSerializerExtension(v.getSerializationBindings(), typeMapper)); @@ -257,11 +264,14 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { AnnotationVisitor av = v.getVisitor().visitAnnotation(asmDescByFqNameWithoutInnerClasses(JvmAnnotationNames.KOTLIN_CLASS), true); av.visit(JvmAnnotationNames.ABI_VERSION_FIELD_NAME, JvmAbi.VERSION); - av.visitEnum( - JvmAnnotationNames.KIND_FIELD_NAME, - Type.getObjectType(KotlinClass.KIND_INTERNAL_NAME).getDescriptor(), - kind.toString() - ); + //noinspection ConstantConditions + if (kind != null) { + av.visitEnum( + JvmAnnotationNames.KIND_FIELD_NAME, + Type.getObjectType(KotlinClass.KIND_INTERNAL_NAME).getDescriptor(), + kind.toString() + ); + } AnnotationVisitor array = av.visitArray(JvmAnnotationNames.DATA_FIELD_NAME); for (String string : BitEncoding.encodeBytes(SerializationUtil.serializeClassData(data))) { array.visit(null, string); diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/header/ReadKotlinClassHeaderAnnotationVisitor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/header/ReadKotlinClassHeaderAnnotationVisitor.java index 8d378365ded..1365f6a39b9 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/header/ReadKotlinClassHeaderAnnotationVisitor.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/header/ReadKotlinClassHeaderAnnotationVisitor.java @@ -68,6 +68,11 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor return null; } + if (headerKind == CLASS && classKind == null) { + // Default class kind is Kind.CLASS + classKind = KotlinClass.Kind.CLASS; + } + if (!AbiVersionUtil.isAbiVersionCompatible(version)) { return new KotlinClassHeader(headerKind, version, null, classKind, syntheticClassKind); } diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/KotlinClass.java b/core/runtime.jvm/src/kotlin/jvm/internal/KotlinClass.java index 8efd073d8f3..cf67e5352cc 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/KotlinClass.java +++ b/core/runtime.jvm/src/kotlin/jvm/internal/KotlinClass.java @@ -23,7 +23,7 @@ import java.lang.annotation.RetentionPolicy; public @interface KotlinClass { int abiVersion(); - Kind kind(); + Kind kind() default Kind.CLASS; String[] data(); @@ -31,7 +31,7 @@ public @interface KotlinClass { CLASS, /** - * A class has this kind if and only if its first non-class container is not a package. + * A class has kind LOCAL_CLASS if and only if it's not an anonymous object and its first non-class container is not a package. */ LOCAL_CLASS,