Search in sources :

Example 1 with N4ClassifierDefinition

use of org.eclipse.n4js.n4JS.N4ClassifierDefinition in project n4js by eclipse.

the class N4JSScopingConsumableMethodsDiagnosis method diagnose.

@Override
DiagnosticMessage diagnose(QualifiedName name, ParameterizedPropertyAccessExpression propertyAccess) {
    // determine containing member declaration and classifier definition
    N4MemberDeclaration containingMemberDeclaration = EcoreUtil2.getContainerOfType(propertyAccess, N4MemberDeclaration.class);
    N4ClassifierDefinition classifierDefinition = EcoreUtil2.getContainerOfType(containingMemberDeclaration, N4ClassifierDefinition.class);
    // if ancestors present and non-static context (no super in static context)
    if (containingMemberDeclaration != null && !containingMemberDeclaration.isStatic() && classifierDefinition != null) {
        // Get candidate methods
        MemberList<TMember>.MemberIterable<TMethod> methods = containerTypesHelper.fromContext(propertyAccess).membersOfImplementedInterfacesForConsumption((TClassifier) classifierDefinition.getDefinedType()).methods();
        boolean hasMethod = methods.stream().filter(// Filter for non-static non-abstract methods
        m -> !m.isHasNoBody() && !m.isStatic()).anyMatch(m -> m.getName().equals(name.toString()));
        if (hasMethod) {
            return createMessage(IssueCodes.CLF_CANNOT_REFER_TO_DEFAULT_METHOD_WITH_SUPER, IssueCodes.getMessageForCLF_CANNOT_REFER_TO_DEFAULT_METHOD_WITH_SUPER());
        }
    }
    return null;
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IssueCodes(org.eclipse.n4js.validation.IssueCodes) Inject(com.google.inject.Inject) TMember(org.eclipse.n4js.ts.types.TMember) TMethod(org.eclipse.n4js.ts.types.TMethod) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) ContainerTypesHelper(org.eclipse.n4js.utils.ContainerTypesHelper) TClassifier(org.eclipse.n4js.ts.types.TClassifier) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) N4MemberDeclaration(org.eclipse.n4js.n4JS.N4MemberDeclaration) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) TMethod(org.eclipse.n4js.ts.types.TMethod) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) N4MemberDeclaration(org.eclipse.n4js.n4JS.N4MemberDeclaration)

Example 2 with N4ClassifierDefinition

use of org.eclipse.n4js.n4JS.N4ClassifierDefinition in project n4js by eclipse.

the class MemberVisibilityChecker method getActualDeclaredReceiverType.

/**
 * Returns the actual receiver type, which usually simply is the declared type of the receiver type. However, in
 * case of classifier references, enums, or structural type references, the actual receiver may be differently
 * computed.
 */
private Type getActualDeclaredReceiverType(EObject context, TypeRef receiverType, ResourceSet resourceSet) {
    if (receiverType instanceof TypeTypeRef) {
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(context);
        return tsh.getStaticType(G, (TypeTypeRef) receiverType);
    }
    if (receiverType instanceof ThisTypeRef) {
        ThisTypeRef thisTypeRef = (ThisTypeRef) receiverType;
        if (thisTypeRef.isUseSiteStructuralTyping()) {
            FunctionOrFieldAccessor foa = N4JSASTUtils.getContainingFunctionOrAccessor(thisTypeRef);
            N4ClassifierDefinition classifier = EcoreUtil2.getContainerOfType(foa, N4ClassifierDefinition.class);
            return classifier.getDefinedType();
        }
    }
    if (receiverType instanceof FunctionTypeExpression) {
        if (resourceSet == null)
            return null;
        // Change receiverType to implicit super class Function.
        BuiltInTypeScope builtInTypeScope = BuiltInTypeScope.get(resourceSet);
        TObjectPrototype functionType = builtInTypeScope.getFunctionType();
        return functionType;
    }
    return receiverType.getDeclaredType();
}
Also used : ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope)

Example 3 with N4ClassifierDefinition

use of org.eclipse.n4js.n4JS.N4ClassifierDefinition in project n4js by eclipse.

the class N4JSMemberRedefinitionValidator method constraints_42_45_46_AbstractMember.

/**
 * Constraints 42, 3 (Abstract Member)<br>
 * Constraints 45, 4 (Extending Interfaces)<br>
 * Constraints 46, 5 (Implementing Interfaces)
 *
 * This method doesn't add issues for missing override annotations but adds the missing-annotation-members to the
 * given collection.
 */
private void constraints_42_45_46_AbstractMember(MemberMatrix mm, Map<ParameterizedTypeRef, MemberList<TMember>> nonAccessibleAbstractMembersBySuperTypeRef) {
    N4ClassifierDefinition classifierDefinition = getCurrentClassifierDefinition();
    TClassifier classifier = getCurrentClassifier();
    TModule contextModule = EcoreUtil2.getContainerOfType(classifier, TModule.class);
    for (SourceAwareIterator iter = mm.allMembers(); iter.hasNext(); ) {
        TMember m = iter.next();
        if (!iter.isOwnedMember() && m.isAbstract()) {
            if (!memberVisibilityChecker.isVisibleWhenOverriding(contextModule, classifier, classifier, m)) {
                Iterable<ParameterizedTypeRef> superTypeRefs = FindClassifierInHierarchyUtils.findSuperTypesWithMember(classifierDefinition, m);
                for (ParameterizedTypeRef superTypeRef : superTypeRefs) {
                    MemberList<TMember> nonAccessible = nonAccessibleAbstractMembersBySuperTypeRef.get(superTypeRef);
                    if (nonAccessible == null) {
                        nonAccessible = new MemberList<>();
                        nonAccessibleAbstractMembersBySuperTypeRef.put(superTypeRef, nonAccessible);
                    }
                    nonAccessible.add(m);
                }
            }
        }
    }
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) SourceAwareIterator(org.eclipse.n4js.validation.validators.utils.MemberMatrix.SourceAwareIterator) TModule(org.eclipse.n4js.ts.types.TModule) TMember(org.eclipse.n4js.ts.types.TMember)

Example 4 with N4ClassifierDefinition

use of org.eclipse.n4js.n4JS.N4ClassifierDefinition in project n4js by eclipse.

the class N4JSMemberRedefinitionValidator method addIssue.

private void addIssue(String message, String issueCode) {
    N4ClassifierDefinition classifier = getCurrentClassifierDefinition();
    EStructuralFeature nameFeature = classifier.eClass().getEStructuralFeature("name");
    addIssue(message, classifier, nameFeature, issueCode);
}
Also used : N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature)

Example 5 with N4ClassifierDefinition

use of org.eclipse.n4js.n4JS.N4ClassifierDefinition in project n4js by eclipse.

the class InternalTypeSystem method applyRuleThisTypeRefEObject.

protected Result<TypeRef> applyRuleThisTypeRefEObject(final RuleEnvironment G, final RuleApplicationTrace _trace_, final EObject location) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    final FunctionOrFieldAccessor containingFunctionOrAccessor = N4JSASTUtils.getContainingFunctionOrAccessor(location);
    boolean _matched = false;
    if (containingFunctionOrAccessor instanceof ArrowFunction) {
        _matched = true;
        /* G |~ containingFunctionOrAccessor ~> T */
        Result<TypeRef> result = thisTypeRefInternal(G, _trace_, ((ArrowFunction) containingFunctionOrAccessor));
        checkAssignableTo(result.getFirst(), TypeRef.class);
        T = (TypeRef) result.getFirst();
    }
    if (!_matched) {
        IdentifiableElement _definedFunctionOrAccessor = null;
        if (containingFunctionOrAccessor != null) {
            _definedFunctionOrAccessor = containingFunctionOrAccessor.getDefinedFunctionOrAccessor();
        }
        final IdentifiableElement containingTFunctionOrAccessor = _definedFunctionOrAccessor;
        final TypeRef declaredThisType = TypeSystemHelper.declaredThisType(containingTFunctionOrAccessor);
        if ((declaredThisType != null)) {
            if ((declaredThisType instanceof ParameterizedTypeRef)) {
                /* G |~ declaredThisType ~> T */
                Result<TypeRef> result = thisTypeRefInternal(G, _trace_, ((ParameterizedTypeRef) declaredThisType));
                checkAssignableTo(result.getFirst(), TypeRef.class);
                T = (TypeRef) result.getFirst();
            } else {
                T = declaredThisType;
            }
        } else {
            final ThisTarget thisTarget = N4JSASTUtils.getProbableThisTarget(location);
            boolean _matched_1 = false;
            if (thisTarget instanceof ObjectLiteral) {
                _matched_1 = true;
                /* G |- thisTarget: T */
                Result<TypeRef> result_1 = typeInternal(G, _trace_, ((ObjectLiteral) thisTarget));
                checkAssignableTo(result_1.getFirst(), TypeRef.class);
                T = (TypeRef) result_1.getFirst();
            }
            if (!_matched_1) {
                if (thisTarget instanceof N4ClassifierDefinition) {
                    _matched_1 = true;
                    Type thisTargetDEFTYPE = ((N4ClassifierDefinition) thisTarget).getDefinedType();
                    if ((thisTarget instanceof N4ClassDeclaration)) {
                        final TClass clazz = ((N4ClassDeclaration) thisTarget).getDefinedTypeAsClass();
                        if (((clazz != null) && clazz.isStaticPolyfill())) {
                            final Type actualClazz = clazz.getSuperClassRef().getDeclaredType();
                            if ((actualClazz != null)) {
                                thisTargetDEFTYPE = actualClazz;
                            }
                        }
                    }
                    if ((thisTargetDEFTYPE != null)) {
                        final FunctionDefinition containingFunction = N4JSASTUtils.getContainingFunction(location);
                        if (((containingFunction instanceof N4MethodDeclaration) && ((N4MemberDeclaration) containingFunction).isStatic())) {
                            boolean _isInReturnDeclaration_Of_StaticMethod = RuleEnvironmentExtensions.isInReturnDeclaration_Of_StaticMethod(location, ((N4MethodDeclaration) containingFunction));
                            if (_isInReturnDeclaration_Of_StaticMethod) {
                                /* G |~ thisTargetDEFTYPE.ref ~> T */
                                TypeRef _ref = TypeExtensions.ref(thisTargetDEFTYPE);
                                Result<TypeRef> result_1 = thisTypeRefInternal(G, _trace_, _ref);
                                checkAssignableTo(result_1.getFirst(), TypeRef.class);
                                T = (TypeRef) result_1.getFirst();
                            } else {
                                boolean _isInBody_Of_StaticMethod = RuleEnvironmentExtensions.isInBody_Of_StaticMethod(location, ((N4MethodDeclaration) containingFunction));
                                if (_isInBody_Of_StaticMethod) {
                                    T = TypeUtils.createClassifierBoundThisTypeRef(TypeUtils.createTypeTypeRef(TypeExtensions.ref(thisTargetDEFTYPE), false));
                                } else {
                                    T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                }
                            }
                        } else {
                            final N4FieldDeclaration n4Field = EcoreUtil2.<N4FieldDeclaration>getContainerOfType(location, N4FieldDeclaration.class);
                            if (((n4Field != null) && n4Field.isStatic())) {
                                T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
                            } else {
                                final N4GetterDeclaration n4Getter = EcoreUtil2.<N4GetterDeclaration>getContainerOfType(location, N4GetterDeclaration.class);
                                if (((n4Getter != null) && n4Getter.isStatic())) {
                                    T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                } else {
                                    final N4SetterDeclaration n4Setter = EcoreUtil2.<N4SetterDeclaration>getContainerOfType(location, N4SetterDeclaration.class);
                                    if (((n4Setter != null) && n4Setter.isStatic())) {
                                        T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                    } else {
                                        /* G |~ thisTargetDEFTYPE.ref ~> T */
                                        TypeRef _ref_1 = TypeExtensions.ref(thisTargetDEFTYPE);
                                        Result<TypeRef> result_2 = thisTypeRefInternal(G, _trace_, _ref_1);
                                        checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                        T = (TypeRef) result_2.getFirst();
                                    }
                                }
                            }
                        }
                    } else {
                        T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
                    }
                }
            }
            if (!_matched_1) {
                boolean _hasGlobalObject = this.jsVariantHelper.hasGlobalObject(location);
                if (_hasGlobalObject) {
                    T = RuleEnvironmentExtensions.globalObjectTypeRef(G);
                } else {
                    T = RuleEnvironmentExtensions.undefinedTypeRef(G);
                }
            }
        }
    }
    return new Result<TypeRef>(T);
}
Also used : ObjectLiteral(org.eclipse.n4js.n4JS.ObjectLiteral) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) N4FieldDeclaration(org.eclipse.n4js.n4JS.N4FieldDeclaration) 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) N4GetterDeclaration(org.eclipse.n4js.n4JS.N4GetterDeclaration) ArrowFunction(org.eclipse.n4js.n4JS.ArrowFunction) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) N4MemberDeclaration(org.eclipse.n4js.n4JS.N4MemberDeclaration) N4ClassDeclaration(org.eclipse.n4js.n4JS.N4ClassDeclaration) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) N4SetterDeclaration(org.eclipse.n4js.n4JS.N4SetterDeclaration) 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) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) N4MethodDeclaration(org.eclipse.n4js.n4JS.N4MethodDeclaration) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) TClass(org.eclipse.n4js.ts.types.TClass) ThisTarget(org.eclipse.n4js.n4JS.ThisTarget)

Aggregations

N4ClassifierDefinition (org.eclipse.n4js.n4JS.N4ClassifierDefinition)5 FunctionOrFieldAccessor (org.eclipse.n4js.n4JS.FunctionOrFieldAccessor)2 N4MemberDeclaration (org.eclipse.n4js.n4JS.N4MemberDeclaration)2 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)2 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)2 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)2 TClassifier (org.eclipse.n4js.ts.types.TClassifier)2 TMember (org.eclipse.n4js.ts.types.TMember)2 Inject (com.google.inject.Inject)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 ArrowFunction (org.eclipse.n4js.n4JS.ArrowFunction)1 FunctionDefinition (org.eclipse.n4js.n4JS.FunctionDefinition)1 N4ClassDeclaration (org.eclipse.n4js.n4JS.N4ClassDeclaration)1 N4FieldDeclaration (org.eclipse.n4js.n4JS.N4FieldDeclaration)1 N4GetterDeclaration (org.eclipse.n4js.n4JS.N4GetterDeclaration)1 N4MethodDeclaration (org.eclipse.n4js.n4JS.N4MethodDeclaration)1 N4SetterDeclaration (org.eclipse.n4js.n4JS.N4SetterDeclaration)1 ObjectLiteral (org.eclipse.n4js.n4JS.ObjectLiteral)1 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)1 ThisTarget (org.eclipse.n4js.n4JS.ThisTarget)1