Search in sources :

Example 1 with TInterface

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

the class N4JSMemberRedefinitionValidator method messageImpossibleExtendsImplements.

private void messageImpossibleExtendsImplements(N4ClassifierDefinition n4ClassifierDefinition, Map<ParameterizedTypeRef, MemberList<TMember>> nonAccessibleAbstractMembersBySuperTypeRef) {
    for (Entry<ParameterizedTypeRef, MemberList<TMember>> entry : nonAccessibleAbstractMembersBySuperTypeRef.entrySet()) {
        final ParameterizedTypeRef superTypeRef = entry.getKey();
        final Type type = superTypeRef.getDeclaredType();
        final String mode = type instanceof TInterface && !(n4ClassifierDefinition instanceof N4InterfaceDeclaration) ? "implement" : "extend";
        final String message = getMessageForCLF_NON_ACCESSIBLE_ABSTRACT_MEMBERS(mode, validatorMessageHelper.description(type), validatorMessageHelper.descriptions(entry.getValue()));
        addIssue(message, superTypeRef.eContainer(), superTypeRef.eContainingFeature(), CLF_NON_ACCESSIBLE_ABSTRACT_MEMBERS);
    }
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Type(org.eclipse.n4js.ts.types.Type) MemberType(org.eclipse.n4js.ts.types.MemberType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) MemberList(org.eclipse.n4js.ts.types.util.MemberList) TInterface(org.eclipse.n4js.ts.types.TInterface) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration)

Example 2 with TInterface

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

the class N4JSSyntaxValidator method checkClassDefinition.

/**
 * Checks that no "with" is used and that list of implemented interfaces is separated with commas and not with
 * keywords. These checks (with some warnings created instead of errors) should help the transition from roles to
 * interfaces. However, they may be useful later on as well, e.g., if an interface is manually refactored into a
 * class or vice versa.
 * <p>
 * Note that "with" is used in Dart for roles, so maybe it is useful to have a user-friendly message instead of a
 * parser error.
 */
@Check
public void checkClassDefinition(N4ClassDefinition n4ClassDefinition) {
    holdsCorrectOrderOfExtendsImplements(n4ClassDefinition);
    ICompositeNode node = NodeModelUtils.findActualNodeFor(n4ClassDefinition);
    ILeafNode keywordNode = findSecondLeafWithKeyword(n4ClassDefinition, "{", node, EXTENDS_KEYWORD, false);
    if (keywordNode != null) {
        TClass tclass = n4ClassDefinition.getDefinedTypeAsClass();
        if (tclass == null) {
            // avoid consequential errors
            return;
        }
        if (StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).allMatch(superTypeRef -> superTypeRef.getDeclaredType() instanceof TInterface)) {
            List<? extends IdentifiableElement> interfaces = StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).map(ref -> (TInterface) (ref.getDeclaredType())).collect(Collectors.toList());
            String message = getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(validatorMessageHelper.description(tclass), "extend", "interface" + (interfaces.size() > 1 ? "s " : " ") + validatorMessageHelper.names(interfaces), IMPLEMENTS_KEYWORD);
            addIssue(message, n4ClassDefinition, keywordNode.getTotalOffset(), keywordNode.getLength(), SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP);
        }
    }
}
Also used : IssueCodes(org.eclipse.n4js.validation.IssueCodes) AbstractElement(org.eclipse.xtext.AbstractElement) IMPLEMENTS_KEYWORD(org.eclipse.n4js.N4JSLanguageConstants.IMPLEMENTS_KEYWORD) N4JSPackage(org.eclipse.n4js.n4JS.N4JSPackage) SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(org.eclipse.n4js.validation.IssueCodes.SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP) TClass(org.eclipse.n4js.ts.types.TClass) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) HashSet(java.util.HashSet) ModifiableElement(org.eclipse.n4js.n4JS.ModifiableElement) TInterface(org.eclipse.n4js.ts.types.TInterface) Type(org.eclipse.n4js.ts.types.Type) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) N4Modifier(org.eclipse.n4js.n4JS.N4Modifier) AbstractN4JSDeclarativeValidator(org.eclipse.n4js.validation.AbstractN4JSDeclarativeValidator) AST_CATCH_VAR_TYPED(org.eclipse.n4js.validation.IssueCodes.AST_CATCH_VAR_TYPED) IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(org.eclipse.n4js.validation.IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP) StreamSupport(java.util.stream.StreamSupport) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition) BidiTreeIterator(org.eclipse.xtext.nodemodel.BidiTreeIterator) IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_WRONG_ORDER(org.eclipse.n4js.validation.IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_WRONG_ORDER) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) Alternatives(org.eclipse.xtext.Alternatives) Keyword(org.eclipse.xtext.Keyword) EXTENDS_KEYWORD(org.eclipse.n4js.N4JSLanguageConstants.EXTENDS_KEYWORD) List(java.util.List) Stream(java.util.stream.Stream) IssueCodes.getMessageForAST_CATCH_VAR_TYPED(org.eclipse.n4js.validation.IssueCodes.getMessageForAST_CATCH_VAR_TYPED) ModifierUtils(org.eclipse.n4js.n4JS.ModifierUtils) CatchVariable(org.eclipse.n4js.n4JS.CatchVariable) EValidatorRegistrar(org.eclipse.xtext.validation.EValidatorRegistrar) Joiner(com.google.common.base.Joiner) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TInterface(org.eclipse.n4js.ts.types.TInterface) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) TClass(org.eclipse.n4js.ts.types.TClass) Check(org.eclipse.xtext.validation.Check)

Example 3 with TInterface

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

the class InternalTypeSystem method applyRuleSubtypeParameterizedTypeRef.

protected Result<Boolean> applyRuleSubtypeParameterizedTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedTypeRef leftOriginal, final ParameterizedTypeRef rightOriginal) throws RuleFailedException {
    final TypeRef left = RuleEnvironmentExtensions.getReplacement(G, leftOriginal);
    final TypeRef right = RuleEnvironmentExtensions.getReplacement(G, rightOriginal);
    final Type leftDeclType = left.getDeclaredType();
    final Type rightDeclType = right.getDeclaredType();
    if (((leftDeclType == null) || (rightDeclType == null))) {
        /* true */
        if (!true) {
            sneakyThrowRuleFailedException("true");
        }
    } else {
        if ((leftDeclType.eIsProxy() || rightDeclType.eIsProxy())) {
            /* true */
            if (!true) {
                sneakyThrowRuleFailedException("true");
            }
        } else {
            if (((leftDeclType instanceof VoidType) || (rightDeclType instanceof VoidType))) {
                /* leftDeclType instanceof VoidType && rightDeclType instanceof VoidType */
                if (!((leftDeclType instanceof VoidType) && (rightDeclType instanceof VoidType))) {
                    sneakyThrowRuleFailedException("leftDeclType instanceof VoidType && rightDeclType instanceof VoidType");
                }
            } else {
                if ((((leftDeclType instanceof UndefinedType) || ((leftDeclType instanceof NullType) && (!(rightDeclType instanceof UndefinedType)))) || (rightDeclType instanceof AnyType))) {
                    /* true */
                    if (!true) {
                        sneakyThrowRuleFailedException("true");
                    }
                } else {
                    if ((((leftDeclType == RuleEnvironmentExtensions.intType(G)) && (rightDeclType == RuleEnvironmentExtensions.numberType(G))) || ((leftDeclType == RuleEnvironmentExtensions.numberType(G)) && (rightDeclType == RuleEnvironmentExtensions.intType(G))))) {
                        /* true */
                        if (!true) {
                            sneakyThrowRuleFailedException("true");
                        }
                    } else {
                        if (((leftDeclType instanceof TEnum) && ((rightDeclType == RuleEnvironmentExtensions.n4EnumType(G)) || (rightDeclType == RuleEnvironmentExtensions.objectType(G))))) {
                            boolean _hasAnnotation = AnnotationDefinition.STRING_BASED.hasAnnotation(leftDeclType);
                            /* !AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType ) */
                            if (!(!_hasAnnotation)) {
                                sneakyThrowRuleFailedException("!AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType )");
                            }
                        } else {
                            if (((leftDeclType instanceof TEnum) && (((rightDeclType == RuleEnvironmentExtensions.n4StringBasedEnumType(G)) || (rightDeclType == RuleEnvironmentExtensions.stringType(G))) || (rightDeclType == RuleEnvironmentExtensions.stringObjectType(G))))) {
                                /* AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType ) */
                                if (!AnnotationDefinition.STRING_BASED.hasAnnotation(leftDeclType)) {
                                    sneakyThrowRuleFailedException("AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType )");
                                }
                            } else {
                                if (((leftDeclType == RuleEnvironmentExtensions.n4StringBasedEnumType(G)) && ((rightDeclType == RuleEnvironmentExtensions.stringType(G)) || (rightDeclType == RuleEnvironmentExtensions.stringObjectType(G))))) {
                                    /* true */
                                    if (!true) {
                                        sneakyThrowRuleFailedException("true");
                                    }
                                } else {
                                    if (((leftDeclType instanceof PrimitiveType) && (((PrimitiveType) leftDeclType).getAssignmentCompatible() == rightDeclType))) {
                                        /* true */
                                        if (!true) {
                                            sneakyThrowRuleFailedException("true");
                                        }
                                    } else {
                                        if (((rightDeclType instanceof PrimitiveType) && (leftDeclType == ((PrimitiveType) rightDeclType).getAssignmentCompatible()))) {
                                            /* true */
                                            if (!true) {
                                                sneakyThrowRuleFailedException("true");
                                            }
                                        } else {
                                            if (((((leftDeclType instanceof TInterface) && (!(rightDeclType instanceof TInterface))) && Objects.equal(right.getTypingStrategy(), TypingStrategy.NOMINAL)) && (!(((rightDeclType == RuleEnvironmentExtensions.n4ObjectType(G)) || (rightDeclType == RuleEnvironmentExtensions.objectType(G))) || (rightDeclType == RuleEnvironmentExtensions.anyType(G)))))) {
                                                /* false */
                                                if (!false) {
                                                    sneakyThrowRuleFailedException("false");
                                                }
                                            } else {
                                                boolean structuralTyping = false;
                                                boolean _isUseSiteStructuralTyping = right.isUseSiteStructuralTyping();
                                                if (_isUseSiteStructuralTyping) {
                                                    final StructuralTypingResult result = this.typeSystemHelper.isStructuralSubtype(G, left, right);
                                                    boolean _isValue = result.isValue();
                                                    boolean _not = (!_isValue);
                                                    if (_not) {
                                                        /* fail error result.message data PRIORITY_ERROR */
                                                        String _message = result.getMessage();
                                                        String error = _message;
                                                        Object data = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                        throwForExplicitFail(error, new ErrorInformation(null, null, data));
                                                    }
                                                    structuralTyping = true;
                                                } else {
                                                    boolean _isDefSiteStructuralTyping = right.isDefSiteStructuralTyping();
                                                    if (_isDefSiteStructuralTyping) {
                                                        Pair<TypeRef, TypeRef> _mappedTo = Pair.<TypeRef, TypeRef>of(left, right);
                                                        Pair<String, Pair<TypeRef, TypeRef>> _mappedTo_1 = Pair.<String, Pair<TypeRef, TypeRef>>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT, _mappedTo);
                                                        Object _get = G.get(_mappedTo_1);
                                                        final Boolean guard = ((Boolean) _get);
                                                        if (((guard == null) || (!(guard).booleanValue()))) {
                                                            final StructuralTypingResult result_1 = this.typeSystemHelper.isStructuralSubtype(G, left, right);
                                                            boolean _isValue_1 = result_1.isValue();
                                                            boolean _not_1 = (!_isValue_1);
                                                            if (_not_1) {
                                                                boolean _and = false;
                                                                boolean _isN4ObjectOnLeftWithDefSite = result_1.isN4ObjectOnLeftWithDefSite();
                                                                if (!_isN4ObjectOnLeftWithDefSite) {
                                                                    _and = false;
                                                                } else {
                                                                    /* G.wrap, (GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT->(left->right))<-true |- left <: right */
                                                                    RuleEnvironment _wrap = RuleEnvironmentExtensions.wrap(G);
                                                                    Pair<TypeRef, TypeRef> _mappedTo_2 = Pair.<TypeRef, TypeRef>of(left, right);
                                                                    Pair<String, Pair<TypeRef, TypeRef>> _mappedTo_3 = Pair.<String, Pair<TypeRef, TypeRef>>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT, _mappedTo_2);
                                                                    boolean _ruleinvocation = subtypeSucceeded(environmentComposition(_wrap, environmentEntry(_mappedTo_3, true)), _trace_, left, right);
                                                                    _and = _ruleinvocation;
                                                                }
                                                                if (_and) {
                                                                    structuralTyping = true;
                                                                } else {
                                                                    /* fail error result.message data PRIORITY_ERROR */
                                                                    String _message_1 = result_1.getMessage();
                                                                    String error_1 = _message_1;
                                                                    Object data_1 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                                    throwForExplicitFail(error_1, new ErrorInformation(null, null, data_1));
                                                                }
                                                            }
                                                            structuralTyping = result_1.isValue();
                                                        }
                                                    }
                                                }
                                                if ((!structuralTyping)) {
                                                    if ((((left.isUseSiteStructuralTyping() || left.isDefSiteStructuralTyping()) && (!(Objects.equal(rightDeclType, RuleEnvironmentExtensions.objectType(G)) && (leftDeclType instanceof TClassifier)))) && (!(leftDeclType instanceof PrimitiveType)))) {
                                                        /* fail error "Structural type " + left.typeRefAsString + " is not a subtype of non-structural type " + right.typeRefAsString data PRIORITY_ERROR */
                                                        String _typeRefAsString = left.getTypeRefAsString();
                                                        String _plus = ("Structural type " + _typeRefAsString);
                                                        String _plus_1 = (_plus + " is not a subtype of non-structural type ");
                                                        String _typeRefAsString_1 = right.getTypeRefAsString();
                                                        String _plus_2 = (_plus_1 + _typeRefAsString_1);
                                                        String error_2 = _plus_2;
                                                        Object data_2 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                        throwForExplicitFail(error_2, new ErrorInformation(null, null, data_2));
                                                    }
                                                    if (((leftDeclType instanceof TypeVariable) || (rightDeclType instanceof TypeVariable))) {
                                                        boolean _equals = Objects.equal(leftDeclType, rightDeclType);
                                                        if (_equals) {
                                                            /* true */
                                                            if (!true) {
                                                                sneakyThrowRuleFailedException("true");
                                                            }
                                                        } else {
                                                            if ((leftDeclType instanceof TypeVariable)) {
                                                                TypeRef _elvis = null;
                                                                TypeRef _declaredUpperBound = ((TypeVariable) leftDeclType).getDeclaredUpperBound();
                                                                if (_declaredUpperBound != null) {
                                                                    _elvis = _declaredUpperBound;
                                                                } else {
                                                                    TypeRef _typeVariableImplicitUpperBound = N4JSLanguageUtils.getTypeVariableImplicitUpperBound(G);
                                                                    _elvis = _typeVariableImplicitUpperBound;
                                                                }
                                                                final TypeRef ub = _elvis;
                                                                /* G |- ub <: right */
                                                                subtypeInternal(G, _trace_, ub, right);
                                                            } else {
                                                                /* false */
                                                                if (!false) {
                                                                    sneakyThrowRuleFailedException("false");
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        boolean _equals_1 = Objects.equal(leftDeclType, rightDeclType);
                                                        if (_equals_1) {
                                                            if (((left.getTypeArgs().size() > 0) && (left.getTypeArgs().size() <= right.getTypeArgs().size()))) {
                                                                final int len = Math.min(Math.min(left.getTypeArgs().size(), right.getTypeArgs().size()), rightDeclType.getTypeVars().size());
                                                                for (int i = 0; (i < len); i++) {
                                                                    final TypeArgument leftArg = left.getTypeArgs().get(i);
                                                                    final TypeArgument rightArg = right.getTypeArgs().get(i);
                                                                    final Variance variance = rightDeclType.getVarianceOfTypeVar(i);
                                                                    TypeRef leftArgUpper = null;
                                                                    /* G |~ leftArg /\ leftArgUpper */
                                                                    Result<TypeRef> result_2 = upperBoundInternal(G, _trace_, leftArg);
                                                                    checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                                                    leftArgUpper = (TypeRef) result_2.getFirst();
                                                                    TypeRef leftArgLower = null;
                                                                    /* G |~ leftArg \/ leftArgLower */
                                                                    Result<TypeRef> result_3 = lowerBoundInternal(G, _trace_, leftArg);
                                                                    checkAssignableTo(result_3.getFirst(), TypeRef.class);
                                                                    leftArgLower = (TypeRef) result_3.getFirst();
                                                                    TypeRef rightArgUpper = null;
                                                                    /* G |~ rightArg /\ rightArgUpper */
                                                                    Result<TypeRef> result_4 = upperBoundInternal(G, _trace_, rightArg);
                                                                    checkAssignableTo(result_4.getFirst(), TypeRef.class);
                                                                    rightArgUpper = (TypeRef) result_4.getFirst();
                                                                    TypeRef rightArgLower = null;
                                                                    /* G |~ rightArg \/ rightArgLower */
                                                                    Result<TypeRef> result_5 = lowerBoundInternal(G, _trace_, rightArg);
                                                                    checkAssignableTo(result_5.getFirst(), TypeRef.class);
                                                                    rightArgLower = (TypeRef) result_5.getFirst();
                                                                    RuleEnvironment G2 = null;
                                                                    if (((rightArg instanceof Wildcard) && ((Wildcard) rightArg).isImplicitUpperBoundInEffect())) {
                                                                        Pair<String, TypeArgument> _mappedTo_4 = Pair.<String, TypeArgument>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS, rightArg);
                                                                        Object _get_1 = G.get(_mappedTo_4);
                                                                        final boolean isGuarded = (_get_1 != null);
                                                                        if ((!isGuarded)) {
                                                                            G2 = RuleEnvironmentExtensions.wrap(G);
                                                                            Pair<String, TypeArgument> _mappedTo_5 = Pair.<String, TypeArgument>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS, rightArg);
                                                                            /* G2.add(GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS->(rightArg), Boolean.TRUE) */
                                                                            if (!G2.add(_mappedTo_5, Boolean.TRUE)) {
                                                                                sneakyThrowRuleFailedException("G2.add(GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS->(rightArg), Boolean.TRUE)");
                                                                            }
                                                                        } else {
                                                                            rightArgUpper = RuleEnvironmentExtensions.topTypeRef(G);
                                                                            G2 = G;
                                                                        }
                                                                    } else {
                                                                        G2 = G;
                                                                    }
                                                                    /* { if(variance!=Variance.CONTRA) { G2 |- leftArgUpper <: rightArgUpper } if(variance!=Variance.CO) { G2 |- rightArgLower <: leftArgLower } } or { if(previousFailure.isOrCausedByPriorityError) { fail error stringRep(left) + " is not a subtype of " + stringRep(right) + " due to incompatible type arguments: " + previousFailure.compileMessage data PRIORITY_ERROR } else { fail } } */
                                                                    {
                                                                        RuleFailedException previousFailure = null;
                                                                        try {
                                                                            boolean _notEquals = (!Objects.equal(variance, Variance.CONTRA));
                                                                            if (_notEquals) {
                                                                                /* G2 |- leftArgUpper <: rightArgUpper */
                                                                                subtypeInternal(G2, _trace_, leftArgUpper, rightArgUpper);
                                                                            }
                                                                            boolean _notEquals_1 = (!Objects.equal(variance, Variance.CO));
                                                                            if (_notEquals_1) {
                                                                                /* G2 |- rightArgLower <: leftArgLower */
                                                                                subtypeInternal(G2, _trace_, rightArgLower, leftArgLower);
                                                                            }
                                                                        } catch (Exception e) {
                                                                            previousFailure = extractRuleFailedException(e);
                                                                            boolean _isOrCausedByPriorityError = TypeSystemErrorExtensions.isOrCausedByPriorityError(previousFailure);
                                                                            if (_isOrCausedByPriorityError) {
                                                                                /* fail error stringRep(left) + " is not a subtype of " + stringRep(right) + " due to incompatible type arguments: " + previousFailure.compileMessage data PRIORITY_ERROR */
                                                                                String _stringRep = this.stringRep(left);
                                                                                String _plus_3 = (_stringRep + " is not a subtype of ");
                                                                                String _stringRep_1 = this.stringRep(right);
                                                                                String _plus_4 = (_plus_3 + _stringRep_1);
                                                                                String _plus_5 = (_plus_4 + " due to incompatible type arguments: ");
                                                                                String _compileMessage = TypeSystemErrorExtensions.compileMessage(previousFailure);
                                                                                String _plus_6 = (_plus_5 + _compileMessage);
                                                                                String error_3 = _plus_6;
                                                                                Object data_3 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                                                throwForExplicitFail(error_3, new ErrorInformation(null, null, data_3));
                                                                            } else {
                                                                                /* fail */
                                                                                throwForExplicitFail();
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        } else {
                                                            List<ParameterizedTypeRef> _xifexpression = null;
                                                            if ((leftDeclType instanceof ContainerType<?>)) {
                                                                _xifexpression = AllSuperTypeRefsCollector.collect(((ContainerType<?>) leftDeclType));
                                                            } else {
                                                                _xifexpression = CollectionLiterals.<ParameterizedTypeRef>newArrayList();
                                                            }
                                                            final List<ParameterizedTypeRef> allSuperTypeRefs = _xifexpression;
                                                            List<ParameterizedTypeRef> _collectAllImplicitSuperTypes = RuleEnvironmentExtensions.collectAllImplicitSuperTypes(G, left);
                                                            final Iterable<ParameterizedTypeRef> superTypeRefs = Iterables.<ParameterizedTypeRef>concat(allSuperTypeRefs, _collectAllImplicitSuperTypes);
                                                            final Function1<ParameterizedTypeRef, Boolean> _function = (ParameterizedTypeRef it) -> {
                                                                Type _declaredType = it.getDeclaredType();
                                                                return Boolean.valueOf((_declaredType == rightDeclType));
                                                            };
                                                            boolean _exists = IterableExtensions.<ParameterizedTypeRef>exists(superTypeRefs, _function);
                                                            if (_exists) {
                                                                final RuleEnvironment localG_left = RuleEnvironmentExtensions.wrap(G);
                                                                this.typeSystemHelper.addSubstitutions(localG_left, left);
                                                                final Function1<TypeVariable, TypeRef> _function_1 = (TypeVariable it) -> {
                                                                    return TypeExtensions.ref(it);
                                                                };
                                                                final TypeRef syntheticTypeRef = TypeExtensions.ref(rightDeclType, ((TypeArgument[]) Conversions.unwrapArray(ListExtensions.<TypeVariable, TypeRef>map(rightDeclType.getTypeVars(), _function_1), TypeArgument.class)));
                                                                /* localG_left |- syntheticTypeRef ~> var TypeRef effectiveSuperTypeRef */
                                                                TypeRef effectiveSuperTypeRef = null;
                                                                Result<TypeArgument> result_2 = substTypeVariablesInternal(localG_left, _trace_, syntheticTypeRef);
                                                                checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                                                effectiveSuperTypeRef = (TypeRef) result_2.getFirst();
                                                                /* G |- effectiveSuperTypeRef <: right */
                                                                subtypeInternal(G, _trace_, effectiveSuperTypeRef, right);
                                                            } else {
                                                                /* false */
                                                                if (!false) {
                                                                    sneakyThrowRuleFailedException("false");
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return new Result<Boolean>(true);
}
Also used : VoidType(org.eclipse.n4js.ts.types.VoidType) TClassifier(org.eclipse.n4js.ts.types.TClassifier) TInterface(org.eclipse.n4js.ts.types.TInterface) 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) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) TaggedTemplateString(org.eclipse.n4js.n4JS.TaggedTemplateString) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Variance(org.eclipse.n4js.ts.types.util.Variance) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ErrorInformation(org.eclipse.xsemantics.runtime.ErrorInformation) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) AnyType(org.eclipse.n4js.ts.types.AnyType) Pair(org.eclipse.xtext.xbase.lib.Pair) PropertyNameValuePair(org.eclipse.n4js.n4JS.PropertyNameValuePair) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) 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) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType)

Example 4 with TInterface

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

the class ConcreteMembersOrderedForTranspiler method mapToAccessorTuples.

/**
 * Maps getters and setters to {@link AccessorTuple}s, used to generate proper setters-getters-pairs for property.
 * The passed maps may be changed!
 *
 * @param getters
 *            list of getters
 * @param fields
 *            list of fields
 * @param io_setters
 *            map with owned getters, this map may be changed by this method
 * @param io_inheritedGetters
 *            map with inherited getters, this map may be changed by this method
 * @param io_inheritedSetters
 *            map with inherited getters, this map may be changed by this method
 * @return list of accessor tuples.
 */
private static List<AccessorTuple> mapToAccessorTuples(Iterable<TGetter> getters, Map<NameStaticPair, TSetter> io_setters, Iterable<TField> fields, Map<NameStaticPair, TGetter> io_inheritedGetters, Map<NameStaticPair, TSetter> io_inheritedSetters) {
    List<AccessorTuple> tuples = new ArrayList<>();
    // add getters (alone & with corresponding setter)
    for (TGetter getter : getters) {
        AccessorTuple tuple = new AccessorTuple(getter.getName(), getter.isStatic());
        tuple.setGetter(getter);
        NameStaticPair nsp = NameStaticPair.of(getter);
        // do not handle the thing twice
        tuple.setSetter(io_setters.remove(nsp));
        if (tuple.getSetter() == null) {
            tuple.setInheritedSetter(io_inheritedSetters.remove(nsp));
        }
        tuples.add(tuple);
    }
    // add setters w/o getter:
    for (TSetter setter : io_setters.values()) {
        AccessorTuple tuple = new AccessorTuple(setter.getName(), setter.isStatic());
        NameStaticPair nsp = NameStaticPair.of(setter);
        tuple.setSetter(setter);
        tuple.setInheritedGetter(io_inheritedGetters.remove(nsp));
        tuples.add(tuple);
    }
    // remove the inherited references - the field will overwrite them.
    for (TField field : fields) {
        NameStaticPair nsp = NameStaticPair.of(field);
        io_inheritedSetters.remove(nsp);
        io_inheritedGetters.remove(nsp);
    }
    // find getters/setters defined in interfaces which need to be combined:
    for (TSetter inhSetter : io_inheritedSetters.values()) {
        TGetter inhGetter = io_inheritedGetters.remove(NameStaticPair.of(inhSetter));
        if (inhGetter != null && inhSetter.getContainingType() != inhGetter.getContainingType() && (inhSetter.getContainingType() instanceof TInterface || inhGetter.getContainingType() instanceof TInterface)) {
            // getter & setter are inherited from different types.
            AccessorTuple tuple = new AccessorTuple(inhSetter.getName(), inhSetter.isStatic());
            tuple.setInheritedGetter(inhGetter);
            tuple.setInheritedSetter(inhSetter);
            tuples.add(tuple);
        }
    }
    return tuples;
}
Also used : NameStaticPair(org.eclipse.n4js.ts.types.util.NameStaticPair) TSetter(org.eclipse.n4js.ts.types.TSetter) TField(org.eclipse.n4js.ts.types.TField) TInterface(org.eclipse.n4js.ts.types.TInterface) TGetter(org.eclipse.n4js.ts.types.TGetter) ArrayList(java.util.ArrayList) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple)

Example 5 with TInterface

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

the class ScriptApiTracker method computeMissingApiMethods.

/**
 * Looking for at.ProvidesDefaultImplementation on Methods. Normal method declarations are not taken into account,
 * since they would not be executed on the interface level.
 *
 * Beware: also the inheritance in the original API will be taken into account since compiled client code will link
 * against that.
 *
 * @param type
 *            type to search for apis.
 * @return List of {@link VirtualApiTMethod}
 */
public List<TMethod> computeMissingApiMethods(TInterface type, EObject context) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(context.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonAdapter projectComparisonAdapter = optAdapt.get();
        ProjectComparisonEntry compareEntry = projectComparisonAdapter.getEntryFor(EcoreUtil2.getContainerOfType(type, TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(type);
        if (typeCompare == null) {
            // are we in a completely missing API implementation (super-interfaces not implemented ?)
            typeCompare = compareEntry.getChildForElementAPI(type);
        }
        if (typeCompare == null) {
            if (logger.isDebugEnabled()) {
                logger.debug(" want to throw new IllegalstateException() --> comparison for implementation not found type=" + type.getTypeAsString() + " in Implementation " + compareEntry.getElementImpl()[0]);
            }
            return emptyList();
        }
        LinkedHashMultimap<TMethod, TInterface> lhmmMehodInterface = LinkedHashMultimap.<TMethod, TInterface>create();
        Predicate<ProjectComparisonEntry> filter = (pce -> pce.getElementAPI() instanceof TMethod);
        filter = filter.and(pce -> pce.getElementImpl()[0] == null).and(pce -> PROVIDES_DEFAULT_IMPLEMENTATION.hasAnnotation((TMethod) pce.getElementAPI()));
        Function<TInterface, Consumer<? super ProjectComparisonEntry>> actionProvider = pivot -> pce -> {
            TMethod method = ((TMethod) pce.getElementAPI());
            lhmmMehodInterface.put(method, pivot);
        };
        if (!checkInterfaceImplementsInterface(type, typeCompare.getElementAPI())) {
            return emptyList();
        }
        // Call the supertype iterations scaffolding:
        interfaceApiSupertypeWalker(filter, actionProvider, projectComparisonAdapter, (TInterface) typeCompare.getElementAPI(), TInterface.class);
        // out in the caller when processing our results...
        return lhmmMehodInterface.keySet().stream().map(m -> new VirtualApiTMethod(m.getName(), TypeUtils.copyPartial(m, TypesPackage.Literals.SYNTAX_RELATED_TELEMENT__AST_ELEMENT))).collect(Collectors.toList());
    }
    return emptyList();
}
Also used : ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TSetter(org.eclipse.n4js.ts.types.TSetter) Logger(org.apache.log4j.Logger) TGetterImpl(org.eclipse.n4js.ts.types.impl.TGetterImpl) TMethodImpl(org.eclipse.n4js.ts.types.impl.TMethodImpl) Type(org.eclipse.n4js.ts.types.Type) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) TFieldImpl(org.eclipse.n4js.ts.types.impl.TFieldImpl) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) TField(org.eclipse.n4js.ts.types.TField) EObject(org.eclipse.emf.ecore.EObject) PROVIDES_DEFAULT_IMPLEMENTATION(org.eclipse.n4js.AnnotationDefinition.PROVIDES_DEFAULT_IMPLEMENTATION) PROVIDES_INITIALZER(org.eclipse.n4js.AnnotationDefinition.PROVIDES_INITIALZER) TMethod(org.eclipse.n4js.ts.types.TMethod) Collectors(java.util.stream.Collectors) TGetter(org.eclipse.n4js.ts.types.TGetter) List(java.util.List) Stream(java.util.stream.Stream) TClassifier(org.eclipse.n4js.ts.types.TClassifier) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Pair(org.eclipse.xtext.xbase.lib.Pair) TypesFactory(org.eclipse.n4js.ts.types.TypesFactory) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) TypeUtils(org.eclipse.n4js.ts.utils.TypeUtils) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TModule(org.eclipse.n4js.ts.types.TModule) TN4Classifier(org.eclipse.n4js.ts.types.TN4Classifier) TInterface(org.eclipse.n4js.ts.types.TInterface) ProjectCompareHelper(org.eclipse.n4js.compare.ProjectCompareHelper) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) TAnnotableElement(org.eclipse.n4js.ts.types.TAnnotableElement) MemberCollector(org.eclipse.n4js.utils.ContainerTypesHelper.MemberCollector) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) Consumer(java.util.function.Consumer) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) TSetterImpl(org.eclipse.n4js.ts.types.impl.TSetterImpl) Collections(java.util.Collections) TMethod(org.eclipse.n4js.ts.types.TMethod) Consumer(java.util.function.Consumer) TInterface(org.eclipse.n4js.ts.types.TInterface) TModule(org.eclipse.n4js.ts.types.TModule) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry)

Aggregations

TInterface (org.eclipse.n4js.ts.types.TInterface)16 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)9 Type (org.eclipse.n4js.ts.types.Type)8 EObject (org.eclipse.emf.ecore.EObject)7 TClass (org.eclipse.n4js.ts.types.TClass)7 TClassifier (org.eclipse.n4js.ts.types.TClassifier)6 TField (org.eclipse.n4js.ts.types.TField)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)4 TGetter (org.eclipse.n4js.ts.types.TGetter)4 TMember (org.eclipse.n4js.ts.types.TMember)4 TSetter (org.eclipse.n4js.ts.types.TSetter)4 AccessorTuple (org.eclipse.n4js.ts.types.util.AccessorTuple)4 MemberList (org.eclipse.n4js.ts.types.util.MemberList)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 N4InterfaceDeclaration (org.eclipse.n4js.n4JS.N4InterfaceDeclaration)3