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