SkSLIntLiteral.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_INTLITERAL
  8. #define SKSL_INTLITERAL
  9. #include "src/sksl/SkSLContext.h"
  10. #include "src/sksl/ir/SkSLExpression.h"
  11. namespace SkSL {
  12. /**
  13. * A literal integer.
  14. */
  15. struct IntLiteral : public Expression {
  16. // FIXME: we will need to revisit this if/when we add full support for both signed and unsigned
  17. // 64-bit integers, but for right now an int64_t will hold every value we care about
  18. IntLiteral(const Context& context, int offset, int64_t value)
  19. : INHERITED(offset, kIntLiteral_Kind, *context.fInt_Type)
  20. , fValue(value) {}
  21. IntLiteral(int offset, int64_t value, const Type* type = nullptr)
  22. : INHERITED(offset, kIntLiteral_Kind, *type)
  23. , fValue(value) {}
  24. String description() const override {
  25. return to_string(fValue);
  26. }
  27. bool hasSideEffects() const override {
  28. return false;
  29. }
  30. bool isConstant() const override {
  31. return true;
  32. }
  33. bool compareConstant(const Context& context, const Expression& other) const override {
  34. IntLiteral& i = (IntLiteral&) other;
  35. return fValue == i.fValue;
  36. }
  37. int coercionCost(const Type& target) const override {
  38. if (target.isSigned() || target.isUnsigned() || target.isFloat()) {
  39. return 0;
  40. }
  41. return INHERITED::coercionCost(target);
  42. }
  43. int64_t getConstantInt() const override {
  44. return fValue;
  45. }
  46. std::unique_ptr<Expression> clone() const override {
  47. return std::unique_ptr<Expression>(new IntLiteral(fOffset, fValue, &fType));
  48. }
  49. const int64_t fValue;
  50. typedef Expression INHERITED;
  51. };
  52. } // namespace
  53. #endif