• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_BINARYEXPRESSION
9 #define SKSL_BINARYEXPRESSION
10 
11 #include "src/sksl/SkSLCompiler.h"
12 #include "src/sksl/SkSLIRGenerator.h"
13 #include "src/sksl/SkSLLexer.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 #include "src/sksl/ir/SkSLExpression.h"
16 
17 namespace SkSL {
18 
19 /**
20  * A binary operation.
21  */
22 struct BinaryExpression : public Expression {
BinaryExpressionBinaryExpression23     BinaryExpression(int offset, std::unique_ptr<Expression> left, Token::Kind op,
24                      std::unique_ptr<Expression> right, const Type& type)
25     : INHERITED(offset, kBinary_Kind, type)
26     , fLeft(std::move(left))
27     , fOperator(op)
28     , fRight(std::move(right)) {}
29 
constantPropagateBinaryExpression30     std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
31                                                   const DefinitionMap& definitions) override {
32         return irGenerator.constantFold(*fLeft,
33                                         fOperator,
34                                         *fRight);
35     }
36 
hasSideEffectsBinaryExpression37     bool hasSideEffects() const override {
38         return Compiler::IsAssignment(fOperator) || fLeft->hasSideEffects() ||
39                fRight->hasSideEffects();
40     }
41 
cloneBinaryExpression42     std::unique_ptr<Expression> clone() const override {
43         return std::unique_ptr<Expression>(new BinaryExpression(fOffset, fLeft->clone(), fOperator,
44                                                                 fRight->clone(), fType));
45     }
46 
descriptionBinaryExpression47     String description() const override {
48         return "(" + fLeft->description() + " " + Compiler::OperatorName(fOperator) + " " +
49                fRight->description() + ")";
50     }
51 
52     std::unique_ptr<Expression> fLeft;
53     const Token::Kind fOperator;
54     std::unique_ptr<Expression> fRight;
55 
56     typedef Expression INHERITED;
57 };
58 
59 } // namespace
60 
61 #endif
62