Basic inline class mapping to the underlying representation

This commit is contained in:
Mikhail Zarechenskiy
2018-01-24 14:33:30 +03:00
parent 4b4525ec17
commit d5400f11a3
7 changed files with 106 additions and 27 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen.state;
@@ -399,7 +388,7 @@ public class KotlinTypeMapper {
@NotNull
public Type mapClass(@NotNull ClassifierDescriptor classifier) {
return mapType(classifier.getDefaultType(), null, TypeMappingMode.DEFAULT);
return mapType(classifier.getDefaultType(), null, TypeMappingMode.CLASS_DECLARATION);
}
@NotNull
@@ -0,0 +1,26 @@
// !LANGUAGE: +InlineClasses
inline class Foo(val x: Int)
object Test {
fun asParam(a: Foo) {}
fun asReturn(): Foo = TODO()
fun Foo.asExtension() {}
fun Foo.asAll(x: Any?, a: Foo, b: Int): Foo = TODO()
}
// method: Test::asParam
// jvm signature: (I)V
// generic signature: null
// method: Test::asReturn
// jvm signature: ()I
// generic signature: null
// method: Test::asExtension
// jvm signature: (I)V
// generic signature: null
// method: Test::asAll
// jvm signature: (ILjava/lang/Object;II)I
// generic signature: null
@@ -0,0 +1,19 @@
// !LANGUAGE: +InlineClasses
inline class Foo(val x: Int?)
class SimpleClass
inline class Bar(val x: SimpleClass)
object Test {
fun asParam(a: Foo) {}
fun asReturn(): Bar = TODO()
}
// method: Test::asParam
// jvm signature: (Ljava/lang/Integer;)V
// generic signature: null
// method: Test::asReturn
// jvm signature: ()LSimpleClass;
// generic signature: null
@@ -550,6 +550,27 @@ public class WriteSignatureTestGenerated extends AbstractWriteSignatureTest {
}
}
@TestMetadata("compiler/testData/writeSignature/inlineClasses")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClasses extends AbstractWriteSignatureTest {
public void testAllFilesPresentInInlineClasses() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/writeSignature/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("simpleSignatureWithInlineClassTypesAsPrimitive.kt")
public void testSimpleSignatureWithInlineClassTypesAsPrimitive() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsPrimitive.kt");
doTest(fileName);
}
@TestMetadata("simpleSignatureWithInlineClassTypesAsReference.kt")
public void testSimpleSignatureWithInlineClassTypesAsReference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsReference.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/writeSignature/nothing")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.load.kotlin
@@ -21,6 +10,7 @@ import org.jetbrains.kotlin.types.Variance
class TypeMappingMode private constructor(
val needPrimitiveBoxing: Boolean = true,
val needInlineClassWrapping: Boolean = false,
val isForAnnotationParameter: Boolean = false,
// Here DeclarationSiteWildcards means wildcard generated because of declaration-site variance
val skipDeclarationSiteWildcards: Boolean = false,
@@ -43,6 +33,18 @@ class TypeMappingMode private constructor(
@JvmField
val DEFAULT = TypeMappingMode(genericArgumentMode = GENERIC_ARGUMENT, needPrimitiveBoxing = false)
/**
* kotlin.Int is mapped to I
* inline class Foo(val x: Int) is mapped to LFoo;
* but in signature fun bar(f: Foo), Foo is mapped to I
*/
@JvmField
val CLASS_DECLARATION = TypeMappingMode(
genericArgumentMode = GENERIC_ARGUMENT,
needPrimitiveBoxing = false,
needInlineClassWrapping = true
)
/**
* kotlin.Int is mapped to Ljava/lang/Integer;
* No projections allowed in immediate arguments
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import org.jetbrains.kotlin.resolve.underlyingRepresentation
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
import org.jetbrains.kotlin.utils.DO_NOTHING_3
@@ -128,6 +129,13 @@ fun <T : Any> mapType(
}
descriptor is ClassDescriptor -> {
if (descriptor.isInline && !mode.needInlineClassWrapping) {
val underlyingType = descriptor.underlyingRepresentation()?.type
if (underlyingType != null) {
return mapType(underlyingType, factory, mode, typeMappingConfiguration, descriptorTypeWriter, writeGenericType)
}
}
val jvmType =
if (mode.isForAnnotationParameter && KotlinBuiltIns.isKClass(descriptor)) {
factory.javaLangClassType
@@ -0,0 +1,14 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
fun ClassDescriptor.underlyingRepresentation(): ValueParameterDescriptor? {
if (!isInline) return null
return unsubstitutedPrimaryConstructor?.valueParameters?.singleOrNull()
}