Flutter Engine
The Flutter Engine
SkSLIRNode.h
Go to the documentation of this file.
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_IRNODE
9#define SKSL_IRNODE
10
11#include "src/sksl/SkSLPool.h"
13
14#include <string>
15
16namespace SkSL {
17
18// The fKind field of IRNode could contain any of these values.
20 kExtension = 0,
21 kFunction,
27
29 kLast = kStructDefinition
30};
31
32enum class SymbolKind {
33 kExternal = (int) ProgramElementKind::kLast + 1,
34 kField,
36 kType,
38
39 kFirst = kExternal,
40 kLast = kVariable
41};
42
43enum class StatementKind {
45 kBreak,
48 kDo,
50 kFor,
51 kIf,
52 kNop,
53 kReturn,
54 kSwitch,
57
58 kFirst = kBlock,
59 kLast = kVarDeclaration,
60};
61
62enum class ExpressionKind {
74 kEmpty,
78 kIndex,
81 kPoison,
83 kPrefix,
89
91 kLast = kVariableReference
92};
93
94/**
95 * Represents a node in the intermediate representation (IR) tree. The IR is a fully-resolved
96 * version of the program (all types determined, everything validated), ready for code generation.
97 */
98class IRNode : public Poolable {
99public:
100 virtual ~IRNode() {}
101
102 virtual std::string description() const = 0;
103
104 // No copy construction or assignment
105 IRNode(const IRNode&) = delete;
106 IRNode& operator=(const IRNode&) = delete;
107
108 // Position of this element within the program being compiled, for error reporting purposes.
110
112 return fPosition;
113 }
114
116 fPosition = p;
117 }
118
119 /**
120 * Use is<T> to check the type of an IRNode.
121 * e.g. replace `s.kind() == Statement::Kind::kReturn` with `s.is<ReturnStatement>()`.
122 */
123 template <typename T>
124 bool is() const {
125 return this->fKind == (int)T::kIRNodeKind;
126 }
127
128 /**
129 * Use as<T> to downcast IRNodes.
130 * e.g. replace `(ReturnStatement&) s` with `s.as<ReturnStatement>()`.
131 */
132 template <typename T>
133 const T& as() const {
134 SkASSERT(this->is<T>());
135 return static_cast<const T&>(*this);
136 }
137
138 template <typename T>
139 T& as() {
140 SkASSERT(this->is<T>());
141 return static_cast<T&>(*this);
142 }
143
144protected:
147 , fKind(kind) {}
148
149 int fKind;
150};
151
152} // namespace SkSL
153
154#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
IRNode(const IRNode &)=delete
IRNode(Position position, int kind)
Definition: SkSLIRNode.h:145
Position position() const
Definition: SkSLIRNode.h:111
bool is() const
Definition: SkSLIRNode.h:124
void setPosition(Position p)
Definition: SkSLIRNode.h:115
const T & as() const
Definition: SkSLIRNode.h:133
virtual ~IRNode()
Definition: SkSLIRNode.h:100
IRNode & operator=(const IRNode &)=delete
virtual std::string description() const =0
Position fPosition
Definition: SkSLIRNode.h:109
StatementKind
Definition: SkSLIRNode.h:43
SymbolKind
Definition: SkSLIRNode.h:32
ProgramElementKind
Definition: SkSLIRNode.h:19
ExpressionKind
Definition: SkSLIRNode.h:62
#define T
Definition: precompiler.cc:65