SkSLExpression.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_EXPRESSION
  8. #define SKSL_EXPRESSION
  9. #include "src/sksl/ir/SkSLType.h"
  10. #include "src/sksl/ir/SkSLVariable.h"
  11. #include <unordered_map>
  12. namespace SkSL {
  13. struct Expression;
  14. class IRGenerator;
  15. typedef std::unordered_map<const Variable*, std::unique_ptr<Expression>*> DefinitionMap;
  16. /**
  17. * Abstract supertype of all expressions.
  18. */
  19. struct Expression : public IRNode {
  20. enum Kind {
  21. kAppendStage_Kind,
  22. kBinary_Kind,
  23. kBoolLiteral_Kind,
  24. kConstructor_Kind,
  25. kExternalFunctionCall_Kind,
  26. kExternalValue_Kind,
  27. kIntLiteral_Kind,
  28. kFieldAccess_Kind,
  29. kFloatLiteral_Kind,
  30. kFunctionReference_Kind,
  31. kFunctionCall_Kind,
  32. kIndex_Kind,
  33. kNullLiteral_Kind,
  34. kPrefix_Kind,
  35. kPostfix_Kind,
  36. kSetting_Kind,
  37. kSwizzle_Kind,
  38. kVariableReference_Kind,
  39. kTernary_Kind,
  40. kTypeReference_Kind,
  41. kDefined_Kind
  42. };
  43. Expression(int offset, Kind kind, const Type& type)
  44. : INHERITED(offset)
  45. , fKind(kind)
  46. , fType(std::move(type)) {}
  47. /**
  48. * Returns true if this expression is constant. compareConstant must be implemented for all
  49. * constants!
  50. */
  51. virtual bool isConstant() const {
  52. return false;
  53. }
  54. /**
  55. * Compares this constant expression against another constant expression of the same type. It is
  56. * an error to call this on non-constant expressions, or if the types of the expressions do not
  57. * match.
  58. */
  59. virtual bool compareConstant(const Context& context, const Expression& other) const {
  60. ABORT("cannot call compareConstant on this type");
  61. }
  62. /**
  63. * For an expression which evaluates to a constant int, returns the value. Otherwise calls
  64. * ABORT.
  65. */
  66. virtual int64_t getConstantInt() const {
  67. ABORT("not a constant int");
  68. }
  69. /**
  70. * For an expression which evaluates to a constant float, returns the value. Otherwise calls
  71. * ABORT.
  72. */
  73. virtual double getConstantFloat() const {
  74. ABORT("not a constant float");
  75. }
  76. /**
  77. * Returns true if evaluating the expression potentially has side effects. Expressions may never
  78. * return false if they actually have side effects, but it is legal (though suboptimal) to
  79. * return true if there are not actually any side effects.
  80. */
  81. virtual bool hasSideEffects() const = 0;
  82. /**
  83. * Given a map of known constant variable values, substitute them in for references to those
  84. * variables occurring in this expression and its subexpressions. Similar simplifications, such
  85. * as folding a constant binary expression down to a single value, may also be performed.
  86. * Returns a new expression which replaces this expression, or null if no replacements were
  87. * made. If a new expression is returned, this expression is no longer valid.
  88. */
  89. virtual std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
  90. const DefinitionMap& definitions) {
  91. return nullptr;
  92. }
  93. virtual int coercionCost(const Type& target) const {
  94. return fType.coercionCost(target);
  95. }
  96. /**
  97. * For a literal vector expression, return the floating point value of the n'th vector
  98. * component. It is an error to call this method on an expression which is not a literal vector.
  99. */
  100. virtual SKSL_FLOAT getFVecComponent(int n) const {
  101. SkASSERT(false);
  102. return 0;
  103. }
  104. /**
  105. * For a literal vector expression, return the integer value of the n'th vector component. It is
  106. * an error to call this method on an expression which is not a literal vector.
  107. */
  108. virtual SKSL_INT getIVecComponent(int n) const {
  109. SkASSERT(false);
  110. return 0;
  111. }
  112. /**
  113. * For a literal matrix expression, return the floating point value of the component at
  114. * [col][row]. It is an error to call this method on an expression which is not a literal
  115. * matrix.
  116. */
  117. virtual SKSL_FLOAT getMatComponent(int col, int row) const {
  118. SkASSERT(false);
  119. return 0;
  120. }
  121. virtual std::unique_ptr<Expression> clone() const = 0;
  122. const Kind fKind;
  123. const Type& fType;
  124. typedef IRNode INHERITED;
  125. };
  126. } // namespace
  127. #endif