Search in sources :

Example 6 with InferenceVariable

use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.

the class BoundSet method combineInvInvWithProperType.

/**
 * Given two type bounds `α = U` and `β = T` with α ≠ β, will return a new constraint `β = T[α:=U]` if
 * <ul>
 * <li>U is proper, and
 * <li>T mentions α.
 * </ul>
 * Otherwise, <code>null</code> is returned.
 */
private TypeConstraint combineInvInvWithProperType(TypeBound boundWithProperRHS, TypeBound boundOther) {
    final InferenceVariable alpha = boundWithProperRHS.left;
    final TypeRef U = boundWithProperRHS.right;
    final TypeRef T = boundOther.right;
    if (TypeUtils.isProper(U) && TypeUtils.getReferencedTypeVars(T).contains(alpha)) {
        final InferenceVariable beta = boundOther.left;
        // returns T[α:=U]
        final TypeRef T_subst = substituteInferenceVariable(T, alpha, U);
        // performance tweak: avoid unnecessary growth of bounds
        removeBound(boundOther);
        return new TypeConstraint(typeRef(beta), T_subst, INV);
    }
    return null;
}
Also used : InferenceVariable(org.eclipse.n4js.ts.types.InferenceVariable) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef)

Example 7 with InferenceVariable

use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.

the class InferenceContext method newInferenceVariablesFor.

/**
 * Introduces newly generated inference variables for each type parameter of the given function type (if generic)
 * and returns a non-generic function type in which all type variables owned by the given function type are replaced
 * by those inference variables; simply returns the given function type unchanged if it already is non-generic.
 * Returns given function type unchanged in case of error (so returned function type may actually be generic, but
 * only in error cases).
 * <p>
 * Example: given a function type such as
 *
 * <pre>
 * {function&lt;T,S>(G&lt;T,string>):S}
 * </pre>
 *
 * this method will add two new type variables α', β' and return
 *
 * <pre>
 * {function(G&lt;α',string>):β'}
 * </pre>
 *
 * Note that the returned function type is non-generic.
 */
public FunctionTypeExprOrRef newInferenceVariablesFor(FunctionTypeExprOrRef funTypeRef) {
    if (!funTypeRef.isGeneric())
        return funTypeRef;
    // NOTE: typeParam may contain null entries!
    final List<TypeVariable> typeParams = funTypeRef.getTypeVars();
    final InferenceVariable[] newInfVars = newInferenceVariables(typeParams.size(), true);
    final List<? extends TypeRef> newInfVarsRefs = Stream.of(newInfVars).map(TypeUtils::createTypeRef).collect(Collectors.toList());
    // new, empty RE
    final RuleEnvironment G_params2infVars = RuleEnvironmentExtensions.newRuleEnvironment(G);
    RuleEnvironmentExtensions.addTypeMappings(G_params2infVars, typeParams, newInfVarsRefs);
    final TypeArgument left_withInfVars = ts.substTypeVariables(G_params2infVars, funTypeRef).getValue();
    if (left_withInfVars instanceof FunctionTypeExprOrRef)
        return (FunctionTypeExprOrRef) left_withInfVars;
    // in case of substitution error: return original funTypeRef
    return funTypeRef;
}
Also used : InferenceVariable(org.eclipse.n4js.ts.types.InferenceVariable) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) FunctionTypeExprOrRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)

Example 8 with InferenceVariable

use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.

the class InferenceContext method newInferenceVariable.

private InferenceVariable newInferenceVariable(boolean internal) {
    final InferenceVariable iv = TypesFactory.eINSTANCE.createInferenceVariable();
    final String name = internal ? "_" + unusedNameGeneratorInternal.next() : unusedNameGenerator.next();
    iv.setName(name);
    addInferenceVariables(internal, iv);
    return iv;
}
Also used : InferenceVariable(org.eclipse.n4js.ts.types.InferenceVariable)

Example 9 with InferenceVariable

use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.

the class Reducer method mightBeSubtypeOf.

private boolean mightBeSubtypeOf(FunctionTypeExprOrRef left, FunctionTypeExprOrRef right) {
    // step 1: replace all inference variables by UnknownTypeRef
    final TypeRef unknown = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
    final RuleEnvironment G_temp = RuleEnvironmentExtensions.newRuleEnvironment(G);
    for (InferenceVariable iv : ic.getInferenceVariables()) {
        RuleEnvironmentExtensions.addTypeMapping(G_temp, iv, unknown);
    }
    final TypeArgument leftSubst = ts.substTypeVariables(G_temp, left).getValue();
    final TypeArgument rightSubst = ts.substTypeVariables(G_temp, right).getValue();
    // step 2: now, perform subtype check reusing existing logic
    return ts.subtypeSucceeded(G, leftSubst, rightSubst);
}
Also used : InferenceVariable(org.eclipse.n4js.ts.types.InferenceVariable) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Aggregations

InferenceVariable (org.eclipse.n4js.ts.types.InferenceVariable)9 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)7 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)7 TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)3 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)3 FunctionTypeExprOrRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)2 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)2 TypeVariable (org.eclipse.n4js.ts.types.TypeVariable)2 Variance (org.eclipse.n4js.ts.types.util.Variance)2 Optional (com.google.common.base.Optional)1 ContiguousSet (com.google.common.collect.ContiguousSet)1 Range (com.google.common.collect.Range)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1