Search in sources :

Example 1 with BoundThisTypeRef

use of org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef in project n4js by eclipse.

the class InternalTypeSystem method upperBoundImpl.

protected Result<TypeRef> upperBoundImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final BoundThisTypeRef boundThisTypeRef) throws RuleFailedException {
    try {
        final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
        final Result<TypeRef> _result_ = applyRuleUpperBoundThisTypeRef(G, _subtrace_, boundThisTypeRef);
        addToTrace(_trace_, new Provider<Object>() {

            public Object get() {
                return ruleName("upperBoundThisTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(boundThisTypeRef) + " /\\ " + stringRep(_result_.getFirst());
            }
        });
        addAsSubtrace(_trace_, _subtrace_);
        return _result_;
    } catch (Exception e_applyRuleUpperBoundThisTypeRef) {
        upperBoundThrowException(ruleName("upperBoundThisTypeRef") + stringRepForEnv(G) + " |~ " + stringRep(boundThisTypeRef) + " /\\ " + "TypeRef", UPPERBOUNDTHISTYPEREF, e_applyRuleUpperBoundThisTypeRef, boundThisTypeRef, new ErrorInformation[] { new ErrorInformation(boundThisTypeRef) });
        return null;
    }
}
Also used : ErrorInformation(org.eclipse.xsemantics.runtime.ErrorInformation) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) RuleApplicationTrace(org.eclipse.xsemantics.runtime.RuleApplicationTrace) EObject(org.eclipse.emf.ecore.EObject) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException)

Example 2 with BoundThisTypeRef

use of org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef in project n4js by eclipse.

the class InternalTypeSystem method applyRuleSubtypeBoundThisTypeRefTypeRef.

protected Result<Boolean> applyRuleSubtypeBoundThisTypeRefTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final BoundThisTypeRef boundThisTypeRef, final TypeRef right) throws RuleFailedException {
    /* boundThisTypeRef === right or { G |~ boundThisTypeRef /\ var TypeRef upperExt G |- upperExt <: right } */
    {
        RuleFailedException previousFailure = null;
        try {
            /* boundThisTypeRef === right */
            if (!(boundThisTypeRef == right)) {
                sneakyThrowRuleFailedException("boundThisTypeRef === right");
            }
        } catch (Exception e) {
            previousFailure = extractRuleFailedException(e);
            /* G |~ boundThisTypeRef /\ var TypeRef upperExt */
            TypeRef upperExt = null;
            Result<TypeRef> result = upperBoundInternal(G, _trace_, boundThisTypeRef);
            checkAssignableTo(result.getFirst(), TypeRef.class);
            upperExt = (TypeRef) result.getFirst();
            /* G |- upperExt <: right */
            subtypeInternal(G, _trace_, upperExt, right);
        }
    }
    return new Result<Boolean>(true);
}
Also used : ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Example 3 with BoundThisTypeRef

use of org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef in project n4js by eclipse.

the class TypeCompareLogic method compare.

/**
 * WARNING: fqnProvider may be <code>null</code>, but then the lower/greater info will be unreliable!
 */
/* package */
static int compare(IQualifiedNameProvider fqnProvider, TypeArgument arg1, TypeArgument arg2) {
    if (arg1 == arg2) {
        return 0;
    }
    if (arg1 == null) {
        return -1;
    }
    if (arg2 == null) {
        return 1;
    }
    if (arg1 instanceof Wildcard || arg2 instanceof Wildcard) {
        if (arg1 instanceof Wildcard && arg2 instanceof Wildcard) {
            final Wildcard w1 = (Wildcard) arg1;
            final Wildcard w2 = (Wildcard) arg2;
            int c;
            // lower bounds
            c = compare(fqnProvider, w1.getDeclaredLowerBound(), w2.getDeclaredLowerBound());
            if (c != 0) {
                return c;
            }
            // upper bounds
            c = compare(fqnProvider, w1.getDeclaredUpperBound(), w2.getDeclaredUpperBound());
            if (c != 0) {
                return c;
            }
            return 0;
        }
        return compareEClasses(arg1.eClass(), arg2.eClass());
    }
    // now, we know we have two TypeRefs
    final TypeRef ref1 = (TypeRef) arg1;
    final TypeRef ref2 = (TypeRef) arg2;
    // @formatter:on
    if (ref1 instanceof ExistentialTypeRef && ref2 instanceof ExistentialTypeRef) {
        return compare(fqnProvider, ((ExistentialTypeRef) ref1).getWildcard(), ((ExistentialTypeRef) ref2).getWildcard());
    }
    // its subclasses
    if (ref1 instanceof FunctionTypeExprOrRef && ref2 instanceof FunctionTypeExprOrRef) {
        final FunctionTypeExprOrRef f1 = (FunctionTypeExprOrRef) ref1;
        final FunctionTypeExprOrRef f2 = (FunctionTypeExprOrRef) ref2;
        return compareFunctionTypeExprOrRefs(fqnProvider, f1, f2);
    }
    int c;
    c = compareEClasses(ref1.eClass(), ref2.eClass());
    if (c != 0) {
        return c;
    }
    // note: ref1 and ref2 are of the same type, otherwise c would not be 0
    // declared type
    c = compare(fqnProvider, ref1.getDeclaredType(), ref2.getDeclaredType());
    if (c != 0) {
        return c;
    }
    // if we got a subclass of StructuralTypeRef -> check properties of StructuralTypeRef beforehand
    if (ref1 instanceof StructuralTypeRef) {
        final StructuralTypeRef sref1 = (StructuralTypeRef) ref1;
        final StructuralTypeRef sref2 = (StructuralTypeRef) ref2;
        // note: for simplicity, we here require sref1/sref2.structuralMembers to have the same order
        // (we aren't doing a semantic compare anyway)
        c = compareComparables(sref1.getTypingStrategy(), sref2.getTypingStrategy());
        if (c != 0) {
            return c;
        }
        c = compare(fqnProvider, sref1.getStructuralType(), sref2.getStructuralType());
        if (c != 0) {
            return c;
        }
        final Iterator<TStructMember> iter1 = sref1.getStructuralMembers().iterator();
        final Iterator<TStructMember> iter2 = sref2.getStructuralMembers().iterator();
        while (iter1.hasNext() && iter2.hasNext()) {
            c = compareMembers(fqnProvider, iter1.next(), iter2.next());
            if (c != 0) {
                return c;
            }
        }
        if (iter1.hasNext()) {
            return 1;
        }
        if (iter2.hasNext()) {
            return -1;
        }
    }
    if (ref1 instanceof ParameterizedTypeRef) {
        final ParameterizedTypeRef pref1 = (ParameterizedTypeRef) ref1;
        final ParameterizedTypeRef pref2 = (ParameterizedTypeRef) ref2;
        c = compareTypeArguments(fqnProvider, pref1.getTypeArgs(), pref2.getTypeArgs());
        if (c != 0) {
            return c;
        }
    } else if (ref1 instanceof ComposedTypeRef) {
        final ComposedTypeRef cref1 = (ComposedTypeRef) ref1;
        final ComposedTypeRef cref2 = (ComposedTypeRef) ref2;
        c = compareTypeArguments(fqnProvider, cref1.getTypeRefs(), cref2.getTypeRefs());
        if (c != 0) {
            return c;
        }
    } else if (ref1 instanceof TypeTypeRef) {
        final TypeTypeRef cref1 = (TypeTypeRef) ref1;
        final TypeTypeRef cref2 = (TypeTypeRef) ref2;
        c = compareComparables(cref1.isConstructorRef(), cref2.isConstructorRef());
        if (c != 0) {
            return c;
        }
        c = compare(fqnProvider, cref1.getTypeArg(), cref2.getTypeArg());
        if (c != 0) {
            return c;
        }
    } else if (ref1 instanceof BoundThisTypeRef) {
        final BoundThisTypeRef bref1 = (BoundThisTypeRef) ref1;
        final BoundThisTypeRef bref2 = (BoundThisTypeRef) ref2;
        c = compare(fqnProvider, bref1.getActualThisTypeRef(), bref2.getActualThisTypeRef());
        if (c != 0) {
            return c;
        }
    }
    // dynamic
    c = Boolean.compare(ref1.isDynamic(), ref2.isDynamic());
    if (c != 0) {
        return c;
    }
    return 0;
}
Also used : ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TStructMember(org.eclipse.n4js.ts.types.TStructMember) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) FunctionTypeExprOrRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)

Example 4 with BoundThisTypeRef

use of org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef in project n4js by eclipse.

the class TypeUtils method createBoundThisTypeRefStructural.

/**
 * Creates a new this type reference bound to the given actual type.
 *
 * @param actualThisTypeRef
 *            must not be null
 */
public static BoundThisTypeRef createBoundThisTypeRefStructural(ParameterizedTypeRef actualThisTypeRef, ThisTypeRefStructural thisTypeStructural) {
    if (actualThisTypeRef == null) {
        throw new NullPointerException("Actual this type must not be null!");
    }
    BoundThisTypeRef boundThisTypeRef = TypeRefsFactory.eINSTANCE.createBoundThisTypeRef();
    boundThisTypeRef.setActualThisTypeRef(TypeUtils.copyIfContained(actualThisTypeRef));
    // set structural typing info
    copyStructuralTypingInfo(boundThisTypeRef, thisTypeStructural);
    return boundThisTypeRef;
}
Also used : BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)

Example 5 with BoundThisTypeRef

use of org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef in project n4js by eclipse.

the class TypeUtils method createBoundThisTypeRef.

/**
 * Creates a new this type reference bound to the given actual type. This method also sets the typing strategy and
 * structural members. The returned type is normalized in terms of only the actual type is never structurally typed.
 * E.g., {@code ~this[~C]} is normalized to {@code ~this[C]}.
 *
 * cf. N4JS Spec, Constraints 63 (Normalized This Type)
 *
 * @param actualThisTypeRef
 *            must not be null
 */
public static BoundThisTypeRef createBoundThisTypeRef(ParameterizedTypeRef actualThisTypeRef) {
    if (actualThisTypeRef == null) {
        throw new NullPointerException("Actual this type must not be null!");
    }
    BoundThisTypeRef boundThisTypeRef = TypeRefsFactory.eINSTANCE.createBoundThisTypeRef();
    ParameterizedTypeRef clonedActualThisType = TypeUtils.copy(actualThisTypeRef);
    boundThisTypeRef.setActualThisTypeRef(clonedActualThisType);
    // IDEBUG-161: Constraints 66 (Type Inference
    boundThisTypeRef.setDefinedTypingStrategy(TypingStrategy.NOMINAL);
    // Heuristic for This-Keyword)
    if (actualThisTypeRef instanceof ParameterizedTypeRefStructural) {
        // set structural typing info
        copyStructuralTypingInfo(boundThisTypeRef, (ParameterizedTypeRefStructural) actualThisTypeRef);
        // and set clonedActualThisType to nominal type
        ((ParameterizedTypeRefStructural) clonedActualThisType).getAstStructuralMembers().clear();
        ((ParameterizedTypeRefStructural) clonedActualThisType).getGenStructuralMembers().clear();
        ((ParameterizedTypeRefStructural) clonedActualThisType).setStructuralType(null);
        ((ParameterizedTypeRefStructural) clonedActualThisType).setDefinedTypingStrategy(TypingStrategy.NOMINAL);
    }
    return boundThisTypeRef;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) ParameterizedTypeRefStructural(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRefStructural) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)

Aggregations

BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)16 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)15 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)14 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)13 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)13 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)13 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)13 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)12 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)12 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)12 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)12 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)12 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)9 Result (org.eclipse.xsemantics.runtime.Result)9 RuleFailedException (org.eclipse.xsemantics.runtime.RuleFailedException)9 TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)5 EObject (org.eclipse.emf.ecore.EObject)4 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)3 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)3 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)3