blob: 5206457ab2ae11c30a1dea5b1c72aeab3e0c6cb9 [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 Stiles842ee5b2021-08-27 12:37:27 -0400643 IntrinsicSet referencedIntrinsics;
644 main = this->finalizeFunction(*invokeDecl, std::move(main), &referencedIntrinsics);
645 auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl,
646 fIsBuiltinCode, std::move(main),
647 std::move(referencedIntrinsics));
John Stiles0d07e142020-12-30 18:03:09 -0500648 invokeDecl->setDefinition(invokeDef.get());
649 fProgramElements->push_back(std::move(invokeDef));
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400650
651 using namespace SkSL::dsl;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400652 DSLGlobalVar loopIdx("sk_InvocationID");
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400653 std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1,
654 "EndPrimitive");
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400655 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400656
Ethan Nicholasbe65cd52021-05-18 12:09:01 -0400657 std::unique_ptr<Statement> block = DSLBlock(
658 For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock(
659 DSLFunction(invokeDecl)(),
660 DSLExpression(std::move(endPrimitive))({})
661 ))
662 ).release();
663 return std::unique_ptr<Block>(&block.release()->as<Block>());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400664}
665
Robert Phillipsfe8da172018-01-24 14:52:02 +0000666std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
John Stilesd45ebbc2021-05-18 16:54:36 -0400667 using namespace SkSL::dsl;
668 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
669
Brian Osman88cda172020-10-09 12:05:16 -0400670 const Variable* skPerVertex = nullptr;
671 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400672 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
673 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400674 }
675
Brian Osman88cda172020-10-09 12:05:16 -0400676 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500677 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
John Stilesd45ebbc2021-05-18 16:54:36 -0400678 return VariableReference::Make(/*offset=*/-1, var);
John Stiles5acf6a82020-11-10 22:38:01 -0500679 };
680 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
John Stiles06d600f2021-03-08 09:18:21 -0500681 return FieldAccess::Make(fContext, Ref(var), idx,
682 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
John Stiles5acf6a82020-11-10 22:38:01 -0500683 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400684 auto Pos = [&]() -> DSLExpression {
685 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
686 FieldAccess::OwnerKind::kAnonymousInterfaceBlock));
John Stiles5acf6a82020-11-10 22:38:01 -0500687 };
John Stilesd45ebbc2021-05-18 16:54:36 -0400688 auto Adjust = [&]() -> DSLExpression {
689 return DSLExpression(fRTAdjustInterfaceBlock
690 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
691 : Ref(fRTAdjust));
John Stiles5acf6a82020-11-10 22:38:01 -0500692 };
John Stiles750109b2020-10-30 13:45:46 -0400693
John Stilesd45ebbc2021-05-18 16:54:36 -0400694 return DSLStatement(
695 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
696 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
697 0,
698 Pos().w())
699 ).release();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000700}
701
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400702void IRGenerator::CheckModifiers(const Context& context,
703 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500704 const Modifiers& modifiers,
705 int permittedModifierFlags,
706 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400707 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
708 { Modifiers::kConst_Flag, "const" },
709 { Modifiers::kIn_Flag, "in" },
710 { Modifiers::kOut_Flag, "out" },
711 { Modifiers::kUniform_Flag, "uniform" },
712 { Modifiers::kFlat_Flag, "flat" },
713 { Modifiers::kNoPerspective_Flag, "noperspective" },
714 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
715 { Modifiers::kInline_Flag, "inline" },
716 { Modifiers::kNoInline_Flag, "noinline" },
717 { Modifiers::kHighp_Flag, "highp" },
718 { Modifiers::kMediump_Flag, "mediump" },
719 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400720 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500721 };
722
Brian Osmanb5de6be2021-08-10 15:16:34 -0400723 int modifierFlags = modifiers.fFlags;
724 for (const auto& f : kModifierFlags) {
725 if (modifierFlags & f.flag) {
726 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400727 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400728 }
729 modifierFlags &= ~f.flag;
730 }
731 }
732 SkASSERT(modifierFlags == 0);
733
734 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
735 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
736 { Layout::kPushConstant_Flag, "push_constant"},
737 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
738 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
739 { Layout::kLocation_Flag, "location"},
740 { Layout::kOffset_Flag, "offset"},
741 { Layout::kBinding_Flag, "binding"},
742 { Layout::kIndex_Flag, "index"},
743 { Layout::kSet_Flag, "set"},
744 { Layout::kBuiltin_Flag, "builtin"},
745 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
746 { Layout::kPrimitive_Flag, "primitive-type"},
747 { Layout::kMaxVertices_Flag, "max_vertices"},
748 { Layout::kInvocations_Flag, "invocations"},
749 };
Brian Osmana654faa2021-02-26 11:52:59 -0500750
751 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400752 for (const auto& lf : kLayoutFlags) {
753 if (layoutFlags & lf.flag) {
754 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400755 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400756 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500757 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400758 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500759 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400760 }
Brian Osmana654faa2021-02-26 11:52:59 -0500761 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400762}
763
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400764std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl,
John Stiles842ee5b2021-08-27 12:37:27 -0400765 std::unique_ptr<Block> body,
766 IntrinsicSet* referencedIntrinsics) {
Ethan Nicholase2c05042021-02-03 10:27:22 -0500767 class Finalizer : public ProgramWriter {
768 public:
John Stiles842ee5b2021-08-27 12:37:27 -0400769 Finalizer(IRGenerator* irGenerator, const FunctionDeclaration* function,
770 IntrinsicSet* referencedIntrinsics)
Ethan Nicholase2c05042021-02-03 10:27:22 -0500771 : fIRGenerator(irGenerator)
John Stiles842ee5b2021-08-27 12:37:27 -0400772 , fFunction(function)
773 , fReferencedIntrinsics(referencedIntrinsics) {}
Ethan Nicholase2c05042021-02-03 10:27:22 -0500774
775 ~Finalizer() override {
776 SkASSERT(!fBreakableLevel);
777 SkASSERT(!fContinuableLevel);
John Stiles2dda50d2021-03-03 10:46:44 -0500778 }
779
780 bool functionReturnsValue() const {
John Stiles2558c462021-03-16 17:49:20 -0400781 return !fFunction->returnType().isVoid();
John Stiles2dda50d2021-03-03 10:46:44 -0500782 }
783
John Stiles441c1bc2021-03-19 15:55:36 -0400784 bool visitExpression(Expression& expr) override {
John Stiles842ee5b2021-08-27 12:37:27 -0400785 if (expr.is<FunctionCall>()) {
786 const FunctionDeclaration& func = expr.as<FunctionCall>().function();
787 if (func.isBuiltin() && func.definition()) {
788 fReferencedIntrinsics->insert(&func);
789 }
790 }
791 return INHERITED::visitExpression(expr);
John Stiles441c1bc2021-03-19 15:55:36 -0400792 }
793
Ethan Nicholase2c05042021-02-03 10:27:22 -0500794 bool visitStatement(Statement& stmt) override {
795 switch (stmt.kind()) {
796 case Statement::Kind::kReturn: {
797 // early returns from a vertex main function will bypass the sk_Position
798 // normalization, so SkASSERT that we aren't doing that. It is of course
799 // possible to fix this by adding a normalization before each return, but it
800 // will probably never actually be necessary.
John Stilesd1204642021-02-17 16:30:02 -0500801 SkASSERT(fIRGenerator->programKind() != ProgramKind::kVertex ||
Ethan Nicholase2c05042021-02-03 10:27:22 -0500802 !fIRGenerator->fRTAdjust ||
John Stilese8da4d22021-03-24 09:19:45 -0400803 !fFunction->isMain());
John Stiles2dda50d2021-03-03 10:46:44 -0500804
805 // Verify that the return statement matches the function's return type.
806 ReturnStatement& returnStmt = stmt.as<ReturnStatement>();
Ethan Nicholase2c05042021-02-03 10:27:22 -0500807 const Type& returnType = fFunction->returnType();
John Stiles2dda50d2021-03-03 10:46:44 -0500808 if (returnStmt.expression()) {
809 if (this->functionReturnsValue()) {
810 // Coerce return expression to the function's return type.
811 returnStmt.setExpression(fIRGenerator->coerce(
812 std::move(returnStmt.expression()), returnType));
Ethan Nicholase2c05042021-02-03 10:27:22 -0500813 } else {
John Stiles2dda50d2021-03-03 10:46:44 -0500814 // Returning something from a function with a void return type.
John Stiles537c8a72021-08-27 11:07:09 -0400815 returnStmt.setExpression(nullptr);
John Stiles2dda50d2021-03-03 10:46:44 -0500816 fIRGenerator->errorReporter().error(returnStmt.fOffset,
817 "may not return a value from a void function");
Ethan Nicholase2c05042021-02-03 10:27:22 -0500818 }
John Stiles2dda50d2021-03-03 10:46:44 -0500819 } else {
820 if (this->functionReturnsValue()) {
821 // Returning nothing from a function with a non-void return type.
822 fIRGenerator->errorReporter().error(returnStmt.fOffset,
823 "expected function to return '" + returnType.displayName() + "'");
824 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500825 }
Ethan Nicholase2c05042021-02-03 10:27:22 -0500826 break;
827 }
828 case Statement::Kind::kDo:
829 case Statement::Kind::kFor: {
830 ++fBreakableLevel;
831 ++fContinuableLevel;
832 bool result = INHERITED::visitStatement(stmt);
833 --fContinuableLevel;
834 --fBreakableLevel;
835 return result;
836 }
837 case Statement::Kind::kSwitch: {
838 ++fBreakableLevel;
839 bool result = INHERITED::visitStatement(stmt);
840 --fBreakableLevel;
841 return result;
842 }
843 case Statement::Kind::kBreak:
844 if (!fBreakableLevel) {
845 fIRGenerator->errorReporter().error(stmt.fOffset,
846 "break statement must be inside a loop or switch");
847 }
848 break;
849 case Statement::Kind::kContinue:
850 if (!fContinuableLevel) {
851 fIRGenerator->errorReporter().error(stmt.fOffset,
852 "continue statement must be inside a loop");
853 }
854 break;
855 default:
856 break;
857 }
858 return INHERITED::visitStatement(stmt);
859 }
860
861 private:
862 IRGenerator* fIRGenerator;
863 const FunctionDeclaration* fFunction;
John Stiles842ee5b2021-08-27 12:37:27 -0400864 // which intrinsics have we encountered in this function
865 IntrinsicSet* fReferencedIntrinsics;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500866 // how deeply nested we are in breakable constructs (for, do, switch).
867 int fBreakableLevel = 0;
868 // how deeply nested we are in continuable constructs (for, do).
869 int fContinuableLevel = 0;
870
871 using INHERITED = ProgramWriter;
872 };
John Stilesb3dcbb12021-03-04 16:00:20 -0500873
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400874 bool isMain = funcDecl.isMain();
875 bool needInvocationIDWorkaround = fInvocations != -1 && isMain &&
876 !this->caps().gsInvocationsSupport();
877 if (needInvocationIDWorkaround) {
878 body = this->applyInvocationIDWorkaround(std::move(body));
879 }
880 if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) {
881 body->children().push_back(this->getNormalizeSkPositionCode());
882 }
883
John Stiles842ee5b2021-08-27 12:37:27 -0400884 Finalizer finalizer{this, &funcDecl, referencedIntrinsics};
John Stiles958f4b52021-03-17 16:39:49 -0400885 finalizer.visitStatement(*body);
John Stilesb3dcbb12021-03-04 16:00:20 -0500886
John Stiles958f4b52021-03-17 16:39:49 -0400887 if (Analysis::CanExitWithoutReturningValue(funcDecl, *body)) {
888 this->errorReporter().error(funcDecl.fOffset, "function '" + funcDecl.name() +
889 "' can exit without returning a value");
John Stilesb3dcbb12021-03-04 16:00:20 -0500890 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -0400891 return body;
Ethan Nicholase2c05042021-02-03 10:27:22 -0500892}
893
Ethan Nicholasfc994162019-06-06 10:04:27 -0400894void IRGenerator::convertFunction(const ASTNode& f) {
895 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400896 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400897 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400898 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700899 }
John Stilesb9af7232020-08-20 15:57:48 -0400900 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400901 std::vector<std::unique_ptr<Variable>> parameters;
902 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400903 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400904 const ASTNode& param = *(iter++);
905 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500906 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400907 auto paramIter = param.begin();
908 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700909 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400910 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700911 }
John Stilesd39aec92020-12-03 14:37:16 -0500912 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500913 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500914 if (!arraySize) {
915 return;
916 }
John Stilesa2179502020-12-03 14:37:57 -0500917 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700918 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400919
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400920 parameters.push_back(std::make_unique<Variable>(param.fOffset,
921 this->modifiersPool().add(pd.fModifiers),
922 pd.fName,
923 type,
924 fIsBuiltinCode,
925 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700926 }
927
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400928 // Conservatively assume all user-defined functions have side effects.
929 Modifiers declModifiers = funcData.fModifiers;
930 if (!fIsBuiltinCode) {
931 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400932 }
933
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400934 if (fContext.fConfig->fSettings.fForceNoInline) {
935 // Apply the `noinline` modifier to every function. This allows us to test Runtime
936 // Effects without any inlining, even when the code is later added to a paint.
937 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
938 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700939 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400940
941 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
942 fContext,
943 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400944 f.fOffset,
945 this->modifiersPool().add(declModifiers),
946 funcData.fName,
947 std::move(parameters),
948 returnType,
949 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700950 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400951 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700952 }
John Stiles569249b2020-11-03 12:18:22 -0500953 if (iter == f.end()) {
954 // If there's no body, we've found a prototype.
955 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
956 fIsBuiltinCode));
957 } else {
958 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700959 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500960 for (const Variable* param : decl->parameters()) {
961 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700962 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400963 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700964 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400965 return;
966 }
John Stiles842ee5b2021-08-27 12:37:27 -0400967 IntrinsicSet referencedIntrinsics;
968 body = this->finalizeFunction(*decl, std::move(body), &referencedIntrinsics);
John Stiles607d36b2020-10-19 15:00:01 -0400969 auto result = std::make_unique<FunctionDefinition>(
John Stiles842ee5b2021-08-27 12:37:27 -0400970 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(referencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -0400971 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400972 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500973 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700974 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700975}
976
John Stilesdc75a972020-11-25 16:24:55 -0500977std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
978 SkASSERT(node.fKind == ASTNode::Kind::kType);
979
980 const Type* type = this->convertType(node);
981 if (!type) {
982 return nullptr;
983 }
John Stilesc0c51062020-12-03 17:16:29 -0500984 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500985 this->errorReporter().error(node.fOffset,
986 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500987 return nullptr;
988 }
Brian Osman02bc5222021-01-28 11:00:20 -0500989 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
990 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500991 return std::make_unique<StructDefinition>(node.fOffset, *type);
992}
993
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400994std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500995 if (this->programKind() != ProgramKind::kFragment &&
996 this->programKind() != ProgramKind::kVertex &&
997 this->programKind() != ProgramKind::kGeometry) {
John Stilesb30151e2021-01-11 16:13:08 -0500998 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400999 return nullptr;
1000 }
1001
Ethan Nicholasfc994162019-06-06 10:04:27 -04001002 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -05001003 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -07001004 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001005 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -07001006 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001007 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001008 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -04001009 {
1010 AutoSymbolTable table(this);
1011 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001012 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
1013 StatementArray decls = this->convertVarDeclarations(*(iter++),
1014 Variable::Storage::kInterfaceBlock);
1015 if (decls.empty()) {
1016 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001017 }
John Stiles869cdef2020-10-30 14:24:24 -04001018 for (const auto& decl : decls) {
1019 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -04001020 if (&vd.var() == fRTAdjust) {
1021 foundRTAdjust = true;
John Stiles54e7c052021-01-11 14:22:36 -05001022 SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4);
John Stiles869cdef2020-10-30 14:24:24 -04001023 fRTAdjustFieldIndex = fields.size();
1024 }
1025 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
1026 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001027 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001028 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001029 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001030 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -04001031 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -05001032 fields));
John Stilesd39aec02020-12-03 10:42:26 -05001033 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -05001034 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001035 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001036 if (size) {
Brian Osman4cf85072021-01-14 14:18:10 -05001037 // convertArraySize rejects unsized arrays. This is the one place we allow those, but
1038 // we've already checked for that, so this is verifying the other aspects (constant,
1039 // positive, not too large).
John Stiles80b02af2021-02-12 17:07:51 -05001040 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osman4cf85072021-01-14 14:18:10 -05001041 if (!arraySize) {
John Stilesd39aec02020-12-03 10:42:26 -05001042 return nullptr;
1043 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001044 } else {
John Stilesd39aec02020-12-03 10:42:26 -05001045 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001046 }
John Stilesa2179502020-12-03 14:37:57 -05001047 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001048 }
Brian Osman5bf3e202020-10-13 10:34:18 -04001049 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -04001050 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -04001051 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001052 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001053 type,
Brian Osman3887a012020-09-30 13:22:27 -04001054 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001055 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001056 if (foundRTAdjust) {
1057 fRTAdjustInterfaceBlock = var;
1058 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001059 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -04001060 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001061 } else {
1062 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -04001063 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001064 }
1065 }
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001066 return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset,
1067 var,
Ethan Nicholas3533ff12021-08-02 12:53:29 -04001068 id.fTypeName,
1069 id.fInstanceName,
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001070 arraySize,
1071 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -07001072}
1073
John Stiles7bd70332020-11-30 17:04:09 -05001074void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
1075 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
1076 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -05001077 const Type* type = &stmt->as<VarDeclaration>().baseType();
1078 if (type->isStruct()) {
1079 auto [iter, wasInserted] = fDefinedStructs.insert(type);
1080 if (wasInserted) {
1081 fProgramElements->push_back(
1082 std::make_unique<StructDefinition>(decl.fOffset, *type));
1083 }
1084 }
Ethan Nicholas624a5292021-04-16 14:54:43 -04001085 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -05001086 }
1087}
1088
John Stiles823c5042021-08-17 12:09:00 -04001089static bool type_contains_private_fields(const Type& type) {
John Stilesb4b627e2020-11-13 15:55:27 -05001090 // Checks for usage of private types, including fields inside a struct.
1091 if (type.isPrivate()) {
1092 return true;
1093 }
John Stilesc0c51062020-12-03 17:16:29 -05001094 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -05001095 for (const auto& f : type.fields()) {
John Stiles823c5042021-08-17 12:09:00 -04001096 if (type_contains_private_fields(*f.fType)) {
John Stilesb4b627e2020-11-13 15:55:27 -05001097 return true;
1098 }
1099 }
1100 }
1101 return false;
1102}
1103
Brian Osmand8070392020-09-09 15:50:02 -04001104const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001105 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -05001106 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -05001107 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -05001108 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -05001109 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001110 }
John Stiles6bef6a72020-12-02 14:26:04 -05001111 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -05001112 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -04001113 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -05001114 this->errorReporter().error(type.fOffset,
1115 "type '" + name + "' not allowed in this context");
1116 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -05001117 }
John Stiles823c5042021-08-17 12:09:00 -04001118 if (!fIsBuiltinCode) {
1119 if (type_contains_private_fields(*result)) {
1120 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
1121 return nullptr;
1122 }
1123 if (this->strictES2Mode() && !result->allowedInES2()) {
1124 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
1125 return nullptr;
1126 }
John Stiles7bd70332020-11-30 17:04:09 -05001127 }
John Stiles6bef6a72020-12-02 14:26:04 -05001128 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -05001129 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -05001130 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -05001131 if (!arraySize) {
1132 return nullptr;
1133 }
John Stilesa2179502020-12-03 14:37:57 -05001134 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -05001135 }
John Stiles6bef6a72020-12-02 14:26:04 -05001136 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001137}
1138
Ethan Nicholasfc994162019-06-06 10:04:27 -04001139std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001140 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001141 case ASTNode::Kind::kBinary:
1142 return this->convertBinaryExpression(expr);
1143 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -05001144 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001145 case ASTNode::Kind::kCall:
1146 return this->convertCallExpression(expr);
1147 case ASTNode::Kind::kField:
1148 return this->convertFieldExpression(expr);
1149 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -05001150 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001151 case ASTNode::Kind::kIdentifier:
1152 return this->convertIdentifier(expr);
1153 case ASTNode::Kind::kIndex:
1154 return this->convertIndexExpression(expr);
1155 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -05001156 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001157 case ASTNode::Kind::kPostfix:
1158 return this->convertPostfixExpression(expr);
1159 case ASTNode::Kind::kPrefix:
1160 return this->convertPrefixExpression(expr);
1161 case ASTNode::Kind::kTernary:
1162 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001163 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001164 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001165 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001166 }
1167}
1168
Ethan Nicholas962dec42021-06-10 13:06:39 -04001169std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001170 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -07001171 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -05001172 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001173 return nullptr;
1174 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001175 switch (result->kind()) {
1176 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -07001177 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -04001178 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -07001179 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001180 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -07001181 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001182 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -04001183 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001184 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -07001185 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001186 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -04001187 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001188 const Modifiers& modifiers = var->modifiers();
1189 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -04001190 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -04001191 if (caps().canUseFragCoord()) {
1192 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001193 }
Brian Salomond8d85b92021-07-07 09:41:17 -04001194 break;
1195 case SK_CLOCKWISE_BUILTIN:
1196 fInputs.fUseFlipRTUniform = true;
1197 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001198 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001199 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -04001200 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001201 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001202 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001203 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -04001204 auto base = VariableReference::Make(offset, &field->owner(),
1205 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -05001206 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
1207 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001208 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001209 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001210 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001211 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001212 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001213 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001214 const ExternalFunction* r = &result->as<ExternalFunction>();
1215 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001216 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001217 default:
John Stilesf57207b2021-02-02 17:50:34 -05001218 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001219 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001220}
1221
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001222std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -04001223 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001224}
1225
Ethan Nicholas11d53972016-11-28 11:23:23 -05001226std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001227 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -05001228 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -07001229}
1230
Ethan Nicholasfc994162019-06-06 10:04:27 -04001231std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1232 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1233 auto iter = expression.begin();
1234 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001235 if (!left) {
1236 return nullptr;
1237 }
John Stiles0f464502020-11-20 12:52:22 -05001238 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001239 if (!right) {
1240 return nullptr;
1241 }
John Stiles23521a82021-03-02 17:02:51 -05001242 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
1243 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -07001244}
1245
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001246std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1247 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1248 auto iter = node.begin();
1249 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1250 if (!test) {
1251 return nullptr;
1252 }
1253 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1254 if (!ifTrue) {
1255 return nullptr;
1256 }
1257 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1258 if (!ifFalse) {
1259 return nullptr;
1260 }
John Stiles23521a82021-03-02 17:02:51 -05001261 return TernaryExpression::Convert(fContext, std::move(test),
1262 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001263}
1264
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001265void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001266 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001267 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001268
1269 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1270 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001271 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1272 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001273 std::sort(intrinsics.begin(), intrinsics.end(),
1274 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001275 if (a->isBuiltin() != b->isBuiltin()) {
1276 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001277 }
1278 if (a->fOffset != b->fOffset) {
1279 return a->fOffset < b->fOffset;
1280 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001281 if (a->name() != b->name()) {
1282 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001283 }
1284 return a->description() < b->description();
1285 });
1286 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001287 this->copyIntrinsicIfNeeded(*f);
1288 }
John Stiles607d36b2020-10-19 15:00:01 -04001289
Brian Osman0006ad02020-11-18 15:38:39 -05001290 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001291 }
1292}
1293
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001294std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001295 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001296 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001297 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001298 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001299 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001300 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001301 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001302 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001303 }
1304 }
John Stilesea9ab822020-08-31 09:55:04 -04001305
John Stilescd7ba502021-03-19 10:54:59 -04001306 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001307}
1308
1309/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001310 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001311 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1312 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001313 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001314CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001315 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001316 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1317 return CoercionCost::Impossible();
1318 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001319 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001320 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001321 }
John Stilesfa889112020-10-12 19:03:43 -04001322 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001323 const Type* ignored;
1324 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001325 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001326 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001327 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001328 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001329 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001330 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001331 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001332}
1333
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001334std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001335 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001336 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001337 switch (functionValue->kind()) {
1338 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001339 return Constructor::Convert(fContext,
1340 offset,
1341 functionValue->as<TypeReference>().value(),
1342 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001343 case Expression::Kind::kExternalFunctionReference: {
1344 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1345 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001346 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001347 this->errorReporter().error(offset, "external function expected " +
1348 to_string(count) + " arguments, but found " +
1349 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001350 return nullptr;
1351 }
1352 static constexpr int PARAMETER_MAX = 16;
1353 SkASSERT(count < PARAMETER_MAX);
1354 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001355 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001356 for (int i = 0; i < count; ++i) {
1357 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1358 if (!arguments[i]) {
1359 return nullptr;
1360 }
1361 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001362 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001363 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001364 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001365 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001366 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001367 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001368 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001369 if (functions.size() > 1) {
1370 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001371 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001372 if (cost < bestCost) {
1373 bestCost = cost;
1374 best = f;
1375 }
1376 }
1377 if (best) {
1378 return this->call(offset, *best, std::move(arguments));
1379 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001380 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001381 String separator;
1382 for (size_t i = 0; i < arguments.size(); i++) {
1383 msg += separator;
1384 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001385 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001386 }
1387 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001388 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001389 return nullptr;
1390 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001391 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001392 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001393 default:
John Stilesb30151e2021-01-11 16:13:08 -05001394 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001395 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001396 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001397}
1398
Ethan Nicholasfc994162019-06-06 10:04:27 -04001399std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1400 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1401 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001402 if (!base) {
1403 return nullptr;
1404 }
John Stiles23521a82021-03-02 17:02:51 -05001405 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001406}
1407
ethannicholasb3058bd2016-07-01 08:22:01 -07001408std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001409 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001410 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001411}
1412
Ethan Nicholasfc994162019-06-06 10:04:27 -04001413std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1414 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1415 auto iter = index.begin();
1416 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001417 if (!base) {
1418 return nullptr;
1419 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001420 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001421 if (base->is<TypeReference>()) {
1422 this->errorReporter().error(index.fOffset, "array must have a size");
1423 } else {
1424 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1425 }
Brian Osman4cf85072021-01-14 14:18:10 -05001426 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001427 }
1428 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1429 if (!converted) {
1430 return nullptr;
1431 }
John Stilesbb8cf582021-08-26 23:34:59 -04001432 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001433}
1434
Ethan Nicholasfc994162019-06-06 10:04:27 -04001435std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1436 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1437 auto iter = callNode.begin();
1438 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1439 if (!base) {
1440 return nullptr;
1441 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001442 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001443 for (; iter != callNode.end(); ++iter) {
1444 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1445 if (!converted) {
1446 return nullptr;
1447 }
1448 arguments.push_back(std::move(converted));
1449 }
1450 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1451}
1452
1453std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1454 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1455 if (!base) {
1456 return nullptr;
1457 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001458 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001459 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001460 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001461 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001462 }
John Stiles06d600f2021-03-08 09:18:21 -05001463 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001464}
1465
1466std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001467 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001468 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1469 if (!base) {
1470 return nullptr;
1471 }
John Stiles23521a82021-03-02 17:02:51 -05001472 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001473}
1474
1475void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001476 switch (expr.kind()) {
John Stilesdc435fa2020-12-30 18:07:10 -05001477 case Expression::Kind::kFunctionCall: {
1478 const FunctionDeclaration& decl = expr.as<FunctionCall>().function();
1479 if (!decl.isBuiltin() && !decl.definition()) {
John Stilesb30151e2021-01-11 16:13:08 -05001480 this->errorReporter().error(expr.fOffset,
1481 "function '" + decl.description() + "' is not defined");
John Stilesdc435fa2020-12-30 18:07:10 -05001482 }
1483 break;
1484 }
John Stiles973009b2021-02-17 17:18:01 -05001485 case Expression::Kind::kFunctionReference:
1486 case Expression::Kind::kTypeReference:
1487 SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()");
1488 this->errorReporter().error(expr.fOffset, "invalid expression");
1489 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001490 default:
John Stiles54e7c052021-01-11 14:22:36 -05001491 if (expr.type() == *fContext.fTypes.fInvalid) {
John Stilesb30151e2021-01-11 16:13:08 -05001492 this->errorReporter().error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07001493 }
John Stiles973009b2021-02-17 17:18:01 -05001494 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001495 }
1496}
1497
Brian Osman9496fe52020-11-18 14:48:19 -05001498void IRGenerator::findAndDeclareBuiltinVariables() {
1499 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001500 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001501 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001502
Brian Osman9496fe52020-11-18 14:48:19 -05001503 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001504 // If this is the *first* time we've seen this builtin, findAndInclude will return
1505 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001506 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001507 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001508 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001509 }
1510 }
1511
John Stiles4d6310a2021-01-26 19:58:22 -05001512 bool visitProgramElement(const ProgramElement& pe) override {
1513 if (pe.is<FunctionDefinition>()) {
1514 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1515 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1516 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001517 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001518 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1519 fPreserveFragColor = true;
1520 }
1521 }
1522 return INHERITED::visitProgramElement(pe);
1523 }
1524
Brian Osman9496fe52020-11-18 14:48:19 -05001525 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001526 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001527 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001528 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001529 return INHERITED::visitExpression(e);
1530 }
1531
1532 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001533 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001534 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001535
Brian Osman9496fe52020-11-18 14:48:19 -05001536 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001537 using INHERITED::visitProgramElement;
1538 };
1539
Brian Osman9496fe52020-11-18 14:48:19 -05001540 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001541 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001542 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001543 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001544 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001545
John Stiles4d6310a2021-01-26 19:58:22 -05001546 if (scanner.fPreserveFragColor) {
1547 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001548 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001549 }
1550
John Stilesd1204642021-02-17 16:30:02 -05001551 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001552 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001553 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1554 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1555 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001556 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001557 break;
1558 default:
1559 break;
1560 }
1561
Brian Osman9496fe52020-11-18 14:48:19 -05001562 fSharedElements->insert(
1563 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001564}
1565
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001566void IRGenerator::start(const ParsedModule& base,
1567 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001568 std::vector<std::unique_ptr<ProgramElement>>* elements,
1569 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001570 fProgramElements = elements;
1571 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001572 fSymbolTable = base.fSymbols;
1573 fIntrinsics = base.fIntrinsics.get();
1574 if (fIntrinsics) {
1575 fIntrinsics->resetAlreadyIncluded();
1576 }
1577 fIsBuiltinCode = isBuiltinCode;
1578
Brian Salomond8d85b92021-07-07 09:41:17 -04001579 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001580 fInvocations = -1;
1581 fRTAdjust = nullptr;
1582 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001583 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001584 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001585
John Stilesd1204642021-02-17 16:30:02 -05001586 if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) {
Brian Osman68c1d402020-10-12 16:36:38 -04001587 // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin.
1588 // If we're applying the workaround, then it's a plain global.
John Stilesc1a98b82021-02-24 13:35:02 -05001589 bool workaround = !this->caps().gsInvocationsSupport();
Brian Osman68c1d402020-10-12 16:36:38 -04001590 Modifiers m;
1591 if (!workaround) {
1592 m.fFlags = Modifiers::kIn_Flag;
1593 m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN;
1594 }
John Stilesf2872e62021-05-04 11:38:43 -04001595 auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m),
John Stilese67bd132021-03-19 18:39:25 -04001596 "sk_InvocationID", fContext.fTypes.fInt.get(),
1597 /*builtin=*/false, Variable::Storage::kGlobal);
1598 auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(),
1599 /*arraySize=*/0, /*value=*/nullptr);
Brian Osman68c1d402020-10-12 16:36:38 -04001600 fSymbolTable->add(std::move(var));
Ethan Nicholas624a5292021-04-16 14:54:43 -04001601 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl)));
Brian Osman68c1d402020-10-12 16:36:38 -04001602 }
1603
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001604 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001605 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001606 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001607 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001608 }
1609 }
John Stilesaecf8d52021-05-14 12:15:01 -04001610
John Stilesbb2ef922021-07-26 08:32:07 -04001611 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001612 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001613 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001614 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1615 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1616 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1617 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1618 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1619 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1620 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1621 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1622 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1623
1624 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1625 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1626 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1627 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1628 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1629 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1630
1631 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1632 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1633 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1634 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1635 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1636 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001637
1638 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1639 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1640 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1641 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001642
1643 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1644 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1645 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1646 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1647
1648 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1649 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1650 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1651 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001652 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001653}
Brian Osman88cda172020-10-09 12:05:16 -04001654
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001655IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001656 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001657 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001658 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001659 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001660
Brian Osmanea485e52021-01-15 13:20:32 -05001661 // Do a pass looking for dangling FunctionReference or TypeReference expressions
Brian Osmanc95d3a12020-09-09 10:56:27 -04001662 class FindIllegalExpressions : public ProgramVisitor {
1663 public:
1664 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
1665
1666 bool visitExpression(const Expression& e) override {
1667 fGenerator->checkValid(e);
1668 return INHERITED::visitExpression(e);
1669 }
1670
1671 IRGenerator* fGenerator;
1672 using INHERITED = ProgramVisitor;
1673 using INHERITED::visitProgramElement;
1674 };
1675 for (const auto& pe : *fProgramElements) {
1676 FindIllegalExpressions{this}.visitProgramElement(*pe);
1677 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001678
Brian Osmanea485e52021-01-15 13:20:32 -05001679 // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the
1680 // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic
1681 // assumes that all loops meet the criteria of Section 4, and if they don't, could crash.
1682 if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) {
1683 for (const auto& pe : *fProgramElements) {
1684 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
1685 }
1686 }
1687
Brian Osman7da06572021-07-21 15:19:34 -04001688 if (this->strictES2Mode()) {
1689 Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter());
1690 }
1691
John Stilesf2872e62021-05-04 11:38:43 -04001692 return IRBundle{std::move(*fProgramElements),
1693 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001694 std::move(fSymbolTable),
1695 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001696}
1697
1698IRGenerator::IRBundle IRGenerator::convertProgram(
1699 const ParsedModule& base,
1700 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001701 skstd::string_view text) {
1702 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001703 fFile = parser.compilationUnit();
1704 if (this->errorReporter().errorCount() == 0) {
1705 SkASSERT(fFile);
1706 for (const auto& decl : fFile->root()) {
1707 switch (decl.fKind) {
1708 case ASTNode::Kind::kVarDeclarations:
1709 this->convertGlobalVarDeclarations(decl);
1710 break;
1711
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001712 case ASTNode::Kind::kFunction:
1713 this->convertFunction(decl);
1714 break;
1715
1716 case ASTNode::Kind::kModifiers: {
1717 std::unique_ptr<ModifiersDeclaration> f =
1718 this->convertModifiersDeclaration(decl);
1719 if (f) {
1720 fProgramElements->push_back(std::move(f));
1721 }
1722 break;
1723 }
1724 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001725 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001726 if (i) {
1727 fProgramElements->push_back(std::move(i));
1728 }
1729 break;
1730 }
1731 case ASTNode::Kind::kExtension: {
1732 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001733 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001734 if (e) {
1735 fProgramElements->push_back(std::move(e));
1736 }
1737 break;
1738 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001739 case ASTNode::Kind::kType: {
1740 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1741 if (s) {
1742 fProgramElements->push_back(std::move(s));
1743 }
1744 break;
1745 }
1746 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001747 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001748 break;
1749 }
1750 }
1751 }
1752 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001753}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001754
John Stilesa6841be2020-08-06 14:11:56 -04001755} // namespace SkSL