use of org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef in project n4js by eclipse.
the class Reducer method reduceConstraintForTypeArgumentPair.
/**
* Will add a constraint ⟨ leftArg :> rightArg ⟩, taking into account wildcards, closed existential types, and
* definition site variance.
*/
private boolean reduceConstraintForTypeArgumentPair(TypeArgument leftArg, TypeVariable leftParam, TypeArgument rightArg) {
boolean wasAdded = false;
if (leftArg instanceof Wildcard) {
final TypeRef ub = ((Wildcard) leftArg).getDeclaredUpperBound();
if (ub != null) {
wasAdded |= reduce(ub, ts.upperBound(G, rightArg).getValue(), CONTRA);
}
final TypeRef lb = ((Wildcard) leftArg).getDeclaredLowerBound();
if (lb != null) {
wasAdded |= reduce(lb, ts.lowerBound(G, rightArg).getValue(), CO);
}
} else if (rightArg instanceof ExistentialTypeRef) {
// TODO IDE-1653 reconsider this entire case
// re-open the existential type, because we assume it was closed only while adding substitutions
// UPDATE: this is wrong if right.typeArgs already contained an ExistentialTypeRef! (but might be
// an non-harmful over approximation)
final Wildcard w = ((ExistentialTypeRef) rightArg).getWildcard();
final TypeRef ub = w.getDeclaredUpperBound();
if (ub != null) {
wasAdded |= reduce(ub, ts.upperBound(G, leftArg).getValue(), CONTRA);
}
final TypeRef lb = w.getDeclaredLowerBound();
if (lb != null) {
wasAdded |= reduce(lb, ts.lowerBound(G, leftArg).getValue(), CO);
}
} else {
if (!(leftArg instanceof TypeRef)) {
throw new UnsupportedOperationException("unsupported subtype of TypeArgument: " + leftArg.getClass().getName());
}
// due to the assumption of the method, we always have: leftArg :> rightArg
// (so for def-site variance we just look at the left side in this case, i.e. leftParam)
final Variance leftDefSiteVarianceRaw = leftParam.getVariance();
final Variance leftDefSiteVariance = leftDefSiteVarianceRaw != null ? leftDefSiteVarianceRaw : INV;
wasAdded |= reduce(leftArg, rightArg, CONTRA.mult(leftDefSiteVariance));
}
return wasAdded;
}
use of org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleUpperBoundExistentialTypeRef.
protected Result<TypeRef> applyRuleUpperBoundExistentialTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ExistentialTypeRef existentialTypeRef) throws RuleFailedException {
// output parameter
TypeRef T = null;
/* G |~ existentialTypeRef.wildcard /\ T */
Wildcard _wildcard = existentialTypeRef.getWildcard();
Result<TypeRef> result = upperBoundInternal(G, _trace_, _wildcard);
checkAssignableTo(result.getFirst(), TypeRef.class);
T = (TypeRef) result.getFirst();
T = TypeUtils.<TypeRef>copy(T);
TypeUtils.copyTypeModifiers(T, existentialTypeRef);
return new Result<TypeRef>(T);
}
use of org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef in project n4js by eclipse.
the class InternalTypeSystem method lowerBoundImpl.
protected Result<TypeRef> lowerBoundImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ExistentialTypeRef existentialTypeRef) throws RuleFailedException {
try {
final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
final Result<TypeRef> _result_ = applyRuleLowerBoundExistentialTypeRef(G, _subtrace_, existentialTypeRef);
addToTrace(_trace_, new Provider<Object>() {
public Object get() {
return ruleName("lowerBoundExistentialTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(existentialTypeRef) + " \\/ " + stringRep(_result_.getFirst());
}
});
addAsSubtrace(_trace_, _subtrace_);
return _result_;
} catch (Exception e_applyRuleLowerBoundExistentialTypeRef) {
lowerBoundThrowException(ruleName("lowerBoundExistentialTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(existentialTypeRef) + " \\/ " + "TypeRef", LOWERBOUNDEXISTENTIALTYPEREF, e_applyRuleLowerBoundExistentialTypeRef, existentialTypeRef, new ErrorInformation[] { new ErrorInformation(existentialTypeRef) });
return null;
}
}
use of org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleSubtypeTypeTypeRef.
protected Result<Boolean> applyRuleSubtypeTypeTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final TypeTypeRef left, final TypeTypeRef right) throws RuleFailedException {
final TypeArgument leftTypeArg = left.getTypeArg();
final TypeArgument rightTypeArg = right.getTypeArg();
final boolean leftIsCtorRef = left.isConstructorRef();
final boolean rightIsCtorRef = right.isConstructorRef();
final boolean rightHasTypeRef = (rightTypeArg instanceof TypeRef);
if (((!leftIsCtorRef) && rightIsCtorRef)) {
/* fail */
throwForExplicitFail();
} else {
if ((rightHasTypeRef && (!rightIsCtorRef))) {
/* G |- leftTypeArg <: rightTypeArg */
subtypeInternal(G, _trace_, leftTypeArg, rightTypeArg);
} else {
if ((rightHasTypeRef && rightIsCtorRef)) {
final Type left_staticType = this.typeSystemHelper.getStaticType(G, left);
final Type right_staticType = this.typeSystemHelper.getStaticType(G, right);
final boolean leftHasCovariantConstructor = ((left_staticType instanceof TClassifier) && N4JSLanguageUtils.hasCovariantConstructor(((TClassifier) left_staticType)));
/* !(leftTypeArg instanceof Wildcard || leftTypeArg instanceof ExistentialTypeRef || leftTypeArg instanceof ThisTypeRef) || leftHasCovariantConstructor */
if (!((!(((leftTypeArg instanceof Wildcard) || (leftTypeArg instanceof ExistentialTypeRef)) || (leftTypeArg instanceof ThisTypeRef))) || leftHasCovariantConstructor)) {
sneakyThrowRuleFailedException("!(leftTypeArg instanceof Wildcard || leftTypeArg instanceof ExistentialTypeRef || leftTypeArg instanceof ThisTypeRef) || leftHasCovariantConstructor");
}
/* G |- leftTypeArg <: rightTypeArg */
subtypeInternal(G, _trace_, leftTypeArg, rightTypeArg);
if (((left_staticType instanceof TypeVariable) || (right_staticType instanceof TypeVariable))) {
/* left_staticType === right_staticType */
if (!(left_staticType == right_staticType)) {
sneakyThrowRuleFailedException("left_staticType === right_staticType");
}
} else {
final TMethod leftCtor = this.containerTypesHelper.fromContext(RuleEnvironmentExtensions.getContextResource(G)).findConstructor(((ContainerType<?>) left_staticType));
final TMethod rightCtor = this.containerTypesHelper.fromContext(RuleEnvironmentExtensions.getContextResource(G)).findConstructor(((ContainerType<?>) right_staticType));
/* leftCtor!==null && rightCtor!==null */
if (!((leftCtor != null) && (rightCtor != null))) {
sneakyThrowRuleFailedException("leftCtor!==null && rightCtor!==null");
}
/* G |- leftCtor : var TypeRef leftCtorRef */
TypeRef leftCtorRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, leftCtor);
checkAssignableTo(result.getFirst(), TypeRef.class);
leftCtorRef = (TypeRef) result.getFirst();
/* G |- rightCtor : var TypeRef rightCtorRef */
TypeRef rightCtorRef = null;
Result<TypeRef> result_1 = typeInternal(G, _trace_, rightCtor);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
rightCtorRef = (TypeRef) result_1.getFirst();
final RuleEnvironment G_left = RuleEnvironmentExtensions.wrap(G);
final RuleEnvironment G_right = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G_left, TypeExtensions.ref(left_staticType));
RuleEnvironmentExtensions.addThisType(G_left, TypeExtensions.ref(left_staticType));
this.typeSystemHelper.addSubstitutions(G_right, TypeExtensions.ref(right_staticType));
RuleEnvironmentExtensions.addThisType(G_right, TypeExtensions.ref(right_staticType));
/* G_left |- leftCtorRef ~> var TypeRef leftCtorRefSubst */
TypeRef leftCtorRefSubst = null;
Result<TypeArgument> result_2 = substTypeVariablesInternal(G_left, _trace_, leftCtorRef);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
leftCtorRefSubst = (TypeRef) result_2.getFirst();
/* G_right |- rightCtorRef ~> var TypeRef rightCtorRefSubst */
TypeRef rightCtorRefSubst = null;
Result<TypeArgument> result_3 = substTypeVariablesInternal(G_right, _trace_, rightCtorRef);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
rightCtorRefSubst = (TypeRef) result_3.getFirst();
/* G |- leftCtorRefSubst <: rightCtorRefSubst */
subtypeInternal(G, _trace_, leftCtorRefSubst, rightCtorRefSubst);
}
} else {
/* G |~ leftTypeArg /\ var TypeRef upperBoundLeft */
TypeRef upperBoundLeft = null;
Result<TypeRef> result_4 = upperBoundInternal(G, _trace_, leftTypeArg);
checkAssignableTo(result_4.getFirst(), TypeRef.class);
upperBoundLeft = (TypeRef) result_4.getFirst();
/* G |~ leftTypeArg \/ var TypeRef lowerBoundLeft */
TypeRef lowerBoundLeft = null;
Result<TypeRef> result_5 = lowerBoundInternal(G, _trace_, leftTypeArg);
checkAssignableTo(result_5.getFirst(), TypeRef.class);
lowerBoundLeft = (TypeRef) result_5.getFirst();
/* G |~ rightTypeArg /\ var TypeRef upperBoundRight */
TypeRef upperBoundRight = null;
Result<TypeRef> result_6 = upperBoundInternal(G, _trace_, rightTypeArg);
checkAssignableTo(result_6.getFirst(), TypeRef.class);
upperBoundRight = (TypeRef) result_6.getFirst();
/* G |~ rightTypeArg \/ var TypeRef lowerBoundRight */
TypeRef lowerBoundRight = null;
Result<TypeRef> result_7 = lowerBoundInternal(G, _trace_, rightTypeArg);
checkAssignableTo(result_7.getFirst(), TypeRef.class);
lowerBoundRight = (TypeRef) result_7.getFirst();
/* G |- upperBoundLeft <: upperBoundRight */
subtypeInternal(G, _trace_, upperBoundLeft, upperBoundRight);
/* G |- lowerBoundRight <: lowerBoundLeft */
subtypeInternal(G, _trace_, lowerBoundRight, lowerBoundLeft);
}
}
}
return new Result<Boolean>(true);
}
use of org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef in project n4js by eclipse.
the class InternalTypeSystem method upperBoundImpl.
protected Result<TypeRef> upperBoundImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ExistentialTypeRef existentialTypeRef) throws RuleFailedException {
try {
final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
final Result<TypeRef> _result_ = applyRuleUpperBoundExistentialTypeRef(G, _subtrace_, existentialTypeRef);
addToTrace(_trace_, new Provider<Object>() {
public Object get() {
return ruleName("upperBoundExistentialTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(existentialTypeRef) + " /\\ " + stringRep(_result_.getFirst());
}
});
addAsSubtrace(_trace_, _subtrace_);
return _result_;
} catch (Exception e_applyRuleUpperBoundExistentialTypeRef) {
upperBoundThrowException(ruleName("upperBoundExistentialTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(existentialTypeRef) + " /\\ " + "TypeRef", UPPERBOUNDEXISTENTIALTYPEREF, e_applyRuleUpperBoundExistentialTypeRef, existentialTypeRef, new ErrorInformation[] { new ErrorInformation(existentialTypeRef) });
return null;
}
}
Aggregations