blob: 20d6307803045e17dcdd2dedc8f7ede6d442f84a [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"
Brian Osmaneb0f29d2021-08-04 11:34:16 -040028#include "src/sksl/ir/SkSLChildCall.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/sksl/ir/SkSLConstructor.h"
30#include "src/sksl/ir/SkSLContinueStatement.h"
31#include "src/sksl/ir/SkSLDiscardStatement.h"
32#include "src/sksl/ir/SkSLDoStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040034#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050035#include "src/sksl/ir/SkSLExternalFunctionReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/sksl/ir/SkSLField.h"
37#include "src/sksl/ir/SkSLFieldAccess.h"
38#include "src/sksl/ir/SkSLFloatLiteral.h"
39#include "src/sksl/ir/SkSLForStatement.h"
40#include "src/sksl/ir/SkSLFunctionCall.h"
41#include "src/sksl/ir/SkSLFunctionDeclaration.h"
42#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050043#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/ir/SkSLFunctionReference.h"
45#include "src/sksl/ir/SkSLIfStatement.h"
46#include "src/sksl/ir/SkSLIndexExpression.h"
47#include "src/sksl/ir/SkSLIntLiteral.h"
48#include "src/sksl/ir/SkSLInterfaceBlock.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060049#include "src/sksl/ir/SkSLNop.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/sksl/ir/SkSLPostfixExpression.h"
51#include "src/sksl/ir/SkSLPrefixExpression.h"
52#include "src/sksl/ir/SkSLReturnStatement.h"
53#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050054#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#include "src/sksl/ir/SkSLSwitchCase.h"
56#include "src/sksl/ir/SkSLSwitchStatement.h"
57#include "src/sksl/ir/SkSLSwizzle.h"
58#include "src/sksl/ir/SkSLTernaryExpression.h"
59#include "src/sksl/ir/SkSLUnresolvedFunction.h"
60#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050061#include "src/sksl/ir/SkSLVariable.h"
62#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070063
64namespace SkSL {
65
66class AutoSymbolTable {
67public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050068 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070069 : fIR(ir)
70 , fPrevious(fIR->fSymbolTable) {
71 fIR->pushSymbolTable();
72 }
73
74 ~AutoSymbolTable() {
75 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040076 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070077 }
78
79 IRGenerator* fIR;
80 std::shared_ptr<SymbolTable> fPrevious;
81};
82
John Stilesc1a98b82021-02-24 13:35:02 -050083IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040084 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050085
86void IRGenerator::pushSymbolTable() {
87 auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
88 fSymbolTable = std::move(childSymTable);
89}
90
91void IRGenerator::popSymbolTable() {
92 fSymbolTable = fSymbolTable->fParent;
93}
94
John Stilese3a91cf2021-01-26 10:13:58 -050095bool IRGenerator::detectVarDeclarationWithoutScope(const Statement& stmt) {
96 // Parsing an AST node containing a single variable declaration creates a lone VarDeclaration
97 // statement. An AST with multiple variable declarations creates an unscoped Block containing
98 // multiple VarDeclaration statements. We need to detect either case.
99 const Variable* var;
100 if (stmt.is<VarDeclaration>()) {
101 // The single-variable case. No blocks at all.
102 var = &stmt.as<VarDeclaration>().var();
103 } else if (stmt.is<Block>()) {
104 // The multiple-variable case: an unscoped, non-empty block...
105 const Block& block = stmt.as<Block>();
106 if (block.isScope() || block.children().empty()) {
107 return false;
108 }
109 // ... holding a variable declaration.
110 const Statement& innerStmt = *block.children().front();
111 if (!innerStmt.is<VarDeclaration>()) {
112 return false;
113 }
114 var = &innerStmt.as<VarDeclaration>().var();
115 } else {
116 // This statement wasn't a variable declaration. No problem.
117 return false;
118 }
119
120 // Report an error.
121 SkASSERT(var);
122 this->errorReporter().error(stmt.fOffset,
123 "variable '" + var->name() + "' must be created in a scope");
124 return true;
125}
126
Ethan Nicholas962dec42021-06-10 13:06:39 -0400127std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -0500128 if (this->programKind() != ProgramKind::kFragment &&
129 this->programKind() != ProgramKind::kVertex &&
130 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500131 this->errorReporter().error(offset, "extensions are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400132 return nullptr;
133 }
134
John Stilesfbd050b2020-08-03 13:21:46 -0400135 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136}
137
John Stiles20c09f92021-03-12 09:25:04 -0500138std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700139 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400140 case ASTNode::Kind::kBlock:
141 return this->convertBlock(statement);
142 case ASTNode::Kind::kVarDeclarations:
143 return this->convertVarDeclarationStatement(statement);
144 case ASTNode::Kind::kIf:
145 return this->convertIf(statement);
146 case ASTNode::Kind::kFor:
147 return this->convertFor(statement);
148 case ASTNode::Kind::kWhile:
149 return this->convertWhile(statement);
150 case ASTNode::Kind::kDo:
151 return this->convertDo(statement);
152 case ASTNode::Kind::kSwitch:
153 return this->convertSwitch(statement);
154 case ASTNode::Kind::kReturn:
155 return this->convertReturn(statement);
156 case ASTNode::Kind::kBreak:
157 return this->convertBreak(statement);
158 case ASTNode::Kind::kContinue:
159 return this->convertContinue(statement);
160 case ASTNode::Kind::kDiscard:
161 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500162 case ASTNode::Kind::kType:
163 // TODO: add IRNode for struct definition inside a function
164 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400165 default:
166 // it's an expression
167 std::unique_ptr<Statement> result = this->convertExpressionStatement(statement);
John Stilesd1204642021-02-17 16:30:02 -0500168 if (fRTAdjust && this->programKind() == ProgramKind::kGeometry) {
John Stilese67bd132021-03-19 18:39:25 -0400169 SkASSERT(result->is<ExpressionStatement>());
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400170 Expression& expr = *result->as<ExpressionStatement>().expression();
John Stilese67bd132021-03-19 18:39:25 -0400171 if (expr.is<FunctionCall>()) {
John Stiles403a3632020-08-20 12:11:48 -0400172 FunctionCall& fc = expr.as<FunctionCall>();
Ethan Nicholased84b732020-10-08 11:45:44 -0400173 if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") {
John Stiles8f2a0cf2020-10-13 12:48:21 -0400174 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400175 statements.reserve_back(2);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000176 statements.push_back(getNormalizeSkPositionCode());
177 statements.push_back(std::move(result));
John Stilesbf16b6c2021-03-12 19:24:31 -0500178 return Block::Make(statement.fOffset, std::move(statements),
179 fSymbolTable, /*isScope=*/true);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000180 }
181 }
182 }
183 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700184 }
185}
186
Ethan Nicholasfc994162019-06-06 10:04:27 -0400187std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
188 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400190 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400191 for (const auto& child : block) {
192 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700193 if (!statement) {
194 return nullptr;
195 }
196 statements.push_back(std::move(statement));
197 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500198 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700199}
200
Ethan Nicholasfc994162019-06-06 10:04:27 -0400201std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
202 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400203 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400204 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 return nullptr;
206 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500207 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700208}
209
John Stiles80b02af2021-02-12 17:07:51 -0500210int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500211 if (!s) {
212 this->errorReporter().error(offset, "array must have a size");
213 return 0;
214 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500215 auto size = this->convertExpression(s);
216 if (!size) {
217 return 0;
218 }
John Stiles80b02af2021-02-12 17:07:51 -0500219 return this->convertArraySize(type, std::move(size));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500220}
221
John Stiles80b02af2021-02-12 17:07:51 -0500222int IRGenerator::convertArraySize(const Type& type, std::unique_ptr<Expression> size) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500223 size = this->coerce(std::move(size), *fContext.fTypes.fInt);
Brian Osman4cf85072021-01-14 14:18:10 -0500224 if (!size) {
225 return 0;
226 }
John Stiles2558c462021-03-16 17:49:20 -0400227 if (type.isVoid()) {
John Stiles80b02af2021-02-12 17:07:51 -0500228 this->errorReporter().error(size->fOffset, "type 'void' may not be used in an array");
229 return 0;
230 }
231 if (type.isOpaque()) {
232 this->errorReporter().error(
233 size->fOffset, "opaque type '" + type.name() + "' may not be used in an array");
234 return 0;
235 }
Brian Osman4cf85072021-01-14 14:18:10 -0500236 if (!size->is<IntLiteral>()) {
237 this->errorReporter().error(size->fOffset, "array size must be an integer");
238 return 0;
239 }
240 SKSL_INT count = size->as<IntLiteral>().value();
241 if (count <= 0) {
242 this->errorReporter().error(size->fOffset, "array size must be positive");
243 return 0;
244 }
245 if (!SkTFitsIn<int>(count)) {
246 this->errorReporter().error(size->fOffset, "array size is too large");
247 return 0;
248 }
249 return static_cast<int>(count);
250}
251
Ethan Nicholas489e5522021-01-20 10:53:11 -0500252void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
253 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500254 if (this->strictES2Mode() && baseType->isArray()) {
255 this->errorReporter().error(offset, "array size must appear after variable name");
256 }
257
John Stiles3624aba2020-12-22 16:29:53 -0500258 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500259 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500260 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500261 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400262 }
Brian Osman8c264792021-07-01 16:41:27 -0400263 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
264 this->errorReporter().error(offset, "'in' variables may not have matrix type");
265 }
266 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
267 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500268 }
John Stilesbb2ef922021-07-26 08:32:07 -0400269 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400270 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400271 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400272 }
273 }
Brian Osman5c625822021-04-06 14:28:07 -0400274 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
275 this->errorReporter().error(
276 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
277 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400278 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400279 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500280 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500281 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400282 }
283 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500284 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500285 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400286 }
287 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500288 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400289 (t.columns() == 3 || t.columns() == 4);
290 };
John Stilesc0c51062020-12-03 17:16:29 -0500291 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400292 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500293 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500294 "'srgb_unpremul' is only permitted on half3, half4, "
295 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400296 }
297 }
John Stilesb806da42021-08-04 16:26:47 -0400298 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
299 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400300 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500301 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400302 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400303 }
Brian Osmana654faa2021-02-26 11:52:59 -0500304 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400305 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500306}
307
Ethan Nicholasbd974002021-02-22 16:20:06 -0500308std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400309 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500310 bool isArray,
311 std::unique_ptr<Expression> arraySize,
312 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500313 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500314 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400315 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500316 this->errorReporter().error(offset,
317 "out location=0, index=0 is reserved for sk_FragColor");
318 }
319 const Type* type = baseType;
320 int arraySizeValue = 0;
321 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500322 SkASSERT(arraySize);
John Stiles80b02af2021-02-12 17:07:51 -0500323 arraySizeValue = this->convertArraySize(*type, std::move(arraySize));
324 if (!arraySizeValue) {
325 return {};
326 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500327 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
328 }
John Stilesf2872e62021-05-04 11:38:43 -0400329 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
330 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500331}
332
333std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400334 std::unique_ptr<Expression> value,
335 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400336 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
337 std::move(value));
338 if (!varDecl) {
339 return nullptr;
340 }
341
342 // Detect the declaration of magical variables.
343 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
344 // Silently ignore duplicate definitions of `sk_FragColor`.
345 const Symbol* symbol = (*fSymbolTable)[var->name()];
346 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500347 return nullptr;
348 }
John Stilese67bd132021-03-19 18:39:25 -0400349 } else if ((var->storage() == Variable::Storage::kGlobal ||
350 var->storage() == Variable::Storage::kInterfaceBlock) &&
351 var->name() == Compiler::RTADJUST_NAME) {
352 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
353 if (fRTAdjust) {
354 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500355 return nullptr;
356 }
John Stilese67bd132021-03-19 18:39:25 -0400357 if (var->type() != *fContext.fTypes.fFloat4) {
358 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500359 return nullptr;
360 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500361 fRTAdjust = var.get();
362 }
John Stilese67bd132021-03-19 18:39:25 -0400363
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400364 if (addToSymbolTable) {
365 fSymbolTable->add(std::move(var));
366 } else {
367 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
368 }
John Stilese67bd132021-03-19 18:39:25 -0400369 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500370}
371
372std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
373 const Modifiers& modifiers,
374 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400375 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500376 bool isArray,
377 std::unique_ptr<Expression> arraySize,
378 std::unique_ptr<Expression> value,
379 Variable::Storage storage) {
380 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
381 std::move(arraySize), storage);
382 if (!var) {
383 return nullptr;
384 }
385 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500386}
387
388StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
389 Variable::Storage storage) {
390 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
391 auto declarationsIter = decls.begin();
392 const Modifiers& modifiers = declarationsIter++->getModifiers();
393 const ASTNode& rawType = *(declarationsIter++);
394 const Type* baseType = this->convertType(rawType);
395 if (!baseType) {
396 return {};
397 }
John Stilesb806da42021-08-04 16:26:47 -0400398 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
399 decls.fOffset);
400 if (!baseType) {
401 return {};
402 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500403
404 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400405
406 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400407 for (; declarationsIter != decls.end(); ++declarationsIter) {
408 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400409 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500410 std::unique_ptr<Expression> arraySize;
411 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500413 if (iter != varDecl.end() && varData.fIsArray) {
414 if (*iter) {
415 arraySize = this->convertExpression(*iter++);
416 } else {
417 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400418 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700419 }
420 }
Brian Osman4cf85072021-01-14 14:18:10 -0500421 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400422 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400424 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700426 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500427 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
428 modifiers,
429 baseType,
430 varData.fName,
431 varData.fIsArray,
432 std::move(arraySize),
433 std::move(value),
434 storage);
435 if (varDeclStmt) {
436 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700437 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700438 }
Brian Osmanc0213602020-10-06 14:43:32 -0400439 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700440}
441
Ethan Nicholasfc994162019-06-06 10:04:27 -0400442std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500443 if (this->programKind() != ProgramKind::kFragment &&
444 this->programKind() != ProgramKind::kVertex &&
445 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500446 this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400447 return nullptr;
448 }
449
Ethan Nicholasfc994162019-06-06 10:04:27 -0400450 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
451 Modifiers modifiers = m.getModifiers();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400452 if (modifiers.fLayout.fInvocations != -1) {
John Stilesd1204642021-02-17 16:30:02 -0500453 if (this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500454 this->errorReporter().error(m.fOffset,
455 "'invocations' is only legal in geometry shaders");
Ethan Nicholasf06576b2019-04-03 15:45:25 -0400456 return nullptr;
457 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400458 fInvocations = modifiers.fLayout.fInvocations;
John Stilesc1a98b82021-02-24 13:35:02 -0500459 if (!this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400460 modifiers.fLayout.fInvocations = -1;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400461 if (modifiers.fLayout.description() == "") {
462 return nullptr;
463 }
464 }
465 }
John Stilesc1a98b82021-02-24 13:35:02 -0500466 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 &&
467 !this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400468 modifiers.fLayout.fMaxVertices *= fInvocations;
469 }
John Stilesf2872e62021-05-04 11:38:43 -0400470 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700471}
472
John Stilesad2319f2020-09-02 15:01:47 -0400473std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
474 SkASSERT(n.fKind == ASTNode::Kind::kIf);
475 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500476 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400477 if (!test) {
478 return nullptr;
479 }
480 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
481 if (!ifTrue) {
482 return nullptr;
483 }
John Stilescf3059e2021-02-25 14:27:02 -0500484 if (this->detectVarDeclarationWithoutScope(*ifTrue)) {
485 return nullptr;
486 }
John Stilesad2319f2020-09-02 15:01:47 -0400487 std::unique_ptr<Statement> ifFalse;
488 if (iter != n.end()) {
489 ifFalse = this->convertStatement(*(iter++));
490 if (!ifFalse) {
491 return nullptr;
492 }
John Stilescf3059e2021-02-25 14:27:02 -0500493 if (this->detectVarDeclarationWithoutScope(*ifFalse)) {
494 return nullptr;
495 }
John Stilesad2319f2020-09-02 15:01:47 -0400496 }
John Stiles5ede6e32021-01-11 13:00:49 -0500497 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500498 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
499 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400500}
501
Ethan Nicholasfc994162019-06-06 10:04:27 -0400502std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
503 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700504 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700505 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400506 auto iter = f.begin();
507 if (*iter) {
508 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700509 if (!initializer) {
510 return nullptr;
511 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700512 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400513 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700514 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400515 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500516 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700517 if (!test) {
518 return nullptr;
519 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700520 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400521 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700522 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400523 if (*iter) {
524 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700525 if (!next) {
526 return nullptr;
527 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700528 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400529 ++iter;
530 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700531 if (!statement) {
532 return nullptr;
533 }
John Stiles2b6ec982021-03-04 10:46:39 -0500534 if (this->detectVarDeclarationWithoutScope(*statement)) {
535 return nullptr;
536 }
Brian Osman77ba8102021-01-12 17:15:30 -0500537
John Stiles23521a82021-03-02 17:02:51 -0500538 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
539 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500540}
541
542std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
543 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400544 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500545 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700546 if (!test) {
547 return nullptr;
548 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400549 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700550 if (!statement) {
551 return nullptr;
552 }
John Stilesb321a072021-02-25 16:24:19 -0500553 if (this->detectVarDeclarationWithoutScope(*statement)) {
554 return nullptr;
555 }
John Stiles23521a82021-03-02 17:02:51 -0500556 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
557 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700558}
559
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500560std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
561 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400562 auto iter = d.begin();
563 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
564 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700565 return nullptr;
566 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500567 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400568 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700569 return nullptr;
570 }
John Stilesea5822e2021-02-26 11:18:20 -0500571 if (this->detectVarDeclarationWithoutScope(*statement)) {
572 return nullptr;
573 }
John Stiles23521a82021-03-02 17:02:51 -0500574 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700575}
576
Ethan Nicholascfefec02021-02-09 15:22:57 -0500577std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
578 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
579
580 auto iter = s.begin();
581 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
582 if (!value) {
583 return nullptr;
584 }
585 AutoSymbolTable table(this);
586 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500587 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500588 for (; iter != s.end(); ++iter) {
589 const ASTNode& c = *iter;
590 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
591 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
592 auto childIter = c.begin();
593 if (*childIter) {
594 caseValue = this->convertExpression(*childIter);
595 if (!caseValue) {
596 return nullptr;
597 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500598 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400599 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500600
John Stilesbf16b6c2021-03-12 19:24:31 -0500601 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400602 for (; childIter != c.end(); ++childIter) {
603 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500604 if (!converted) {
605 return nullptr;
606 }
607 statements.push_back(std::move(converted));
608 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500609
610 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500611 }
John Stiles23521a82021-03-02 17:02:51 -0500612 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
613 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500614}
615
Ethan Nicholasfc994162019-06-06 10:04:27 -0400616std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
617 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700618 if (!e) {
619 return nullptr;
620 }
John Stiles3e5871c2021-02-25 20:52:03 -0500621 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700622}
623
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500624std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
625 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500626 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500627}
628
629std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
630 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
631 if (r.begin() != r.end()) {
632 std::unique_ptr<Expression> value = this->convertExpression(*r.begin());
633 if (!value) {
634 return nullptr;
635 }
636 return this->convertReturn(r.fOffset, std::move(value));
637 } else {
638 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700639 }
640}
641
Ethan Nicholasfc994162019-06-06 10:04:27 -0400642std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
643 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500644 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700645}
646
Ethan Nicholasfc994162019-06-06 10:04:27 -0400647std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
648 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500649 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650}
651
Ethan Nicholasfc994162019-06-06 10:04:27 -0400652std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
653 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400654 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500655 this->errorReporter().error(d.fOffset,
656 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500657 return nullptr;
658 }
John Stilesa0c04d62021-03-11 23:07:24 -0500659 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700660}
661
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500662std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400663 Layout invokeLayout;
664 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
John Stiles586df952020-11-12 18:27:13 -0500665 const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>(
666 /*offset=*/-1,
John Stilesf2872e62021-05-04 11:38:43 -0400667 this->modifiersPool().add(invokeModifiers),
John Stiles586df952020-11-12 18:27:13 -0500668 "_invoke",
669 std::vector<const Variable*>(),
John Stiles54e7c052021-01-11 14:22:36 -0500670 fContext.fTypes.fVoid.get(),
John Stiles586df952020-11-12 18:27:13 -0500671 fIsBuiltinCode));
John Stiles0d07e142020-12-30 18:03:09 -0500672 auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl, fIsBuiltinCode,
673 std::move(main));
674 invokeDecl->setDefinition(invokeDef.get());
675 fProgramElements->push_back(std::move(invokeDef));
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400676
677 using namespace SkSL::dsl;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400678 DSLGlobalVar loopIdx("sk_InvocationID");
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400679 std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1,
680 "EndPrimitive");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400681 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400682
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400683 std::unique_ptr<Statement> block = DSLBlock(
684 For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock(
685 DSLFunction(invokeDecl)(),
686 DSLExpression(std::move(endPrimitive))({})
687 ))
688 ).release();
689 return std::unique_ptr<Block>(&block.release()->as<Block>());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400690}
691
Robert Phillipsfe8da172018-01-24 14:52:02 +0000692std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
John Stilesd45ebbc2021-05-18 16:54:36 -0400693 using namespace SkSL::dsl;
694 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
695
Brian Osman88cda172020-10-09 12:05:16 -0400696 const Variable* skPerVertex = nullptr;
697 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400698 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
699 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400700 }
701
Brian Osman88cda172020-10-09 12:05:16 -0400702 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500703 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
John Stilesd45ebbc2021-05-18 16:54:36 -0400704 return VariableReference::Make(/*offset=*/-1, var);
John Stiles5acf6a82020-11-10 22:38:01 -0500705 };
706 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
John Stiles06d600f2021-03-08 09:18:21 -0500707 return FieldAccess::Make(fContext, Ref(var), idx,
708 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
John Stiles5acf6a82020-11-10 22:38:01 -0500709 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400710 auto Pos = [&]() -> DSLExpression {
711 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
712 FieldAccess::OwnerKind::kAnonymousInterfaceBlock));
John Stiles5acf6a82020-11-10 22:38:01 -0500713 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400714 auto Adjust = [&]() -> DSLExpression {
715 return DSLExpression(fRTAdjustInterfaceBlock
716 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
717 : Ref(fRTAdjust));
John Stiles5acf6a82020-11-10 22:38:01 -0500718 };
John Stiles750109b2020-10-30 13:45:46 -0400719
John Stilesd45ebbc2021-05-18 16:54:36 -0400720 return DSLStatement(
721 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
722 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
723 0,
724 Pos().w())
725 ).release();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000726}
727
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400728void IRGenerator::CheckModifiers(const Context& context,
729 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500730 const Modifiers& modifiers,
731 int permittedModifierFlags,
732 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400733 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
734 { Modifiers::kConst_Flag, "const" },
735 { Modifiers::kIn_Flag, "in" },
736 { Modifiers::kOut_Flag, "out" },
737 { Modifiers::kUniform_Flag, "uniform" },
738 { Modifiers::kFlat_Flag, "flat" },
739 { Modifiers::kNoPerspective_Flag, "noperspective" },
740 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
741 { Modifiers::kInline_Flag, "inline" },
742 { Modifiers::kNoInline_Flag, "noinline" },
743 { Modifiers::kHighp_Flag, "highp" },
744 { Modifiers::kMediump_Flag, "mediump" },
745 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400746 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500747 };
748
Brian Osmanb5de6be2021-08-10 15:16:34 -0400749 int modifierFlags = modifiers.fFlags;
750 for (const auto& f : kModifierFlags) {
751 if (modifierFlags & f.flag) {
752 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas8d116542021-08-11 13:27:16 -0400753 context.errors().error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400754 }
755 modifierFlags &= ~f.flag;
756 }
757 }
758 SkASSERT(modifierFlags == 0);
759
760 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
761 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
762 { Layout::kPushConstant_Flag, "push_constant"},
763 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
764 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
765 { Layout::kLocation_Flag, "location"},
766 { Layout::kOffset_Flag, "offset"},
767 { Layout::kBinding_Flag, "binding"},
768 { Layout::kIndex_Flag, "index"},
769 { Layout::kSet_Flag, "set"},
770 { Layout::kBuiltin_Flag, "builtin"},
771 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
772 { Layout::kPrimitive_Flag, "primitive-type"},
773 { Layout::kMaxVertices_Flag, "max_vertices"},
774 { Layout::kInvocations_Flag, "invocations"},
775 };
Brian Osmana654faa2021-02-26 11:52:59 -0500776
777 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400778 for (const auto& lf : kLayoutFlags) {
779 if (layoutFlags & lf.flag) {
780 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas8d116542021-08-11 13:27:16 -0400781 context.errors().error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400782 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500783 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400784 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500785 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400786 }
Brian Osmana654faa2021-02-26 11:52:59 -0500787 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400788}
789
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400790std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl,
791 std::unique_ptr<Block> body) {
Ethan Nicholase2c05042021-02-03 10:27:22 -0500792 class Finalizer : public ProgramWriter {
793 public:
794 Finalizer(IRGenerator* irGenerator, const FunctionDeclaration* function)
795 : fIRGenerator(irGenerator)
796 , fFunction(function) {}
797
798 ~Finalizer() override {
799 SkASSERT(!fBreakableLevel);
800 SkASSERT(!fContinuableLevel);
John Stiles2dda50d2021-03-03 10:46:44 -0500801 }
802
803 bool functionReturnsValue() const {
John Stiles2558c462021-03-16 17:49:20 -0400804 return !fFunction->returnType().isVoid();
John Stiles2dda50d2021-03-03 10:46:44 -0500805 }
806
John Stiles441c1bc2021-03-19 15:55:36 -0400807 bool visitExpression(Expression& expr) override {
808 // Do not recurse into expressions.
809 return false;
810 }
811
Ethan Nicholase2c05042021-02-03 10:27:22 -0500812 bool visitStatement(Statement& stmt) override {
813 switch (stmt.kind()) {
814 case Statement::Kind::kReturn: {
815 // early returns from a vertex main function will bypass the sk_Position
816 // normalization, so SkASSERT that we aren't doing that. It is of course
817 // possible to fix this by adding a normalization before each return, but it
818 // will probably never actually be necessary.
John Stilesd1204642021-02-17 16:30:02 -0500819 SkASSERT(fIRGenerator->programKind() != ProgramKind::kVertex ||
Ethan Nicholase2c05042021-02-03 10:27:22 -0500820 !fIRGenerator->fRTAdjust ||
John Stilese8da4d22021-03-24 09:19:45 -0400821 !fFunction->isMain());
John Stiles2dda50d2021-03-03 10:46:44 -0500822
823 // Verify that the return statement matches the function's return type.
824 ReturnStatement& returnStmt = stmt.as<ReturnStatement>();
Ethan Nicholase2c05042021-02-03 10:27:22 -0500825 const Type& returnType = fFunction->returnType();
John Stiles2dda50d2021-03-03 10:46:44 -0500826 if (returnStmt.expression()) {
827 if (this->functionReturnsValue()) {
828 // Coerce return expression to the function's return type.
829 returnStmt.setExpression(fIRGenerator->coerce(
830 std::move(returnStmt.expression()), returnType));
Ethan Nicholase2c05042021-02-03 10:27:22 -0500831 } else {
John Stiles2dda50d2021-03-03 10:46:44 -0500832 // Returning something from a function with a void return type.
833 fIRGenerator->errorReporter().error(returnStmt.fOffset,
834 "may not return a value from a void function");
Ethan Nicholase2c05042021-02-03 10:27:22 -0500835 }
John Stiles2dda50d2021-03-03 10:46:44 -0500836 } else {
837 if (this->functionReturnsValue()) {
838 // Returning nothing from a function with a non-void return type.
839 fIRGenerator->errorReporter().error(returnStmt.fOffset,
840 "expected function to return '" + returnType.displayName() + "'");
841 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500842 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500843 break;
844 }
845 case Statement::Kind::kDo:
846 case Statement::Kind::kFor: {
847 ++fBreakableLevel;
848 ++fContinuableLevel;
849 bool result = INHERITED::visitStatement(stmt);
850 --fContinuableLevel;
851 --fBreakableLevel;
852 return result;
853 }
854 case Statement::Kind::kSwitch: {
855 ++fBreakableLevel;
856 bool result = INHERITED::visitStatement(stmt);
857 --fBreakableLevel;
858 return result;
859 }
860 case Statement::Kind::kBreak:
861 if (!fBreakableLevel) {
862 fIRGenerator->errorReporter().error(stmt.fOffset,
863 "break statement must be inside a loop or switch");
864 }
865 break;
866 case Statement::Kind::kContinue:
867 if (!fContinuableLevel) {
868 fIRGenerator->errorReporter().error(stmt.fOffset,
869 "continue statement must be inside a loop");
870 }
871 break;
872 default:
873 break;
874 }
875 return INHERITED::visitStatement(stmt);
876 }
877
878 private:
879 IRGenerator* fIRGenerator;
880 const FunctionDeclaration* fFunction;
881 // how deeply nested we are in breakable constructs (for, do, switch).
882 int fBreakableLevel = 0;
883 // how deeply nested we are in continuable constructs (for, do).
884 int fContinuableLevel = 0;
885
886 using INHERITED = ProgramWriter;
887 };
John Stilesb3dcbb12021-03-04 16:00:20 -0500888
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400889 bool isMain = funcDecl.isMain();
890 bool needInvocationIDWorkaround = fInvocations != -1 && isMain &&
891 !this->caps().gsInvocationsSupport();
892 if (needInvocationIDWorkaround) {
893 body = this->applyInvocationIDWorkaround(std::move(body));
894 }
895 if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) {
896 body->children().push_back(this->getNormalizeSkPositionCode());
897 }
898
John Stiles958f4b52021-03-17 16:39:49 -0400899 Finalizer finalizer{this, &funcDecl};
900 finalizer.visitStatement(*body);
John Stilesb3dcbb12021-03-04 16:00:20 -0500901
John Stiles958f4b52021-03-17 16:39:49 -0400902 if (Analysis::CanExitWithoutReturningValue(funcDecl, *body)) {
903 this->errorReporter().error(funcDecl.fOffset, "function '" + funcDecl.name() +
904 "' can exit without returning a value");
John Stilesb3dcbb12021-03-04 16:00:20 -0500905 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400906 return body;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500907}
908
Ethan Nicholasfc994162019-06-06 10:04:27 -0400909void IRGenerator::convertFunction(const ASTNode& f) {
John Stilesb2705a12021-02-19 10:55:14 -0500910 SkASSERT(fReferencedIntrinsics.empty());
911 SK_AT_SCOPE_EXIT(fReferencedIntrinsics.clear());
912
Ethan Nicholasfc994162019-06-06 10:04:27 -0400913 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400914 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400915 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400916 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700917 }
John Stilesb9af7232020-08-20 15:57:48 -0400918 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400919 std::vector<std::unique_ptr<Variable>> parameters;
920 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400921 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400922 const ASTNode& param = *(iter++);
923 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500924 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400925 auto paramIter = param.begin();
926 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700927 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400928 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700929 }
John Stilesd39aec92020-12-03 14:37:16 -0500930 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500931 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500932 if (!arraySize) {
933 return;
934 }
John Stilesa2179502020-12-03 14:37:57 -0500935 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700936 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400937
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400938 parameters.push_back(std::make_unique<Variable>(param.fOffset,
939 this->modifiersPool().add(pd.fModifiers),
940 pd.fName,
941 type,
942 fIsBuiltinCode,
943 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700944 }
945
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400946 // Conservatively assume all user-defined functions have side effects.
947 Modifiers declModifiers = funcData.fModifiers;
948 if (!fIsBuiltinCode) {
949 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400950 }
951
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400952 if (fContext.fConfig->fSettings.fForceNoInline) {
953 // Apply the `noinline` modifier to every function. This allows us to test Runtime
954 // Effects without any inlining, even when the code is later added to a paint.
955 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
956 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700957 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400958
959 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
960 fContext,
961 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400962 f.fOffset,
963 this->modifiersPool().add(declModifiers),
964 funcData.fName,
965 std::move(parameters),
966 returnType,
967 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700968 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400969 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700970 }
John Stiles569249b2020-11-03 12:18:22 -0500971 if (iter == f.end()) {
972 // If there's no body, we've found a prototype.
973 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
974 fIsBuiltinCode));
975 } else {
976 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700977 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500978 for (const Variable* param : decl->parameters()) {
979 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700980 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400981 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700982 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400983 return;
984 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400985 body = this->finalizeFunction(*decl, std::move(body));
John Stiles607d36b2020-10-19 15:00:01 -0400986 auto result = std::make_unique<FunctionDefinition>(
987 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -0400988 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400989 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500990 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700991 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700992}
993
John Stilesdc75a972020-11-25 16:24:55 -0500994std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
995 SkASSERT(node.fKind == ASTNode::Kind::kType);
996
997 const Type* type = this->convertType(node);
998 if (!type) {
999 return nullptr;
1000 }
John Stilesc0c51062020-12-03 17:16:29 -05001001 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -05001002 this->errorReporter().error(node.fOffset,
1003 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -05001004 return nullptr;
1005 }
Brian Osman02bc5222021-01-28 11:00:20 -05001006 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
1007 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -05001008 return std::make_unique<StructDefinition>(node.fOffset, *type);
1009}
1010
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001011std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -05001012 if (this->programKind() != ProgramKind::kFragment &&
1013 this->programKind() != ProgramKind::kVertex &&
1014 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -05001015 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -04001016 return nullptr;
1017 }
1018
Ethan Nicholasfc994162019-06-06 10:04:27 -04001019 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -05001020 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -07001021 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001022 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -07001023 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001024 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001025 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -04001026 {
1027 AutoSymbolTable table(this);
1028 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001029 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
1030 StatementArray decls = this->convertVarDeclarations(*(iter++),
1031 Variable::Storage::kInterfaceBlock);
1032 if (decls.empty()) {
1033 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001034 }
John Stiles869cdef2020-10-30 14:24:24 -04001035 for (const auto& decl : decls) {
1036 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles1d757782020-11-17 09:43:22 -05001037 if (vd.var().type().isOpaque()) {
John Stilesb30151e2021-01-11 16:13:08 -05001038 this->errorReporter().error(decl->fOffset,
1039 "opaque type '" + vd.var().type().name() +
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001040 "' is not permitted in an interface block");
John Stiles1d757782020-11-17 09:43:22 -05001041 }
John Stiles869cdef2020-10-30 14:24:24 -04001042 if (&vd.var() == fRTAdjust) {
1043 foundRTAdjust = true;
John Stiles54e7c052021-01-11 14:22:36 -05001044 SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4);
John Stiles869cdef2020-10-30 14:24:24 -04001045 fRTAdjustFieldIndex = fields.size();
1046 }
1047 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
1048 &vd.var().type()));
1049 if (vd.value()) {
John Stilesb30151e2021-01-11 16:13:08 -05001050 this->errorReporter().error(
1051 decl->fOffset,
1052 "initializers are not permitted on interface block fields");
John Stiles869cdef2020-10-30 14:24:24 -04001053 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001054 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001055 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001056 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001057 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -04001058 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -05001059 fields));
John Stilesd39aec02020-12-03 10:42:26 -05001060 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -05001061 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001062 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001063 if (size) {
Brian Osman4cf85072021-01-14 14:18:10 -05001064 // convertArraySize rejects unsized arrays. This is the one place we allow those, but
1065 // we've already checked for that, so this is verifying the other aspects (constant,
1066 // positive, not too large).
John Stiles80b02af2021-02-12 17:07:51 -05001067 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osman4cf85072021-01-14 14:18:10 -05001068 if (!arraySize) {
John Stilesd39aec02020-12-03 10:42:26 -05001069 return nullptr;
1070 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001071 } else {
John Stilesd39aec02020-12-03 10:42:26 -05001072 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001073 }
John Stilesa2179502020-12-03 14:37:57 -05001074 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001075 }
Brian Osman5bf3e202020-10-13 10:34:18 -04001076 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -04001077 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -04001078 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001079 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001080 type,
Brian Osman3887a012020-09-30 13:22:27 -04001081 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001082 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001083 if (foundRTAdjust) {
1084 fRTAdjustInterfaceBlock = var;
1085 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001086 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -04001087 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001088 } else {
1089 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -04001090 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001091 }
1092 }
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001093 return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset,
1094 var,
Ethan Nicholas3533ff12021-08-02 12:53:29 -04001095 id.fTypeName,
1096 id.fInstanceName,
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001097 arraySize,
1098 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -07001099}
1100
John Stiles7bd70332020-11-30 17:04:09 -05001101void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
1102 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
1103 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -05001104 const Type* type = &stmt->as<VarDeclaration>().baseType();
1105 if (type->isStruct()) {
1106 auto [iter, wasInserted] = fDefinedStructs.insert(type);
1107 if (wasInserted) {
1108 fProgramElements->push_back(
1109 std::make_unique<StructDefinition>(decl.fOffset, *type));
1110 }
1111 }
Ethan Nicholas624a5292021-04-16 14:54:43 -04001112 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -05001113 }
1114}
1115
John Stilesb4b627e2020-11-13 15:55:27 -05001116bool IRGenerator::typeContainsPrivateFields(const Type& type) {
1117 // Checks for usage of private types, including fields inside a struct.
1118 if (type.isPrivate()) {
1119 return true;
1120 }
John Stilesc0c51062020-12-03 17:16:29 -05001121 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -05001122 for (const auto& f : type.fields()) {
1123 if (this->typeContainsPrivateFields(*f.fType)) {
1124 return true;
1125 }
1126 }
1127 }
1128 return false;
1129}
1130
Brian Osmand8070392020-09-09 15:50:02 -04001131const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001132 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -05001133 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -05001134 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -05001135 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -05001136 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001137 }
John Stiles6bef6a72020-12-02 14:26:04 -05001138 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -05001139 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -04001140 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -05001141 this->errorReporter().error(type.fOffset,
1142 "type '" + name + "' not allowed in this context");
1143 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -05001144 }
John Stiles6bef6a72020-12-02 14:26:04 -05001145 if (!fIsBuiltinCode && this->typeContainsPrivateFields(*result)) {
Brian Osman00fea5b2021-01-28 09:46:30 -05001146 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
John Stiles7bd70332020-11-30 17:04:09 -05001147 return nullptr;
1148 }
John Stiles6bef6a72020-12-02 14:26:04 -05001149 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -05001150 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -05001151 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -05001152 if (!arraySize) {
1153 return nullptr;
1154 }
John Stilesa2179502020-12-03 14:37:57 -05001155 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -05001156 }
John Stiles6bef6a72020-12-02 14:26:04 -05001157 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001158}
1159
Ethan Nicholasfc994162019-06-06 10:04:27 -04001160std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001161 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001162 case ASTNode::Kind::kBinary:
1163 return this->convertBinaryExpression(expr);
1164 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -05001165 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001166 case ASTNode::Kind::kCall:
1167 return this->convertCallExpression(expr);
1168 case ASTNode::Kind::kField:
1169 return this->convertFieldExpression(expr);
1170 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -05001171 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001172 case ASTNode::Kind::kIdentifier:
1173 return this->convertIdentifier(expr);
1174 case ASTNode::Kind::kIndex:
1175 return this->convertIndexExpression(expr);
1176 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -05001177 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001178 case ASTNode::Kind::kPostfix:
1179 return this->convertPostfixExpression(expr);
1180 case ASTNode::Kind::kPrefix:
1181 return this->convertPrefixExpression(expr);
1182 case ASTNode::Kind::kTernary:
1183 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001184 default:
John Stileseada7bc2021-02-02 16:29:32 -05001185 SkDEBUGFAILF("unsupported expression: %s\n", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001186 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001187 }
1188}
1189
Ethan Nicholas962dec42021-06-10 13:06:39 -04001190std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001191 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -07001192 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -05001193 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001194 return nullptr;
1195 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001196 switch (result->kind()) {
1197 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -07001198 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -04001199 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -07001200 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001201 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -07001202 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001203 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -04001204 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001205 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -07001206 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001207 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -04001208 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001209 const Modifiers& modifiers = var->modifiers();
1210 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -04001211 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -04001212 if (caps().canUseFragCoord()) {
1213 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001214 }
Brian Salomond8d85b92021-07-07 09:41:17 -04001215 break;
1216 case SK_CLOCKWISE_BUILTIN:
1217 fInputs.fUseFlipRTUniform = true;
1218 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001219 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001220 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -04001221 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001222 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001223 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001224 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -04001225 auto base = VariableReference::Make(offset, &field->owner(),
1226 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -05001227 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
1228 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001229 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001230 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001231 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001232 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001233 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001234 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001235 const ExternalFunction* r = &result->as<ExternalFunction>();
1236 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001237 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001238 default:
John Stilesf57207b2021-02-02 17:50:34 -05001239 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001240 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001241}
1242
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001243std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001244 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001245}
1246
Ethan Nicholas11d53972016-11-28 11:23:23 -05001247std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001248 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -05001249 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -07001250}
1251
Ethan Nicholasfc994162019-06-06 10:04:27 -04001252std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1253 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1254 auto iter = expression.begin();
1255 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001256 if (!left) {
1257 return nullptr;
1258 }
John Stiles0f464502020-11-20 12:52:22 -05001259 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001260 if (!right) {
1261 return nullptr;
1262 }
John Stiles23521a82021-03-02 17:02:51 -05001263 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
1264 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -07001265}
1266
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001267std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1268 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1269 auto iter = node.begin();
1270 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1271 if (!test) {
1272 return nullptr;
1273 }
1274 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1275 if (!ifTrue) {
1276 return nullptr;
1277 }
1278 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1279 if (!ifFalse) {
1280 return nullptr;
1281 }
John Stiles23521a82021-03-02 17:02:51 -05001282 return TernaryExpression::Convert(fContext, std::move(test),
1283 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001284}
1285
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001286void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001287 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001288 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001289
1290 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1291 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001292 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1293 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001294 std::sort(intrinsics.begin(), intrinsics.end(),
1295 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001296 if (a->isBuiltin() != b->isBuiltin()) {
1297 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001298 }
1299 if (a->fOffset != b->fOffset) {
1300 return a->fOffset < b->fOffset;
1301 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001302 if (a->name() != b->name()) {
1303 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001304 }
1305 return a->description() < b->description();
1306 });
1307 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001308 this->copyIntrinsicIfNeeded(*f);
1309 }
John Stiles607d36b2020-10-19 15:00:01 -04001310
Brian Osman0006ad02020-11-18 15:38:39 -05001311 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001312 }
1313}
1314
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001315std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001316 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001317 ExpressionArray arguments) {
Brian Osmaneb0f29d2021-08-04 11:34:16 -04001318 if (function.intrinsicKind() == k_sample_IntrinsicKind && arguments.size() >= 1 &&
1319 arguments[0]->type().isEffectChild()) {
1320 // Translate old-style sample(child, ...) calls into new-style child(...) IR
1321 SkASSERT(arguments[0]->is<VariableReference>());
1322 const Variable& child = *arguments[0]->as<VariableReference>().variable();
1323 ExpressionArray argumentsWithoutChild;
1324 for (size_t i = 1; i < arguments.size(); i++) {
1325 argumentsWithoutChild.push_back(std::move(arguments[i]));
1326 }
1327 return ChildCall::Convert(fContext, offset, child, std::move(argumentsWithoutChild));
1328 }
1329
Ethan Nicholased84b732020-10-08 11:45:44 -04001330 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001331 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001332 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001333 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001334 if (function.definition()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001335 fReferencedIntrinsics.insert(&function);
1336 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001337 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001338 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001339 }
1340 }
John Stilesea9ab822020-08-31 09:55:04 -04001341
John Stilescd7ba502021-03-19 10:54:59 -04001342 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001343}
1344
1345/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001346 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001347 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1348 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001349 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001350CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001351 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001352 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1353 return CoercionCost::Impossible();
1354 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001355 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001356 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001357 }
John Stilesfa889112020-10-12 19:03:43 -04001358 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001359 const Type* ignored;
1360 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001361 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001362 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001363 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001364 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001365 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001366 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001367 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001368}
1369
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001370std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001371 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001372 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001373 switch (functionValue->kind()) {
1374 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001375 return Constructor::Convert(fContext,
1376 offset,
1377 functionValue->as<TypeReference>().value(),
1378 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001379 case Expression::Kind::kExternalFunctionReference: {
1380 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1381 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001382 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001383 this->errorReporter().error(offset, "external function expected " +
1384 to_string(count) + " arguments, but found " +
1385 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001386 return nullptr;
1387 }
1388 static constexpr int PARAMETER_MAX = 16;
1389 SkASSERT(count < PARAMETER_MAX);
1390 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001391 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001392 for (int i = 0; i < count; ++i) {
1393 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1394 if (!arguments[i]) {
1395 return nullptr;
1396 }
1397 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001398 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001399 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001400 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001401 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001402 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001403 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001404 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001405 if (functions.size() > 1) {
1406 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001407 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001408 if (cost < bestCost) {
1409 bestCost = cost;
1410 best = f;
1411 }
1412 }
1413 if (best) {
1414 return this->call(offset, *best, std::move(arguments));
1415 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001416 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001417 String separator;
1418 for (size_t i = 0; i < arguments.size(); i++) {
1419 msg += separator;
1420 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001421 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001422 }
1423 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001424 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001425 return nullptr;
1426 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001427 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001428 }
Brian Osmaneb0f29d2021-08-04 11:34:16 -04001429 case Expression::Kind::kVariableReference: {
1430 if (!functionValue->type().isEffectChild()) {
1431 this->errorReporter().error(offset, "not a function");
1432 return nullptr;
1433 }
1434 return ChildCall::Convert(fContext,
1435 offset,
1436 *functionValue->as<VariableReference>().variable(),
1437 std::move(arguments));
1438 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001439 default:
John Stilesb30151e2021-01-11 16:13:08 -05001440 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001441 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001442 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001443}
1444
Ethan Nicholasfc994162019-06-06 10:04:27 -04001445std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1446 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1447 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001448 if (!base) {
1449 return nullptr;
1450 }
John Stiles23521a82021-03-02 17:02:51 -05001451 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001452}
1453
ethannicholasb3058bd2016-07-01 08:22:01 -07001454std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001455 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001456 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001457}
1458
Ethan Nicholasfc994162019-06-06 10:04:27 -04001459std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1460 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1461 auto iter = index.begin();
1462 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001463 if (!base) {
1464 return nullptr;
1465 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001466 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001467 if (base->is<TypeReference>()) {
1468 this->errorReporter().error(index.fOffset, "array must have a size");
1469 } else {
1470 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1471 }
Brian Osman4cf85072021-01-14 14:18:10 -05001472 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001473 }
1474 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1475 if (!converted) {
1476 return nullptr;
1477 }
Ethan Nicholas251e6342021-07-20 13:16:19 -04001478 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base),
1479 std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001480}
1481
Ethan Nicholasfc994162019-06-06 10:04:27 -04001482std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1483 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1484 auto iter = callNode.begin();
1485 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1486 if (!base) {
1487 return nullptr;
1488 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001489 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001490 for (; iter != callNode.end(); ++iter) {
1491 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1492 if (!converted) {
1493 return nullptr;
1494 }
1495 arguments.push_back(std::move(converted));
1496 }
1497 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1498}
1499
1500std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1501 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1502 if (!base) {
1503 return nullptr;
1504 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001505 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001506 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001507 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001508 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001509 }
John Stiles06d600f2021-03-08 09:18:21 -05001510 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001511}
1512
1513std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001514 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001515 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1516 if (!base) {
1517 return nullptr;
1518 }
John Stiles23521a82021-03-02 17:02:51 -05001519 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001520}
1521
1522void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001523 switch (expr.kind()) {
John Stilesdc435fa2020-12-30 18:07:10 -05001524 case Expression::Kind::kFunctionCall: {
1525 const FunctionDeclaration& decl = expr.as<FunctionCall>().function();
1526 if (!decl.isBuiltin() && !decl.definition()) {
John Stilesb30151e2021-01-11 16:13:08 -05001527 this->errorReporter().error(expr.fOffset,
1528 "function '" + decl.description() + "' is not defined");
John Stilesdc435fa2020-12-30 18:07:10 -05001529 }
1530 break;
1531 }
John Stiles973009b2021-02-17 17:18:01 -05001532 case Expression::Kind::kFunctionReference:
1533 case Expression::Kind::kTypeReference:
1534 SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()");
1535 this->errorReporter().error(expr.fOffset, "invalid expression");
1536 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001537 default:
John Stiles54e7c052021-01-11 14:22:36 -05001538 if (expr.type() == *fContext.fTypes.fInvalid) {
John Stilesb30151e2021-01-11 16:13:08 -05001539 this->errorReporter().error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07001540 }
John Stiles973009b2021-02-17 17:18:01 -05001541 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001542 }
1543}
1544
Brian Osman9496fe52020-11-18 14:48:19 -05001545void IRGenerator::findAndDeclareBuiltinVariables() {
1546 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001547 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001548 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001549
Brian Osman9496fe52020-11-18 14:48:19 -05001550 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001551 // If this is the *first* time we've seen this builtin, findAndInclude will return
1552 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001553 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001554 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001555 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001556 }
1557 }
1558
John Stiles4d6310a2021-01-26 19:58:22 -05001559 bool visitProgramElement(const ProgramElement& pe) override {
1560 if (pe.is<FunctionDefinition>()) {
1561 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1562 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1563 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001564 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001565 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1566 fPreserveFragColor = true;
1567 }
1568 }
1569 return INHERITED::visitProgramElement(pe);
1570 }
1571
Brian Osman9496fe52020-11-18 14:48:19 -05001572 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001573 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001574 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001575 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001576 return INHERITED::visitExpression(e);
1577 }
1578
1579 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001580 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001581 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001582
Brian Osman9496fe52020-11-18 14:48:19 -05001583 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001584 using INHERITED::visitProgramElement;
1585 };
1586
Brian Osman9496fe52020-11-18 14:48:19 -05001587 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001588 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001589 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001590 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001591 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001592
John Stiles4d6310a2021-01-26 19:58:22 -05001593 if (scanner.fPreserveFragColor) {
1594 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001595 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001596 }
1597
John Stilesd1204642021-02-17 16:30:02 -05001598 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001599 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001600 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1601 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1602 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001603 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001604 break;
1605 default:
1606 break;
1607 }
1608
Brian Osman9496fe52020-11-18 14:48:19 -05001609 fSharedElements->insert(
1610 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001611}
1612
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001613void IRGenerator::start(const ParsedModule& base,
1614 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001615 std::vector<std::unique_ptr<ProgramElement>>* elements,
1616 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001617 fProgramElements = elements;
1618 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001619 fSymbolTable = base.fSymbols;
1620 fIntrinsics = base.fIntrinsics.get();
1621 if (fIntrinsics) {
1622 fIntrinsics->resetAlreadyIncluded();
1623 }
1624 fIsBuiltinCode = isBuiltinCode;
1625
Brian Salomond8d85b92021-07-07 09:41:17 -04001626 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001627 fInvocations = -1;
1628 fRTAdjust = nullptr;
1629 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001630 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001631 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001632
John Stilesd1204642021-02-17 16:30:02 -05001633 if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) {
Brian Osman68c1d402020-10-12 16:36:38 -04001634 // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin.
1635 // If we're applying the workaround, then it's a plain global.
John Stilesc1a98b82021-02-24 13:35:02 -05001636 bool workaround = !this->caps().gsInvocationsSupport();
Brian Osman68c1d402020-10-12 16:36:38 -04001637 Modifiers m;
1638 if (!workaround) {
1639 m.fFlags = Modifiers::kIn_Flag;
1640 m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN;
1641 }
John Stilesf2872e62021-05-04 11:38:43 -04001642 auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m),
John Stilese67bd132021-03-19 18:39:25 -04001643 "sk_InvocationID", fContext.fTypes.fInt.get(),
1644 /*builtin=*/false, Variable::Storage::kGlobal);
1645 auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(),
1646 /*arraySize=*/0, /*value=*/nullptr);
Brian Osman68c1d402020-10-12 16:36:38 -04001647 fSymbolTable->add(std::move(var));
Ethan Nicholas624a5292021-04-16 14:54:43 -04001648 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl)));
Brian Osman68c1d402020-10-12 16:36:38 -04001649 }
1650
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001651 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001652 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001653 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001654 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001655 }
1656 }
John Stilesaecf8d52021-05-14 12:15:01 -04001657
John Stilesbb2ef922021-07-26 08:32:07 -04001658 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001659 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001660 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001661 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1662 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1663 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1664 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1665 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1666 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1667 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1668 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1669 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1670
1671 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1672 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1673 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1674 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1675 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1676 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1677
1678 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1679 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1680 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1681 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1682 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1683 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001684
1685 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1686 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1687 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1688 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001689
1690 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1691 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1692 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1693 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1694
1695 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1696 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1697 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1698 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001699 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001700}
Brian Osman88cda172020-10-09 12:05:16 -04001701
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001702IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001703 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001704 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001705 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001706 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001707
Brian Osmanea485e52021-01-15 13:20:32 -05001708 // Do a pass looking for dangling FunctionReference or TypeReference expressions
Brian Osmanc95d3a12020-09-09 10:56:27 -04001709 class FindIllegalExpressions : public ProgramVisitor {
1710 public:
1711 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
1712
1713 bool visitExpression(const Expression& e) override {
1714 fGenerator->checkValid(e);
1715 return INHERITED::visitExpression(e);
1716 }
1717
1718 IRGenerator* fGenerator;
1719 using INHERITED = ProgramVisitor;
1720 using INHERITED::visitProgramElement;
1721 };
1722 for (const auto& pe : *fProgramElements) {
1723 FindIllegalExpressions{this}.visitProgramElement(*pe);
1724 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001725
Brian Osmanea485e52021-01-15 13:20:32 -05001726 // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the
1727 // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic
1728 // assumes that all loops meet the criteria of Section 4, and if they don't, could crash.
1729 if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) {
1730 for (const auto& pe : *fProgramElements) {
1731 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
1732 }
1733 }
1734
Brian Osman7da06572021-07-21 15:19:34 -04001735 if (this->strictES2Mode()) {
1736 Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter());
1737 }
1738
John Stilesf2872e62021-05-04 11:38:43 -04001739 return IRBundle{std::move(*fProgramElements),
1740 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001741 std::move(fSymbolTable),
1742 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001743}
1744
1745IRGenerator::IRBundle IRGenerator::convertProgram(
1746 const ParsedModule& base,
1747 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001748 skstd::string_view text) {
1749 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001750 fFile = parser.compilationUnit();
1751 if (this->errorReporter().errorCount() == 0) {
1752 SkASSERT(fFile);
1753 for (const auto& decl : fFile->root()) {
1754 switch (decl.fKind) {
1755 case ASTNode::Kind::kVarDeclarations:
1756 this->convertGlobalVarDeclarations(decl);
1757 break;
1758
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001759 case ASTNode::Kind::kFunction:
1760 this->convertFunction(decl);
1761 break;
1762
1763 case ASTNode::Kind::kModifiers: {
1764 std::unique_ptr<ModifiersDeclaration> f =
1765 this->convertModifiersDeclaration(decl);
1766 if (f) {
1767 fProgramElements->push_back(std::move(f));
1768 }
1769 break;
1770 }
1771 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001772 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001773 if (i) {
1774 fProgramElements->push_back(std::move(i));
1775 }
1776 break;
1777 }
1778 case ASTNode::Kind::kExtension: {
1779 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001780 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001781 if (e) {
1782 fProgramElements->push_back(std::move(e));
1783 }
1784 break;
1785 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001786 case ASTNode::Kind::kType: {
1787 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1788 if (s) {
1789 fProgramElements->push_back(std::move(s));
1790 }
1791 break;
1792 }
1793 default:
1794 SkDEBUGFAILF("unsupported declaration: %s\n", decl.description().c_str());
1795 break;
1796 }
1797 }
1798 }
1799 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001800}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001801
John Stilesa6841be2020-08-06 14:11:56 -04001802} // namespace SkSL