blob: 79f9b26688f7b22738e620fb0371d61dba1bfb56 [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 Stiles7253a202021-08-23 09:24:33 -0400219 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500220}
221
Ethan Nicholas489e5522021-01-20 10:53:11 -0500222void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
223 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500224 if (this->strictES2Mode() && baseType->isArray()) {
225 this->errorReporter().error(offset, "array size must appear after variable name");
226 }
227
John Stiles3624aba2020-12-22 16:29:53 -0500228 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500229 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500230 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500231 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400232 }
Brian Osman8c264792021-07-01 16:41:27 -0400233 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
234 this->errorReporter().error(offset, "'in' variables may not have matrix type");
235 }
236 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
237 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500238 }
John Stilesbb2ef922021-07-26 08:32:07 -0400239 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400240 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400241 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400242 }
243 }
Brian Osman5c625822021-04-06 14:28:07 -0400244 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
245 this->errorReporter().error(
246 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
247 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400248 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400249 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500250 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500251 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400252 }
253 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500254 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500255 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400256 }
257 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500258 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400259 (t.columns() == 3 || t.columns() == 4);
260 };
John Stilesc0c51062020-12-03 17:16:29 -0500261 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400262 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500263 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500264 "'srgb_unpremul' is only permitted on half3, half4, "
265 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400266 }
267 }
John Stilesb806da42021-08-04 16:26:47 -0400268 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
269 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400270 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500271 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400272 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400273 }
Brian Osmana654faa2021-02-26 11:52:59 -0500274 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400275 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500276}
277
Ethan Nicholasbd974002021-02-22 16:20:06 -0500278std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400279 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500280 bool isArray,
281 std::unique_ptr<Expression> arraySize,
282 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500283 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500284 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400285 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500286 this->errorReporter().error(offset,
287 "out location=0, index=0 is reserved for sk_FragColor");
288 }
289 const Type* type = baseType;
290 int arraySizeValue = 0;
291 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500292 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400293 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500294 if (!arraySizeValue) {
295 return {};
296 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500297 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
298 }
John Stilesf2872e62021-05-04 11:38:43 -0400299 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
300 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500301}
302
303std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400304 std::unique_ptr<Expression> value,
305 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400306 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
307 std::move(value));
308 if (!varDecl) {
309 return nullptr;
310 }
311
312 // Detect the declaration of magical variables.
313 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
314 // Silently ignore duplicate definitions of `sk_FragColor`.
315 const Symbol* symbol = (*fSymbolTable)[var->name()];
316 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500317 return nullptr;
318 }
John Stilese67bd132021-03-19 18:39:25 -0400319 } else if ((var->storage() == Variable::Storage::kGlobal ||
320 var->storage() == Variable::Storage::kInterfaceBlock) &&
321 var->name() == Compiler::RTADJUST_NAME) {
322 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
323 if (fRTAdjust) {
324 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500325 return nullptr;
326 }
John Stilese67bd132021-03-19 18:39:25 -0400327 if (var->type() != *fContext.fTypes.fFloat4) {
328 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500329 return nullptr;
330 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500331 fRTAdjust = var.get();
332 }
John Stilese67bd132021-03-19 18:39:25 -0400333
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400334 if (addToSymbolTable) {
335 fSymbolTable->add(std::move(var));
336 } else {
337 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
338 }
John Stilese67bd132021-03-19 18:39:25 -0400339 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500340}
341
342std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
343 const Modifiers& modifiers,
344 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400345 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500346 bool isArray,
347 std::unique_ptr<Expression> arraySize,
348 std::unique_ptr<Expression> value,
349 Variable::Storage storage) {
350 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
351 std::move(arraySize), storage);
352 if (!var) {
353 return nullptr;
354 }
355 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500356}
357
358StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
359 Variable::Storage storage) {
360 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
361 auto declarationsIter = decls.begin();
362 const Modifiers& modifiers = declarationsIter++->getModifiers();
363 const ASTNode& rawType = *(declarationsIter++);
364 const Type* baseType = this->convertType(rawType);
365 if (!baseType) {
366 return {};
367 }
John Stilesb806da42021-08-04 16:26:47 -0400368 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
369 decls.fOffset);
370 if (!baseType) {
371 return {};
372 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500373
374 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400375
376 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400377 for (; declarationsIter != decls.end(); ++declarationsIter) {
378 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400379 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500380 std::unique_ptr<Expression> arraySize;
381 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400382 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500383 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400384 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500385 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400386 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700387 }
John Stiles8f633ef2021-08-21 17:13:11 -0400388 arraySize = this->convertExpression(*iter++);
389 if (!arraySize) {
390 continue;
391 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700392 }
Brian Osman4cf85072021-01-14 14:18:10 -0500393 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400394 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700395 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400396 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700397 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700398 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500399 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
400 modifiers,
401 baseType,
402 varData.fName,
403 varData.fIsArray,
404 std::move(arraySize),
405 std::move(value),
406 storage);
407 if (varDeclStmt) {
408 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700409 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700410 }
Brian Osmanc0213602020-10-06 14:43:32 -0400411 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700412}
413
Ethan Nicholasfc994162019-06-06 10:04:27 -0400414std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500415 if (this->programKind() != ProgramKind::kFragment &&
416 this->programKind() != ProgramKind::kVertex &&
417 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500418 this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400419 return nullptr;
420 }
421
Ethan Nicholasfc994162019-06-06 10:04:27 -0400422 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
423 Modifiers modifiers = m.getModifiers();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400424 if (modifiers.fLayout.fInvocations != -1) {
John Stilesd1204642021-02-17 16:30:02 -0500425 if (this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500426 this->errorReporter().error(m.fOffset,
427 "'invocations' is only legal in geometry shaders");
Ethan Nicholasf06576b2019-04-03 15:45:25 -0400428 return nullptr;
429 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400430 fInvocations = modifiers.fLayout.fInvocations;
John Stilesc1a98b82021-02-24 13:35:02 -0500431 if (!this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400432 modifiers.fLayout.fInvocations = -1;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400433 if (modifiers.fLayout.description() == "") {
434 return nullptr;
435 }
436 }
437 }
John Stilesc1a98b82021-02-24 13:35:02 -0500438 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 &&
439 !this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400440 modifiers.fLayout.fMaxVertices *= fInvocations;
441 }
John Stilesf2872e62021-05-04 11:38:43 -0400442 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700443}
444
John Stilesad2319f2020-09-02 15:01:47 -0400445std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
446 SkASSERT(n.fKind == ASTNode::Kind::kIf);
447 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500448 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400449 if (!test) {
450 return nullptr;
451 }
452 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
453 if (!ifTrue) {
454 return nullptr;
455 }
John Stilescf3059e2021-02-25 14:27:02 -0500456 if (this->detectVarDeclarationWithoutScope(*ifTrue)) {
457 return nullptr;
458 }
John Stilesad2319f2020-09-02 15:01:47 -0400459 std::unique_ptr<Statement> ifFalse;
460 if (iter != n.end()) {
461 ifFalse = this->convertStatement(*(iter++));
462 if (!ifFalse) {
463 return nullptr;
464 }
John Stilescf3059e2021-02-25 14:27:02 -0500465 if (this->detectVarDeclarationWithoutScope(*ifFalse)) {
466 return nullptr;
467 }
John Stilesad2319f2020-09-02 15:01:47 -0400468 }
John Stiles5ede6e32021-01-11 13:00:49 -0500469 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500470 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
471 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400472}
473
Ethan Nicholasfc994162019-06-06 10:04:27 -0400474std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
475 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700476 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700477 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400478 auto iter = f.begin();
479 if (*iter) {
480 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700481 if (!initializer) {
482 return nullptr;
483 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700484 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400485 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700486 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400487 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500488 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700489 if (!test) {
490 return nullptr;
491 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700492 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400493 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700494 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400495 if (*iter) {
496 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700497 if (!next) {
498 return nullptr;
499 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700500 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400501 ++iter;
502 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700503 if (!statement) {
504 return nullptr;
505 }
John Stiles2b6ec982021-03-04 10:46:39 -0500506 if (this->detectVarDeclarationWithoutScope(*statement)) {
507 return nullptr;
508 }
Brian Osman77ba8102021-01-12 17:15:30 -0500509
John Stiles23521a82021-03-02 17:02:51 -0500510 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
511 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500512}
513
514std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
515 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400516 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500517 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700518 if (!test) {
519 return nullptr;
520 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400521 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700522 if (!statement) {
523 return nullptr;
524 }
John Stilesb321a072021-02-25 16:24:19 -0500525 if (this->detectVarDeclarationWithoutScope(*statement)) {
526 return nullptr;
527 }
John Stiles23521a82021-03-02 17:02:51 -0500528 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
529 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700530}
531
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500532std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
533 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400534 auto iter = d.begin();
535 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
536 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700537 return nullptr;
538 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500539 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400540 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700541 return nullptr;
542 }
John Stilesea5822e2021-02-26 11:18:20 -0500543 if (this->detectVarDeclarationWithoutScope(*statement)) {
544 return nullptr;
545 }
John Stiles23521a82021-03-02 17:02:51 -0500546 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700547}
548
Ethan Nicholascfefec02021-02-09 15:22:57 -0500549std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
550 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
551
552 auto iter = s.begin();
553 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
554 if (!value) {
555 return nullptr;
556 }
557 AutoSymbolTable table(this);
558 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500559 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500560 for (; iter != s.end(); ++iter) {
561 const ASTNode& c = *iter;
562 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
563 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
564 auto childIter = c.begin();
565 if (*childIter) {
566 caseValue = this->convertExpression(*childIter);
567 if (!caseValue) {
568 return nullptr;
569 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500570 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400571 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500572
John Stilesbf16b6c2021-03-12 19:24:31 -0500573 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400574 for (; childIter != c.end(); ++childIter) {
575 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500576 if (!converted) {
577 return nullptr;
578 }
579 statements.push_back(std::move(converted));
580 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500581
582 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500583 }
John Stiles23521a82021-03-02 17:02:51 -0500584 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
585 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500586}
587
Ethan Nicholasfc994162019-06-06 10:04:27 -0400588std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
589 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700590 if (!e) {
591 return nullptr;
592 }
John Stiles3e5871c2021-02-25 20:52:03 -0500593 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700594}
595
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500596std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
597 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500598 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500599}
600
601std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
602 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
603 if (r.begin() != r.end()) {
604 std::unique_ptr<Expression> value = this->convertExpression(*r.begin());
605 if (!value) {
606 return nullptr;
607 }
608 return this->convertReturn(r.fOffset, std::move(value));
609 } else {
610 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700611 }
612}
613
Ethan Nicholasfc994162019-06-06 10:04:27 -0400614std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
615 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500616 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700617}
618
Ethan Nicholasfc994162019-06-06 10:04:27 -0400619std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
620 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500621 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700622}
623
Ethan Nicholasfc994162019-06-06 10:04:27 -0400624std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
625 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400626 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500627 this->errorReporter().error(d.fOffset,
628 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500629 return nullptr;
630 }
John Stilesa0c04d62021-03-11 23:07:24 -0500631 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700632}
633
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500634std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400635 Layout invokeLayout;
636 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
John Stiles586df952020-11-12 18:27:13 -0500637 const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>(
638 /*offset=*/-1,
John Stilesf2872e62021-05-04 11:38:43 -0400639 this->modifiersPool().add(invokeModifiers),
John Stiles586df952020-11-12 18:27:13 -0500640 "_invoke",
641 std::vector<const Variable*>(),
John Stiles54e7c052021-01-11 14:22:36 -0500642 fContext.fTypes.fVoid.get(),
John Stiles586df952020-11-12 18:27:13 -0500643 fIsBuiltinCode));
John Stiles0d07e142020-12-30 18:03:09 -0500644 auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl, fIsBuiltinCode,
645 std::move(main));
646 invokeDecl->setDefinition(invokeDef.get());
647 fProgramElements->push_back(std::move(invokeDef));
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400648
649 using namespace SkSL::dsl;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400650 DSLGlobalVar loopIdx("sk_InvocationID");
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400651 std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1,
652 "EndPrimitive");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400653 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400654
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400655 std::unique_ptr<Statement> block = DSLBlock(
656 For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock(
657 DSLFunction(invokeDecl)(),
658 DSLExpression(std::move(endPrimitive))({})
659 ))
660 ).release();
661 return std::unique_ptr<Block>(&block.release()->as<Block>());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400662}
663
Robert Phillipsfe8da172018-01-24 14:52:02 +0000664std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
John Stilesd45ebbc2021-05-18 16:54:36 -0400665 using namespace SkSL::dsl;
666 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
667
Brian Osman88cda172020-10-09 12:05:16 -0400668 const Variable* skPerVertex = nullptr;
669 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400670 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
671 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400672 }
673
Brian Osman88cda172020-10-09 12:05:16 -0400674 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500675 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
John Stilesd45ebbc2021-05-18 16:54:36 -0400676 return VariableReference::Make(/*offset=*/-1, var);
John Stiles5acf6a82020-11-10 22:38:01 -0500677 };
678 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
John Stiles06d600f2021-03-08 09:18:21 -0500679 return FieldAccess::Make(fContext, Ref(var), idx,
680 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
John Stiles5acf6a82020-11-10 22:38:01 -0500681 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400682 auto Pos = [&]() -> DSLExpression {
683 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
684 FieldAccess::OwnerKind::kAnonymousInterfaceBlock));
John Stiles5acf6a82020-11-10 22:38:01 -0500685 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400686 auto Adjust = [&]() -> DSLExpression {
687 return DSLExpression(fRTAdjustInterfaceBlock
688 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
689 : Ref(fRTAdjust));
John Stiles5acf6a82020-11-10 22:38:01 -0500690 };
John Stiles750109b2020-10-30 13:45:46 -0400691
John Stilesd45ebbc2021-05-18 16:54:36 -0400692 return DSLStatement(
693 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
694 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
695 0,
696 Pos().w())
697 ).release();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000698}
699
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400700void IRGenerator::CheckModifiers(const Context& context,
701 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500702 const Modifiers& modifiers,
703 int permittedModifierFlags,
704 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400705 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
706 { Modifiers::kConst_Flag, "const" },
707 { Modifiers::kIn_Flag, "in" },
708 { Modifiers::kOut_Flag, "out" },
709 { Modifiers::kUniform_Flag, "uniform" },
710 { Modifiers::kFlat_Flag, "flat" },
711 { Modifiers::kNoPerspective_Flag, "noperspective" },
712 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
713 { Modifiers::kInline_Flag, "inline" },
714 { Modifiers::kNoInline_Flag, "noinline" },
715 { Modifiers::kHighp_Flag, "highp" },
716 { Modifiers::kMediump_Flag, "mediump" },
717 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400718 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500719 };
720
Brian Osmanb5de6be2021-08-10 15:16:34 -0400721 int modifierFlags = modifiers.fFlags;
722 for (const auto& f : kModifierFlags) {
723 if (modifierFlags & f.flag) {
724 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400725 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400726 }
727 modifierFlags &= ~f.flag;
728 }
729 }
730 SkASSERT(modifierFlags == 0);
731
732 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
733 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
734 { Layout::kPushConstant_Flag, "push_constant"},
735 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
736 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
737 { Layout::kLocation_Flag, "location"},
738 { Layout::kOffset_Flag, "offset"},
739 { Layout::kBinding_Flag, "binding"},
740 { Layout::kIndex_Flag, "index"},
741 { Layout::kSet_Flag, "set"},
742 { Layout::kBuiltin_Flag, "builtin"},
743 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
744 { Layout::kPrimitive_Flag, "primitive-type"},
745 { Layout::kMaxVertices_Flag, "max_vertices"},
746 { Layout::kInvocations_Flag, "invocations"},
747 };
Brian Osmana654faa2021-02-26 11:52:59 -0500748
749 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400750 for (const auto& lf : kLayoutFlags) {
751 if (layoutFlags & lf.flag) {
752 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400753 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400754 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500755 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400756 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500757 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400758 }
Brian Osmana654faa2021-02-26 11:52:59 -0500759 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400760}
761
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400762std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl,
763 std::unique_ptr<Block> body) {
Ethan Nicholase2c05042021-02-03 10:27:22 -0500764 class Finalizer : public ProgramWriter {
765 public:
766 Finalizer(IRGenerator* irGenerator, const FunctionDeclaration* function)
767 : fIRGenerator(irGenerator)
768 , fFunction(function) {}
769
770 ~Finalizer() override {
771 SkASSERT(!fBreakableLevel);
772 SkASSERT(!fContinuableLevel);
John Stiles2dda50d2021-03-03 10:46:44 -0500773 }
774
775 bool functionReturnsValue() const {
John Stiles2558c462021-03-16 17:49:20 -0400776 return !fFunction->returnType().isVoid();
John Stiles2dda50d2021-03-03 10:46:44 -0500777 }
778
John Stiles441c1bc2021-03-19 15:55:36 -0400779 bool visitExpression(Expression& expr) override {
780 // Do not recurse into expressions.
781 return false;
782 }
783
Ethan Nicholase2c05042021-02-03 10:27:22 -0500784 bool visitStatement(Statement& stmt) override {
785 switch (stmt.kind()) {
786 case Statement::Kind::kReturn: {
787 // early returns from a vertex main function will bypass the sk_Position
788 // normalization, so SkASSERT that we aren't doing that. It is of course
789 // possible to fix this by adding a normalization before each return, but it
790 // will probably never actually be necessary.
John Stilesd1204642021-02-17 16:30:02 -0500791 SkASSERT(fIRGenerator->programKind() != ProgramKind::kVertex ||
Ethan Nicholase2c05042021-02-03 10:27:22 -0500792 !fIRGenerator->fRTAdjust ||
John Stilese8da4d22021-03-24 09:19:45 -0400793 !fFunction->isMain());
John Stiles2dda50d2021-03-03 10:46:44 -0500794
795 // Verify that the return statement matches the function's return type.
796 ReturnStatement& returnStmt = stmt.as<ReturnStatement>();
Ethan Nicholase2c05042021-02-03 10:27:22 -0500797 const Type& returnType = fFunction->returnType();
John Stiles2dda50d2021-03-03 10:46:44 -0500798 if (returnStmt.expression()) {
799 if (this->functionReturnsValue()) {
800 // Coerce return expression to the function's return type.
801 returnStmt.setExpression(fIRGenerator->coerce(
802 std::move(returnStmt.expression()), returnType));
Ethan Nicholase2c05042021-02-03 10:27:22 -0500803 } else {
John Stiles2dda50d2021-03-03 10:46:44 -0500804 // Returning something from a function with a void return type.
805 fIRGenerator->errorReporter().error(returnStmt.fOffset,
806 "may not return a value from a void function");
Ethan Nicholase2c05042021-02-03 10:27:22 -0500807 }
John Stiles2dda50d2021-03-03 10:46:44 -0500808 } else {
809 if (this->functionReturnsValue()) {
810 // Returning nothing from a function with a non-void return type.
811 fIRGenerator->errorReporter().error(returnStmt.fOffset,
812 "expected function to return '" + returnType.displayName() + "'");
813 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500814 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500815 break;
816 }
817 case Statement::Kind::kDo:
818 case Statement::Kind::kFor: {
819 ++fBreakableLevel;
820 ++fContinuableLevel;
821 bool result = INHERITED::visitStatement(stmt);
822 --fContinuableLevel;
823 --fBreakableLevel;
824 return result;
825 }
826 case Statement::Kind::kSwitch: {
827 ++fBreakableLevel;
828 bool result = INHERITED::visitStatement(stmt);
829 --fBreakableLevel;
830 return result;
831 }
832 case Statement::Kind::kBreak:
833 if (!fBreakableLevel) {
834 fIRGenerator->errorReporter().error(stmt.fOffset,
835 "break statement must be inside a loop or switch");
836 }
837 break;
838 case Statement::Kind::kContinue:
839 if (!fContinuableLevel) {
840 fIRGenerator->errorReporter().error(stmt.fOffset,
841 "continue statement must be inside a loop");
842 }
843 break;
844 default:
845 break;
846 }
847 return INHERITED::visitStatement(stmt);
848 }
849
850 private:
851 IRGenerator* fIRGenerator;
852 const FunctionDeclaration* fFunction;
853 // how deeply nested we are in breakable constructs (for, do, switch).
854 int fBreakableLevel = 0;
855 // how deeply nested we are in continuable constructs (for, do).
856 int fContinuableLevel = 0;
857
858 using INHERITED = ProgramWriter;
859 };
John Stilesb3dcbb12021-03-04 16:00:20 -0500860
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400861 bool isMain = funcDecl.isMain();
862 bool needInvocationIDWorkaround = fInvocations != -1 && isMain &&
863 !this->caps().gsInvocationsSupport();
864 if (needInvocationIDWorkaround) {
865 body = this->applyInvocationIDWorkaround(std::move(body));
866 }
867 if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) {
868 body->children().push_back(this->getNormalizeSkPositionCode());
869 }
870
John Stiles958f4b52021-03-17 16:39:49 -0400871 Finalizer finalizer{this, &funcDecl};
872 finalizer.visitStatement(*body);
John Stilesb3dcbb12021-03-04 16:00:20 -0500873
John Stiles958f4b52021-03-17 16:39:49 -0400874 if (Analysis::CanExitWithoutReturningValue(funcDecl, *body)) {
875 this->errorReporter().error(funcDecl.fOffset, "function '" + funcDecl.name() +
876 "' can exit without returning a value");
John Stilesb3dcbb12021-03-04 16:00:20 -0500877 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400878 return body;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500879}
880
Ethan Nicholasfc994162019-06-06 10:04:27 -0400881void IRGenerator::convertFunction(const ASTNode& f) {
John Stilesb2705a12021-02-19 10:55:14 -0500882 SkASSERT(fReferencedIntrinsics.empty());
883 SK_AT_SCOPE_EXIT(fReferencedIntrinsics.clear());
884
Ethan Nicholasfc994162019-06-06 10:04:27 -0400885 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400886 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400887 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400888 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700889 }
John Stilesb9af7232020-08-20 15:57:48 -0400890 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400891 std::vector<std::unique_ptr<Variable>> parameters;
892 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400893 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400894 const ASTNode& param = *(iter++);
895 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500896 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400897 auto paramIter = param.begin();
898 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700899 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400900 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700901 }
John Stilesd39aec92020-12-03 14:37:16 -0500902 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500903 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500904 if (!arraySize) {
905 return;
906 }
John Stilesa2179502020-12-03 14:37:57 -0500907 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700908 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400909
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400910 parameters.push_back(std::make_unique<Variable>(param.fOffset,
911 this->modifiersPool().add(pd.fModifiers),
912 pd.fName,
913 type,
914 fIsBuiltinCode,
915 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700916 }
917
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400918 // Conservatively assume all user-defined functions have side effects.
919 Modifiers declModifiers = funcData.fModifiers;
920 if (!fIsBuiltinCode) {
921 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400922 }
923
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400924 if (fContext.fConfig->fSettings.fForceNoInline) {
925 // Apply the `noinline` modifier to every function. This allows us to test Runtime
926 // Effects without any inlining, even when the code is later added to a paint.
927 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
928 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700929 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400930
931 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
932 fContext,
933 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400934 f.fOffset,
935 this->modifiersPool().add(declModifiers),
936 funcData.fName,
937 std::move(parameters),
938 returnType,
939 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700940 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400941 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700942 }
John Stiles569249b2020-11-03 12:18:22 -0500943 if (iter == f.end()) {
944 // If there's no body, we've found a prototype.
945 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
946 fIsBuiltinCode));
947 } else {
948 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700949 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500950 for (const Variable* param : decl->parameters()) {
951 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700952 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400953 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700954 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400955 return;
956 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400957 body = this->finalizeFunction(*decl, std::move(body));
John Stiles607d36b2020-10-19 15:00:01 -0400958 auto result = std::make_unique<FunctionDefinition>(
959 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -0400960 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400961 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500962 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700963 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700964}
965
John Stilesdc75a972020-11-25 16:24:55 -0500966std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
967 SkASSERT(node.fKind == ASTNode::Kind::kType);
968
969 const Type* type = this->convertType(node);
970 if (!type) {
971 return nullptr;
972 }
John Stilesc0c51062020-12-03 17:16:29 -0500973 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500974 this->errorReporter().error(node.fOffset,
975 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500976 return nullptr;
977 }
Brian Osman02bc5222021-01-28 11:00:20 -0500978 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
979 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500980 return std::make_unique<StructDefinition>(node.fOffset, *type);
981}
982
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400983std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500984 if (this->programKind() != ProgramKind::kFragment &&
985 this->programKind() != ProgramKind::kVertex &&
986 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500987 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400988 return nullptr;
989 }
990
Ethan Nicholasfc994162019-06-06 10:04:27 -0400991 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500992 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700993 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400994 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700995 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000996 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400997 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400998 {
999 AutoSymbolTable table(this);
1000 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001001 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
1002 StatementArray decls = this->convertVarDeclarations(*(iter++),
1003 Variable::Storage::kInterfaceBlock);
1004 if (decls.empty()) {
1005 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001006 }
John Stiles869cdef2020-10-30 14:24:24 -04001007 for (const auto& decl : decls) {
1008 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles1d757782020-11-17 09:43:22 -05001009 if (vd.var().type().isOpaque()) {
John Stilesb30151e2021-01-11 16:13:08 -05001010 this->errorReporter().error(decl->fOffset,
1011 "opaque type '" + vd.var().type().name() +
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001012 "' is not permitted in an interface block");
John Stiles1d757782020-11-17 09:43:22 -05001013 }
John Stiles869cdef2020-10-30 14:24:24 -04001014 if (&vd.var() == fRTAdjust) {
1015 foundRTAdjust = true;
John Stiles54e7c052021-01-11 14:22:36 -05001016 SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4);
John Stiles869cdef2020-10-30 14:24:24 -04001017 fRTAdjustFieldIndex = fields.size();
1018 }
1019 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
1020 &vd.var().type()));
1021 if (vd.value()) {
John Stilesb30151e2021-01-11 16:13:08 -05001022 this->errorReporter().error(
1023 decl->fOffset,
1024 "initializers are not permitted on interface block fields");
John Stiles869cdef2020-10-30 14:24:24 -04001025 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001026 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001027 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001028 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001029 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -04001030 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -05001031 fields));
John Stilesd39aec02020-12-03 10:42:26 -05001032 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -05001033 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001034 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001035 if (size) {
Brian Osman4cf85072021-01-14 14:18:10 -05001036 // convertArraySize rejects unsized arrays. This is the one place we allow those, but
1037 // we've already checked for that, so this is verifying the other aspects (constant,
1038 // positive, not too large).
John Stiles80b02af2021-02-12 17:07:51 -05001039 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osman4cf85072021-01-14 14:18:10 -05001040 if (!arraySize) {
John Stilesd39aec02020-12-03 10:42:26 -05001041 return nullptr;
1042 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001043 } else {
John Stilesd39aec02020-12-03 10:42:26 -05001044 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001045 }
John Stilesa2179502020-12-03 14:37:57 -05001046 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001047 }
Brian Osman5bf3e202020-10-13 10:34:18 -04001048 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -04001049 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -04001050 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001051 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001052 type,
Brian Osman3887a012020-09-30 13:22:27 -04001053 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001054 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001055 if (foundRTAdjust) {
1056 fRTAdjustInterfaceBlock = var;
1057 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001058 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -04001059 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001060 } else {
1061 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -04001062 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001063 }
1064 }
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001065 return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset,
1066 var,
Ethan Nicholas3533ff12021-08-02 12:53:29 -04001067 id.fTypeName,
1068 id.fInstanceName,
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001069 arraySize,
1070 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -07001071}
1072
John Stiles7bd70332020-11-30 17:04:09 -05001073void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
1074 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
1075 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -05001076 const Type* type = &stmt->as<VarDeclaration>().baseType();
1077 if (type->isStruct()) {
1078 auto [iter, wasInserted] = fDefinedStructs.insert(type);
1079 if (wasInserted) {
1080 fProgramElements->push_back(
1081 std::make_unique<StructDefinition>(decl.fOffset, *type));
1082 }
1083 }
Ethan Nicholas624a5292021-04-16 14:54:43 -04001084 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -05001085 }
1086}
1087
John Stiles823c5042021-08-17 12:09:00 -04001088static bool type_contains_private_fields(const Type& type) {
John Stilesb4b627e2020-11-13 15:55:27 -05001089 // Checks for usage of private types, including fields inside a struct.
1090 if (type.isPrivate()) {
1091 return true;
1092 }
John Stilesc0c51062020-12-03 17:16:29 -05001093 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -05001094 for (const auto& f : type.fields()) {
John Stiles823c5042021-08-17 12:09:00 -04001095 if (type_contains_private_fields(*f.fType)) {
John Stilesb4b627e2020-11-13 15:55:27 -05001096 return true;
1097 }
1098 }
1099 }
1100 return false;
1101}
1102
Brian Osmand8070392020-09-09 15:50:02 -04001103const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001104 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -05001105 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -05001106 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -05001107 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -05001108 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001109 }
John Stiles6bef6a72020-12-02 14:26:04 -05001110 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -05001111 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -04001112 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -05001113 this->errorReporter().error(type.fOffset,
1114 "type '" + name + "' not allowed in this context");
1115 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -05001116 }
John Stiles823c5042021-08-17 12:09:00 -04001117 if (!fIsBuiltinCode) {
1118 if (type_contains_private_fields(*result)) {
1119 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
1120 return nullptr;
1121 }
1122 if (this->strictES2Mode() && !result->allowedInES2()) {
1123 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
1124 return nullptr;
1125 }
John Stiles7bd70332020-11-30 17:04:09 -05001126 }
John Stiles6bef6a72020-12-02 14:26:04 -05001127 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -05001128 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -05001129 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -05001130 if (!arraySize) {
1131 return nullptr;
1132 }
John Stilesa2179502020-12-03 14:37:57 -05001133 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -05001134 }
John Stiles6bef6a72020-12-02 14:26:04 -05001135 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001136}
1137
Ethan Nicholasfc994162019-06-06 10:04:27 -04001138std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001139 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001140 case ASTNode::Kind::kBinary:
1141 return this->convertBinaryExpression(expr);
1142 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -05001143 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001144 case ASTNode::Kind::kCall:
1145 return this->convertCallExpression(expr);
1146 case ASTNode::Kind::kField:
1147 return this->convertFieldExpression(expr);
1148 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -05001149 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001150 case ASTNode::Kind::kIdentifier:
1151 return this->convertIdentifier(expr);
1152 case ASTNode::Kind::kIndex:
1153 return this->convertIndexExpression(expr);
1154 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -05001155 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001156 case ASTNode::Kind::kPostfix:
1157 return this->convertPostfixExpression(expr);
1158 case ASTNode::Kind::kPrefix:
1159 return this->convertPrefixExpression(expr);
1160 case ASTNode::Kind::kTernary:
1161 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001162 default:
John Stileseada7bc2021-02-02 16:29:32 -05001163 SkDEBUGFAILF("unsupported expression: %s\n", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001164 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001165 }
1166}
1167
Ethan Nicholas962dec42021-06-10 13:06:39 -04001168std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001169 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -07001170 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -05001171 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001172 return nullptr;
1173 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001174 switch (result->kind()) {
1175 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -07001176 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -04001177 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -07001178 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001179 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -07001180 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001181 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -04001182 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001183 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -07001184 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001185 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -04001186 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001187 const Modifiers& modifiers = var->modifiers();
1188 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -04001189 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -04001190 if (caps().canUseFragCoord()) {
1191 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001192 }
Brian Salomond8d85b92021-07-07 09:41:17 -04001193 break;
1194 case SK_CLOCKWISE_BUILTIN:
1195 fInputs.fUseFlipRTUniform = true;
1196 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001197 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001198 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -04001199 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001200 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001201 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001202 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -04001203 auto base = VariableReference::Make(offset, &field->owner(),
1204 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -05001205 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
1206 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001207 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001208 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001209 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001210 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001211 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001212 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001213 const ExternalFunction* r = &result->as<ExternalFunction>();
1214 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001215 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001216 default:
John Stilesf57207b2021-02-02 17:50:34 -05001217 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001218 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001219}
1220
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001221std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001222 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001223}
1224
Ethan Nicholas11d53972016-11-28 11:23:23 -05001225std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001226 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -05001227 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -07001228}
1229
Ethan Nicholasfc994162019-06-06 10:04:27 -04001230std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1231 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1232 auto iter = expression.begin();
1233 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001234 if (!left) {
1235 return nullptr;
1236 }
John Stiles0f464502020-11-20 12:52:22 -05001237 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001238 if (!right) {
1239 return nullptr;
1240 }
John Stiles23521a82021-03-02 17:02:51 -05001241 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
1242 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -07001243}
1244
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001245std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1246 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1247 auto iter = node.begin();
1248 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1249 if (!test) {
1250 return nullptr;
1251 }
1252 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1253 if (!ifTrue) {
1254 return nullptr;
1255 }
1256 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1257 if (!ifFalse) {
1258 return nullptr;
1259 }
John Stiles23521a82021-03-02 17:02:51 -05001260 return TernaryExpression::Convert(fContext, std::move(test),
1261 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001262}
1263
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001264void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001265 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001266 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001267
1268 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1269 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001270 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1271 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001272 std::sort(intrinsics.begin(), intrinsics.end(),
1273 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001274 if (a->isBuiltin() != b->isBuiltin()) {
1275 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001276 }
1277 if (a->fOffset != b->fOffset) {
1278 return a->fOffset < b->fOffset;
1279 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001280 if (a->name() != b->name()) {
1281 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001282 }
1283 return a->description() < b->description();
1284 });
1285 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001286 this->copyIntrinsicIfNeeded(*f);
1287 }
John Stiles607d36b2020-10-19 15:00:01 -04001288
Brian Osman0006ad02020-11-18 15:38:39 -05001289 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001290 }
1291}
1292
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001293std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001294 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001295 ExpressionArray arguments) {
Brian Osmaneb0f29d2021-08-04 11:34:16 -04001296 if (function.intrinsicKind() == k_sample_IntrinsicKind && arguments.size() >= 1 &&
1297 arguments[0]->type().isEffectChild()) {
1298 // Translate old-style sample(child, ...) calls into new-style child(...) IR
1299 SkASSERT(arguments[0]->is<VariableReference>());
1300 const Variable& child = *arguments[0]->as<VariableReference>().variable();
1301 ExpressionArray argumentsWithoutChild;
1302 for (size_t i = 1; i < arguments.size(); i++) {
1303 argumentsWithoutChild.push_back(std::move(arguments[i]));
1304 }
1305 return ChildCall::Convert(fContext, offset, child, std::move(argumentsWithoutChild));
1306 }
1307
Ethan Nicholased84b732020-10-08 11:45:44 -04001308 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001309 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001310 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001311 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001312 if (function.definition()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001313 fReferencedIntrinsics.insert(&function);
1314 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001315 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001316 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001317 }
1318 }
John Stilesea9ab822020-08-31 09:55:04 -04001319
John Stilescd7ba502021-03-19 10:54:59 -04001320 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001321}
1322
1323/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001324 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001325 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1326 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001327 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001328CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001329 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001330 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1331 return CoercionCost::Impossible();
1332 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001333 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001334 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001335 }
John Stilesfa889112020-10-12 19:03:43 -04001336 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001337 const Type* ignored;
1338 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001339 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001340 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001341 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001342 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001343 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001344 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001345 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001346}
1347
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001348std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001349 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001350 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001351 switch (functionValue->kind()) {
1352 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001353 return Constructor::Convert(fContext,
1354 offset,
1355 functionValue->as<TypeReference>().value(),
1356 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001357 case Expression::Kind::kExternalFunctionReference: {
1358 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1359 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001360 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001361 this->errorReporter().error(offset, "external function expected " +
1362 to_string(count) + " arguments, but found " +
1363 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001364 return nullptr;
1365 }
1366 static constexpr int PARAMETER_MAX = 16;
1367 SkASSERT(count < PARAMETER_MAX);
1368 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001369 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001370 for (int i = 0; i < count; ++i) {
1371 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1372 if (!arguments[i]) {
1373 return nullptr;
1374 }
1375 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001376 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001377 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001378 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001379 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001380 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001381 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001382 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001383 if (functions.size() > 1) {
1384 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001385 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001386 if (cost < bestCost) {
1387 bestCost = cost;
1388 best = f;
1389 }
1390 }
1391 if (best) {
1392 return this->call(offset, *best, std::move(arguments));
1393 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001394 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001395 String separator;
1396 for (size_t i = 0; i < arguments.size(); i++) {
1397 msg += separator;
1398 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001399 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001400 }
1401 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001402 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001403 return nullptr;
1404 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001405 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001406 }
Brian Osmaneb0f29d2021-08-04 11:34:16 -04001407 case Expression::Kind::kVariableReference: {
1408 if (!functionValue->type().isEffectChild()) {
1409 this->errorReporter().error(offset, "not a function");
1410 return nullptr;
1411 }
1412 return ChildCall::Convert(fContext,
1413 offset,
1414 *functionValue->as<VariableReference>().variable(),
1415 std::move(arguments));
1416 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001417 default:
John Stilesb30151e2021-01-11 16:13:08 -05001418 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001419 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001420 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001421}
1422
Ethan Nicholasfc994162019-06-06 10:04:27 -04001423std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1424 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1425 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001426 if (!base) {
1427 return nullptr;
1428 }
John Stiles23521a82021-03-02 17:02:51 -05001429 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001430}
1431
ethannicholasb3058bd2016-07-01 08:22:01 -07001432std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001433 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001434 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001435}
1436
Ethan Nicholasfc994162019-06-06 10:04:27 -04001437std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1438 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1439 auto iter = index.begin();
1440 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001441 if (!base) {
1442 return nullptr;
1443 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001444 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001445 if (base->is<TypeReference>()) {
1446 this->errorReporter().error(index.fOffset, "array must have a size");
1447 } else {
1448 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1449 }
Brian Osman4cf85072021-01-14 14:18:10 -05001450 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001451 }
1452 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1453 if (!converted) {
1454 return nullptr;
1455 }
Ethan Nicholas251e6342021-07-20 13:16:19 -04001456 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base),
1457 std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001458}
1459
Ethan Nicholasfc994162019-06-06 10:04:27 -04001460std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1461 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1462 auto iter = callNode.begin();
1463 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1464 if (!base) {
1465 return nullptr;
1466 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001467 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001468 for (; iter != callNode.end(); ++iter) {
1469 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1470 if (!converted) {
1471 return nullptr;
1472 }
1473 arguments.push_back(std::move(converted));
1474 }
1475 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1476}
1477
1478std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1479 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1480 if (!base) {
1481 return nullptr;
1482 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001483 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001484 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001485 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001486 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001487 }
John Stiles06d600f2021-03-08 09:18:21 -05001488 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001489}
1490
1491std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001492 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001493 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1494 if (!base) {
1495 return nullptr;
1496 }
John Stiles23521a82021-03-02 17:02:51 -05001497 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001498}
1499
1500void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001501 switch (expr.kind()) {
John Stilesdc435fa2020-12-30 18:07:10 -05001502 case Expression::Kind::kFunctionCall: {
1503 const FunctionDeclaration& decl = expr.as<FunctionCall>().function();
1504 if (!decl.isBuiltin() && !decl.definition()) {
John Stilesb30151e2021-01-11 16:13:08 -05001505 this->errorReporter().error(expr.fOffset,
1506 "function '" + decl.description() + "' is not defined");
John Stilesdc435fa2020-12-30 18:07:10 -05001507 }
1508 break;
1509 }
John Stiles973009b2021-02-17 17:18:01 -05001510 case Expression::Kind::kFunctionReference:
1511 case Expression::Kind::kTypeReference:
1512 SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()");
1513 this->errorReporter().error(expr.fOffset, "invalid expression");
1514 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001515 default:
John Stiles54e7c052021-01-11 14:22:36 -05001516 if (expr.type() == *fContext.fTypes.fInvalid) {
John Stilesb30151e2021-01-11 16:13:08 -05001517 this->errorReporter().error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07001518 }
John Stiles973009b2021-02-17 17:18:01 -05001519 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001520 }
1521}
1522
Brian Osman9496fe52020-11-18 14:48:19 -05001523void IRGenerator::findAndDeclareBuiltinVariables() {
1524 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001525 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001526 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001527
Brian Osman9496fe52020-11-18 14:48:19 -05001528 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001529 // If this is the *first* time we've seen this builtin, findAndInclude will return
1530 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001531 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001532 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001533 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001534 }
1535 }
1536
John Stiles4d6310a2021-01-26 19:58:22 -05001537 bool visitProgramElement(const ProgramElement& pe) override {
1538 if (pe.is<FunctionDefinition>()) {
1539 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1540 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1541 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001542 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001543 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1544 fPreserveFragColor = true;
1545 }
1546 }
1547 return INHERITED::visitProgramElement(pe);
1548 }
1549
Brian Osman9496fe52020-11-18 14:48:19 -05001550 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001551 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001552 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001553 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001554 return INHERITED::visitExpression(e);
1555 }
1556
1557 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001558 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001559 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001560
Brian Osman9496fe52020-11-18 14:48:19 -05001561 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001562 using INHERITED::visitProgramElement;
1563 };
1564
Brian Osman9496fe52020-11-18 14:48:19 -05001565 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001566 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001567 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001568 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001569 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001570
John Stiles4d6310a2021-01-26 19:58:22 -05001571 if (scanner.fPreserveFragColor) {
1572 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001573 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001574 }
1575
John Stilesd1204642021-02-17 16:30:02 -05001576 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001577 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001578 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1579 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1580 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001581 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001582 break;
1583 default:
1584 break;
1585 }
1586
Brian Osman9496fe52020-11-18 14:48:19 -05001587 fSharedElements->insert(
1588 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001589}
1590
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001591void IRGenerator::start(const ParsedModule& base,
1592 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001593 std::vector<std::unique_ptr<ProgramElement>>* elements,
1594 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001595 fProgramElements = elements;
1596 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001597 fSymbolTable = base.fSymbols;
1598 fIntrinsics = base.fIntrinsics.get();
1599 if (fIntrinsics) {
1600 fIntrinsics->resetAlreadyIncluded();
1601 }
1602 fIsBuiltinCode = isBuiltinCode;
1603
Brian Salomond8d85b92021-07-07 09:41:17 -04001604 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001605 fInvocations = -1;
1606 fRTAdjust = nullptr;
1607 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001608 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001609 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001610
John Stilesd1204642021-02-17 16:30:02 -05001611 if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) {
Brian Osman68c1d402020-10-12 16:36:38 -04001612 // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin.
1613 // If we're applying the workaround, then it's a plain global.
John Stilesc1a98b82021-02-24 13:35:02 -05001614 bool workaround = !this->caps().gsInvocationsSupport();
Brian Osman68c1d402020-10-12 16:36:38 -04001615 Modifiers m;
1616 if (!workaround) {
1617 m.fFlags = Modifiers::kIn_Flag;
1618 m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN;
1619 }
John Stilesf2872e62021-05-04 11:38:43 -04001620 auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m),
John Stilese67bd132021-03-19 18:39:25 -04001621 "sk_InvocationID", fContext.fTypes.fInt.get(),
1622 /*builtin=*/false, Variable::Storage::kGlobal);
1623 auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(),
1624 /*arraySize=*/0, /*value=*/nullptr);
Brian Osman68c1d402020-10-12 16:36:38 -04001625 fSymbolTable->add(std::move(var));
Ethan Nicholas624a5292021-04-16 14:54:43 -04001626 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl)));
Brian Osman68c1d402020-10-12 16:36:38 -04001627 }
1628
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001629 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001630 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001631 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001632 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001633 }
1634 }
John Stilesaecf8d52021-05-14 12:15:01 -04001635
John Stilesbb2ef922021-07-26 08:32:07 -04001636 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001637 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001638 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001639 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1640 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1641 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1642 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1643 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1644 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1645 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1646 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1647 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1648
1649 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1650 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1651 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1652 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1653 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1654 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1655
1656 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1657 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1658 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1659 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1660 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1661 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001662
1663 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1664 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1665 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1666 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001667
1668 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1669 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1670 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1671 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1672
1673 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1674 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1675 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1676 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001677 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001678}
Brian Osman88cda172020-10-09 12:05:16 -04001679
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001680IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001681 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001682 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001683 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001684 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001685
Brian Osmanea485e52021-01-15 13:20:32 -05001686 // Do a pass looking for dangling FunctionReference or TypeReference expressions
Brian Osmanc95d3a12020-09-09 10:56:27 -04001687 class FindIllegalExpressions : public ProgramVisitor {
1688 public:
1689 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
1690
1691 bool visitExpression(const Expression& e) override {
1692 fGenerator->checkValid(e);
1693 return INHERITED::visitExpression(e);
1694 }
1695
1696 IRGenerator* fGenerator;
1697 using INHERITED = ProgramVisitor;
1698 using INHERITED::visitProgramElement;
1699 };
1700 for (const auto& pe : *fProgramElements) {
1701 FindIllegalExpressions{this}.visitProgramElement(*pe);
1702 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001703
Brian Osmanea485e52021-01-15 13:20:32 -05001704 // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the
1705 // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic
1706 // assumes that all loops meet the criteria of Section 4, and if they don't, could crash.
1707 if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) {
1708 for (const auto& pe : *fProgramElements) {
1709 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
1710 }
1711 }
1712
Brian Osman7da06572021-07-21 15:19:34 -04001713 if (this->strictES2Mode()) {
1714 Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter());
1715 }
1716
John Stilesf2872e62021-05-04 11:38:43 -04001717 return IRBundle{std::move(*fProgramElements),
1718 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001719 std::move(fSymbolTable),
1720 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001721}
1722
1723IRGenerator::IRBundle IRGenerator::convertProgram(
1724 const ParsedModule& base,
1725 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001726 skstd::string_view text) {
1727 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001728 fFile = parser.compilationUnit();
1729 if (this->errorReporter().errorCount() == 0) {
1730 SkASSERT(fFile);
1731 for (const auto& decl : fFile->root()) {
1732 switch (decl.fKind) {
1733 case ASTNode::Kind::kVarDeclarations:
1734 this->convertGlobalVarDeclarations(decl);
1735 break;
1736
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001737 case ASTNode::Kind::kFunction:
1738 this->convertFunction(decl);
1739 break;
1740
1741 case ASTNode::Kind::kModifiers: {
1742 std::unique_ptr<ModifiersDeclaration> f =
1743 this->convertModifiersDeclaration(decl);
1744 if (f) {
1745 fProgramElements->push_back(std::move(f));
1746 }
1747 break;
1748 }
1749 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001750 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001751 if (i) {
1752 fProgramElements->push_back(std::move(i));
1753 }
1754 break;
1755 }
1756 case ASTNode::Kind::kExtension: {
1757 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001758 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001759 if (e) {
1760 fProgramElements->push_back(std::move(e));
1761 }
1762 break;
1763 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001764 case ASTNode::Kind::kType: {
1765 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1766 if (s) {
1767 fProgramElements->push_back(std::move(s));
1768 }
1769 break;
1770 }
1771 default:
1772 SkDEBUGFAILF("unsupported declaration: %s\n", decl.description().c_str());
1773 break;
1774 }
1775 }
1776 }
1777 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001778}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001779
John Stilesa6841be2020-08-06 14:11:56 -04001780} // namespace SkSL