Coverage Report

Created: 2024-09-14 07:19

/src/skia/src/sksl/ir/SkSLVariableReference.h
Line
Count
Source
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
20
namespace SkSL {
21
22
class Variable;
23
enum class OperatorPrecedence : uint8_t;
24
25
enum 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
 */
41
class VariableReference final : public Expression {
42
public:
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
32.6k
                                            RefKind refKind = RefKind::kRead) {
54
32.6k
        SkASSERT(variable);
55
32.6k
        return std::make_unique<VariableReference>(pos, variable, refKind);
56
32.6k
    }
57
58
    VariableReference(const VariableReference&) = delete;
59
    VariableReference& operator=(const VariableReference&) = delete;
60
61
72.0k
    const Variable* variable() const {
62
72.0k
        return fVariable;
63
72.0k
    }
64
65
57.7k
    RefKind refKind() const {
66
57.7k
        return fRefKind;
67
57.7k
    }
68
69
    void setRefKind(RefKind refKind);
70
    void setVariable(const Variable* variable);
71
72
15.1k
    std::unique_ptr<Expression> clone(Position pos) const override {
73
15.1k
        return std::make_unique<VariableReference>(pos, this->variable(), this->refKind());
74
15.1k
    }
75
76
    std::string description(OperatorPrecedence) const override;
77
78
private:
79
    const Variable* fVariable;
80
    VariableRefKind fRefKind;
81
82
    using INHERITED = Expression;
83
};
84
85
}  // namespace SkSL
86
87
#endif