blob: 51ced681ae94db2e9bebdd9fb3290c3cb8171d5d [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
Ethan Nicholas962dec42021-06-10 13:06:39 -040094std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -050095 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -040096 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -050097 this->errorReporter().error(offset, "extensions are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -040098 return nullptr;
99 }
100
John Stilesfbd050b2020-08-03 13:21:46 -0400101 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700102}
103
John Stiles20c09f92021-03-12 09:25:04 -0500104std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400106 case ASTNode::Kind::kBlock:
107 return this->convertBlock(statement);
108 case ASTNode::Kind::kVarDeclarations:
109 return this->convertVarDeclarationStatement(statement);
110 case ASTNode::Kind::kIf:
111 return this->convertIf(statement);
112 case ASTNode::Kind::kFor:
113 return this->convertFor(statement);
114 case ASTNode::Kind::kWhile:
115 return this->convertWhile(statement);
116 case ASTNode::Kind::kDo:
117 return this->convertDo(statement);
118 case ASTNode::Kind::kSwitch:
119 return this->convertSwitch(statement);
120 case ASTNode::Kind::kReturn:
121 return this->convertReturn(statement);
122 case ASTNode::Kind::kBreak:
123 return this->convertBreak(statement);
124 case ASTNode::Kind::kContinue:
125 return this->convertContinue(statement);
126 case ASTNode::Kind::kDiscard:
127 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500128 case ASTNode::Kind::kType:
129 // TODO: add IRNode for struct definition inside a function
130 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400131 default:
132 // it's an expression
Brian Osman99ddd2a2021-08-27 11:21:12 -0400133 return this->convertExpressionStatement(statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134 }
135}
136
Ethan Nicholasfc994162019-06-06 10:04:27 -0400137std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
138 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700139 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400140 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400141 for (const auto& child : block) {
142 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700143 if (!statement) {
144 return nullptr;
145 }
146 statements.push_back(std::move(statement));
147 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500148 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700149}
150
Ethan Nicholasfc994162019-06-06 10:04:27 -0400151std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
152 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400153 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400154 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700155 return nullptr;
156 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500157 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700158}
159
John Stiles80b02af2021-02-12 17:07:51 -0500160int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500161 if (!s) {
162 this->errorReporter().error(offset, "array must have a size");
163 return 0;
164 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500165 auto size = this->convertExpression(s);
166 if (!size) {
167 return 0;
168 }
John Stiles7253a202021-08-23 09:24:33 -0400169 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500170}
171
Ethan Nicholas489e5522021-01-20 10:53:11 -0500172void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
173 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500174 if (this->strictES2Mode() && baseType->isArray()) {
175 this->errorReporter().error(offset, "array size must appear after variable name");
176 }
177
John Stiles3624aba2020-12-22 16:29:53 -0500178 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500179 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500180 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500181 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400182 }
Brian Osman8c264792021-07-01 16:41:27 -0400183 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
184 this->errorReporter().error(offset, "'in' variables may not have matrix type");
185 }
186 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
187 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500188 }
John Stilesbb2ef922021-07-26 08:32:07 -0400189 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400190 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400191 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400192 }
193 }
Brian Osman5c625822021-04-06 14:28:07 -0400194 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
195 this->errorReporter().error(
196 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
197 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400198 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400199 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500200 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500201 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400202 }
203 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500204 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500205 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400206 }
207 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500208 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400209 (t.columns() == 3 || t.columns() == 4);
210 };
John Stilesc0c51062020-12-03 17:16:29 -0500211 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400212 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500213 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500214 "'srgb_unpremul' is only permitted on half3, half4, "
215 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400216 }
217 }
John Stilesb806da42021-08-04 16:26:47 -0400218 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
219 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400220 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500221 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400222 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400223 }
Brian Osmana654faa2021-02-26 11:52:59 -0500224 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400225 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500226}
227
Ethan Nicholasbd974002021-02-22 16:20:06 -0500228std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400229 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500230 bool isArray,
231 std::unique_ptr<Expression> arraySize,
232 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500233 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500234 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400235 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500236 this->errorReporter().error(offset,
237 "out location=0, index=0 is reserved for sk_FragColor");
238 }
239 const Type* type = baseType;
240 int arraySizeValue = 0;
241 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500242 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400243 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500244 if (!arraySizeValue) {
245 return {};
246 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500247 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
248 }
John Stilesf2872e62021-05-04 11:38:43 -0400249 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
250 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500251}
252
253std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400254 std::unique_ptr<Expression> value,
255 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400256 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
257 std::move(value));
258 if (!varDecl) {
259 return nullptr;
260 }
261
262 // Detect the declaration of magical variables.
263 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
264 // Silently ignore duplicate definitions of `sk_FragColor`.
265 const Symbol* symbol = (*fSymbolTable)[var->name()];
266 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500267 return nullptr;
268 }
John Stilese67bd132021-03-19 18:39:25 -0400269 } else if ((var->storage() == Variable::Storage::kGlobal ||
270 var->storage() == Variable::Storage::kInterfaceBlock) &&
271 var->name() == Compiler::RTADJUST_NAME) {
272 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
273 if (fRTAdjust) {
274 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500275 return nullptr;
276 }
John Stilese67bd132021-03-19 18:39:25 -0400277 if (var->type() != *fContext.fTypes.fFloat4) {
278 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500279 return nullptr;
280 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500281 fRTAdjust = var.get();
282 }
John Stilese67bd132021-03-19 18:39:25 -0400283
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400284 if (addToSymbolTable) {
285 fSymbolTable->add(std::move(var));
286 } else {
287 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
288 }
John Stilese67bd132021-03-19 18:39:25 -0400289 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500290}
291
292std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
293 const Modifiers& modifiers,
294 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400295 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500296 bool isArray,
297 std::unique_ptr<Expression> arraySize,
298 std::unique_ptr<Expression> value,
299 Variable::Storage storage) {
300 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
301 std::move(arraySize), storage);
302 if (!var) {
303 return nullptr;
304 }
305 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500306}
307
308StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
309 Variable::Storage storage) {
310 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
311 auto declarationsIter = decls.begin();
312 const Modifiers& modifiers = declarationsIter++->getModifiers();
313 const ASTNode& rawType = *(declarationsIter++);
314 const Type* baseType = this->convertType(rawType);
315 if (!baseType) {
316 return {};
317 }
John Stilesb806da42021-08-04 16:26:47 -0400318 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
319 decls.fOffset);
320 if (!baseType) {
321 return {};
322 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500323
324 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400325
326 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400327 for (; declarationsIter != decls.end(); ++declarationsIter) {
328 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400329 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500330 std::unique_ptr<Expression> arraySize;
331 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400332 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500333 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400334 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500335 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400336 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700337 }
John Stiles8f633ef2021-08-21 17:13:11 -0400338 arraySize = this->convertExpression(*iter++);
339 if (!arraySize) {
340 continue;
341 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700342 }
Brian Osman4cf85072021-01-14 14:18:10 -0500343 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400344 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700345 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400346 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700347 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700348 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500349 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
350 modifiers,
351 baseType,
352 varData.fName,
353 varData.fIsArray,
354 std::move(arraySize),
355 std::move(value),
356 storage);
357 if (varDeclStmt) {
358 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700359 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700360 }
Brian Osmanc0213602020-10-06 14:43:32 -0400361 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700362}
363
Ethan Nicholasfc994162019-06-06 10:04:27 -0400364std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500365 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400366 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -0500367 this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400368 return nullptr;
369 }
370
Ethan Nicholasfc994162019-06-06 10:04:27 -0400371 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
372 Modifiers modifiers = m.getModifiers();
John Stilesf2872e62021-05-04 11:38:43 -0400373 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700374}
375
John Stilesad2319f2020-09-02 15:01:47 -0400376std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
377 SkASSERT(n.fKind == ASTNode::Kind::kIf);
378 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500379 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400380 if (!test) {
381 return nullptr;
382 }
383 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
384 if (!ifTrue) {
385 return nullptr;
386 }
John Stilesad2319f2020-09-02 15:01:47 -0400387 std::unique_ptr<Statement> ifFalse;
388 if (iter != n.end()) {
389 ifFalse = this->convertStatement(*(iter++));
390 if (!ifFalse) {
391 return nullptr;
392 }
John Stilesad2319f2020-09-02 15:01:47 -0400393 }
John Stiles5ede6e32021-01-11 13:00:49 -0500394 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500395 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
396 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400397}
398
Ethan Nicholasfc994162019-06-06 10:04:27 -0400399std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
400 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700401 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700402 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400403 auto iter = f.begin();
404 if (*iter) {
405 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700406 if (!initializer) {
407 return nullptr;
408 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700409 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400410 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700411 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500413 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700414 if (!test) {
415 return nullptr;
416 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700417 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400418 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700419 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400420 if (*iter) {
421 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700422 if (!next) {
423 return nullptr;
424 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400426 ++iter;
427 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700428 if (!statement) {
429 return nullptr;
430 }
Brian Osman77ba8102021-01-12 17:15:30 -0500431
John Stiles23521a82021-03-02 17:02:51 -0500432 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
433 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500434}
435
436std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
437 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400438 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500439 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700440 if (!test) {
441 return nullptr;
442 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400443 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700444 if (!statement) {
445 return nullptr;
446 }
John Stiles23521a82021-03-02 17:02:51 -0500447 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
448 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700449}
450
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500451std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
452 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400453 auto iter = d.begin();
454 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
455 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700456 return nullptr;
457 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500458 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400459 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700460 return nullptr;
461 }
John Stiles23521a82021-03-02 17:02:51 -0500462 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700463}
464
Ethan Nicholascfefec02021-02-09 15:22:57 -0500465std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
466 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
467
468 auto iter = s.begin();
469 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
470 if (!value) {
471 return nullptr;
472 }
473 AutoSymbolTable table(this);
474 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500475 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500476 for (; iter != s.end(); ++iter) {
477 const ASTNode& c = *iter;
478 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
479 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
480 auto childIter = c.begin();
481 if (*childIter) {
482 caseValue = this->convertExpression(*childIter);
483 if (!caseValue) {
484 return nullptr;
485 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500486 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400487 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500488
John Stilesbf16b6c2021-03-12 19:24:31 -0500489 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400490 for (; childIter != c.end(); ++childIter) {
491 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500492 if (!converted) {
493 return nullptr;
494 }
495 statements.push_back(std::move(converted));
496 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500497
498 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500499 }
John Stiles23521a82021-03-02 17:02:51 -0500500 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
501 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500502}
503
Ethan Nicholasfc994162019-06-06 10:04:27 -0400504std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
505 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700506 if (!e) {
507 return nullptr;
508 }
John Stiles3e5871c2021-02-25 20:52:03 -0500509 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700510}
511
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500512std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
513 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500514 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500515}
516
517std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
518 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
519 if (r.begin() != r.end()) {
520 std::unique_ptr<Expression> value = this->convertExpression(*r.begin());
521 if (!value) {
522 return nullptr;
523 }
524 return this->convertReturn(r.fOffset, std::move(value));
525 } else {
526 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700527 }
528}
529
Ethan Nicholasfc994162019-06-06 10:04:27 -0400530std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
531 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500532 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700533}
534
Ethan Nicholasfc994162019-06-06 10:04:27 -0400535std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
536 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500537 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700538}
539
Ethan Nicholasfc994162019-06-06 10:04:27 -0400540std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
541 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400542 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500543 this->errorReporter().error(d.fOffset,
544 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500545 return nullptr;
546 }
John Stilesa0c04d62021-03-11 23:07:24 -0500547 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700548}
549
John Stiles3b204892021-08-27 17:35:35 -0400550void IRGenerator::appendRTAdjustFixupToVertexMain(const FunctionDeclaration& decl, Block* body) {
John Stilesd45ebbc2021-05-18 16:54:36 -0400551 using namespace SkSL::dsl;
552 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
John Stiles3b204892021-08-27 17:35:35 -0400553 using OwnerKind = SkSL::FieldAccess::OwnerKind;
John Stilesd45ebbc2021-05-18 16:54:36 -0400554
John Stiles3b204892021-08-27 17:35:35 -0400555 // If this is a vertex program that uses RTAdjust, and this is main()...
Ethan Nicholasa97ad0f2021-09-02 09:54:04 -0400556 if ((fRTAdjust || fRTAdjustInterfaceBlock) && decl.isMain() &&
557 ProgramKind::kVertex == this->programKind()) {
John Stiles3b204892021-08-27 17:35:35 -0400558 // ... append a line to the end of the function body which fixes up sk_Position.
559 const Variable* skPerVertex = nullptr;
560 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
561 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
562 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
563 }
564
565 SkASSERT(skPerVertex);
566 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
567 return VariableReference::Make(/*offset=*/-1, var);
568 };
569 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
570 return FieldAccess::Make(fContext, Ref(var), idx, OwnerKind::kAnonymousInterfaceBlock);
571 };
572 auto Pos = [&]() -> DSLExpression {
573 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
574 OwnerKind::kAnonymousInterfaceBlock));
575 };
576 auto Adjust = [&]() -> DSLExpression {
577 return DSLExpression(fRTAdjustInterfaceBlock
578 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
579 : Ref(fRTAdjust));
580 };
581
582 auto fixupStmt = DSLStatement(
583 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
584 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
585 0,
586 Pos().w())
587 );
588
589 body->children().push_back(fixupStmt.release());
Brian Osman88cda172020-10-09 12:05:16 -0400590 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000591}
592
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400593void IRGenerator::CheckModifiers(const Context& context,
594 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500595 const Modifiers& modifiers,
596 int permittedModifierFlags,
597 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400598 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
599 { Modifiers::kConst_Flag, "const" },
600 { Modifiers::kIn_Flag, "in" },
601 { Modifiers::kOut_Flag, "out" },
602 { Modifiers::kUniform_Flag, "uniform" },
603 { Modifiers::kFlat_Flag, "flat" },
604 { Modifiers::kNoPerspective_Flag, "noperspective" },
605 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
606 { Modifiers::kInline_Flag, "inline" },
607 { Modifiers::kNoInline_Flag, "noinline" },
608 { Modifiers::kHighp_Flag, "highp" },
609 { Modifiers::kMediump_Flag, "mediump" },
610 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400611 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500612 };
613
Brian Osmanb5de6be2021-08-10 15:16:34 -0400614 int modifierFlags = modifiers.fFlags;
615 for (const auto& f : kModifierFlags) {
616 if (modifierFlags & f.flag) {
617 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400618 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400619 }
620 modifierFlags &= ~f.flag;
621 }
622 }
623 SkASSERT(modifierFlags == 0);
624
625 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
626 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
627 { Layout::kPushConstant_Flag, "push_constant"},
628 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
629 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
630 { Layout::kLocation_Flag, "location"},
631 { Layout::kOffset_Flag, "offset"},
632 { Layout::kBinding_Flag, "binding"},
633 { Layout::kIndex_Flag, "index"},
634 { Layout::kSet_Flag, "set"},
635 { Layout::kBuiltin_Flag, "builtin"},
636 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
Brian Osmanb5de6be2021-08-10 15:16:34 -0400637 };
Brian Osmana654faa2021-02-26 11:52:59 -0500638
639 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400640 for (const auto& lf : kLayoutFlags) {
641 if (layoutFlags & lf.flag) {
642 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400643 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400644 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500645 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400646 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500647 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400648 }
Brian Osmana654faa2021-02-26 11:52:59 -0500649 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400650}
651
Ethan Nicholasfc994162019-06-06 10:04:27 -0400652void IRGenerator::convertFunction(const ASTNode& f) {
653 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400654 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400655 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400656 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700657 }
John Stilesb9af7232020-08-20 15:57:48 -0400658 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400659 std::vector<std::unique_ptr<Variable>> parameters;
660 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400661 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400662 const ASTNode& param = *(iter++);
663 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500664 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400665 auto paramIter = param.begin();
666 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700667 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400668 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700669 }
John Stilesd39aec92020-12-03 14:37:16 -0500670 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500671 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500672 if (!arraySize) {
673 return;
674 }
John Stilesa2179502020-12-03 14:37:57 -0500675 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700676 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400677
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400678 parameters.push_back(std::make_unique<Variable>(param.fOffset,
679 this->modifiersPool().add(pd.fModifiers),
680 pd.fName,
681 type,
682 fIsBuiltinCode,
683 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700684 }
685
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400686 // Conservatively assume all user-defined functions have side effects.
687 Modifiers declModifiers = funcData.fModifiers;
688 if (!fIsBuiltinCode) {
689 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400690 }
691
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400692 if (fContext.fConfig->fSettings.fForceNoInline) {
693 // Apply the `noinline` modifier to every function. This allows us to test Runtime
694 // Effects without any inlining, even when the code is later added to a paint.
695 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
696 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700697 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400698
699 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
700 fContext,
701 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400702 f.fOffset,
703 this->modifiersPool().add(declModifiers),
704 funcData.fName,
705 std::move(parameters),
706 returnType,
707 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700708 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400709 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 }
John Stiles569249b2020-11-03 12:18:22 -0500711 if (iter == f.end()) {
712 // If there's no body, we've found a prototype.
713 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
714 fIsBuiltinCode));
715 } else {
716 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700717 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500718 for (const Variable* param : decl->parameters()) {
719 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700720 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400721 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700722 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400723 return;
724 }
John Stiles3b204892021-08-27 17:35:35 -0400725 this->appendRTAdjustFixupToVertexMain(*decl, body.get());
726 std::unique_ptr<FunctionDefinition> result = FunctionDefinition::Convert(
727 fContext, f.fOffset, *decl, std::move(body), fIsBuiltinCode);
Ethan Nicholased84b732020-10-08 11:45:44 -0400728 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400729 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500730 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700731 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700732}
733
John Stilesdc75a972020-11-25 16:24:55 -0500734std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
735 SkASSERT(node.fKind == ASTNode::Kind::kType);
736
737 const Type* type = this->convertType(node);
738 if (!type) {
739 return nullptr;
740 }
John Stilesc0c51062020-12-03 17:16:29 -0500741 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500742 this->errorReporter().error(node.fOffset,
743 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500744 return nullptr;
745 }
Brian Osman02bc5222021-01-28 11:00:20 -0500746 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
747 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500748 return std::make_unique<StructDefinition>(node.fOffset, *type);
749}
750
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400751void IRGenerator::scanInterfaceBlock(SkSL::InterfaceBlock& intf) {
752 const std::vector<Type::Field>& fields = intf.variable().type().componentType().fields();
753 for (size_t i = 0; i < fields.size(); ++i) {
754 const Type::Field& f = fields[i];
755 if (f.fName == Compiler::RTADJUST_NAME) {
756 if (*f.fType == *fContext.fTypes.fFloat4) {
757 fRTAdjustInterfaceBlock = &intf.variable();
758 fRTAdjustFieldIndex = i;
759 } else {
760 this->errorReporter().error(intf.fOffset, "sk_RTAdjust must have type 'float4'");
761 }
762 }
763 }
764}
765
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400766std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500767 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400768 this->programKind() != ProgramKind::kVertex) {
John Stilesb30151e2021-01-11 16:13:08 -0500769 this->errorReporter().error(intf.fOffset, "interface block is not allowed here");
Brian Osman16f376f2020-09-02 12:30:59 -0400770 return nullptr;
771 }
772
Ethan Nicholasfc994162019-06-06 10:04:27 -0400773 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500774 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700775 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400776 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700777 std::vector<Type::Field> fields;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400778 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400779 {
780 AutoSymbolTable table(this);
781 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400782 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
783 StatementArray decls = this->convertVarDeclarations(*(iter++),
784 Variable::Storage::kInterfaceBlock);
785 if (decls.empty()) {
786 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400787 }
John Stiles869cdef2020-10-30 14:24:24 -0400788 for (const auto& decl : decls) {
789 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -0400790 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
791 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400792 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500793 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700794 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400795 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400796 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -0500797 fields));
John Stilesd39aec02020-12-03 10:42:26 -0500798 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -0500799 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400800 const ASTNode& size = *(iter++);
Brian Osmanb42c3832021-08-27 15:19:03 -0400801 arraySize = this->convertArraySize(*type, size.fOffset, size);
802 if (!arraySize) {
803 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500804 }
John Stilesa2179502020-12-03 14:37:57 -0500805 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500806 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400807 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400808 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -0400809 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400810 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400811 type,
Brian Osman3887a012020-09-30 13:22:27 -0400812 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400813 Variable::Storage::kGlobal));
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400814 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -0400815 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700816 } else {
817 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -0400818 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -0700819 }
820 }
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400821 std::unique_ptr<SkSL::InterfaceBlock> result = std::make_unique<SkSL::InterfaceBlock>(
822 intf.fOffset, var, id.fTypeName, id.fInstanceName, arraySize, symbols);
823 this->scanInterfaceBlock(*result);
824 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700825}
826
John Stiles7bd70332020-11-30 17:04:09 -0500827void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
828 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
829 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -0500830 const Type* type = &stmt->as<VarDeclaration>().baseType();
831 if (type->isStruct()) {
832 auto [iter, wasInserted] = fDefinedStructs.insert(type);
833 if (wasInserted) {
834 fProgramElements->push_back(
835 std::make_unique<StructDefinition>(decl.fOffset, *type));
836 }
837 }
Ethan Nicholas624a5292021-04-16 14:54:43 -0400838 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -0500839 }
840}
841
John Stiles823c5042021-08-17 12:09:00 -0400842static bool type_contains_private_fields(const Type& type) {
John Stilesb4b627e2020-11-13 15:55:27 -0500843 // Checks for usage of private types, including fields inside a struct.
844 if (type.isPrivate()) {
845 return true;
846 }
John Stilesc0c51062020-12-03 17:16:29 -0500847 if (type.isStruct()) {
John Stilesb4b627e2020-11-13 15:55:27 -0500848 for (const auto& f : type.fields()) {
John Stiles823c5042021-08-17 12:09:00 -0400849 if (type_contains_private_fields(*f.fType)) {
John Stilesb4b627e2020-11-13 15:55:27 -0500850 return true;
851 }
852 }
853 }
854 return false;
855}
856
Brian Osmand8070392020-09-09 15:50:02 -0400857const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400858 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -0500859 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -0500860 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -0500861 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -0500862 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700863 }
John Stiles6bef6a72020-12-02 14:26:04 -0500864 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -0500865 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -0400866 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -0500867 this->errorReporter().error(type.fOffset,
868 "type '" + name + "' not allowed in this context");
869 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -0500870 }
John Stiles823c5042021-08-17 12:09:00 -0400871 if (!fIsBuiltinCode) {
872 if (type_contains_private_fields(*result)) {
873 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
874 return nullptr;
875 }
876 if (this->strictES2Mode() && !result->allowedInES2()) {
877 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
878 return nullptr;
879 }
John Stiles7bd70332020-11-30 17:04:09 -0500880 }
John Stiles6bef6a72020-12-02 14:26:04 -0500881 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -0500882 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -0500883 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -0500884 if (!arraySize) {
885 return nullptr;
886 }
John Stilesa2179502020-12-03 14:37:57 -0500887 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -0500888 }
John Stiles6bef6a72020-12-02 14:26:04 -0500889 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700890}
891
Ethan Nicholasfc994162019-06-06 10:04:27 -0400892std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700893 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400894 case ASTNode::Kind::kBinary:
895 return this->convertBinaryExpression(expr);
896 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -0500897 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400898 case ASTNode::Kind::kCall:
899 return this->convertCallExpression(expr);
900 case ASTNode::Kind::kField:
901 return this->convertFieldExpression(expr);
902 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -0500903 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400904 case ASTNode::Kind::kIdentifier:
905 return this->convertIdentifier(expr);
906 case ASTNode::Kind::kIndex:
907 return this->convertIndexExpression(expr);
908 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -0500909 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400910 case ASTNode::Kind::kPostfix:
911 return this->convertPostfixExpression(expr);
912 case ASTNode::Kind::kPrefix:
913 return this->convertPrefixExpression(expr);
914 case ASTNode::Kind::kTernary:
915 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700916 default:
John Stilesfbb905e2021-08-25 15:51:50 -0400917 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500918 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700919 }
920}
921
Ethan Nicholas962dec42021-06-10 13:06:39 -0400922std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500923 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -0700924 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -0500925 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700926 return nullptr;
927 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400928 switch (result->kind()) {
929 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -0700930 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -0400931 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -0700932 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500933 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700934 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400935 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400936 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500937 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -0700938 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400939 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400940 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400941 const Modifiers& modifiers = var->modifiers();
942 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400943 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -0400944 if (caps().canUseFragCoord()) {
945 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400946 }
Brian Salomond8d85b92021-07-07 09:41:17 -0400947 break;
948 case SK_CLOCKWISE_BUILTIN:
949 fInputs.fUseFlipRTUniform = true;
950 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400951 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500952 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -0400953 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -0700954 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400955 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400956 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -0400957 auto base = VariableReference::Make(offset, &field->owner(),
958 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -0500959 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
960 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700961 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400962 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400963 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500964 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -0700965 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400966 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500967 const ExternalFunction* r = &result->as<ExternalFunction>();
968 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400969 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700970 default:
John Stilesf57207b2021-02-02 17:50:34 -0500971 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -0700972 }
Ethan Nicholasc0709392017-06-27 11:20:22 -0400973}
974
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500975std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400976 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500977}
978
Ethan Nicholas11d53972016-11-28 11:23:23 -0500979std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700980 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -0500981 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -0700982}
983
Ethan Nicholasfc994162019-06-06 10:04:27 -0400984std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
985 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
986 auto iter = expression.begin();
987 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700988 if (!left) {
989 return nullptr;
990 }
John Stiles0f464502020-11-20 12:52:22 -0500991 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700992 if (!right) {
993 return nullptr;
994 }
John Stiles23521a82021-03-02 17:02:51 -0500995 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
996 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -0700997}
998
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500999std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1000 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1001 auto iter = node.begin();
1002 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
1003 if (!test) {
1004 return nullptr;
1005 }
1006 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1007 if (!ifTrue) {
1008 return nullptr;
1009 }
1010 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1011 if (!ifFalse) {
1012 return nullptr;
1013 }
John Stiles23521a82021-03-02 17:02:51 -05001014 return TernaryExpression::Convert(fContext, std::move(test),
1015 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001016}
1017
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001018void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001019 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001020 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001021
1022 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1023 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001024 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1025 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001026 std::sort(intrinsics.begin(), intrinsics.end(),
1027 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001028 if (a->isBuiltin() != b->isBuiltin()) {
1029 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001030 }
1031 if (a->fOffset != b->fOffset) {
1032 return a->fOffset < b->fOffset;
1033 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001034 if (a->name() != b->name()) {
1035 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001036 }
1037 return a->description() < b->description();
1038 });
1039 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001040 this->copyIntrinsicIfNeeded(*f);
1041 }
John Stiles607d36b2020-10-19 15:00:01 -04001042
Brian Osman0006ad02020-11-18 15:38:39 -05001043 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001044 }
1045}
1046
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001047std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001048 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001049 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001050 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001051 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001052 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001053 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001054 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001055 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001056 }
1057 }
John Stilesea9ab822020-08-31 09:55:04 -04001058
John Stilescd7ba502021-03-19 10:54:59 -04001059 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001060}
1061
1062/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001063 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001064 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1065 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001066 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001067CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001068 const ExpressionArray& arguments) {
John Stilesefde90d2021-08-12 23:06:24 -04001069 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1070 return CoercionCost::Impossible();
1071 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001072 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001073 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001074 }
John Stilesfa889112020-10-12 19:03:43 -04001075 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001076 const Type* ignored;
1077 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001078 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001079 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001080 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001081 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001082 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001083 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001084 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001085}
1086
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001087std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001088 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001089 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001090 switch (functionValue->kind()) {
1091 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001092 return Constructor::Convert(fContext,
1093 offset,
1094 functionValue->as<TypeReference>().value(),
1095 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001096 case Expression::Kind::kExternalFunctionReference: {
1097 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1098 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001099 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001100 this->errorReporter().error(offset, "external function expected " +
1101 to_string(count) + " arguments, but found " +
1102 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001103 return nullptr;
1104 }
1105 static constexpr int PARAMETER_MAX = 16;
1106 SkASSERT(count < PARAMETER_MAX);
1107 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001108 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001109 for (int i = 0; i < count; ++i) {
1110 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1111 if (!arguments[i]) {
1112 return nullptr;
1113 }
1114 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001115 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001116 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001117 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001118 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001119 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04001120 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001121 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04001122 if (functions.size() > 1) {
1123 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001124 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001125 if (cost < bestCost) {
1126 bestCost = cost;
1127 best = f;
1128 }
1129 }
1130 if (best) {
1131 return this->call(offset, *best, std::move(arguments));
1132 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001133 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001134 String separator;
1135 for (size_t i = 0; i < arguments.size(); i++) {
1136 msg += separator;
1137 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04001138 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001139 }
1140 msg += ")";
John Stilesb30151e2021-01-11 16:13:08 -05001141 this->errorReporter().error(offset, msg);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001142 return nullptr;
1143 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04001144 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001145 }
Ethan Nicholasace470b2021-08-30 16:15:20 -04001146 case Expression::Kind::kPoison:
1147 return functionValue;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001148 default:
John Stilesb30151e2021-01-11 16:13:08 -05001149 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001150 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001151 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001152}
1153
Ethan Nicholasfc994162019-06-06 10:04:27 -04001154std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1155 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1156 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001157 if (!base) {
1158 return nullptr;
1159 }
John Stiles23521a82021-03-02 17:02:51 -05001160 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001161}
1162
ethannicholasb3058bd2016-07-01 08:22:01 -07001163std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001164 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001165 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001166}
1167
Ethan Nicholasfc994162019-06-06 10:04:27 -04001168std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1169 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1170 auto iter = index.begin();
1171 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001172 if (!base) {
1173 return nullptr;
1174 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001175 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001176 if (base->is<TypeReference>()) {
1177 this->errorReporter().error(index.fOffset, "array must have a size");
1178 } else {
1179 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1180 }
Brian Osman4cf85072021-01-14 14:18:10 -05001181 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001182 }
1183 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1184 if (!converted) {
1185 return nullptr;
1186 }
John Stilesbb8cf582021-08-26 23:34:59 -04001187 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001188}
1189
Ethan Nicholasfc994162019-06-06 10:04:27 -04001190std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1191 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1192 auto iter = callNode.begin();
1193 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1194 if (!base) {
1195 return nullptr;
1196 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001197 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001198 for (; iter != callNode.end(); ++iter) {
1199 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1200 if (!converted) {
1201 return nullptr;
1202 }
1203 arguments.push_back(std::move(converted));
1204 }
1205 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1206}
1207
1208std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1209 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1210 if (!base) {
1211 return nullptr;
1212 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001213 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001214 const Type& baseType = base->type();
Ethan Nicholas5c75e832021-06-10 18:28:19 -04001215 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) {
John Stiles06d600f2021-03-08 09:18:21 -05001216 return FieldAccess::Convert(fContext, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001217 }
John Stiles06d600f2021-03-08 09:18:21 -05001218 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001219}
1220
1221std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001222 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001223 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1224 if (!base) {
1225 return nullptr;
1226 }
John Stiles23521a82021-03-02 17:02:51 -05001227 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001228}
1229
Brian Osman9496fe52020-11-18 14:48:19 -05001230void IRGenerator::findAndDeclareBuiltinVariables() {
1231 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001232 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001233 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001234
Brian Osman9496fe52020-11-18 14:48:19 -05001235 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001236 // If this is the *first* time we've seen this builtin, findAndInclude will return
1237 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001238 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001239 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001240 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001241 }
1242 }
1243
John Stiles4d6310a2021-01-26 19:58:22 -05001244 bool visitProgramElement(const ProgramElement& pe) override {
1245 if (pe.is<FunctionDefinition>()) {
1246 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1247 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1248 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001249 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001250 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1251 fPreserveFragColor = true;
1252 }
1253 }
1254 return INHERITED::visitProgramElement(pe);
1255 }
1256
Brian Osman9496fe52020-11-18 14:48:19 -05001257 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001258 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001259 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001260 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001261 return INHERITED::visitExpression(e);
1262 }
1263
1264 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001265 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001266 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001267
Brian Osman9496fe52020-11-18 14:48:19 -05001268 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001269 using INHERITED::visitProgramElement;
1270 };
1271
Brian Osman9496fe52020-11-18 14:48:19 -05001272 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001273 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001274 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001275 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001276 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001277
John Stiles4d6310a2021-01-26 19:58:22 -05001278 if (scanner.fPreserveFragColor) {
1279 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001280 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001281 }
1282
John Stilesd1204642021-02-17 16:30:02 -05001283 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001284 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001285 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1286 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1287 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001288 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001289 break;
1290 default:
1291 break;
1292 }
1293
Brian Osman9496fe52020-11-18 14:48:19 -05001294 fSharedElements->insert(
1295 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001296}
1297
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001298void IRGenerator::start(const ParsedModule& base,
1299 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001300 std::vector<std::unique_ptr<ProgramElement>>* elements,
1301 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001302 fProgramElements = elements;
1303 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001304 fSymbolTable = base.fSymbols;
1305 fIntrinsics = base.fIntrinsics.get();
1306 if (fIntrinsics) {
1307 fIntrinsics->resetAlreadyIncluded();
1308 }
1309 fIsBuiltinCode = isBuiltinCode;
1310
Brian Salomond8d85b92021-07-07 09:41:17 -04001311 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001312 fRTAdjust = nullptr;
1313 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001314 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001315 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001316
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001317 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001318 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001319 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001320 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001321 }
1322 }
John Stilesaecf8d52021-05-14 12:15:01 -04001323
John Stilesbb2ef922021-07-26 08:32:07 -04001324 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001325 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001326 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001327 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1328 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1329 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1330 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1331 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1332 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1333 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1334 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1335 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1336
1337 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1338 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1339 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1340 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1341 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1342 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1343
1344 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1345 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1346 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1347 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1348 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1349 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001350
1351 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1352 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1353 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1354 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001355
1356 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1357 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1358 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1359 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1360
1361 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1362 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1363 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1364 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001365 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001366}
Brian Osman88cda172020-10-09 12:05:16 -04001367
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001368IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001369 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001370 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001371 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001372 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001373
John Stilesf2872e62021-05-04 11:38:43 -04001374 return IRBundle{std::move(*fProgramElements),
1375 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001376 std::move(fSymbolTable),
1377 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001378}
1379
1380IRGenerator::IRBundle IRGenerator::convertProgram(
1381 const ParsedModule& base,
1382 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001383 skstd::string_view text) {
1384 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001385 fFile = parser.compilationUnit();
1386 if (this->errorReporter().errorCount() == 0) {
1387 SkASSERT(fFile);
1388 for (const auto& decl : fFile->root()) {
1389 switch (decl.fKind) {
1390 case ASTNode::Kind::kVarDeclarations:
1391 this->convertGlobalVarDeclarations(decl);
1392 break;
1393
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001394 case ASTNode::Kind::kFunction:
1395 this->convertFunction(decl);
1396 break;
1397
1398 case ASTNode::Kind::kModifiers: {
1399 std::unique_ptr<ModifiersDeclaration> f =
1400 this->convertModifiersDeclaration(decl);
1401 if (f) {
1402 fProgramElements->push_back(std::move(f));
1403 }
1404 break;
1405 }
1406 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001407 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001408 if (i) {
1409 fProgramElements->push_back(std::move(i));
1410 }
1411 break;
1412 }
1413 case ASTNode::Kind::kExtension: {
1414 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001415 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001416 if (e) {
1417 fProgramElements->push_back(std::move(e));
1418 }
1419 break;
1420 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001421 case ASTNode::Kind::kType: {
1422 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1423 if (s) {
1424 fProgramElements->push_back(std::move(s));
1425 }
1426 break;
1427 }
1428 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001429 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001430 break;
1431 }
1432 }
1433 }
1434 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001435}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001436
John Stilesa6841be2020-08-06 14:11:56 -04001437} // namespace SkSL