Search in sources :

Example 1 with TypeTypeRef

use of org.eclipse.n4js.ts.typeRefs.TypeTypeRef 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);
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) TMethod(org.eclipse.n4js.ts.types.TMethod) 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) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) Type(org.eclipse.n4js.ts.types.Type) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) VoidType(org.eclipse.n4js.ts.types.VoidType) AnyType(org.eclipse.n4js.ts.types.AnyType) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment)

Example 2 with TypeTypeRef

use of org.eclipse.n4js.ts.typeRefs.TypeTypeRef 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 3 with TypeTypeRef

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

the class TypeUtils method createTypeTypeRef.

/**
 * Creates new {@link TypeTypeRef} for the given type.
 */
public static TypeTypeRef createTypeTypeRef(Type type, boolean isConstructorRef) {
    final TypeTypeRef typeTypeRef = TypeRefsFactory.eINSTANCE.createTypeTypeRef();
    typeTypeRef.setTypeArg(createTypeRef(type));
    typeTypeRef.setConstructorRef(isConstructorRef);
    return typeTypeRef;
}
Also used : TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef)

Example 4 with TypeTypeRef

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

the class TypeUtils method createClassifierBoundThisTypeRef.

/**
 * from type{S} to type{this[S]}, as Part of IDE-785
 *
 * @param actualThisTypeRef
 *            must not be null, must not contain a this unbound-this-type-ref.
 */
public static TypeTypeRef createClassifierBoundThisTypeRef(TypeTypeRef actualThisTypeRef) {
    if (actualThisTypeRef == null) {
        throw new NullPointerException("Actual this type must not be null!");
    }
    TypeArgument typeArg = actualThisTypeRef.getTypeArg();
    final BoundThisTypeRef boundThisTypeRef;
    if (typeArg instanceof ParameterizedTypeRef) {
        boundThisTypeRef = createBoundThisTypeRef((ParameterizedTypeRef) typeArg);
    } else if (typeArg instanceof BoundThisTypeRef) {
        boundThisTypeRef = (BoundThisTypeRef) typeArg;
    } else {
        // invalid use
        throw new IllegalArgumentException("Cannot turn unbound type{this} into type{this[X]}, must be called with type{X}!");
    }
    TypeTypeRef classifierBoundThisTypeRef = createTypeTypeRef(boundThisTypeRef, false);
    // TODO is there anything else to copy ?
    return classifierBoundThisTypeRef;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 5 with TypeTypeRef

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

the class TypeUtils method createConstructorTypeRef.

/**
 * Creates new type reference for constructor. if the declared type is TFunction a FunctionTypeRef is created. If
 * declared type is TClassifier than TypeTypeRef is created (i.e. <code>constructor{A}</code> in N4JS code)
 */
public static TypeRef createConstructorTypeRef(Type declaredType, TypeArgument... typeArgs) {
    TypeRef typeRef = null;
    if (declaredType instanceof TFunction) {
        // TODO is this correct?
        FunctionTypeRef ref = TypeRefsFactory.eINSTANCE.createFunctionTypeRef();
        ref.setDeclaredType(declaredType);
        for (TypeArgument typeArg : typeArgs) {
            ref.getTypeArgs().add(TypeUtils.copyIfContained(typeArg));
        }
        typeRef = ref;
    } else if (declaredType instanceof TClassifier) {
        TClassifier tClassifier = (TClassifier) declaredType;
        typeRef = createTypeTypeRef(createTypeRef(tClassifier, typeArgs), true);
    } else if (declaredType instanceof TypeVariable) {
        TypeVariable tTypeVar = (TypeVariable) declaredType;
        typeRef = createTypeTypeRef(createTypeRef(tTypeVar), true);
    }
    return typeRef;
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) TFunction(org.eclipse.n4js.ts.types.TFunction) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) DeferredTypeRef(org.eclipse.n4js.ts.typeRefs.DeferredTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Aggregations

TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)18 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)14 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)11 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)10 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)10 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)10 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)10 TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)10 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)10 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)9 FunctionTypeExpression (org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression)9 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)9 IntersectionTypeExpression (org.eclipse.n4js.ts.typeRefs.IntersectionTypeExpression)8 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)8 UnionTypeExpression (org.eclipse.n4js.ts.typeRefs.UnionTypeExpression)8 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)8 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)8 Result (org.eclipse.xsemantics.runtime.Result)8 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)6 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)6