Remake KotlinTypes.

This commit is contained in:
Stanislav Erokhin
2016-06-03 18:45:41 +03:00
parent 56122460aa
commit 1d9ca06b96
40 changed files with 283 additions and 523 deletions
@@ -33,7 +33,7 @@ import org.jetbrains.kotlin.serialization.SerializerExtension;
import org.jetbrains.kotlin.serialization.StringTable;
import org.jetbrains.kotlin.serialization.jvm.ClassMapperLite;
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf;
import org.jetbrains.kotlin.types.DelegatingFlexibleType;
import org.jetbrains.kotlin.types.FlexibleType;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.org.objectweb.asm.Type;
import org.jetbrains.org.objectweb.asm.commons.Method;
@@ -84,7 +84,7 @@ public class JvmSerializerExtension extends SerializerExtension {
@Override
public void serializeFlexibleType(
@NotNull DelegatingFlexibleType flexibleType,
@NotNull FlexibleType flexibleType,
@NotNull ProtoBuf.Type.Builder lowerProto,
@NotNull ProtoBuf.Type.Builder upperProto
) {
@@ -94,12 +94,12 @@ public class PossiblyBareType {
KotlinType nullableActualType = TypeUtils.makeNullable(getActualType());
KotlinType abbreviatedType = KotlinTypeKt.getAbbreviatedType(getActualType());
KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(getActualType());
if (abbreviatedType == null) {
return type(nullableActualType);
}
else {
return type(KotlinTypeKt.withAbbreviatedType(KotlinTypeKt.asSimpleType(nullableActualType), KotlinTypeKt.asSimpleType(TypeUtils.makeNullable(abbreviatedType))));
return type(SpecialTypesKt.withAbbreviatedType(KotlinTypeKt.asSimpleType(nullableActualType), KotlinTypeKt.asSimpleType(TypeUtils.makeNullable(abbreviatedType))));
}
}
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.*
import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing
@@ -102,7 +103,7 @@ class TypeResolver(
class LazyKotlinType : WrappedType(), LazyEntity {
private val _delegate = storageManager.createLazyValue { doResolvePossiblyBareType(c, typeReference).getActualType() }
override fun unwrap() = _delegate()
override val delegate: KotlinType by _delegate
override fun isComputed() = _delegate.isComputed()
override fun forceResolveAllContents() {
@@ -88,7 +88,7 @@ public class DeferredType extends WrappedType implements LazyType {
@NotNull
@Override
public KotlinType unwrap() {
public KotlinType getDelegate() {
return lazyValue.invoke();
}
@@ -506,7 +506,7 @@ public class DescriptorSerializer {
}
if (FlexibleTypesKt.isFlexible(type)) {
DelegatingFlexibleType flexibleType = (DelegatingFlexibleType) FlexibleTypesKt.asFlexibleType(type);
FlexibleType flexibleType = FlexibleTypesKt.asFlexibleType(type);
ProtoBuf.Type.Builder lowerBound = type(flexibleType.getLowerBound());
ProtoBuf.Type.Builder upperBound = type(flexibleType.getUpperBound());
@@ -548,7 +548,7 @@ public class DescriptorSerializer {
builder.setNullable(type.isMarkedNullable());
}
KotlinType abbreviatedType = KotlinTypeKt.getAbbreviatedType(type);
KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(type);
if (abbreviatedType != null) {
if (useTypeTable()) {
builder.setAbbreviatedTypeId(typeId(abbreviatedType));
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.serialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.types.DelegatingFlexibleType;
import org.jetbrains.kotlin.types.FlexibleType;
import org.jetbrains.kotlin.types.KotlinType;
public abstract class SerializerExtension {
@@ -50,7 +50,7 @@ public abstract class SerializerExtension {
public void serializeValueParameter(@NotNull ValueParameterDescriptor descriptor, @NotNull ProtoBuf.ValueParameter.Builder proto) {
}
public void serializeFlexibleType(@NotNull DelegatingFlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto) {
public void serializeFlexibleType(@NotNull FlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto) {
}
public void serializeType(@NotNull KotlinType type, @NotNull ProtoBuf.Type.Builder proto) {
@@ -9,5 +9,5 @@ Resolved call:
Resulting descriptor: fun foo(): dynamic defined in bar
Explicit receiver kind = DISPATCH_RECEIVER
Dispatch receiver = a {('Nothing'..'Any?')}
Dispatch receiver = a {dynamic}
Extension receiver = NO_RECEIVER
@@ -12,4 +12,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in root package
Explicit receiver kind = EXTENSION_RECEIVER
Dispatch receiver = NO_RECEIVER
Extension receiver = a {('Nothing'..'Any?')}
Extension receiver = a {dynamic}
@@ -16,4 +16,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in A
Explicit receiver kind = EXTENSION_RECEIVER
Dispatch receiver = AExt{fun A.<anonymous>(): Unit defined in bar}
Extension receiver = b {('Nothing'..'Any?')}
Extension receiver = b {dynamic}
@@ -18,4 +18,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in A
Explicit receiver kind = NO_EXPLICIT_RECEIVER
Dispatch receiver = AExt{fun A.<anonymous>(): Unit defined in bar}
Extension receiver = ('Nothing'..'Any?')Ext{fun dynamic.<anonymous>(): Unit defined in bar.<anonymous>}
Extension receiver = dynamicExt{fun dynamic.<anonymous>(): Unit defined in bar.<anonymous>}
@@ -9,5 +9,5 @@ Resolved call:
Resulting descriptor: fun foo(): dynamic defined in bar
Explicit receiver kind = NO_EXPLICIT_RECEIVER
Dispatch receiver = ('Nothing'..'Any?')Ext{fun dynamic.bar(): Unit defined in root package}
Dispatch receiver = dynamicExt{fun dynamic.bar(): Unit defined in root package}
Extension receiver = NO_RECEIVER
@@ -12,4 +12,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in root package
Explicit receiver kind = NO_EXPLICIT_RECEIVER
Dispatch receiver = NO_RECEIVER
Extension receiver = ('Nothing'..'Any?')Ext{fun dynamic.bar(): Unit defined in root package}
Extension receiver = dynamicExt{fun dynamic.bar(): Unit defined in root package}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* 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.
@@ -19,12 +19,12 @@ package org.jetbrains.kotlin.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase;
import org.jetbrains.kotlin.types.DelegatingFlexibleType;
import org.jetbrains.kotlin.types.FlexibleTypeImpl;
public abstract class KotlinTestWithEnvironmentManagement extends KtUsefulTestCase {
static {
System.setProperty("java.awt.headless", "true");
DelegatingFlexibleType.RUN_SLOW_ASSERTIONS = true;
FlexibleTypeImpl.RUN_SLOW_ASSERTIONS = true;
}
@NotNull
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.serialization.deserialization.TypeDeserializer
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeImpl
import org.jetbrains.kotlin.utils.Printer
import java.lang.reflect.Constructor
import java.lang.reflect.GenericDeclaration
@@ -177,7 +176,7 @@ class LazyOperationsLog(
o.joinTo(sb, ", ", prefix = "{", postfix = "}", limit = 3) { render(it) }
}
}
o is KotlinTypeImpl -> {
o is KotlinType -> {
StringBuilder().apply {
append(o.constructor)
if (!o.arguments.isEmpty()) {
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.load.java.lazy.types.RawTypeImpl
import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition
import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition.Result
import org.jetbrains.kotlin.resolve.OverridingUtil
import org.jetbrains.kotlin.types.unwrap
import org.jetbrains.kotlin.utils.singletonOrEmptyList
class ErasedOverridabilityCondition : ExternalOverridabilityCondition {
@@ -115,10 +115,10 @@ class LazyJavaTypeResolver(
) : AbstractLazyType(c.storageManager) {
override val annotations = CompositeAnnotations(listOf(LazyJavaAnnotations(c, javaType), attr.typeAnnotations))
private val classifier = c.storageManager.createNullableLazyValue { javaType.classifier }
private val classifier: JavaClassifier? get() = javaType.classifier
override fun computeTypeConstructor(): TypeConstructor {
val classifier = classifier() ?: return createNotFoundClass()
val classifier = classifier ?: return createNotFoundClass()
return when (classifier) {
is JavaClass -> {
val fqName = classifier.fqName.sure { "Class type should have a FQ name: $classifier" }
@@ -279,13 +279,13 @@ class LazyJavaTypeResolver(
!attr.isMarkedNotNull &&
// 'L extends List<T>' in Java is a List<T> in Kotlin, not a List<T?>
// nullability will be taken care of in individual member signatures
when (classifier()) {
when (classifier) {
is JavaTypeParameter -> {
attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, UPPER_BOUND, SUPERTYPE_ARGUMENT, SUPERTYPE)
}
is JavaClass,
null -> attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, SUPERTYPE_ARGUMENT, SUPERTYPE)
else -> error("Unknown classifier: ${classifier()}")
else -> error("Unknown classifier: ${classifier}")
}
}
@@ -27,17 +27,15 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFlexibleType(lowerBound, upperBound), RawType {
class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), RawType {
init {
assert (KotlinTypeChecker.DEFAULT.isSubtypeOf(lowerBound, upperBound)) {
"Lower bound $lowerBound of a flexible type must be a subtype of the upper bound $upperBound"
}
}
override fun getDelegate(): KotlinType = delegateType
override val delegateType: KotlinType get() = lowerBound
override val delegate: SimpleType get() = lowerBound
override val memberScope: MemberScope
get() {
@@ -46,11 +44,11 @@ class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFl
return classDescriptor.getMemberScope(RawSubstitution)
}
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType
= RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations).asSimpleType(), upperBound)
override fun replaceAnnotations(newAnnotations: Annotations)
= RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations))
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType
= RawTypeImpl(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable))
override fun makeNullableAsSpecified(newNullability: Boolean)
= RawTypeImpl(lowerBound.makeNullableAsSpecified(newNullability), upperBound.makeNullableAsSpecified(newNullability))
override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String {
fun onlyOutDiffers(first: String, second: String) = first == second.removePrefix("out ") || second == "*"
@@ -124,14 +124,14 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers
val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, enhancedArguments)
val enhancedType = KotlinTypeImpl.create(
val enhancedType = KotlinTypeFactory.simpleType(
newAnnotations,
typeConstructor,
enhancedNullability,
enhancedArguments,
enhancedNullability,
if (enhancedClassifier is ClassDescriptor)
enhancedClassifier.getMemberScope(newSubstitution)
else enhancedClassifier.getDefaultType().memberScope
else enhancedClassifier.defaultType.memberScope
)
val result = if (effectiveQualifiers.isNotNullTypeParameter) NotNullTypeParameter(enhancedType) else enhancedType
@@ -213,8 +213,7 @@ private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor {
override fun toString() = "[EnhancedType]"
}
internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTypeVariable, DelegatingType(), SimpleType {
override fun getDelegate(): KotlinType? = delegate
internal class NotNullTypeParameter(override val delegate: SimpleType) : CustomTypeVariable, DelegatingSimpleType() {
override val isTypeVariable: Boolean
get() = true
@@ -240,4 +239,10 @@ internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTy
return NotNullTypeParameter(result)
}
override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameter(delegate.replaceAnnotations(newAnnotations))
override fun makeNullableAsSpecified(newNullability: Boolean) = this
override val isError: Boolean
get() = false
}
@@ -78,7 +78,7 @@ open class JvmBuiltInsSettings(
//NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing
val superTypes = listOf(object : WrappedType() {
override fun unwrap() = moduleDescriptor.builtIns.anyType
override val delegate: KotlinType get() = moduleDescriptor.builtIns.anyType
})
val mockSerializableClass = ClassDescriptorImpl(
@@ -62,7 +62,7 @@ class CapturedType(
override val constructor: TypeConstructor = CapturedTypeConstructor(typeProjection),
override val isMarkedNullable: Boolean = false,
override val annotations: Annotations = Annotations.EMPTY
): AbstractKotlinType(), SubtypingRepresentatives, TypeWithCustomReplacement {
): SimpleType(), SubtypingRepresentatives {
override val arguments: List<TypeProjection>
get() = listOf()
@@ -83,14 +83,14 @@ class CapturedType(
override fun sameTypeConstructor(type: KotlinType) = constructor === type.constructor
override fun toString() = "Captured($typeProjection)${if (isMarkedNullable) '?' else ""}"
override fun toString() = "Captured($typeProjection)" + if (isMarkedNullable) "?" else ""
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
if (nullable == isMarkedNullable) return this
return CapturedType(typeProjection, constructor, nullable, annotations)
override fun makeNullableAsSpecified(newNullability: Boolean): CapturedType {
if (newNullability == isMarkedNullable) return this
return CapturedType(typeProjection, constructor, newNullability, annotations)
}
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations)
override fun replaceAnnotations(newAnnotations: Annotations): CapturedType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations)
}
fun createCapturedType(typeProjection: TypeProjection): KotlinType
@@ -120,7 +120,8 @@ private fun TypeProjection.createCapturedIfNeeded(typeParameterDescriptor: TypeP
// TODO: Make star projection type lazy
return if (isStarProjection)
TypeProjectionImpl(object : WrappedType() {
override fun unwrap(): KotlinType = this@createCapturedIfNeeded.type
override val delegate: KotlinType
get() = this@createCapturedIfNeeded.type
})
else
TypeProjectionImpl(this@createCapturedIfNeeded.type)
@@ -1,84 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.Iterator;
import java.util.List;
public abstract class AbstractKotlinType implements KotlinType, SimpleType { // TODO temporary upper bound
@Override
public final int hashCode() {
int result = getConstructor().hashCode();
result = 31 * result + getArguments().hashCode();
result = 31 * result + (isMarkedNullable() ? 1 : 0);
return result;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof KotlinType)) return false;
KotlinType type = (KotlinType) obj;
return isMarkedNullable() == type.isMarkedNullable() && KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, type);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (AnnotationWithTarget annotationWithTarget : getAnnotations().getAllAnnotations()) {
sb.append("[");
sb.append(DescriptorRenderer.DEBUG_TEXT.renderAnnotation(
annotationWithTarget.getAnnotation(), annotationWithTarget.getTarget()));
sb.append("] ");
}
sb.append(getConstructor());
List<TypeProjection> arguments = getArguments();
if (!arguments.isEmpty()) {
sb.append("<");
for (Iterator<TypeProjection> i = arguments.iterator(); i.hasNext(); ) {
sb.append(i.next());
if (i.hasNext()) {
sb.append(", ");
}
}
sb.append(">");
}
if (isMarkedNullable()) {
sb.append("?");
}
return sb.toString();
}
@Nullable
@Override
public SimpleType getAbbreviatedType() {
return null;
}
}
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlinType(), SimpleType, LazyType {
abstract class AbstractLazyType(storageManager: StorageManager) : SimpleType(), LazyType {
private val typeConstructor = storageManager.createLazyValue { computeTypeConstructor() }
override val constructor by typeConstructor
@@ -52,6 +52,10 @@ abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlin
override val annotations: Annotations get() = Annotations.EMPTY
// TODO: do not force resolution
override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = KotlinTypeFactory.simpleType(this, annotations = newAnnotations)
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType = KotlinTypeFactory.simpleType(this, nullable = newNullability)
override fun toString() = when {
!typeConstructor.isComputed() -> "[Not-computed]"
!_arguments.isComputed() ->
@@ -1,88 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.List;
public abstract class DelegatingType implements KotlinType, SimpleType { // TODO temporary upper bound
protected abstract KotlinType getDelegate();
@NotNull
@Override
public TypeConstructor getConstructor() {
return getDelegate().getConstructor();
}
@NotNull
@Override
public List<TypeProjection> getArguments() {
return getDelegate().getArguments();
}
@Override
public boolean isMarkedNullable() {
return getDelegate().isMarkedNullable();
}
@NotNull
@Override
public MemberScope getMemberScope() {
return getDelegate().getMemberScope();
}
@Override
public boolean isError() {
return getDelegate().isError();
}
@NotNull
@Override
public Annotations getAnnotations() {
return getDelegate().getAnnotations();
}
@Override
public int hashCode() {
return getDelegate().hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof KotlinType)) return false;
KotlinType type = (KotlinType) obj;
return KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, type);
}
@Override
public String toString() {
return getDelegate().toString();
}
@Nullable
@Override
public SimpleType getAbbreviatedType() {
return null;
}
}
@@ -390,7 +390,7 @@ public class ErrorUtils {
@NotNull
public static SimpleType createErrorTypeWithArguments(@NotNull String debugMessage, @NotNull List<TypeProjection> arguments) {
return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments);
return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments, false);
}
@NotNull
@@ -472,35 +472,26 @@ public class ErrorUtils {
return candidate instanceof ErrorClassDescriptor;
}
@NotNull
public static TypeParameterDescriptor createErrorTypeParameter(int index, @NotNull String debugMessage) {
return TypeParameterDescriptorImpl.createWithDefaultBound(
ERROR_CLASS,
Annotations.Companion.getEMPTY(),
false,
Variance.INVARIANT,
Name.special("<ERROR: " + debugMessage + ">"),
index
);
}
private static class ErrorTypeImpl implements SimpleType {
private static class ErrorTypeImpl extends SimpleType {
private final TypeConstructor constructor;
private final MemberScope memberScope;
private final List<TypeProjection> arguments;
private final boolean nullability;
private ErrorTypeImpl(
@NotNull TypeConstructor constructor,
@NotNull MemberScope memberScope,
@NotNull List<TypeProjection> arguments
@NotNull List<TypeProjection> arguments,
boolean nullability
) {
this.constructor = constructor;
this.memberScope = memberScope;
this.arguments = arguments;
this.nullability = nullability;
}
private ErrorTypeImpl(@NotNull TypeConstructor constructor, @NotNull MemberScope memberScope) {
this(constructor, memberScope, Collections.<TypeProjection>emptyList());
this(constructor, memberScope, Collections.<TypeProjection>emptyList(), false);
}
@NotNull
@@ -517,7 +508,7 @@ public class ErrorUtils {
@Override
public boolean isMarkedNullable() {
return false;
return nullability;
}
@NotNull
@@ -542,10 +533,16 @@ public class ErrorUtils {
return constructor.toString() + (arguments.isEmpty() ? "" : joinToString(arguments, ", ", "<", ">", -1, "...", null));
}
@Nullable
@NotNull
@Override
public SimpleType getAbbreviatedType() {
return null;
public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) {
return this;
}
@NotNull
@Override
public SimpleType makeNullableAsSpecified(boolean newNullability) {
return new ErrorTypeImpl(constructor, memberScope, arguments, newNullability);
}
}
@@ -19,59 +19,59 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.renderer.DescriptorRendererOptions
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
/**
* @see KotlinTypeChecker.isSubtypeOf
*/
interface KotlinType : Annotated {
val constructor: TypeConstructor
sealed class KotlinType : Annotated {
val arguments: List<TypeProjection>
abstract val constructor: TypeConstructor
abstract val arguments: List<TypeProjection>
abstract val isMarkedNullable: Boolean
abstract val memberScope: MemberScope
abstract val isError: Boolean
val isMarkedNullable: Boolean
abstract fun unwrap(): UnwrappedType
val memberScope: MemberScope
// ------- internal staff ------
val isError: Boolean
final override fun hashCode(): Int {
if (isError) return super.hashCode()
override fun equals(other: Any?): Boolean
var result = constructor.hashCode()
result = 31 * result + arguments.hashCode()
result = 31 * result + if (isMarkedNullable) 1 else 0
return result
}
final override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is KotlinType) return false
return isMarkedNullable == other.isMarkedNullable && KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, other)
}
}
@Deprecated("Temporary marker method for refactoring")
fun KotlinType.asSimpleType(): SimpleType {
return unwrap() as SimpleType
}
abstract class WrappedType() : KotlinType(), LazyType {
open fun isComputed(): Boolean = true
protected abstract val delegate: KotlinType
fun KotlinType.unwrap(): KotlinType {
if (this is WrappedType) return unwrap().unwrap()
return this
}
interface SimpleType : KotlinType {
val abbreviatedType : SimpleType? get() = null
}
interface TypeWithCustomReplacement : KotlinType {
fun makeNullableAsSpecified(nullable: Boolean): KotlinType
fun replaceAnnotations(newAnnotations: Annotations): KotlinType
}
abstract class WrappedType() : KotlinType, LazyType {
override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
override val memberScope: MemberScope get() = delegate.memberScope
open fun isComputed(): Boolean = true
open val delegate: KotlinType
get() = unwrap()
abstract fun unwrap(): KotlinType
override final fun unwrap(): UnwrappedType {
var result = delegate
while (result is WrappedType) {
result = result.delegate
}
return result as UnwrappedType
}
override fun toString(): String {
if (isComputed()) {
@@ -84,58 +84,26 @@ abstract class WrappedType() : KotlinType, LazyType {
// todo: remove this later
override val isError: Boolean get() = delegate.isError
override fun equals(other: Any?): Boolean = unwrap().equals(other)
override fun hashCode(): Int = unwrap().hashCode()
}
fun SimpleType.lazyReplaceNullability(newNullable: Boolean): SimpleType {
if (this is WrappedSimpleType) {
return WrappedSimpleType(delegate, newAnnotations, newNullable)
}
else {
return WrappedSimpleType(this, newNullable = newNullable)
}
sealed class UnwrappedType: KotlinType() {
abstract fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType
abstract fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType
override final fun unwrap(): UnwrappedType = this
}
fun SimpleType.lazyReplaceAnnotations(newAnnotations: Annotations): SimpleType {
if (this is WrappedSimpleType) {
return WrappedSimpleType(delegate, newAnnotations, newNullable)
}
else {
return WrappedSimpleType(this, newAnnotations)
}
}
fun KotlinType.getAbbreviatedType(): SimpleType? = (unwrap() as? SimpleType)?.abbreviatedType
fun SimpleType.withAbbreviatedType(abbreviatedType: SimpleType): SimpleType {
if (isError) return this
return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, abbreviatedType)
}
private class WrappedSimpleType(
override val delegate: SimpleType,
val newAnnotations: Annotations? = null,
val newNullable: Boolean? = null
): WrappedType(), SimpleType {
override val annotations: Annotations
get() = newAnnotations ?: delegate.annotations
override val isMarkedNullable: Boolean
get() = newNullable ?: delegate.isMarkedNullable
override fun unwrap(): KotlinType {
if (delegate.isError) return delegate // todo
if (delegate is CustomTypeVariable) return delegate // todo
return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, abbreviatedType)
}
abstract class SimpleType : UnwrappedType() {
abstract override fun replaceAnnotations(newAnnotations: Annotations): SimpleType
abstract override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType
override fun toString(): String {
if (isError) return delegate.toString()
// for error types this method should be overridden
if (isError) return "ErrorType"
return buildString {
for (annotation in annotations.getAllAnnotations()) {
append("[", DescriptorRenderer.DEBUG_TEXT.renderAnnotation(annotation.annotation, annotation.target), "] ")
for ((annotation, target) in annotations.getAllAnnotations()) {
append("[", DescriptorRenderer.DEBUG_TEXT.renderAnnotation(annotation, target), "] ")
}
append(constructor)
@@ -143,4 +111,34 @@ private class WrappedSimpleType(
if (isMarkedNullable) append("?")
}
}
}
}
// lowerBound is a subtype of upperBound
abstract class FlexibleType(val lowerBound: SimpleType, val upperBound: SimpleType) :
UnwrappedType(), SubtypingRepresentatives {
abstract val delegate: SimpleType
override val subTypeRepresentative: KotlinType
get() = lowerBound
override val superTypeRepresentative: KotlinType
get() = upperBound
override fun sameTypeConstructor(type: KotlinType) = false
abstract fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String
override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
override val memberScope: MemberScope get() = delegate.memberScope
override val isError: Boolean get() = false
override fun toString(): String = DescriptorRenderer.DEBUG_TEXT.renderType(this)
}
@Deprecated("Temporary marker method for refactoring")
fun KotlinType.asSimpleType(): SimpleType {
return unwrap() as SimpleType
}
@@ -29,14 +29,14 @@ object KotlinTypeFactory {
arguments: List<TypeProjection>,
nullable: Boolean,
memberScope: MemberScope
): SimpleType = KotlinTypeImpl.create(annotations, constructor, nullable, arguments, memberScope)
): SimpleType = SimpleTypeImpl(annotations, constructor, arguments, nullable, memberScope)
@JvmStatic
fun simpleNotNullType(
annotations: Annotations,
descriptor: ClassDescriptor,
arguments: List<TypeProjection>
): SimpleType = KotlinTypeImpl.create(annotations, descriptor.typeConstructor, false, arguments, descriptor.getMemberScope(arguments))
): SimpleType = SimpleTypeImpl(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments))
@JvmStatic
fun simpleType(
@@ -49,8 +49,28 @@ object KotlinTypeFactory {
): SimpleType = simpleType(annotations, constructor, arguments, nullable, memberScope)
@JvmStatic
fun flexibleType(lowerBound: SimpleType, upperBound: SimpleType): KotlinType {
fun flexibleType(lowerBound: SimpleType, upperBound: SimpleType): UnwrappedType {
if (lowerBound == upperBound) return lowerBound
return FlexibleTypeImpl(lowerBound, upperBound)
}
}
private class SimpleTypeImpl(
override val annotations: Annotations,
override val constructor: TypeConstructor,
override val arguments: List<TypeProjection>,
override val isMarkedNullable: Boolean,
override val memberScope: MemberScope
) : SimpleType() {
override fun replaceAnnotations(newAnnotations: Annotations) = SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope)
override fun makeNullableAsSpecified(newNullability: Boolean) = SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope)
override val isError: Boolean
get() = false
init {
if (memberScope is ErrorUtils.ErrorScope) {
throw IllegalStateException("SimpleTypeImpl should not be created for error type: $memberScope\n$constructor")
}
}
}
@@ -1,83 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope
open class KotlinTypeImpl
internal constructor(
override val annotations: Annotations,
final override val constructor: TypeConstructor,
override val isMarkedNullable: Boolean,
override val arguments: List<TypeProjection>,
final override val memberScope: MemberScope
) : AbstractKotlinType(), SimpleType {
companion object {
@Deprecated("", ReplaceWith("KotlinTypeFactory.simpleType(annotations, constructor, arguments, nullable, memberScope)", "org.jetbrains.kotlin.types.KotlinTypeFactory"))
@JvmStatic fun create(annotations: Annotations,
constructor: TypeConstructor,
nullable: Boolean,
arguments: List<TypeProjection>,
memberScope: MemberScope): KotlinTypeImpl
= KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope)
@JvmStatic fun create(annotations: Annotations,
constructor: TypeConstructor,
nullable: Boolean,
arguments: List<TypeProjection>,
memberScope: MemberScope,
abbreviatedType: SimpleType? = null
): KotlinTypeImpl {
if (abbreviatedType != null) {
return WithCapabilities(annotations, constructor, nullable, arguments, memberScope, abbreviatedType)
}
return KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope)
}
@Deprecated("", ReplaceWith("KotlinTypeFactory.simpleType(annotations, descriptor, arguments, nullable)", "org.jetbrains.kotlin.types.KotlinTypeFactory"))
@JvmStatic fun create(annotations: Annotations,
descriptor: ClassDescriptor,
nullable: Boolean,
arguments: List<TypeProjection>): KotlinTypeImpl
= KotlinTypeImpl(
annotations, descriptor.typeConstructor, nullable, arguments, descriptor.getMemberScope(arguments)
)
}
private class WithCapabilities(
annotations: Annotations,
constructor: TypeConstructor,
nullable: Boolean,
arguments: List<TypeProjection>,
memberScope: MemberScope,
override val abbreviatedType: SimpleType?
) : KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope)
init {
if (memberScope is ErrorUtils.ErrorScope) {
throw IllegalStateException("JetTypeImpl should not be created for error type: $memberScope\n$constructor")
}
}
override val isError: Boolean get() = false
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* 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.
@@ -16,5 +16,5 @@
package org.jetbrains.kotlin.types;
public interface LazyType extends KotlinType {
public interface LazyType {
}
@@ -0,0 +1,47 @@
/*
* 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.
*/
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope
abstract class DelegatingSimpleType : SimpleType() {
protected abstract val delegate: SimpleType
override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
override val memberScope: MemberScope get() = delegate.memberScope
}
private class AbbreviatedType(override val delegate: SimpleType, val abbreviatedType: SimpleType) : DelegatingSimpleType() {
override fun replaceAnnotations(newAnnotations: Annotations)
= AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviatedType)
override fun makeNullableAsSpecified(newNullability: Boolean)
= AbbreviatedType(delegate.makeNullableAsSpecified(newNullability), abbreviatedType.makeNullableAsSpecified(newNullability))
override val isError: Boolean get() = false
}
fun KotlinType.getAbbreviatedType(): SimpleType? = (unwrap() as? AbbreviatedType)?.abbreviatedType
fun SimpleType.withAbbreviatedType(abbreviatedType: SimpleType): SimpleType {
if (isError) return this
return AbbreviatedType(this, abbreviatedType)
}
@@ -121,11 +121,11 @@ fun KotlinType.replace(
if (newArguments.isEmpty() && newAnnotations === annotations) return this
if (newArguments.isEmpty()) {
return KotlinTypeImpl.create(
return KotlinTypeFactory.simpleType(
newAnnotations,
constructor,
isMarkedNullable,
arguments,
isMarkedNullable,
memberScope
)
}
@@ -138,11 +138,11 @@ fun KotlinType.replace(
declarationDescriptor.getMemberScope(newSubstitution)
else ErrorUtils.createErrorScope("Unexpected declaration descriptor for type constructor: $constructor")
return KotlinTypeImpl.create(
return KotlinTypeFactory.simpleType(
newAnnotations,
constructor,
isMarkedNullable,
newArguments,
isMarkedNullable,
newScope
)
}
@@ -133,7 +133,7 @@ public class TypeSubstitutor {
// The type is within the substitution range, i.e. T or T?
KotlinType type = originalProjection.getType();
if (DynamicTypesKt.isDynamic(type) || KotlinTypeKt.unwrap(type) instanceof RawType) {
if (DynamicTypesKt.isDynamic(type) || type.unwrap() instanceof RawType) {
return originalProjection; // todo investigate
}
@@ -36,7 +36,7 @@ public class TypeUtils {
public static final SimpleType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
public static final SimpleType CANT_INFER_FUNCTION_PARAM_TYPE = ErrorUtils.createErrorType("Cannot be inferred");
public static class SpecialType implements SimpleType {
public static class SpecialType extends DelegatingSimpleType {
private final String name;
public SpecialType(String name) {
@@ -45,24 +45,7 @@ public class TypeUtils {
@NotNull
@Override
public TypeConstructor getConstructor() {
throw new IllegalStateException(name);
}
@NotNull
@Override
public List<TypeProjection> getArguments() {
throw new IllegalStateException(name);
}
@Override
public boolean isMarkedNullable() {
throw new IllegalStateException(name);
}
@NotNull
@Override
public MemberScope getMemberScope() {
protected SimpleType getDelegate() {
throw new IllegalStateException(name);
}
@@ -73,7 +56,13 @@ public class TypeUtils {
@NotNull
@Override
public Annotations getAnnotations() {
public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) {
throw new IllegalStateException(name);
}
@NotNull
@Override
public SimpleType makeNullableAsSpecified(boolean newNullability) {
throw new IllegalStateException(name);
}
@@ -81,12 +70,6 @@ public class TypeUtils {
public String toString() {
return name;
}
@Nullable
@Override
public SimpleType getAbbreviatedType() {
return null;
}
}
@NotNull
@@ -129,17 +112,7 @@ public class TypeUtils {
@NotNull
public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) {
KotlinType unwrappedType = KotlinTypeKt.unwrap(type);
if (unwrappedType instanceof TypeWithCustomReplacement) {
return ((TypeWithCustomReplacement) unwrappedType).makeNullableAsSpecified(nullable);
}
else {
SimpleType simpleType = (SimpleType) unwrappedType;
if (!(simpleType instanceof LazyType) && simpleType.isMarkedNullable() == nullable) return simpleType;
return KotlinTypeKt.lazyReplaceNullability(simpleType, nullable);
}
return type.unwrap().makeNullableAsSpecified(nullable);
}
@NotNull
@@ -431,7 +404,7 @@ public class TypeUtils {
if (type == null) return false;
if (isSpecialType.invoke(type)) return true;
KotlinType unwrappedType = KotlinTypeKt.unwrap(type);
UnwrappedType unwrappedType = type.unwrap();
FlexibleType flexibleType = unwrappedType instanceof FlexibleType ? (FlexibleType) unwrappedType : null;
if (flexibleType != null
&& (contains(flexibleType.getLowerBound(), isSpecialType) || contains(flexibleType.getUpperBound(), isSpecialType))) {
@@ -85,10 +85,7 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro
fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType {
if (annotations.isEmpty() && newAnnotations.isEmpty()) return this
val unwrappedType = unwrap()
if (unwrappedType is TypeWithCustomReplacement) return unwrappedType.replaceAnnotations(newAnnotations)
return asSimpleType().lazyReplaceAnnotations(newAnnotations)
return unwrap().replaceAnnotations(newAnnotations)
}
fun KotlinTypeChecker.equalTypesOrNulls(type1: KotlinType?, type2: KotlinType?): Boolean {
@@ -165,7 +162,7 @@ fun KotlinType.replaceArgumentsWithStarProjections(): KotlinType {
)
}
else {
return asSimpleType().replaceArgumentsWithStarProjections()
return (unwrapped as SimpleType).replaceArgumentsWithStarProjections()
}
}
@@ -36,17 +36,15 @@ fun KotlinType.isDynamic(): Boolean = unwrap() is DynamicType
fun createDynamicType(builtIns: KotlinBuiltIns) = DynamicType(builtIns, Annotations.EMPTY)
class DynamicType(builtIns: KotlinBuiltIns, override val annotations: Annotations) : DelegatingFlexibleType(builtIns.nothingType, builtIns.nullableAnyType) {
override val delegateType: KotlinType get() = upperBound
class DynamicType(builtIns: KotlinBuiltIns, override val annotations: Annotations) : FlexibleType(builtIns.nothingType, builtIns.nullableAnyType) {
override val delegate: SimpleType get() = upperBound
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
// Nullability has no effect on dynamics
return createDynamicType(delegateType.builtIns)
}
// Nullability has no effect on dynamics
override fun makeNullableAsSpecified(newNullability: Boolean): DynamicType = this
override val isMarkedNullable: Boolean get() = false
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = DynamicType(delegateType.builtIns, annotations)
override fun replaceAnnotations(newAnnotations: Annotations): DynamicType = DynamicType(delegate.builtIns, annotations)
override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String = "dynamic"
}
@@ -23,21 +23,6 @@ import org.jetbrains.kotlin.renderer.DescriptorRendererOptions
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
interface FlexibleType : SubtypingRepresentatives, TypeWithCustomReplacement {
// lowerBound is a subtype of upperBound
val lowerBound: SimpleType
val upperBound: SimpleType
override val subTypeRepresentative: KotlinType
get() = lowerBound
override val superTypeRepresentative: KotlinType
get() = upperBound
override fun sameTypeConstructor(type: KotlinType) = false
fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String
}
fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType
fun KotlinType.asFlexibleType(): FlexibleType = unwrap() as FlexibleType
@@ -84,21 +69,18 @@ fun Collection<TypeProjection>.singleBestRepresentative(): TypeProjection? {
fun KotlinType.lowerIfFlexible(): SimpleType = (if (this.isFlexible()) this.asFlexibleType().lowerBound else this).asSimpleType()
fun KotlinType.upperIfFlexible(): SimpleType = (if (this.isFlexible()) this.asFlexibleType().upperBound else this).asSimpleType()
abstract class DelegatingFlexibleType protected constructor(
override val lowerBound: SimpleType,
override val upperBound: SimpleType
) : DelegatingType(), FlexibleType {
class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), CustomTypeVariable {
companion object {
@JvmField
var RUN_SLOW_ASSERTIONS = false
}
// These assertions are needed for checking invariants of flexible types.
//
// Unfortunately isSubtypeOf is running resolve for lazy types.
// Because of this we can't run these assertions when we are creating this type. See EA-74904
//
// Also isSubtypeOf is not a very fast operation, so we are running assertions only if ASSERTIONS_ENABLED. See KT-7540
// These assertions are needed for checking invariants of flexible types.
//
// Unfortunately isSubtypeOf is running resolve for lazy types.
// Because of this we can't run these assertions when we are creating this type. See EA-74904
//
// Also isSubtypeOf is not a very fast operation, so we are running assertions only if ASSERTIONS_ENABLED. See KT-7540
private var assertionsDone = false
private fun runAssertions() {
@@ -113,39 +95,25 @@ abstract class DelegatingFlexibleType protected constructor(
}
}
protected abstract val delegateType: KotlinType
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
return KotlinTypeFactory.flexibleType(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable))
}
override fun getDelegate(): KotlinType {
runAssertions()
return delegateType
}
override fun toString() = "('$lowerBound'..'$upperBound')"
}
class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFlexibleType(lowerBound, upperBound), CustomTypeVariable {
override val delegateType: KotlinType get() = lowerBound
override val delegate: SimpleType
get() {
runAssertions()
return lowerBound
}
override val isTypeVariable: Boolean get() = lowerBound.constructor.declarationDescriptor is TypeParameterDescriptor
&& lowerBound.constructor == upperBound.constructor
override fun substitutionResult(replacement: KotlinType): KotlinType {
if (replacement.isFlexible()) {
return replacement
}
else {
val simpleType = replacement.asSimpleType()
return KotlinTypeFactory.flexibleType(simpleType, TypeUtils.makeNullable(simpleType))
val unwrapped = replacement.unwrap()
return when(unwrapped) {
is FlexibleType -> unwrapped
is SimpleType -> KotlinTypeFactory.flexibleType(unwrapped, TypeUtils.makeNullable(unwrapped))
}
}
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType
= KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations).asSimpleType(), upperBound)
override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType
= KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations))
override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String {
if (options.debugMode) {
@@ -153,4 +121,7 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Delegat
}
return renderer.renderFlexibleType(renderer.renderType(lowerBound), renderer.renderType(upperBound))
}
override fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType
= KotlinTypeFactory.flexibleType(lowerBound.makeNullableAsSpecified(newNullability), upperBound.makeNullableAsSpecified(newNullability))
}
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.utils.toReadOnlyList
class DeserializedType(
class DeserializedType private constructor(
private val c: DeserializationContext,
private val typeProto: ProtoBuf.Type,
private val additionalAnnotations: Annotations = Annotations.EMPTY
@@ -54,9 +54,18 @@ class DeserializedType(
return descriptor != null && ErrorUtils.isError(descriptor)
}
override val abbreviatedType by c.storageManager.createNullableLazyValue {
val abbreviatedTypeProto = typeProto.abbreviatedType(c.typeTable) ?: return@createNullableLazyValue null
companion object {
fun create(
c: DeserializationContext,
typeProto: ProtoBuf.Type,
additionalAnnotations: Annotations
): SimpleType {
val deserializedType = DeserializedType(c, typeProto, additionalAnnotations)
val abbreviatedTypeProto = typeProto.abbreviatedType(c.typeTable) ?: return deserializedType
c.typeDeserializer.type(abbreviatedTypeProto, additionalAnnotations).asSimpleType()
return deserializedType.withAbbreviatedType(DeserializedType(c, abbreviatedTypeProto, additionalAnnotations))
}
}
}
@@ -58,12 +58,12 @@ class TypeDeserializer(
fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType {
if (proto.hasFlexibleTypeCapabilitiesId()) {
val id = c.nameResolver.getString(proto.flexibleTypeCapabilitiesId)
val lowerBound = DeserializedType(c, proto, additionalAnnotations)
val upperBound = DeserializedType(c, proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations)
val lowerBound = DeserializedType.create(c, proto, additionalAnnotations)
val upperBound = DeserializedType.create(c, proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations)
return c.components.flexibleTypeDeserializer.create(proto, id, lowerBound, upperBound)
}
return DeserializedType(c, proto, additionalAnnotations)
return DeserializedType.create(c, proto, additionalAnnotations)
}
fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor =
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.types.FlexibleType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.unwrap
fun KtFunctionLiteral.findLabelAndCall(): Pair<Name?, KtCallExpression?> {
val literalParent = (this.parent as KtLambdaExpression).parent
@@ -89,8 +89,7 @@ private object ForceTypeCopySubstitution : TypeSubstitution() {
override fun get(key: KotlinType) =
with(key) {
if (isError) return@with asTypeProjection()
KotlinTypeImpl.create(
annotations, constructor, isMarkedNullable, arguments, memberScope).asTypeProjection()
KotlinTypeFactory.simpleType(annotations, constructor, arguments, isMarkedNullable, memberScope).asTypeProjection()
}
override fun isEmpty() = false
@@ -20,10 +20,10 @@ import com.google.protobuf.ExtensionRegistryLite
import org.jetbrains.kotlin.serialization.KotlinSerializerExtensionBase
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol
import org.jetbrains.kotlin.types.DelegatingFlexibleType
import org.jetbrains.kotlin.types.FlexibleType
class KotlinJavascriptSerializerExtension : KotlinSerializerExtensionBase(JsSerializerProtocol) {
override fun serializeFlexibleType(flexibleType: DelegatingFlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) {
override fun serializeFlexibleType(flexibleType: FlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) {
lowerProto.flexibleTypeCapabilitiesId = stringTable.getStringIndex(DynamicTypeDeserializer.id)
}
}