Search in sources :

Example 1 with TClass

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

the class N4JSMemberRedefinitionValidator method checkUnpairedAccessorFilling.

private void checkUnpairedAccessorFilling(MemberMatrix mm, N4ClassifierDefinition definition) {
    if (definition.getDefinedType().isStaticPolyfill() && mm.hasMixedAccessorPair()) {
        FieldAccessor ownedAccessor = (FieldAccessor) Iterables.getFirst(mm.owned(), null);
        if (null == ownedAccessor) {
            // Should not happen, a mixed accessor pair implies at least one owned member
            return;
        }
        if (!(definition instanceof N4ClassDefinition)) {
            // Non-class static polyfills aren't allowed. Validated somewhere else.
            return;
        }
        TClass filledClass = MemberRedefinitionUtils.getFilledClass((N4ClassDefinition) definition);
        if (null == filledClass) {
            // Invalid static polyfill class. Validated somewhere else.
            return;
        }
        // Iterate over all inherited members
        SourceAwareIterator memberIterator = mm.actuallyInheritedAndMixedMembers();
        while (memberIterator.hasNext()) {
            TMember next = memberIterator.next();
            ContainerType<?> containingType = next.getContainingType();
            // Issue an error if the member isn't owned by the filled class
            if (containingType != filledClass) {
                messageMissingOwnedAccessor(ownedAccessor);
            }
        }
    }
}
Also used : SourceAwareIterator(org.eclipse.n4js.validation.validators.utils.MemberMatrix.SourceAwareIterator) TMember(org.eclipse.n4js.ts.types.TMember) FieldAccessor(org.eclipse.n4js.ts.types.FieldAccessor) TClass(org.eclipse.n4js.ts.types.TClass) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition)

Example 2 with TClass

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

the class N4JSMemberRedefinitionValidator method constraints_41_AbstractClass.

/**
 * Constraints 41 (Abstract Class)
 */
private boolean constraints_41_AbstractClass(TClassifier classifier, MemberCube memberCube) {
    List<TMember> abstractMembers = null;
    if (!classifier.isAbstract() && classifier instanceof TClass) {
        for (Entry<NameStaticPair, MemberMatrix> entry : memberCube.entrySet()) {
            MemberMatrix mm = entry.getValue();
            MemberList<TMember> l = new MemberList<>();
            Iterators.addAll(l, mm.actuallyInheritedAndMixedMembers());
            for (SourceAwareIterator iter = mm.actuallyInheritedAndMixedMembers(); iter.hasNext(); ) {
                TMember m = iter.next();
                if (m.isAbstract()) {
                    if (abstractMembers == null) {
                        abstractMembers = new ArrayList<>();
                    }
                    abstractMembers.add(m);
                }
            }
        }
    }
    if (abstractMembers != null) {
        messageMissingImplementations(abstractMembers);
        return false;
    }
    return true;
}
Also used : NameStaticPair(org.eclipse.n4js.ts.types.util.NameStaticPair) MemberList(org.eclipse.n4js.ts.types.util.MemberList) SourceAwareIterator(org.eclipse.n4js.validation.validators.utils.MemberMatrix.SourceAwareIterator) MemberMatrix(org.eclipse.n4js.validation.validators.utils.MemberMatrix) TMember(org.eclipse.n4js.ts.types.TMember) TClass(org.eclipse.n4js.ts.types.TClass)

Example 3 with TClass

use of org.eclipse.n4js.ts.types.TClass 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 4 with TClass

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

the class PolyfillValidatorFragment method holdsPolyfill.

/**
 * Checks polyfill constraints on given class declaration using validator to issue errors. Constraints (Polyfill
 * Class) 156: Polyfill
 */
public boolean holdsPolyfill(N4JSClassValidator validator, N4ClassDeclaration n4Class) {
    boolean isStaticPolyFill = isStaticPolyfill(n4Class);
    if (isStaticPolyFill || isPolyfill(n4Class)) {
        PolyfillValidationState state = new PolyfillValidationState();
        state.host = validator;
        state.n4Class = n4Class;
        state.name = n4Class.getName();
        if (state.name == null || !(n4Class.getDefinedType() instanceof TClass)) {
            // consequential error, AST corrupt
            return true;
        }
        state.polyType = (TClass) n4Class.getDefinedType();
        if (state.polyType == null || state.name == null) {
            // consequential error
            return true;
        }
        if (!holdsExpliciteExtends(state)) {
            return false;
        }
        final Type superType = n4Class.getSuperClassRef().getDeclaredType();
        if (!(superType instanceof TClassifier)) {
            // consequential error
            return true;
        }
        state.filledType = (TClassifier) superType;
        // Different rules for static/non-static polyfills:
        if (!isStaticPolyFill) {
            if (!(// 
            holdPolyfillName(state) && // 
            holdsProvidedByRuntime(state) && // 
            holdsNoImplementsOrConsumes(state) && // 
            holdsEqualModifiers(state) && // 
            holdsEqualTypeVariables(state) && // 
            holdsSinglePolyfillSource(state))) {
                return false;
            }
        } else {
            // static polyfill case, IDE-1735
            if (!(// 
            holdPolyfillName(state) && // && holdsNoImplementsOrConsumes(state) //
            holdsFilledClassIsStaticPolyfillAware(// 
            state) && // 
            holdsSameJavascriptVariant(state) && // 
            holdsEqualModifiers(state) && // 
            holdsEqualTypeVariables(state) && // 
            holdsSinglePolyfillSource(state))) {
                return false;
            }
        }
    }
    // ยง 140.1 only polyfills are allowed in StaticPolyfillModule.
    if (!isStaticPolyFill && isContainedInStaticPolyfillModule(n4Class)) {
        // n4Class is toplevel by default
        validator.addIssue(getMessageForPOLY_STATIC_POLYFILL_MODULE_ONLY_FILLING_CLASSES(), n4Class, N4_TYPE_DECLARATION__NAME, POLY_STATIC_POLYFILL_MODULE_ONLY_FILLING_CLASSES);
        return false;
    }
    return true;
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) Type(org.eclipse.n4js.ts.types.Type) TClass(org.eclipse.n4js.ts.types.TClass)

Example 5 with TClass

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

the class InternalTypeSystem method applyRuleTypeSuperLiteral.

protected Result<TypeRef> applyRuleTypeSuperLiteral(final RuleEnvironment G, final RuleApplicationTrace _trace_, final SuperLiteral superLiteral) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    final N4MemberDeclaration containingMemberDecl = EcoreUtil2.<N4MemberDeclaration>getContainerOfType(superLiteral.eContainer(), N4MemberDeclaration.class);
    /* if (containingMemberDecl === null) { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } else { val containingClass = (containingMemberDecl.eContainer as N4ClassDeclaration).definedType as TClass; val superClass = G.getDeclaredOrImplicitSuperType(containingClass) var effectiveSuperClass = superClass if( containingClass.isStaticPolyfill ) { effectiveSuperClass = G.getDeclaredOrImplicitSuperType( superClass as TClass ) } { superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression if(containingMemberDecl.static) T = effectiveSuperClass?.createConstructorTypeRef else T = effectiveSuperClass?.createTypeRef if (T !== null) T = TypeUtils.enforceNominalTyping(T) } or { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } } or { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } */
    {
        RuleFailedException previousFailure = null;
        try {
            if ((containingMemberDecl == null)) {
                T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
            } else {
                EObject _eContainer = containingMemberDecl.eContainer();
                Type _definedType = ((N4ClassDeclaration) _eContainer).getDefinedType();
                final TClass containingClass = ((TClass) _definedType);
                final TClassifier superClass = RuleEnvironmentExtensions.getDeclaredOrImplicitSuperType(G, containingClass);
                TClassifier effectiveSuperClass = superClass;
                boolean _isStaticPolyfill = containingClass.isStaticPolyfill();
                if (_isStaticPolyfill) {
                    effectiveSuperClass = RuleEnvironmentExtensions.getDeclaredOrImplicitSuperType(G, ((TClass) superClass));
                }
                /* { superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression if(containingMemberDecl.static) T = effectiveSuperClass?.createConstructorTypeRef else T = effectiveSuperClass?.createTypeRef if (T !== null) T = TypeUtils.enforceNominalTyping(T) } or { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } */
                {
                    try {
                        /* superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression */
                        if (!((superLiteral.eContainer() instanceof ParameterizedPropertyAccessExpression) || (superLiteral.eContainer() instanceof IndexedAccessExpression))) {
                            sneakyThrowRuleFailedException("superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression");
                        }
                        boolean _isStatic = containingMemberDecl.isStatic();
                        if (_isStatic) {
                            TypeRef _createConstructorTypeRef = null;
                            if (effectiveSuperClass != null) {
                                _createConstructorTypeRef = TypeUtils.createConstructorTypeRef(effectiveSuperClass);
                            }
                            T = _createConstructorTypeRef;
                        } else {
                            ParameterizedTypeRef _createTypeRef = null;
                            if (effectiveSuperClass != null) {
                                _createTypeRef = TypeUtils.createTypeRef(effectiveSuperClass);
                            }
                            T = _createTypeRef;
                        }
                        if ((T != null)) {
                            T = TypeUtils.enforceNominalTyping(T);
                        }
                    } catch (Exception e) {
                        previousFailure = extractRuleFailedException(e);
                        /* { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } */
                        {
                            try {
                                EObject _eContainer_1 = superLiteral.eContainer();
                                /* superLiteral.eContainer instanceof ParameterizedCallExpression */
                                if (!(_eContainer_1 instanceof ParameterizedCallExpression)) {
                                    sneakyThrowRuleFailedException("superLiteral.eContainer instanceof ParameterizedCallExpression");
                                }
                                if (((containingMemberDecl instanceof N4MethodDeclaration) && Objects.equal(containingMemberDecl.getName(), "constructor"))) {
                                    final TMethod ctor = this.containerTypesHelper.fromContext(superLiteral.eResource()).findConstructor(effectiveSuperClass);
                                    ParameterizedTypeRef _createTypeRef_1 = null;
                                    if (ctor != null) {
                                        _createTypeRef_1 = TypeUtils.createTypeRef(ctor);
                                    }
                                    T = _createTypeRef_1;
                                } else {
                                    T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
                                }
                            } catch (Exception e_1) {
                                previousFailure = extractRuleFailedException(e_1);
                                EObject _eContainer_2 = superLiteral.eContainer();
                                /* superLiteral.eContainer instanceof NewExpression */
                                if (!(_eContainer_2 instanceof NewExpression)) {
                                    sneakyThrowRuleFailedException("superLiteral.eContainer instanceof NewExpression");
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e_2) {
            previousFailure = extractRuleFailedException(e_2);
            T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
        }
    }
    return new Result<TypeRef>(T);
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) IndexedAccessExpression(org.eclipse.n4js.n4JS.IndexedAccessExpression) 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) NewExpression(org.eclipse.n4js.n4JS.NewExpression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) N4MemberDeclaration(org.eclipse.n4js.n4JS.N4MemberDeclaration) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) 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) EObject(org.eclipse.emf.ecore.EObject) N4MethodDeclaration(org.eclipse.n4js.n4JS.N4MethodDeclaration) TClass(org.eclipse.n4js.ts.types.TClass)

Aggregations

TClass (org.eclipse.n4js.ts.types.TClass)27 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)16 Type (org.eclipse.n4js.ts.types.Type)15 TMember (org.eclipse.n4js.ts.types.TMember)11 TClassifier (org.eclipse.n4js.ts.types.TClassifier)10 TInterface (org.eclipse.n4js.ts.types.TInterface)9 TMethod (org.eclipse.n4js.ts.types.TMethod)8 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 EObject (org.eclipse.emf.ecore.EObject)5 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)5 TField (org.eclipse.n4js.ts.types.TField)5 MemberList (org.eclipse.n4js.ts.types.util.MemberList)5 Collections.emptyList (java.util.Collections.emptyList)4 HashMap (java.util.HashMap)4 Stream (java.util.stream.Stream)4 N4InterfaceDeclaration (org.eclipse.n4js.n4JS.N4InterfaceDeclaration)4 Script (org.eclipse.n4js.n4JS.Script)4