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
8 #include "src/sksl/ir/SkSLVariable.h"
9
10 #include "src/base/SkEnumBitMask.h"
11 #include "src/base/SkStringView.h"
12 #include "src/sksl/SkSLCompiler.h"
13 #include "src/sksl/SkSLContext.h"
14 #include "src/sksl/SkSLErrorReporter.h"
15 #include "src/sksl/SkSLIntrinsicList.h"
16 #include "src/sksl/SkSLMangler.h"
17 #include "src/sksl/SkSLProgramSettings.h"
18 #include "src/sksl/ir/SkSLExpression.h"
19 #include "src/sksl/ir/SkSLIRNode.h"
20 #include "src/sksl/ir/SkSLInterfaceBlock.h"
21 #include "src/sksl/ir/SkSLLayout.h"
22 #include "src/sksl/ir/SkSLSymbolTable.h"
23 #include "src/sksl/ir/SkSLVarDeclarations.h"
24
25 #include <utility>
26
27 namespace SkSL {
28
29 static constexpr Layout kDefaultLayout;
30
~Variable()31 Variable::~Variable() {
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
~ExtendedVariable()38 ExtendedVariable::~ExtendedVariable() {
39 // Unhook this Variable from its associated InterfaceBlock, since we're being deleted.
40 if (fInterfaceBlockElement) {
41 fInterfaceBlockElement->detachDeadVariable();
42 }
43 }
44
initialValue() const45 const Expression* Variable::initialValue() const {
46 VarDeclaration* declaration = this->varDeclaration();
47 return declaration ? declaration->value().get() : nullptr;
48 }
49
varDeclaration() const50 VarDeclaration* Variable::varDeclaration() const {
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
globalVarDeclaration() const61 GlobalVarDeclaration* Variable::globalVarDeclaration() const {
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
setVarDeclaration(VarDeclaration * declaration)72 void Variable::setVarDeclaration(VarDeclaration* declaration) {
73 SkASSERT(!fDeclaringElement || this == declaration->var());
74 if (!fDeclaringElement) {
75 fDeclaringElement = declaration;
76 }
77 }
78
setGlobalVarDeclaration(GlobalVarDeclaration * global)79 void Variable::setGlobalVarDeclaration(GlobalVarDeclaration* global) {
80 SkASSERT(!fDeclaringElement || this == global->varDeclaration().var());
81 fDeclaringElement = global;
82 }
83
layout() const84 const Layout& Variable::layout() const {
85 return kDefaultLayout;
86 }
87
mangledName() const88 std::string_view ExtendedVariable::mangledName() const {
89 return fMangledName.empty() ? this->name() : fMangledName;
90 }
91
Convert(const Context & context,Position pos,Position modifiersPos,const Layout & layout,ModifierFlags flags,const Type * type,Position namePos,std::string_view name,Storage storage)92 std::unique_ptr<Variable> Variable::Convert(const Context& context,
93 Position pos,
94 Position modifiersPos,
95 const Layout& layout,
96 ModifierFlags flags,
97 const Type* type,
98 Position namePos,
99 std::string_view name,
100 Storage storage) {
101 if (layout.fLocation == 0 &&
102 layout.fIndex == 0 &&
103 (flags & ModifierFlag::kOut) &&
104 ProgramConfig::IsFragment(context.fConfig->fKind) &&
105 name != Compiler::FRAGCOLOR_NAME) {
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 && storage != Variable::Storage::kParameter) {
111 context.fErrors->error(pos, "unsized arrays are not permitted here");
112 }
113 if (ProgramConfig::IsCompute(context.fConfig->fKind) && layout.fBuiltin == -1) {
114 if (storage == Variable::Storage::kGlobal) {
115 if (flags & ModifierFlag::kIn) {
116 context.fErrors->error(pos, "pipeline inputs not permitted in compute shaders");
117 } else if (flags & ModifierFlag::kOut) {
118 context.fErrors->error(pos, "pipeline outputs not permitted in compute shaders");
119 }
120 }
121 }
122 if (storage == Variable::Storage::kParameter) {
123 // The `in` modifier on function parameters is implicit, so we can replace `in float x` with
124 // `float x`. This prevents any ambiguity when matching a function by its param types.
125 if ((flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn) {
126 flags &= ~(ModifierFlag::kOut | ModifierFlag::kIn);
127 }
128 }
129
130 // Invent a mangled name for the variable, if it needs one.
131 std::string mangledName;
132 if (skstd::starts_with(name, '$')) {
133 // The $ prefix will fail to compile in GLSL, so replace it with `sk_Priv`.
134 mangledName = "sk_Priv" + std::string(name.substr(1));
135 } else if (FindIntrinsicKind(name) != kNotIntrinsic) {
136 // Having a variable name overlap an intrinsic name will prevent us from calling the
137 // intrinsic, but it's not illegal for user names to shadow a global symbol.
138 // Mangle the name to avoid a possible collision.
139 mangledName = Mangler{}.uniqueName(name, context.fSymbolTable);
140 }
141
142 return Make(pos, modifiersPos, layout, flags, type, name, std::move(mangledName),
143 context.fConfig->isBuiltinCode(), storage);
144 }
145
Make(Position pos,Position modifiersPosition,const Layout & layout,ModifierFlags flags,const Type * type,std::string_view name,std::string mangledName,bool builtin,Variable::Storage storage)146 std::unique_ptr<Variable> Variable::Make(Position pos,
147 Position modifiersPosition,
148 const Layout& layout,
149 ModifierFlags flags,
150 const Type* type,
151 std::string_view name,
152 std::string mangledName,
153 bool builtin,
154 Variable::Storage storage) {
155 // the `in` modifier on function parameters is implicit and should have been removed
156 SkASSERT(!(storage == Variable::Storage::kParameter &&
157 (flags & (ModifierFlag::kOut | ModifierFlag::kIn)) == ModifierFlag::kIn));
158
159 if (type->componentType().isInterfaceBlock() || !mangledName.empty() ||
160 layout != kDefaultLayout) {
161 return std::make_unique<ExtendedVariable>(pos,
162 modifiersPosition,
163 layout,
164 flags,
165 name,
166 type,
167 builtin,
168 storage,
169 std::move(mangledName));
170 } else {
171 return std::make_unique<Variable>(pos,
172 modifiersPosition,
173 flags,
174 name,
175 type,
176 builtin,
177 storage);
178 }
179 }
180
MakeScratchVariable(const Context & context,Mangler & mangler,std::string_view baseName,const Type * type,SymbolTable * symbolTable,std::unique_ptr<Expression> initialValue)181 Variable::ScratchVariable Variable::MakeScratchVariable(const Context& context,
182 Mangler& mangler,
183 std::string_view baseName,
184 const Type* type,
185 SymbolTable* symbolTable,
186 std::unique_ptr<Expression> initialValue) {
187 // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
188 // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
189 // somewhere during compilation.
190 if (type->isLiteral()) {
191 SkDEBUGFAIL("found a $literal type in MakeScratchVariable");
192 type = &type->scalarTypeForLiteral();
193 }
194
195 // Provide our new variable with a unique name, and add it to our symbol table.
196 const std::string* name =
197 symbolTable->takeOwnershipOfString(mangler.uniqueName(baseName, symbolTable));
198
199 // Create our new variable and add it to the symbol table.
200 ScratchVariable result;
201 auto var = std::make_unique<Variable>(initialValue ? initialValue->fPosition : Position(),
202 /*modifiersPosition=*/Position(),
203 ModifierFlag::kNone,
204 name->c_str(),
205 type,
206 symbolTable->isBuiltin(),
207 Variable::Storage::kLocal);
208
209 // If we are creating an array type, reduce it to base type plus array-size.
210 int arraySize = 0;
211 if (type->isArray()) {
212 arraySize = type->columns();
213 type = &type->componentType();
214 }
215 // Create our variable declaration.
216 result.fVarDecl = VarDeclaration::Make(context, var.get(), type, arraySize,
217 std::move(initialValue));
218 result.fVarSymbol = symbolTable->add(context, std::move(var));
219 return result;
220 }
221
222 } // namespace SkSL
223