| 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 { |
| 23 | 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 | |
| 30 | std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, |
| 31 | const DefinitionMap& definitions) override { |
| 32 | return irGenerator.constantFold(*fLeft, |
| 33 | fOperator, |
| 34 | *fRight); |
| 35 | } |
| 36 | |
| 37 | bool hasProperty(Property property) const override { |
| 38 | if (property == Property::kSideEffects && Compiler::IsAssignment(fOperator)) { |
| 39 | return true; |
| 40 | } |
| 41 | return fLeft->hasProperty(property) || fRight->hasProperty(property); |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<Expression> clone() const override { |
| 45 | return std::unique_ptr<Expression>(new BinaryExpression(fOffset, fLeft->clone(), fOperator, |
| 46 | fRight->clone(), fType)); |
| 47 | } |
| 48 | |
| 49 | #ifdef SK_DEBUG |
| 50 | String description() const override { |
| 51 | return "(" + fLeft->description() + " " + Compiler::OperatorName(fOperator) + " " + |
| 52 | fRight->description() + ")" ; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | std::unique_ptr<Expression> fLeft; |
| 57 | const Token::Kind fOperator; |
| 58 | std::unique_ptr<Expression> fRight; |
| 59 | |
| 60 | typedef Expression INHERITED; |
| 61 | }; |
| 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | #endif |
| 66 | |