translator: merge Primitives in kotstd, add exception messages to TranslatorException, add support subdirectories in kotstd

This commit is contained in:
Alexey Stepanov
2016-08-29 12:52:55 +03:00
parent cec8344c14
commit c886622e8b
14 changed files with 270 additions and 278 deletions
+251
View File
@@ -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)
}
-37
View File
@@ -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)
}
-37
View File
@@ -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)
}
-36
View File
@@ -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
}
-36
View File
@@ -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)
}
-36
View File
@@ -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)
}
-36
View File
@@ -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)
}
-37
View File
@@ -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)
}
+8 -8
View File
@@ -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<String>) {
@@ -11,8 +11,10 @@ fun main(args: Array<String>) {
val analyzedFiles = ArrayList<String>()
val stdlib = mutableListOf<String>()
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<String>) {
if (arguments.output == null) {
println(code)
return
} else {
val output = File(arguments.output)
output.writeText(code)
}
val output = File(arguments.output)
output.writeText(code)
}
}
@@ -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()
@@ -44,7 +44,6 @@ class TranslationState(val environment: KotlinCoreEnvironment, val bindingContex
var properties = HashMap<String, PropertyCodegen>()
val codeBuilder = LLVMBuilder(arm)
val mainFunctions = ArrayList<String>()
val extensionFunctions = HashMap<String, HashMap<String, FunctionCodegen>>()
}
@@ -58,12 +57,10 @@ fun parseAndAnalyze(sources: List<String>, 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<String>, 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)
}
@@ -35,7 +35,6 @@ class VariableManager(val globalVariableCollection: HashMap<String, LLVMVariable
fun receiveVariable(name: String, type: LLVMType, scope: LLVMScope, pointer: Int): LLVMVariable {
val ourVersion = variableVersion.getOrDefault(name, 0) + 1
variableVersion.put(name, ourVersion)
return LLVMVariable("managed.$name.$ourVersion", type, name, scope, pointer)
}
@@ -1,3 +1,3 @@
package org.kotlinnative.translator.exceptions
class TranslationException : Exception()
class TranslationException(message: String) : Exception(message)
@@ -13,10 +13,8 @@ class LLVMBuilder(val arm: Boolean = false) {
object UniqueGenerator {
private var unique = 0
fun generateUniqueString(): String {
unique++
return ".unique." + unique
}
fun generateUniqueString() =
".unique." + unique++
}
init {