SkSLFloatLiteral.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_FLOATLITERAL
  8. #define SKSL_FLOATLITERAL
  9. #include "src/sksl/SkSLContext.h"
  10. #include "src/sksl/ir/SkSLExpression.h"
  11. namespace SkSL {
  12. /**
  13. * A literal floating point number.
  14. */
  15. struct FloatLiteral : public Expression {
  16. FloatLiteral(const Context& context, int offset, double value)
  17. : INHERITED(offset, kFloatLiteral_Kind, *context.fFloatLiteral_Type)
  18. , fValue(value) {}
  19. FloatLiteral(int offset, double value, const Type* type)
  20. : INHERITED(offset, kFloatLiteral_Kind, *type)
  21. , fValue(value) {}
  22. String description() const override {
  23. return to_string(fValue);
  24. }
  25. bool hasSideEffects() const override {
  26. return false;
  27. }
  28. bool isConstant() const override {
  29. return true;
  30. }
  31. int coercionCost(const Type& target) const override {
  32. if (target.isFloat()) {
  33. return 0;
  34. }
  35. return INHERITED::coercionCost(target);
  36. }
  37. bool compareConstant(const Context& context, const Expression& other) const override {
  38. FloatLiteral& f = (FloatLiteral&) other;
  39. return fValue == f.fValue;
  40. }
  41. double getConstantFloat() const override {
  42. return fValue;
  43. }
  44. std::unique_ptr<Expression> clone() const override {
  45. return std::unique_ptr<Expression>(new FloatLiteral(fOffset, fValue, &fType));
  46. }
  47. const double fValue;
  48. typedef Expression INHERITED;
  49. };
  50. } // namespace
  51. #endif