Search in sources :

Example 1 with BinaryLogicalExpression

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

the class InternalTypeSystem method applyRuleTypeBinaryLogicalExpression.

protected Result<TypeRef> applyRuleTypeBinaryLogicalExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final BinaryLogicalExpression e) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    final Expression lhs = e.getLhs();
    final Expression rhs = e.getRhs();
    boolean _xifexpression = false;
    if ((lhs instanceof ArrayLiteral)) {
        _xifexpression = ((ArrayLiteral) lhs).getElements().isEmpty();
    }
    final boolean lhsIsEmptyArrayLiteral = _xifexpression;
    boolean _xifexpression_1 = false;
    if ((rhs instanceof ArrayLiteral)) {
        _xifexpression_1 = ((ArrayLiteral) rhs).getElements().isEmpty();
    }
    final boolean rhsIsEmptyArrayLiteral = _xifexpression_1;
    /* G |- e.lhs : var TypeRef L */
    Expression _lhs = e.getLhs();
    TypeRef L = null;
    Result<TypeRef> result = typeInternal(G, _trace_, _lhs);
    checkAssignableTo(result.getFirst(), TypeRef.class);
    L = (TypeRef) result.getFirst();
    /* G |- e.rhs : var TypeRef R */
    Expression _rhs = e.getRhs();
    TypeRef R = null;
    Result<TypeRef> result_1 = typeInternal(G, _trace_, _rhs);
    checkAssignableTo(result_1.getFirst(), TypeRef.class);
    R = (TypeRef) result_1.getFirst();
    boolean _and = false;
    boolean _lhsIsEmptyArrayLiteral = lhsIsEmptyArrayLiteral;
    if (!_lhsIsEmptyArrayLiteral) {
        _and = false;
    } else {
        Type _declaredType = null;
        if (R != null) {
            _declaredType = R.getDeclaredType();
        }
        TObjectPrototype _arrayType = RuleEnvironmentExtensions.arrayType(G);
        boolean _tripleEquals = (_declaredType == _arrayType);
        _and = _tripleEquals;
    }
    if (_and) {
        T = R;
    } else {
        boolean _and_1 = false;
        boolean _rhsIsEmptyArrayLiteral = rhsIsEmptyArrayLiteral;
        if (!_rhsIsEmptyArrayLiteral) {
            _and_1 = false;
        } else {
            Type _declaredType_1 = null;
            if (L != null) {
                _declaredType_1 = L.getDeclaredType();
            }
            TObjectPrototype _arrayType_1 = RuleEnvironmentExtensions.arrayType(G);
            boolean _tripleEquals_1 = (_declaredType_1 == _arrayType_1);
            _and_1 = _tripleEquals_1;
        }
        if (_and_1) {
            T = L;
        } else {
            T = this.typeSystemHelper.createUnionType(G, L, R);
        }
    }
    return new Result<TypeRef>(T);
}
Also used : 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) 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) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) 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) ArrayLiteral(org.eclipse.n4js.n4JS.ArrayLiteral) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Example 2 with BinaryLogicalExpression

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

the class AssignmentRelationFactory method handleSubexpressions.

private void handleSubexpressions(Multimap<Symbol, Object> assgns, ControlFlowElement lhs, Expression rhs) {
    rhs = ASTUtils.unwrapParentheses(rhs);
    if (rhs instanceof AssignmentExpression) {
        AssignmentExpression ae = (AssignmentExpression) rhs;
        Expression innerRhs = ae.getRhs();
        // The inner assignment is handled already.
        handleSubexpressions(assgns, lhs, innerRhs);
    } else if (rhs instanceof ConditionalExpression) {
        ConditionalExpression ce = (ConditionalExpression) rhs;
        Expression trueExpr = ce.getTrueExpression();
        Expression falseExpr = ce.getFalseExpression();
        handleSubexpressions(assgns, lhs, trueExpr);
        handleSubexpressions(assgns, lhs, falseExpr);
    } else if (rhs instanceof BinaryLogicalExpression) {
        BinaryLogicalExpression ble = (BinaryLogicalExpression) rhs;
        if (ble.getOp() == BinaryLogicalOperator.OR) {
            handleSubexpressions(assgns, lhs, ble.getLhs());
        }
        handleSubexpressions(assgns, lhs, ble.getRhs());
    } else {
        createRelation(assgns, lhs, rhs);
    }
}
Also used : BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) AssignmentExpression(org.eclipse.n4js.n4JS.AssignmentExpression) BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) AssignmentExpression(org.eclipse.n4js.n4JS.AssignmentExpression) Expression(org.eclipse.n4js.n4JS.Expression) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression)

Example 3 with BinaryLogicalExpression

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

the class FlowAssertionFactory method addBooleanExpressions.

private static void addBooleanExpressions(EObject topContainer, ArrayList<BooleanExpression> bExprs, EObject condition) {
    if (topContainer == condition) {
        return;
    }
    BooleanExpression nextValue = null;
    if (condition instanceof UnaryExpression) {
        UnaryExpression ue = (UnaryExpression) condition;
        if (ue.getOp() == UnaryOperator.NOT) {
            nextValue = BooleanExpression.not;
        }
    } else if (condition instanceof BinaryLogicalExpression) {
        BinaryLogicalExpression ble = (BinaryLogicalExpression) condition;
        switch(ble.getOp()) {
            case AND:
                nextValue = BooleanExpression.and;
                break;
            case OR:
                nextValue = BooleanExpression.or;
                break;
        }
    } else if (condition instanceof EqualityExpression) {
        EqualityExpression ee = (EqualityExpression) condition;
        switch(ee.getOp()) {
            case EQ:
            case SAME:
                nextValue = BooleanExpression.eq;
                break;
            case NEQ:
            case NSAME:
                nextValue = BooleanExpression.neq;
                break;
        }
    }
    if (nextValue != null) {
        bExprs.add(nextValue);
    }
    EObject parent = condition.eContainer();
    // tail recursion
    addBooleanExpressions(topContainer, bExprs, parent);
}
Also used : BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) EObject(org.eclipse.emf.ecore.EObject) UnaryExpression(org.eclipse.n4js.n4JS.UnaryExpression) EqualityExpression(org.eclipse.n4js.n4JS.EqualityExpression)

Example 4 with BinaryLogicalExpression

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

the class GuardFactory method createGuardForTruthy.

private static Guard createGuardForTruthy(EObject topContainer, Expression expr, boolean negateTree) {
    if (SymbolFactory.canCreate(expr)) {
        EObject parent = expr.eContainer();
        boolean isTruthy = false;
        isTruthy |= parent instanceof Statement;
        isTruthy |= parent instanceof ParenExpression;
        isTruthy |= parent instanceof ConditionalExpression;
        isTruthy |= parent instanceof BinaryLogicalExpression;
        isTruthy |= parent instanceof UnaryExpression && ((UnaryExpression) parent).getOp() == UnaryOperator.NOT;
        if (isTruthy) {
            GuardAssertion asserts = FlowAssertionFactory.getGuard(topContainer, expr, negateTree, false);
            Guard guard = createIsTruthyGuard(expr, asserts);
            return guard;
        }
    }
    return null;
}
Also used : BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) Statement(org.eclipse.n4js.n4JS.Statement) EObject(org.eclipse.emf.ecore.EObject) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) ParenExpression(org.eclipse.n4js.n4JS.ParenExpression) UnaryExpression(org.eclipse.n4js.n4JS.UnaryExpression)

Example 5 with BinaryLogicalExpression

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

the class BinaryLogicalExpressionFactory method isTopJumpCatcher.

/**
 * Short circuit evaluation of a {@link BinaryLogicalExpression} <i>ble</i> causes jumps that start at a lhs
 * condition and jump e.g. directly into the then or else block. However, sometimes this jump does not target such a
 * then or else block, for instance when <i>ble</i> is not used as a condition. In this case, the <i>ble</i> itself
 * or one of its parents will catch the short circuit jump.
 * <p>
 * This method returns true if the given {@link BinaryLogicalExpression} is catching a jump that is caused by a
 * short circuit evaluation. It returns false, if there is a parent element that will catch the jump instead.
 */
private static boolean isTopJumpCatcher(BinaryLogicalExpression ble) {
    EObject child, parent = ble;
    do {
        // skip parentheses
        child = parent;
        parent = parent.eContainer();
    } while (parent instanceof ParenExpression);
    if (parent instanceof BinaryLogicalExpression) {
        return false;
    }
    if (parent instanceof ConditionalExpression) {
        ConditionalExpression isParent = (ConditionalExpression) parent;
        return isParent.getExpression() != child;
    }
    if (parent instanceof Statement) {
        if (parent instanceof IfStatement) {
            IfStatement isParent = (IfStatement) parent;
            return isParent.getExpression() != child;
        }
        if (parent instanceof ForStatement) {
            ForStatement isParent = (ForStatement) parent;
            return isParent.getExpression() != child || !isParent.isForPlain();
        }
        if (parent instanceof WhileStatement) {
            WhileStatement isParent = (WhileStatement) parent;
            return isParent.getExpression() != child;
        }
        if (parent instanceof DoStatement) {
            DoStatement isParent = (DoStatement) parent;
            return isParent.getExpression() != child;
        }
    }
    return true;
}
Also used : IfStatement(org.eclipse.n4js.n4JS.IfStatement) BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) DoStatement(org.eclipse.n4js.n4JS.DoStatement) ForStatement(org.eclipse.n4js.n4JS.ForStatement) IfStatement(org.eclipse.n4js.n4JS.IfStatement) Statement(org.eclipse.n4js.n4JS.Statement) WhileStatement(org.eclipse.n4js.n4JS.WhileStatement) DoStatement(org.eclipse.n4js.n4JS.DoStatement) EObject(org.eclipse.emf.ecore.EObject) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) ParenExpression(org.eclipse.n4js.n4JS.ParenExpression) WhileStatement(org.eclipse.n4js.n4JS.WhileStatement) ForStatement(org.eclipse.n4js.n4JS.ForStatement)

Aggregations

BinaryLogicalExpression (org.eclipse.n4js.n4JS.BinaryLogicalExpression)7 ConditionalExpression (org.eclipse.n4js.n4JS.ConditionalExpression)6 EObject (org.eclipse.emf.ecore.EObject)3 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)3 DoStatement (org.eclipse.n4js.n4JS.DoStatement)3 EqualityExpression (org.eclipse.n4js.n4JS.EqualityExpression)3 ForStatement (org.eclipse.n4js.n4JS.ForStatement)3 IfStatement (org.eclipse.n4js.n4JS.IfStatement)3 ParenExpression (org.eclipse.n4js.n4JS.ParenExpression)3 UnaryExpression (org.eclipse.n4js.n4JS.UnaryExpression)3 WhileStatement (org.eclipse.n4js.n4JS.WhileStatement)3 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)2 ArrayLiteral (org.eclipse.n4js.n4JS.ArrayLiteral)2 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)2 BinaryBitwiseExpression (org.eclipse.n4js.n4JS.BinaryBitwiseExpression)2 CastExpression (org.eclipse.n4js.n4JS.CastExpression)2 CommaExpression (org.eclipse.n4js.n4JS.CommaExpression)2 Expression (org.eclipse.n4js.n4JS.Expression)2 Statement (org.eclipse.n4js.n4JS.Statement)2 EPackage (org.eclipse.emf.ecore.EPackage)1