diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java index 145f21e9bd2..b19bfa36421 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java @@ -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 diff --git a/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsPrimitive.kt b/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsPrimitive.kt new file mode 100644 index 00000000000..268801e2ee2 --- /dev/null +++ b/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsPrimitive.kt @@ -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 diff --git a/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsReference.kt b/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsReference.kt new file mode 100644 index 00000000000..5ac7890735d --- /dev/null +++ b/compiler/testData/writeSignature/inlineClasses/simpleSignatureWithInlineClassTypesAsReference.kt @@ -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 diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/WriteSignatureTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/WriteSignatureTestGenerated.java index cf1070c01b0..8b91c340da9 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/WriteSignatureTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/WriteSignatureTestGenerated.java @@ -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) diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt index d9c582a7530..0381bcb71e3 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/TypeMappingMode.kt @@ -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 diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt index 1c950db948f..49c45ab0782 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt @@ -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 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 diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/inlineClassesUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/inlineClassesUtils.kt new file mode 100644 index 00000000000..b3da887af88 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/inlineClassesUtils.kt @@ -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() +}