translator: fix for null return from function

This commit is contained in:
Alexey Stepanov
2016-08-17 16:41:08 +03:00
parent 8062c0a9f1
commit cb248253c8
4 changed files with 29 additions and 2 deletions
@@ -816,6 +816,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
firstOp.type!!.operatorNeq(firstPointedArgument, secondPointedArgument)
}
KtTokens.EQ -> {
codeBuilder.addComment("start variable assignment")
if (secondOp.type is LLVMNullType) {
val result = codeBuilder.getNewVariable(firstOp.type!!, firstOp.pointer)
codeBuilder.allocStaticVar(result)
@@ -828,6 +829,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
val result = firstOp as LLVMVariable
val sourceArgument = if (result.pointer == secondOp.pointer + 1) secondOp else secondNativeOp
codeBuilder.storeVariable(result, sourceArgument)
codeBuilder.addComment("end variable assignment")
return result
}
else -> addPrimitiveReferenceOperation(referenceName!!, firstOp, secondNativeOp)
@@ -1059,13 +1061,20 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
private fun evaluateReturnInstruction(element: PsiElement, scopeDepth: Int): LLVMVariable? {
val next = element.getNextSiblingIgnoringWhitespaceAndComments()
val retVar = evaluateExpression(next, scopeDepth)
var retVar = evaluateExpression(next, scopeDepth)
val type = retVar?.type ?: LLVMVoidType()
when (type) {
is LLVMReferenceType -> genReferenceReturn(retVar!!)
is LLVMVoidType,
is LLVMNullType -> {
retVar = when (retVar) {
is LLVMConstant -> LLVMConstant(retVar.value, LLVMNullType(returnType!!.type), returnType!!.pointer - 1)
is LLVMVariable -> LLVMVariable(retVar.label, LLVMNullType(returnType!!.type), retVar.kotlinName, retVar.scope, returnType!!.pointer - 1)
else -> throw IllegalStateException()
}
genReferenceReturn(retVar)
}
is LLVMVoidType -> {
codeBuilder.addAnyReturn(LLVMVoidType())
}
else -> {
@@ -16,7 +16,10 @@ class LLVMNullType(var basetype: LLVMType? = null) : LLVMReferred, LLVMType() {
return other is LLVMNullType
}
override fun parseArg(inputArg: String) = "null"
override fun hashCode() =
"null".hashCode()
override fun toString() = basetype?.toString() ?: ""
}
@@ -0,0 +1 @@
null_comparison_1() == 87
@@ -0,0 +1,14 @@
class null_comparison_1_class
fun null_comparison_1_getClass(): null_comparison_1_class? {
return null
}
fun null_comparison_1(): Int {
val a: null_comparison_1_class? = null_comparison_1_getClass()
println(if (a == null) 555 else 9990)
if (a == null) {
return 87
}
return 945
}