Esempio n. 1
0
        public override void Visit(LogicalAndExpression node)
        {
            var implementation = GetCloneOf(node.Implementation);

            unfinishedClone = new LogicalAndExpression(implementation.Left, implementation.Middle, implementation);
            base.Visit(node);
        }
        // Currently disabled, to avoid problems with expressions
        // like hello " + (1 * "World".
        //
        // public Expression visit(BinaryOperatorExpression boe) {
        //   Token op = boe.operator;
        //
        //   if (boe.leftExpression instanceof NumberLiteral
        //       && boe.rightExpression instanceof NumberLiteral) {
        //     double leftValue = ((NumberLiteral) boe.leftExpression).value;
        //     double rightValue = ((NumberLiteral) boe.rightExpression).value;
        //
        //     if ((op == Token.OPERATOR_PLUS && leftValue == 0.0)
        //         || (op == Token.OPERATOR_MULTIPLY && leftValue == 1.0)) {
        //       return boe.rightExpression;
        //     }
        //
        //     if((op == Token.OPERATOR_PLUS && rightValue == 0.0)
        //         || (op == Token.OPERATOR_MULTIPLY && rightValue == 1.0)
        //         || (op == Token.OPERATOR_DIVIDE && rightValue == 1.0))
        //       return boe.leftExpression;
        //   }
        //
        //   return boe;
        // }

        public override Expression visit(LogicalAndExpression expression)
        {
            if (expression.leftExpression is BooleanLiteral)
            {
                if (((BooleanLiteral)expression.leftExpression).value == false)
                {
                    return(expression.leftExpression);
                }
                else if (((BooleanLiteral)expression.leftExpression).value == true)
                {
                    return(expression.rightExpression);
                }
            }

            if (expression.rightExpression is BooleanLiteral)
            {
                if (((BooleanLiteral)expression.rightExpression).value == false)
                {
                    return(expression.rightExpression);
                }
                else if (((BooleanLiteral)expression.rightExpression).value == true)
                {
                    return(expression.leftExpression);
                }
            }

            return(expression);
        }
Esempio n. 3
0
 public void Visit(LogicalAndExpression node)
 {
     for (int i = 0, len = node.Arity; i < len; ++i)
     {
         var oprand = node.GetOperand(i);
         VisitChild(2, _verdictColumn && IsVerdictPassthroughWhere(oprand), false, oprand);
     }
 }
Esempio n. 4
0
        public void LogicalAndExpression()
        {
            var e = new LogicalAndExpression(new Literal("13"), new Literal("37"));

            Assert.IsFalse(e.IsTrivial);
            Assert.AreEqual("(13&&37)", e.ToString());
            Assert.AreEqual("LogicalAnd", e.Name);
        }
Esempio n. 5
0
        // [22] LogicalANDExpression = InclusiveORExpression {'&&' InclusiveORExpression}.
        private bool IsLogicalAndExpression(out LogicalAndExpression logicalAndExpression)
        {
            logicalAndExpression = new LogicalAndExpression();
            if (!IsInclusiveOrExpression(out var inclusiveOrExpression))
            {
                return(false);
            }
            while (CheckToken(SyntaxKind.AmpersandAmpersandToken))
            {
                if (!IsLogicalOrExpression(out var logicalOrExpression))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 6
0
        internal Expression ParseImplicitLogicalAnd(Expression lhs) // E' -> E E'
        {
            Expression rhs = this.ParseEx();

            if (rhs != null)
            {
                Expression res = new LogicalAndExpression()
                {
                    Lhs = lhs, Rhs = rhs
                };
                return(this.ParseExPrime(res));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 7
0
 internal Expression ParseLogicalAnd(Expression lhs) // E' -> && E E'
 {
     if (this.input.Read() is LogicalAndToken)
     {
         Expression rhs = this.ParseEx();
         if (rhs != null)
         {
             Expression res = new LogicalAndExpression()
             {
                 Lhs = lhs, Rhs = rhs
             };
             return(this.ParseExPrime(res));
         }
         else
         {
             throw new ArgumentException("Failed to parsed LogicalAnd");
         }
     }
     else
     {
         this.input.Unread();
         return(null);
     }
 }
        /// <summary>
        /// Reads a logical expression.
        /// </summary>
        /// <param name="expressionProxy">Proxy object for the expression being created.</param>
        /// <param name="leftHandSide">The expression on the left hand side of the operator.</param>
        /// <param name="previousPrecedence">The precedence of the expression just before this one.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private LogicalExpression GetLogicalExpression(
            CodeUnitProxy expressionProxy, Expression leftHandSide, ExpressionPrecedence previousPrecedence, bool unsafeCode)
        {
            Param.AssertNotNull(expressionProxy, "expressionProxy");
            Param.AssertNotNull(leftHandSide, "leftHandSide");
            Param.Ignore(previousPrecedence);
            Param.Ignore(unsafeCode);

            LogicalExpression expression = null;

            // Read the details of the expression.
            OperatorSymbolToken operatorToken = this.PeekOperatorSymbolToken();
            CsLanguageService.Debug.Assert(operatorToken.Category == OperatorCategory.Logical, "Expected a logical operator");

            // Check the precedence of the operators to make sure we can gather this statement now.
            ExpressionPrecedence precedence = GetOperatorPrecedence(operatorToken.SymbolType);
            if (CheckPrecedence(previousPrecedence, precedence))
            {
                // Create the operator toke again and save it.
                operatorToken = this.GetOperatorSymbolToken(expressionProxy);

                // Get the expression on the right-hand side of the operator.
                Expression rightHandSide = this.GetOperatorRightHandExpression(expressionProxy, precedence, unsafeCode);

                // Get the expression operator type.
                switch (operatorToken.SymbolType)
                {
                    case OperatorType.LogicalAnd:
                        expression = new LogicalAndExpression(expressionProxy, leftHandSide, rightHandSide);
                        break;

                    case OperatorType.LogicalOr:
                        expression = new LogicalOrExpression(expressionProxy, leftHandSide, rightHandSide);
                        break;

                    case OperatorType.LogicalXor:
                        expression = new LogicalXorExpression(expressionProxy, leftHandSide, rightHandSide);
                        break;

                    default:
                        CsLanguageService.Debug.Fail("Unexpected operator type");
                        throw new InvalidOperationException();
                }
            }

            return expression;
        }
Esempio n. 9
0
 public override void Visit(LogicalAndExpression node)
 {
     PushLocation(node);
     VisitNode(node.Implementation);
     PopLocation();
 }
Esempio n. 10
0
 public virtual Expression visit(LogicalAndExpression expression)
 {
     return(expression);
 }
Esempio n. 11
0
 public override void Visit(LogicalAndExpression node)
 {
     Visit((BinaryExpression)node);
 }
Esempio n. 12
0
 public virtual Expression visit(LogicalAndExpression expression)
 {
     return(visitBinaryExpression(expression));
 }
Esempio n. 13
0
        /// <summary>
        /// The following are rules for C expressions.
        /// </summary>
        public static void SetExpressionRules()
        {
            // expression
            //   : assignment-expression [ ',' assignment-expression ]*
            Expression.Is(
                AssignmentExpression
                .OneOrMore(Comma)
                .Then(exprs => {
                if (exprs.Count == 1)
                {
                    return(exprs[0]);
                }
                return(AssignmentList.Create(exprs));
            })
                );

            // primary-expression
            //   : identifier          # Cannot be a typedef name.
            //   | constant
            //   | string-literal
            //   | '(' expression ')'
            PrimaryExpression.Is(
                Either(Variable)
                .Or(Constant)
                .Or(StringLiteral)
                .Or(
                    (LeftParen).Then(Expression).Then(RightParen)
                    )
                );

            // An identifier for a variable must not be defined as a typedef name.
            Variable.Is(
                Identifier.Check(result => !result.Environment.IsTypedefName(result.Result)).Then(AST.Variable.Create)
                );

            // constant
            //   : const-char
            //   : const-int
            //   : const-float
            Constant.Is(
                Either(ConstChar)
                .Or(ConstInt)
                .Or(ConstFloat)
                );


            // constant-expression
            //   : conditional-expression
            //
            // Note:
            // The size of an array should be a constant.
            // Note that the check is actually performed in semantic analysis.
            ConstantExpression.Is(
                ConditionalExpression
                );

            // conditional-expression
            //   : logical-or-expression [ '?' expression ':' conditional-expression ]?
            ConditionalExpression.Is(
                (LogicalOrExpression)
                .Then(
                    Given <Expr>()
                    .Then(Question)
                    .Then(Expression)
                    .Then(Colon)
                    .Then(ConditionalExpression)
                    .Then(AST.ConditionalExpression.Create)
                    .Optional()
                    )
                );

            // assignment-expression
            //   : unary-expression assignment-operator assignment-expression   # first-set = first-set(unary-expression)
            //   | conditional-expression                                       # first-set = first-set(cast-expression) = first-set(unary-expression) ++ { '(' }
            //
            // Note:
            //   Assignment operators are:
            //     '=', '*=', '/=', '%=', '+=', '-=', '<<=', '>>=', '&=', '^=', '|='
            AssignmentExpression.Is(
                Either(
                    AssignmentOperator(
                        UnaryExpression,
                        AssignmentExpression,
                        BinaryOperatorBuilder.Create(Assign, Assignment.Create),
                        BinaryOperatorBuilder.Create(MultAssign, AST.MultAssign.Create),
                        BinaryOperatorBuilder.Create(DivAssign, AST.DivAssign.Create),
                        BinaryOperatorBuilder.Create(ModAssign, AST.ModAssign.Create),
                        BinaryOperatorBuilder.Create(AddAssign, AST.AddAssign.Create),
                        BinaryOperatorBuilder.Create(SubAssign, AST.SubAssign.Create),
                        BinaryOperatorBuilder.Create(LeftShiftAssign, LShiftAssign.Create),
                        BinaryOperatorBuilder.Create(RightShiftAssign, RShiftAssign.Create),
                        BinaryOperatorBuilder.Create(BitwiseAndAssign, AST.BitwiseAndAssign.Create),
                        BinaryOperatorBuilder.Create(XorAssign, AST.XorAssign.Create),
                        BinaryOperatorBuilder.Create(BitwiseOrAssign, AST.BitwiseOrAssign.Create)
                        )
                    ).Or(
                    ConditionalExpression
                    )
                );

            // postfix-expression
            //   : primary-expression [
            //         '[' expression ']'                      # Get element from array
            //       | '(' [argument-expression-list]? ')'     # Function call
            //       | '.' identifier                          # Get member from struct/union
            //       | '->' identifier                         # Get member from struct/union
            //       | '++'                                    # Increment
            //       | '--'                                    # Decrement
            //     ]*
            PostfixExpression.Is(
                PrimaryExpression
                .Then(
                    Either(
                        Given <Expr>()
                        .Then(LeftBracket)
                        .Then(Expression)
                        .Then(RightBracket)
                        .Then((array, index) => Dereference.Create(AST.Add.Create(array, index)))
                        ).Or(
                        Given <Expr>()
                        .Then(LeftParen)
                        .Then(ArgumentExpressionList.Optional(ImmutableList <Expr> .Empty))
                        .Then(RightParen)
                        .Then(FuncCall.Create)
                        ).Or(
                        Given <Expr>()
                        .Then(Period)
                        .Then(Identifier)
                        .Then(Attribute.Create)
                        ).Or(
                        Given <Expr>()
                        .Then(RightArrow)
                        .Then(Identifier)
                        .Then((expr, member) => Attribute.Create(Dereference.Create(expr), member))
                        ).Or(
                        Given <Expr>()
                        .Then(Increment)
                        .Then(PostIncrement.Create)
                        ).Or(
                        Given <Expr>()
                        .Then(Decrement)
                        .Then(PostDecrement.Create)
                        ).ZeroOrMore()
                    )
                );

            // argument-expression-list
            //   : assignment-expression [ ',' assignment-expression ]*
            ArgumentExpressionList.Is(
                AssignmentExpression.OneOrMore(Comma)
                );

            // unary-expression
            //   : postfix-expression               # first-set = { id, const, string }
            //   | '++' unary-expression            # first-set = { '++' }
            //   | '--' unary-expression            # first-set = { '--' }
            //   | unary-operator cast-expression   # first-set = { '&', '*', '+', '-', '~', '!' }
            //   | 'sizeof' unary-expression        # first-set = { 'sizeof' }
            //   | 'sizeof' '(' Type-name ')'       # first-set = { 'sizeof' }
            //
            // Notes:
            // 1. unary-operator can be '&', '*', '+', '-', '~', '!'.
            // 2. The last two rules are ambiguous: you can't figure out whether the x in sizeof(x) is a typedef of a variable.
            //    I have a parser hack for this: add a parser environment to track all the typedefs.
            // 3. first_set = first_set(postfix-expression) + { '++', '--', '&', '*', '+', '-', '~', '!', 'sizeof' }
            //              = first_set(primary-expression) + { '++', '--', '&', '*', '+', '-', '~', '!', 'sizeof' }
            //              = { id, const, string, '++', '--', '&', '*', '+', '-', '~', '!', 'sizeof' }
            UnaryExpression.Is(
                Either(
                    PostfixExpression
                    ).Or(
                    (Increment).Then(UnaryExpression).Then(PreIncrement.Create)
                    ).Or(
                    (Decrement).Then(UnaryExpression).Then(PreDecrement.Create)
                    ).Or(
                    (BitwiseAnd).Then(CastExpression).Then(Reference.Create)
                    ).Or(
                    (Mult).Then(CastExpression).Then(Dereference.Create)
                    ).Or(
                    (Add).Then(CastExpression).Then(Positive.Create)
                    ).Or(
                    (Sub).Then(CastExpression).Then(Negative.Create)
                    ).Or(
                    (BitwiseNot).Then(CastExpression).Then(AST.BitwiseNot.Create)
                    ).Or(
                    (LogicalNot).Then(CastExpression).Then(AST.LogicalNot.Create)
                    ).Or(
                    (SizeOf).Then(UnaryExpression).Then(SizeofExpr.Create)
                    ).Or(
                    (SizeOf).Then(LeftParen).Then(TypeName).Then(RightParen).Then(SizeofType.Create)
                    )
                );

            // cast-expression
            //   : unary-expression                     # first-set = { id, const, string, '++', '--', '&', '*', '+', '-', '~', '!', 'sizeof' }
            //   | '(' type_name ')' cast-expression    # first-set = '('
            CastExpression.Is(
                Either(
                    UnaryExpression
                    ).Or(
                    (LeftParen).Then(TypeName).Then(RightParen).Then(CastExpression)
                    .Then(TypeCast.Create)
                    )
                );

            // multiplicative-expression
            //   : cast-expression [ [ '*' | '/' | '%' ] cast-expression ]*
            MultiplicativeExpression.Is(
                BinaryOperator(
                    CastExpression,
                    BinaryOperatorBuilder.Create(Mult, Multiply.Create),
                    BinaryOperatorBuilder.Create(Div, Divide.Create),
                    BinaryOperatorBuilder.Create(Mod, Modulo.Create)
                    )
                );

            // additive-expression
            //   : multiplicative-expression [ [ '+' | '-' ] multiplicative-expression ]*
            AdditiveExpression.Is(
                BinaryOperator(
                    MultiplicativeExpression,
                    BinaryOperatorBuilder.Create(Add, AST.Add.Create),
                    BinaryOperatorBuilder.Create(Sub, AST.Sub.Create)
                    )
                );

            // shift-expression
            //   : additive-expression [ [ '<<' | '>>' ] additive-expression ]*
            ShiftExpression.Is(
                BinaryOperator(
                    AdditiveExpression,
                    BinaryOperatorBuilder.Create(LeftShift, LShift.Create),
                    BinaryOperatorBuilder.Create(RightShift, RShift.Create)
                    )
                );

            // relational-expression
            //   : shift-expression [ [ '<' | '>' | '<=' | '>=' ] shift-expression ]*
            RelationalExpression.Is(
                BinaryOperator(
                    ShiftExpression,
                    BinaryOperatorBuilder.Create(Less, AST.Less.Create),
                    BinaryOperatorBuilder.Create(Greater, AST.Greater.Create),
                    BinaryOperatorBuilder.Create(LessEqual, LEqual.Create),
                    BinaryOperatorBuilder.Create(GreaterEqual, GEqual.Create)
                    )
                );

            // equality-expression
            //   : relational-expression [ [ '==' | '!=' ] relational-expression ]*
            EqualityExpression.Is(
                BinaryOperator(
                    RelationalExpression,
                    BinaryOperatorBuilder.Create(Equal, AST.Equal.Create),
                    BinaryOperatorBuilder.Create(NotEqual, AST.NotEqual.Create)
                    )
                );

            // and-expression
            //   : equality-expression [ '&' equality-expression ]*
            AndExpression.Is(
                BinaryOperator(
                    EqualityExpression,
                    BinaryOperatorBuilder.Create(BitwiseAnd, AST.BitwiseAnd.Create)
                    )
                );

            // exclusive-or-expression
            //   : and-expression [ '^' and-expression ]*
            ExclusiveOrExpression.Is(
                BinaryOperator(
                    AndExpression,
                    BinaryOperatorBuilder.Create(Xor, AST.Xor.Create)
                    )
                );

            // inclusive-or-expression
            //   : exclusive-or-expression [ '|' exclusive-or-expression ]*
            InclusiveOrExpression.Is(
                BinaryOperator(
                    ExclusiveOrExpression,
                    BinaryOperatorBuilder.Create(BitwiseOr, AST.BitwiseOr.Create)
                    )
                );

            // logical-and-expression
            //   : inclusive-or-expression [ '&&' inclusive-or-expression ]*
            LogicalAndExpression.Is(
                BinaryOperator(
                    InclusiveOrExpression,
                    BinaryOperatorBuilder.Create(LogicalAnd, AST.LogicalAnd.Create)
                    )
                );

            // logical-or-expression
            //   :logical-and-expression [ '||' logical-and-expression ]*
            LogicalOrExpression.Is(
                BinaryOperator(
                    LogicalAndExpression,
                    BinaryOperatorBuilder.Create(LogicalOr, AST.LogicalOr.Create)
                    )
                );
        }
Esempio n. 14
0
 public override void Visit(LogicalAndExpression node)
 {
     VisitNode(node.Implementation);
 }
Esempio n. 15
0
 public virtual void Visit(LogicalAndExpression node)
 {
     Visit((PolyadicOperatorExpression)node);
 }
 BinaryExpressionSyntax(LogicalAndExpression) logicalAnd => CombineLogicalAndOperands(logicalAnd, model),
 CasePatternSwitchLabelSyntax {
     WhenClause : { } whenClause
Esempio n. 17
0
 public override void Visit(LogicalAndExpression node)
 {
     UpdateType(node, TypeCalculator.GetType(node));
 }