Search in sources :

Example 1 with ParameterizedPropertyAccessExpression

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

the class N4JSEnumValidator method checkUsageOfStringBasedEnum.

/**
 * See N4JS Specification, Req. IDE-41, Nr. 6.
 */
@Check
public void checkUsageOfStringBasedEnum(IdentifierRef identRef) {
    final IdentifiableElement id = identRef.getId();
    if (id == null || id.eIsProxy()) {
        return;
    }
    if (!(id instanceof TEnum)) {
        return;
    }
    final TEnum tEnum = (TEnum) id;
    if (!AnnotationDefinition.STRING_BASED.hasAnnotation(tEnum)) {
        return;
    }
    // we now have an IdentifierRef pointing to a string-based enum ...
    final EObject parent = N4JSASTUtils.skipParenExpressionUpward(identRef.eContainer());
    final ParameterizedPropertyAccessExpression parentPAE = parent instanceof ParameterizedPropertyAccessExpression ? (ParameterizedPropertyAccessExpression) parent : null;
    final IdentifiableElement prop = parentPAE != null ? parentPAE.getProperty() : null;
    if (prop != null) {
        if (prop.eIsProxy()) {
            // unnecessary duplicate error
            return;
        }
        if (tEnum.getLiterals().contains(prop)) {
            // reference to one of tEnum's literals -> valid usage!
            return;
        }
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(identRef);
        final TMember getterLiterals = RuleEnvironmentExtensions.n4StringBasedEnumType(G).findOwnedMember("literals", false, true);
        if (prop == getterLiterals) {
            // reference to static getter 'literals' in N4StringBasedEnum -> valid usage!
            return;
        }
    }
    // invalid usage!
    addIssue(getMessageForENM_INVALID_USE_OF_STRINGBASED_ENUM(), identRef, ENM_INVALID_USE_OF_STRINGBASED_ENUM);
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember) Check(org.eclipse.xtext.validation.Check)

Example 2 with ParameterizedPropertyAccessExpression

use of org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression 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)

Example 3 with ParameterizedPropertyAccessExpression

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

the class ScopeXpectMethod method binding.

/**
 * Checks that a given element is bound to something identified by (simple) qualified name. The check is designed as
 * simple as possible. That is, simply the next following expression is tested, and within that we expect a property
 * access or a direct identifiable element. The compared name is the simple qualified name, that is container (type)
 * followed by elements name, without URIs of modules etc.
 */
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public // 
void binding(// 
@CommaSeparatedValuesExpectation ICommaSeparatedValuesExpectation expectation, // 
ICrossEReferenceAndEObject arg1) {
    EObject eobj = arg1.getEObject();
    ParameterizedPropertyAccessExpression ppae = EcoreUtil2.getContainerOfType(eobj, ParameterizedPropertyAccessExpression.class);
    IdentifiableElement element;
    if (ppae != null) {
        element = ppae.getProperty();
    } else if (eobj instanceof IdentifiableElement) {
        element = (IdentifiableElement) eobj;
    } else {
        throw new IllegalArgumentException("Cannot check binding for " + (eobj == null ? "null" : eobj.eClass().getName()));
    }
    String container = "";
    if (element instanceof TMember) {
        container = ((TMember) element).getContainingType().getName() + ".";
    }
    final String qn = container + element.getName();
    // URI uri = eobj == null ? null : eobj.eResource() == null ? null : eobj.eResource().getURI();
    expectation.assertEquals(Collections.singleton(qn));
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) EObject(org.eclipse.emf.ecore.EObject) ICrossEReferenceAndEObject(org.eclipse.xpect.xtext.lib.util.XtextOffsetAdapter.ICrossEReferenceAndEObject) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) TMember(org.eclipse.n4js.ts.types.TMember) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 4 with ParameterizedPropertyAccessExpression

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

the class ParameterizedCallExpressionImpl method getReceiver.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public Expression getReceiver() {
    Expression _xifexpression = null;
    Expression _target = this.getTarget();
    if ((_target instanceof ParameterizedPropertyAccessExpression)) {
        Expression _target_1 = this.getTarget();
        _xifexpression = ((ParameterizedPropertyAccessExpression) _target_1).getTarget();
    } else {
        _xifexpression = null;
    }
    return _xifexpression;
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) Expression(org.eclipse.n4js.n4JS.Expression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression)

Example 5 with ParameterizedPropertyAccessExpression

use of org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression 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)

Aggregations

ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)11 EObject (org.eclipse.emf.ecore.EObject)5 Expression (org.eclipse.n4js.n4JS.Expression)5 ParameterizedCallExpression (org.eclipse.n4js.n4JS.ParameterizedCallExpression)5 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)3 IndexedAccessExpression (org.eclipse.n4js.n4JS.IndexedAccessExpression)3 NewExpression (org.eclipse.n4js.n4JS.NewExpression)3 UnaryExpression (org.eclipse.n4js.n4JS.UnaryExpression)3 TMember (org.eclipse.n4js.ts.types.TMember)3 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)2 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)2 BinaryBitwiseExpression (org.eclipse.n4js.n4JS.BinaryBitwiseExpression)2 BinaryLogicalExpression (org.eclipse.n4js.n4JS.BinaryLogicalExpression)2 CastExpression (org.eclipse.n4js.n4JS.CastExpression)2 CommaExpression (org.eclipse.n4js.n4JS.CommaExpression)2 ConditionalExpression (org.eclipse.n4js.n4JS.ConditionalExpression)2 EqualityExpression (org.eclipse.n4js.n4JS.EqualityExpression)2 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)2 ExportedVariableDeclaration (org.eclipse.n4js.n4JS.ExportedVariableDeclaration)2 FunctionDeclaration (org.eclipse.n4js.n4JS.FunctionDeclaration)2