diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index d8578c1cfa7..e363ea451dd 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -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 diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt index 723dcad0bc2..4e7360f1ace 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/ClassCodegen.kt @@ -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) } } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/ObjectCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/ObjectCodegen.kt index f637a75467c..8124d33b724 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/ObjectCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/ObjectCodegen.kt @@ -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) } } diff --git a/translator/src/test/kotlin/tests/input/companion_object_1.txt b/translator/src/test/kotlin/tests/input/companion_object_1.txt index f66e0b5dacb..36a4313f03d 100644 --- a/translator/src/test/kotlin/tests/input/companion_object_1.txt +++ b/translator/src/test/kotlin/tests/input/companion_object_1.txt @@ -1 +1 @@ -companion_object_1() == 5 +companion_object_1_test() == 5 diff --git a/translator/src/test/kotlin/tests/input/companion_object_2.txt b/translator/src/test/kotlin/tests/input/companion_object_2.txt new file mode 100644 index 00000000000..63586168060 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/companion_object_2.txt @@ -0,0 +1 @@ +companion_object_2_test(5) == 244 diff --git a/translator/src/test/kotlin/tests/kotlin/companion_object_1.kt b/translator/src/test/kotlin/tests/kotlin/companion_object_1.kt index a24f8fd6d1a..b029625bc10 100644 --- a/translator/src/test/kotlin/tests/kotlin/companion_object_1.kt +++ b/translator/src/test/kotlin/tests/kotlin/companion_object_1.kt @@ -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() } \ No newline at end of file diff --git a/translator/src/test/kotlin/tests/kotlin/companion_object_2.kt b/translator/src/test/kotlin/tests/kotlin/companion_object_2.kt new file mode 100644 index 00000000000..752db07bb56 --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/companion_object_2.kt @@ -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 +} \ No newline at end of file