[IR] Generify lowerings

#KT-1179
This commit is contained in:
Evgeniy.Zhelenskiy
2022-03-06 03:22:35 +03:00
committed by teamcity
parent 22b2554368
commit 282ab398c6
36 changed files with 3972 additions and 373 deletions
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.descriptors
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
class MultiFieldValueClassRepresentation<Type : SimpleTypeMarker>(
val underlyingPropertyNamesToTypes: List<Pair<Name, Type>>
) {
init {
require(underlyingPropertyNamesToTypes.size > 1) { "MultiFieldValueClassRepresentation has at least 2 properties" }
}
private val map = underlyingPropertyNamesToTypes.toMap().also {
require(it.size == underlyingPropertyNamesToTypes.size) { "Some properties have the same names" }
}
fun containsPropertyWithName(name: Name): Boolean = name in map
fun propertyTypeByName(name: Name): Type? = map[name]
inline fun <Other : SimpleTypeMarker> mapUnderlyingType(transform: (Type) -> Other): MultiFieldValueClassRepresentation<Other> =
MultiFieldValueClassRepresentation(underlyingPropertyNamesToTypes.map { (key, value) -> key to transform(value) })
}
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.jvm.createMappedTypeParametersSubstitution
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorBase
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.FakePureImplementationsProvider
@@ -40,7 +39,6 @@ import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.util.*
class LazyJavaClassDescriptor(
val outerContext: LazyJavaResolverContext,
@@ -200,6 +198,8 @@ class LazyJavaClassDescriptor(
override fun getInlineClassRepresentation(): InlineClassRepresentation<SimpleType>? = null
override fun getMultiFieldValueClassRepresentation(): MultiFieldValueClassRepresentation<SimpleType>? = null
override fun toString() = "Lazy Java class ${this.fqNameUnsafe}"
private inner class LazyJavaClassTypeConstructor : AbstractClassTypeConstructor(c.storageManager) {
@@ -88,6 +88,7 @@ class FunctionClassDescriptor(
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
override fun getSealedSubclasses() = emptyList<ClassDescriptor>()
override fun getInlineClassRepresentation(): InlineClassRepresentation<SimpleType>? = null
override fun getMultiFieldValueClassRepresentation(): MultiFieldValueClassRepresentation<SimpleType>? = null
override fun getDeclaredTypeParameters() = parameters
@@ -104,6 +104,9 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
@Nullable
InlineClassRepresentation<SimpleType> getInlineClassRepresentation();
@Nullable
MultiFieldValueClassRepresentation<SimpleType> getMultiFieldValueClassRepresentation();
@NotNull
@Override
ClassDescriptor getOriginal();
@@ -85,6 +85,7 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
override fun getCompanionObjectDescriptor(): ClassDescriptor? = null
override fun getSealedSubclasses(): Collection<ClassDescriptor> = emptyList()
override fun getInlineClassRepresentation(): InlineClassRepresentation<SimpleType>? = null
override fun getMultiFieldValueClassRepresentation(): MultiFieldValueClassRepresentation<SimpleType>? = null
override fun toString() = "class $name (not found)"
}
@@ -181,4 +181,10 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
public InlineClassRepresentation<SimpleType> getInlineClassRepresentation() {
return null;
}
@Nullable
@Override
public MultiFieldValueClassRepresentation<SimpleType> getMultiFieldValueClassRepresentation() {
return null;
}
}
@@ -199,6 +199,12 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
return null;
}
@Nullable
@Override
public MultiFieldValueClassRepresentation<SimpleType> getMultiFieldValueClassRepresentation() {
return null;
}
private class EnumEntryScope extends MemberScopeImpl {
private final MemoizedFunctionToNotNull<Name, Collection<? extends SimpleFunctionDescriptor>> functions;
private final MemoizedFunctionToNotNull<Name, Collection<? extends PropertyDescriptor>> properties;
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.descriptors.impl;
import kotlin.Pair;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
@@ -328,10 +329,27 @@ public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor
//noinspection ConstantConditions
return representation == null ? null : new InlineClassRepresentation<SimpleType>(
representation.getUnderlyingPropertyName(),
substituteSimpleType(getInlineClassRepresentation().getUnderlyingType())
substituteSimpleType(representation.getUnderlyingType())
);
}
@Nullable
@Override
public MultiFieldValueClassRepresentation<SimpleType> getMultiFieldValueClassRepresentation() {
MultiFieldValueClassRepresentation<SimpleType> representation = original.getMultiFieldValueClassRepresentation();
if (representation == null) {
return null;
}
List<Pair<Name, SimpleType>> underlyingList = new ArrayList<Pair<Name, SimpleType>>(representation.getUnderlyingPropertyNamesToTypes());
for (int i = 0; i < underlyingList.size(); i++) {
Pair<Name, SimpleType> pair = underlyingList.get(i);
Name name = pair.getFirst();
SimpleType simpleType = pair.getSecond();
underlyingList.set(i, new Pair<Name, SimpleType>(name, substituteSimpleType(simpleType)));
}
return new MultiFieldValueClassRepresentation<SimpleType>(underlyingList);
}
@Nullable
@Override
public SimpleType getDefaultFunctionTypeForSamInterface() {
@@ -195,6 +195,12 @@ public class MutableClassDescriptor extends ClassDescriptorBase {
return null;
}
@Nullable
@Override
public MultiFieldValueClassRepresentation<SimpleType> getMultiFieldValueClassRepresentation() {
return null;
}
@Override
public String toString() {
return DeclarationDescriptorImpl.toString(this);
@@ -53,7 +53,12 @@ class DeserializedClassDescriptor(
private val typeConstructor = DeserializedClassTypeConstructor()
private val memberScopeHolder =
ScopesHolderForClass.create(this, c.storageManager, c.components.kotlinTypeChecker.kotlinTypeRefiner, this::DeserializedClassMemberScope)
ScopesHolderForClass.create(
this,
c.storageManager,
c.components.kotlinTypeChecker.kotlinTypeRefiner,
this::DeserializedClassMemberScope
)
private val memberScope get() = memberScopeHolder.getScope(c.components.kotlinTypeChecker.kotlinTypeRefiner)
private val enumEntries = if (kind == ClassKind.ENUM_CLASS) EnumEntryClassDescriptors() else null
@@ -64,6 +69,8 @@ class DeserializedClassDescriptor(
private val companionObjectDescriptor = c.storageManager.createNullableLazyValue { computeCompanionObjectDescriptor() }
private val sealedSubclasses = c.storageManager.createLazyValue { computeSubclassesForSealedClass() }
private val inlineClassRepresentation = c.storageManager.createNullableLazyValue { computeInlineClassRepresentation() }
private val multiFieldValueClassRepresentation =
c.storageManager.createNullableLazyValue { computeMultiFieldValueClassRepresentation() }
internal val thisAsProtoContainer: ProtoContainer.Class = ProtoContainer.Class(
classProto, c.nameResolver, c.typeTable, sourceElement,
@@ -176,9 +183,11 @@ class DeserializedClassDescriptor(
override fun getSealedSubclasses() = sealedSubclasses()
override fun getInlineClassRepresentation(): InlineClassRepresentation<SimpleType>? = inlineClassRepresentation()
override fun getMultiFieldValueClassRepresentation(): MultiFieldValueClassRepresentation<SimpleType>? =
multiFieldValueClassRepresentation()
private fun computeInlineClassRepresentation(): InlineClassRepresentation<SimpleType>? {
if (!isInlineOrValueClass()) return null
if (!isInlineClass()) return null
val propertyName = when {
classProto.hasInlineClassUnderlyingPropertyName() ->
@@ -193,17 +202,35 @@ class DeserializedClassDescriptor(
}
val type = classProto.inlineClassUnderlyingType(c.typeTable)?.let(c.typeDeserializer::simpleType)
?: run {
val underlyingProperty =
memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_DESERIALIZATION)
.singleOrNull { it.extensionReceiverParameter == null }
?: error("Inline class has no underlying property: $this")
underlyingProperty.type as SimpleType
}
?: getPropertyTypeFromContributedVariables(propertyName)
return InlineClassRepresentation(propertyName, type)
}
private fun getPropertyTypeFromContributedVariables(propertyName: Name): SimpleType {
val underlyingProperty = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_DESERIALIZATION)
.singleOrNull { it.extensionReceiverParameter == null } ?: error("Value class has no underlying property: $this")
return underlyingProperty.type as SimpleType
}
private fun computeMultiFieldValueClassRepresentation(): MultiFieldValueClassRepresentation<SimpleType>? {
if (!isValueClass() || isInlineClass()) return null
val representation = classProto.multiFieldValueClassRepresentation
?: error("No multiFieldValueClassRepresentation for multi-field value class: $this")
val propertyList = representation.propertyList.map { valueClassProperty ->
val name: Name = c.nameResolver.getName(valueClassProperty.name)
val protoType: ProtoBuf.Type = when {
valueClassProperty.hasType() -> valueClassProperty.type
valueClassProperty.hasTypeId() -> c.typeTable[valueClassProperty.typeId]
else -> error("Unlike inline classes before 1.5.0, multi-field value classes always have intermediate representation")
}
name to c.typeDeserializer.simpleType(protoType)
}
require(propertyList.size > 1)
require(propertyList.distinctBy { (name, _) -> name }.size == propertyList.size)
return MultiFieldValueClassRepresentation(propertyList)
}
override fun toString() =
"deserialized ${if (isExpect) "expect " else ""}class $name" // not using descriptor renderer to preserve laziness
@@ -256,7 +283,7 @@ class DeserializedClassDescriptor(
private inner class DeserializedClassMemberScope(private val kotlinTypeRefiner: KotlinTypeRefiner) : DeserializedMemberScope(
c, classProto.functionList, classProto.propertyList, classProto.typeAliasList,
classProto.nestedClassNameList.map(c.nameResolver::getName).let { { it } } // workaround KT-13454
classProto.nestedClassNameList.map(c.nameResolver::getName).let { list -> { list } } // workaround KT-13454
) {
private val classDescriptor: DeserializedClassDescriptor get() = this@DeserializedClassDescriptor
@@ -26,7 +26,7 @@ class JvmMetadataVersion(versionArray: IntArray, val isStrictSemantics: Boolean)
companion object {
@JvmField
val INSTANCE = JvmMetadataVersion(1, 7, 0)
val INSTANCE = JvmMetadataVersion(1, 7, 1)
@JvmField
val INVALID_VERSION = JvmMetadataVersion()
+13
View File
@@ -242,6 +242,19 @@ message Class {
optional Type inline_class_underlying_type = 18;
optional int32 inline_class_underlying_type_id = 19 [(type_id_in_table) = true];
message MultiFieldValueClassRepresentation {
message MultiFieldValueClassProperty {
required int32 name = 1;
oneof type_representation {
Type type = 2;
int32 type_id = 3;
}
}
repeated MultiFieldValueClassProperty property = 1;
}
optional MultiFieldValueClassRepresentation multi_field_value_class_representation = 22;
optional TypeTable type_table = 30;
// Index into the VersionRequirementTable
File diff suppressed because it is too large Load Diff