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-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()
}