added renderers for constraint system and type bounds

moved classes
This commit is contained in:
Svetlana Isakova
2013-12-27 14:58:09 +04:00
parent 45abab2a2c
commit c5d9757b94
4 changed files with 77 additions and 20 deletions
@@ -40,8 +40,11 @@ import org.jetbrains.jet.renderer.Renderer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.lang.diagnostics.rendering.TabledDescriptorRenderer.*;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds.BoundKind.LOWER_BOUND;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds.BoundKind.UPPER_BOUND;
public class Renderers {
public static final Renderer<Object> TO_STRING = new Renderer<Object>() {
@@ -342,6 +345,45 @@ public class Renderers {
}
};
public static final Renderer<ConstraintSystem> RENDER_CONSTRAINT_SYSTEM = new Renderer<ConstraintSystem>() {
@NotNull
@Override
public String render(@NotNull ConstraintSystem constraintSystem) {
Set<TypeParameterDescriptor> typeVariables = constraintSystem.getTypeVariables();
Set<TypeBounds> typeBounds = Sets.newLinkedHashSet();
for (TypeParameterDescriptor variable : typeVariables) {
typeBounds.add(constraintSystem.getTypeBounds(variable));
}
Function<TypeBounds, String> renderTypeBounds = rendererToFunction(RENDER_TYPE_BOUNDS);
return "type parameter bounds: \n" + StringUtil.join(typeBounds, renderTypeBounds, ";\n") + "\n" +
"status:\n" + ConstraintsUtil.getDebugMessageForStatus(constraintSystem.getStatus());
}
};
public static final Renderer<TypeBounds> RENDER_TYPE_BOUNDS = new Renderer<TypeBounds>() {
@NotNull
@Override
public String render(@NotNull TypeBounds typeBounds) {
Function<TypeBoundsImpl.Bound, String> renderBound = new Function<TypeBoundsImpl.Bound, String>() {
@Override
public String fun(TypeBoundsImpl.Bound bound) {
String arrow = bound.kind == LOWER_BOUND ? "<: " : bound.kind == UPPER_BOUND ? ">: " : "= ";
return arrow + bound.type + '(' + bound.position + ')';
}
};
return typeBounds.getTypeVariable().getName() + " " + StringUtil.join(typeBounds.getBounds(), renderBound, ", ");
}
};
@NotNull
public static <T> Function<T, String> rendererToFunction(final @NotNull Renderer<T> renderer) {
return new Function<T, String>() {
@Override
public String fun(T t) {
return renderer.render(t);
}
};
}
private Renderers() {
}
@@ -40,8 +40,8 @@ import java.util.Set;
import static org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemImpl.ConstraintKind.EQUAL;
import static org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemImpl.ConstraintKind.SUB_TYPE;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBoundsImpl.BoundKind.*;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBoundsImpl.Bound;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds.BoundKind.*;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds.Bound;
import static org.jetbrains.jet.lang.types.TypeUtils.CANT_INFER_TYPE_PARAMETER;
import static org.jetbrains.jet.lang.types.TypeUtils.DONT_CARE;
@@ -18,16 +18,22 @@ package org.jetbrains.jet.lang.resolve.calls.inference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.Variance;
import java.util.Collection;
import java.util.Set;
public interface TypeBounds {
@NotNull
Variance getVarianceOfPosition();
@NotNull
TypeParameterDescriptor getTypeVariable();
@NotNull
Collection<Bound> getBounds();
boolean isEmpty();
@Nullable
@@ -35,4 +41,20 @@ public interface TypeBounds {
@NotNull
Collection<JetType> getValues();
enum BoundKind {
LOWER_BOUND, UPPER_BOUND, EXACT_BOUND
}
class Bound {
public final JetType type;
public final BoundKind kind;
public final ConstraintPosition position;
public Bound(@NotNull JetType type, @NotNull BoundKind kind, @NotNull ConstraintPosition position) {
this.type = type;
this.kind = kind;
this.position = position;
}
}
}
@@ -31,23 +31,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds.BoundKind.LOWER_BOUND;
public class TypeBoundsImpl implements TypeBounds {
public static enum BoundKind {
LOWER_BOUND, UPPER_BOUND, EXACT_BOUND
}
public static class Bound {
public final JetType type;
public final BoundKind kind;
public final ConstraintPosition position;
public Bound(@NotNull JetType type, @NotNull BoundKind kind, @NotNull ConstraintPosition position) {
this.type = type;
this.kind = kind;
this.position = position;
}
}
private final TypeParameterDescriptor typeVariable;
private final Variance varianceOfPosition;
private final Set<Bound> bounds = Sets.newLinkedHashSet();
@@ -78,6 +64,13 @@ public class TypeBoundsImpl implements TypeBounds {
return getValues().isEmpty();
}
@NotNull
@Override
public TypeParameterDescriptor getTypeVariable() {
return typeVariable;
}
@Override
@NotNull
public Collection<Bound> getBounds() {
return bounds;
@@ -175,7 +168,7 @@ public class TypeBoundsImpl implements TypeBounds {
values.addAll(exactBounds);
Pair<Collection<JetType>, Collection<JetType>> pair =
TypeUtils.filterNumberTypes(filterBounds(bounds, BoundKind.LOWER_BOUND, values));
TypeUtils.filterNumberTypes(filterBounds(bounds, LOWER_BOUND, values));
Collection<JetType> generalLowerBounds = pair.getFirst();
Collection<JetType> numberLowerBounds = pair.getSecond();