Introduce SimpleType.

This commit is contained in:
Stanislav Erokhin
2016-05-20 18:06:37 +03:00
parent 44829a61e7
commit 8c6dd95e3f
18 changed files with 78 additions and 57 deletions
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeImpl
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeProjection
internal fun createValueParametersForInvokeInFunctionType(
@@ -54,7 +55,7 @@ fun createFunctionType(
receiverType: KotlinType?,
parameterTypes: List<KotlinType>,
returnType: KotlinType
): KotlinType {
): SimpleType {
val arguments = getFunctionTypeArgumentProjections(receiverType, parameterTypes, returnType)
val size = parameterTypes.size
val classDescriptor = builtIns.getFunction(if (receiverType == null) size else size + 1)
@@ -54,10 +54,7 @@ import org.jetbrains.kotlin.storage.MemoizedFunctionToNotNull;
import org.jetbrains.kotlin.storage.NotNullLazyValue;
import org.jetbrains.kotlin.storage.NullableLazyValue;
import org.jetbrains.kotlin.storage.StorageManager;
import org.jetbrains.kotlin.types.AbstractClassTypeConstructor;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeConstructor;
import org.jetbrains.kotlin.types.TypeUtils;
import org.jetbrains.kotlin.types.*;
import java.util.ArrayList;
import java.util.Collection;
@@ -693,7 +690,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
KtClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
if (classOrObject == null) {
return Collections.singleton(c.getModuleDescriptor().getBuiltIns().getAnyType());
return Collections.<KotlinType>singleton(c.getModuleDescriptor().getBuiltIns().getAnyType());
}
List<KotlinType> allSupertypes =
@@ -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.
@@ -606,22 +606,22 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getNothingType() {
public SimpleType getNothingType() {
return getNothing().getDefaultType();
}
@NotNull
public KotlinType getNullableNothingType() {
public SimpleType getNullableNothingType() {
return TypeUtils.makeNullable(getNothingType());
}
@NotNull
public KotlinType getAnyType() {
public SimpleType getAnyType() {
return getAny().getDefaultType();
}
@NotNull
public KotlinType getNullableAnyType() {
public SimpleType getNullableAnyType() {
return TypeUtils.makeNullable(getAnyType());
}
@@ -631,7 +631,7 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getPrimitiveKotlinType(@NotNull PrimitiveType type) {
public SimpleType getPrimitiveKotlinType(@NotNull PrimitiveType type) {
return getPrimitiveClassDescriptor(type).getDefaultType();
}
@@ -676,7 +676,7 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getUnitType() {
public SimpleType getUnitType() {
return getUnit().getDefaultType();
}
@@ -728,7 +728,7 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getArrayType(@NotNull Variance projectionType, @NotNull KotlinType argument) {
public SimpleType getArrayType(@NotNull Variance projectionType, @NotNull KotlinType argument) {
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return KotlinTypeImpl.create(
Annotations.Companion.getEMPTY(),
@@ -739,7 +739,7 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getEnumType(@NotNull KotlinType argument) {
public SimpleType getEnumType(@NotNull SimpleType argument) {
Variance projectionType = Variance.INVARIANT;
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return KotlinTypeImpl.create(
@@ -751,7 +751,7 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public KotlinType getAnnotationType() {
public SimpleType getAnnotationType() {
return getAnnotation().getDefaultType();
}
@@ -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.
@@ -20,7 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.SimpleType;
import org.jetbrains.kotlin.types.TypeProjection;
import org.jetbrains.kotlin.types.TypeSubstitution;
import org.jetbrains.kotlin.types.TypeSubstitutor;
@@ -57,7 +57,7 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
*/
@NotNull
@Override
KotlinType getDefaultType();
SimpleType getDefaultType();
@NotNull
@Override
@@ -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.
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.SimpleType;
import org.jetbrains.kotlin.types.TypeConstructor;
public interface ClassifierDescriptor extends DeclarationDescriptorNonRoot {
@@ -25,5 +25,5 @@ public interface ClassifierDescriptor extends DeclarationDescriptorNonRoot {
TypeConstructor getTypeConstructor();
@NotNull
KotlinType getDefaultType();
SimpleType getDefaultType();
}
@@ -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.
@@ -18,7 +18,9 @@ package org.jetbrains.kotlin.descriptors.impl;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorVisitor;
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.scopes.InnerClassesScopeWrapper;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
@@ -31,15 +33,15 @@ import java.util.List;
public abstract class AbstractClassDescriptor implements ClassDescriptor {
private final Name name;
protected final NotNullLazyValue<KotlinType> defaultType;
protected final NotNullLazyValue<SimpleType> defaultType;
private final NotNullLazyValue<MemberScope> unsubstitutedInnerClassesScope;
private final NotNullLazyValue<ReceiverParameterDescriptor> thisAsReceiverParameter;
public AbstractClassDescriptor(@NotNull StorageManager storageManager, @NotNull Name name) {
this.name = name;
this.defaultType = storageManager.createLazyValue(new Function0<KotlinType>() {
this.defaultType = storageManager.createLazyValue(new Function0<SimpleType>() {
@Override
public KotlinType invoke() {
public SimpleType invoke() {
return TypeUtils.makeUnsubstitutedType(AbstractClassDescriptor.this, getUnsubstitutedMemberScope());
}
});
@@ -113,7 +115,7 @@ public abstract class AbstractClassDescriptor implements ClassDescriptor {
@NotNull
@Override
public KotlinType getDefaultType() {
public SimpleType getDefaultType() {
return defaultType.invoke();
}
@@ -22,8 +22,8 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.TypeSubstitutor
abstract class AbstractTypeAliasDescriptor(
containingDeclaration: DeclarationDescriptor,
@@ -57,7 +57,7 @@ abstract class AbstractTypeAliasDescriptor(
override fun getVisibility() = visibilityImpl
override fun getDefaultType(): KotlinType =
override fun getDefaultType(): SimpleType =
TODO("typealias getDefaultType")
override fun getTypeConstructor(): TypeConstructor =
@@ -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.
@@ -41,7 +41,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
private final int index;
private final NotNullLazyValue<TypeConstructor> typeConstructor;
private final NotNullLazyValue<KotlinType> defaultType;
private final NotNullLazyValue<SimpleType> defaultType;
protected AbstractTypeParameterDescriptor(
@NotNull final StorageManager storageManager,
@@ -65,9 +65,9 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
return new TypeParameterTypeConstructor(storageManager, supertypeLoopChecker);
}
});
this.defaultType = storageManager.createLazyValue(new Function0<KotlinType>() {
this.defaultType = storageManager.createLazyValue(new Function0<SimpleType>() {
@Override
public KotlinType invoke() {
public SimpleType invoke() {
return KotlinTypeImpl.create(
Annotations.Companion.getEMPTY(),
getTypeConstructor(), false, Collections.<TypeProjection>emptyList(),
@@ -124,7 +124,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
@NotNull
@Override
public KotlinType getDefaultType() {
public SimpleType getDefaultType() {
return defaultType.invoke();
}
@@ -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.
@@ -130,7 +130,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
@NotNull
@Override
public KotlinType getDefaultType() {
public SimpleType getDefaultType() {
List<TypeProjection> typeProjections = TypeUtils.getDefaultTypeProjections(getTypeConstructor().getParameters());
return KotlinTypeImpl.create(
getAnnotations(),
@@ -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.
@@ -106,7 +106,7 @@ public abstract class AbstractClassTypeConstructor extends AbstractTypeConstruct
// class B : A.C {}
DeclarationDescriptor containingDeclaration = getDeclarationDescriptor().getContainingDeclaration();
if (containingDeclaration instanceof ClassDescriptor) {
return Collections.singleton(((ClassDescriptor) containingDeclaration).getDefaultType());
return Collections.<KotlinType>singleton(((ClassDescriptor) containingDeclaration).getDefaultType());
}
return Collections.emptyList();
}
@@ -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.
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.Iterator;
import java.util.List;
public abstract class AbstractKotlinType implements KotlinType {
public abstract class AbstractKotlinType implements KotlinType, SimpleType { // TODO temporary upper bound
@Nullable
@Override
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
@@ -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(), LazyType {
abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlinType(), SimpleType, LazyType {
private val typeConstructor = storageManager.createLazyValue { computeTypeConstructor() }
override val constructor by typeConstructor
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.List;
public abstract class DelegatingType implements KotlinType {
public abstract class DelegatingType implements KotlinType, SimpleType { // TODO temporary upper bound
protected abstract KotlinType getDelegate();
@NotNull
@@ -374,22 +374,22 @@ public class ErrorUtils {
}
@NotNull
public static KotlinType createErrorType(@NotNull String debugMessage) {
public static SimpleType createErrorType(@NotNull String debugMessage) {
return createErrorTypeWithArguments(debugMessage, Collections.<TypeProjection>emptyList());
}
@NotNull
public static KotlinType createErrorTypeWithCustomDebugName(@NotNull String debugName) {
public static SimpleType createErrorTypeWithCustomDebugName(@NotNull String debugName) {
return createErrorTypeWithCustomConstructor(debugName, createErrorTypeConstructorWithCustomDebugName(debugName));
}
@NotNull
public static KotlinType createErrorTypeWithCustomConstructor(@NotNull String debugName, @NotNull TypeConstructor typeConstructor) {
public static SimpleType createErrorTypeWithCustomConstructor(@NotNull String debugName, @NotNull TypeConstructor typeConstructor) {
return new ErrorTypeImpl(typeConstructor, createErrorScope(debugName));
}
@NotNull
public static KotlinType createErrorTypeWithArguments(@NotNull String debugMessage, @NotNull List<TypeProjection> arguments) {
public static SimpleType createErrorTypeWithArguments(@NotNull String debugMessage, @NotNull List<TypeProjection> arguments) {
return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments);
}
@@ -484,7 +484,7 @@ public class ErrorUtils {
);
}
private static class ErrorTypeImpl implements KotlinType {
private static class ErrorTypeImpl implements SimpleType {
private final TypeConstructor constructor;
private final MemberScope memberScope;
private final List<TypeProjection> arguments;
@@ -39,4 +39,10 @@ interface KotlinType : Annotated {
fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T?
val capabilities: TypeCapabilities
}
@Deprecated("Temporary marker method for refactoring")
fun KotlinType.asSimpleType(): SimpleType = this as SimpleType
interface SimpleType : KotlinType
@@ -27,7 +27,7 @@ private constructor(
override val isMarkedNullable: Boolean,
override val arguments: List<TypeProjection>,
final override val memberScope: MemberScope
) : AbstractKotlinType() {
) : AbstractKotlinType(), SimpleType {
companion object {
@JvmStatic fun create(annotations: Annotations,
@@ -33,10 +33,10 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.*;
public class TypeUtils {
public static final KotlinType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
public static final KotlinType CANT_INFER_FUNCTION_PARAM_TYPE = ErrorUtils.createErrorType("Cannot be inferred");
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 KotlinType {
public static class SpecialType implements SimpleType {
private final String name;
public SpecialType(String name) {
@@ -96,9 +96,9 @@ public class TypeUtils {
}
@NotNull
public static final KotlinType NO_EXPECTED_TYPE = new SpecialType("NO_EXPECTED_TYPE");
public static final SimpleType NO_EXPECTED_TYPE = new SpecialType("NO_EXPECTED_TYPE");
public static final KotlinType UNIT_EXPECTED_TYPE = new SpecialType("UNIT_EXPECTED_TYPE");
public static final SimpleType UNIT_EXPECTED_TYPE = new SpecialType("UNIT_EXPECTED_TYPE");
public static boolean noExpectedType(@NotNull KotlinType type) {
return type == NO_EXPECTED_TYPE || type == UNIT_EXPECTED_TYPE;
@@ -118,6 +118,21 @@ public class TypeUtils {
return makeNullableAsSpecified(type, false);
}
@NotNull
public static SimpleType makeNullable(@NotNull SimpleType type) {
return KotlinTypeKt.asSimpleType(makeNullableAsSpecified(type, true));
}
@NotNull
public static SimpleType makeNotNullable(@NotNull SimpleType type) {
return KotlinTypeKt.asSimpleType(makeNullableAsSpecified(type, false));
}
@NotNull
public static SimpleType makeNullableAsSpecified(@NotNull SimpleType type, boolean nullable) {
return KotlinTypeKt.asSimpleType(makeNullableAsSpecified((KotlinType) type, nullable));
}
@NotNull
public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) {
Flexibility flexibility = type.getCapability(Flexibility.class);
@@ -229,7 +244,7 @@ public class TypeUtils {
}
@NotNull
public static KotlinType makeUnsubstitutedType(ClassifierDescriptor classifierDescriptor, MemberScope unsubstitutedMemberScope) {
public static SimpleType makeUnsubstitutedType(ClassifierDescriptor classifierDescriptor, MemberScope unsubstitutedMemberScope) {
if (ErrorUtils.isError(classifierDescriptor)) {
return ErrorUtils.createErrorType("Unsubstituted type for " + classifierDescriptor);
}
@@ -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.
@@ -161,7 +161,7 @@ private fun KotlinType.isIntegerType(): Boolean {
private fun CallableDescriptor.getParametersTypes(): List<KotlinType> {
val list = arrayListOf((containingDeclaration as ClassDescriptor).defaultType)
val list = arrayListOf<KotlinType>((containingDeclaration as ClassDescriptor).defaultType)
valueParameters.map { it.type }.forEach {
list.add(TypeUtils.makeNotNullable(it))
}