[KT-4124] Add test case for nested/inner classes inside native class. Add diagnostic of inner classes inside native classes.

This commit is contained in:
Alexey Andreev
2016-02-15 17:55:31 +03:00
parent 040a646174
commit f5786dd567
17 changed files with 176 additions and 70 deletions
@@ -204,35 +204,27 @@ object ConstructorCallCase : FunctionCallCase() {
}
override fun FunctionCallInfo.noReceivers(): JsExpression {
val fqName = context.getQualifiedReference(callableDescriptor)
val functionRef = if (isNative()) fqName else context.aliasOrValue(callableDescriptor) { fqName }
val constructorDescriptor = callableDescriptor as ConstructorDescriptor
if (constructorDescriptor.isPrimary || AnnotationsUtils.isNativeObject(constructorDescriptor)) {
return JsNew(functionRef, argumentsInfo.translateArguments)
}
else {
return JsInvocation(functionRef, argumentsInfo.translateArguments)
}
return receiver { it }
}
override fun FunctionCallInfo.dispatchReceiver(): JsExpression {
return receiver {
val receiver = superCallReceiver
if (receiver != null) (sequenceOf(receiver) + it).toList() else it
}
}
private inline fun FunctionCallInfo.receiver(argumentTranslator: (List<JsExpression>) -> List<JsExpression>): JsExpression {
val fqName = context.getQualifiedReference(callableDescriptor)
val functionRef = context.aliasOrValue(callableDescriptor) { fqName }
val functionRef = if (isNative()) fqName else context.aliasOrValue(callableDescriptor) { fqName }
val arguments = argumentTranslator(argumentsInfo.translateArguments)
val constructorDescriptor = callableDescriptor as ConstructorDescriptor
val receiver = superCallReceiver
var allArguments = when (receiver) {
null -> argumentsInfo.translateArguments
else -> (sequenceOf(receiver) + argumentsInfo.translateArguments).toList()
}
if (constructorDescriptor.isPrimary || AnnotationsUtils.isNativeObject(constructorDescriptor)) {
return JsNew(functionRef, allArguments)
return JsNew(functionRef, arguments)
}
else {
return JsInvocation(functionRef, allArguments)
return JsInvocation(functionRef, arguments)
}
}
}
@@ -65,6 +65,8 @@ public final class Namer {
public static final String CALL_FUNCTION = "call";
private static final String APPLY_FUNCTION = "apply";
public static final String OUTER_FIELD_NAME = "$outer";
private static final String CLASS_OBJECT_NAME = "createClass";
private static final String ENUM_CLASS_OBJECT_NAME = "createEnumClass";
private static final String TRAIT_OBJECT_NAME = "createTrait";
@@ -421,14 +421,15 @@ public class TranslationContext {
}
@Nullable
private static ClassDescriptor getNearestClass(@Nullable DeclarationDescriptor declaration) {
while (declaration != null) {
if (declaration instanceof ClassDescriptor) {
if (!DescriptorUtils.isAnonymousObject(declaration) && !DescriptorUtils.isObject(declaration)) {
return (ClassDescriptor) declaration;
private static ClassDescriptor getNearestClass(@NotNull DeclarationDescriptor declaration) {
DeclarationDescriptor decl = declaration;
while (decl != null) {
if (decl instanceof ClassDescriptor) {
if (!DescriptorUtils.isAnonymousObject(decl) && !DescriptorUtils.isObject(decl)) {
return (ClassDescriptor) decl;
}
}
declaration = declaration.getContainingDeclaration();
decl = decl.getContainingDeclaration();
}
return null;
}
@@ -445,14 +446,8 @@ public class TranslationContext {
return descriptor;
}
public boolean hasEnclosingFunction() {
public boolean isLocal() {
DeclarationDescriptor descriptor = declarationDescriptor;
while (descriptor != null) {
if (descriptor instanceof FunctionDescriptor) {
return true;
}
descriptor = descriptor.getContainingDeclaration();
}
return false;
return descriptor != null && DescriptorUtils.isLocal(descriptor);
}
}
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.backend.common.bridges.generateBridgesForFunctionDes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.js.descriptorUtils.hasPrimaryConstructor
import org.jetbrains.kotlin.js.resolve.diagnostics.ErrorsJs
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator
import org.jetbrains.kotlin.js.translate.context.DefinitionPlace
import org.jetbrains.kotlin.js.translate.context.Namer
@@ -48,7 +47,6 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils.*
import org.jetbrains.kotlin.types.CommonSupertypes.topologicallySortSuperclassesAndRecordAllInstances
import org.jetbrains.kotlin.types.KotlinType
@@ -79,11 +77,6 @@ class ClassTranslator private constructor(
private fun isTrait(): Boolean = descriptor.kind == ClassKind.INTERFACE
private fun getClassCreateInvocationArguments(declarationContext: TranslationContext): List<JsExpression> {
if (!DescriptorUtils.isAnonymousObject(descriptor) && !DescriptorUtils.isObject(descriptor) &&
declarationContext.hasEnclosingFunction()) {
declarationContext.bindingTrace().report(ErrorsJs.NOT_SUPPORTED.on(classDeclaration, classDeclaration))
return emptyList()
}
var context = declarationContext
val invocationArguments = ArrayList<JsExpression>()
@@ -188,7 +181,7 @@ class ClassTranslator private constructor(
return emptyList()
}
if (supertypes.size == 1) {
val type = supertypes.get(0)
val type = supertypes[0]
val supertypeDescriptor = getClassDescriptorForType(type)
return listOf<JsExpression>(getClassReference(supertypeDescriptor))
}
@@ -124,7 +124,7 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
}
// TODO: avoid name clashing
JsName outerName = initFunction.getScope().declareName("$outer");
JsName outerName = initFunction.getScope().declareName(Namer.OUTER_FIELD_NAME);
initFunction.getParameters().add(0, new JsParameter(outerName));
JsExpression target = new JsNameRef(outerName, JsLiteral.THIS);
@@ -0,0 +1,31 @@
package foo
@native class A(x: Int) {
var x: Int
get() = noImpl
set(value) = noImpl
fun foo(): Int = noImpl
class B(val value: Int) {
fun bar(): Int = noImpl
}
inner class C(val value: Int) {
fun bar(): Int = noImpl
fun dec(): Unit = noImpl
}
}
fun box(): String {
var b = A.B(23)
if (b.bar() != 10023) return "failed1: ${b.bar()}"
var c = A(11).C(23)
if (c.bar() != 10034) return "failed2: ${c.bar()}"
c.dec()
if (c.bar() != 10033) return "failed3: ${c.bar()}"
return "OK"
}
@@ -0,0 +1,30 @@
function A(x) {
this.x = x;
}
A.prototype = {
foo : function() {
return this.x;
}
};
A.B = function(value) {
this.value = value;
};
A.B.prototype = {
bar : function() {
return 10000 + this.value;
}
};
A.C = function(outer, value) {
this.outer = outer;
this.value = value;
};
A.C.prototype = {
bar : function() {
return this.outer.foo() + this.value + 10000;
},
dec : function() {
this.outer.x--;
}
};