summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/sksl/ir/SkSLVariableReference.h
blob: d33c569314c25831dd3bdf2c9cb927624b714ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_VARIABLEREFERENCE
#define SKSL_VARIABLEREFERENCE

#include "include/core/SkTypes.h"
#include "include/private/SkSLIRNode.h"
#include "include/sksl/SkSLPosition.h"
#include "src/sksl/ir/SkSLExpression.h"

#include <cstdint>
#include <memory>
#include <string>

namespace SkSL {

class Variable;
enum class OperatorPrecedence : uint8_t;

enum class VariableRefKind : int8_t {
    kRead,
    kWrite,
    kReadWrite,
    // taking the address of a variable - we consider this a read & write but don't complain if
    // the variable was not previously assigned
    kPointer
};

/**
 * A reference to a variable, through which it can be read or written. In the statement:
 *
 * x = x + 1;
 *
 * there is only one Variable 'x', but two VariableReferences to it.
 */
class VariableReference final : public Expression {
public:
    using RefKind = VariableRefKind;

    inline static constexpr Kind kIRNodeKind = Kind::kVariableReference;

    VariableReference(Position pos, const Variable* variable, RefKind refKind);

    // Creates a VariableReference. There isn't much in the way of error-checking or optimization
    // opportunities here.
    static std::unique_ptr<Expression> Make(Position pos,
                                            const Variable* variable,
                                            RefKind refKind = RefKind::kRead) {
        SkASSERT(variable);
        return std::make_unique<VariableReference>(pos, variable, refKind);
    }

    VariableReference(const VariableReference&) = delete;
    VariableReference& operator=(const VariableReference&) = delete;

    const Variable* variable() const {
        return fVariable;
    }

    RefKind refKind() const {
        return fRefKind;
    }

    void setRefKind(RefKind refKind);
    void setVariable(const Variable* variable);

    std::unique_ptr<Expression> clone(Position pos) const override {
        return std::make_unique<VariableReference>(pos, this->variable(), this->refKind());
    }

    std::string description(OperatorPrecedence) const override;

private:
    const Variable* fVariable;
    VariableRefKind fRefKind;

    using INHERITED = Expression;
};

}  // namespace SkSL

#endif