• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 namespace SkSL {
14 
15 class IRGenerator;
16 class Variable;
17 
18 enum class VariableRefKind : int8_t {
19     kRead,
20     kWrite,
21     kReadWrite,
22     // taking the address of a variable - we consider this a read & write but don't complain if
23     // the variable was not previously assigned
24     kPointer
25 };
26 
27 /**
28  * A reference to a variable, through which it can be read or written. In the statement:
29  *
30  * x = x + 1;
31  *
32  * there is only one Variable 'x', but two VariableReferences to it.
33  */
34 class VariableReference final : public Expression {
35 public:
36     using RefKind = VariableRefKind;
37 
38     static constexpr Kind kExpressionKind = Kind::kVariableReference;
39 
40     VariableReference(int offset, const Variable* variable, RefKind refKind);
41 
42     // Creates a VariableReference. There isn't much in the way of error-checking or optimization
43     // opportunities here.
44     static std::unique_ptr<Expression> Make(int offset,
45                                             const Variable* variable,
46                                             RefKind refKind = RefKind::kRead) {
47         SkASSERT(variable);
48         return std::make_unique<VariableReference>(offset, variable, refKind);
49     }
50 
51     VariableReference(const VariableReference&) = delete;
52     VariableReference& operator=(const VariableReference&) = delete;
53 
variable()54     const Variable* variable() const {
55         return fVariable;
56     }
57 
refKind()58     RefKind refKind() const {
59         return fRefKind;
60     }
61 
62     void setRefKind(RefKind refKind);
63     void setVariable(const Variable* variable);
64 
65     bool hasProperty(Property property) const override;
66 
67     bool isConstantOrUniform() const override;
68 
clone()69     std::unique_ptr<Expression> clone() const override {
70         return std::make_unique<VariableReference>(fOffset, this->variable(), this->refKind());
71     }
72 
73     String description() const override;
74 
75 private:
76     const Variable* fVariable;
77     VariableRefKind fRefKind;
78 
79     using INHERITED = Expression;
80 };
81 
82 }  // namespace SkSL
83 
84 #endif
85