TypeProjection interface extracted

This commit is contained in:
Andrey Breslav
2013-09-28 12:02:21 +02:00
parent b98ec9f56b
commit 3960426698
35 changed files with 167 additions and 115 deletions
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.TypeProjectionImpl;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import java.util.*;
@@ -263,7 +264,7 @@ public final class DescriptorResolverUtils {
for (Map.Entry<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameter : originalToAltTypeParameters
.entrySet()) {
typeSubstitutionContext.put(originalToAltTypeParameter.getKey().getTypeConstructor(),
new TypeProjection(originalToAltTypeParameter.getValue().getDefaultType()));
new TypeProjectionImpl(originalToAltTypeParameter.getValue().getDefaultType()));
}
return TypeSubstitutor.create(typeSubstitutionContext);
}
@@ -30,7 +30,7 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.JetTypeImpl;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.TypeProjectionImpl;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import javax.inject.Inject;
@@ -157,7 +157,7 @@ public final class JavaAnnotationArgumentResolver {
ClassDescriptor jlClass = classResolver.resolveClass(JL_CLASS_FQ_NAME, IGNORE_KOTLIN_SOURCES);
if (jlClass == null) return null;
List<TypeProjection> arguments = Collections.singletonList(new TypeProjection(type));
List<TypeProjectionImpl> arguments = Collections.singletonList(new TypeProjectionImpl(type));
JetTypeImpl javaClassType = new JetTypeImpl(
jlClass.getAnnotations(),
jlClass.getTypeConstructor(),
@@ -51,7 +51,7 @@ public class JavaTypeTransformer {
@NotNull TypeUsage howThisTypeIsUsed
) {
if (!(type instanceof JavaWildcardType)) {
return new TypeProjection(transformToType(type, howThisTypeIsUsed, typeVariableResolver));
return new TypeProjectionImpl(transformToType(type, howThisTypeIsUsed, typeVariableResolver));
}
JavaWildcardType wildcardType = (JavaWildcardType) type;
@@ -62,7 +62,7 @@ public class JavaTypeTransformer {
Variance variance = wildcardType.isExtends() ? OUT_VARIANCE : IN_VARIANCE;
return new TypeProjection(variance, transformToType(bound, UPPER_BOUND, typeVariableResolver));
return new TypeProjectionImpl(variance, transformToType(bound, UPPER_BOUND, typeVariableResolver));
}
@NotNull
@@ -181,7 +181,7 @@ public class JavaTypeTransformer {
Variance projectionKind = parameter.getVariance() == OUT_VARIANCE || howThisTypeIsUsed == SUPERTYPE
? INVARIANT
: OUT_VARIANCE;
arguments.add(new TypeProjection(projectionKind, KotlinBuiltIns.getInstance().getNullableAnyType()));
arguments.add(new TypeProjectionImpl(projectionKind, KotlinBuiltIns.getInstance().getNullableAnyType()));
}
}
else {
@@ -190,7 +190,7 @@ public class JavaTypeTransformer {
if (parameters.size() != javaTypeArguments.size()) {
// Most of the time this means there is an error in the Java code
for (TypeParameterDescriptor parameter : parameters) {
arguments.add(new TypeProjection(ErrorUtils.createErrorType(parameter.getName().asString())));
arguments.add(new TypeProjectionImpl(ErrorUtils.createErrorType(parameter.getName().asString())));
}
}
else {
@@ -204,7 +204,7 @@ public class JavaTypeTransformer {
if (typeProjection.getProjectionKind() == typeParameterDescriptor.getVariance()) {
// remove redundant 'out' and 'in'
arguments.add(new TypeProjection(INVARIANT, typeProjection.getType()));
arguments.add(new TypeProjectionImpl(INVARIANT, typeProjection.getType()));
}
else {
arguments.add(typeProjection);
@@ -63,7 +63,7 @@ public class SingleAbstractMethodUtils {
Variance kind = argument.getProjectionKind();
if (kind != INVARIANT && variance != INVARIANT) {
if (kind == variance) {
arguments.add(new TypeProjection(argument.getType()));
arguments.add(new TypeProjectionImpl(argument.getType()));
}
else {
return null;
@@ -29,7 +29,7 @@ import java.util.List;
public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor, ClassOrNamespaceDescriptor {
@NotNull
JetScope getMemberScope(List<TypeProjection> typeArguments);
JetScope getMemberScope(List<? extends TypeProjection> typeArguments);
@NotNull
JetScope getUnsubstitutedInnerClassesScope();
@@ -37,7 +37,7 @@ public abstract class AbstractClassDescriptor implements ClassDescriptor {
@NotNull
@Override
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
public JetScope getMemberScope(List<? extends TypeProjection> typeArguments) {
assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Illegal number of type arguments: expected "
+ getTypeConstructor().getParameters().size() + " but was " + typeArguments.size()
+ " for " + getTypeConstructor() + " " + getTypeConstructor().getParameters();
@@ -97,7 +97,7 @@ public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implem
@Override
@NotNull
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
public JetScope getMemberScope(List<? extends TypeProjection> typeArguments) {
assert typeArguments.size() == typeConstructor.getParameters().size() : typeArguments;
if (typeConstructor.getParameters().isEmpty()) {
return memberDeclarations;
@@ -87,7 +87,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
@NotNull
@Override
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
public JetScope getMemberScope(List<? extends TypeProjection> typeArguments) {
JetScope memberScope = original.getMemberScope(typeArguments);
if (originalSubstitutor.isEmpty()) {
return memberScope;
@@ -261,7 +261,7 @@ public class OverridingUtil {
TypeParameterDescriptor subTypeParameter = subTypeParameters.get(i);
substitutionContext.put(
superTypeParameter.getTypeConstructor(),
new TypeProjection(subTypeParameter.getDefaultType()));
new TypeProjectionImpl(subTypeParameter.getDefaultType()));
}
return TypeSubstitutor.create(substitutionContext);
}
@@ -126,9 +126,9 @@ public class ConstraintSystemImpl implements ConstraintSystem {
};
public ConstraintSystemImpl() {
this.resultingSubstitutor = createTypeSubstitutorWithDefaultForUnknownTypeParameter(new TypeProjection(
this.resultingSubstitutor = createTypeSubstitutorWithDefaultForUnknownTypeParameter(new TypeProjectionImpl(
TypeUtils.CANT_INFER_TYPE_PARAMETER));
this.currentSubstitutor = createTypeSubstitutorWithDefaultForUnknownTypeParameter(new TypeProjection(TypeUtils.DONT_CARE));
this.currentSubstitutor = createTypeSubstitutorWithDefaultForUnknownTypeParameter(new TypeProjectionImpl(TypeUtils.DONT_CARE));
}
private TypeSubstitutor createTypeSubstitutorWithDefaultForUnknownTypeParameter(@Nullable final TypeProjection defaultTypeProjection) {
@@ -142,7 +142,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
if (typeParameterBounds.containsKey(descriptor)) {
JetType value = getTypeBounds(descriptor).getValue();
if (value != null && !TypeUtils.equalsOrContainsAsArgument(value, TypeUtils.DONT_CARE)) {
return new TypeProjection(value);
return new TypeProjectionImpl(value);
}
return defaultTypeProjection;
}
@@ -219,20 +219,20 @@ public class CommonSupertypes {
JetType intersection = TypeUtils.intersect(JetTypeChecker.INSTANCE, ins);
if (intersection == null) {
if (outs != null) {
return new TypeProjection(OUT_VARIANCE, commonSupertype(outs));
return new TypeProjectionImpl(OUT_VARIANCE, commonSupertype(outs));
}
return new TypeProjection(OUT_VARIANCE, commonSupertype(parameterDescriptor.getUpperBounds()));
return new TypeProjectionImpl(OUT_VARIANCE, commonSupertype(parameterDescriptor.getUpperBounds()));
}
Variance projectionKind = variance == IN_VARIANCE ? Variance.INVARIANT : IN_VARIANCE;
return new TypeProjection(projectionKind, intersection);
return new TypeProjectionImpl(projectionKind, intersection);
}
else if (outs != null) {
Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE;
return new TypeProjection(projectionKind, commonSupertype(outs));
return new TypeProjectionImpl(projectionKind, commonSupertype(outs));
}
else {
Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE;
return new TypeProjection(projectionKind, commonSupertype(parameterDescriptor.getUpperBounds()));
return new TypeProjectionImpl(projectionKind, commonSupertype(parameterDescriptor.getUpperBounds()));
}
}
@@ -77,7 +77,7 @@ public class DescriptorSubstitutor {
descriptor.getIndex());
substituted.setInitialized();
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjection(substituted.getDefaultType()));
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
substitutedMap.put(descriptor, substituted);
result.add(substituted);
@@ -106,7 +106,7 @@ public class DescriptorSubstitutor {
for (TypeParameterDescriptor descriptor : topologicallySortTypeParameters(typeParameters)) {
JetType upperBoundsAsType = descriptor.getUpperBoundsAsType();
JetType substitutedUpperBoundsAsType = substitutor.substitute(upperBoundsAsType, Variance.INVARIANT);
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjection(substitutedUpperBoundsAsType));
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substitutedUpperBoundsAsType));
}
return substitutor;
@@ -29,11 +29,11 @@ import java.util.List;
public final class JetTypeImpl extends AnnotatedImpl implements JetType {
private final TypeConstructor constructor;
private final List<TypeProjection> arguments;
private final List<? extends TypeProjection> arguments;
private final boolean nullable;
private final JetScope memberScope;
public JetTypeImpl(List<AnnotationDescriptor> annotations, TypeConstructor constructor, boolean nullable, @NotNull List<TypeProjection> arguments, JetScope memberScope) {
public JetTypeImpl(List<AnnotationDescriptor> annotations, TypeConstructor constructor, boolean nullable, @NotNull List<? extends TypeProjection> arguments, JetScope memberScope) {
super(annotations);
if (memberScope instanceof ErrorUtils.ErrorScope) {
@@ -67,7 +67,8 @@ public final class JetTypeImpl extends AnnotatedImpl implements JetType {
@NotNull
@Override
public List<TypeProjection> getArguments() {
return arguments;
//noinspection unchecked
return (List) arguments;
}
@Override
@@ -98,13 +98,13 @@ public class SubstitutionUtils {
}
@NotNull
public static Map<TypeConstructor, TypeProjection> buildSubstitutionContext(@NotNull List<TypeParameterDescriptor> parameters, @NotNull List<TypeProjection> contextArguments) {
public static Map<TypeConstructor, TypeProjection> buildSubstitutionContext(@NotNull List<TypeParameterDescriptor> parameters, @NotNull List<? extends TypeProjection> contextArguments) {
Map<TypeConstructor, TypeProjection> parameterValues = new HashMap<TypeConstructor, TypeProjection>();
fillInSubstitutionContext(parameters, contextArguments, parameterValues);
return parameterValues;
}
private static void fillInSubstitutionContext(List<TypeParameterDescriptor> parameters, List<TypeProjection> contextArguments, Map<TypeConstructor, TypeProjection> parameterValues) {
private static void fillInSubstitutionContext(List<TypeParameterDescriptor> parameters, List<? extends TypeProjection> contextArguments, Map<TypeConstructor, TypeProjection> parameterValues) {
if (parameters.size() != contextArguments.size()) {
throw new IllegalArgumentException("type parameter count != context arguments");
}
@@ -117,7 +117,7 @@ public class SubstitutionUtils {
@NotNull
public static TypeProjection makeStarProjection(@NotNull TypeParameterDescriptor parameterDescriptor) {
return new TypeProjection(parameterDescriptor.getVariance() == Variance.OUT_VARIANCE
return new TypeProjectionImpl(parameterDescriptor.getVariance() == Variance.OUT_VARIANCE
? Variance.INVARIANT
: Variance.OUT_VARIANCE, parameterDescriptor.getUpperBoundsAsType());
}
@@ -18,54 +18,10 @@ package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
public class TypeProjection {
private final Variance projection;
private final JetType type;
public TypeProjection(@NotNull Variance projection, @NotNull JetType type) {
this.projection = projection;
this.type = type;
}
public TypeProjection(JetType type) {
this(Variance.INVARIANT, type);
}
public interface TypeProjection {
@NotNull
Variance getProjectionKind();
@NotNull
public Variance getProjectionKind() {
return projection;
}
@NotNull
public JetType getType() {
return type;
}
@Override
public String toString() {
if (projection == Variance.INVARIANT) {
return type.toString();
}
return projection + " " + type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeProjection that = (TypeProjection) o;
if (projection != that.projection) return false;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
return true;
}
@Override
public int hashCode() {
int result = projection != null ? projection.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
JetType getType();
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2013 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.jet.lang.types;
public abstract class TypeProjectionBase implements TypeProjection {
@Override
public String toString() {
if (getProjectionKind() == Variance.INVARIANT) {
return getType().toString();
}
return getProjectionKind() + " " + getType();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TypeProjection)) return false;
TypeProjection that = (TypeProjection) o;
if (getProjectionKind() != that.getProjectionKind()) return false;
if (!getType().equals(that.getType())) return false;
return true;
}
@Override
public int hashCode() {
int result = getProjectionKind().hashCode();
result = 31 * result + (getType().hashCode());
return result;
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2013 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.jet.lang.types;
import org.jetbrains.annotations.NotNull;
public class TypeProjectionImpl extends TypeProjectionBase {
private final Variance projection;
private final JetType type;
public TypeProjectionImpl(@NotNull Variance projection, @NotNull JetType type) {
this.projection = projection;
this.type = type;
}
public TypeProjectionImpl(@NotNull JetType type) {
this(Variance.INVARIANT, type);
}
@Override
@NotNull
public Variance getProjectionKind() {
return projection;
}
@Override
@NotNull
public JetType getType() {
return type;
}
}
@@ -114,7 +114,7 @@ public class TypeSubstitutor {
}
try {
return unsafeSubstitute(new TypeProjection(howThisTypeIsUsed, type), 0).getType();
return unsafeSubstitute(new TypeProjectionImpl(howThisTypeIsUsed, type), 0).getType();
} catch (SubstitutionException e) {
return ErrorUtils.createErrorType(e.getMessage());
}
@@ -122,7 +122,7 @@ public class TypeSubstitutor {
@Nullable
public JetType substitute(@NotNull JetType type, @NotNull Variance howThisTypeIsUsed) {
TypeProjection projection = substitute(new TypeProjection(howThisTypeIsUsed, type));
TypeProjection projection = substitute(new TypeProjectionImpl(howThisTypeIsUsed, type));
return projection == null ? null : projection.getType();
}
@@ -162,7 +162,7 @@ public class TypeSubstitutor {
JetType substitutedType = TypeUtils.makeNullableAsSpecified(replacement.getType(), resultingIsNullable);
Variance resultingProjectionKind = combine(originalProjection.getProjectionKind(), replacement.getProjectionKind());
return new TypeProjection(resultingProjectionKind, substitutedType);
return new TypeProjectionImpl(resultingProjectionKind, substitutedType);
default:
throw new IllegalStateException();
}
@@ -177,7 +177,7 @@ public class TypeSubstitutor {
type.isNullable(), // Same nullability
substitutedArguments,
new SubstitutingScope(type.getMemberScope(), this));
return new TypeProjection(originalProjection.getProjectionKind(), substitutedType);
return new TypeProjectionImpl(originalProjection.getProjectionKind(), substitutedType);
}
}
@@ -194,7 +194,7 @@ public class TypeSubstitutor {
case NO_CONFLICT:
// if the corresponding type parameter is already co/contra-variant, there's not need for an explicit projection
if (typeParameter.getVariance() != Variance.INVARIANT) {
substitutedTypeArgument = new TypeProjection(Variance.INVARIANT, substitutedTypeArgument.getType());
substitutedTypeArgument = new TypeProjectionImpl(Variance.INVARIANT, substitutedTypeArgument.getType());
}
break;
case OUT_IN_IN_POSITION:
@@ -381,7 +381,7 @@ public class TypeUtils {
public static List<TypeProjection> getDefaultTypeProjections(List<TypeParameterDescriptor> parameters) {
List<TypeProjection> result = new ArrayList<TypeProjection>();
for (TypeParameterDescriptor parameterDescriptor : parameters) {
result.add(new TypeProjection(parameterDescriptor.getDefaultType()));
result.add(new TypeProjectionImpl(parameterDescriptor.getDefaultType()));
}
return result;
}
@@ -472,7 +472,7 @@ public class TypeUtils {
List<TypeProjection> projections = ContainerUtil.map(typeArguments, new com.intellij.util.Function<JetType, TypeProjection>() {
@Override
public TypeProjection fun(JetType type) {
return new TypeProjection(type);
return new TypeProjectionImpl(type);
}
});
@@ -713,7 +713,7 @@ public class TypeUtils {
for (TypeParameterDescriptor typeParameterDescriptor : typeParameterDescriptors) {
constructors.add(typeParameterDescriptor.getTypeConstructor());
}
final TypeProjection projection = new TypeProjection(type);
final TypeProjection projection = new TypeProjectionImpl(type);
return TypeSubstitutor.create(new TypeSubstitution() {
@Override
@@ -685,7 +685,7 @@ public class KotlinBuiltIns {
@NotNull
public JetType getArrayType(@NotNull Variance projectionType, @NotNull JetType argument) {
List<TypeProjection> types = Collections.singletonList(new TypeProjection(projectionType, argument));
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return new JetTypeImpl(
Collections.<AnnotationDescriptor>emptyList(),
getArray().getTypeConstructor(),
@@ -703,7 +703,7 @@ public class KotlinBuiltIns {
@NotNull
public JetType getEnumType(@NotNull JetType argument) {
Variance projectionType = Variance.INVARIANT;
List<TypeProjection> types = Collections.singletonList(new TypeProjection(projectionType, argument));
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return new JetTypeImpl(
Collections.<AnnotationDescriptor>emptyList(),
getEnum().getTypeConstructor(),
@@ -798,7 +798,7 @@ public class KotlinBuiltIns {
}
private static TypeProjection defaultProjection(JetType returnType) {
return new TypeProjection(Variance.INVARIANT, returnType);
return new TypeProjectionImpl(Variance.INVARIANT, returnType);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////