SkSLBinaryExpression.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef SKSL_BINARYEXPRESSION
  8. #define SKSL_BINARYEXPRESSION
  9. #include "src/sksl/SkSLCompiler.h"
  10. #include "src/sksl/SkSLIRGenerator.h"
  11. #include "src/sksl/SkSLLexer.h"
  12. #include "src/sksl/ir/SkSLExpression.h"
  13. #include "src/sksl/ir/SkSLExpression.h"
  14. namespace SkSL {
  15. /**
  16. * A binary operation.
  17. */
  18. struct BinaryExpression : public Expression {
  19. BinaryExpression(int offset, std::unique_ptr<Expression> left, Token::Kind op,
  20. std::unique_ptr<Expression> right, const Type& type)
  21. : INHERITED(offset, kBinary_Kind, type)
  22. , fLeft(std::move(left))
  23. , fOperator(op)
  24. , fRight(std::move(right)) {}
  25. std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
  26. const DefinitionMap& definitions) override {
  27. return irGenerator.constantFold(*fLeft,
  28. fOperator,
  29. *fRight);
  30. }
  31. bool hasSideEffects() const override {
  32. return Compiler::IsAssignment(fOperator) || fLeft->hasSideEffects() ||
  33. fRight->hasSideEffects();
  34. }
  35. std::unique_ptr<Expression> clone() const override {
  36. return std::unique_ptr<Expression>(new BinaryExpression(fOffset, fLeft->clone(), fOperator,
  37. fRight->clone(), fType));
  38. }
  39. String description() const override {
  40. return "(" + fLeft->description() + " " + Compiler::OperatorName(fOperator) + " " +
  41. fRight->description() + ")";
  42. }
  43. std::unique_ptr<Expression> fLeft;
  44. const Token::Kind fOperator;
  45. std::unique_ptr<Expression> fRight;
  46. typedef Expression INHERITED;
  47. };
  48. } // namespace
  49. #endif