translator: add support variables declaration
This commit is contained in:
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParenthese
|
|||||||
import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant
|
import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
|
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType
|
||||||
import org.kotlinnative.translator.llvm.*
|
import org.kotlinnative.translator.llvm.*
|
||||||
import org.kotlinnative.translator.llvm.types.*
|
import org.kotlinnative.translator.llvm.types.*
|
||||||
import java.rmi.UnexpectedException
|
import java.rmi.UnexpectedException
|
||||||
@@ -1019,30 +1020,35 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
|
|||||||
val variable = state.bindingContext.get(BindingContext.VARIABLE, element)!!
|
val variable = state.bindingContext.get(BindingContext.VARIABLE, element)!!
|
||||||
val identifier = variable.name.toString()
|
val identifier = variable.name.toString()
|
||||||
|
|
||||||
val assignExpression = evaluateExpression(element.delegateExpressionOrInitializer, scopeDepth) ?: return null
|
val assignExpression = evaluateExpression(element.delegateExpressionOrInitializer, scopeDepth)
|
||||||
|
val expectedExpressionType = LLVMMapStandardType(variable.type, state)
|
||||||
|
val assignExpressionType = assignExpression?.type
|
||||||
|
|
||||||
when (assignExpression) {
|
when (assignExpression) {
|
||||||
|
null,
|
||||||
is LLVMVariable -> {
|
is LLVMVariable -> {
|
||||||
if (assignExpression.pointer < 2) {
|
if (variable.type.isPrimitiveNumberType() || (assignExpression == null)) {
|
||||||
if (assignExpression.type is LLVMReferenceType) {
|
val allocVar = variableManager.receiveVariable(identifier, expectedExpressionType, LLVMRegisterScope(), pointer =
|
||||||
throw UnexpectedException(element.text)
|
if (variable.type.isPrimitiveNumberType()) 0 else 1)
|
||||||
}
|
|
||||||
|
|
||||||
val allocVar = variableManager.receiveVariable(identifier, assignExpression.type, LLVMRegisterScope(), pointer = 0)
|
|
||||||
codeBuilder.allocStackVar(allocVar)
|
codeBuilder.allocStackVar(allocVar)
|
||||||
allocVar.pointer++
|
allocVar.pointer++
|
||||||
allocVar.kotlinName = identifier
|
allocVar.kotlinName = identifier
|
||||||
|
|
||||||
variableManager.addVariable(identifier, allocVar, scopeDepth)
|
variableManager.addVariable(identifier, allocVar, scopeDepth)
|
||||||
|
if (assignExpression != null) {
|
||||||
|
if (assignExpression.type is LLVMReferenceType) {
|
||||||
|
throw UnexpectedException(element.text)
|
||||||
|
}
|
||||||
|
|
||||||
addPrimitiveBinaryOperation(KtTokens.EQ, null, allocVar, assignExpression)
|
addPrimitiveBinaryOperation(KtTokens.EQ, null, allocVar, assignExpression)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assignExpression.kotlinName = identifier
|
(assignExpression as LLVMVariable).kotlinName = identifier
|
||||||
variableManager.addVariable(identifier, assignExpression, scopeDepth)
|
variableManager.addVariable(identifier, assignExpression, scopeDepth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is LLVMConstant -> {
|
is LLVMConstant -> {
|
||||||
if (assignExpression.type is LLVMNullType) {
|
if (assignExpressionType!! is LLVMNullType) {
|
||||||
val reference = LLVMInstanceOfStandardType(identifier, variable.type, state = state)
|
val reference = LLVMInstanceOfStandardType(identifier, variable.type, state = state)
|
||||||
if (state.classes.containsKey(variable.type.toString().dropLast(1))) {
|
if (state.classes.containsKey(variable.type.toString().dropLast(1))) {
|
||||||
(reference.type as LLVMReferenceType).prefix = "class"
|
(reference.type as LLVMReferenceType).prefix = "class"
|
||||||
@@ -1056,10 +1062,13 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val newVar = variableManager.receiveVariable(identifier, assignExpression.type!!, LLVMRegisterScope(), pointer = 1)
|
val newVar = variableManager.receiveVariable(identifier, assignExpressionType, LLVMRegisterScope(), pointer = 1)
|
||||||
codeBuilder.addConstant(newVar, assignExpression)
|
codeBuilder.addConstant(newVar, assignExpression)
|
||||||
variableManager.addVariable(identifier, newVar, scopeDepth)
|
variableManager.addVariable(identifier, newVar, scopeDepth)
|
||||||
}
|
}
|
||||||
|
null -> {
|
||||||
|
val xrptrz = 442
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
throw UnsupportedOperationException()
|
throw UnsupportedOperationException()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
declaration_test_1_variable() == 85
|
||||||
|
declaration_test_1_class() == 96799
|
||||||
@@ -1 +1,3 @@
|
|||||||
null_comparison_1() == 87
|
null_comparison_1() == 87
|
||||||
|
null_comparison_1_reassigned() == 87
|
||||||
|
null_comparison_1_declaration() == 87
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
class declaration_test_1_cls()
|
||||||
|
|
||||||
|
fun declaration_test_1_variable(): Int {
|
||||||
|
val a: Int
|
||||||
|
a = 85
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
fun declaration_test_1_class_slave(): declaration_test_1_cls {
|
||||||
|
val a: declaration_test_1_cls
|
||||||
|
a = declaration_test_1_cls()
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun declaration_test_1_class(): Int {
|
||||||
|
return if (declaration_test_1_class_slave() == null) 10 else 96799
|
||||||
|
}
|
||||||
@@ -12,3 +12,21 @@ fun null_comparison_1(): Int {
|
|||||||
}
|
}
|
||||||
return 945
|
return 945
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun null_comparison_1_reassigned(): Int {
|
||||||
|
var a: null_comparison_1_class? = null_comparison_1_class()
|
||||||
|
a = null_comparison_1_getClass()
|
||||||
|
if (a == null) {
|
||||||
|
return 87
|
||||||
|
}
|
||||||
|
return 945
|
||||||
|
}
|
||||||
|
|
||||||
|
fun null_comparison_1_declaration(): Int {
|
||||||
|
var a: null_comparison_1_class?
|
||||||
|
a = null_comparison_1_getClass()
|
||||||
|
if (a == null) {
|
||||||
|
return 87
|
||||||
|
}
|
||||||
|
return 945
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user