ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_EXPRESSION |
| 9 | #define SKSL_EXPRESSION |
| 10 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | #include "SkSLType.h" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 12 | #include "SkSLVariable.h" |
| 13 | |
| 14 | #include <unordered_map> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | |
| 16 | namespace SkSL { |
| 17 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 18 | struct Expression; |
| 19 | class IRGenerator; |
| 20 | |
| 21 | typedef std::unordered_map<const Variable*, std::unique_ptr<Expression>*> DefinitionMap; |
| 22 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 23 | /** |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 24 | * Abstract supertype of all expressions. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | */ |
| 26 | struct Expression : public IRNode { |
| 27 | enum Kind { |
| 28 | kBinary_Kind, |
| 29 | kBoolLiteral_Kind, |
| 30 | kConstructor_Kind, |
| 31 | kIntLiteral_Kind, |
| 32 | kFieldAccess_Kind, |
| 33 | kFloatLiteral_Kind, |
| 34 | kFunctionReference_Kind, |
| 35 | kFunctionCall_Kind, |
| 36 | kIndex_Kind, |
| 37 | kPrefix_Kind, |
| 38 | kPostfix_Kind, |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 39 | kSetting_Kind, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 40 | kSwizzle_Kind, |
| 41 | kVariableReference_Kind, |
| 42 | kTernary_Kind, |
| 43 | kTypeReference_Kind, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 44 | kDefined_Kind |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 47 | Expression(Position position, Kind kind, const Type& type) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | : INHERITED(position) |
| 49 | , fKind(kind) |
| 50 | , fType(std::move(type)) {} |
| 51 | |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Returns true if this expression is constant. compareConstant must be implemented for all |
| 54 | * constants! |
| 55 | */ |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | virtual bool isConstant() const { |
| 57 | return false; |
| 58 | } |
| 59 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 60 | /** |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 61 | * Compares this constant expression against another constant expression of the same type. It is |
| 62 | * an error to call this on non-constant expressions, or if the types of the expressions do not |
| 63 | * match. |
| 64 | */ |
| 65 | virtual bool compareConstant(const Context& context, const Expression& other) const { |
| 66 | ABORT("cannot call compareConstant on this type"); |
| 67 | } |
| 68 | |
| 69 | /** |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 70 | * Returns true if evaluating the expression potentially has side effects. Expressions may never |
| 71 | * return false if they actually have side effects, but it is legal (though suboptimal) to |
| 72 | * return true if there are not actually any side effects. |
| 73 | */ |
| 74 | virtual bool hasSideEffects() const = 0; |
| 75 | |
| 76 | /** |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 77 | * Given a map of known constant variable values, substitute them in for references to those |
| 78 | * variables occurring in this expression and its subexpressions. Similar simplifications, such |
| 79 | * as folding a constant binary expression down to a single value, may also be performed. |
| 80 | * Returns a new expression which replaces this expression, or null if no replacements were |
| 81 | * made. If a new expression is returned, this expression is no longer valid. |
| 82 | */ |
| 83 | virtual std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, |
| 84 | const DefinitionMap& definitions) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | const Kind fKind; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 89 | const Type& fType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 90 | |
| 91 | typedef IRNode INHERITED; |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | #endif |