Flutter Engine
The Flutter Engine
SkSLVariable.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC.
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
9
24
25#include <utility>
26
27namespace SkSL {
28
29static constexpr Layout kDefaultLayout;
30
32 // Unhook this Variable from its associated VarDeclaration, since we're being deleted.
33 if (VarDeclaration* declaration = this->varDeclaration()) {
34 declaration->detachDeadVariable();
35 }
36}
37
39 // Unhook this Variable from its associated InterfaceBlock, since we're being deleted.
40 if (fInterfaceBlockElement) {
41 fInterfaceBlockElement->detachDeadVariable();
42 }
43}
44
46 VarDeclaration* declaration = this->varDeclaration();
47 return declaration ? declaration->value().get() : nullptr;
48}
49
51 if (!fDeclaringElement) {
52 return nullptr;
53 }
54 SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
55 fDeclaringElement->is<GlobalVarDeclaration>());
56 return fDeclaringElement->is<GlobalVarDeclaration>()
57 ? &fDeclaringElement->as<GlobalVarDeclaration>().varDeclaration()
58 : &fDeclaringElement->as<VarDeclaration>();
59}
60
62 if (!fDeclaringElement) {
63 return nullptr;
64 }
65 SkASSERT(fDeclaringElement->is<VarDeclaration>() ||
66 fDeclaringElement->is<GlobalVarDeclaration>());
67 return fDeclaringElement->is<GlobalVarDeclaration>()
68 ? &fDeclaringElement->as<GlobalVarDeclaration>()
69 : nullptr;
70}
71
73 SkASSERT(!fDeclaringElement || this == declaration->var());
74 if (!fDeclaringElement) {
75 fDeclaringElement = declaration;
76 }
77}
78
80 SkASSERT(!fDeclaringElement || this == global->varDeclaration().var());
81 fDeclaringElement = global;
82}
83
84const Layout& Variable::layout() const {
85 return kDefaultLayout;
86}
87
88std::string_view ExtendedVariable::mangledName() const {
89 return fMangledName.empty() ? this->name() : fMangledName;
90}
91
92std::unique_ptr<Variable> Variable::Convert(const Context& context,
94 Position modifiersPos,
95 const Layout& layout,
97 const Type* type,
98 Position namePos,
99 std::string_view name,
100 Storage storage) {
101 if (layout.fLocation == 0 &&
102 layout.fIndex == 0 &&
106 context.fErrors->error(modifiersPos,
107 "out location=0, index=0 is reserved for sk_FragColor");
108 }
109 if (type->isUnsizedArray() && storage != Variable::Storage::kInterfaceBlock) {
110 context.fErrors->error(pos, "unsized arrays are not permitted here");
111 }
112 if (ProgramConfig::IsCompute(context.fConfig->fKind) && layout.fBuiltin == -1) {
114 if (flags & ModifierFlag::kIn) {
115 context.fErrors->error(pos, "pipeline inputs not permitted in compute shaders");
116 } else if (flags & ModifierFlag::kOut) {
117 context.fErrors->error(pos, "pipeline outputs not permitted in compute shaders");
118 }
119 }
120 }
121 if (storage == Variable::Storage::kParameter) {
122 // The `in` modifier on function parameters is implicit, so we can replace `in float x` with
123 // `float x`. This prevents any ambiguity when matching a function by its param types.
126 }
127 }
128
129 // Invent a mangled name for the variable, if it needs one.
130 std::string mangledName;
131 if (skstd::starts_with(name, '$')) {
132 // The $ prefix will fail to compile in GLSL, so replace it with `sk_Priv`.
133 mangledName = "sk_Priv" + std::string(name.substr(1));
134 } else if (FindIntrinsicKind(name) != kNotIntrinsic) {
135 // Having a variable name overlap an intrinsic name will prevent us from calling the
136 // intrinsic, but it's not illegal for user names to shadow a global symbol.
137 // Mangle the name to avoid a possible collision.
139 }
140
141 return Make(pos, modifiersPos, layout, flags, type, name, std::move(mangledName),
142 context.fConfig->fIsBuiltinCode, storage);
143}
144
145std::unique_ptr<Variable> Variable::Make(Position pos,
146 Position modifiersPosition,
147 const Layout& layout,
149 const Type* type,
150 std::string_view name,
151 std::string mangledName,
152 bool builtin,
153 Variable::Storage storage) {
154 // the `in` modifier on function parameters is implicit and should have been removed
155 SkASSERT(!(storage == Variable::Storage::kParameter &&
157
158 if (type->componentType().isInterfaceBlock() || !mangledName.empty() ||
160 return std::make_unique<ExtendedVariable>(pos,
162 layout,
163 flags,
164 name,
165 type,
166 builtin,
167 storage,
168 std::move(mangledName));
169 } else {
170 return std::make_unique<Variable>(pos,
172 flags,
173 name,
174 type,
175 builtin,
176 storage);
177 }
178}
179
181 Mangler& mangler,
182 std::string_view baseName,
183 const Type* type,
184 SymbolTable* symbolTable,
185 std::unique_ptr<Expression> initialValue) {
186 // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
187 // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
188 // somewhere during compilation.
189 if (type->isLiteral()) {
190 SkDEBUGFAIL("found a $literal type in MakeScratchVariable");
192 }
193
194 // Provide our new variable with a unique name, and add it to our symbol table.
195 const std::string* name =
196 symbolTable->takeOwnershipOfString(mangler.uniqueName(baseName, symbolTable));
197
198 // Create our new variable and add it to the symbol table.
200 auto var = std::make_unique<Variable>(initialValue ? initialValue->fPosition : Position(),
201 /*modifiersPosition=*/Position(),
203 name->c_str(),
204 type,
205 symbolTable->isBuiltin(),
207
208 // If we are creating an array type, reduce it to base type plus array-size.
209 int arraySize = 0;
210 if (type->isArray()) {
211 arraySize = type->columns();
212 type = &type->componentType();
213 }
214 // Create our variable declaration.
215 result.fVarDecl = VarDeclaration::Make(context, var.get(), type, arraySize,
216 std::move(initialValue));
217 result.fVarSymbol = symbolTable->add(context, std::move(var));
218 return result;
219}
220
221} // namespace SkSL
SkPoint pos
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118
#define SkASSERT(cond)
Definition: SkAssert.h:116
GLenum type
static constexpr const char FRAGCOLOR_NAME[]
Definition: SkSLCompiler.h:71
ErrorReporter * fErrors
Definition: SkSLContext.h:36
SymbolTable * fSymbolTable
Definition: SkSLContext.h:48
ProgramConfig * fConfig
Definition: SkSLContext.h:33
void error(Position position, std::string_view msg)
~ExtendedVariable() override
std::string_view mangledName() const override
VarDeclaration & varDeclaration()
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133
Position fPosition
Definition: SkSLIRNode.h:109
std::string uniqueName(std::string_view baseName, SymbolTable *symbolTable)
Definition: SkSLMangler.cpp:21
const std::string * takeOwnershipOfString(std::string n)
bool isBuiltin() const
T * add(const Context &context, std::unique_ptr< T > symbol)
std::string_view name() const
Definition: SkSLSymbol.h:51
const Type & type() const
Definition: SkSLSymbol.h:42
virtual bool isArray() const
Definition: SkSLType.h:532
virtual const Type & componentType() const
Definition: SkSLType.h:404
virtual bool isInterfaceBlock() const
Definition: SkSLType.h:544
virtual bool isLiteral() const
Definition: SkSLType.h:516
virtual int columns() const
Definition: SkSLType.h:429
virtual const Type & scalarTypeForLiteral() const
Definition: SkSLType.h:520
virtual bool isUnsizedArray() const
Definition: SkSLType.h:536
std::unique_ptr< Expression > & value()
static std::unique_ptr< VarDeclaration > Make(const Context &context, Variable *var, const Type *baseType, int arraySize, std::unique_ptr< Expression > value)
Variable * var() const
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)
Storage storage() const
Definition: SkSLVariable.h:103
GlobalVarDeclaration * globalVarDeclaration() const
~Variable() override
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 const Layout & layout() const
FlutterSemanticsFlag flags
GAsyncResult * result
static constexpr Layout kDefaultLayout
VariableStorage
Definition: SkSLVariable.h:36
IntrinsicKind FindIntrinsicKind(std::string_view functionName)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
constexpr bool starts_with(std::string_view str, std::string_view prefix)
Definition: SkStringView.h:17
static bool IsFragment(ProgramKind kind)
static bool IsCompute(ProgramKind kind)