FIC: Support base version of conversions in JVM backend

Proper support for JVM backend will be in the further commmits
This commit is contained in:
Mikhail Zarechenskiy
2019-11-14 15:30:14 +03:00
parent c71c1d45c6
commit 0ee977c42e
6 changed files with 40 additions and 9 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.codegen.state.TypeMapperUtilsKt;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
@@ -62,21 +63,25 @@ public class SamType {
}
@NotNull
public JavaClassDescriptor getJavaClassDescriptor() {
public ClassDescriptor getClassDescriptor() {
ClassifierDescriptor classifier = type.getConstructor().getDeclarationDescriptor();
assert classifier instanceof JavaClassDescriptor : "Sam interface not a Java class: " + classifier;
return (JavaClassDescriptor) classifier;
assert classifier instanceof ClassDescriptor : "Sam/Fun interface not a class descriptor: " + classifier;
return (ClassDescriptor) classifier;
}
@NotNull
public KotlinType getKotlinFunctionType() {
//noinspection ConstantConditions
return getJavaClassDescriptor().getDefaultFunctionTypeForSamInterface();
ClassDescriptor descriptor = getClassDescriptor();
if (descriptor instanceof JavaClassDescriptor) {
return ((JavaClassDescriptor) descriptor).getDefaultFunctionTypeForSamInterface();
}
return null;
}
@NotNull
public SimpleFunctionDescriptor getOriginalAbstractMethod() {
return (SimpleFunctionDescriptor) SingleAbstractMethodUtils.getAbstractMembers(getJavaClassDescriptor()).get(0);
return (SimpleFunctionDescriptor) SingleAbstractMethodUtils.getAbstractMembers(getClassDescriptor()).get(0);
}
@Override
@@ -82,7 +82,7 @@ public class SamWrapperCodegen {
KotlinType functionType = samType.getKotlinFunctionType();
ClassDescriptor classDescriptor = new ClassDescriptorImpl(
samType.getJavaClassDescriptor().getContainingDeclaration(),
samType.getClassDescriptor().getContainingDeclaration(),
fqName.shortName(),
Modality.FINAL,
ClassKind.CLASS,
@@ -132,7 +132,7 @@ public class SamWrapperCodegen {
}
private void generateConstructor(Type ownerType, Type functionType, ClassBuilder cv) {
MethodVisitor mv = cv.newMethod(JvmDeclarationOriginKt.OtherOrigin(samType.getJavaClassDescriptor()),
MethodVisitor mv = cv.newMethod(JvmDeclarationOriginKt.OtherOrigin(samType.getClassDescriptor()),
visibility, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), null, null);
if (state.getClassBuilderMode().generateBodies) {
@@ -198,7 +198,7 @@ public class SamWrapperCodegen {
"%s$sam%s$%s" + SAM_WRAPPER_SUFFIX,
outermostOwner.shortName().asString(),
(isInsideInline ? "$i" : ""),
DescriptorUtils.getFqNameSafe(samType.getJavaClassDescriptor()).asString().replace('.', '_')
DescriptorUtils.getFqNameSafe(samType.getClassDescriptor()).asString().replace('.', '_')
);
return outermostOwner.parent().child(Name.identifier(shortName));
}
@@ -0,0 +1,11 @@
// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
fun interface Foo {
fun invoke(): String
}
fun foo(f: Foo) = f.invoke()
fun box(): String {
return foo { "OK" }
}
@@ -13684,6 +13684,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testBasicFunInterface() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt");
}
@TestMetadata("basicFunInterfaceConversion.kt")
public void testBasicFunInterfaceConversion() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling")
@@ -13684,6 +13684,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
public void testBasicFunInterface() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt");
}
@TestMetadata("basicFunInterfaceConversion.kt")
public void testBasicFunInterfaceConversion() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling")
@@ -12534,6 +12534,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
public void testBasicFunInterface() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt");
}
@TestMetadata("basicFunInterfaceConversion.kt")
public void testBasicFunInterfaceConversion() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling")