aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ir/SkSLVariableReference.h
blob: 92aef94290c716baa8ea025d9d70cfe1f3581c0d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * 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 "SkSLBoolLiteral.h"
#include "SkSLConstructor.h"
#include "SkSLExpression.h"
#include "SkSLFloatLiteral.h"
#include "SkSLIRGenerator.h"
#include "SkSLIntLiteral.h"

namespace SkSL {

/**
 * 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.
 */
struct VariableReference : public Expression {
    enum RefKind {
        kRead_RefKind,
        kWrite_RefKind,
        kReadWrite_RefKind
    };

    VariableReference(Position position, const Variable& variable, RefKind refKind = kRead_RefKind)
    : INHERITED(position, kVariableReference_Kind, variable.fType)
    , fVariable(variable)
    , fRefKind(refKind) {
        if (refKind != kRead_RefKind) {
            fVariable.fWriteCount++;
        }
        if (refKind != kWrite_RefKind) {
            fVariable.fReadCount++;
        }
    }

    ~VariableReference() override {
        if (fRefKind != kWrite_RefKind) {
            fVariable.fReadCount--;
        }
    }

    RefKind refKind() {
        return fRefKind;
    }

    void setRefKind(RefKind refKind) {
        if (fRefKind != kRead_RefKind) {
            fVariable.fWriteCount--;
        }
        if (fRefKind != kWrite_RefKind) {
            fVariable.fReadCount--;
        }
        if (refKind != kRead_RefKind) {
            fVariable.fWriteCount++;
        }
        if (refKind != kWrite_RefKind) {
            fVariable.fReadCount++;
        }
        fRefKind = refKind;
    }

    bool hasSideEffects() const override {
        return false;
    }

    String description() const override {
        return fVariable.fName;
    }

    static std::unique_ptr<Expression> copy_constant(const IRGenerator& irGenerator,
                                                     const Expression* expr) {
        ASSERT(expr->isConstant());
        switch (expr->fKind) {
            case Expression::kIntLiteral_Kind:
                return std::unique_ptr<Expression>(new IntLiteral(
                                                                 irGenerator.fContext,
                                                                 Position(),
                                                                 ((IntLiteral*) expr)->fValue));
            case Expression::kFloatLiteral_Kind:
                return std::unique_ptr<Expression>(new FloatLiteral(
                                                               irGenerator.fContext,
                                                               Position(),
                                                               ((FloatLiteral*) expr)->fValue));
            case Expression::kBoolLiteral_Kind:
                return std::unique_ptr<Expression>(new BoolLiteral(irGenerator.fContext,
                                                                   Position(),
                                                                   ((BoolLiteral*) expr)->fValue));
            case Expression::kConstructor_Kind: {
                const Constructor* c = (const Constructor*) expr;
                std::vector<std::unique_ptr<Expression>> args;
                for (const auto& arg : c->fArguments) {
                    args.push_back(copy_constant(irGenerator, arg.get()));
                }
                return std::unique_ptr<Expression>(new Constructor(Position(), c->fType,
                                                                   std::move(args)));
            }
            default:
                ABORT("unsupported constant\n");
        }
    }

    std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
                                                  const DefinitionMap& definitions) override {
        if (fRefKind != kRead_RefKind) {
            return nullptr;
        }
        auto exprIter = definitions.find(&fVariable);
        if (exprIter != definitions.end() && exprIter->second &&
            (*exprIter->second)->isConstant()) {
            return copy_constant(irGenerator, exprIter->second->get());
        }
        return nullptr;
    }

    const Variable& fVariable;
    RefKind fRefKind;

private:
    typedef Expression INHERITED;
};

} // namespace

#endif