translator: add fields in companion objects, fix nested class resolve

This commit is contained in:
Alexey Stepanov
2016-07-21 11:02:47 +03:00
parent 500e36995c
commit 8f908e3dd5
7 changed files with 32 additions and 9 deletions
@@ -111,8 +111,12 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
val fieldName = state.bindingContext.get(BindingContext.REFERENCE_TARGET, expr)!!.name.toString()
val field = classScope!!.companionFieldsIndex[fieldName]
val companionObject = classScope.companionFieldsSource[fieldName]
val receiver = variableManager.getLLVMvalue(companionObject!!.fullName)!!
val result = codeBuilder.getNewVariable(field!!.type, pointer = 1)
codeBuilder.loadClassField(result, field, field.offset)
codeBuilder.loadClassField(result, receiver, field.offset)
return result
}
@@ -150,6 +154,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
var i = 1
while (i < type.location.size) {
codegen = codegen.nestedClasses[type.location[i]]!!
i++
}
return codegen.nestedClasses[type.type]!!
@@ -167,7 +172,8 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
private fun evaluateReferenceExpression(expr: KtReferenceExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? = when (expr) {
is KtArrayAccessExpression -> evaluateArrayAccessExpression(expr, scopeDepth + 1)
else -> variableManager.getLLVMvalue(expr.firstChild.text)
else -> if ((expr is KtNameReferenceExpression) && (classScope != null)) evaluatenameReferenceExpression(expr, scopeDepth + 1, classScope)
else variableManager.getLLVMvalue(expr.firstChild.text)
}
private fun evaluateCallExpression(expr: KtCallExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? {
val function = expr.firstChild.firstChild.text
@@ -198,7 +204,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM
if (classDescriptor.companionMethods.containsKey(methodShortName)) {
val descriptor = classDescriptor.companionMethods[methodShortName] ?: return null
val parentDescriptor = descriptor.parentCodegen!!
val receiver = variableManager.getLLVMvalue(parentDescriptor.structName)!!
val receiver = variableManager.getLLVMvalue(parentDescriptor.fullName)!!
val methodFullName = descriptor.name
val returnType = descriptor.returnType!!.type
@@ -89,9 +89,11 @@ class ClassCodegen(override val state: TranslationState,
for (method in property.methods) {
val methodName = method.key.removePrefix(companionObjectName + ".")
companionMethods.put(structName + "." + methodName, method.value)
companionFieldsSource.put(structName + "." + methodName, property)
}
companionFields.addAll(property.fields)
for (field in property.fields) {
companionFieldsSource.put(field.label, property)
}
companionFieldsIndex.putAll(property.fieldsIndex)
}
}
@@ -32,6 +32,6 @@ class ObjectCodegen(override val state: TranslationState,
generate(objectDeclaration.declarations)
val classInstance = LLVMVariable("object.instance.$structName", type, objectDeclaration.name, LLVMVariableScope(), pointer = 1)
codeBuilder.addGlobalIntialize(classInstance, type)
variableManager.addGlobalVariable(structName, classInstance)
variableManager.addGlobalVariable(fullName, classInstance)
}
}
@@ -1 +1 @@
companion_object_1() == 5
companion_object_1_test() == 5
@@ -0,0 +1 @@
companion_object_2_test(5) == 244
@@ -1,4 +1,4 @@
class companion_object {
class companion_object_1 {
companion object Factory {
val fieldCompanion: Int = 5790
@@ -9,6 +9,6 @@ class companion_object {
}
fun companion_object_1(): Int {
return companion_object.create()
fun companion_object_1_test(): Int {
return companion_object_1.create()
}
@@ -0,0 +1,14 @@
class companion_object_2 {
companion object Factory {
var fieldCompanion: Int = 5790
fun create(): Int {
return 5
}
}
}
fun companion_object_2_test(v: Int): Int {
companion_object_2.fieldCompanion = 239 + v
return companion_object_2.fieldCompanion
}