blob: c623bf8b468c7fe4bfef5f311e5d978326e3f391 [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"
Brian Osman3099f792021-09-01 13:12:16 -040048#include "src/sksl/ir/SkSLMethodReference.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060049#include "src/sksl/ir/SkSLNop.h"
John Stilesff6c5bf2021-09-07 15:22:02 -040050#include "src/sksl/ir/SkSLPoison.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/sksl/ir/SkSLPostfixExpression.h"
52#include "src/sksl/ir/SkSLPrefixExpression.h"
53#include "src/sksl/ir/SkSLReturnStatement.h"
54#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050055#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/sksl/ir/SkSLSwitchCase.h"
57#include "src/sksl/ir/SkSLSwitchStatement.h"
58#include "src/sksl/ir/SkSLSwizzle.h"
59#include "src/sksl/ir/SkSLTernaryExpression.h"
60#include "src/sksl/ir/SkSLUnresolvedFunction.h"
61#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062#include "src/sksl/ir/SkSLVariable.h"
63#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070064
65namespace SkSL {
66
67class AutoSymbolTable {
68public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050069 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070070 : fIR(ir)
71 , fPrevious(fIR->fSymbolTable) {
72 fIR->pushSymbolTable();
73 }
74
75 ~AutoSymbolTable() {
76 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040077 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070078 }
79
80 IRGenerator* fIR;
81 std::shared_ptr<SymbolTable> fPrevious;
82};
83
John Stilesc1a98b82021-02-24 13:35:02 -050084IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040085 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050086
87void IRGenerator::pushSymbolTable() {
88 auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
89 fSymbolTable = std::move(childSymTable);
90}
91
92void IRGenerator::popSymbolTable() {
93 fSymbolTable = fSymbolTable->fParent;
94}
95
Ethan Nicholas962dec42021-06-10 13:06:39 -040096std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -050097 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -040098 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -040099 this->errorReporter().error(offset, "extensions are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400100 return nullptr;
101 }
102
John Stilesfbd050b2020-08-03 13:21:46 -0400103 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700104}
105
John Stiles20c09f92021-03-12 09:25:04 -0500106std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700107 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400108 case ASTNode::Kind::kBlock:
109 return this->convertBlock(statement);
110 case ASTNode::Kind::kVarDeclarations:
111 return this->convertVarDeclarationStatement(statement);
112 case ASTNode::Kind::kIf:
113 return this->convertIf(statement);
114 case ASTNode::Kind::kFor:
115 return this->convertFor(statement);
116 case ASTNode::Kind::kWhile:
117 return this->convertWhile(statement);
118 case ASTNode::Kind::kDo:
119 return this->convertDo(statement);
120 case ASTNode::Kind::kSwitch:
121 return this->convertSwitch(statement);
122 case ASTNode::Kind::kReturn:
123 return this->convertReturn(statement);
124 case ASTNode::Kind::kBreak:
125 return this->convertBreak(statement);
126 case ASTNode::Kind::kContinue:
127 return this->convertContinue(statement);
128 case ASTNode::Kind::kDiscard:
129 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500130 case ASTNode::Kind::kType:
131 // TODO: add IRNode for struct definition inside a function
132 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400133 default:
134 // it's an expression
Brian Osman99ddd2a2021-08-27 11:21:12 -0400135 return this->convertExpressionStatement(statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136 }
137}
138
Ethan Nicholasfc994162019-06-06 10:04:27 -0400139std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
140 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400142 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400143 for (const auto& child : block) {
144 std::unique_ptr<Statement> statement = this->convertStatement(child);
John Stilesff6c5bf2021-09-07 15:22:02 -0400145 if (statement) {
146 statements.push_back(std::move(statement));
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500149 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150}
151
Ethan Nicholasfc994162019-06-06 10:04:27 -0400152std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
153 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400154 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400155 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700156 return nullptr;
157 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500158 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700159}
160
John Stiles80b02af2021-02-12 17:07:51 -0500161int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500162 if (!s) {
163 this->errorReporter().error(offset, "array must have a size");
164 return 0;
165 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500166 auto size = this->convertExpression(s);
167 if (!size) {
168 return 0;
169 }
John Stiles7253a202021-08-23 09:24:33 -0400170 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500171}
172
Ethan Nicholas489e5522021-01-20 10:53:11 -0500173void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
174 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500175 if (this->strictES2Mode() && baseType->isArray()) {
176 this->errorReporter().error(offset, "array size must appear after variable name");
177 }
178
John Stiles3624aba2020-12-22 16:29:53 -0500179 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500180 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500181 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500182 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400183 }
Brian Osman8c264792021-07-01 16:41:27 -0400184 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
185 this->errorReporter().error(offset, "'in' variables may not have matrix type");
186 }
187 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
188 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500189 }
John Stilesbb2ef922021-07-26 08:32:07 -0400190 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400191 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400192 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400193 }
194 }
Brian Osman5c625822021-04-06 14:28:07 -0400195 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
196 this->errorReporter().error(
197 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
198 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400199 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400200 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500201 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500202 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400203 }
204 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500205 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500206 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400207 }
208 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500209 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400210 (t.columns() == 3 || t.columns() == 4);
211 };
John Stilesc0c51062020-12-03 17:16:29 -0500212 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400213 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500214 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500215 "'srgb_unpremul' is only permitted on half3, half4, "
216 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400217 }
218 }
John Stilesb806da42021-08-04 16:26:47 -0400219 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
220 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400221 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500222 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400223 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400224 }
Brian Osmana654faa2021-02-26 11:52:59 -0500225 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400226 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500227}
228
Ethan Nicholasbd974002021-02-22 16:20:06 -0500229std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400230 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500231 bool isArray,
232 std::unique_ptr<Expression> arraySize,
233 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500234 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500235 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400236 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500237 this->errorReporter().error(offset,
238 "out location=0, index=0 is reserved for sk_FragColor");
239 }
240 const Type* type = baseType;
241 int arraySizeValue = 0;
242 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500243 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400244 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500245 if (!arraySizeValue) {
246 return {};
247 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500248 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
249 }
John Stilesf2872e62021-05-04 11:38:43 -0400250 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
251 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500252}
253
254std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400255 std::unique_ptr<Expression> value,
256 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400257 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
258 std::move(value));
259 if (!varDecl) {
260 return nullptr;
261 }
262
263 // Detect the declaration of magical variables.
264 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
265 // Silently ignore duplicate definitions of `sk_FragColor`.
266 const Symbol* symbol = (*fSymbolTable)[var->name()];
267 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500268 return nullptr;
269 }
John Stilese67bd132021-03-19 18:39:25 -0400270 } else if ((var->storage() == Variable::Storage::kGlobal ||
271 var->storage() == Variable::Storage::kInterfaceBlock) &&
272 var->name() == Compiler::RTADJUST_NAME) {
273 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
274 if (fRTAdjust) {
275 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500276 return nullptr;
277 }
John Stilese67bd132021-03-19 18:39:25 -0400278 if (var->type() != *fContext.fTypes.fFloat4) {
279 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500280 return nullptr;
281 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500282 fRTAdjust = var.get();
283 }
John Stilese67bd132021-03-19 18:39:25 -0400284
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400285 if (addToSymbolTable) {
286 fSymbolTable->add(std::move(var));
287 } else {
288 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
289 }
John Stilese67bd132021-03-19 18:39:25 -0400290 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500291}
292
293std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
294 const Modifiers& modifiers,
295 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400296 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500297 bool isArray,
298 std::unique_ptr<Expression> arraySize,
299 std::unique_ptr<Expression> value,
300 Variable::Storage storage) {
301 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
302 std::move(arraySize), storage);
303 if (!var) {
304 return nullptr;
305 }
306 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500307}
308
309StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
310 Variable::Storage storage) {
311 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
312 auto declarationsIter = decls.begin();
313 const Modifiers& modifiers = declarationsIter++->getModifiers();
314 const ASTNode& rawType = *(declarationsIter++);
315 const Type* baseType = this->convertType(rawType);
316 if (!baseType) {
317 return {};
318 }
John Stilesb806da42021-08-04 16:26:47 -0400319 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
320 decls.fOffset);
321 if (!baseType) {
322 return {};
323 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500324
325 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400326
327 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400328 for (; declarationsIter != decls.end(); ++declarationsIter) {
329 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400330 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500331 std::unique_ptr<Expression> arraySize;
332 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400333 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500334 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400335 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500336 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400337 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700338 }
John Stiles8f633ef2021-08-21 17:13:11 -0400339 arraySize = this->convertExpression(*iter++);
340 if (!arraySize) {
341 continue;
342 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700343 }
Brian Osman4cf85072021-01-14 14:18:10 -0500344 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400345 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700346 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400347 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700348 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700349 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500350 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
351 modifiers,
352 baseType,
353 varData.fName,
354 varData.fIsArray,
355 std::move(arraySize),
356 std::move(value),
357 storage);
358 if (varDeclStmt) {
359 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700360 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700361 }
Brian Osmanc0213602020-10-06 14:43:32 -0400362 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700363}
364
Ethan Nicholasfc994162019-06-06 10:04:27 -0400365std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500366 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400367 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400368 this->errorReporter().error(m.fOffset,
369 "layout qualifiers are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400370 return nullptr;
371 }
372
Ethan Nicholasfc994162019-06-06 10:04:27 -0400373 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
374 Modifiers modifiers = m.getModifiers();
John Stilesf2872e62021-05-04 11:38:43 -0400375 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700376}
377
John Stilesad2319f2020-09-02 15:01:47 -0400378std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
379 SkASSERT(n.fKind == ASTNode::Kind::kIf);
380 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500381 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400382 if (!test) {
383 return nullptr;
384 }
385 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
386 if (!ifTrue) {
387 return nullptr;
388 }
John Stilesad2319f2020-09-02 15:01:47 -0400389 std::unique_ptr<Statement> ifFalse;
390 if (iter != n.end()) {
391 ifFalse = this->convertStatement(*(iter++));
392 if (!ifFalse) {
393 return nullptr;
394 }
John Stilesad2319f2020-09-02 15:01:47 -0400395 }
John Stiles5ede6e32021-01-11 13:00:49 -0500396 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500397 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
398 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400399}
400
Ethan Nicholasfc994162019-06-06 10:04:27 -0400401std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
402 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholasb3058bd2016-07-01 08:22:01 -0700403 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700404 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400405 auto iter = f.begin();
406 if (*iter) {
407 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700408 if (!initializer) {
409 return nullptr;
410 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700411 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700413 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400414 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500415 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700416 if (!test) {
417 return nullptr;
418 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700419 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400420 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700421 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400422 if (*iter) {
423 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700424 if (!next) {
425 return nullptr;
426 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700427 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400428 ++iter;
429 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700430 if (!statement) {
431 return nullptr;
432 }
Brian Osman77ba8102021-01-12 17:15:30 -0500433
John Stiles23521a82021-03-02 17:02:51 -0500434 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
435 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500436}
437
438std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
439 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400440 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500441 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700442 if (!test) {
443 return nullptr;
444 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400445 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700446 if (!statement) {
447 return nullptr;
448 }
John Stiles23521a82021-03-02 17:02:51 -0500449 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
450 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700451}
452
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500453std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
454 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400455 auto iter = d.begin();
456 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
457 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700458 return nullptr;
459 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500460 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400461 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700462 return nullptr;
463 }
John Stiles23521a82021-03-02 17:02:51 -0500464 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700465}
466
Ethan Nicholascfefec02021-02-09 15:22:57 -0500467std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
468 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
469
470 auto iter = s.begin();
471 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
472 if (!value) {
473 return nullptr;
474 }
475 AutoSymbolTable table(this);
476 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500477 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500478 for (; iter != s.end(); ++iter) {
479 const ASTNode& c = *iter;
480 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
481 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
482 auto childIter = c.begin();
483 if (*childIter) {
484 caseValue = this->convertExpression(*childIter);
485 if (!caseValue) {
486 return nullptr;
487 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500488 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400489 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500490
John Stilesbf16b6c2021-03-12 19:24:31 -0500491 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400492 for (; childIter != c.end(); ++childIter) {
493 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500494 if (!converted) {
495 return nullptr;
496 }
497 statements.push_back(std::move(converted));
498 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500499
500 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500501 }
John Stiles23521a82021-03-02 17:02:51 -0500502 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
503 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500504}
505
Ethan Nicholasfc994162019-06-06 10:04:27 -0400506std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
507 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700508 if (!e) {
509 return nullptr;
510 }
John Stiles3e5871c2021-02-25 20:52:03 -0500511 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700512}
513
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500514std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
515 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500516 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500517}
518
519std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
520 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
521 if (r.begin() != r.end()) {
John Stilesff6c5bf2021-09-07 15:22:02 -0400522 if (std::unique_ptr<Expression> value = this->convertExpression(*r.begin())) {
523 return this->convertReturn(r.fOffset, std::move(value));
524 } else {
525 return this->convertReturn(r.fOffset, Poison::Make(r.fOffset, fContext));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500526 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700527 }
John Stilesff6c5bf2021-09-07 15:22:02 -0400528
529 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700530}
531
Ethan Nicholasfc994162019-06-06 10:04:27 -0400532std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
533 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500534 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700535}
536
Ethan Nicholasfc994162019-06-06 10:04:27 -0400537std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
538 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500539 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700540}
541
Ethan Nicholasfc994162019-06-06 10:04:27 -0400542std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
543 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400544 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500545 this->errorReporter().error(d.fOffset,
546 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500547 return nullptr;
548 }
John Stilesa0c04d62021-03-11 23:07:24 -0500549 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700550}
551
John Stiles3b204892021-08-27 17:35:35 -0400552void IRGenerator::appendRTAdjustFixupToVertexMain(const FunctionDeclaration& decl, Block* body) {
John Stilesd45ebbc2021-05-18 16:54:36 -0400553 using namespace SkSL::dsl;
554 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
John Stiles3b204892021-08-27 17:35:35 -0400555 using OwnerKind = SkSL::FieldAccess::OwnerKind;
John Stilesd45ebbc2021-05-18 16:54:36 -0400556
John Stiles3b204892021-08-27 17:35:35 -0400557 // If this is a vertex program that uses RTAdjust, and this is main()...
Ethan Nicholasa97ad0f2021-09-02 09:54:04 -0400558 if ((fRTAdjust || fRTAdjustInterfaceBlock) && decl.isMain() &&
559 ProgramKind::kVertex == this->programKind()) {
John Stiles3b204892021-08-27 17:35:35 -0400560 // ... append a line to the end of the function body which fixes up sk_Position.
561 const Variable* skPerVertex = nullptr;
562 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
563 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
564 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
565 }
566
567 SkASSERT(skPerVertex);
568 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
569 return VariableReference::Make(/*offset=*/-1, var);
570 };
571 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
572 return FieldAccess::Make(fContext, Ref(var), idx, OwnerKind::kAnonymousInterfaceBlock);
573 };
574 auto Pos = [&]() -> DSLExpression {
575 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
576 OwnerKind::kAnonymousInterfaceBlock));
577 };
578 auto Adjust = [&]() -> DSLExpression {
579 return DSLExpression(fRTAdjustInterfaceBlock
580 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
581 : Ref(fRTAdjust));
582 };
583
584 auto fixupStmt = DSLStatement(
585 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
586 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
587 0,
588 Pos().w())
589 );
590
591 body->children().push_back(fixupStmt.release());
Brian Osman88cda172020-10-09 12:05:16 -0400592 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000593}
594
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400595void IRGenerator::CheckModifiers(const Context& context,
596 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500597 const Modifiers& modifiers,
598 int permittedModifierFlags,
599 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400600 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
601 { Modifiers::kConst_Flag, "const" },
602 { Modifiers::kIn_Flag, "in" },
603 { Modifiers::kOut_Flag, "out" },
604 { Modifiers::kUniform_Flag, "uniform" },
605 { Modifiers::kFlat_Flag, "flat" },
606 { Modifiers::kNoPerspective_Flag, "noperspective" },
607 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
608 { Modifiers::kInline_Flag, "inline" },
609 { Modifiers::kNoInline_Flag, "noinline" },
610 { Modifiers::kHighp_Flag, "highp" },
611 { Modifiers::kMediump_Flag, "mediump" },
612 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400613 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500614 };
615
Brian Osmanb5de6be2021-08-10 15:16:34 -0400616 int modifierFlags = modifiers.fFlags;
617 for (const auto& f : kModifierFlags) {
618 if (modifierFlags & f.flag) {
619 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400620 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400621 }
622 modifierFlags &= ~f.flag;
623 }
624 }
625 SkASSERT(modifierFlags == 0);
626
627 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
628 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
629 { Layout::kPushConstant_Flag, "push_constant"},
630 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
631 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
632 { Layout::kLocation_Flag, "location"},
633 { Layout::kOffset_Flag, "offset"},
634 { Layout::kBinding_Flag, "binding"},
635 { Layout::kIndex_Flag, "index"},
636 { Layout::kSet_Flag, "set"},
637 { Layout::kBuiltin_Flag, "builtin"},
638 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
Brian Osmanb5de6be2021-08-10 15:16:34 -0400639 };
Brian Osmana654faa2021-02-26 11:52:59 -0500640
641 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400642 for (const auto& lf : kLayoutFlags) {
643 if (layoutFlags & lf.flag) {
644 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400645 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400646 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500647 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400648 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500649 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400650 }
Brian Osmana654faa2021-02-26 11:52:59 -0500651 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400652}
653
Ethan Nicholasfc994162019-06-06 10:04:27 -0400654void IRGenerator::convertFunction(const ASTNode& f) {
655 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400656 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400657 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400658 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700659 }
John Stilesb9af7232020-08-20 15:57:48 -0400660 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400661 std::vector<std::unique_ptr<Variable>> parameters;
662 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400663 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400664 const ASTNode& param = *(iter++);
665 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500666 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400667 auto paramIter = param.begin();
668 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700669 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400670 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700671 }
John Stilesd39aec92020-12-03 14:37:16 -0500672 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500673 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500674 if (!arraySize) {
675 return;
676 }
John Stilesa2179502020-12-03 14:37:57 -0500677 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700678 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400679
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400680 parameters.push_back(std::make_unique<Variable>(param.fOffset,
681 this->modifiersPool().add(pd.fModifiers),
682 pd.fName,
683 type,
684 fIsBuiltinCode,
685 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700686 }
687
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400688 // Conservatively assume all user-defined functions have side effects.
689 Modifiers declModifiers = funcData.fModifiers;
690 if (!fIsBuiltinCode) {
691 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400692 }
693
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400694 if (fContext.fConfig->fSettings.fForceNoInline) {
695 // Apply the `noinline` modifier to every function. This allows us to test Runtime
696 // Effects without any inlining, even when the code is later added to a paint.
697 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
698 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700699 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400700
701 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
702 fContext,
703 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400704 f.fOffset,
705 this->modifiersPool().add(declModifiers),
706 funcData.fName,
707 std::move(parameters),
708 returnType,
709 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400711 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700712 }
John Stiles569249b2020-11-03 12:18:22 -0500713 if (iter == f.end()) {
714 // If there's no body, we've found a prototype.
715 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
716 fIsBuiltinCode));
717 } else {
718 // Compile function body.
ethannicholasd598f792016-07-25 10:08:54 -0700719 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -0500720 for (const Variable* param : decl->parameters()) {
721 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700722 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400723 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700724 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400725 return;
726 }
John Stiles3b204892021-08-27 17:35:35 -0400727 this->appendRTAdjustFixupToVertexMain(*decl, body.get());
728 std::unique_ptr<FunctionDefinition> result = FunctionDefinition::Convert(
729 fContext, f.fOffset, *decl, std::move(body), fIsBuiltinCode);
Ethan Nicholased84b732020-10-08 11:45:44 -0400730 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400731 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500732 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700733 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700734}
735
John Stilesdc75a972020-11-25 16:24:55 -0500736std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
737 SkASSERT(node.fKind == ASTNode::Kind::kType);
738
739 const Type* type = this->convertType(node);
740 if (!type) {
741 return nullptr;
742 }
John Stilesc0c51062020-12-03 17:16:29 -0500743 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500744 this->errorReporter().error(node.fOffset,
745 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500746 return nullptr;
747 }
Brian Osman02bc5222021-01-28 11:00:20 -0500748 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
749 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500750 return std::make_unique<StructDefinition>(node.fOffset, *type);
751}
752
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400753void IRGenerator::scanInterfaceBlock(SkSL::InterfaceBlock& intf) {
754 const std::vector<Type::Field>& fields = intf.variable().type().componentType().fields();
755 for (size_t i = 0; i < fields.size(); ++i) {
756 const Type::Field& f = fields[i];
757 if (f.fName == Compiler::RTADJUST_NAME) {
758 if (*f.fType == *fContext.fTypes.fFloat4) {
759 fRTAdjustInterfaceBlock = &intf.variable();
760 fRTAdjustFieldIndex = i;
761 } else {
762 this->errorReporter().error(intf.fOffset, "sk_RTAdjust must have type 'float4'");
763 }
764 }
765 }
766}
767
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400768std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500769 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400770 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400771 this->errorReporter().error(intf.fOffset,
772 "interface blocks are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400773 return nullptr;
774 }
775
Ethan Nicholasfc994162019-06-06 10:04:27 -0400776 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500777 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700778 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400779 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700780 std::vector<Type::Field> fields;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400781 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400782 {
783 AutoSymbolTable table(this);
784 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400785 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
786 StatementArray decls = this->convertVarDeclarations(*(iter++),
787 Variable::Storage::kInterfaceBlock);
788 if (decls.empty()) {
789 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400790 }
John Stiles869cdef2020-10-30 14:24:24 -0400791 for (const auto& decl : decls) {
792 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -0400793 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
794 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400795 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500796 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700797 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400798 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400799 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -0500800 fields));
John Stilesd39aec02020-12-03 10:42:26 -0500801 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -0500802 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400803 const ASTNode& size = *(iter++);
Brian Osmanb42c3832021-08-27 15:19:03 -0400804 arraySize = this->convertArraySize(*type, size.fOffset, size);
805 if (!arraySize) {
806 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500807 }
John Stilesa2179502020-12-03 14:37:57 -0500808 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500809 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400810 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400811 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -0400812 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400813 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400814 type,
Brian Osman3887a012020-09-30 13:22:27 -0400815 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400816 Variable::Storage::kGlobal));
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400817 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -0400818 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700819 } else {
820 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -0400821 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -0700822 }
823 }
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400824 std::unique_ptr<SkSL::InterfaceBlock> result = std::make_unique<SkSL::InterfaceBlock>(
825 intf.fOffset, var, id.fTypeName, id.fInstanceName, arraySize, symbols);
826 this->scanInterfaceBlock(*result);
827 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700828}
829
John Stiles7bd70332020-11-30 17:04:09 -0500830void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
831 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
832 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -0500833 const Type* type = &stmt->as<VarDeclaration>().baseType();
834 if (type->isStruct()) {
835 auto [iter, wasInserted] = fDefinedStructs.insert(type);
836 if (wasInserted) {
837 fProgramElements->push_back(
838 std::make_unique<StructDefinition>(decl.fOffset, *type));
839 }
840 }
Ethan Nicholas624a5292021-04-16 14:54:43 -0400841 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -0500842 }
843}
844
Brian Osmand8070392020-09-09 15:50:02 -0400845const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400846 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -0500847 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -0500848 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -0500849 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -0500850 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700851 }
John Stiles6bef6a72020-12-02 14:26:04 -0500852 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -0500853 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -0400854 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -0500855 this->errorReporter().error(type.fOffset,
856 "type '" + name + "' not allowed in this context");
857 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -0500858 }
John Stiles823c5042021-08-17 12:09:00 -0400859 if (!fIsBuiltinCode) {
Ethan Nicholas517f4ff2021-09-02 10:57:45 -0400860 if (result->containsPrivateFields()) {
John Stiles823c5042021-08-17 12:09:00 -0400861 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
862 return nullptr;
863 }
864 if (this->strictES2Mode() && !result->allowedInES2()) {
865 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
866 return nullptr;
867 }
John Stiles7bd70332020-11-30 17:04:09 -0500868 }
John Stiles6bef6a72020-12-02 14:26:04 -0500869 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -0500870 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -0500871 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -0500872 if (!arraySize) {
873 return nullptr;
874 }
John Stilesa2179502020-12-03 14:37:57 -0500875 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -0500876 }
John Stiles6bef6a72020-12-02 14:26:04 -0500877 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700878}
879
Ethan Nicholasfc994162019-06-06 10:04:27 -0400880std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700881 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400882 case ASTNode::Kind::kBinary:
883 return this->convertBinaryExpression(expr);
884 case ASTNode::Kind::kBool:
John Stiles9ce80f72021-03-11 22:35:19 -0500885 return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400886 case ASTNode::Kind::kCall:
887 return this->convertCallExpression(expr);
888 case ASTNode::Kind::kField:
889 return this->convertFieldExpression(expr);
890 case ASTNode::Kind::kFloat:
John Stiles9ce80f72021-03-11 22:35:19 -0500891 return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400892 case ASTNode::Kind::kIdentifier:
893 return this->convertIdentifier(expr);
894 case ASTNode::Kind::kIndex:
895 return this->convertIndexExpression(expr);
896 case ASTNode::Kind::kInt:
John Stiles9ce80f72021-03-11 22:35:19 -0500897 return IntLiteral::Make(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400898 case ASTNode::Kind::kPostfix:
899 return this->convertPostfixExpression(expr);
900 case ASTNode::Kind::kPrefix:
901 return this->convertPrefixExpression(expr);
902 case ASTNode::Kind::kTernary:
903 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700904 default:
John Stilesfbb905e2021-08-25 15:51:50 -0400905 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500906 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700907 }
908}
909
Ethan Nicholas962dec42021-06-10 13:06:39 -0400910std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500911 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -0700912 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -0500913 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700914 return nullptr;
915 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400916 switch (result->kind()) {
917 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -0700918 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -0400919 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -0700920 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500921 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700922 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400923 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400924 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500925 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -0700926 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400927 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400928 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400929 const Modifiers& modifiers = var->modifiers();
930 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400931 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -0400932 if (caps().canUseFragCoord()) {
933 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400934 }
Brian Salomond8d85b92021-07-07 09:41:17 -0400935 break;
936 case SK_CLOCKWISE_BUILTIN:
937 fInputs.fUseFlipRTUniform = true;
938 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400939 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500940 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -0400941 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -0700942 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400943 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400944 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -0400945 auto base = VariableReference::Make(offset, &field->owner(),
946 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -0500947 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
948 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700949 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400950 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400951 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500952 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -0700953 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400954 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500955 const ExternalFunction* r = &result->as<ExternalFunction>();
956 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400957 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700958 default:
John Stilesf57207b2021-02-02 17:50:34 -0500959 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -0700960 }
Ethan Nicholasc0709392017-06-27 11:20:22 -0400961}
962
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500963std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400964 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500965}
966
Ethan Nicholas11d53972016-11-28 11:23:23 -0500967std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700968 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -0500969 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -0700970}
971
Ethan Nicholasfc994162019-06-06 10:04:27 -0400972std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
973 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
974 auto iter = expression.begin();
975 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700976 if (!left) {
977 return nullptr;
978 }
John Stiles0f464502020-11-20 12:52:22 -0500979 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700980 if (!right) {
981 return nullptr;
982 }
John Stiles23521a82021-03-02 17:02:51 -0500983 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
984 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -0700985}
986
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500987std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
988 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
989 auto iter = node.begin();
990 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
991 if (!test) {
992 return nullptr;
993 }
994 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
995 if (!ifTrue) {
996 return nullptr;
997 }
998 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
999 if (!ifFalse) {
1000 return nullptr;
1001 }
John Stiles23521a82021-03-02 17:02:51 -05001002 return TernaryExpression::Convert(fContext, std::move(test),
1003 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001004}
1005
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001006void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001007 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04001008 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04001009
1010 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
1011 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001012 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
1013 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04001014 std::sort(intrinsics.begin(), intrinsics.end(),
1015 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001016 if (a->isBuiltin() != b->isBuiltin()) {
1017 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04001018 }
1019 if (a->fOffset != b->fOffset) {
1020 return a->fOffset < b->fOffset;
1021 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04001022 if (a->name() != b->name()) {
1023 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04001024 }
1025 return a->description() < b->description();
1026 });
1027 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001028 this->copyIntrinsicIfNeeded(*f);
1029 }
John Stiles607d36b2020-10-19 15:00:01 -04001030
Brian Osman0006ad02020-11-18 15:38:39 -05001031 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001032 }
1033}
1034
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001035std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001036 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001037 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001038 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001039 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001040 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001041 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001042 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001043 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001044 }
1045 }
John Stilesea9ab822020-08-31 09:55:04 -04001046
John Stilescd7ba502021-03-19 10:54:59 -04001047 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001048}
1049
1050/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001051 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001052 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1053 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001054 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001055CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
Brian Osman3099f792021-09-01 13:12:16 -04001056 const ExpressionArray& arguments) const {
John Stilesefde90d2021-08-12 23:06:24 -04001057 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1058 return CoercionCost::Impossible();
1059 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001060 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001061 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001062 }
John Stilesfa889112020-10-12 19:03:43 -04001063 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001064 const Type* ignored;
1065 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001066 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001067 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001068 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001069 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001070 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001071 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001072 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001073}
1074
Brian Osman3099f792021-09-01 13:12:16 -04001075const FunctionDeclaration* IRGenerator::findBestFunctionForCall(
1076 const std::vector<const FunctionDeclaration*>& functions,
1077 const ExpressionArray& arguments) const {
1078 if (functions.size() == 1) {
1079 return functions.front();
1080 }
1081 CoercionCost bestCost = CoercionCost::Impossible();
1082 const FunctionDeclaration* best = nullptr;
1083 for (const auto& f : functions) {
1084 CoercionCost cost = this->callCost(*f, arguments);
1085 if (cost < bestCost) {
1086 bestCost = cost;
1087 best = f;
1088 }
1089 }
1090 return best;
1091}
1092
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001093std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001094 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001095 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001096 switch (functionValue->kind()) {
1097 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001098 return Constructor::Convert(fContext,
1099 offset,
1100 functionValue->as<TypeReference>().value(),
1101 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001102 case Expression::Kind::kExternalFunctionReference: {
1103 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1104 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001105 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001106 this->errorReporter().error(offset, "external function expected " +
1107 to_string(count) + " arguments, but found " +
1108 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001109 return nullptr;
1110 }
1111 static constexpr int PARAMETER_MAX = 16;
1112 SkASSERT(count < PARAMETER_MAX);
1113 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001114 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001115 for (int i = 0; i < count; ++i) {
1116 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1117 if (!arguments[i]) {
1118 return nullptr;
1119 }
1120 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001121 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001122 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001123 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001124 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001125 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman3099f792021-09-01 13:12:16 -04001126 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1127 if (best) {
1128 return this->call(offset, *best, std::move(arguments));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001129 }
Brian Osman3099f792021-09-01 13:12:16 -04001130 String msg = "no match for " + functions[0]->name() + "(";
1131 String separator;
1132 for (size_t i = 0; i < arguments.size(); i++) {
1133 msg += separator;
1134 separator = ", ";
1135 msg += arguments[i]->type().displayName();
1136 }
1137 msg += ")";
1138 this->errorReporter().error(offset, msg);
1139 return nullptr;
1140 }
1141 case Expression::Kind::kMethodReference: {
1142 MethodReference& ref = functionValue->as<MethodReference>();
1143 arguments.push_back(std::move(ref.self()));
1144
1145 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
1146 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1147 if (best) {
1148 return this->call(offset, *best, std::move(arguments));
1149 }
1150 String msg = "no match for " + arguments.back()->type().displayName() +
1151 "::" + functions[0]->name().substr(1) + "(";
1152 String separator;
1153 for (size_t i = 0; i < arguments.size() - 1; i++) {
1154 msg += separator;
1155 separator = ", ";
1156 msg += arguments[i]->type().displayName();
1157 }
1158 msg += ")";
1159 this->errorReporter().error(offset, msg);
1160 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001161 }
Ethan Nicholasace470b2021-08-30 16:15:20 -04001162 case Expression::Kind::kPoison:
1163 return functionValue;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001164 default:
John Stilesb30151e2021-01-11 16:13:08 -05001165 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001166 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001167 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001168}
1169
Ethan Nicholasfc994162019-06-06 10:04:27 -04001170std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1171 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1172 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001173 if (!base) {
1174 return nullptr;
1175 }
John Stiles23521a82021-03-02 17:02:51 -05001176 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001177}
1178
ethannicholasb3058bd2016-07-01 08:22:01 -07001179std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001180 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001181 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001182}
1183
Ethan Nicholasfc994162019-06-06 10:04:27 -04001184std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1185 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1186 auto iter = index.begin();
1187 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001188 if (!base) {
1189 return nullptr;
1190 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001191 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001192 if (base->is<TypeReference>()) {
1193 this->errorReporter().error(index.fOffset, "array must have a size");
1194 } else {
1195 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1196 }
Brian Osman4cf85072021-01-14 14:18:10 -05001197 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001198 }
1199 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1200 if (!converted) {
1201 return nullptr;
1202 }
John Stilesbb8cf582021-08-26 23:34:59 -04001203 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001204}
1205
Ethan Nicholasfc994162019-06-06 10:04:27 -04001206std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1207 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1208 auto iter = callNode.begin();
1209 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1210 if (!base) {
1211 return nullptr;
1212 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001213 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001214 for (; iter != callNode.end(); ++iter) {
1215 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1216 if (!converted) {
1217 return nullptr;
1218 }
1219 arguments.push_back(std::move(converted));
1220 }
1221 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1222}
1223
1224std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1225 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1226 if (!base) {
1227 return nullptr;
1228 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001229 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001230 const Type& baseType = base->type();
Brian Osman3099f792021-09-01 13:12:16 -04001231 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct() || baseType.isEffectChild()) {
1232 return FieldAccess::Convert(fContext, *fSymbolTable, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001233 }
John Stiles06d600f2021-03-08 09:18:21 -05001234 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001235}
1236
1237std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001238 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001239 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1240 if (!base) {
1241 return nullptr;
1242 }
John Stiles23521a82021-03-02 17:02:51 -05001243 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001244}
1245
Brian Osman9496fe52020-11-18 14:48:19 -05001246void IRGenerator::findAndDeclareBuiltinVariables() {
1247 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001248 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001249 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001250
Brian Osman9496fe52020-11-18 14:48:19 -05001251 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001252 // If this is the *first* time we've seen this builtin, findAndInclude will return
1253 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001254 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001255 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001256 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001257 }
1258 }
1259
John Stiles4d6310a2021-01-26 19:58:22 -05001260 bool visitProgramElement(const ProgramElement& pe) override {
1261 if (pe.is<FunctionDefinition>()) {
1262 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1263 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1264 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001265 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001266 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1267 fPreserveFragColor = true;
1268 }
1269 }
1270 return INHERITED::visitProgramElement(pe);
1271 }
1272
Brian Osman9496fe52020-11-18 14:48:19 -05001273 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001274 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001275 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001276 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001277 return INHERITED::visitExpression(e);
1278 }
1279
1280 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001281 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001282 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001283
Brian Osman9496fe52020-11-18 14:48:19 -05001284 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001285 using INHERITED::visitProgramElement;
1286 };
1287
Brian Osman9496fe52020-11-18 14:48:19 -05001288 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001289 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001290 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001291 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001292 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001293
John Stiles4d6310a2021-01-26 19:58:22 -05001294 if (scanner.fPreserveFragColor) {
1295 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001296 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001297 }
1298
John Stilesd1204642021-02-17 16:30:02 -05001299 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001300 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001301 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1302 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1303 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001304 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001305 break;
1306 default:
1307 break;
1308 }
1309
Brian Osman9496fe52020-11-18 14:48:19 -05001310 fSharedElements->insert(
1311 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001312}
1313
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001314void IRGenerator::start(const ParsedModule& base,
1315 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001316 std::vector<std::unique_ptr<ProgramElement>>* elements,
1317 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001318 fProgramElements = elements;
1319 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001320 fSymbolTable = base.fSymbols;
1321 fIntrinsics = base.fIntrinsics.get();
1322 if (fIntrinsics) {
1323 fIntrinsics->resetAlreadyIncluded();
1324 }
1325 fIsBuiltinCode = isBuiltinCode;
1326
Brian Salomond8d85b92021-07-07 09:41:17 -04001327 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001328 fRTAdjust = nullptr;
1329 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001330 fDefinedStructs.clear();
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001331 this->pushSymbolTable();
Brian Osman88cda172020-10-09 12:05:16 -04001332
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001333 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001334 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001335 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001336 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001337 }
1338 }
John Stilesaecf8d52021-05-14 12:15:01 -04001339
John Stilesbb2ef922021-07-26 08:32:07 -04001340 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001341 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001342 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001343 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1344 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1345 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1346 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1347 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1348 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1349 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1350 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1351 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1352
1353 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1354 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1355 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1356 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1357 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1358 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1359
1360 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1361 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1362 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1363 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1364 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1365 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001366
1367 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1368 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1369 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1370 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001371
1372 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1373 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1374 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1375 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1376
1377 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1378 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1379 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1380 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001381 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001382}
Brian Osman88cda172020-10-09 12:05:16 -04001383
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001384IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001385 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001386 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001387 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001388 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001389
John Stilesf2872e62021-05-04 11:38:43 -04001390 return IRBundle{std::move(*fProgramElements),
1391 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001392 std::move(fSymbolTable),
1393 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001394}
1395
1396IRGenerator::IRBundle IRGenerator::convertProgram(
1397 const ParsedModule& base,
1398 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001399 skstd::string_view text) {
1400 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001401 fFile = parser.compilationUnit();
1402 if (this->errorReporter().errorCount() == 0) {
1403 SkASSERT(fFile);
1404 for (const auto& decl : fFile->root()) {
1405 switch (decl.fKind) {
1406 case ASTNode::Kind::kVarDeclarations:
1407 this->convertGlobalVarDeclarations(decl);
1408 break;
1409
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001410 case ASTNode::Kind::kFunction:
1411 this->convertFunction(decl);
1412 break;
1413
1414 case ASTNode::Kind::kModifiers: {
1415 std::unique_ptr<ModifiersDeclaration> f =
1416 this->convertModifiersDeclaration(decl);
1417 if (f) {
1418 fProgramElements->push_back(std::move(f));
1419 }
1420 break;
1421 }
1422 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001423 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001424 if (i) {
1425 fProgramElements->push_back(std::move(i));
1426 }
1427 break;
1428 }
1429 case ASTNode::Kind::kExtension: {
1430 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001431 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001432 if (e) {
1433 fProgramElements->push_back(std::move(e));
1434 }
1435 break;
1436 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001437 case ASTNode::Kind::kType: {
1438 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1439 if (s) {
1440 fProgramElements->push_back(std::move(s));
1441 }
1442 break;
1443 }
1444 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001445 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001446 break;
1447 }
1448 }
1449 }
1450 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001451}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001452
John Stilesa6841be2020-08-06 14:11:56 -04001453} // namespace SkSL