summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/include/private/SkSLIRNode.h
blob: 8fb4279b76745d3183eb84310da2fb609342a451 (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
135
136
137
138
139
140
141
142
143
144
145
/*
 * 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_IRNODE
#define SKSL_IRNODE

#include "include/sksl/SkSLPosition.h"
#include "src/sksl/SkSLPool.h"

#include <string>

namespace SkSL {

// The fKind field of IRNode could contain any of these values.
enum class ProgramElementKind {
    kExtension = 0,
    kFunction,
    kFunctionPrototype,
    kGlobalVar,
    kInterfaceBlock,
    kModifiers,
    kStructDefinition,

    kFirst = kExtension,
    kLast = kStructDefinition
};

enum class SymbolKind {
    kExternal = (int) ProgramElementKind::kLast + 1,
    kField,
    kFunctionDeclaration,
    kType,
    kVariable,

    kFirst = kExternal,
    kLast = kVariable
};

enum class StatementKind {
    kBlock = (int) SymbolKind::kLast + 1,
    kBreak,
    kContinue,
    kDiscard,
    kDo,
    kExpression,
    kFor,
    kIf,
    kNop,
    kReturn,
    kSwitch,
    kSwitchCase,
    kVarDeclaration,

    kFirst = kBlock,
    kLast = kVarDeclaration,
};

enum class ExpressionKind {
    kBinary = (int) StatementKind::kLast + 1,
    kChildCall,
    kConstructorArray,
    kConstructorArrayCast,
    kConstructorCompound,
    kConstructorCompoundCast,
    kConstructorDiagonalMatrix,
    kConstructorMatrixResize,
    kConstructorScalarCast,
    kConstructorSplat,
    kConstructorStruct,
    kFieldAccess,
    kFunctionReference,
    kFunctionCall,
    kIndex,
    kLiteral,
    kMethodReference,
    kPoison,
    kPostfix,
    kPrefix,
    kSetting,
    kSwizzle,
    kTernary,
    kTypeReference,
    kVariableReference,

    kFirst = kBinary,
    kLast = kVariableReference
};

/**
 * Represents a node in the intermediate representation (IR) tree. The IR is a fully-resolved
 * version of the program (all types determined, everything validated), ready for code generation.
 */
class IRNode : public Poolable {
public:
    virtual ~IRNode() {}

    virtual std::string description() const = 0;

    // No copy construction or assignment
    IRNode(const IRNode&) = delete;
    IRNode& operator=(const IRNode&) = delete;

    // position of this element within the program being compiled, for error reporting purposes
    Position fPosition;

    /**
     *  Use is<T> to check the type of an IRNode.
     *  e.g. replace `s.kind() == Statement::Kind::kReturn` with `s.is<ReturnStatement>()`.
     */
    template <typename T>
    bool is() const {
        return this->fKind == (int)T::kIRNodeKind;
    }

    /**
     *  Use as<T> to downcast IRNodes.
     *  e.g. replace `(ReturnStatement&) s` with `s.as<ReturnStatement>()`.
     */
    template <typename T>
    const T& as() const {
        SkASSERT(this->is<T>());
        return static_cast<const T&>(*this);
    }

    template <typename T>
    T& as() {
        SkASSERT(this->is<T>());
        return static_cast<T&>(*this);
    }

protected:
    IRNode(Position position, int kind)
        : fPosition(position)
        , fKind(kind) {}

    int fKind;
};

}  // namespace SkSL

#endif