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 "src/sksl/ir/SkSLExpression.h"
12
13namespace SkSL {
14
15class IRGenerator;
16
17/**
18 * A reference to a variable, through which it can be read or written. In the statement:
19 *
20 * x = x + 1;
21 *
22 * there is only one Variable 'x', but two VariableReferences to it.
23 */
24struct VariableReference : public Expression {
25 enum RefKind {
26 kRead_RefKind,
27 kWrite_RefKind,
28 kReadWrite_RefKind,
29 // taking the address of a variable - we consider this a read & write but don't complain if
30 // the variable was not previously assigned
31 kPointer_RefKind
32 };
33
34 VariableReference(int offset, const Variable& variable, RefKind refKind = kRead_RefKind);
35
36 ~VariableReference() override;
37
38 VariableReference(const VariableReference&) = delete;
39 VariableReference& operator=(const VariableReference&) = delete;
40
41 RefKind refKind() const {
42 return fRefKind;
43 }
44
45 void setRefKind(RefKind refKind);
46
47 bool hasProperty(Property property) const override {
48 switch (property) {
49 case Property::kSideEffects: return false;
50 case Property::kContainsRTAdjust: return fVariable.fName == "sk_RTAdjust";
51 default:
52 SkASSERT(false);
53 return false;
54 }
55 }
56
57 bool isConstant() const override {
58 return 0 != (fVariable.fModifiers.fFlags & Modifiers::kConst_Flag);
59 }
60
61 std::unique_ptr<Expression> clone() const override {
62 return std::unique_ptr<Expression>(new VariableReference(fOffset, fVariable, fRefKind));
63 }
64
65#ifdef SK_DEBUG
66 String description() const override {
67 return fVariable.fName;
68 }
69#endif
70
71 static std::unique_ptr<Expression> copy_constant(const IRGenerator& irGenerator,
72 const Expression* expr);
73
74 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
75 const DefinitionMap& definitions) override;
76
77 const Variable& fVariable;
78 RefKind fRefKind;
79
80private:
81 typedef Expression INHERITED;
82};
83
84} // namespace
85
86#endif
87