From c886622e8b736bfbcf6c0331162818f5601417ec Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Mon, 29 Aug 2016 12:52:55 +0300 Subject: [PATCH] translator: merge Primitives in kotstd, add exception messages to TranslatorException, add support subdirectories in kotstd --- kotstd/include/Primitives.kt | 251 ++++++++++++++++++ kotstd/include/PrimitivesByte.kt | 37 --- kotstd/include/PrimitivesChar.kt | 37 --- kotstd/include/PrimitivesDouble.kt | 36 --- kotstd/include/PrimitivesFloat.kt | 36 --- kotstd/include/PrimitivesInt.kt | 36 --- kotstd/include/PrimitivesLong.kt | 36 --- kotstd/include/PrimitivesShort.kt | 37 --- translator/src/main/kotlin/main.kt | 16 +- .../kotlinnative/translator/ClassCodegen.kt | 6 +- .../translator/TranslationState.kt | 11 +- .../translator/VariableManager.kt | 1 - .../exceptions/TranslationException.kt | 2 +- .../translator/llvm/LLVMBuilder.kt | 6 +- 14 files changed, 270 insertions(+), 278 deletions(-) create mode 100644 kotstd/include/Primitives.kt delete mode 100644 kotstd/include/PrimitivesByte.kt delete mode 100644 kotstd/include/PrimitivesChar.kt delete mode 100644 kotstd/include/PrimitivesDouble.kt delete mode 100644 kotstd/include/PrimitivesFloat.kt delete mode 100644 kotstd/include/PrimitivesInt.kt delete mode 100644 kotstd/include/PrimitivesLong.kt delete mode 100644 kotstd/include/PrimitivesShort.kt diff --git a/kotstd/include/Primitives.kt b/kotstd/include/Primitives.kt new file mode 100644 index 00000000000..3da2e500545 --- /dev/null +++ b/kotstd/include/Primitives.kt @@ -0,0 +1,251 @@ +package kotlin + +external fun kotlinclib_byteToChar(value: Byte): Char +external fun kotlinclib_byteToShort(value: Byte): Short +external fun kotlinclib_byteToInt(value: Byte): Int +external fun kotlinclib_byteToLong(value: Byte): Long +external fun kotlinclib_byteToFloat(value: Byte): Float +external fun kotlinclib_byteToDouble(value: Byte): Double + + +fun Byte.toByte(): Byte { + return this +} + +fun Byte.toInt(): Int { + return kotlinclib_byteToInt(this) +} + +fun Byte.toChar(): Char { + return kotlinclib_byteToChar(this) +} + +fun Byte.toShort(): Short { + return kotlinclib_byteToShort(this) +} + +fun Byte.toLong(): Long { + return kotlinclib_byteToLong(this) +} + +fun Byte.toFloat(): Float { + return kotlinclib_byteToFloat(this) +} + +fun Byte.toDouble(): Double { + return kotlinclib_byteToDouble(this) +} + +external fun kotlinclib_charToByte(value: Char): Byte +external fun kotlinclib_charToShort(value: Char): Short +external fun kotlinclib_charToInt(value: Char): Int +external fun kotlinclib_charToLong(value: Char): Long +external fun kotlinclib_charToFloat(value: Char): Float +external fun kotlinclib_charToDouble(value: Char): Double + + +fun Char.toByte(): Byte { + return kotlinclib_charToByte(this) +} + +fun Char.toInt(): Int { + return kotlinclib_charToInt(this) +} + +fun Char.toChar(): Char { + return this +} + +fun Char.toShort(): Short { + return kotlinclib_charToShort(this) +} + +fun Char.toLong(): Long { + return kotlinclib_charToLong(this) +} + +fun Char.toFloat(): Float { + return kotlinclib_charToFloat(this) +} + +fun Char.toDouble(): Double { + return kotlinclib_charToDouble(this) +} + +external fun kotlinclib_doubleToByte(value: Double): Byte +external fun kotlinclib_doubleToChar(value: Double): Char +external fun kotlinclib_doubleToShort(value: Double): Short +external fun kotlinclib_doubleToInt(value: Double): Int +external fun kotlinclib_doubleToLong(value: Double): Long +external fun kotlinclib_doubleToFloat(value: Double): Float + +fun Double.toByte(): Byte { + return kotlinclib_doubleToByte(this) +} + +fun Double.toChar(): Char { + return kotlinclib_doubleToChar(this) +} + +fun Double.toShort(): Short { + return kotlinclib_doubleToShort(this) +} + +fun Double.toInt(): Int { + return kotlinclib_doubleToInt(this) +} + +fun Double.toLong(): Long { + return kotlinclib_doubleToLong(this) +} + +fun Double.toFloat(): Float { + return kotlinclib_doubleToFloat(this) +} + +fun Double.toDouble(): Double { + return this +} + + +external fun kotlinclib_floatToByte(value: Float): Byte +external fun kotlinclib_floatToChar(value: Float): Char +external fun kotlinclib_floatToShort(value: Float): Short +external fun kotlinclib_floatToInt(value: Float): Int +external fun kotlinclib_floatToLong(value: Float): Long +external fun kotlinclib_floatToDouble(value: Float): Double + +fun Float.toByte(): Byte { + return kotlinclib_floatToByte(this) +} + +fun Float.toChar(): Char { + return kotlinclib_floatToChar(this) +} + +fun Float.toShort(): Short { + return kotlinclib_floatToShort(this) +} + +fun Float.toInt(): Int { + return kotlinclib_floatToInt(this) +} + +fun Float.toLong(): Long { + return kotlinclib_floatToLong(this) +} + +fun Float.toFloat(): Float { + return this +} + +fun Float.toDouble(): Double { + return kotlinclib_floatToDouble(this) +} + + +external fun kotlinclib_intToByte(value: Int): Byte +external fun kotlinclib_intToChar(value: Int): Char +external fun kotlinclib_intToShort(value: Int): Short +external fun kotlinclib_intToLong(value: Int): Long +external fun kotlinclib_intToFloat(value: Int): Float +external fun kotlinclib_intToDouble(value: Int): Double + +fun Int.toByte(): Byte { + return kotlinclib_intToByte(this) +} + +fun Int.toInt(): Int { + return this +} + +fun Int.toChar(): Char { + return kotlinclib_intToChar(this) +} + +fun Int.toShort(): Short { + return kotlinclib_intToShort(this) +} + +fun Int.toLong(): Long { + return kotlinclib_intToLong(this) +} + +fun Int.toFloat(): Float { + return kotlinclib_intToFloat(this) +} + +fun Int.toDouble(): Double { + return kotlinclib_intToDouble(this) +} + +external fun kotlinclib_longToByte(value: Long): Byte +external fun kotlinclib_longToChar(value: Long): Char +external fun kotlinclib_longToShort(value: Long): Short +external fun kotlinclib_longToInt(value: Long): Int +external fun kotlinclib_longToFloat(value: Long): Float +external fun kotlinclib_longToDouble(value: Long): Double + +fun Long.toByte(): Byte { + return kotlinclib_longToByte(this) +} + +fun Long.toLong(): Long { + return this +} + +fun Long.toChar(): Char { + return kotlinclib_longToChar(this) +} + +fun Long.toShort(): Short { + return kotlinclib_longToShort(this) +} + +fun Long.toInt(): Int { + return kotlinclib_longToInt(this) +} + +fun Long.toFloat(): Float { + return kotlinclib_longToFloat(this) +} + +fun Long.toDouble(): Double { + return kotlinclib_longToDouble(this) +} + +external fun kotlinclib_shortToByte(value: Short): Byte +external fun kotlinclib_shortToChar(value: Short): Char +external fun kotlinclib_shortToInt(value: Short): Int +external fun kotlinclib_shortToLong(value: Short): Long +external fun kotlinclib_shortToFloat(value: Short): Float +external fun kotlinclib_shortToDouble(value: Short): Double + + +fun Short.toByte(): Byte { + return kotlinclib_shortToByte(this) +} + +fun Short.toInt(): Int { + return kotlinclib_shortToInt(this) +} + +fun Short.toChar(): Char { + return kotlinclib_shortToChar(this) +} + +fun Short.toShort(): Short { + return this +} + +fun Short.toLong(): Long { + return kotlinclib_shortToLong(this) +} + +fun Short.toFloat(): Float { + return kotlinclib_shortToFloat(this) +} + +fun Short.toDouble(): Double { + return kotlinclib_shortToDouble(this) +} diff --git a/kotstd/include/PrimitivesByte.kt b/kotstd/include/PrimitivesByte.kt deleted file mode 100644 index d295a4ab1c7..00000000000 --- a/kotstd/include/PrimitivesByte.kt +++ /dev/null @@ -1,37 +0,0 @@ -package kotlin - -external fun kotlinclib_byteToChar(value: Byte): Char -external fun kotlinclib_byteToShort(value: Byte): Short -external fun kotlinclib_byteToInt(value: Byte): Int -external fun kotlinclib_byteToLong(value: Byte): Long -external fun kotlinclib_byteToFloat(value: Byte): Float -external fun kotlinclib_byteToDouble(value: Byte): Double - - -fun Byte.toByte(): Byte { - return this -} - -fun Byte.toInt(): Int { - return kotlinclib_byteToInt(this) -} - -fun Byte.toChar(): Char { - return kotlinclib_byteToChar(this) -} - -fun Byte.toShort(): Short { - return kotlinclib_byteToShort(this) -} - -fun Byte.toLong(): Long { - return kotlinclib_byteToLong(this) -} - -fun Byte.toFloat(): Float { - return kotlinclib_byteToFloat(this) -} - -fun Byte.toDouble(): Double { - return kotlinclib_byteToDouble(this) -} \ No newline at end of file diff --git a/kotstd/include/PrimitivesChar.kt b/kotstd/include/PrimitivesChar.kt deleted file mode 100644 index a105dfe2b8a..00000000000 --- a/kotstd/include/PrimitivesChar.kt +++ /dev/null @@ -1,37 +0,0 @@ -package kotlin - -external fun kotlinclib_charToByte(value: Char): Byte -external fun kotlinclib_charToShort(value: Char): Short -external fun kotlinclib_charToInt(value: Char): Int -external fun kotlinclib_charToLong(value: Char): Long -external fun kotlinclib_charToFloat(value: Char): Float -external fun kotlinclib_charToDouble(value: Char): Double - - -fun Char.toByte(): Byte { - return kotlinclib_charToByte(this) -} - -fun Char.toInt(): Int { - return kotlinclib_charToInt(this) -} - -fun Char.toChar(): Char { - return this -} - -fun Char.toShort(): Short { - return kotlinclib_charToShort(this) -} - -fun Char.toLong(): Long { - return kotlinclib_charToLong(this) -} - -fun Char.toFloat(): Float { - return kotlinclib_charToFloat(this) -} - -fun Char.toDouble(): Double { - return kotlinclib_charToDouble(this) -} diff --git a/kotstd/include/PrimitivesDouble.kt b/kotstd/include/PrimitivesDouble.kt deleted file mode 100644 index 96749f84eb1..00000000000 --- a/kotstd/include/PrimitivesDouble.kt +++ /dev/null @@ -1,36 +0,0 @@ -package kotlin - -external fun kotlinclib_doubleToByte(value: Double): Byte -external fun kotlinclib_doubleToChar(value: Double): Char -external fun kotlinclib_doubleToShort(value: Double): Short -external fun kotlinclib_doubleToInt(value: Double): Int -external fun kotlinclib_doubleToLong(value: Double): Long -external fun kotlinclib_doubleToFloat(value: Double): Float - -fun Double.toByte(): Byte { - return kotlinclib_doubleToByte(this) -} - -fun Double.toChar(): Char { - return kotlinclib_doubleToChar(this) -} - -fun Double.toShort(): Short { - return kotlinclib_doubleToShort(this) -} - -fun Double.toInt(): Int { - return kotlinclib_doubleToInt(this) -} - -fun Double.toLong(): Long { - return kotlinclib_doubleToLong(this) -} - -fun Double.toFloat(): Float { - return kotlinclib_doubleToFloat(this) -} - -fun Double.toDouble(): Double { - return this -} diff --git a/kotstd/include/PrimitivesFloat.kt b/kotstd/include/PrimitivesFloat.kt deleted file mode 100644 index 3f6fa5c3a7c..00000000000 --- a/kotstd/include/PrimitivesFloat.kt +++ /dev/null @@ -1,36 +0,0 @@ -package kotlin - -external fun kotlinclib_floatToByte(value: Float): Byte -external fun kotlinclib_floatToChar(value: Float): Char -external fun kotlinclib_floatToShort(value: Float): Short -external fun kotlinclib_floatToInt(value: Float): Int -external fun kotlinclib_floatToLong(value: Float): Long -external fun kotlinclib_floatToDouble(value: Float): Double - -fun Float.toByte(): Byte { - return kotlinclib_floatToByte(this) -} - -fun Float.toChar(): Char { - return kotlinclib_floatToChar(this) -} - -fun Float.toShort(): Short { - return kotlinclib_floatToShort(this) -} - -fun Float.toInt(): Int { - return kotlinclib_floatToInt(this) -} - -fun Float.toLong(): Long { - return kotlinclib_floatToLong(this) -} - -fun Float.toFloat(): Float { - return this -} - -fun Float.toDouble(): Double { - return kotlinclib_floatToDouble(this) -} diff --git a/kotstd/include/PrimitivesInt.kt b/kotstd/include/PrimitivesInt.kt deleted file mode 100644 index 7cc9eed9e56..00000000000 --- a/kotstd/include/PrimitivesInt.kt +++ /dev/null @@ -1,36 +0,0 @@ -package kotlin - -external fun kotlinclib_intToByte(value: Int): Byte -external fun kotlinclib_intToChar(value: Int): Char -external fun kotlinclib_intToShort(value: Int): Short -external fun kotlinclib_intToLong(value: Int): Long -external fun kotlinclib_intToFloat(value: Int): Float -external fun kotlinclib_intToDouble(value: Int): Double - -fun Int.toByte(): Byte { - return kotlinclib_intToByte(this) -} - -fun Int.toInt(): Int { - return this -} - -fun Int.toChar(): Char { - return kotlinclib_intToChar(this) -} - -fun Int.toShort(): Short { - return kotlinclib_intToShort(this) -} - -fun Int.toLong(): Long { - return kotlinclib_intToLong(this) -} - -fun Int.toFloat(): Float { - return kotlinclib_intToFloat(this) -} - -fun Int.toDouble(): Double { - return kotlinclib_intToDouble(this) -} diff --git a/kotstd/include/PrimitivesLong.kt b/kotstd/include/PrimitivesLong.kt deleted file mode 100644 index eaef3c70665..00000000000 --- a/kotstd/include/PrimitivesLong.kt +++ /dev/null @@ -1,36 +0,0 @@ -package kotlin - -external fun kotlinclib_longToByte(value: Long): Byte -external fun kotlinclib_longToChar(value: Long): Char -external fun kotlinclib_longToShort(value: Long): Short -external fun kotlinclib_longToInt(value: Long): Int -external fun kotlinclib_longToFloat(value: Long): Float -external fun kotlinclib_longToDouble(value: Long): Double - -fun Long.toByte(): Byte { - return kotlinclib_longToByte(this) -} - -fun Long.toLong(): Long { - return this -} - -fun Long.toChar(): Char { - return kotlinclib_longToChar(this) -} - -fun Long.toShort(): Short { - return kotlinclib_longToShort(this) -} - -fun Long.toInt(): Int { - return kotlinclib_longToInt(this) -} - -fun Long.toFloat(): Float { - return kotlinclib_longToFloat(this) -} - -fun Long.toDouble(): Double { - return kotlinclib_longToDouble(this) -} diff --git a/kotstd/include/PrimitivesShort.kt b/kotstd/include/PrimitivesShort.kt deleted file mode 100644 index 69830a309d1..00000000000 --- a/kotstd/include/PrimitivesShort.kt +++ /dev/null @@ -1,37 +0,0 @@ -package kotlin - -external fun kotlinclib_shortToByte(value: Short): Byte -external fun kotlinclib_shortToChar(value: Short): Char -external fun kotlinclib_shortToInt(value: Short): Int -external fun kotlinclib_shortToLong(value: Short): Long -external fun kotlinclib_shortToFloat(value: Short): Float -external fun kotlinclib_shortToDouble(value: Short): Double - - -fun Short.toByte(): Byte { - return kotlinclib_shortToByte(this) -} - -fun Short.toInt(): Int { - return kotlinclib_shortToInt(this) -} - -fun Short.toChar(): Char { - return kotlinclib_shortToChar(this) -} - -fun Short.toShort(): Short { - return this -} - -fun Short.toLong(): Long { - return kotlinclib_shortToLong(this) -} - -fun Short.toFloat(): Float { - return kotlinclib_shortToFloat(this) -} - -fun Short.toDouble(): Double { - return kotlinclib_shortToDouble(this) -} diff --git a/translator/src/main/kotlin/main.kt b/translator/src/main/kotlin/main.kt index 5d7db6ff0ed..4f442700ed0 100644 --- a/translator/src/main/kotlin/main.kt +++ b/translator/src/main/kotlin/main.kt @@ -2,7 +2,7 @@ import com.intellij.openapi.util.Disposer import com.jshmrsn.karg.parseArguments import org.kotlinnative.translator.ProjectTranslator import org.kotlinnative.translator.parseAndAnalyze -import java.io.File +import java.io.* import java.util.* fun main(args: Array) { @@ -11,8 +11,10 @@ fun main(args: Array) { val analyzedFiles = ArrayList() val stdlib = mutableListOf() + if (arguments.includeDir != null) { - stdlib.addAll(File(arguments.includeDir).listFiles().map { it.absolutePath }) + val libraryFile = File(arguments.includeDir).walk().filter { !it.isDirectory }.map { it.absolutePath } + stdlib.addAll(libraryFile) analyzedFiles.addAll(stdlib) } @@ -24,10 +26,8 @@ fun main(args: Array) { if (arguments.output == null) { println(code) - return + } else { + val output = File(arguments.output) + output.writeText(code) } - - val output = File(arguments.output) - output.writeText(code) -} - +} \ No newline at end of file diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt index f2ab5753283..b3b5b2b9514 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt @@ -1,5 +1,6 @@ package org.kotlinnative.translator +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtParameter @@ -22,6 +23,7 @@ class ClassCodegen(state: TranslationState, val annotation: Boolean val enum: Boolean var companionObjectCodegen: ObjectCodegen? = null + val descriptor: ClassDescriptor override var size: Int = 0 override val structName: String = clazz.fqName?.asString()!! @@ -34,7 +36,7 @@ class ClassCodegen(state: TranslationState, type.location.add(parentCodegen.structName) } - val descriptor = state.bindingContext.get(BindingContext.CLASS, clazz) ?: throw TranslationException() + descriptor = state.bindingContext.get(BindingContext.CLASS, clazz) ?: throw TranslationException("Can't receive descriptor of class " + clazz.name) annotation = descriptor.kind == ClassKind.ANNOTATION_CLASS enum = descriptor.kind == ClassKind.ENUM_CLASS @@ -70,10 +72,10 @@ class ClassCodegen(state: TranslationState, if (annotation) { return } + super.prepareForGenerate() nestedClasses.forEach { x, classCodegen -> classCodegen.prepareForGenerate() } - val descriptor = state.bindingContext.get(BindingContext.CLASS, clazz) ?: throw TranslationException() val companionObjectDescriptor = descriptor.companionObjectDescriptor if (companionObjectDescriptor != null) { val companionObject = clazz.getCompanionObjects().first() diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/TranslationState.kt b/translator/src/main/kotlin/org/kotlinnative/translator/TranslationState.kt index 85c67ff6857..f77541233a8 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/TranslationState.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/TranslationState.kt @@ -44,7 +44,6 @@ class TranslationState(val environment: KotlinCoreEnvironment, val bindingContex var properties = HashMap() val codeBuilder = LLVMBuilder(arm) val mainFunctions = ArrayList() - val extensionFunctions = HashMap>() } @@ -58,12 +57,10 @@ fun parseAndAnalyze(sources: List, disposer: Disposable, arm: Boolean = override fun hasErrors(): Boolean = hasError override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { - if (!severity.isError) { - return + if (severity.isError) { + System.err.println("[${severity.toString()}]${location.path} ${location.line}:${location.column} $message") + hasError = true } - - System.err.println("[${severity.toString()}]${location.path} ${location.line}:${location.column} $message") - hasError = true } } @@ -73,7 +70,7 @@ fun parseAndAnalyze(sources: List, disposer: Disposable, arm: Boolean = configuration.addKotlinSourceRoots(sources) val environment = KotlinCoreEnvironment.createForProduction(disposer, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) - val bindingContext = analyze(environment)?.bindingContext ?: throw TranslationException() + val bindingContext = analyze(environment)?.bindingContext ?: throw TranslationException("Can't initialize binding context for project") return TranslationState(environment, bindingContext, arm) } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/VariableManager.kt b/translator/src/main/kotlin/org/kotlinnative/translator/VariableManager.kt index 6f917dda40a..d0c9f990aa9 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/VariableManager.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/VariableManager.kt @@ -35,7 +35,6 @@ class VariableManager(val globalVariableCollection: HashMap