blob: 75b7f3dcd417ae7a2329b37ce6db50e6b3ebd830 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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
13namespace 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 */
22struct VariableReference : public Expression {
Ethan Nicholas86a43402017-01-19 13:32:00 -050023 enum RefKind {
24 kRead_RefKind,
25 kWrite_RefKind,
26 kReadWrite_RefKind
27 };
28
29 VariableReference(Position position, const Variable& variable, RefKind refKind = kRead_RefKind)
ethannicholasd598f792016-07-25 10:08:54 -070030 : INHERITED(position, kVariableReference_Kind, variable.fType)
Ethan Nicholas86a43402017-01-19 13:32:00 -050031 , 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
Ethan Nicholas113628d2017-02-02 16:11:39 -050041 ~VariableReference() override {
Ethan Nicholas86a43402017-01-19 13:32:00 -050042 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 }
ethannicholasb3058bd2016-07-01 08:22:01 -070066
Ethan Nicholas113628d2017-02-02 16:11:39 -050067 bool hasSideEffects() const override {
68 return false;
69 }
70
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050071 SkString description() const override {
ethannicholasd598f792016-07-25 10:08:54 -070072 return fVariable.fName;
ethannicholasb3058bd2016-07-01 08:22:01 -070073 }
74
Ethan Nicholas113628d2017-02-02 16:11:39 -050075 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
76 const DefinitionMap& definitions) override {
77 if (fRefKind != kRead_RefKind) {
78 return nullptr;
79 }
Ethan Nicholas86a43402017-01-19 13:32:00 -050080 auto exprIter = definitions.find(&fVariable);
81 if (exprIter != definitions.end() && exprIter->second) {
82 const Expression* expr = exprIter->second->get();
83 switch (expr->fKind) {
84 case Expression::kIntLiteral_Kind:
85 return std::unique_ptr<Expression>(new IntLiteral(
86 irGenerator.fContext,
87 Position(),
88 ((IntLiteral*) expr)->fValue));
89 case Expression::kFloatLiteral_Kind:
90 return std::unique_ptr<Expression>(new FloatLiteral(
91 irGenerator.fContext,
92 Position(),
93 ((FloatLiteral*) expr)->fValue));
Ethan Nicholas113628d2017-02-02 16:11:39 -050094 case Expression::kBoolLiteral_Kind:
95 return std::unique_ptr<Expression>(new BoolLiteral(
96 irGenerator.fContext,
97 Position(),
98 ((BoolLiteral*) expr)->fValue));
Ethan Nicholas86a43402017-01-19 13:32:00 -050099 default:
100 break;
101 }
102 }
103 return nullptr;
104 }
105
ethannicholasd598f792016-07-25 10:08:54 -0700106 const Variable& fVariable;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500107 RefKind fRefKind;
108
Ethan Nicholas113628d2017-02-02 16:11:39 -0500109private:
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 typedef Expression INHERITED;
111};
112
113} // namespace
114
115#endif