Search in sources :

Example 6 with TypeRef

use of org.eclipse.n4js.ts.typeRefs.TypeRef 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;
}
Also used : ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) 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) Variance(org.eclipse.n4js.ts.types.util.Variance)

Example 7 with TypeRef

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

the class Reducer method reduceWildcard.

// TODO IDE-1653 reconsider handling of wildcards in Reducer#reduceWildcard()
private boolean reduceWildcard(Wildcard left, Wildcard right, @SuppressWarnings("unused") Variance variance) {
    if (left == right) {
        // trivial ==, <:, and :> of a wildcard to itself.
        return false;
    }
    boolean wasAdded = false;
    final TypeRef lbLeft = left.getDeclaredLowerBound();
    final TypeRef lbRight = right.getDeclaredLowerBound();
    if (lbLeft != null || lbRight != null) {
        // ⟨ ? super L Φ ? ⟩ implies `L = bottom`
        // ⟨ ? super L Φ ? extends R ⟩ implies `L = bottom`
        // ⟨ ? super L Φ ? super R ⟩ implies `L = R`
        final TypeRef lbLeftOrBottom = (lbLeft != null) ? lbLeft : bottom();
        final TypeRef lbRightOrBottom = (lbRight != null) ? lbRight : bottom();
        wasAdded |= reduce(lbLeftOrBottom, lbRightOrBottom, INV);
    }
    final TypeRef ubLeft = left.getDeclaredUpperBound();
    final TypeRef ubRight = right.getDeclaredUpperBound();
    if (ubLeft != null || ubRight != null) {
        // ⟨ ? extends L Φ ? ⟩ implies `L = top`
        // ⟨ ? extends L Φ ? super R ⟩ implies `L = top`
        // ⟨ ? extends L Φ ? extends R ⟩ implies `L = R`
        final TypeRef ubLeftOrTop = (ubLeft != null) ? ubLeft : top();
        final TypeRef ubRightOrTop = (ubRight != null) ? ubRight : top();
        wasAdded |= reduce(ubLeftOrTop, ubRightOrTop, INV);
    }
    return wasAdded;
}
Also used : 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)

Example 8 with TypeRef

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

the class FieldFactory method create.

@Override
public TField create(String name) {
    TField field = TypesFactory.eINSTANCE.createTField();
    field.setComposed(true);
    TypeRef typeRef = getReturnTypeRef();
    TypeUtils.setMemberTypeRef(field, typeRef);
    field.setName(name);
    field.setDeclaredFinal(isFinal());
    field.setDeclaredMemberAccessModifier(getAccessability());
    return field;
}
Also used : TField(org.eclipse.n4js.ts.types.TField) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef)

Example 9 with TypeRef

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

the class InternalTypeSystem method applyRuleTypeYieldExpression.

protected Result<TypeRef> applyRuleTypeYieldExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final YieldExpression y) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    TypeRef t = null;
    boolean _isMany = y.isMany();
    if (_isMany) {
        final Expression yieldValue = y.getExpression();
        /* G |- yieldValue : var TypeRef yieldValueTypeRef */
        TypeRef yieldValueTypeRef = null;
        Result<TypeRef> result = typeInternal(G, _trace_, yieldValue);
        checkAssignableTo(result.getFirst(), TypeRef.class);
        yieldValueTypeRef = (TypeRef) result.getFirst();
        final BuiltInTypeScope scope = RuleEnvironmentExtensions.getPredefinedTypes(G).builtInTypeScope;
        boolean _isGenerator = TypeUtils.isGenerator(yieldValueTypeRef, scope);
        if (_isGenerator) {
            t = this.typeSystemHelper.getGeneratorTReturn(G, yieldValueTypeRef);
        } else {
            final ParameterizedTypeRef itTypeRef = RuleEnvironmentExtensions.iterableTypeRef(G, TypeUtils.createWildcard());
            /* G |- yieldValueTypeRef <: itTypeRef */
            boolean _ruleinvocation = subtypeSucceeded(G, _trace_, yieldValueTypeRef, itTypeRef);
            final boolean isIterable = _ruleinvocation;
            if (isIterable) {
                t = scope.getAnyTypeRef();
            }
        }
    } else {
        final TypeRef actualGenTypeRef = this.typeSystemHelper.getActualGeneratorReturnType(G, y);
        if ((actualGenTypeRef != null)) {
            t = this.typeSystemHelper.getGeneratorTNext(G, actualGenTypeRef);
        }
    }
    if ((t == null)) {
        t = RuleEnvironmentExtensions.anyTypeRef(G);
    }
    T = t;
    return new Result<TypeRef>(T);
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BinaryBitwiseExpression(org.eclipse.n4js.n4JS.BinaryBitwiseExpression) AssignmentExpression(org.eclipse.n4js.n4JS.AssignmentExpression) IndexedAccessExpression(org.eclipse.n4js.n4JS.IndexedAccessExpression) UnionTypeExpression(org.eclipse.n4js.ts.typeRefs.UnionTypeExpression) FunctionExpression(org.eclipse.n4js.n4JS.FunctionExpression) PromisifyExpression(org.eclipse.n4js.n4JS.PromisifyExpression) UnaryExpression(org.eclipse.n4js.n4JS.UnaryExpression) ParenExpression(org.eclipse.n4js.n4JS.ParenExpression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) AdditiveExpression(org.eclipse.n4js.n4JS.AdditiveExpression) PostfixExpression(org.eclipse.n4js.n4JS.PostfixExpression) YieldExpression(org.eclipse.n4js.n4JS.YieldExpression) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) RelationalExpression(org.eclipse.n4js.n4JS.RelationalExpression) NewExpression(org.eclipse.n4js.n4JS.NewExpression) IntersectionTypeExpression(org.eclipse.n4js.ts.typeRefs.IntersectionTypeExpression) AwaitExpression(org.eclipse.n4js.n4JS.AwaitExpression) CommaExpression(org.eclipse.n4js.n4JS.CommaExpression) Expression(org.eclipse.n4js.n4JS.Expression) CastExpression(org.eclipse.n4js.n4JS.CastExpression) BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) EqualityExpression(org.eclipse.n4js.n4JS.EqualityExpression) ShiftExpression(org.eclipse.n4js.n4JS.ShiftExpression) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) MultiplicativeExpression(org.eclipse.n4js.n4JS.MultiplicativeExpression) N4ClassExpression(org.eclipse.n4js.n4JS.N4ClassExpression) 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) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Example 10 with TypeRef

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

the class InternalTypeSystem method typeImpl.

protected Result<TypeRef> typeImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ShiftExpression e) throws RuleFailedException {
    try {
        final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
        final Result<TypeRef> _result_ = applyRuleTypeShiftExpression(G, _subtrace_, e);
        addToTrace(_trace_, new Provider<Object>() {

            public Object get() {
                return ruleName("typeShiftExpression") + stringRepForEnv(G) + " |- " + stringRep(e) + " : " + stringRep(_result_.getFirst());
            }
        });
        addAsSubtrace(_trace_, _subtrace_);
        return _result_;
    } catch (Exception e_applyRuleTypeShiftExpression) {
        typeThrowException(ruleName("typeShiftExpression") + stringRepForEnv(G) + " |- " + stringRep(e) + " : " + "ParameterizedTypeRef", TYPESHIFTEXPRESSION, e_applyRuleTypeShiftExpression, e, new ErrorInformation[] { new ErrorInformation(e) });
        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)

Aggregations

TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)292 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)223 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)206 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)206 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)205 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)202 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)201 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)200 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)200 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)200 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)199 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)193 EObject (org.eclipse.emf.ecore.EObject)117 RuleFailedException (org.eclipse.xsemantics.runtime.RuleFailedException)117 ErrorInformation (org.eclipse.xsemantics.runtime.ErrorInformation)103 RuleApplicationTrace (org.eclipse.xsemantics.runtime.RuleApplicationTrace)101 Result (org.eclipse.xsemantics.runtime.Result)90 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)89 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)42 ParameterizedCallExpression (org.eclipse.n4js.n4JS.ParameterizedCallExpression)26