Flutter Engine
The Flutter Engine
SkSLVariable.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_VARIABLE
9#define SKSL_VARIABLE
10
19
20#include <cstdint>
21#include <memory>
22#include <string>
23#include <string_view>
24#include <utility>
25
26namespace SkSL {
27
28class Context;
29class Expression;
30class GlobalVarDeclaration;
31class InterfaceBlock;
32class Mangler;
33class SymbolTable;
34class VarDeclaration;
35
36enum class VariableStorage : int8_t {
37 kGlobal,
39 kLocal,
41};
42
43/**
44 * Represents a variable, whether local, global, or a function parameter. This represents the
45 * variable itself (the storage location), which is shared between all VariableReferences which
46 * read or write that storage location.
47 */
48class Variable : public Symbol {
49public:
51
52 inline static constexpr Kind kIRNodeKind = Kind::kVariable;
53
55 std::string_view name, const Type* type, bool builtin, Storage storage)
57 , fModifierFlags(modifierFlags)
58 , fModifiersPosition(modifiersPosition)
59 , fStorage(storage)
60 , fBuiltin(builtin) {}
61
62 ~Variable() override;
63
64 static std::unique_ptr<Variable> Convert(const Context& context, Position pos,
65 Position modifiersPos, const Layout& layout,
67 Position namePos, std::string_view name,
69
70 static std::unique_ptr<Variable> Make(Position pos, Position modifiersPosition,
72 const Type* type, std::string_view name,
73 std::string mangledName, bool builtin, Storage storage);
74
75 /**
76 * Creates a local scratch variable and the associated VarDeclaration statement.
77 * Useful when doing IR rewrites, e.g. inlining a function call.
78 */
81 std::unique_ptr<Statement> fVarDecl;
82 };
83 static ScratchVariable MakeScratchVariable(const Context& context,
84 Mangler& mangler,
85 std::string_view baseName,
86 const Type* type,
87 SymbolTable* symbolTable,
88 std::unique_ptr<Expression> initialValue);
90 return fModifierFlags;
91 }
92
93 virtual const Layout& layout() const;
94
96 return fModifiersPosition;
97 }
98
99 bool isBuiltin() const {
100 return fBuiltin;
101 }
102
103 Storage storage() const {
104 return fStorage;
105 }
106
107 const Expression* initialValue() const;
108
110
111 void setVarDeclaration(VarDeclaration* declaration);
112
114
116
118 // The VarDeclaration is being deleted, so our reference to it has become stale.
119 fDeclaringElement = nullptr;
120 }
121
122 // The interfaceBlock methods are no-op stubs here. They have proper implementations in
123 // ExtendedVariable, declared below this class, which dedicates extra space to store the pointer
124 // back to the InterfaceBlock.
125 virtual InterfaceBlock* interfaceBlock() const { return nullptr; }
126
128
129 virtual void detachDeadInterfaceBlock() {}
130
131 // Only ExtendedVariables support mangled names.
132 virtual std::string_view mangledName() const { return this->name(); }
133
134 std::string description() const override {
135 return this->layout().paddedDescription() + this->modifierFlags().paddedDescription() +
136 this->type().displayName() + " " + std::string(this->name());
137 }
138
139private:
140 IRNode* fDeclaringElement = nullptr;
141 ModifierFlags fModifierFlags;
142 Position fModifiersPosition;
143 VariableStorage fStorage;
144 bool fBuiltin;
145
146 using INHERITED = Symbol;
147};
148
149/**
150 * ExtendedVariable is functionally equivalent to a regular Variable, but it also contains extra
151 * fields that most variables don't need:
152 * - The variable's associated InterfaceBlock
153 * - The variable's layout
154 * - The variable's mangled name
155 *
156 * Some of these fields can be null/empty.
157 */
158class ExtendedVariable final : public Variable {
159public:
161 ModifierFlags flags, std::string_view name, const Type* type, bool builtin,
162 Storage storage, std::string mangledName)
164 , fLayout(layout)
165 , fMangledName(std::move(mangledName)) {}
166
167 ~ExtendedVariable() override;
168
169 InterfaceBlock* interfaceBlock() const override {
170 return fInterfaceBlockElement;
171 }
172
173 const Layout& layout() const override {
174 return fLayout;
175 }
176
177 void setInterfaceBlock(InterfaceBlock* elem) override {
178 SkASSERT(!fInterfaceBlockElement);
179 fInterfaceBlockElement = elem;
180 }
181
182 void detachDeadInterfaceBlock() override {
183 // The InterfaceBlock is being deleted, so our reference to it has become stale.
184 fInterfaceBlockElement = nullptr;
185 }
186
187 std::string_view mangledName() const override;
188
189private:
190 InterfaceBlock* fInterfaceBlockElement = nullptr;
191 Layout fLayout;
192 std::string fMangledName;
193
194 using INHERITED = Variable;
195};
196
197} // namespace SkSL
198
199#endif
SkPoint pos
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
const Layout & layout() const override
Definition: SkSLVariable.h:173
~ExtendedVariable() override
void detachDeadInterfaceBlock() override
Definition: SkSLVariable.h:182
ExtendedVariable(Position pos, Position modifiersPosition, const Layout &layout, ModifierFlags flags, std::string_view name, const Type *type, bool builtin, Storage storage, std::string mangledName)
Definition: SkSLVariable.h:160
std::string_view mangledName() const override
InterfaceBlock * interfaceBlock() const override
Definition: SkSLVariable.h:169
void setInterfaceBlock(InterfaceBlock *elem) override
Definition: SkSLVariable.h:177
std::string paddedDescription() const
std::string_view name() const
Definition: SkSLSymbol.h:51
Symbol(Position pos, Kind kind, std::string_view name, const Type *type=nullptr)
Definition: SkSLSymbol.h:31
const Type & type() const
Definition: SkSLSymbol.h:42
std::string displayName() const
Definition: SkSLType.h:234
Variable(Position pos, Position modifiersPosition, ModifierFlags modifierFlags, std::string_view name, const Type *type, bool builtin, Storage storage)
Definition: SkSLVariable.h:54
void setVarDeclaration(VarDeclaration *declaration)
virtual std::string_view mangledName() const
Definition: SkSLVariable.h:132
static std::unique_ptr< Variable > Make(Position pos, Position modifiersPosition, const Layout &layout, ModifierFlags flags, const Type *type, std::string_view name, std::string mangledName, bool builtin, Storage storage)
void setGlobalVarDeclaration(GlobalVarDeclaration *global)
void detachDeadVarDeclaration()
Definition: SkSLVariable.h:117
std::string description() const override
Definition: SkSLVariable.h:134
Storage storage() const
Definition: SkSLVariable.h:103
VariableStorage Storage
Definition: SkSLVariable.h:50
static constexpr Kind kIRNodeKind
Definition: SkSLVariable.h:52
GlobalVarDeclaration * globalVarDeclaration() const
virtual void setInterfaceBlock(InterfaceBlock *)
Definition: SkSLVariable.h:127
virtual InterfaceBlock * interfaceBlock() const
Definition: SkSLVariable.h:125
~Variable() override
bool isBuiltin() const
Definition: SkSLVariable.h:99
Position modifiersPosition() const
Definition: SkSLVariable.h:95
const Expression * initialValue() const
static ScratchVariable MakeScratchVariable(const Context &context, Mangler &mangler, std::string_view baseName, const Type *type, SymbolTable *symbolTable, std::unique_ptr< Expression > initialValue)
VarDeclaration * varDeclaration() const
static std::unique_ptr< Variable > Convert(const Context &context, Position pos, Position modifiersPos, const Layout &layout, ModifierFlags flags, const Type *type, Position namePos, std::string_view name, Storage storage)
virtual void detachDeadInterfaceBlock()
Definition: SkSLVariable.h:129
ModifierFlags modifierFlags() const
Definition: SkSLVariable.h:89
virtual const Layout & layout() const
FlutterSemanticsFlag flags
SymbolKind
Definition: SkSLIRNode.h:32
VariableStorage
Definition: SkSLVariable.h:36
Definition: ref_ptr.h:256
std::string paddedDescription() const
std::unique_ptr< Statement > fVarDecl
Definition: SkSLVariable.h:81