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 | */ |
| 7 | |
| 8 | #ifndef SKSL_VARIABLEREFERENCE |
| 9 | #define SKSL_VARIABLEREFERENCE |
| 10 | |
| 11 | #include "SkSLExpression.h" |
| 12 | |
| 13 | namespace SkSL { |
| 14 | |
| 15 | /** |
| 16 | * A reference to a variable, through which it can be read or written. In the statement: |
| 17 | * |
| 18 | * x = x + 1; |
| 19 | * |
| 20 | * there is only one Variable 'x', but two VariableReferences to it. |
| 21 | */ |
| 22 | struct VariableReference : public Expression { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 23 | enum RefKind { |
| 24 | kRead_RefKind, |
| 25 | kWrite_RefKind, |
| 26 | kReadWrite_RefKind |
| 27 | }; |
| 28 | |
| 29 | VariableReference(Position position, const Variable& variable, RefKind refKind = kRead_RefKind) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 30 | : INHERITED(position, kVariableReference_Kind, variable.fType) |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 31 | , fVariable(variable) |
| 32 | , fRefKind(refKind) { |
| 33 | if (refKind != kRead_RefKind) { |
| 34 | fVariable.fWriteCount++; |
| 35 | } |
| 36 | if (refKind != kWrite_RefKind) { |
| 37 | fVariable.fReadCount++; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | virtual ~VariableReference() override { |
| 42 | if (fRefKind != kWrite_RefKind) { |
| 43 | fVariable.fReadCount--; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | RefKind refKind() { |
| 48 | return fRefKind; |
| 49 | } |
| 50 | |
| 51 | void setRefKind(RefKind refKind) { |
| 52 | if (fRefKind != kRead_RefKind) { |
| 53 | fVariable.fWriteCount--; |
| 54 | } |
| 55 | if (fRefKind != kWrite_RefKind) { |
| 56 | fVariable.fReadCount--; |
| 57 | } |
| 58 | if (refKind != kRead_RefKind) { |
| 59 | fVariable.fWriteCount++; |
| 60 | } |
| 61 | if (refKind != kWrite_RefKind) { |
| 62 | fVariable.fReadCount++; |
| 63 | } |
| 64 | fRefKind = refKind; |
| 65 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 67 | SkString description() const override { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 68 | return fVariable.fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | virtual std::unique_ptr<Expression> constantPropagate( |
| 72 | const IRGenerator& irGenerator, |
| 73 | const DefinitionMap& definitions) override { |
| 74 | auto exprIter = definitions.find(&fVariable); |
| 75 | if (exprIter != definitions.end() && exprIter->second) { |
| 76 | const Expression* expr = exprIter->second->get(); |
| 77 | switch (expr->fKind) { |
| 78 | case Expression::kIntLiteral_Kind: |
| 79 | return std::unique_ptr<Expression>(new IntLiteral( |
| 80 | irGenerator.fContext, |
| 81 | Position(), |
| 82 | ((IntLiteral*) expr)->fValue)); |
| 83 | case Expression::kFloatLiteral_Kind: |
| 84 | return std::unique_ptr<Expression>(new FloatLiteral( |
| 85 | irGenerator.fContext, |
| 86 | Position(), |
| 87 | ((FloatLiteral*) expr)->fValue)); |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 95 | const Variable& fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 96 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 97 | private: |
| 98 | RefKind fRefKind; |
| 99 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 100 | typedef Expression INHERITED; |
| 101 | }; |
| 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | #endif |