From 71088c85ad61d2cef3b02c17521ebb4ac5f09b2d Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 9 Nov 2016 09:20:34 +0300 Subject: [PATCH] Add more runtime, avoid codegen for interfaces. (#47) * Add more runtime, avoid codegen for interfaces. * backend: fix bug in RTTIGenerator kotlin.Nothing has no supertypes * runtime: enable kotlin.Nothing --- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 6 ++ .../backend/konan/llvm/RTTIGenerator.kt | 2 +- runtime/src/main/kotlin/kotlin/Array.kt | 22 +++++ runtime/src/main/kotlin/kotlin/Arrays.kt | 21 ----- runtime/src/main/kotlin/kotlin/Enum.kt | 34 ++++++++ runtime/src/main/kotlin/kotlin/Iterator.kt | 86 +++++++++++++++++++ runtime/src/main/kotlin/kotlin/Nothing.kt | 7 ++ 7 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 runtime/src/main/kotlin/kotlin/Array.kt create mode 100644 runtime/src/main/kotlin/kotlin/Enum.kt create mode 100644 runtime/src/main/kotlin/kotlin/Iterator.kt create mode 100644 runtime/src/main/kotlin/kotlin/Nothing.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index be9720c2a3b..72f66d7dbbf 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -2,6 +2,7 @@ package org.jetbrains.kotlin.backend.konan.llvm import kotlin_native.interop.* import llvm.* +import org.jetbrains.kotlin.backend.konan.isInterface import org.jetbrains.kotlin.backend.konan.isIntrinsic import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* @@ -204,6 +205,11 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid return } + if (declaration.descriptor.isInterface) { + // Do not generate any code for interfaces. + return + } + super.visitClass(declaration) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt index 664bd5bae99..afff358e453 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt @@ -81,7 +81,7 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils { return emptyList() } - val superVtableEntries = if (KotlinBuiltIns.isAny(classDesc)) { + val superVtableEntries = if (KotlinBuiltIns.isSpecialClassWithNoSupertypes(classDesc)) { emptyList() } else { getVtableEntries(classDesc.getSuperClassOrAny()) diff --git a/runtime/src/main/kotlin/kotlin/Array.kt b/runtime/src/main/kotlin/kotlin/Array.kt new file mode 100644 index 00000000000..5b16a6df3c7 --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/Array.kt @@ -0,0 +1,22 @@ +package kotlin + +@ExportTypeInfo("theArrayTypeInfo") +class Array : Cloneable { + // Constructors are handled with compiler magic. + private constructor() {} + + public val size: Int + get() = getArrayLength() + + @SymbolName("Kotlin_Array_clone") + external public override fun clone(): Any + + @SymbolName("Kotlin_Array_get") + external public operator fun get(index: Int): T + + @SymbolName("Kotlin_Array_set") + external public operator fun set(index: Int, value: T): Unit + + @SymbolName("Kotlin_Array_getArrayLength") + external private fun getArrayLength(): Int +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 669a0f17ead..9eb10f14d60 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -1,26 +1,5 @@ package kotlin -@ExportTypeInfo("theArrayTypeInfo") -class Array : Cloneable { - // Constructors are handled with compiler magic. - private constructor() {} - - public val size: Int - get() = getArrayLength() - - @SymbolName("Kotlin_Array_clone") - external public override fun clone(): Any - - @SymbolName("Kotlin_Array_get") - external public operator fun get(index: Int): T - - @SymbolName("Kotlin_Array_set") - external public operator fun set(index: Int, value: T): Unit - - @SymbolName("Kotlin_Array_getArrayLength") - external private fun getArrayLength(): Int -} - @ExportTypeInfo("theByteArrayTypeInfo") class ByteArray : Cloneable { // Constructors are handled with compiler magic. diff --git a/runtime/src/main/kotlin/kotlin/Enum.kt b/runtime/src/main/kotlin/kotlin/Enum.kt new file mode 100644 index 00000000000..8c9d22bf3cd --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/Enum.kt @@ -0,0 +1,34 @@ +package kotlin + +/** + * The common base class of all enum classes. + * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/enum-classes.html) for more + * information on enum classes. + */ +/* +public abstract class Enum>(name: String, ordinal: Int): Comparable { + companion object {} + + /** + * Returns the name of this enum constant, exactly as declared in its enum declaration. + */ + public final val name: String + + /** + * Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant + * is assigned an ordinal of zero). + */ + public final val ordinal: Int + + public override final fun compareTo(other: E): Int + + /** + * Throws an exception since enum constants cannot be cloned. + * This method prevents enum classes from inheriting from [Cloneable]. + */ + protected final fun clone(): Any + + public override final fun equals(other: Any?): Boolean + public override final fun hashCode(): Int + public override fun toString(): String +} */ diff --git a/runtime/src/main/kotlin/kotlin/Iterator.kt b/runtime/src/main/kotlin/kotlin/Iterator.kt new file mode 100644 index 00000000000..92c2d9d70dc --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/Iterator.kt @@ -0,0 +1,86 @@ +package kotlin.collections + +/** + * An iterator over a collection or another entity that can be represented as a sequence of elements. + * Allows to sequentially access the elements. + */ +public interface Iterator { + /** + * Returns the next element in the iteration. + */ + public operator fun next(): T + + /** + * Returns `true` if the iteration has more elements. + */ + public operator fun hasNext(): Boolean +} + +/** + * An iterator over a mutable collection. Provides the ability to remove elements while iterating. + * @see MutableCollection.iterator + */ +public interface MutableIterator : Iterator { + /** + * Removes from the underlying collection the last element returned by this iterator. + */ + public fun remove(): Unit +} + +/** + * An iterator over a collection that supports indexed access. + * @see List.listIterator + */ +public interface ListIterator : Iterator { + // Query Operations + override fun next(): T + override fun hasNext(): Boolean + + /** + * Returns `true` if there are elements in the iteration before the current element. + */ + public fun hasPrevious(): Boolean + + /** + * Returns the previous element in the iteration and moves the cursor position backwards. + */ + public fun previous(): T + + /** + * Returns the index of the element that would be returned by a subsequent call to [next]. + */ + public fun nextIndex(): Int + + /** + * Returns the index of the element that would be returned by a subsequent call to [previous]. + */ + public fun previousIndex(): Int +} + +/** + * An iterator over a mutable collection that supports indexed access. Provides the ability + * to add, modify and remove elements while iterating. + */ +public interface MutableListIterator : ListIterator, MutableIterator { + // Query Operations + override fun next(): T + override fun hasNext(): Boolean + + // Modification Operations + override fun remove(): Unit + + /** + * Replaces the last element returned by [next] or [previous] with the specified element [element]. + */ + public fun set(element: T): Unit + + /** + * Adds the specified element [element] into the underlying collection immediately before the element that would be + * returned by [next], if any, and after the element that would be returned by [previous], if any. + * (If the collection contains no elements, the new element becomes the sole element in the collection.) + * The new element is inserted before the implicit cursor: a subsequent call to [next] would be unaffected, + * and a subsequent call to [previous] would return the new element. (This call increases by one the value \ + * that would be returned by a call to [nextIndex] or [previousIndex].) + */ + public fun add(element: T): Unit +} diff --git a/runtime/src/main/kotlin/kotlin/Nothing.kt b/runtime/src/main/kotlin/kotlin/Nothing.kt new file mode 100644 index 00000000000..92a1ab872d1 --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/Nothing.kt @@ -0,0 +1,7 @@ +package kotlin + +/** + * Nothing has no instances. You can use Nothing to represent "a value that never exists": for example, + * if a function has the return type of Nothing, it means that it never returns (always throws an exception). + */ +public class Nothing private constructor() {}