Search in sources :

Example 6 with TypeVariable

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

the class FunctionTypeExprOrRefImpl method getTypeRefAsString.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public String getTypeRefAsString() {
    String _xifexpression = null;
    TypeRef _declaredThisType = this.getDeclaredThisType();
    boolean _tripleNotEquals = (_declaredThisType != null);
    if (_tripleNotEquals) {
        String _typeRefAsString = this.getDeclaredThisType().getTypeRefAsString();
        String _plus = ("@This(" + _typeRefAsString);
        _xifexpression = (_plus + ") ");
    } else {
        _xifexpression = "";
    }
    String _plus_1 = ("{" + _xifexpression);
    String _plus_2 = (_plus_1 + "function");
    String _xifexpression_1 = null;
    boolean _isGeneric = this.isGeneric();
    if (_isGeneric) {
        final Function1<TypeVariable, String> _function = new Function1<TypeVariable, String>() {

            public String apply(final TypeVariable it) {
                return it.getTypeVariableAsString(FunctionTypeExprOrRefImpl.this.getTypeVarUpperBound(it));
            }
        };
        String _join = IterableExtensions.join(XcoreEListExtensions.<TypeVariable, String>map(this.getTypeVars(), _function), ",");
        String _plus_3 = ("<" + _join);
        _xifexpression_1 = (_plus_3 + ">");
    } else {
        _xifexpression_1 = "";
    }
    String _plus_4 = (_plus_2 + _xifexpression_1);
    String _plus_5 = (_plus_4 + "(");
    final Function1<TFormalParameter, String> _function_1 = new Function1<TFormalParameter, String>() {

        public String apply(final TFormalParameter it) {
            return it.getFormalParameterAsTypesString();
        }
    };
    String _join_1 = IterableExtensions.join(XcoreEListExtensions.<TFormalParameter, String>map(this.getFpars(), _function_1), ",");
    String _plus_6 = (_plus_5 + _join_1);
    String _plus_7 = (_plus_6 + ")");
    String _xifexpression_2 = null;
    TypeRef _returnTypeRef = this.getReturnTypeRef();
    boolean _tripleNotEquals_1 = (_returnTypeRef != null);
    if (_tripleNotEquals_1) {
        String _typeRefAsString_1 = this.getReturnTypeRef().getTypeRefAsString();
        _xifexpression_2 = (":" + _typeRefAsString_1);
    } else {
        _xifexpression_2 = "";
    }
    String _plus_8 = (_plus_7 + _xifexpression_2);
    String _xifexpression_3 = null;
    boolean _isReturnValueOptional = this.isReturnValueOptional();
    if (_isReturnValueOptional) {
        _xifexpression_3 = "?";
    } else {
        _xifexpression_3 = "";
    }
    String _plus_9 = (_plus_8 + _xifexpression_3);
    String _plus_10 = (_plus_9 + "}");
    String _modifiersAsString = this.getModifiersAsString();
    return (_plus_10 + _modifiersAsString);
}
Also used : TFormalParameter(org.eclipse.n4js.ts.types.TFormalParameter) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1)

Example 7 with TypeVariable

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

the class FunctionTypeRefImpl method getTypeRefAsString.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public String getTypeRefAsString() {
    String _xifexpression = null;
    TypeRef _declaredThisType = this.getDeclaredThisType();
    boolean _tripleNotEquals = (_declaredThisType != null);
    if (_tripleNotEquals) {
        String _typeRefAsString = this.getDeclaredThisType().getTypeRefAsString();
        String _plus = ("@This(" + _typeRefAsString);
        _xifexpression = (_plus + ") ");
    } else {
        _xifexpression = "";
    }
    String _plus_1 = ("{" + _xifexpression);
    String _plus_2 = (_plus_1 + "function");
    String _xifexpression_1 = null;
    boolean _isGeneric = this.isGeneric();
    if (_isGeneric) {
        final Function1<TypeVariable, String> _function = new Function1<TypeVariable, String>() {

            public String apply(final TypeVariable it) {
                return it.getTypeVariableAsString(FunctionTypeRefImpl.this.getTypeVarUpperBound(it));
            }
        };
        String _join = IterableExtensions.join(XcoreEListExtensions.<TypeVariable, String>map(this.getTypeVars(), _function), ",");
        String _plus_3 = ("<" + _join);
        _xifexpression_1 = (_plus_3 + ">");
    } else {
        _xifexpression_1 = "";
    }
    String _plus_4 = (_plus_2 + _xifexpression_1);
    String _plus_5 = (_plus_4 + "(");
    final Function1<TFormalParameter, String> _function_1 = new Function1<TFormalParameter, String>() {

        public String apply(final TFormalParameter it) {
            return it.getFormalParameterAsTypesString();
        }
    };
    String _join_1 = IterableExtensions.join(XcoreEListExtensions.<TFormalParameter, String>map(this.getFpars(), _function_1), ",");
    String _plus_6 = (_plus_5 + _join_1);
    String _plus_7 = (_plus_6 + ")");
    String _xifexpression_2 = null;
    TypeRef _returnTypeRef = this.getReturnTypeRef();
    boolean _tripleNotEquals_1 = (_returnTypeRef != null);
    if (_tripleNotEquals_1) {
        String _typeRefAsString_1 = this.getReturnTypeRef().getTypeRefAsString();
        _xifexpression_2 = (":" + _typeRefAsString_1);
    } else {
        _xifexpression_2 = "";
    }
    String _plus_8 = (_plus_7 + _xifexpression_2);
    String _xifexpression_3 = null;
    boolean _isReturnValueOptional = this.isReturnValueOptional();
    if (_isReturnValueOptional) {
        _xifexpression_3 = "?";
    } else {
        _xifexpression_3 = "";
    }
    String _plus_9 = (_plus_8 + _xifexpression_3);
    String _plus_10 = (_plus_9 + "}");
    String _modifiersAsString = this.getModifiersAsString();
    return (_plus_10 + _modifiersAsString);
}
Also used : TFormalParameter(org.eclipse.n4js.ts.types.TFormalParameter) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1)

Example 8 with TypeVariable

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

the class ProjectCompareHelper method internalCompareApiImpl.

private ProjectCompareResult internalCompareApiImpl(ProjectComparisonEntry entry, int implIdx) {
    if (!entry.isElementEntry())
        // not an entry for an EObject element -> never report differences
        return ProjectCompareResult.equal();
    final int implCount = entry.getImplCount();
    if (implIdx < 0 || implIdx >= implCount)
        // implementation index out of range -> never report differences
        return ProjectCompareResult.equal();
    // compare implementation to API
    final EObject api = entry.getElementAPI();
    final EObject impl = entry.getElementImpl()[implIdx];
    // special case: no API
    if (api == null) {
        if (impl != null)
            return ProjectCompareResult.compliant();
        else
            return ProjectCompareResult.equal();
    }
    // special case: no impl
    if (impl == null) {
        // note: we know api!=null because of above check
        return ProjectCompareResult.error("missing implementation");
    }
    // accessibility-based compare:
    if (api instanceof TMember && impl instanceof TMember) {
        // order important: check for member before type!
        if (AccessModifiers.less((TMember) impl, (TMember) api))
            return ProjectCompareResult.error("reduced visibility");
    } else if (api instanceof Type && impl instanceof Type) {
        final MemberAccessModifier apiAcc = AccessModifiers.toMemberModifier((Type) api);
        final MemberAccessModifier implAcc = AccessModifiers.toMemberModifier((Type) impl);
        if (AccessModifiers.less(implAcc, apiAcc))
            return ProjectCompareResult.error("reduced visibility");
    }
    ImplToApiReplacementProvider typeReplacementProvider = new ImplToApiReplacementProvider(entry.getRoot());
    // subtype-based compare:
    if (api instanceof TMember && impl instanceof TMember) {
        final TMember apiMember = (TMember) api;
        final TMember implMember = (TMember) impl;
        if (apiMember instanceof TField) {
            boolean bAPIProvidesInitializer = PROVIDES_INITIALZER.hasAnnotation(apiMember);
            if (bAPIProvidesInitializer && !hasInitializer(impl)) {
                if (bAPIProvidesInitializer) {
                    return ProjectCompareResult.error("no initializer in implementation but @" + PROVIDES_INITIALZER.name + " in API");
                } else {
                    return ProjectCompareResult.error("initializer in implementation but no @" + PROVIDES_INITIALZER.name + " in API)");
                }
            }
        } else {
            // Method or accessor
            boolean bAPIProvidesDefImpl = PROVIDES_DEFAULT_IMPLEMENTATION.hasAnnotation(apiMember);
            if ((bAPIProvidesDefImpl != hasBody(impl)) && apiMember.eContainer() instanceof TInterface) {
                if (bAPIProvidesDefImpl) {
                    return ProjectCompareResult.error("no body in implementation but @" + PROVIDES_DEFAULT_IMPLEMENTATION.name + " in API");
                } else {
                    return ProjectCompareResult.error("body in implementation but no @" + PROVIDES_DEFAULT_IMPLEMENTATION.name + " in API");
                }
            }
        }
        final TypeRef context = TypeUtils.createTypeRef((Type) api.eContainer());
        final TypeRef typeApi = typeSystem.tau(apiMember, context);
        final TypeRef typeImpl = typeSystem.tau(implMember, context);
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(api);
        RuleEnvironmentExtensions.setTypeReplacement(G, typeReplacementProvider);
        final Result<Boolean> implSubtypeApi = typeSystem.subtype(G, typeImpl, typeApi);
        final Result<Boolean> apiSubtypeImpl = typeSystem.subtype(G, typeApi, typeImpl);
        final boolean isImplSubtypeApi = !implSubtypeApi.failed();
        final boolean isApiSubtypeImpl = !apiSubtypeImpl.failed();
        final boolean isEqualType = isImplSubtypeApi && isApiSubtypeImpl;
        if (!isEqualType) {
            if (isImplSubtypeApi)
                // not equal but at least compliant
                return ProjectCompareResult.compliant();
            else {
                final String msg = implSubtypeApi.getRuleFailedException().getLocalizedMessage();
                // not even compliant
                return ProjectCompareResult.error(msg);
            }
        }
        if (isSpecialCaseOfHiddenMethodDiff(api, impl)) {
            // not equal but at least compliant
            return ProjectCompareResult.compliant();
        }
        // all fine
        return ProjectCompareResult.equal();
    }
    // classifier compare
    if (api instanceof TClassifier && impl instanceof TClassifier) {
        TClassifier apiClassifier = (TClassifier) api;
        TClassifier implClassifier = (TClassifier) impl;
        EList<TypeVariable> apiTypeVars = apiClassifier.getTypeVars();
        EList<TypeVariable> implTypeVars = implClassifier.getTypeVars();
        // check for number of type variables
        if (apiTypeVars.size() != implTypeVars.size()) {
            return ProjectCompareResult.error("the number of type variables doesn't match");
        }
        final RuleEnvironment ruleEnvironment = RuleEnvironmentExtensions.newRuleEnvironment(api);
        RuleEnvironmentExtensions.setTypeReplacement(ruleEnvironment, typeReplacementProvider);
        // check for upper bound compatibility
        for (int i = 0; i < apiTypeVars.size(); i++) {
            TypeVariable apiTypeVar = apiTypeVars.get(i);
            TypeVariable implTypeVar = implTypeVars.get(i);
            TypeRef apiDeclaredUpperBound = apiTypeVar.getDeclaredUpperBound();
            TypeRef implDeclaredUpperBound = implTypeVar.getDeclaredUpperBound();
            if ((apiDeclaredUpperBound != null) != (implDeclaredUpperBound != null) || (apiDeclaredUpperBound != null && implDeclaredUpperBound != null && !typeSystem.equaltypeSucceeded(ruleEnvironment, apiDeclaredUpperBound, implDeclaredUpperBound))) {
                return ProjectCompareResult.error(String.format("the upper bound of type variable %s isn't compatible with the API", implTypeVar.getName()));
            }
        }
        return ProjectCompareResult.equal();
    }
    // text-based compare:
    // always compare with API
    final String textApi = entry.getTextAPI();
    final String textImpl = entry.getTextImpl(implIdx);
    final boolean isEqual = textApi != null ? textApi.equals(textImpl) : textImpl == null;
    if (!isEqual)
        return ProjectCompareResult.error(textImpl + " is not equal to " + textApi);
    return ProjectCompareResult.equal();
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) TField(org.eclipse.n4js.ts.types.TField) TInterface(org.eclipse.n4js.ts.types.TInterface) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) MemberAccessModifier(org.eclipse.n4js.ts.types.MemberAccessModifier) ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) EObject(org.eclipse.emf.ecore.EObject) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember)

Example 9 with TypeVariable

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

the class TypeUtils method primCollectTypeVarsInStructMembers.

private static void primCollectTypeVarsInStructMembers(StructuralTypeRef typeRef, Set<TypeVariable> addHere) {
    typeRef.getStructuralMembers().forEach(currM -> {
        currM.eAllContents().forEachRemaining(currObj -> {
            if (currObj instanceof ParameterizedTypeRef && ((ParameterizedTypeRef) currObj).getDeclaredType() instanceof TypeVariable) {
                final TypeVariable tv = (TypeVariable) ((ParameterizedTypeRef) currObj).getDeclaredType();
                addHere.add(tv);
            }
            if (currObj instanceof StructuralTypeRef) {
                primCollectTypeVarsInStructMembers((StructuralTypeRef) currObj, addHere);
            }
        });
    });
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable)

Example 10 with TypeVariable

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

the class ExistentialTypeRefImpl method setBoundTypeVariable.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setBoundTypeVariable(TypeVariable newBoundTypeVariable) {
    TypeVariable oldBoundTypeVariable = boundTypeVariable;
    boundTypeVariable = newBoundTypeVariable;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, TypeRefsPackage.EXISTENTIAL_TYPE_REF__BOUND_TYPE_VARIABLE, oldBoundTypeVariable, boundTypeVariable));
}
Also used : TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Aggregations

TypeVariable (org.eclipse.n4js.ts.types.TypeVariable)24 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)16 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)14 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)10 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)9 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)8 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)8 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)8 ContainerType (org.eclipse.n4js.ts.types.ContainerType)8 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)7 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)7 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)7 TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)7 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)7 TFormalParameter (org.eclipse.n4js.ts.types.TFormalParameter)7 Type (org.eclipse.n4js.ts.types.Type)7 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)7 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)5 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)4 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)4