Search in sources :

Example 6 with VariableDeclaration

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

the class SymbolOfIdentifierRef method getDeclaration.

@Override
public EObject getDeclaration() {
    VariableDeclaration varDecl = null;
    IdentifiableElement id = ir.getId();
    if (id instanceof TVariable) {
        TVariable tvar = (TVariable) id;
        varDecl = (VariableDeclaration) tvar.getAstElement();
        return varDecl;
    } else {
        // id instanceof VariableDeclaration
        return id;
    }
}
Also used : TVariable(org.eclipse.n4js.ts.types.TVariable) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration)

Example 7 with VariableDeclaration

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

the class UsedBeforeDeclaredAnalyserFastVersion method visit.

@Override
protected void visit(ControlFlowElement cfe) {
    if (cfe instanceof VariableDeclaration) {
        checkLists.put((VariableDeclaration) cfe, new HashSet<>());
    }
    if (cfe instanceof IdentifierRef) {
        IdentifierRef ir = (IdentifierRef) cfe;
        IdentifiableElement id = ir.getId();
        if (id instanceof TVariable) {
            TVariable tvar = (TVariable) id;
            id = (VariableDeclaration) tvar.getAstElement();
        }
        Set<IdentifierRef> refs = checkLists.get(id);
        if (refs != null) {
            refs.add(ir);
        }
    }
}
Also used : TVariable(org.eclipse.n4js.ts.types.TVariable) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef)

Example 8 with VariableDeclaration

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

the class N4JSStatementValidator method checkForInLoop.

/**
 * 9.1.4 Iteration Statements, Constraints 118 (For-In-Statement Constraints) Variable declaration must be a string
 */
@Check
public void checkForInLoop(ForStatement forStatement) {
    if (forStatement.isForIn()) {
        TypeRef loopVarType = null;
        EObject location = null;
        RuleEnvironment G = (RuleEnvironment) getContext().get(RuleEnvironment.class);
        if (G == null)
            // wrongly configured test
            return;
        if (!forStatement.getVarDeclsOrBindings().isEmpty()) {
            VariableDeclarationOrBinding varDeclOrBinding = forStatement.getVarDeclsOrBindings().iterator().next();
            location = varDeclOrBinding;
            if (varDeclOrBinding instanceof VariableDeclaration) {
                loopVarType = ((VariableDeclaration) varDeclOrBinding).getDeclaredTypeRef();
            } else {
                VariableBinding varBinding = (VariableBinding) varDeclOrBinding;
                Result<TypeRef> res = typeSystem.type(G, varBinding.getExpression());
                if (!res.failed()) {
                    loopVarType = res.getFirst();
                }
            }
        } else if (forStatement.getInitExpr() != null) {
            location = forStatement.getInitExpr();
            Result<TypeRef> res = typeSystem.type(G, forStatement.getInitExpr());
            if (!res.failed()) {
                loopVarType = res.getFirst();
            }
        }
        if (loopVarType != null) {
            Result<Boolean> res = typeSystem.subtype(G, RuleEnvironmentExtensions.stringTypeRef(G), loopVarType);
            if (res.failed() || !res.getFirst()) {
                addIssue(getMessageForTYS_FOR_IN_VAR_STRING(loopVarType.getTypeRefAsString()), location, TYS_FOR_IN_VAR_STRING);
            }
        }
    }
}
Also used : TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) VariableDeclarationOrBinding(org.eclipse.n4js.n4JS.VariableDeclarationOrBinding) VariableBinding(org.eclipse.n4js.n4JS.VariableBinding) Result(org.eclipse.xsemantics.runtime.Result) Check(org.eclipse.xtext.validation.Check)

Example 9 with VariableDeclaration

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

the class BuiltInTypeScopeTest method testParsing.

@SuppressWarnings("javadoc")
@Test
public void testParsing() throws Exception {
    String content = "var name: any = 'global'";
    Script result = parseHelper.parse(content);
    Assert.assertNotNull(result);
    Assert.assertEquals(1, result.getScriptElements().size());
    ScriptElement elem = result.getScriptElements().get(0);
    Assert.assertTrue(elem instanceof VariableStatement);
    VariableStatement stmt = (VariableStatement) elem;
    VariableDeclaration varDecl = stmt.getVarDecl().get(0);
    ParameterizedTypeRef typeRef = (ParameterizedTypeRef) varDecl.getDeclaredTypeRef();
    Assert.assertFalse("Proxy URI: " + ((InternalEObject) typeRef.getDeclaredType()).eProxyURI(), typeRef.getDeclaredType().eIsProxy());
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Script(org.eclipse.n4js.n4JS.Script) VariableStatement(org.eclipse.n4js.n4JS.VariableStatement) ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) InternalEObject(org.eclipse.emf.ecore.InternalEObject) Test(org.junit.Test)

Example 10 with VariableDeclaration

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

the class InternalTypeSystem method applyRuleExpectedTypeInForStatement.

protected Result<TypeRef> applyRuleExpectedTypeInForStatement(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ForStatement forStmnt, final Expression expression) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    if ((forStmnt.isForOf() && (expression == forStmnt.getExpression()))) {
        final Wildcard wildThing = TypeRefsFactory.eINSTANCE.createWildcard();
        boolean _isTopOfDestructuringForStatement = DestructureUtils.isTopOfDestructuringForStatement(forStmnt);
        if (_isTopOfDestructuringForStatement) {
        } else {
            VariableDeclaration _xifexpression = null;
            boolean _isEmpty = forStmnt.getVarDecl().isEmpty();
            boolean _not = (!_isEmpty);
            if (_not) {
                _xifexpression = forStmnt.getVarDecl().get(0);
            }
            final VariableDeclaration varDeclInFor = _xifexpression;
            VariableDeclaration _xifexpression_1 = null;
            if (((forStmnt.getInitExpr() instanceof IdentifierRef) && (((IdentifierRef) forStmnt.getInitExpr()).getId() instanceof VariableDeclaration))) {
                Expression _initExpr = forStmnt.getInitExpr();
                IdentifiableElement _id = ((IdentifierRef) _initExpr).getId();
                _xifexpression_1 = ((VariableDeclaration) _id);
            }
            final VariableDeclaration varDeclOutside = _xifexpression_1;
            boolean _or = false;
            TypeRef _declaredTypeRef = null;
            if (varDeclInFor != null) {
                _declaredTypeRef = varDeclInFor.getDeclaredTypeRef();
            }
            boolean _tripleNotEquals = (_declaredTypeRef != null);
            if (_tripleNotEquals) {
                _or = true;
            } else {
                _or = (varDeclOutside != null);
            }
            if (_or) {
                VariableDeclaration _xifexpression_2 = null;
                if ((varDeclOutside != null)) {
                    _xifexpression_2 = varDeclOutside;
                } else {
                    _xifexpression_2 = varDeclInFor;
                }
                final VariableDeclaration varDecl = _xifexpression_2;
                /* G |- varDecl : var TypeRef varTypeRef */
                TypeRef varTypeRef = null;
                Result<TypeRef> result = typeInternal(G, _trace_, varDecl);
                checkAssignableTo(result.getFirst(), TypeRef.class);
                varTypeRef = (TypeRef) result.getFirst();
                wildThing.setDeclaredUpperBound(TypeUtils.<TypeRef>copyIfContained(varTypeRef));
            }
        }
        T = RuleEnvironmentExtensions.iterableTypeRef(G, wildThing);
    } else {
        if ((forStmnt.isForIn() && (expression == forStmnt.getExpression()))) {
            T = TypeUtils.createNonSimplifiedUnionType(RuleEnvironmentExtensions.objectTypeRef(G), RuleEnvironmentExtensions.stringTypeRef(G), RuleEnvironmentExtensions.argumentsTypeRef(G));
        }
    }
    return new Result<TypeRef>(T);
}
Also used : Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) BinaryBitwiseExpression(org.eclipse.n4js.n4JS.BinaryBitwiseExpression) AssignmentExpression(org.eclipse.n4js.n4JS.AssignmentExpression) IndexedAccessExpression(org.eclipse.n4js.n4JS.IndexedAccessExpression) UnionTypeExpression(org.eclipse.n4js.ts.typeRefs.UnionTypeExpression) FunctionExpression(org.eclipse.n4js.n4JS.FunctionExpression) PromisifyExpression(org.eclipse.n4js.n4JS.PromisifyExpression) UnaryExpression(org.eclipse.n4js.n4JS.UnaryExpression) ParenExpression(org.eclipse.n4js.n4JS.ParenExpression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) AdditiveExpression(org.eclipse.n4js.n4JS.AdditiveExpression) PostfixExpression(org.eclipse.n4js.n4JS.PostfixExpression) YieldExpression(org.eclipse.n4js.n4JS.YieldExpression) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) RelationalExpression(org.eclipse.n4js.n4JS.RelationalExpression) NewExpression(org.eclipse.n4js.n4JS.NewExpression) IntersectionTypeExpression(org.eclipse.n4js.ts.typeRefs.IntersectionTypeExpression) AwaitExpression(org.eclipse.n4js.n4JS.AwaitExpression) CommaExpression(org.eclipse.n4js.n4JS.CommaExpression) Expression(org.eclipse.n4js.n4JS.Expression) CastExpression(org.eclipse.n4js.n4JS.CastExpression) BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) EqualityExpression(org.eclipse.n4js.n4JS.EqualityExpression) ShiftExpression(org.eclipse.n4js.n4JS.ShiftExpression) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) MultiplicativeExpression(org.eclipse.n4js.n4JS.MultiplicativeExpression) N4ClassExpression(org.eclipse.n4js.n4JS.N4ClassExpression) 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) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Aggregations

VariableDeclaration (org.eclipse.n4js.n4JS.VariableDeclaration)11 IdentifiableElement (org.eclipse.n4js.ts.types.IdentifiableElement)4 EObject (org.eclipse.emf.ecore.EObject)3 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)3 ExportedVariableDeclaration (org.eclipse.n4js.n4JS.ExportedVariableDeclaration)3 IdentifierRef (org.eclipse.n4js.n4JS.IdentifierRef)3 ParameterizedCallExpression (org.eclipse.n4js.n4JS.ParameterizedCallExpression)3 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)3 VariableStatement (org.eclipse.n4js.n4JS.VariableStatement)3 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)3 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)3 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)2 Annotation (org.eclipse.n4js.n4JS.Annotation)2 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)2 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)2 BinaryBitwiseExpression (org.eclipse.n4js.n4JS.BinaryBitwiseExpression)2 BinaryLogicalExpression (org.eclipse.n4js.n4JS.BinaryLogicalExpression)2 BindingElement (org.eclipse.n4js.n4JS.BindingElement)2 CastExpression (org.eclipse.n4js.n4JS.CastExpression)2 CommaExpression (org.eclipse.n4js.n4JS.CommaExpression)2