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 "include/core/SkTypes.h"
12#include "src/sksl/SkSLPosition.h"
13#include "src/sksl/ir/SkSLExpression.h"
14#include "src/sksl/ir/SkSLIRNode.h"
15
16#include <cstdint>
17#include <memory>
18#include <string>
19
20namespace SkSL {
21
22class Variable;
23enum class OperatorPrecedence : uint8_t;
24
25enum class VariableRefKind : int8_t {
26 kRead,
27 kWrite,
28 kReadWrite,
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
32};
33
34/**
35 * A reference to a variable, through which it can be read or written. In the statement:
36 *
37 * x = x + 1;
38 *
39 * there is only one Variable 'x', but two VariableReferences to it.
40 */
41class VariableReference final : public Expression {
42public:
43 using RefKind = VariableRefKind;
44
45 inline static constexpr Kind kIRNodeKind = Kind::kVariableReference;
46
47 VariableReference(Position pos, const Variable* variable, RefKind refKind);
48
49 // Creates a VariableReference. There isn't much in the way of error-checking or optimization
50 // opportunities here.
51 static std::unique_ptr<Expression> Make(Position pos,
52 const Variable* variable,
53 RefKind refKind = RefKind::kRead) {
54 SkASSERT(variable);
55 return std::make_unique<VariableReference>(args&: pos, args&: variable, args&: refKind);
56 }
57
58 VariableReference(const VariableReference&) = delete;
59 VariableReference& operator=(const VariableReference&) = delete;
60
61 const Variable* variable() const {
62 return fVariable;
63 }
64
65 RefKind refKind() const {
66 return fRefKind;
67 }
68
69 void setRefKind(RefKind refKind);
70 void setVariable(const Variable* variable);
71
72 std::unique_ptr<Expression> clone(Position pos) const override {
73 return std::make_unique<VariableReference>(args&: pos, args: this->variable(), args: this->refKind());
74 }
75
76 std::string description(OperatorPrecedence) const override;
77
78private:
79 const Variable* fVariable;
80 VariableRefKind fRefKind;
81
82 using INHERITED = Expression;
83};
84
85} // namespace SkSL
86
87#endif
88

source code of flutter_engine/third_party/skia/src/sksl/ir/SkSLVariableReference.h