SkSLTernaryExpression.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_TERNARYEXPRESSION
  8. #define SKSL_TERNARYEXPRESSION
  9. #include "src/sksl/SkSLPosition.h"
  10. #include "src/sksl/ir/SkSLExpression.h"
  11. namespace SkSL {
  12. /**
  13. * A ternary expression (test ? ifTrue : ifFalse).
  14. */
  15. struct TernaryExpression : public Expression {
  16. TernaryExpression(int offset, std::unique_ptr<Expression> test,
  17. std::unique_ptr<Expression> ifTrue, std::unique_ptr<Expression> ifFalse)
  18. : INHERITED(offset, kTernary_Kind, ifTrue->fType)
  19. , fTest(std::move(test))
  20. , fIfTrue(std::move(ifTrue))
  21. , fIfFalse(std::move(ifFalse)) {
  22. SkASSERT(fIfTrue->fType == fIfFalse->fType);
  23. }
  24. bool hasSideEffects() const override {
  25. return fTest->hasSideEffects() || fIfTrue->hasSideEffects() || fIfFalse->hasSideEffects();
  26. }
  27. std::unique_ptr<Expression> clone() const override {
  28. return std::unique_ptr<Expression>(new TernaryExpression(fOffset, fTest->clone(),
  29. fIfTrue->clone(),
  30. fIfFalse->clone()));
  31. }
  32. String description() const override {
  33. return "(" + fTest->description() + " ? " + fIfTrue->description() + " : " +
  34. fIfFalse->description() + ")";
  35. }
  36. std::unique_ptr<Expression> fTest;
  37. std::unique_ptr<Expression> fIfTrue;
  38. std::unique_ptr<Expression> fIfFalse;
  39. typedef Expression INHERITED;
  40. };
  41. } // namespace
  42. #endif