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 "SkSLType.h" 12 #include "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 kIntLiteral_Kind, 33 kFieldAccess_Kind, 34 kFloatLiteral_Kind, 35 kFunctionReference_Kind, 36 kFunctionCall_Kind, 37 kIndex_Kind, 38 kPrefix_Kind, 39 kPostfix_Kind, 40 kSetting_Kind, 41 kSwizzle_Kind, 42 kVariableReference_Kind, 43 kTernary_Kind, 44 kTypeReference_Kind, 45 kDefined_Kind 46 }; 47 ExpressionExpression48 Expression(int offset, Kind kind, const Type& type) 49 : INHERITED(offset) 50 , fKind(kind) 51 , fType(std::move(type)) {} 52 53 /** 54 * Returns true if this expression is constant. compareConstant must be implemented for all 55 * constants! 56 */ isConstantExpression57 virtual bool isConstant() const { 58 return false; 59 } 60 61 /** 62 * Compares this constant expression against another constant expression of the same type. It is 63 * an error to call this on non-constant expressions, or if the types of the expressions do not 64 * match. 65 */ compareConstantExpression66 virtual bool compareConstant(const Context& context, const Expression& other) const { 67 ABORT("cannot call compareConstant on this type"); 68 } 69 70 /** 71 * For an expression which evaluates to a constant int, returns the value. Otherwise calls 72 * ABORT. 73 */ getConstantIntExpression74 virtual int64_t getConstantInt() const { 75 ABORT("not a constant int"); 76 } 77 78 /** 79 * For an expression which evaluates to a constant float, returns the value. Otherwise calls 80 * ABORT. 81 */ getConstantFloatExpression82 virtual double getConstantFloat() const { 83 ABORT("not a constant float"); 84 } 85 86 /** 87 * Returns true if evaluating the expression potentially has side effects. Expressions may never 88 * return false if they actually have side effects, but it is legal (though suboptimal) to 89 * return true if there are not actually any side effects. 90 */ 91 virtual bool hasSideEffects() const = 0; 92 93 /** 94 * Given a map of known constant variable values, substitute them in for references to those 95 * variables occurring in this expression and its subexpressions. Similar simplifications, such 96 * as folding a constant binary expression down to a single value, may also be performed. 97 * Returns a new expression which replaces this expression, or null if no replacements were 98 * made. If a new expression is returned, this expression is no longer valid. 99 */ constantPropagateExpression100 virtual std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, 101 const DefinitionMap& definitions) { 102 return nullptr; 103 } 104 coercionCostExpression105 virtual int coercionCost(const Type& target) const { 106 return fType.coercionCost(target); 107 } 108 109 virtual std::unique_ptr<Expression> clone() const = 0; 110 111 const Kind fKind; 112 const Type& fType; 113 114 typedef IRNode INHERITED; 115 }; 116 117 } // namespace 118 119 #endif 120