blob: e7a5dd9d7929e6252a498d2ea8bf7fa4f80085e7 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
10#include "limits.h"
John Stiles44e96be2020-08-31 13:16:04 -040011#include <iterator>
John Stilesfbd050b2020-08-03 13:21:46 -040012#include <memory>
Ethan Nicholasaf197692017-02-27 13:26:45 -050013#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070014
Ethan Nicholasdaed2592021-03-04 14:30:25 -050015#include "include/private/SkSLLayout.h"
John Stiles6bef6a72020-12-02 14:26:04 -050016#include "include/private/SkTArray.h"
Ethan Nicholasbe65cd52021-05-18 12:09:01 -040017#include "include/sksl/DSLCore.h"
John Stilesb2705a12021-02-19 10:55:14 -050018#include "src/core/SkScopeExit.h"
Ethan Nicholas6e0fa402020-08-20 14:08:23 -040019#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/SkSLCompiler.h"
John Stilesdc8ec312021-01-11 11:05:21 -050021#include "src/sksl/SkSLConstantFolder.h"
Brian Osman00185012021-02-04 16:07:11 -050022#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLParser.h"
Brian Osman3000d6b2020-07-31 15:57:28 -040024#include "src/sksl/SkSLUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLBinaryExpression.h"
26#include "src/sksl/ir/SkSLBoolLiteral.h"
27#include "src/sksl/ir/SkSLBreakStatement.h"
28#include "src/sksl/ir/SkSLConstructor.h"
29#include "src/sksl/ir/SkSLContinueStatement.h"
30#include "src/sksl/ir/SkSLDiscardStatement.h"
31#include "src/sksl/ir/SkSLDoStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040033#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050034#include "src/sksl/ir/SkSLExternalFunctionReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/sksl/ir/SkSLField.h"
36#include "src/sksl/ir/SkSLFieldAccess.h"
37#include "src/sksl/ir/SkSLFloatLiteral.h"
38#include "src/sksl/ir/SkSLForStatement.h"
39#include "src/sksl/ir/SkSLFunctionCall.h"
40#include "src/sksl/ir/SkSLFunctionDeclaration.h"
41#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050042#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLFunctionReference.h"
44#include "src/sksl/ir/SkSLIfStatement.h"
45#include "src/sksl/ir/SkSLIndexExpression.h"
46#include "src/sksl/ir/SkSLIntLiteral.h"
47#include "src/sksl/ir/SkSLInterfaceBlock.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060048#include "src/sksl/ir/SkSLNop.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/sksl/ir/SkSLPostfixExpression.h"
50#include "src/sksl/ir/SkSLPrefixExpression.h"
51#include "src/sksl/ir/SkSLReturnStatement.h"
52#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050053#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/sksl/ir/SkSLSwitchCase.h"
55#include "src/sksl/ir/SkSLSwitchStatement.h"
56#include "src/sksl/ir/SkSLSwizzle.h"
57#include "src/sksl/ir/SkSLTernaryExpression.h"
58#include "src/sksl/ir/SkSLUnresolvedFunction.h"
59#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050060#include "src/sksl/ir/SkSLVariable.h"
61#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070062
63namespace SkSL {
64
65class AutoSymbolTable {
66public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050067 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070068 : fIR(ir)
69 , fPrevious(fIR->fSymbolTable) {
70 fIR->pushSymbolTable();
71 }
72
73 ~AutoSymbolTable() {
74 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040075 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070076 }
77
78 IRGenerator* fIR;
79 std::shared_ptr<SymbolTable> fPrevious;
80};
81
John Stilesc1a98b82021-02-24 13:35:02 -050082IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040083 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050084
85void IRGenerator::pushSymbolTable() {
86 auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
87 fSymbolTable = std::move(childSymTable);
88}
89
90void IRGenerator::popSymbolTable() {
91 fSymbolTable = fSymbolTable->fParent;
92}
93
John Stilese3a91cf2021-01-26 10:13:58 -050094bool IRGenerator::detectVarDeclarationWithoutScope(const Statement& stmt) {
95 // Parsing an AST node containing a single variable declaration creates a lone VarDeclaration
96 // statement. An AST with multiple variable declarations creates an unscoped Block containing
97 // multiple VarDeclaration statements. We need to detect either case.
98 const Variable* var;
99 if (stmt.is<VarDeclaration>()) {
100 // The single-variable case. No blocks at all.
101 var = &stmt.as<VarDeclaration>().var();
102 } else if (stmt.is<Block>()) {
103 // The multiple-variable case: an unscoped, non-empty block...
104 const Block& block = stmt.as<Block>();
105 if (block.isScope() || block.children().empty()) {
106 return false;
107 }
108 // ... holding a variable declaration.
109 const Statement& innerStmt = *block.children().front();
110 if (!innerStmt.is<VarDeclaration>()) {
111 return false;
112 }
113 var = &innerStmt.as<VarDeclaration>().var();
114 } else {
115 // This statement wasn't a variable declaration. No problem.
116 return false;
117 }
118
119 // Report an error.
120 SkASSERT(var);
121 this->errorReporter().error(stmt.fOffset,
122 "variable '" + var->name() + "' must be created in a scope");
123 return true;
124}
125
Ethan Nicholas962dec42021-06-10 13:06:39 -0400126std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -0500127 if (this->programKind() != ProgramKind::kFragment &&
128 this->programKind() != ProgramKind::kVertex &&
129 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500130 this->errorReporter().error(offset, "extensions are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400131 return nullptr;
132 }
133
John Stilesfbd050b2020-08-03 13:21:46 -0400134 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700135}
136
John Stiles20c09f92021-03-12 09:25:04 -0500137std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700138 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400139 case ASTNode::Kind::kBlock:
140 return this->convertBlock(statement);
141 case ASTNode::Kind::kVarDeclarations:
142 return this->convertVarDeclarationStatement(statement);
143 case ASTNode::Kind::kIf:
144 return this->convertIf(statement);
145 case ASTNode::Kind::kFor:
146 return this->convertFor(statement);
147 case ASTNode::Kind::kWhile:
148 return this->convertWhile(statement);
149 case ASTNode::Kind::kDo:
150 return this->convertDo(statement);
151 case ASTNode::Kind::kSwitch:
152 return this->convertSwitch(statement);
153 case ASTNode::Kind::kReturn:
154 return this->convertReturn(statement);
155 case ASTNode::Kind::kBreak:
156 return this->convertBreak(statement);
157 case ASTNode::Kind::kContinue:
158 return this->convertContinue(statement);
159 case ASTNode::Kind::kDiscard:
160 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500161 case ASTNode::Kind::kType:
162 // TODO: add IRNode for struct definition inside a function
163 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400164 default:
165 // it's an expression
166 std::unique_ptr<Statement> result = this->convertExpressionStatement(statement);
John Stilesd1204642021-02-17 16:30:02 -0500167 if (fRTAdjust && this->programKind() == ProgramKind::kGeometry) {
John Stilese67bd132021-03-19 18:39:25 -0400168 SkASSERT(result->is<ExpressionStatement>());
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400169 Expression& expr = *result->as<ExpressionStatement>().expression();
John Stilese67bd132021-03-19 18:39:25 -0400170 if (expr.is<FunctionCall>()) {
John Stiles403a3632020-08-20 12:11:48 -0400171 FunctionCall& fc = expr.as<FunctionCall>();
Ethan Nicholased84b732020-10-08 11:45:44 -0400172 if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") {
John Stiles8f2a0cf2020-10-13 12:48:21 -0400173 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400174 statements.reserve_back(2);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000175 statements.push_back(getNormalizeSkPositionCode());
176 statements.push_back(std::move(result));
John Stilesbf16b6c2021-03-12 19:24:31 -0500177 return Block::Make(statement.fOffset, std::move(statements),
178 fSymbolTable, /*isScope=*/true);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000179 }
180 }
181 }
182 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700183 }
184}
185
Ethan Nicholasfc994162019-06-06 10:04:27 -0400186std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
187 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400189 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400190 for (const auto& child : block) {
191 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 if (!statement) {
193 return nullptr;
194 }
195 statements.push_back(std::move(statement));
196 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500197 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198}
199
Ethan Nicholasfc994162019-06-06 10:04:27 -0400200std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
201 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400202 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400203 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 return nullptr;
205 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500206 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700207}
208
John Stiles80b02af2021-02-12 17:07:51 -0500209int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500210 if (!s) {
211 this->errorReporter().error(offset, "array must have a size");
212 return 0;
213 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500214 auto size = this->convertExpression(s);
215 if (!size) {
216 return 0;
217 }
John Stiles7253a202021-08-23 09:24:33 -0400218 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500219}
220
Ethan Nicholas489e5522021-01-20 10:53:11 -0500221void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
222 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500223 if (this->strictES2Mode() && baseType->isArray()) {
224 this->errorReporter().error(offset, "array size must appear after variable name");
225 }
226
John Stiles3624aba2020-12-22 16:29:53 -0500227 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500228 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500229 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500230 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400231 }
Brian Osman8c264792021-07-01 16:41:27 -0400232 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
233 this->errorReporter().error(offset, "'in' variables may not have matrix type");
234 }
235 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
236 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500237 }
John Stilesbb2ef922021-07-26 08:32:07 -0400238 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400239 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400240 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400241 }
242 }
Brian Osman5c625822021-04-06 14:28:07 -0400243 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
244 this->errorReporter().error(
245 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
246 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400247 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400248 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500249 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500250 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400251 }
252 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500253 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500254 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400255 }
256 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500257 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400258 (t.columns() == 3 || t.columns() == 4);
259 };
John Stilesc0c51062020-12-03 17:16:29 -0500260 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400261 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500262 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500263 "'srgb_unpremul' is only permitted on half3, half4, "
264 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400265 }
266 }
John Stilesb806da42021-08-04 16:26:47 -0400267 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
268 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400269 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500270 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400271 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400272 }
Brian Osmana654faa2021-02-26 11:52:59 -0500273 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400274 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500275}
276
Ethan Nicholasbd974002021-02-22 16:20:06 -0500277std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400278 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500279 bool isArray,
280 std::unique_ptr<Expression> arraySize,
281 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500282 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500283 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400284 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500285 this->errorReporter().error(offset,
286 "out location=0, index=0 is reserved for sk_FragColor");
287 }
288 const Type* type = baseType;
289 int arraySizeValue = 0;
290 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500291 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400292 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500293 if (!arraySizeValue) {
294 return {};
295 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500296 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
297 }
John Stilesf2872e62021-05-04 11:38:43 -0400298 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
299 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500300}
301
302std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400303 std::unique_ptr<Expression> value,
304 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400305 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
306 std::move(value));
307 if (!varDecl) {
308 return nullptr;
309 }
310
311 // Detect the declaration of magical variables.
312 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
313 // Silently ignore duplicate definitions of `sk_FragColor`.
314 const Symbol* symbol = (*fSymbolTable)[var->name()];
315 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500316 return nullptr;
317 }
John Stilese67bd132021-03-19 18:39:25 -0400318 } else if ((var->storage() == Variable::Storage::kGlobal ||
319 var->storage() == Variable::Storage::kInterfaceBlock) &&
320 var->name() == Compiler::RTADJUST_NAME) {
321 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
322 if (fRTAdjust) {
323 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500324 return nullptr;
325 }
John Stilese67bd132021-03-19 18:39:25 -0400326 if (var->type() != *fContext.fTypes.fFloat4) {
327 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500328 return nullptr;
329 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500330 fRTAdjust = var.get();
331 }
John Stilese67bd132021-03-19 18:39:25 -0400332
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400333 if (addToSymbolTable) {
334 fSymbolTable->add(std::move(var));
335 } else {
336 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
337 }
John Stilese67bd132021-03-19 18:39:25 -0400338 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500339}
340
341std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
342 const Modifiers& modifiers,
343 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400344 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500345 bool isArray,
346 std::unique_ptr<Expression> arraySize,
347 std::unique_ptr<Expression> value,
348 Variable::Storage storage) {
349 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
350 std::move(arraySize), storage);
351 if (!var) {
352 return nullptr;
353 }
354 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500355}
356
357StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
358 Variable::Storage storage) {
359 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
360 auto declarationsIter = decls.begin();
361 const Modifiers& modifiers = declarationsIter++->getModifiers();
362 const ASTNode& rawType = *(declarationsIter++);
363 const Type* baseType = this->convertType(rawType);
364 if (!baseType) {
365 return {};
366 }
John Stilesb806da42021-08-04 16:26:47 -0400367 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
368 decls.fOffset);
369 if (!baseType) {
370 return {};
371 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500372
373 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400374
375 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400376 for (; declarationsIter != decls.end(); ++declarationsIter) {
377 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400378 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500379 std::unique_ptr<Expression> arraySize;
380 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400381 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500382 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400383 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500384 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400385 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700386 }
John Stiles8f633ef2021-08-21 17:13:11 -0400387 arraySize = this->convertExpression(*iter++);
388 if (!arraySize) {
389 continue;
390 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 }
Brian Osman4cf85072021-01-14 14:18:10 -0500392 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400393 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700394 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400395 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700396 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700397 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500398 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
399 modifiers,
400 baseType,
401 varData.fName,
402 varData.fIsArray,
403 std::move(arraySize),
404 std::move(value),
405 storage);
406 if (varDeclStmt) {
407 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700408 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700409 }
Brian Osmanc0213602020-10-06 14:43:32 -0400410 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700411}
412
Ethan Nicholasfc994162019-06-06 10:04:27 -0400413std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500414 if (this->programKind() != ProgramKind::kFragment &&
415 this->programKind() != ProgramKind::kVertex &&
416 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500417 this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400418 return nullptr;
419 }
420
Ethan Nicholasfc994162019-06-06 10:04:27 -0400421 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
422 Modifiers modifiers = m.getModifiers();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400423 if (modifiers.fLayout.fInvocations != -1) {
John Stilesd1204642021-02-17 16:30:02 -0500424 if (this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500425 this->errorReporter().error(m.fOffset,
426 "'invocations' is only legal in geometry shaders");
Ethan Nicholasf06576b2019-04-03 15:45:25 -0400427 return nullptr;
428 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400429 fInvocations = modifiers.fLayout.fInvocations;
John Stilesc1a98b82021-02-24 13:35:02 -0500430 if (!this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400431 modifiers.fLayout.fInvocations = -1;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400432 if (modifiers.fLayout.description() == "") {
433 return nullptr;
434 }
435 }
436 }
John Stilesc1a98b82021-02-24 13:35:02 -0500437 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 &&
438 !this->caps().gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400439 modifiers.fLayout.fMaxVertices *= fInvocations;
440 }
John Stilesf2872e62021-05-04 11:38:43 -0400441 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700442}
443
John Stilesad2319f2020-09-02 15:01:47 -0400444std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
445 SkASSERT(n.fKind == ASTNode::Kind::kIf);
446 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500447 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400448 if (!test) {
449 return nullptr;
450 }
451 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
452 if (!ifTrue) {
453 return nullptr;
454 }
John Stilescf3059e2021-02-25 14:27:02 -0500455 if (this->detectVarDeclarationWithoutScope(*ifTrue)) {
456 return nullptr;
457 }
John Stilesad2319f2020-09-02 15:01:47 -0400458 std::unique_ptr<Statement> ifFalse;
459 if (iter != n.end()) {
460 ifFalse = this->convertStatement(*(iter++));
461 if (!ifFalse) {
462 return nullptr;
463 }
John Stilescf3059e2021-02-25 14:27:02 -0500464 if (this->detectVarDeclarationWithoutScope(*ifFalse)) {
465 return nullptr;
466 }
John Stilesad2319f2020-09-02 15:01:47 -0400467 }
John Stiles5ede6e32021-01-11 13:00:49 -0500468 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500469 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
470 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400471}
472
Ethan Nicholasfc994162019-06-06 10:04:27 -0400473std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
474 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700475 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700476 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400477 auto iter = f.begin();
478 if (*iter) {
479 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700480 if (!initializer) {
481 return nullptr;
482 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700483 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400484 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700485 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400486 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500487 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700488 if (!test) {
489 return nullptr;
490 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700491 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400492 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700493 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400494 if (*iter) {
495 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700496 if (!next) {
497 return nullptr;
498 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700499 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400500 ++iter;
501 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700502 if (!statement) {
503 return nullptr;
504 }
John Stiles2b6ec982021-03-04 10:46:39 -0500505 if (this->detectVarDeclarationWithoutScope(*statement)) {
506 return nullptr;
507 }
Brian Osman77ba8102021-01-12 17:15:30 -0500508
John Stiles23521a82021-03-02 17:02:51 -0500509 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
510 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500511}
512
513std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
514 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400515 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500516 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700517 if (!test) {
518 return nullptr;
519 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400520 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700521 if (!statement) {
522 return nullptr;
523 }
John Stilesb321a072021-02-25 16:24:19 -0500524 if (this->detectVarDeclarationWithoutScope(*statement)) {
525 return nullptr;
526 }
John Stiles23521a82021-03-02 17:02:51 -0500527 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
528 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700529}
530
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500531std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
532 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400533 auto iter = d.begin();
534 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
535 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700536 return nullptr;
537 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500538 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400539 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700540 return nullptr;
541 }
John Stilesea5822e2021-02-26 11:18:20 -0500542 if (this->detectVarDeclarationWithoutScope(*statement)) {
543 return nullptr;
544 }
John Stiles23521a82021-03-02 17:02:51 -0500545 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700546}
547
Ethan Nicholascfefec02021-02-09 15:22:57 -0500548std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
549 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
550
551 auto iter = s.begin();
552 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
553 if (!value) {
554 return nullptr;
555 }
556 AutoSymbolTable table(this);
557 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500558 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500559 for (; iter != s.end(); ++iter) {
560 const ASTNode& c = *iter;
561 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
562 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
563 auto childIter = c.begin();
564 if (*childIter) {
565 caseValue = this->convertExpression(*childIter);
566 if (!caseValue) {
567 return nullptr;
568 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500569 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400570 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500571
John Stilesbf16b6c2021-03-12 19:24:31 -0500572 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400573 for (; childIter != c.end(); ++childIter) {
574 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500575 if (!converted) {
576 return nullptr;
577 }
578 statements.push_back(std::move(converted));
579 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500580
581 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500582 }
John Stiles23521a82021-03-02 17:02:51 -0500583 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
584 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500585}
586
Ethan Nicholasfc994162019-06-06 10:04:27 -0400587std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
588 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700589 if (!e) {
590 return nullptr;
591 }
John Stiles3e5871c2021-02-25 20:52:03 -0500592 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700593}
594
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500595std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
596 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500597 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500598}
599
600std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
601 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
602 if (r.begin() != r.end()) {
603 std::unique_ptr<Expression> value = this->convertExpression(*r.begin());
604 if (!value) {
605 return nullptr;
606 }
607 return this->convertReturn(r.fOffset, std::move(value));
608 } else {
609 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700610 }
611}
612
Ethan Nicholasfc994162019-06-06 10:04:27 -0400613std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
614 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500615 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700616}
617
Ethan Nicholasfc994162019-06-06 10:04:27 -0400618std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
619 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500620 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700621}
622
Ethan Nicholasfc994162019-06-06 10:04:27 -0400623std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
624 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400625 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500626 this->errorReporter().error(d.fOffset,
627 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500628 return nullptr;
629 }
John Stilesa0c04d62021-03-11 23:07:24 -0500630 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700631}
632
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500633std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400634 Layout invokeLayout;
635 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
John Stiles586df952020-11-12 18:27:13 -0500636 const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>(
637 /*offset=*/-1,
John Stilesf2872e62021-05-04 11:38:43 -0400638 this->modifiersPool().add(invokeModifiers),
John Stiles586df952020-11-12 18:27:13 -0500639 "_invoke",
640 std::vector<const Variable*>(),
John Stiles54e7c052021-01-11 14:22:36 -0500641 fContext.fTypes.fVoid.get(),
John Stiles586df952020-11-12 18:27:13 -0500642 fIsBuiltinCode));
John Stiles0d07e142020-12-30 18:03:09 -0500643 auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl, fIsBuiltinCode,
644 std::move(main));
645 invokeDecl->setDefinition(invokeDef.get());
646 fProgramElements->push_back(std::move(invokeDef));
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400647
648 using namespace SkSL::dsl;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400649 DSLGlobalVar loopIdx("sk_InvocationID");
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400650 std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1,
651 "EndPrimitive");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400652 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400653
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400654 std::unique_ptr<Statement> block = DSLBlock(
655 For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock(
656 DSLFunction(invokeDecl)(),
657 DSLExpression(std::move(endPrimitive))({})
658 ))
659 ).release();
660 return std::unique_ptr<Block>(&block.release()->as<Block>());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400661}
662
Robert Phillipsfe8da172018-01-24 14:52:02 +0000663std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
John Stilesd45ebbc2021-05-18 16:54:36 -0400664 using namespace SkSL::dsl;
665 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
666
Brian Osman88cda172020-10-09 12:05:16 -0400667 const Variable* skPerVertex = nullptr;
668 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400669 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
670 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400671 }
672
Brian Osman88cda172020-10-09 12:05:16 -0400673 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500674 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
John Stilesd45ebbc2021-05-18 16:54:36 -0400675 return VariableReference::Make(/*offset=*/-1, var);
John Stiles5acf6a82020-11-10 22:38:01 -0500676 };
677 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
John Stiles06d600f2021-03-08 09:18:21 -0500678 return FieldAccess::Make(fContext, Ref(var), idx,
679 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
John Stiles5acf6a82020-11-10 22:38:01 -0500680 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400681 auto Pos = [&]() -> DSLExpression {
682 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
683 FieldAccess::OwnerKind::kAnonymousInterfaceBlock));
John Stiles5acf6a82020-11-10 22:38:01 -0500684 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400685 auto Adjust = [&]() -> DSLExpression {
686 return DSLExpression(fRTAdjustInterfaceBlock
687 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
688 : Ref(fRTAdjust));
John Stiles5acf6a82020-11-10 22:38:01 -0500689 };
John Stiles750109b2020-10-30 13:45:46 -0400690
John Stilesd45ebbc2021-05-18 16:54:36 -0400691 return DSLStatement(
692 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
693 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
694 0,
695 Pos().w())
696 ).release();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000697}
698
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400699void IRGenerator::CheckModifiers(const Context& context,
700 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500701 const Modifiers& modifiers,
702 int permittedModifierFlags,
703 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400704 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
705 { Modifiers::kConst_Flag, "const" },
706 { Modifiers::kIn_Flag, "in" },
707 { Modifiers::kOut_Flag, "out" },
708 { Modifiers::kUniform_Flag, "uniform" },
709 { Modifiers::kFlat_Flag, "flat" },
710 { Modifiers::kNoPerspective_Flag, "noperspective" },
711 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
712 { Modifiers::kInline_Flag, "inline" },
713 { Modifiers::kNoInline_Flag, "noinline" },
714 { Modifiers::kHighp_Flag, "highp" },
715 { Modifiers::kMediump_Flag, "mediump" },
716 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400717 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500718 };
719
Brian Osmanb5de6be2021-08-10 15:16:34 -0400720 int modifierFlags = modifiers.fFlags;
721 for (const auto& f : kModifierFlags) {
722 if (modifierFlags & f.flag) {
723 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400724 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400725 }
726 modifierFlags &= ~f.flag;
727 }
728 }
729 SkASSERT(modifierFlags == 0);
730
731 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
732 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
733 { Layout::kPushConstant_Flag, "push_constant"},
734 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
735 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
736 { Layout::kLocation_Flag, "location"},
737 { Layout::kOffset_Flag, "offset"},
738 { Layout::kBinding_Flag, "binding"},
739 { Layout::kIndex_Flag, "index"},
740 { Layout::kSet_Flag, "set"},
741 { Layout::kBuiltin_Flag, "builtin"},
742 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
743 { Layout::kPrimitive_Flag, "primitive-type"},
744 { Layout::kMaxVertices_Flag, "max_vertices"},
745 { Layout::kInvocations_Flag, "invocations"},
746 };
Brian Osmana654faa2021-02-26 11:52:59 -0500747
748 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400749 for (const auto& lf : kLayoutFlags) {
750 if (layoutFlags & lf.flag) {
751 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400752 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400753 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500754 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400755 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500756 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400757 }
Brian Osmana654faa2021-02-26 11:52:59 -0500758 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400759}
760
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400761std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl,
762 std::unique_ptr<Block> body) {
Ethan Nicholase2c05042021-02-03 10:27:22 -0500763 class Finalizer : public ProgramWriter {
764 public:
765 Finalizer(IRGenerator* irGenerator, const FunctionDeclaration* function)
766 : fIRGenerator(irGenerator)
767 , fFunction(function) {}
768
769 ~Finalizer() override {
770 SkASSERT(!fBreakableLevel);
771 SkASSERT(!fContinuableLevel);
John Stiles2dda50d2021-03-03 10:46:44 -0500772 }
773
774 bool functionReturnsValue() const {
John Stiles2558c462021-03-16 17:49:20 -0400775 return !fFunction->returnType().isVoid();
John Stiles2dda50d2021-03-03 10:46:44 -0500776 }
777
John Stiles441c1bc2021-03-19 15:55:36 -0400778 bool visitExpression(Expression& expr) override {
779 // Do not recurse into expressions.
780 return false;
781 }
782
Ethan Nicholase2c05042021-02-03 10:27:22 -0500783 bool visitStatement(Statement& stmt) override {
784 switch (stmt.kind()) {
785 case Statement::Kind::kReturn: {
786 // early returns from a vertex main function will bypass the sk_Position
787 // normalization, so SkASSERT that we aren't doing that. It is of course
788 // possible to fix this by adding a normalization before each return, but it
789 // will probably never actually be necessary.
John Stilesd1204642021-02-17 16:30:02 -0500790 SkASSERT(fIRGenerator->programKind() != ProgramKind::kVertex ||
Ethan Nicholase2c05042021-02-03 10:27:22 -0500791 !fIRGenerator->fRTAdjust ||
John Stilese8da4d22021-03-24 09:19:45 -0400792 !fFunction->isMain());
John Stiles2dda50d2021-03-03 10:46:44 -0500793
794 // Verify that the return statement matches the function's return type.
795 ReturnStatement& returnStmt = stmt.as<ReturnStatement>();
Ethan Nicholase2c05042021-02-03 10:27:22 -0500796 const Type& returnType = fFunction->returnType();
John Stiles2dda50d2021-03-03 10:46:44 -0500797 if (returnStmt.expression()) {
798 if (this->functionReturnsValue()) {
799 // Coerce return expression to the function's return type.
800 returnStmt.setExpression(fIRGenerator->coerce(
801 std::move(returnStmt.expression()), returnType));
Ethan Nicholase2c05042021-02-03 10:27:22 -0500802 } else {
John Stiles2dda50d2021-03-03 10:46:44 -0500803 // Returning something from a function with a void return type.
804 fIRGenerator->errorReporter().error(returnStmt.fOffset,
805 "may not return a value from a void function");
Ethan Nicholase2c05042021-02-03 10:27:22 -0500806 }
John Stiles2dda50d2021-03-03 10:46:44 -0500807 } else {
808 if (this->functionReturnsValue()) {
809 // Returning nothing from a function with a non-void return type.
810 fIRGenerator->errorReporter().error(returnStmt.fOffset,
811 "expected function to return '" + returnType.displayName() + "'");
812 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500813 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500814 break;
815 }
816 case Statement::Kind::kDo:
817 case Statement::Kind::kFor: {
818 ++fBreakableLevel;
819 ++fContinuableLevel;
820 bool result = INHERITED::visitStatement(stmt);
821 --fContinuableLevel;
822 --fBreakableLevel;
823 return result;
824 }
825 case Statement::Kind::kSwitch: {
826 ++fBreakableLevel;
827 bool result = INHERITED::visitStatement(stmt);
828 --fBreakableLevel;
829 return result;
830 }
831 case Statement::Kind::kBreak:
832 if (!fBreakableLevel) {
833 fIRGenerator->errorReporter().error(stmt.fOffset,
834 "break statement must be inside a loop or switch");
835 }
836 break;
837 case Statement::Kind::kContinue:
838 if (!fContinuableLevel) {
839 fIRGenerator->errorReporter().error(stmt.fOffset,
840 "continue statement must be inside a loop");
841 }
842 break;
843 default:
844 break;
845 }
846 return INHERITED::visitStatement(stmt);
847 }
848
849 private:
850 IRGenerator* fIRGenerator;
851 const FunctionDeclaration* fFunction;
852 // how deeply nested we are in breakable constructs (for, do, switch).
853 int fBreakableLevel = 0;
854 // how deeply nested we are in continuable constructs (for, do).
855 int fContinuableLevel = 0;
856
857 using INHERITED = ProgramWriter;
858 };
John Stilesb3dcbb12021-03-04 16:00:20 -0500859
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400860 bool isMain = funcDecl.isMain();
861 bool needInvocationIDWorkaround = fInvocations != -1 && isMain &&
862 !this->caps().gsInvocationsSupport();
863 if (needInvocationIDWorkaround) {
864 body = this->applyInvocationIDWorkaround(std::move(body));
865 }
866 if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) {
867 body->children().push_back(this->getNormalizeSkPositionCode());
868 }
869
John Stiles958f4b52021-03-17 16:39:49 -0400870 Finalizer finalizer{this, &funcDecl};
871 finalizer.visitStatement(*body);
John Stilesb3dcbb12021-03-04 16:00:20 -0500872
John Stiles958f4b52021-03-17 16:39:49 -0400873 if (Analysis::CanExitWithoutReturningValue(funcDecl, *body)) {
874 this->errorReporter().error(funcDecl.fOffset, "function '" + funcDecl.name() +
875 "' can exit without returning a value");
John Stilesb3dcbb12021-03-04 16:00:20 -0500876 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400877 return body;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500878}
879
Ethan Nicholasfc994162019-06-06 10:04:27 -0400880void IRGenerator::convertFunction(const ASTNode& f) {
John Stilesb2705a12021-02-19 10:55:14 -0500881 SkASSERT(fReferencedIntrinsics.empty());
882 SK_AT_SCOPE_EXIT(fReferencedIntrinsics.clear());
883
Ethan Nicholasfc994162019-06-06 10:04:27 -0400884 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400885 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400886 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400887 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700888 }
John Stilesb9af7232020-08-20 15:57:48 -0400889 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400890 std::vector<std::unique_ptr<Variable>> parameters;
891 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400892 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400893 const ASTNode& param = *(iter++);
894 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500895 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400896 auto paramIter = param.begin();
897 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700898 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400899 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700900 }
John Stilesd39aec92020-12-03 14:37:16 -0500901 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500902 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500903 if (!arraySize) {
904 return;
905 }
John Stilesa2179502020-12-03 14:37:57 -0500906 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700907 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400908
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400909 parameters.push_back(std::make_unique<Variable>(param.fOffset,
910 this->modifiersPool().add(pd.fModifiers),
911 pd.fName,
912 type,
913 fIsBuiltinCode,
914 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700915 }
916
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400917 // Conservatively assume all user-defined functions have side effects.
918 Modifiers declModifiers = funcData.fModifiers;
919 if (!fIsBuiltinCode) {
920 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400921 }
922
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400923 if (fContext.fConfig->fSettings.fForceNoInline) {
924 // Apply the `noinline` modifier to every function. This allows us to test Runtime
925 // Effects without any inlining, even when the code is later added to a paint.
926 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
927 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700928 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400929
930 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
931 fContext,
932 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400933 f.fOffset,
934 this->modifiersPool().add(declModifiers),
935 funcData.fName,
936 std::move(parameters),
937 returnType,
938 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700939 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400940 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700941 }
John Stiles569249b2020-11-03 12:18:22 -0500942 if (iter == f.end()) {
943 // If there's no body, we've found a prototype.
944 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
945 fIsBuiltinCode));
946 } else {
947 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700948 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500949 for (const Variable* param : decl->parameters()) {
950 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700951 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400952 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700953 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400954 return;
955 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400956 body = this->finalizeFunction(*decl, std::move(body));
John Stiles607d36b2020-10-19 15:00:01 -0400957 auto result = std::make_unique<FunctionDefinition>(
958 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -0400959 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400960 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500961 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700962 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700963}
964
John Stilesdc75a972020-11-25 16:24:55 -0500965std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
966 SkASSERT(node.fKind == ASTNode::Kind::kType);
967
968 const Type* type = this->convertType(node);
969 if (!type) {
970 return nullptr;
971 }
John Stilesc0c51062020-12-03 17:16:29 -0500972 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500973 this->errorReporter().error(node.fOffset,
974 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500975 return nullptr;
976 }
Brian Osman02bc5222021-01-28 11:00:20 -0500977 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
978 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500979 return std::make_unique<StructDefinition>(node.fOffset, *type);
980}
981
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400982std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500983 if (this->programKind() != ProgramKind::kFragment &&
984 this->programKind() != ProgramKind::kVertex &&
985 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500986 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400987 return nullptr;
988 }
989
Ethan Nicholasfc994162019-06-06 10:04:27 -0400990 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500991 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700992 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400993 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700994 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000995 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400996 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400997 {
998 AutoSymbolTable table(this);
999 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001000 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
1001 StatementArray decls = this->convertVarDeclarations(*(iter++),
1002 Variable::Storage::kInterfaceBlock);
1003 if (decls.empty()) {
1004 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001005 }
John Stiles869cdef2020-10-30 14:24:24 -04001006 for (const auto& decl : decls) {
1007 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles1d757782020-11-17 09:43:22 -05001008 if (vd.var().type().isOpaque()) {
John Stilesb30151e2021-01-11 16:13:08 -05001009 this->errorReporter().error(decl->fOffset,
1010 "opaque type '" + vd.var().type().name() +
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001011 "' is not permitted in an interface block");
John Stiles1d757782020-11-17 09:43:22 -05001012 }
John Stiles869cdef2020-10-30 14:24:24 -04001013 if (&vd.var() == fRTAdjust) {
1014 foundRTAdjust = true;
John Stiles54e7c052021-01-11 14:22:36 -05001015 SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4);
John Stiles869cdef2020-10-30 14:24:24 -04001016 fRTAdjustFieldIndex = fields.size();
1017 }
1018 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
1019 &vd.var().type()));
1020 if (vd.value()) {
John Stilesb30151e2021-01-11 16:13:08 -05001021 this->errorReporter().error(
1022 decl->fOffset,
1023 "initializers are not permitted on interface block fields");
John Stiles869cdef2020-10-30 14:24:24 -04001024 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001025 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001026 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001027 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001028 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -04001029 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -05001030 fields));
John Stilesd39aec02020-12-03 10:42:26 -05001031 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -05001032 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001033 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001034 if (size) {
Brian Osman4cf85072021-01-14 14:18:10 -05001035 // convertArraySize rejects unsized arrays. This is the one place we allow those, but
1036 // we've already checked for that, so this is verifying the other aspects (constant,
1037 // positive, not too large).
John Stiles80b02af2021-02-12 17:07:51 -05001038 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osman4cf85072021-01-14 14:18:10 -05001039 if (!arraySize) {
John Stilesd39aec02020-12-03 10:42:26 -05001040 return nullptr;
1041 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001042 } else {
John Stilesd39aec02020-12-03 10:42:26 -05001043 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001044 }
John Stilesa2179502020-12-03 14:37:57 -05001045 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001046 }
Brian Osman5bf3e202020-10-13 10:34:18 -04001047 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -04001048 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -04001049 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001050 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001051 type,
Brian Osman3887a012020-09-30 13:22:27 -04001052 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001053 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001054 if (foundRTAdjust) {
1055 fRTAdjustInterfaceBlock = var;
1056 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001057 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -04001058 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001059 } else {
1060 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -04001061 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001062 }
1063 }
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001064 return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset,
1065 var,
Ethan Nicholas3533ff12021-08-02 12:53:29 -04001066 id.fTypeName,
1067 id.fInstanceName,
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001068 arraySize,
1069 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -07001070}
1071
John Stiles7bd70332020-11-30 17:04:09 -05001072void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
1073 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
1074 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -05001075 const Type* type = &stmt->as<VarDeclaration>().baseType();
1076 if (type->isStruct()) {
1077 auto [iter, wasInserted] = fDefinedStructs.insert(type);
1078 if (wasInserted) {
1079 fProgramElements->push_back(
1080 std::make_unique<StructDefinition>(decl.fOffset, *type));
1081 }
1082 }
Ethan Nicholas624a5292021-04-16 14:54:43 -04001083 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -05001084 }
1085}
1086
John Stiles823c5042021-08-17 12:09:00 -04001087static bool type_contains_private_fields(const Type& type) {
John Stilesb4b627e2020-11-13 15:55:27 -05001088 // Checks for usage of private types, including fields inside a struct.
1089 if (type.isPrivate()) {
1090 return true;
1091 }
John Stilesc0c51062020-12-03 17:16:29 -05001092 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -05001093 for (const auto& f : type.fields()) {
John Stiles823c5042021-08-17 12:09:00 -04001094 if (type_contains_private_fields(*f.fType)) {
John Stilesb4b627e2020-11-13 15:55:27 -05001095 return true;
1096 }
1097 }
1098 }
1099 return false;
1100}
1101
Brian Osmand8070392020-09-09 15:50:02 -04001102const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001103 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -05001104 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -05001105 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -05001106 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -05001107 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001108 }
John Stiles6bef6a72020-12-02 14:26:04 -05001109 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -05001110 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -04001111 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -05001112 this->errorReporter().error(type.fOffset,
1113 "type '" + name + "' not allowed in this context");
1114 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -05001115 }
John Stiles823c5042021-08-17 12:09:00 -04001116 if (!fIsBuiltinCode) {
1117 if (type_contains_private_fields(*result)) {
1118 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
1119 return nullptr;
1120 }
1121 if (this->strictES2Mode() && !result->allowedInES2()) {
1122 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
1123 return nullptr;
1124 }
John Stiles7bd70332020-11-30 17:04:09 -05001125 }
John Stiles6bef6a72020-12-02 14:26:04 -05001126 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -05001127 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -05001128 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -05001129 if (!arraySize) {
1130 return nullptr;
1131 }
John Stilesa2179502020-12-03 14:37:57 -05001132 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -05001133 }
John Stiles6bef6a72020-12-02 14:26:04 -05001134 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001135}
1136
Ethan Nicholasfc994162019-06-06 10:04:27 -04001137std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001138 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001139 case ASTNode::Kind::kBinary:
1140 return this->convertBinaryExpression(expr);
1141 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -05001142 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001143 case ASTNode::Kind::kCall:
1144 return this->convertCallExpression(expr);
1145 case ASTNode::Kind::kField:
1146 return this->convertFieldExpression(expr);
1147 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -05001148 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001149 case ASTNode::Kind::kIdentifier:
1150 return this->convertIdentifier(expr);
1151 case ASTNode::Kind::kIndex:
1152 return this->convertIndexExpression(expr);
1153 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -05001154 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001155 case ASTNode::Kind::kPostfix:
1156 return this->convertPostfixExpression(expr);
1157 case ASTNode::Kind::kPrefix:
1158 return this->convertPrefixExpression(expr);
1159 case ASTNode::Kind::kTernary:
1160 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001161 default:
John Stileseada7bc2021-02-02 16:29:32 -05001162 SkDEBUGFAILF("unsupported expression: %s\n", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001163 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001164 }
1165}
1166
Ethan Nicholas962dec42021-06-10 13:06:39 -04001167std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001168 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -07001169 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -05001170 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001171 return nullptr;
1172 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001173 switch (result->kind()) {
1174 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -07001175 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -04001176 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -07001177 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001178 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -07001179 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001180 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -04001181 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001182 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -07001183 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001184 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -04001185 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001186 const Modifiers& modifiers = var->modifiers();
1187 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -04001188 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -04001189 if (caps().canUseFragCoord()) {
1190 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001191 }
Brian Salomond8d85b92021-07-07 09:41:17 -04001192 break;
1193 case SK_CLOCKWISE_BUILTIN:
1194 fInputs.fUseFlipRTUniform = true;
1195 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001196 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001197 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -04001198 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001199 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001200 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001201 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -04001202 auto base = VariableReference::Make(offset, &field->owner(),
1203 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -05001204 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
1205 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001206 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001207 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001208 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001209 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001210 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001211 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001212 const ExternalFunction* r = &result->as<ExternalFunction>();
1213 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001214 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001215 default:
John Stilesf57207b2021-02-02 17:50:34 -05001216 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001217 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001218}
1219
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001220std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001221 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001222}
1223
Ethan Nicholas11d53972016-11-28 11:23:23 -05001224std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001225 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -05001226 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -07001227}
1228
Ethan Nicholasfc994162019-06-06 10:04:27 -04001229std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1230 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1231 auto iter = expression.begin();
1232 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001233 if (!left) {
1234 return nullptr;
1235 }
John Stiles0f464502020-11-20 12:52:22 -05001236 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001237 if (!right) {
1238 return nullptr;
1239 }
John Stiles23521a82021-03-02 17:02:51 -05001240 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
1241 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -07001242}
1243
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001244std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1245 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1246 auto iter = node.begin();
1247 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1248 if (!test) {
1249 return nullptr;
1250 }
1251 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1252 if (!ifTrue) {
1253 return nullptr;
1254 }
1255 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1256 if (!ifFalse) {
1257 return nullptr;
1258 }
John Stiles23521a82021-03-02 17:02:51 -05001259 return TernaryExpression::Convert(fContext, std::move(test),
1260 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001261}
1262
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001263void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001264 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001265 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001266
1267 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1268 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001269 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1270 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001271 std::sort(intrinsics.begin(), intrinsics.end(),
1272 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001273 if (a->isBuiltin() != b->isBuiltin()) {
1274 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001275 }
1276 if (a->fOffset != b->fOffset) {
1277 return a->fOffset < b->fOffset;
1278 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001279 if (a->name() != b->name()) {
1280 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001281 }
1282 return a->description() < b->description();
1283 });
1284 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001285 this->copyIntrinsicIfNeeded(*f);
1286 }
John Stiles607d36b2020-10-19 15:00:01 -04001287
Brian Osman0006ad02020-11-18 15:38:39 -05001288 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001289 }
1290}
1291
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001292std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001293 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001294 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001295 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001296 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001297 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001298 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001299 if (function.definition()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001300 fReferencedIntrinsics.insert(&function);
1301 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001302 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001303 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001304 }
1305 }
John Stilesea9ab822020-08-31 09:55:04 -04001306
John Stilescd7ba502021-03-19 10:54:59 -04001307 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001308}
1309
1310/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001311 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001312 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1313 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001314 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001315CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001316 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001317 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1318 return CoercionCost::Impossible();
1319 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001320 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001321 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001322 }
John Stilesfa889112020-10-12 19:03:43 -04001323 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001324 const Type* ignored;
1325 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001326 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001327 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001328 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001329 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001330 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001331 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001332 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001333}
1334
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001335std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001336 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001337 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001338 switch (functionValue->kind()) {
1339 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001340 return Constructor::Convert(fContext,
1341 offset,
1342 functionValue->as<TypeReference>().value(),
1343 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001344 case Expression::Kind::kExternalFunctionReference: {
1345 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1346 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001347 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001348 this->errorReporter().error(offset, "external function expected " +
1349 to_string(count) + " arguments, but found " +
1350 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001351 return nullptr;
1352 }
1353 static constexpr int PARAMETER_MAX = 16;
1354 SkASSERT(count < PARAMETER_MAX);
1355 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001356 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001357 for (int i = 0; i < count; ++i) {
1358 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1359 if (!arguments[i]) {
1360 return nullptr;
1361 }
1362 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001363 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001364 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001365 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001366 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001367 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001368 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001369 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001370 if (functions.size() > 1) {
1371 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001372 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001373 if (cost < bestCost) {
1374 bestCost = cost;
1375 best = f;
1376 }
1377 }
1378 if (best) {
1379 return this->call(offset, *best, std::move(arguments));
1380 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001381 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001382 String separator;
1383 for (size_t i = 0; i < arguments.size(); i++) {
1384 msg += separator;
1385 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001386 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001387 }
1388 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001389 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001390 return nullptr;
1391 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001392 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001393 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001394 default:
John Stilesb30151e2021-01-11 16:13:08 -05001395 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001396 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001397 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001398}
1399
Ethan Nicholasfc994162019-06-06 10:04:27 -04001400std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1401 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1402 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001403 if (!base) {
1404 return nullptr;
1405 }
John Stiles23521a82021-03-02 17:02:51 -05001406 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001407}
1408
ethannicholasb3058bd2016-07-01 08:22:01 -07001409std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001410 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001411 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001412}
1413
Ethan Nicholasfc994162019-06-06 10:04:27 -04001414std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1415 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1416 auto iter = index.begin();
1417 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001418 if (!base) {
1419 return nullptr;
1420 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001421 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001422 if (base->is<TypeReference>()) {
1423 this->errorReporter().error(index.fOffset, "array must have a size");
1424 } else {
1425 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1426 }
Brian Osman4cf85072021-01-14 14:18:10 -05001427 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001428 }
1429 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1430 if (!converted) {
1431 return nullptr;
1432 }
Ethan Nicholas251e6342021-07-20 13:16:19 -04001433 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base),
1434 std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001435}
1436
Ethan Nicholasfc994162019-06-06 10:04:27 -04001437std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1438 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1439 auto iter = callNode.begin();
1440 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1441 if (!base) {
1442 return nullptr;
1443 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001444 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001445 for (; iter != callNode.end(); ++iter) {
1446 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1447 if (!converted) {
1448 return nullptr;
1449 }
1450 arguments.push_back(std::move(converted));
1451 }
1452 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1453}
1454
1455std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1456 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1457 if (!base) {
1458 return nullptr;
1459 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001460 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001461 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001462 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001463 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001464 }
John Stiles06d600f2021-03-08 09:18:21 -05001465 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001466}
1467
1468std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001469 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001470 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1471 if (!base) {
1472 return nullptr;
1473 }
John Stiles23521a82021-03-02 17:02:51 -05001474 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001475}
1476
1477void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001478 switch (expr.kind()) {
John Stilesdc435fa2020-12-30 18:07:10 -05001479 case Expression::Kind::kFunctionCall: {
1480 const FunctionDeclaration& decl = expr.as<FunctionCall>().function();
1481 if (!decl.isBuiltin() && !decl.definition()) {
John Stilesb30151e2021-01-11 16:13:08 -05001482 this->errorReporter().error(expr.fOffset,
1483 "function '" + decl.description() + "' is not defined");
John Stilesdc435fa2020-12-30 18:07:10 -05001484 }
1485 break;
1486 }
John Stiles973009b2021-02-17 17:18:01 -05001487 case Expression::Kind::kFunctionReference:
1488 case Expression::Kind::kTypeReference:
1489 SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()");
1490 this->errorReporter().error(expr.fOffset, "invalid expression");
1491 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001492 default:
John Stiles54e7c052021-01-11 14:22:36 -05001493 if (expr.type() == *fContext.fTypes.fInvalid) {
John Stilesb30151e2021-01-11 16:13:08 -05001494 this->errorReporter().error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07001495 }
John Stiles973009b2021-02-17 17:18:01 -05001496 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001497 }
1498}
1499
Brian Osman9496fe52020-11-18 14:48:19 -05001500void IRGenerator::findAndDeclareBuiltinVariables() {
1501 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001502 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001503 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001504
Brian Osman9496fe52020-11-18 14:48:19 -05001505 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001506 // If this is the *first* time we've seen this builtin, findAndInclude will return
1507 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001508 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001509 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001510 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001511 }
1512 }
1513
John Stiles4d6310a2021-01-26 19:58:22 -05001514 bool visitProgramElement(const ProgramElement& pe) override {
1515 if (pe.is<FunctionDefinition>()) {
1516 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1517 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1518 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001519 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001520 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1521 fPreserveFragColor = true;
1522 }
1523 }
1524 return INHERITED::visitProgramElement(pe);
1525 }
1526
Brian Osman9496fe52020-11-18 14:48:19 -05001527 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001528 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001529 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001530 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001531 return INHERITED::visitExpression(e);
1532 }
1533
1534 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001535 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001536 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001537
Brian Osman9496fe52020-11-18 14:48:19 -05001538 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001539 using INHERITED::visitProgramElement;
1540 };
1541
Brian Osman9496fe52020-11-18 14:48:19 -05001542 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001543 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001544 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001545 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001546 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001547
John Stiles4d6310a2021-01-26 19:58:22 -05001548 if (scanner.fPreserveFragColor) {
1549 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001550 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001551 }
1552
John Stilesd1204642021-02-17 16:30:02 -05001553 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001554 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001555 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1556 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1557 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001558 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001559 break;
1560 default:
1561 break;
1562 }
1563
Brian Osman9496fe52020-11-18 14:48:19 -05001564 fSharedElements->insert(
1565 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001566}
1567
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001568void IRGenerator::start(const ParsedModule& base,
1569 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001570 std::vector<std::unique_ptr<ProgramElement>>* elements,
1571 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001572 fProgramElements = elements;
1573 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001574 fSymbolTable = base.fSymbols;
1575 fIntrinsics = base.fIntrinsics.get();
1576 if (fIntrinsics) {
1577 fIntrinsics->resetAlreadyIncluded();
1578 }
1579 fIsBuiltinCode = isBuiltinCode;
1580
Brian Salomond8d85b92021-07-07 09:41:17 -04001581 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001582 fInvocations = -1;
1583 fRTAdjust = nullptr;
1584 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001585 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001586 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001587
John Stilesd1204642021-02-17 16:30:02 -05001588 if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) {
Brian Osman68c1d402020-10-12 16:36:38 -04001589 // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin.
1590 // If we're applying the workaround, then it's a plain global.
John Stilesc1a98b82021-02-24 13:35:02 -05001591 bool workaround = !this->caps().gsInvocationsSupport();
Brian Osman68c1d402020-10-12 16:36:38 -04001592 Modifiers m;
1593 if (!workaround) {
1594 m.fFlags = Modifiers::kIn_Flag;
1595 m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN;
1596 }
John Stilesf2872e62021-05-04 11:38:43 -04001597 auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m),
John Stilese67bd132021-03-19 18:39:25 -04001598 "sk_InvocationID", fContext.fTypes.fInt.get(),
1599 /*builtin=*/false, Variable::Storage::kGlobal);
1600 auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(),
1601 /*arraySize=*/0, /*value=*/nullptr);
Brian Osman68c1d402020-10-12 16:36:38 -04001602 fSymbolTable->add(std::move(var));
Ethan Nicholas624a5292021-04-16 14:54:43 -04001603 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl)));
Brian Osman68c1d402020-10-12 16:36:38 -04001604 }
1605
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001606 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001607 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001608 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001609 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001610 }
1611 }
John Stilesaecf8d52021-05-14 12:15:01 -04001612
John Stilesbb2ef922021-07-26 08:32:07 -04001613 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001614 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001615 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001616 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1617 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1618 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1619 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1620 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1621 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1622 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1623 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1624 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1625
1626 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1627 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1628 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1629 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1630 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1631 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1632
1633 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1634 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1635 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1636 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1637 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1638 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001639
1640 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1641 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1642 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1643 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001644
1645 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1646 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1647 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1648 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1649
1650 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1651 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1652 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1653 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001654 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001655}
Brian Osman88cda172020-10-09 12:05:16 -04001656
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001657IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001658 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001659 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001660 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001661 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001662
Brian Osmanea485e52021-01-15 13:20:32 -05001663 // Do a pass looking for dangling FunctionReference or TypeReference expressions
Brian Osmanc95d3a12020-09-09 10:56:27 -04001664 class FindIllegalExpressions : public ProgramVisitor {
1665 public:
1666 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
1667
1668 bool visitExpression(const Expression& e) override {
1669 fGenerator->checkValid(e);
1670 return INHERITED::visitExpression(e);
1671 }
1672
1673 IRGenerator* fGenerator;
1674 using INHERITED = ProgramVisitor;
1675 using INHERITED::visitProgramElement;
1676 };
1677 for (const auto& pe : *fProgramElements) {
1678 FindIllegalExpressions{this}.visitProgramElement(*pe);
1679 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001680
Brian Osmanea485e52021-01-15 13:20:32 -05001681 // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the
1682 // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic
1683 // assumes that all loops meet the criteria of Section 4, and if they don't, could crash.
1684 if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) {
1685 for (const auto& pe : *fProgramElements) {
1686 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
1687 }
1688 }
1689
Brian Osman7da06572021-07-21 15:19:34 -04001690 if (this->strictES2Mode()) {
1691 Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter());
1692 }
1693
John Stilesf2872e62021-05-04 11:38:43 -04001694 return IRBundle{std::move(*fProgramElements),
1695 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001696 std::move(fSymbolTable),
1697 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001698}
1699
1700IRGenerator::IRBundle IRGenerator::convertProgram(
1701 const ParsedModule& base,
1702 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001703 skstd::string_view text) {
1704 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001705 fFile = parser.compilationUnit();
1706 if (this->errorReporter().errorCount() == 0) {
1707 SkASSERT(fFile);
1708 for (const auto& decl : fFile->root()) {
1709 switch (decl.fKind) {
1710 case ASTNode::Kind::kVarDeclarations:
1711 this->convertGlobalVarDeclarations(decl);
1712 break;
1713
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001714 case ASTNode::Kind::kFunction:
1715 this->convertFunction(decl);
1716 break;
1717
1718 case ASTNode::Kind::kModifiers: {
1719 std::unique_ptr<ModifiersDeclaration> f =
1720 this->convertModifiersDeclaration(decl);
1721 if (f) {
1722 fProgramElements->push_back(std::move(f));
1723 }
1724 break;
1725 }
1726 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001727 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001728 if (i) {
1729 fProgramElements->push_back(std::move(i));
1730 }
1731 break;
1732 }
1733 case ASTNode::Kind::kExtension: {
1734 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001735 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001736 if (e) {
1737 fProgramElements->push_back(std::move(e));
1738 }
1739 break;
1740 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001741 case ASTNode::Kind::kType: {
1742 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1743 if (s) {
1744 fProgramElements->push_back(std::move(s));
1745 }
1746 break;
1747 }
1748 default:
1749 SkDEBUGFAILF("unsupported declaration: %s\n", decl.description().c_str());
1750 break;
1751 }
1752 }
1753 }
1754 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001755}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001756
John Stilesa6841be2020-08-06 14:11:56 -04001757} // namespace SkSL