Search in sources :

Example 6 with FunctionDefinition

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

the class PropertyMethodDeclarationImpl method getDefinedFunctionOrAccessor.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public IdentifiableElement getDefinedFunctionOrAccessor() {
    final FunctionOrFieldAccessor _this = this;
    EObject _switchResult = null;
    boolean _matched = false;
    if (_this instanceof FunctionDefinition) {
        _matched = true;
        _switchResult = ((FunctionDefinition) _this).getDefinedType();
    }
    if (!_matched) {
        if (_this instanceof FieldAccessor) {
            _matched = true;
            _switchResult = ((FieldAccessor) _this).getDefinedAccessor();
        }
    }
    return ((IdentifiableElement) _switchResult);
}
Also used : FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) FieldAccessor(org.eclipse.n4js.n4JS.FieldAccessor)

Example 7 with FunctionDefinition

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

the class FunctionValidationHelper method internalCheckFormalParameters.

/**
 * IDEBUG-211, IDE-145</br>
 * Check for variadic, default, and missing initializer forward references in formal parameters. </br>
 * Note: This method is called for the {@link N4JSFunctionValidator}.
 */
public static <T extends EObject> void internalCheckFormalParameters(T[] fpars, Predicate<T> variadic, Predicate<T> hasInitAssgn, Function<T, String> name, TripleConsumer<String, String, EObject> issueConsumer) {
    List<T> fparsL = Arrays.asList(fpars);
    for (T fPar : fparsL) {
        // check 1. and 2. only if the we are not in a FunctionDefinition to avoid double checking!
        // Parameters of FunctionDefintions are also checked by the ASTStructureValidator,
        // which invokes the following method already for parameter.
        boolean isInFunctionDefinition = fPar.eContainer() instanceof FunctionDefinition;
        if (!isInFunctionDefinition)
            internalCheckFormalParameter(fparsL, fPar, variadic, hasInitAssgn, issueConsumer);
        // 3. Reference to succeeding parameter
        if (hasInitAssgn.test(fPar)) {
            int fpPos = fparsL.indexOf(fPar);
            List<IdentifierRef> irs = EcoreUtil2.getAllContentsOfType(fPar, IdentifierRef.class);
            for (IdentifierRef ir : irs) {
                if (fparsL.indexOf(ir.getId()) >= fpPos) {
                    String msg = getMessageForFUN_PARAM_INITIALIZER_ILLEGAL_FORWARD_REFERENCE();
                    issueConsumer.accept(msg, FUN_PARAM_INITIALIZER_ILLEGAL_FORWARD_REFERENCE, ir);
                }
            }
        }
    }
    // 4. Implicit default formal parameters
    boolean initAssgnVisited = false;
    Iterator<T> iter = fparsL.iterator();
    while (iter.hasNext() && !initAssgnVisited) {
        initAssgnVisited = hasInitAssgn.test(iter.next());
    }
    while (initAssgnVisited && iter.hasNext()) {
        T fpar = iter.next();
        if (!hasInitAssgn.test(fpar) && !variadic.test(fpar)) {
            String msg = getMessageForFUN_PARAM_IMPLICIT_DEFAULT_PARAM(name.apply(fpar));
            issueConsumer.accept(msg, FUN_PARAM_IMPLICIT_DEFAULT_PARAM, fpar);
        }
    }
}
Also used : FUN_PARAM_VARIADIC_ONLY_LAST(org.eclipse.n4js.validation.IssueCodes.FUN_PARAM_VARIADIC_ONLY_LAST) IssueCodes.getMessageForFUN_PARAM_VARIADIC_ONLY_LAST(org.eclipse.n4js.validation.IssueCodes.getMessageForFUN_PARAM_VARIADIC_ONLY_LAST) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef)

Example 8 with FunctionDefinition

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

Example 9 with FunctionDefinition

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

the class FGUtils method isCFContainer.

/**
 * @return true iff the given {@link ControlFlowElement} is a container such as a function's body.
 */
public static boolean isCFContainer(EObject cfe) {
    boolean isScript = cfe instanceof Script;
    boolean isBlock = cfe instanceof Block;
    boolean isExpression = cfe instanceof Expression;
    boolean isBindingPattern = cfe instanceof BindingPattern;
    if (!isScript && !isBlock && !isExpression && !isBindingPattern) {
        return false;
    }
    EObject cfeContainer = cfe.eContainer();
    EObject cfeContainer2 = (cfeContainer == null) ? null : cfeContainer.eContainer();
    boolean containerIsFunctionDeclaration = cfeContainer instanceof FunctionDeclaration;
    boolean containerIsFunctionDefinition = cfeContainer instanceof FunctionDefinition;
    boolean containerIsFieldAccessor = cfeContainer instanceof FieldAccessor;
    boolean containerIsFormalParameter = cfeContainer instanceof FormalParameter;
    boolean containerIsFieldDeclaration = cfeContainer instanceof N4FieldDeclaration;
    boolean containerIsAnnotationArgument = cfeContainer instanceof AnnotationArgument;
    boolean containerIsLiteralOrComputedPropertyName = cfeContainer instanceof LiteralOrComputedPropertyName;
    boolean containerIsExportSpecifier = cfeContainer instanceof ExportSpecifier;
    boolean containerIsExportDeclaration = cfeContainer instanceof ExportDeclaration;
    boolean containerIsN4ClassDefinition = cfeContainer instanceof N4ClassDefinition;
    boolean container2IsN4FieldDeclaration = cfeContainer2 instanceof N4FieldDeclaration;
    boolean isCFContainer = false;
    isCFContainer |= isScript;
    isCFContainer |= isBlock && containerIsFunctionDeclaration;
    isCFContainer |= isBlock && containerIsFunctionDefinition;
    isCFContainer |= isBlock && containerIsFieldAccessor;
    isCFContainer |= isBindingPattern && containerIsFormalParameter;
    isCFContainer |= isExpression && containerIsFormalParameter;
    isCFContainer |= isExpression && containerIsFieldDeclaration;
    isCFContainer |= isExpression && containerIsAnnotationArgument;
    isCFContainer |= isExpression && containerIsLiteralOrComputedPropertyName && container2IsN4FieldDeclaration;
    isCFContainer |= isExpression && containerIsN4ClassDefinition;
    isCFContainer |= isExpression && containerIsExportSpecifier;
    isCFContainer |= isExpression && containerIsExportDeclaration;
    return isCFContainer;
}
Also used : FormalParameter(org.eclipse.n4js.n4JS.FormalParameter) Script(org.eclipse.n4js.n4JS.Script) BindingPattern(org.eclipse.n4js.n4JS.BindingPattern) N4FieldDeclaration(org.eclipse.n4js.n4JS.N4FieldDeclaration) ExportSpecifier(org.eclipse.n4js.n4JS.ExportSpecifier) ExportDeclaration(org.eclipse.n4js.n4JS.ExportDeclaration) FieldAccessor(org.eclipse.n4js.n4JS.FieldAccessor) AnnotationArgument(org.eclipse.n4js.n4JS.AnnotationArgument) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition) FunctionDeclaration(org.eclipse.n4js.n4JS.FunctionDeclaration) Expression(org.eclipse.n4js.n4JS.Expression) EObject(org.eclipse.emf.ecore.EObject) CatchBlock(org.eclipse.n4js.n4JS.CatchBlock) Block(org.eclipse.n4js.n4JS.Block) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) LiteralOrComputedPropertyName(org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName)

Aggregations

FunctionDefinition (org.eclipse.n4js.n4JS.FunctionDefinition)9 EObject (org.eclipse.emf.ecore.EObject)7 FunctionOrFieldAccessor (org.eclipse.n4js.n4JS.FunctionOrFieldAccessor)6 InternalEObject (org.eclipse.emf.ecore.InternalEObject)5 FieldAccessor (org.eclipse.n4js.n4JS.FieldAccessor)5 N4FieldDeclaration (org.eclipse.n4js.n4JS.N4FieldDeclaration)2 AnnotationArgument (org.eclipse.n4js.n4JS.AnnotationArgument)1 ArrowFunction (org.eclipse.n4js.n4JS.ArrowFunction)1 BindingPattern (org.eclipse.n4js.n4JS.BindingPattern)1 Block (org.eclipse.n4js.n4JS.Block)1 CatchBlock (org.eclipse.n4js.n4JS.CatchBlock)1 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)1 ExportSpecifier (org.eclipse.n4js.n4JS.ExportSpecifier)1 Expression (org.eclipse.n4js.n4JS.Expression)1 FormalParameter (org.eclipse.n4js.n4JS.FormalParameter)1 FunctionDeclaration (org.eclipse.n4js.n4JS.FunctionDeclaration)1 FunctionExpression (org.eclipse.n4js.n4JS.FunctionExpression)1 IdentifierRef (org.eclipse.n4js.n4JS.IdentifierRef)1 LiteralOrComputedPropertyName (org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName)1 N4ClassDeclaration (org.eclipse.n4js.n4JS.N4ClassDeclaration)1