use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.
the class Reducer method reduceTypeTypeRef.
private boolean reduceTypeTypeRef(TypeTypeRef left, TypeTypeRef right, Variance variance) {
final TypeArgument leftStatic = TypeUtils.copy(left.getTypeArg());
final TypeArgument rightStatic = TypeUtils.copy(right.getTypeArg());
if (!left.isConstructorRef() && !right.isConstructorRef()) {
// both sides are plain TypeTypeRefs
return reduce(leftStatic, rightStatic, variance);
} else {
// TODO this is wrong
return reduce(leftStatic, rightStatic, INV);
// instead:
// ⟨ constructor{D} <: constructor{C} ⟩ implies ⟨ D <: C ⟩ (as above) and ⟨ D#constructor <: C#constructor ⟩
}
}
use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.
the class Reducer method reduceParameterizedTypeRefNominal.
/**
* Reduction for parameterized type references according to nominal subtyping rules.
* <p>
* NOTE: 'left' might be a structural type reference iff variance == CO / 'right' might be structural (iff variance
* == CONTRA) but that is irrelevant and the reduction must still follow nominal subtyping rules (because the RHS of
* the subtype relation determines whether to use nominal or structural rules).
*/
private boolean reduceParameterizedTypeRefNominal(ParameterizedTypeRef left, ParameterizedTypeRef right, Variance variance) {
// e.g., ⟨ Iterable3<int,string,int> :> Array<α> ⟩ should be reduced to ⟨ int >: α ⟩ and ⟨ string >: α ⟩
if ((variance == CO && isSpecialCaseOfArraySubtypeIterableN(left, right)) || (variance == CONTRA && isSpecialCaseOfArraySubtypeIterableN(right, left))) {
final List<TypeArgument> typeArgsOfArray = variance == CO ? left.getTypeArgs() : right.getTypeArgs();
final List<TypeArgument> typeArgsOfIterableN = variance == CO ? right.getTypeArgs() : left.getTypeArgs();
final List<TypeVariable> typeParamsOfIterableN = variance == CO ? right.getDeclaredType().getTypeVars() : left.getDeclaredType().getTypeVars();
final TypeArgument singleTypeArgOfArray = !typeArgsOfArray.isEmpty() ? typeArgsOfArray.get(0) : null;
boolean wasAdded = false;
final int len = Math.min(typeArgsOfIterableN.size(), typeParamsOfIterableN.size());
for (int idx = 0; idx < len; idx++) {
TypeArgument currTypeArgOfIterableN = typeArgsOfIterableN.get(idx);
TypeVariable curTypeParamOfIterableN = typeParamsOfIterableN.get(idx);
wasAdded |= reduceConstraintForTypeArgumentPair(currTypeArgOfIterableN, curTypeParamOfIterableN, singleTypeArgOfArray);
}
return wasAdded;
}
// standard cases:
final TypeRef leftRaw = TypeUtils.createTypeRef(left.getDeclaredType());
// note: enforcing nominal here!
final TypeRef rightRaw = TypeUtils.createTypeRef(right.getDeclaredType());
if ((variance == CO && !ts.subtypeSucceeded(G, leftRaw, rightRaw)) || (variance == CONTRA && !ts.subtypeSucceeded(G, rightRaw, leftRaw)) || (variance == INV && !ts.equaltypeSucceeded(G, leftRaw, rightRaw))) {
return giveUp(left, right, variance);
}
// IV <-> string
if (variance == CO) {
// normalize ⟨ B <: A ⟩ to ⟨ A :> B ⟩ to make sure the (raw) subtype is on the right-hand side
final ParameterizedTypeRef tmp = left;
left = right;
right = tmp;
variance = CONTRA;
}
boolean wasAdded = false;
final RuleEnvironment Gx = RuleEnvironmentExtensions.newRuleEnvironment(G);
tsh.addSubstitutions(Gx, right);
final Type leftType = left.getDeclaredType();
final List<TypeArgument> leftArgs = left.getTypeArgs();
final List<TypeVariable> leftParams = leftType.getTypeVars();
final int len = Math.min(leftArgs.size(), leftParams.size());
for (int idx = 0; idx < len; ++idx) {
final TypeArgument leftArg = leftArgs.get(idx);
final TypeVariable leftParam = leftParams.get(idx);
if (RuleEnvironmentExtensions.hasSubstitutionFor(Gx, leftParam)) {
final TypeArgument leftParamSubst = ts.substTypeVariables(Gx, TypeUtils.createTypeRef(leftParam)).getValue();
wasAdded |= reduceConstraintForTypeArgumentPair(leftArg, leftParam, leftParamSubst);
}
}
return wasAdded;
}
use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.
the class InternalTypeSystem method substTypeVariablesImpl.
protected Result<TypeArgument> substTypeVariablesImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final TypeArgument type) throws RuleFailedException {
try {
final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
final Result<TypeArgument> _result_ = applyRuleSubstTypeVariablesBaseCase(G, _subtrace_, type);
addToTrace(_trace_, new Provider<Object>() {
public Object get() {
return ruleName("substTypeVariablesBaseCase") + stringRepForEnv(G) + " |- " + stringRep(type) + " ~> " + stringRep(_result_.getFirst());
}
});
addAsSubtrace(_trace_, _subtrace_);
return _result_;
} catch (Exception e_applyRuleSubstTypeVariablesBaseCase) {
substTypeVariablesThrowException(ruleName("substTypeVariablesBaseCase") + stringRepForEnv(G) + " |- " + stringRep(type) + " ~> " + "TypeArgument", SUBSTTYPEVARIABLESBASECASE, e_applyRuleSubstTypeVariablesBaseCase, type, new ErrorInformation[] { new ErrorInformation(type) });
return null;
}
}
use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.
the class InternalTypeSystem method applyRuleTypeCallExpression.
protected Result<TypeRef> applyRuleTypeCallExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedCallExpression expr) throws RuleFailedException {
// output parameter
TypeRef T = null;
/* G |- expr.target : var TypeRef targetTypeRef */
Expression _target = expr.getTarget();
TypeRef targetTypeRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, _target);
checkAssignableTo(result.getFirst(), TypeRef.class);
targetTypeRef = (TypeRef) result.getFirst();
if ((targetTypeRef instanceof FunctionTypeExprOrRef)) {
final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) targetTypeRef);
final TFunction tFunction = F.getFunctionType();
/* { val inferring = env(G, GUARD_TYPE_CALL_EXPRESSION -> expr, TypeRef) G |- inferring ~> T } or { val G2 = G.wrap; G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) if(expr.eContainer instanceof AwaitExpression && expr.eContainmentFeature === N4JSPackage.eINSTANCE.getAwaitExpression_Expression() && tFunction!==null && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction)) { T = promisifyHelper.extractPromisifiedReturnType(expr); } else { T = F.returnTypeRef ?: G.anyTypeRef; } typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef); G2 |- T ~> T T = versionResolver.resolveVersion(T, F); if (T instanceof BoundThisTypeRef && !(expr.receiver instanceof ThisLiteral || expr.receiver instanceof SuperLiteral)) { G2 |~ T /\ T } } */
{
RuleFailedException previousFailure = null;
try {
Pair<String, ParameterizedCallExpression> _mappedTo = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
final TypeRef inferring = this.<TypeRef>env(G, _mappedTo, TypeRef.class);
/* G |- inferring ~> T */
Result<TypeArgument> result_1 = substTypeVariablesInternal(G, _trace_, inferring);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
T = (TypeRef) result_1.getFirst();
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
Pair<String, ParameterizedCallExpression> _mappedTo_1 = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
boolean _add = G2.add(_mappedTo_1, F.getReturnTypeRef());
/* G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) */
if (!_add) {
sneakyThrowRuleFailedException("G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef)");
}
if (((((expr.eContainer() instanceof AwaitExpression) && (expr.eContainmentFeature() == N4JSPackage.eINSTANCE.getAwaitExpression_Expression())) && (tFunction != null)) && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction))) {
T = this.promisifyHelper.extractPromisifiedReturnType(expr);
} else {
TypeRef _elvis = null;
TypeRef _returnTypeRef = F.getReturnTypeRef();
if (_returnTypeRef != null) {
_elvis = _returnTypeRef;
} else {
ParameterizedTypeRef _anyTypeRef = RuleEnvironmentExtensions.anyTypeRef(G);
_elvis = _anyTypeRef;
}
T = _elvis;
}
this.typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef);
/* G2 |- T ~> T */
Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, T);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
T = (TypeRef) result_2.getFirst();
T = this.versionResolver.<TypeRef, FunctionTypeExprOrRef>resolveVersion(T, F);
if (((T instanceof BoundThisTypeRef) && (!((expr.getReceiver() instanceof ThisLiteral) || (expr.getReceiver() instanceof SuperLiteral))))) {
/* G2 |~ T /\ T */
Result<TypeRef> result_3 = upperBoundInternal(G2, _trace_, T);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
T = (TypeRef) result_3.getFirst();
}
}
}
} else {
Type _declaredType = null;
if (targetTypeRef != null) {
_declaredType = targetTypeRef.getDeclaredType();
}
TObjectPrototype _functionType = RuleEnvironmentExtensions.functionType(G);
boolean _tripleEquals = (_declaredType == _functionType);
if (_tripleEquals) {
T = RuleEnvironmentExtensions.anyTypeRef(G);
} else {
boolean _isDynamic = targetTypeRef.isDynamic();
if (_isDynamic) {
T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
}
return new Result<TypeRef>(T);
}
use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.
the class InternalTypeSystem method applyRuleSubstTypeVariablesInComposedTypeRef.
protected Result<TypeArgument> applyRuleSubstTypeVariablesInComposedTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ComposedTypeRef typeRef) throws RuleFailedException {
// output parameter
ComposedTypeRef result = null;
boolean haveReplacement = false;
final ArrayList<TypeRef> substTypeRefs = CollectionLiterals.<TypeRef>newArrayList();
EList<TypeRef> _typeRefs = typeRef.getTypeRefs();
for (final TypeRef currTypeRef : _typeRefs) {
/* G |- currTypeRef ~> var TypeRef substTypeRef */
TypeRef substTypeRef = null;
Result<TypeArgument> result_1 = substTypeVariablesInternal(G, _trace_, currTypeRef);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
substTypeRef = (TypeRef) result_1.getFirst();
boolean _add = substTypeRefs.add(substTypeRef);
/* substTypeRefs.add(substTypeRef) */
if (!_add) {
sneakyThrowRuleFailedException("substTypeRefs.add(substTypeRef)");
}
haveReplacement = (haveReplacement || (substTypeRef != currTypeRef));
}
if (haveReplacement) {
result = TypeUtils.<ComposedTypeRef>copy(typeRef);
result.getTypeRefs().clear();
/* result.typeRefs.addAll(TypeUtils.copyAll(substTypeRefs)) */
if (!result.getTypeRefs().addAll(TypeUtils.<TypeRef>copyAll(substTypeRefs))) {
sneakyThrowRuleFailedException("result.typeRefs.addAll(TypeUtils.copyAll(substTypeRefs))");
}
} else {
result = typeRef;
}
return new Result<TypeArgument>(result);
}
Aggregations