blob: bc856d6c8cb87679f22ef9049c798a92e4f296e0 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
10#include "limits.h"
John Stiles44e96be2020-08-31 13:16:04 -040011#include <iterator>
John Stilesfbd050b2020-08-03 13:21:46 -040012#include <memory>
Ethan Nicholasaf197692017-02-27 13:26:45 -050013#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070014
Ethan Nicholasdaed2592021-03-04 14:30:25 -050015#include "include/private/SkSLLayout.h"
John Stiles6bef6a72020-12-02 14:26:04 -050016#include "include/private/SkTArray.h"
Ethan Nicholasbe65cd52021-05-18 12:09:01 -040017#include "include/sksl/DSLCore.h"
John Stilesb2705a12021-02-19 10:55:14 -050018#include "src/core/SkScopeExit.h"
Ethan Nicholas6e0fa402020-08-20 14:08:23 -040019#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/SkSLCompiler.h"
John Stilesdc8ec312021-01-11 11:05:21 -050021#include "src/sksl/SkSLConstantFolder.h"
Brian Osman00185012021-02-04 16:07:11 -050022#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLParser.h"
Brian Osman3000d6b2020-07-31 15:57:28 -040024#include "src/sksl/SkSLUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLBinaryExpression.h"
26#include "src/sksl/ir/SkSLBoolLiteral.h"
27#include "src/sksl/ir/SkSLBreakStatement.h"
28#include "src/sksl/ir/SkSLConstructor.h"
29#include "src/sksl/ir/SkSLContinueStatement.h"
30#include "src/sksl/ir/SkSLDiscardStatement.h"
31#include "src/sksl/ir/SkSLDoStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040033#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050034#include "src/sksl/ir/SkSLExternalFunctionReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/sksl/ir/SkSLField.h"
36#include "src/sksl/ir/SkSLFieldAccess.h"
37#include "src/sksl/ir/SkSLFloatLiteral.h"
38#include "src/sksl/ir/SkSLForStatement.h"
39#include "src/sksl/ir/SkSLFunctionCall.h"
40#include "src/sksl/ir/SkSLFunctionDeclaration.h"
41#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050042#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLFunctionReference.h"
44#include "src/sksl/ir/SkSLIfStatement.h"
45#include "src/sksl/ir/SkSLIndexExpression.h"
46#include "src/sksl/ir/SkSLIntLiteral.h"
47#include "src/sksl/ir/SkSLInterfaceBlock.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060048#include "src/sksl/ir/SkSLNop.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/sksl/ir/SkSLPostfixExpression.h"
50#include "src/sksl/ir/SkSLPrefixExpression.h"
51#include "src/sksl/ir/SkSLReturnStatement.h"
52#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050053#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/sksl/ir/SkSLSwitchCase.h"
55#include "src/sksl/ir/SkSLSwitchStatement.h"
56#include "src/sksl/ir/SkSLSwizzle.h"
57#include "src/sksl/ir/SkSLTernaryExpression.h"
58#include "src/sksl/ir/SkSLUnresolvedFunction.h"
59#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050060#include "src/sksl/ir/SkSLVariable.h"
61#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070062
63namespace SkSL {
64
65class AutoSymbolTable {
66public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050067 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070068 : fIR(ir)
69 , fPrevious(fIR->fSymbolTable) {
70 fIR->pushSymbolTable();
71 }
72
73 ~AutoSymbolTable() {
74 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040075 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070076 }
77
78 IRGenerator* fIR;
79 std::shared_ptr<SymbolTable> fPrevious;
80};
81
John Stilesc1a98b82021-02-24 13:35:02 -050082IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040083 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050084
85void IRGenerator::pushSymbolTable() {
86 auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
87 fSymbolTable = std::move(childSymTable);
88}
89
90void IRGenerator::popSymbolTable() {
91 fSymbolTable = fSymbolTable->fParent;
92}
93
John Stilese3a91cf2021-01-26 10:13:58 -050094bool IRGenerator::detectVarDeclarationWithoutScope(const Statement& stmt) {
95 // Parsing an AST node containing a single variable declaration creates a lone VarDeclaration
96 // statement. An AST with multiple variable declarations creates an unscoped Block containing
97 // multiple VarDeclaration statements. We need to detect either case.
98 const Variable* var;
99 if (stmt.is<VarDeclaration>()) {
100 // The single-variable case. No blocks at all.
101 var = &stmt.as<VarDeclaration>().var();
102 } else if (stmt.is<Block>()) {
103 // The multiple-variable case: an unscoped, non-empty block...
104 const Block& block = stmt.as<Block>();
105 if (block.isScope() || block.children().empty()) {
106 return false;
107 }
108 // ... holding a variable declaration.
109 const Statement& innerStmt = *block.children().front();
110 if (!innerStmt.is<VarDeclaration>()) {
111 return false;
112 }
113 var = &innerStmt.as<VarDeclaration>().var();
114 } else {
115 // This statement wasn't a variable declaration. No problem.
116 return false;
117 }
118
119 // Report an error.
120 SkASSERT(var);
121 this->errorReporter().error(stmt.fOffset,
122 "variable '" + var->name() + "' must be created in a scope");
123 return true;
124}
125
Ethan Nicholas962dec42021-06-10 13:06:39 -0400126std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -0500127 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400128 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -0500129 this->errorReporter().error(offset, "extensions are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400130 return nullptr;
131 }
132
John Stilesfbd050b2020-08-03 13:21:46 -0400133 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134}
135
John Stiles20c09f92021-03-12 09:25:04 -0500136std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700137 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400138 case ASTNode::Kind::kBlock:
139 return this->convertBlock(statement);
140 case ASTNode::Kind::kVarDeclarations:
141 return this->convertVarDeclarationStatement(statement);
142 case ASTNode::Kind::kIf:
143 return this->convertIf(statement);
144 case ASTNode::Kind::kFor:
145 return this->convertFor(statement);
146 case ASTNode::Kind::kWhile:
147 return this->convertWhile(statement);
148 case ASTNode::Kind::kDo:
149 return this->convertDo(statement);
150 case ASTNode::Kind::kSwitch:
151 return this->convertSwitch(statement);
152 case ASTNode::Kind::kReturn:
153 return this->convertReturn(statement);
154 case ASTNode::Kind::kBreak:
155 return this->convertBreak(statement);
156 case ASTNode::Kind::kContinue:
157 return this->convertContinue(statement);
158 case ASTNode::Kind::kDiscard:
159 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500160 case ASTNode::Kind::kType:
161 // TODO: add IRNode for struct definition inside a function
162 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400163 default:
164 // it's an expression
Brian Osman99ddd2a2021-08-27 11:21:12 -0400165 return this->convertExpressionStatement(statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 }
167}
168
Ethan Nicholasfc994162019-06-06 10:04:27 -0400169std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
170 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700171 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400172 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400173 for (const auto& child : block) {
174 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700175 if (!statement) {
176 return nullptr;
177 }
178 statements.push_back(std::move(statement));
179 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500180 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181}
182
Ethan Nicholasfc994162019-06-06 10:04:27 -0400183std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
184 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400185 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400186 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 return nullptr;
188 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500189 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700190}
191
John Stiles80b02af2021-02-12 17:07:51 -0500192int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500193 if (!s) {
194 this->errorReporter().error(offset, "array must have a size");
195 return 0;
196 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500197 auto size = this->convertExpression(s);
198 if (!size) {
199 return 0;
200 }
John Stiles7253a202021-08-23 09:24:33 -0400201 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500202}
203
Ethan Nicholas489e5522021-01-20 10:53:11 -0500204void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
205 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500206 if (this->strictES2Mode() && baseType->isArray()) {
207 this->errorReporter().error(offset, "array size must appear after variable name");
208 }
209
John Stiles3624aba2020-12-22 16:29:53 -0500210 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500211 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500212 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500213 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400214 }
Brian Osman8c264792021-07-01 16:41:27 -0400215 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
216 this->errorReporter().error(offset, "'in' variables may not have matrix type");
217 }
218 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
219 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500220 }
John Stilesbb2ef922021-07-26 08:32:07 -0400221 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400222 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400223 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400224 }
225 }
Brian Osman5c625822021-04-06 14:28:07 -0400226 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
227 this->errorReporter().error(
228 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
229 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400230 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400231 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500232 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500233 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400234 }
235 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500236 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500237 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400238 }
239 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500240 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400241 (t.columns() == 3 || t.columns() == 4);
242 };
John Stilesc0c51062020-12-03 17:16:29 -0500243 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400244 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500245 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500246 "'srgb_unpremul' is only permitted on half3, half4, "
247 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400248 }
249 }
John Stilesb806da42021-08-04 16:26:47 -0400250 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
251 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400252 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500253 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400254 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400255 }
Brian Osmana654faa2021-02-26 11:52:59 -0500256 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400257 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500258}
259
Ethan Nicholasbd974002021-02-22 16:20:06 -0500260std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400261 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500262 bool isArray,
263 std::unique_ptr<Expression> arraySize,
264 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500265 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500266 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400267 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500268 this->errorReporter().error(offset,
269 "out location=0, index=0 is reserved for sk_FragColor");
270 }
271 const Type* type = baseType;
272 int arraySizeValue = 0;
273 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500274 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400275 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500276 if (!arraySizeValue) {
277 return {};
278 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500279 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
280 }
John Stilesf2872e62021-05-04 11:38:43 -0400281 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
282 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500283}
284
285std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400286 std::unique_ptr<Expression> value,
287 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400288 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
289 std::move(value));
290 if (!varDecl) {
291 return nullptr;
292 }
293
294 // Detect the declaration of magical variables.
295 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
296 // Silently ignore duplicate definitions of `sk_FragColor`.
297 const Symbol* symbol = (*fSymbolTable)[var->name()];
298 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500299 return nullptr;
300 }
John Stilese67bd132021-03-19 18:39:25 -0400301 } else if ((var->storage() == Variable::Storage::kGlobal ||
302 var->storage() == Variable::Storage::kInterfaceBlock) &&
303 var->name() == Compiler::RTADJUST_NAME) {
304 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
305 if (fRTAdjust) {
306 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500307 return nullptr;
308 }
John Stilese67bd132021-03-19 18:39:25 -0400309 if (var->type() != *fContext.fTypes.fFloat4) {
310 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500311 return nullptr;
312 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500313 fRTAdjust = var.get();
314 }
John Stilese67bd132021-03-19 18:39:25 -0400315
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400316 if (addToSymbolTable) {
317 fSymbolTable->add(std::move(var));
318 } else {
319 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
320 }
John Stilese67bd132021-03-19 18:39:25 -0400321 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500322}
323
324std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
325 const Modifiers& modifiers,
326 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400327 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500328 bool isArray,
329 std::unique_ptr<Expression> arraySize,
330 std::unique_ptr<Expression> value,
331 Variable::Storage storage) {
332 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
333 std::move(arraySize), storage);
334 if (!var) {
335 return nullptr;
336 }
337 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500338}
339
340StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
341 Variable::Storage storage) {
342 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
343 auto declarationsIter = decls.begin();
344 const Modifiers& modifiers = declarationsIter++->getModifiers();
345 const ASTNode& rawType = *(declarationsIter++);
346 const Type* baseType = this->convertType(rawType);
347 if (!baseType) {
348 return {};
349 }
John Stilesb806da42021-08-04 16:26:47 -0400350 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
351 decls.fOffset);
352 if (!baseType) {
353 return {};
354 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500355
356 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400357
358 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400359 for (; declarationsIter != decls.end(); ++declarationsIter) {
360 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400361 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500362 std::unique_ptr<Expression> arraySize;
363 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400364 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500365 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400366 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500367 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400368 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700369 }
John Stiles8f633ef2021-08-21 17:13:11 -0400370 arraySize = this->convertExpression(*iter++);
371 if (!arraySize) {
372 continue;
373 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700374 }
Brian Osman4cf85072021-01-14 14:18:10 -0500375 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400376 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700377 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400378 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700379 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700380 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500381 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
382 modifiers,
383 baseType,
384 varData.fName,
385 varData.fIsArray,
386 std::move(arraySize),
387 std::move(value),
388 storage);
389 if (varDeclStmt) {
390 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700391 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700392 }
Brian Osmanc0213602020-10-06 14:43:32 -0400393 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700394}
395
Ethan Nicholasfc994162019-06-06 10:04:27 -0400396std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500397 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400398 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -0500399 this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400400 return nullptr;
401 }
402
Ethan Nicholasfc994162019-06-06 10:04:27 -0400403 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
404 Modifiers modifiers = m.getModifiers();
John Stilesf2872e62021-05-04 11:38:43 -0400405 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700406}
407
John Stilesad2319f2020-09-02 15:01:47 -0400408std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
409 SkASSERT(n.fKind == ASTNode::Kind::kIf);
410 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500411 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400412 if (!test) {
413 return nullptr;
414 }
415 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
416 if (!ifTrue) {
417 return nullptr;
418 }
John Stilescf3059e2021-02-25 14:27:02 -0500419 if (this->detectVarDeclarationWithoutScope(*ifTrue)) {
420 return nullptr;
421 }
John Stilesad2319f2020-09-02 15:01:47 -0400422 std::unique_ptr<Statement> ifFalse;
423 if (iter != n.end()) {
424 ifFalse = this->convertStatement(*(iter++));
425 if (!ifFalse) {
426 return nullptr;
427 }
John Stilescf3059e2021-02-25 14:27:02 -0500428 if (this->detectVarDeclarationWithoutScope(*ifFalse)) {
429 return nullptr;
430 }
John Stilesad2319f2020-09-02 15:01:47 -0400431 }
John Stiles5ede6e32021-01-11 13:00:49 -0500432 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500433 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
434 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400435}
436
Ethan Nicholasfc994162019-06-06 10:04:27 -0400437std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
438 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700439 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700440 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400441 auto iter = f.begin();
442 if (*iter) {
443 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700444 if (!initializer) {
445 return nullptr;
446 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700447 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400448 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700449 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400450 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500451 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700452 if (!test) {
453 return nullptr;
454 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700455 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400456 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700457 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400458 if (*iter) {
459 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700460 if (!next) {
461 return nullptr;
462 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700463 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400464 ++iter;
465 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700466 if (!statement) {
467 return nullptr;
468 }
John Stiles2b6ec982021-03-04 10:46:39 -0500469 if (this->detectVarDeclarationWithoutScope(*statement)) {
470 return nullptr;
471 }
Brian Osman77ba8102021-01-12 17:15:30 -0500472
John Stiles23521a82021-03-02 17:02:51 -0500473 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
474 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500475}
476
477std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
478 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400479 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500480 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700481 if (!test) {
482 return nullptr;
483 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400484 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700485 if (!statement) {
486 return nullptr;
487 }
John Stilesb321a072021-02-25 16:24:19 -0500488 if (this->detectVarDeclarationWithoutScope(*statement)) {
489 return nullptr;
490 }
John Stiles23521a82021-03-02 17:02:51 -0500491 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
492 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700493}
494
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500495std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
496 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400497 auto iter = d.begin();
498 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
499 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700500 return nullptr;
501 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500502 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400503 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700504 return nullptr;
505 }
John Stilesea5822e2021-02-26 11:18:20 -0500506 if (this->detectVarDeclarationWithoutScope(*statement)) {
507 return nullptr;
508 }
John Stiles23521a82021-03-02 17:02:51 -0500509 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700510}
511
Ethan Nicholascfefec02021-02-09 15:22:57 -0500512std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
513 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
514
515 auto iter = s.begin();
516 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
517 if (!value) {
518 return nullptr;
519 }
520 AutoSymbolTable table(this);
521 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500522 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500523 for (; iter != s.end(); ++iter) {
524 const ASTNode& c = *iter;
525 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
526 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
527 auto childIter = c.begin();
528 if (*childIter) {
529 caseValue = this->convertExpression(*childIter);
530 if (!caseValue) {
531 return nullptr;
532 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500533 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400534 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500535
John Stilesbf16b6c2021-03-12 19:24:31 -0500536 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400537 for (; childIter != c.end(); ++childIter) {
538 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500539 if (!converted) {
540 return nullptr;
541 }
542 statements.push_back(std::move(converted));
543 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500544
545 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500546 }
John Stiles23521a82021-03-02 17:02:51 -0500547 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
548 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500549}
550
Ethan Nicholasfc994162019-06-06 10:04:27 -0400551std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
552 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700553 if (!e) {
554 return nullptr;
555 }
John Stiles3e5871c2021-02-25 20:52:03 -0500556 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700557}
558
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500559std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
560 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500561 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500562}
563
564std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
565 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
566 if (r.begin() != r.end()) {
567 std::unique_ptr<Expression> value = this->convertExpression(*r.begin());
568 if (!value) {
569 return nullptr;
570 }
571 return this->convertReturn(r.fOffset, std::move(value));
572 } else {
573 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700574 }
575}
576
Ethan Nicholasfc994162019-06-06 10:04:27 -0400577std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
578 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500579 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700580}
581
Ethan Nicholasfc994162019-06-06 10:04:27 -0400582std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
583 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500584 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700585}
586
Ethan Nicholasfc994162019-06-06 10:04:27 -0400587std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
588 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400589 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500590 this->errorReporter().error(d.fOffset,
591 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500592 return nullptr;
593 }
John Stilesa0c04d62021-03-11 23:07:24 -0500594 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700595}
596
Robert Phillipsfe8da172018-01-24 14:52:02 +0000597std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
John Stilesd45ebbc2021-05-18 16:54:36 -0400598 using namespace SkSL::dsl;
599 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
600
Brian Osman88cda172020-10-09 12:05:16 -0400601 const Variable* skPerVertex = nullptr;
602 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400603 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
604 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400605 }
606
Brian Osman88cda172020-10-09 12:05:16 -0400607 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500608 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
John Stilesd45ebbc2021-05-18 16:54:36 -0400609 return VariableReference::Make(/*offset=*/-1, var);
John Stiles5acf6a82020-11-10 22:38:01 -0500610 };
611 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
John Stiles06d600f2021-03-08 09:18:21 -0500612 return FieldAccess::Make(fContext, Ref(var), idx,
613 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
John Stiles5acf6a82020-11-10 22:38:01 -0500614 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400615 auto Pos = [&]() -> DSLExpression {
616 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
617 FieldAccess::OwnerKind::kAnonymousInterfaceBlock));
John Stiles5acf6a82020-11-10 22:38:01 -0500618 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400619 auto Adjust = [&]() -> DSLExpression {
620 return DSLExpression(fRTAdjustInterfaceBlock
621 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
622 : Ref(fRTAdjust));
John Stiles5acf6a82020-11-10 22:38:01 -0500623 };
John Stiles750109b2020-10-30 13:45:46 -0400624
John Stilesd45ebbc2021-05-18 16:54:36 -0400625 return DSLStatement(
626 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
627 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
628 0,
629 Pos().w())
630 ).release();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000631}
632
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400633void IRGenerator::CheckModifiers(const Context& context,
634 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500635 const Modifiers& modifiers,
636 int permittedModifierFlags,
637 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400638 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
639 { Modifiers::kConst_Flag, "const" },
640 { Modifiers::kIn_Flag, "in" },
641 { Modifiers::kOut_Flag, "out" },
642 { Modifiers::kUniform_Flag, "uniform" },
643 { Modifiers::kFlat_Flag, "flat" },
644 { Modifiers::kNoPerspective_Flag, "noperspective" },
645 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
646 { Modifiers::kInline_Flag, "inline" },
647 { Modifiers::kNoInline_Flag, "noinline" },
648 { Modifiers::kHighp_Flag, "highp" },
649 { Modifiers::kMediump_Flag, "mediump" },
650 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400651 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500652 };
653
Brian Osmanb5de6be2021-08-10 15:16:34 -0400654 int modifierFlags = modifiers.fFlags;
655 for (const auto& f : kModifierFlags) {
656 if (modifierFlags & f.flag) {
657 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400658 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400659 }
660 modifierFlags &= ~f.flag;
661 }
662 }
663 SkASSERT(modifierFlags == 0);
664
665 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
666 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
667 { Layout::kPushConstant_Flag, "push_constant"},
668 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
669 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
670 { Layout::kLocation_Flag, "location"},
671 { Layout::kOffset_Flag, "offset"},
672 { Layout::kBinding_Flag, "binding"},
673 { Layout::kIndex_Flag, "index"},
674 { Layout::kSet_Flag, "set"},
675 { Layout::kBuiltin_Flag, "builtin"},
676 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
Brian Osmanb5de6be2021-08-10 15:16:34 -0400677 };
Brian Osmana654faa2021-02-26 11:52:59 -0500678
679 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400680 for (const auto& lf : kLayoutFlags) {
681 if (layoutFlags & lf.flag) {
682 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400683 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400684 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500685 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400686 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500687 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400688 }
Brian Osmana654faa2021-02-26 11:52:59 -0500689 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400690}
691
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400692std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl,
John Stiles842ee5b2021-08-27 12:37:27 -0400693 std::unique_ptr<Block> body,
694 IntrinsicSet* referencedIntrinsics) {
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400695 bool isMain = funcDecl.isMain();
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400696 if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) {
697 body->children().push_back(this->getNormalizeSkPositionCode());
698 }
699
John Stiles8d130842021-08-27 12:42:45 -0400700 FunctionDefinition::FinalizeFunctionBody(fContext, funcDecl, body.get(), referencedIntrinsics);
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400701 return body;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500702}
703
Ethan Nicholasfc994162019-06-06 10:04:27 -0400704void IRGenerator::convertFunction(const ASTNode& f) {
705 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400706 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400707 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400708 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700709 }
John Stilesb9af7232020-08-20 15:57:48 -0400710 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400711 std::vector<std::unique_ptr<Variable>> parameters;
712 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400713 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400714 const ASTNode& param = *(iter++);
715 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500716 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400717 auto paramIter = param.begin();
718 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700719 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400720 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700721 }
John Stilesd39aec92020-12-03 14:37:16 -0500722 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500723 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500724 if (!arraySize) {
725 return;
726 }
John Stilesa2179502020-12-03 14:37:57 -0500727 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700728 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400729
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400730 parameters.push_back(std::make_unique<Variable>(param.fOffset,
731 this->modifiersPool().add(pd.fModifiers),
732 pd.fName,
733 type,
734 fIsBuiltinCode,
735 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700736 }
737
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400738 // Conservatively assume all user-defined functions have side effects.
739 Modifiers declModifiers = funcData.fModifiers;
740 if (!fIsBuiltinCode) {
741 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400742 }
743
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400744 if (fContext.fConfig->fSettings.fForceNoInline) {
745 // Apply the `noinline` modifier to every function. This allows us to test Runtime
746 // Effects without any inlining, even when the code is later added to a paint.
747 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
748 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700749 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400750
751 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
752 fContext,
753 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400754 f.fOffset,
755 this->modifiersPool().add(declModifiers),
756 funcData.fName,
757 std::move(parameters),
758 returnType,
759 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700760 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400761 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700762 }
John Stiles569249b2020-11-03 12:18:22 -0500763 if (iter == f.end()) {
764 // If there's no body, we've found a prototype.
765 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
766 fIsBuiltinCode));
767 } else {
768 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700769 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500770 for (const Variable* param : decl->parameters()) {
771 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700772 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400773 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700774 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400775 return;
776 }
John Stiles842ee5b2021-08-27 12:37:27 -0400777 IntrinsicSet referencedIntrinsics;
778 body = this->finalizeFunction(*decl, std::move(body), &referencedIntrinsics);
John Stiles607d36b2020-10-19 15:00:01 -0400779 auto result = std::make_unique<FunctionDefinition>(
John Stiles842ee5b2021-08-27 12:37:27 -0400780 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(referencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -0400781 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400782 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500783 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700784 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700785}
786
John Stilesdc75a972020-11-25 16:24:55 -0500787std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
788 SkASSERT(node.fKind == ASTNode::Kind::kType);
789
790 const Type* type = this->convertType(node);
791 if (!type) {
792 return nullptr;
793 }
John Stilesc0c51062020-12-03 17:16:29 -0500794 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500795 this->errorReporter().error(node.fOffset,
796 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500797 return nullptr;
798 }
Brian Osman02bc5222021-01-28 11:00:20 -0500799 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
800 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500801 return std::make_unique<StructDefinition>(node.fOffset, *type);
802}
803
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400804std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500805 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400806 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -0500807 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400808 return nullptr;
809 }
810
Ethan Nicholasfc994162019-06-06 10:04:27 -0400811 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500812 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700813 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400814 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700815 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000816 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400817 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400818 {
819 AutoSymbolTable table(this);
820 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400821 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
822 StatementArray decls = this->convertVarDeclarations(*(iter++),
823 Variable::Storage::kInterfaceBlock);
824 if (decls.empty()) {
825 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400826 }
John Stiles869cdef2020-10-30 14:24:24 -0400827 for (const auto& decl : decls) {
828 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -0400829 if (&vd.var() == fRTAdjust) {
830 foundRTAdjust = true;
John Stiles54e7c052021-01-11 14:22:36 -0500831 SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4);
John Stiles869cdef2020-10-30 14:24:24 -0400832 fRTAdjustFieldIndex = fields.size();
833 }
834 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
835 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400836 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500837 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700838 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400839 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400840 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -0500841 fields));
John Stilesd39aec02020-12-03 10:42:26 -0500842 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -0500843 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400844 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500845 if (size) {
Brian Osman4cf85072021-01-14 14:18:10 -0500846 // convertArraySize rejects unsized arrays. This is the one place we allow those, but
847 // we've already checked for that, so this is verifying the other aspects (constant,
848 // positive, not too large).
John Stiles80b02af2021-02-12 17:07:51 -0500849 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osman4cf85072021-01-14 14:18:10 -0500850 if (!arraySize) {
John Stilesd39aec02020-12-03 10:42:26 -0500851 return nullptr;
852 }
Ethan Nicholas50afc172017-02-16 14:49:57 -0500853 } else {
John Stilesd39aec02020-12-03 10:42:26 -0500854 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500855 }
John Stilesa2179502020-12-03 14:37:57 -0500856 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500857 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400858 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400859 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -0400860 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400861 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400862 type,
Brian Osman3887a012020-09-30 13:22:27 -0400863 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400864 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000865 if (foundRTAdjust) {
866 fRTAdjustInterfaceBlock = var;
867 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400868 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -0400869 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700870 } else {
871 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -0400872 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -0700873 }
874 }
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400875 return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset,
876 var,
Ethan Nicholas3533ff12021-08-02 12:53:29 -0400877 id.fTypeName,
878 id.fInstanceName,
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400879 arraySize,
880 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -0700881}
882
John Stiles7bd70332020-11-30 17:04:09 -0500883void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
884 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
885 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -0500886 const Type* type = &stmt->as<VarDeclaration>().baseType();
887 if (type->isStruct()) {
888 auto [iter, wasInserted] = fDefinedStructs.insert(type);
889 if (wasInserted) {
890 fProgramElements->push_back(
891 std::make_unique<StructDefinition>(decl.fOffset, *type));
892 }
893 }
Ethan Nicholas624a5292021-04-16 14:54:43 -0400894 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -0500895 }
896}
897
John Stiles823c5042021-08-17 12:09:00 -0400898static bool type_contains_private_fields(const Type& type) {
John Stilesb4b627e2020-11-13 15:55:27 -0500899 // Checks for usage of private types, including fields inside a struct.
900 if (type.isPrivate()) {
901 return true;
902 }
John Stilesc0c51062020-12-03 17:16:29 -0500903 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -0500904 for (const auto& f : type.fields()) {
John Stiles823c5042021-08-17 12:09:00 -0400905 if (type_contains_private_fields(*f.fType)) {
John Stilesb4b627e2020-11-13 15:55:27 -0500906 return true;
907 }
908 }
909 }
910 return false;
911}
912
Brian Osmand8070392020-09-09 15:50:02 -0400913const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400914 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -0500915 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -0500916 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -0500917 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -0500918 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700919 }
John Stiles6bef6a72020-12-02 14:26:04 -0500920 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -0500921 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -0400922 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -0500923 this->errorReporter().error(type.fOffset,
924 "type '" + name + "' not allowed in this context");
925 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -0500926 }
John Stiles823c5042021-08-17 12:09:00 -0400927 if (!fIsBuiltinCode) {
928 if (type_contains_private_fields(*result)) {
929 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
930 return nullptr;
931 }
932 if (this->strictES2Mode() && !result->allowedInES2()) {
933 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
934 return nullptr;
935 }
John Stiles7bd70332020-11-30 17:04:09 -0500936 }
John Stiles6bef6a72020-12-02 14:26:04 -0500937 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -0500938 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -0500939 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -0500940 if (!arraySize) {
941 return nullptr;
942 }
John Stilesa2179502020-12-03 14:37:57 -0500943 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -0500944 }
John Stiles6bef6a72020-12-02 14:26:04 -0500945 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700946}
947
Ethan Nicholasfc994162019-06-06 10:04:27 -0400948std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700949 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400950 case ASTNode::Kind::kBinary:
951 return this->convertBinaryExpression(expr);
952 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -0500953 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400954 case ASTNode::Kind::kCall:
955 return this->convertCallExpression(expr);
956 case ASTNode::Kind::kField:
957 return this->convertFieldExpression(expr);
958 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -0500959 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400960 case ASTNode::Kind::kIdentifier:
961 return this->convertIdentifier(expr);
962 case ASTNode::Kind::kIndex:
963 return this->convertIndexExpression(expr);
964 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -0500965 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400966 case ASTNode::Kind::kPostfix:
967 return this->convertPostfixExpression(expr);
968 case ASTNode::Kind::kPrefix:
969 return this->convertPrefixExpression(expr);
970 case ASTNode::Kind::kTernary:
971 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700972 default:
John Stilesfbb905e2021-08-25 15:51:50 -0400973 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500974 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700975 }
976}
977
Ethan Nicholas962dec42021-06-10 13:06:39 -0400978std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500979 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -0700980 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -0500981 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700982 return nullptr;
983 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400984 switch (result->kind()) {
985 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -0700986 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -0400987 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -0700988 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500989 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700990 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400991 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400992 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500993 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -0700994 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400995 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400996 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400997 const Modifiers& modifiers = var->modifiers();
998 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400999 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -04001000 if (caps().canUseFragCoord()) {
1001 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001002 }
Brian Salomond8d85b92021-07-07 09:41:17 -04001003 break;
1004 case SK_CLOCKWISE_BUILTIN:
1005 fInputs.fUseFlipRTUniform = true;
1006 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001007 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001008 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -04001009 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001010 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001011 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001012 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -04001013 auto base = VariableReference::Make(offset, &field->owner(),
1014 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -05001015 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
1016 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001017 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001018 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001019 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001020 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001021 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001022 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001023 const ExternalFunction* r = &result->as<ExternalFunction>();
1024 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001025 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001026 default:
John Stilesf57207b2021-02-02 17:50:34 -05001027 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001028 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001029}
1030
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001031std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001032 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001033}
1034
Ethan Nicholas11d53972016-11-28 11:23:23 -05001035std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001036 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -05001037 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -07001038}
1039
Ethan Nicholasfc994162019-06-06 10:04:27 -04001040std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1041 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1042 auto iter = expression.begin();
1043 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001044 if (!left) {
1045 return nullptr;
1046 }
John Stiles0f464502020-11-20 12:52:22 -05001047 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001048 if (!right) {
1049 return nullptr;
1050 }
John Stiles23521a82021-03-02 17:02:51 -05001051 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
1052 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -07001053}
1054
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001055std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1056 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1057 auto iter = node.begin();
1058 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1059 if (!test) {
1060 return nullptr;
1061 }
1062 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1063 if (!ifTrue) {
1064 return nullptr;
1065 }
1066 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1067 if (!ifFalse) {
1068 return nullptr;
1069 }
John Stiles23521a82021-03-02 17:02:51 -05001070 return TernaryExpression::Convert(fContext, std::move(test),
1071 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001072}
1073
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001074void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001075 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001076 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001077
1078 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1079 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001080 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1081 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001082 std::sort(intrinsics.begin(), intrinsics.end(),
1083 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001084 if (a->isBuiltin() != b->isBuiltin()) {
1085 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001086 }
1087 if (a->fOffset != b->fOffset) {
1088 return a->fOffset < b->fOffset;
1089 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001090 if (a->name() != b->name()) {
1091 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001092 }
1093 return a->description() < b->description();
1094 });
1095 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001096 this->copyIntrinsicIfNeeded(*f);
1097 }
John Stiles607d36b2020-10-19 15:00:01 -04001098
Brian Osman0006ad02020-11-18 15:38:39 -05001099 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001100 }
1101}
1102
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001103std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001104 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001105 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001106 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001107 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001108 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001109 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001110 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001111 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001112 }
1113 }
John Stilesea9ab822020-08-31 09:55:04 -04001114
John Stilescd7ba502021-03-19 10:54:59 -04001115 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001116}
1117
1118/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001119 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001120 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1121 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001122 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001123CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001124 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001125 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1126 return CoercionCost::Impossible();
1127 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001128 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001129 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001130 }
John Stilesfa889112020-10-12 19:03:43 -04001131 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001132 const Type* ignored;
1133 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001134 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001135 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001136 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001137 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001138 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001139 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001140 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001141}
1142
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001143std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001144 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001145 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001146 switch (functionValue->kind()) {
1147 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001148 return Constructor::Convert(fContext,
1149 offset,
1150 functionValue->as<TypeReference>().value(),
1151 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001152 case Expression::Kind::kExternalFunctionReference: {
1153 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1154 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001155 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001156 this->errorReporter().error(offset, "external function expected " +
1157 to_string(count) + " arguments, but found " +
1158 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001159 return nullptr;
1160 }
1161 static constexpr int PARAMETER_MAX = 16;
1162 SkASSERT(count < PARAMETER_MAX);
1163 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001164 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001165 for (int i = 0; i < count; ++i) {
1166 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1167 if (!arguments[i]) {
1168 return nullptr;
1169 }
1170 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001171 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001172 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001173 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001174 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001175 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001176 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001177 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001178 if (functions.size() > 1) {
1179 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001180 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001181 if (cost < bestCost) {
1182 bestCost = cost;
1183 best = f;
1184 }
1185 }
1186 if (best) {
1187 return this->call(offset, *best, std::move(arguments));
1188 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001189 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001190 String separator;
1191 for (size_t i = 0; i < arguments.size(); i++) {
1192 msg += separator;
1193 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001194 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001195 }
1196 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001197 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001198 return nullptr;
1199 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001200 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001201 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001202 default:
John Stilesb30151e2021-01-11 16:13:08 -05001203 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001204 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001205 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001206}
1207
Ethan Nicholasfc994162019-06-06 10:04:27 -04001208std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1209 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1210 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001211 if (!base) {
1212 return nullptr;
1213 }
John Stiles23521a82021-03-02 17:02:51 -05001214 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001215}
1216
ethannicholasb3058bd2016-07-01 08:22:01 -07001217std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001218 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001219 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001220}
1221
Ethan Nicholasfc994162019-06-06 10:04:27 -04001222std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1223 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1224 auto iter = index.begin();
1225 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001226 if (!base) {
1227 return nullptr;
1228 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001229 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001230 if (base->is<TypeReference>()) {
1231 this->errorReporter().error(index.fOffset, "array must have a size");
1232 } else {
1233 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1234 }
Brian Osman4cf85072021-01-14 14:18:10 -05001235 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001236 }
1237 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1238 if (!converted) {
1239 return nullptr;
1240 }
John Stilesbb8cf582021-08-26 23:34:59 -04001241 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001242}
1243
Ethan Nicholasfc994162019-06-06 10:04:27 -04001244std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1245 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1246 auto iter = callNode.begin();
1247 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1248 if (!base) {
1249 return nullptr;
1250 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001251 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001252 for (; iter != callNode.end(); ++iter) {
1253 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1254 if (!converted) {
1255 return nullptr;
1256 }
1257 arguments.push_back(std::move(converted));
1258 }
1259 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1260}
1261
1262std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1263 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1264 if (!base) {
1265 return nullptr;
1266 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001267 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001268 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001269 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001270 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001271 }
John Stiles06d600f2021-03-08 09:18:21 -05001272 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001273}
1274
1275std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001276 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001277 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1278 if (!base) {
1279 return nullptr;
1280 }
John Stiles23521a82021-03-02 17:02:51 -05001281 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001282}
1283
1284void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001285 switch (expr.kind()) {
John Stilesdc435fa2020-12-30 18:07:10 -05001286 case Expression::Kind::kFunctionCall: {
1287 const FunctionDeclaration& decl = expr.as<FunctionCall>().function();
1288 if (!decl.isBuiltin() && !decl.definition()) {
John Stilesb30151e2021-01-11 16:13:08 -05001289 this->errorReporter().error(expr.fOffset,
1290 "function '" + decl.description() + "' is not defined");
John Stilesdc435fa2020-12-30 18:07:10 -05001291 }
1292 break;
1293 }
John Stiles973009b2021-02-17 17:18:01 -05001294 case Expression::Kind::kFunctionReference:
1295 case Expression::Kind::kTypeReference:
1296 SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()");
1297 this->errorReporter().error(expr.fOffset, "invalid expression");
1298 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001299 default:
John Stiles54e7c052021-01-11 14:22:36 -05001300 if (expr.type() == *fContext.fTypes.fInvalid) {
John Stilesb30151e2021-01-11 16:13:08 -05001301 this->errorReporter().error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07001302 }
John Stiles973009b2021-02-17 17:18:01 -05001303 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001304 }
1305}
1306
Brian Osman9496fe52020-11-18 14:48:19 -05001307void IRGenerator::findAndDeclareBuiltinVariables() {
1308 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001309 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001310 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001311
Brian Osman9496fe52020-11-18 14:48:19 -05001312 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001313 // If this is the *first* time we've seen this builtin, findAndInclude will return
1314 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001315 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001316 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001317 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001318 }
1319 }
1320
John Stiles4d6310a2021-01-26 19:58:22 -05001321 bool visitProgramElement(const ProgramElement& pe) override {
1322 if (pe.is<FunctionDefinition>()) {
1323 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1324 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1325 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001326 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001327 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1328 fPreserveFragColor = true;
1329 }
1330 }
1331 return INHERITED::visitProgramElement(pe);
1332 }
1333
Brian Osman9496fe52020-11-18 14:48:19 -05001334 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001335 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001336 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001337 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001338 return INHERITED::visitExpression(e);
1339 }
1340
1341 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001342 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001343 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001344
Brian Osman9496fe52020-11-18 14:48:19 -05001345 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001346 using INHERITED::visitProgramElement;
1347 };
1348
Brian Osman9496fe52020-11-18 14:48:19 -05001349 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001350 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001351 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001352 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001353 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001354
John Stiles4d6310a2021-01-26 19:58:22 -05001355 if (scanner.fPreserveFragColor) {
1356 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001357 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001358 }
1359
John Stilesd1204642021-02-17 16:30:02 -05001360 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001361 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001362 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1363 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1364 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001365 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001366 break;
1367 default:
1368 break;
1369 }
1370
Brian Osman9496fe52020-11-18 14:48:19 -05001371 fSharedElements->insert(
1372 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001373}
1374
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001375void IRGenerator::start(const ParsedModule& base,
1376 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001377 std::vector<std::unique_ptr<ProgramElement>>* elements,
1378 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001379 fProgramElements = elements;
1380 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001381 fSymbolTable = base.fSymbols;
1382 fIntrinsics = base.fIntrinsics.get();
1383 if (fIntrinsics) {
1384 fIntrinsics->resetAlreadyIncluded();
1385 }
1386 fIsBuiltinCode = isBuiltinCode;
1387
Brian Salomond8d85b92021-07-07 09:41:17 -04001388 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001389 fRTAdjust = nullptr;
1390 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001391 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001392 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001393
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001394 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001395 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001396 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001397 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001398 }
1399 }
John Stilesaecf8d52021-05-14 12:15:01 -04001400
John Stilesbb2ef922021-07-26 08:32:07 -04001401 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001402 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001403 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001404 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1405 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1406 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1407 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1408 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1409 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1410 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1411 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1412 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1413
1414 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1415 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1416 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1417 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1418 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1419 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1420
1421 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1422 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1423 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1424 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1425 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1426 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001427
1428 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1429 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1430 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1431 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001432
1433 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1434 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1435 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1436 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1437
1438 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1439 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1440 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1441 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001442 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001443}
Brian Osman88cda172020-10-09 12:05:16 -04001444
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001445IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001446 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001447 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001448 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001449 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001450
Brian Osmanea485e52021-01-15 13:20:32 -05001451 // Do a pass looking for dangling FunctionReference or TypeReference expressions
Brian Osmanc95d3a12020-09-09 10:56:27 -04001452 class FindIllegalExpressions : public ProgramVisitor {
1453 public:
1454 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
1455
1456 bool visitExpression(const Expression& e) override {
1457 fGenerator->checkValid(e);
1458 return INHERITED::visitExpression(e);
1459 }
1460
1461 IRGenerator* fGenerator;
1462 using INHERITED = ProgramVisitor;
1463 using INHERITED::visitProgramElement;
1464 };
1465 for (const auto& pe : *fProgramElements) {
1466 FindIllegalExpressions{this}.visitProgramElement(*pe);
1467 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001468
Brian Osmanea485e52021-01-15 13:20:32 -05001469 // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the
1470 // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic
1471 // assumes that all loops meet the criteria of Section 4, and if they don't, could crash.
1472 if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) {
1473 for (const auto& pe : *fProgramElements) {
1474 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
1475 }
1476 }
1477
Brian Osman7da06572021-07-21 15:19:34 -04001478 if (this->strictES2Mode()) {
1479 Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter());
1480 }
1481
John Stilesf2872e62021-05-04 11:38:43 -04001482 return IRBundle{std::move(*fProgramElements),
1483 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001484 std::move(fSymbolTable),
1485 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001486}
1487
1488IRGenerator::IRBundle IRGenerator::convertProgram(
1489 const ParsedModule& base,
1490 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001491 skstd::string_view text) {
1492 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001493 fFile = parser.compilationUnit();
1494 if (this->errorReporter().errorCount() == 0) {
1495 SkASSERT(fFile);
1496 for (const auto& decl : fFile->root()) {
1497 switch (decl.fKind) {
1498 case ASTNode::Kind::kVarDeclarations:
1499 this->convertGlobalVarDeclarations(decl);
1500 break;
1501
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001502 case ASTNode::Kind::kFunction:
1503 this->convertFunction(decl);
1504 break;
1505
1506 case ASTNode::Kind::kModifiers: {
1507 std::unique_ptr<ModifiersDeclaration> f =
1508 this->convertModifiersDeclaration(decl);
1509 if (f) {
1510 fProgramElements->push_back(std::move(f));
1511 }
1512 break;
1513 }
1514 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001515 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001516 if (i) {
1517 fProgramElements->push_back(std::move(i));
1518 }
1519 break;
1520 }
1521 case ASTNode::Kind::kExtension: {
1522 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001523 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001524 if (e) {
1525 fProgramElements->push_back(std::move(e));
1526 }
1527 break;
1528 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001529 case ASTNode::Kind::kType: {
1530 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1531 if (s) {
1532 fProgramElements->push_back(std::move(s));
1533 }
1534 break;
1535 }
1536 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001537 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001538 break;
1539 }
1540 }
1541 }
1542 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001543}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001544
John Stilesa6841be2020-08-06 14:11:56 -04001545} // namespace SkSL