Search in sources :

Example 21 with TFormalParameter

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

the class Reducer method reduceFunctionTypeExprOrRef.

/**
 * IMPORTANT: the implementation of this method has to be kept consistent with
 * {@link SubtypeComputer#isSubtypeFunction(RuleEnvironment, FunctionTypeExprOrRef, FunctionTypeExprOrRef)} and esp.
 * <code>#primIsSubtypeFunction()</code>.
 */
private boolean reduceFunctionTypeExprOrRef(FunctionTypeExprOrRef left, FunctionTypeExprOrRef right, Variance variance) {
    if (left.isGeneric() || right.isGeneric()) {
        final FunctionTypeExprOrRef leftNonGen = ic.newInferenceVariablesFor(left);
        final FunctionTypeExprOrRef rightNonGen = ic.newInferenceVariablesFor(right);
        return reduceFunctionTypeExprOrRef(leftNonGen, rightNonGen, variance);
    }
    boolean wasAdded = false;
    // derive constraints for types of fpars
    final Iterator<TFormalParameter> valueParsIt = right.getFpars().iterator();
    for (TFormalParameter keyPar : left.getFpars()) {
        if (valueParsIt.hasNext()) {
            wasAdded |= reduce(keyPar.getTypeRef(), valueParsIt.next().getTypeRef(), variance.mult(CONTRA));
        }
    }
    // derive constraints for return types
    final boolean isVoidLeft = TypeUtils.isVoidReturnType(left);
    final boolean isVoidRight = TypeUtils.isVoidReturnType(right);
    if (isVoidLeft && isVoidRight) {
        // void on both sides:
        wasAdded |= addBound(true);
    } else if ((variance == CO && isVoidRight) || (variance == CONTRA && isVoidLeft)) {
        // we have a constraint like:
        // ⟨ {function():α} <: {function():void} ⟩
        // --> α is not constrained in any way --> just add bound TRUE
        wasAdded |= addBound(true);
    } else if (isVoidLeft || isVoidRight) {
        // we have a constraint like:
        // ⟨ {function():void} <: {function():α} ⟩ or ⟨ {function():void} = {function():α} ⟩
        // --> we're doomed, unless the non-void return value is optional
        final boolean isRetValOpt = isVoidLeft ? right.isReturnValueOptional() : left.isReturnValueOptional();
        wasAdded |= addBound(isRetValOpt);
    } else {
        wasAdded |= reduce(left.getReturnTypeRef(), right.getReturnTypeRef(), variance.mult(CO));
    }
    // derive constraints for declared this types
    final TypeRef leftThis = left.getDeclaredThisType();
    final TypeRef rightThis = right.getDeclaredThisType();
    if (leftThis != null || rightThis != null) {
        if (leftThis == null && rightThis != null) {
            if (variance == CO) {
                wasAdded |= addBound(true);
            } else {
                wasAdded |= giveUp(left, right, variance);
            }
        } else if (leftThis != null && rightThis == null) {
            if (variance == CONTRA) {
                wasAdded |= addBound(true);
            } else {
                wasAdded |= giveUp(left, right, variance);
            }
        } else if (leftThis != null && rightThis != null) {
            wasAdded |= reduce(leftThis, rightThis, variance.mult(CONTRA));
        }
    }
    return wasAdded;
}
Also used : TFormalParameter(org.eclipse.n4js.ts.types.TFormalParameter) 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) FunctionTypeExprOrRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)

Example 22 with TFormalParameter

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

the class MethodFactory method create.

@Override
public TMethod create(String name) {
    TMethod method = TypesFactory.eINSTANCE.createTMethod();
    method.setComposed(true);
    method.setDeclaredMemberAccessModifier(getAccessability());
    method.setName(name);
    TypeUtils.setMemberTypeRef(method, getReturnTypeRefComposition());
    if (!fpas.isEmpty()) {
        boolean variFparNecessary = cma.isVariadicButLastFParIsDifferent();
        if (variFparNecessary) {
            List<ComposedFParInfo> fpAggrs = cma.getFParAggregates();
            ComposedFParInfo lastFPAggr = fpAggrs.get(cma.getFParAggregates().size() - 1);
            List<TypeRef> variadicTypeRefs = lastFPAggr.getTypeRefsVariadicAccumulated();
            MethodFParFactory varpar = new NewLastVariadicFPar(variadicTypeRefs);
            fpas.add(varpar);
        }
    }
    for (MethodFParFactory currFparDesc : fpas) {
        TFormalParameter tFPar = currFparDesc.create();
        method.getFpars().add(tFPar);
    }
    return method;
}
Also used : TFormalParameter(org.eclipse.n4js.ts.types.TFormalParameter) TMethod(org.eclipse.n4js.ts.types.TMethod) ComposedFParInfo(org.eclipse.n4js.scoping.members.ComposedMemberInfo.ComposedFParInfo) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef)

Aggregations

TFormalParameter (org.eclipse.n4js.ts.types.TFormalParameter)22 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)12 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)9 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)7 TypeVariable (org.eclipse.n4js.ts.types.TypeVariable)7 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)6 TMethod (org.eclipse.n4js.ts.types.TMethod)5 FunctionTypeExpression (org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression)4 IntersectionTypeExpression (org.eclipse.n4js.ts.typeRefs.IntersectionTypeExpression)4 ThisTypeRefStructural (org.eclipse.n4js.ts.typeRefs.ThisTypeRefStructural)4 UnionTypeExpression (org.eclipse.n4js.ts.typeRefs.UnionTypeExpression)4 EPackage (org.eclipse.emf.ecore.EPackage)3 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)3 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)3 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)3 BinaryBitwiseExpression (org.eclipse.n4js.n4JS.BinaryBitwiseExpression)3 BinaryLogicalExpression (org.eclipse.n4js.n4JS.BinaryLogicalExpression)3 CastExpression (org.eclipse.n4js.n4JS.CastExpression)3 CommaExpression (org.eclipse.n4js.n4JS.CommaExpression)3 ConditionalExpression (org.eclipse.n4js.n4JS.ConditionalExpression)3