blob: 41383e1405ae4c35c4fbd486b1b2c7bd3c402304 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/sksl/ir/SkSLBreakStatement.h"
27#include "src/sksl/ir/SkSLConstructor.h"
28#include "src/sksl/ir/SkSLContinueStatement.h"
29#include "src/sksl/ir/SkSLDiscardStatement.h"
30#include "src/sksl/ir/SkSLDoStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040032#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050033#include "src/sksl/ir/SkSLExternalFunctionReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLField.h"
35#include "src/sksl/ir/SkSLFieldAccess.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/sksl/ir/SkSLForStatement.h"
37#include "src/sksl/ir/SkSLFunctionCall.h"
38#include "src/sksl/ir/SkSLFunctionDeclaration.h"
39#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050040#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLFunctionReference.h"
42#include "src/sksl/ir/SkSLIfStatement.h"
43#include "src/sksl/ir/SkSLIndexExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/ir/SkSLInterfaceBlock.h"
John Stiles7591d4b2021-09-13 13:32:06 -040045#include "src/sksl/ir/SkSLLiteral.h"
Brian Osman3099f792021-09-01 13:12:16 -040046#include "src/sksl/ir/SkSLMethodReference.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060047#include "src/sksl/ir/SkSLNop.h"
John Stilesff6c5bf2021-09-07 15:22:02 -040048#include "src/sksl/ir/SkSLPoison.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
John Stilesc1a98b82021-02-24 13:35:02 -050065IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040066 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050067
Ethan Nicholas962dec42021-06-10 13:06:39 -040068std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -050069 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -040070 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -040071 this->errorReporter().error(offset, "extensions are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -040072 return nullptr;
73 }
74
John Stilesfbd050b2020-08-03 13:21:46 -040075 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -070076}
77
John Stiles20c09f92021-03-12 09:25:04 -050078std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -070079 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -040080 case ASTNode::Kind::kBlock:
81 return this->convertBlock(statement);
82 case ASTNode::Kind::kVarDeclarations:
83 return this->convertVarDeclarationStatement(statement);
84 case ASTNode::Kind::kIf:
85 return this->convertIf(statement);
86 case ASTNode::Kind::kFor:
87 return this->convertFor(statement);
88 case ASTNode::Kind::kWhile:
89 return this->convertWhile(statement);
90 case ASTNode::Kind::kDo:
91 return this->convertDo(statement);
92 case ASTNode::Kind::kSwitch:
93 return this->convertSwitch(statement);
94 case ASTNode::Kind::kReturn:
95 return this->convertReturn(statement);
96 case ASTNode::Kind::kBreak:
97 return this->convertBreak(statement);
98 case ASTNode::Kind::kContinue:
99 return this->convertContinue(statement);
100 case ASTNode::Kind::kDiscard:
101 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500102 case ASTNode::Kind::kType:
103 // TODO: add IRNode for struct definition inside a function
104 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400105 default:
106 // it's an expression
Brian Osman99ddd2a2021-08-27 11:21:12 -0400107 return this->convertExpressionStatement(statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 }
109}
110
Ethan Nicholasfc994162019-06-06 10:04:27 -0400111std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
112 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
John Stilesde2b6092021-09-17 10:40:47 -0400113 AutoSymbolTable table(&fSymbolTable);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400114 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400115 for (const auto& child : block) {
116 std::unique_ptr<Statement> statement = this->convertStatement(child);
John Stilesff6c5bf2021-09-07 15:22:02 -0400117 if (statement) {
118 statements.push_back(std::move(statement));
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500121 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700122}
123
Ethan Nicholasfc994162019-06-06 10:04:27 -0400124std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
125 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400126 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400127 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 return nullptr;
129 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500130 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700131}
132
John Stiles80b02af2021-02-12 17:07:51 -0500133int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500134 if (!s) {
135 this->errorReporter().error(offset, "array must have a size");
136 return 0;
137 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500138 auto size = this->convertExpression(s);
139 if (!size) {
140 return 0;
141 }
John Stiles7253a202021-08-23 09:24:33 -0400142 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500143}
144
Ethan Nicholas489e5522021-01-20 10:53:11 -0500145void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
146 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500147 if (this->strictES2Mode() && baseType->isArray()) {
148 this->errorReporter().error(offset, "array size must appear after variable name");
149 }
150
John Stiles3624aba2020-12-22 16:29:53 -0500151 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500152 this->errorReporter().error(
Ethan Nicholas489e5522021-01-20 10:53:11 -0500153 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500154 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400155 }
Brian Osman8c264792021-07-01 16:41:27 -0400156 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
157 this->errorReporter().error(offset, "'in' variables may not have matrix type");
158 }
159 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
160 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500161 }
John Stilesbb2ef922021-07-26 08:32:07 -0400162 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400163 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman39675882021-03-31 14:37:10 -0400164 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400165 }
166 }
Brian Osman5c625822021-04-06 14:28:07 -0400167 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
168 this->errorReporter().error(
169 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
170 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400171 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400172 if (!this->isRuntimeEffect()) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500173 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500174 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400175 }
176 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500177 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500178 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400179 }
180 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500181 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400182 (t.columns() == 3 || t.columns() == 4);
183 };
John Stilesc0c51062020-12-03 17:16:29 -0500184 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400185 validColorXformType(baseType->componentType()))) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500186 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500187 "'srgb_unpremul' is only permitted on half3, half4, "
188 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400189 }
190 }
John Stilesb806da42021-08-04 16:26:47 -0400191 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
192 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400193 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500194 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400195 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400196 }
Brian Osmana654faa2021-02-26 11:52:59 -0500197 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400198 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500199}
200
Ethan Nicholasbd974002021-02-22 16:20:06 -0500201std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400202 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500203 bool isArray,
204 std::unique_ptr<Expression> arraySize,
205 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500206 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500207 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400208 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500209 this->errorReporter().error(offset,
210 "out location=0, index=0 is reserved for sk_FragColor");
211 }
212 const Type* type = baseType;
213 int arraySizeValue = 0;
214 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500215 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400216 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500217 if (!arraySizeValue) {
218 return {};
219 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500220 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
221 }
John Stilesf2872e62021-05-04 11:38:43 -0400222 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
223 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500224}
225
226std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400227 std::unique_ptr<Expression> value,
228 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400229 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
230 std::move(value));
231 if (!varDecl) {
232 return nullptr;
233 }
234
235 // Detect the declaration of magical variables.
236 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
237 // Silently ignore duplicate definitions of `sk_FragColor`.
238 const Symbol* symbol = (*fSymbolTable)[var->name()];
239 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500240 return nullptr;
241 }
John Stilese67bd132021-03-19 18:39:25 -0400242 } else if ((var->storage() == Variable::Storage::kGlobal ||
243 var->storage() == Variable::Storage::kInterfaceBlock) &&
244 var->name() == Compiler::RTADJUST_NAME) {
245 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
246 if (fRTAdjust) {
247 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500248 return nullptr;
249 }
John Stilese67bd132021-03-19 18:39:25 -0400250 if (var->type() != *fContext.fTypes.fFloat4) {
251 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500252 return nullptr;
253 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500254 fRTAdjust = var.get();
255 }
John Stilese67bd132021-03-19 18:39:25 -0400256
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400257 if (addToSymbolTable) {
258 fSymbolTable->add(std::move(var));
259 } else {
260 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
261 }
John Stilese67bd132021-03-19 18:39:25 -0400262 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500263}
264
265std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
266 const Modifiers& modifiers,
267 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400268 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500269 bool isArray,
270 std::unique_ptr<Expression> arraySize,
271 std::unique_ptr<Expression> value,
272 Variable::Storage storage) {
273 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
274 std::move(arraySize), storage);
275 if (!var) {
276 return nullptr;
277 }
278 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500279}
280
281StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
282 Variable::Storage storage) {
283 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
284 auto declarationsIter = decls.begin();
285 const Modifiers& modifiers = declarationsIter++->getModifiers();
286 const ASTNode& rawType = *(declarationsIter++);
287 const Type* baseType = this->convertType(rawType);
288 if (!baseType) {
289 return {};
290 }
John Stilesb806da42021-08-04 16:26:47 -0400291 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
292 decls.fOffset);
293 if (!baseType) {
294 return {};
295 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500296
297 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400298
299 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400300 for (; declarationsIter != decls.end(); ++declarationsIter) {
301 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400302 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500303 std::unique_ptr<Expression> arraySize;
304 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400305 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500306 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400307 if (!*iter) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500308 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400309 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700310 }
John Stiles8f633ef2021-08-21 17:13:11 -0400311 arraySize = this->convertExpression(*iter++);
312 if (!arraySize) {
313 continue;
314 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700315 }
Brian Osman4cf85072021-01-14 14:18:10 -0500316 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400317 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400319 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700320 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700321 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500322 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
323 modifiers,
324 baseType,
325 varData.fName,
326 varData.fIsArray,
327 std::move(arraySize),
328 std::move(value),
329 storage);
330 if (varDeclStmt) {
331 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700332 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700333 }
Brian Osmanc0213602020-10-06 14:43:32 -0400334 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700335}
336
Ethan Nicholasfc994162019-06-06 10:04:27 -0400337std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500338 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400339 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400340 this->errorReporter().error(m.fOffset,
341 "layout qualifiers are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400342 return nullptr;
343 }
344
Ethan Nicholasfc994162019-06-06 10:04:27 -0400345 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
346 Modifiers modifiers = m.getModifiers();
John Stilesf2872e62021-05-04 11:38:43 -0400347 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700348}
349
John Stilesad2319f2020-09-02 15:01:47 -0400350std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
351 SkASSERT(n.fKind == ASTNode::Kind::kIf);
352 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500353 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400354 if (!test) {
355 return nullptr;
356 }
357 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
358 if (!ifTrue) {
359 return nullptr;
360 }
John Stilesad2319f2020-09-02 15:01:47 -0400361 std::unique_ptr<Statement> ifFalse;
362 if (iter != n.end()) {
363 ifFalse = this->convertStatement(*(iter++));
364 if (!ifFalse) {
365 return nullptr;
366 }
John Stilesad2319f2020-09-02 15:01:47 -0400367 }
John Stiles5ede6e32021-01-11 13:00:49 -0500368 bool isStatic = n.getBool();
John Stiles23521a82021-03-02 17:02:51 -0500369 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
370 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400371}
372
Ethan Nicholasfc994162019-06-06 10:04:27 -0400373std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
374 SkASSERT(f.fKind == ASTNode::Kind::kFor);
John Stilesde2b6092021-09-17 10:40:47 -0400375 AutoSymbolTable table(&fSymbolTable);
ethannicholas22f939e2016-10-13 13:25:34 -0700376 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400377 auto iter = f.begin();
378 if (*iter) {
379 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700380 if (!initializer) {
381 return nullptr;
382 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700383 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400384 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700385 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400386 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500387 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700388 if (!test) {
389 return nullptr;
390 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400392 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700393 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400394 if (*iter) {
395 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700396 if (!next) {
397 return nullptr;
398 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700399 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400400 ++iter;
401 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402 if (!statement) {
403 return nullptr;
404 }
Brian Osman77ba8102021-01-12 17:15:30 -0500405
John Stiles23521a82021-03-02 17:02:51 -0500406 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
407 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500408}
409
410std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
411 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500413 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700414 if (!test) {
415 return nullptr;
416 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400417 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700418 if (!statement) {
419 return nullptr;
420 }
John Stiles23521a82021-03-02 17:02:51 -0500421 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
422 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423}
424
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500425std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
426 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400427 auto iter = d.begin();
428 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
429 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700430 return nullptr;
431 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500432 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400433 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700434 return nullptr;
435 }
John Stiles23521a82021-03-02 17:02:51 -0500436 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700437}
438
Ethan Nicholascfefec02021-02-09 15:22:57 -0500439std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
440 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
441
442 auto iter = s.begin();
443 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
444 if (!value) {
445 return nullptr;
446 }
John Stilesde2b6092021-09-17 10:40:47 -0400447 AutoSymbolTable table(&fSymbolTable);
Ethan Nicholascfefec02021-02-09 15:22:57 -0500448 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500449 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500450 for (; iter != s.end(); ++iter) {
451 const ASTNode& c = *iter;
452 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
453 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
454 auto childIter = c.begin();
455 if (*childIter) {
456 caseValue = this->convertExpression(*childIter);
457 if (!caseValue) {
458 return nullptr;
459 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500460 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400461 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500462
John Stilesbf16b6c2021-03-12 19:24:31 -0500463 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400464 for (; childIter != c.end(); ++childIter) {
465 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500466 if (!converted) {
467 return nullptr;
468 }
469 statements.push_back(std::move(converted));
470 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500471
472 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500473 }
John Stiles23521a82021-03-02 17:02:51 -0500474 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
475 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500476}
477
Ethan Nicholasfc994162019-06-06 10:04:27 -0400478std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
479 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700480 if (!e) {
481 return nullptr;
482 }
John Stiles3e5871c2021-02-25 20:52:03 -0500483 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700484}
485
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500486std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
487 std::unique_ptr<Expression> result) {
John Stilesa0c04d62021-03-11 23:07:24 -0500488 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500489}
490
491std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
492 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
493 if (r.begin() != r.end()) {
John Stilesff6c5bf2021-09-07 15:22:02 -0400494 if (std::unique_ptr<Expression> value = this->convertExpression(*r.begin())) {
495 return this->convertReturn(r.fOffset, std::move(value));
496 } else {
497 return this->convertReturn(r.fOffset, Poison::Make(r.fOffset, fContext));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500498 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700499 }
John Stilesff6c5bf2021-09-07 15:22:02 -0400500
501 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700502}
503
Ethan Nicholasfc994162019-06-06 10:04:27 -0400504std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
505 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
John Stilesa0c04d62021-03-11 23:07:24 -0500506 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700507}
508
Ethan Nicholasfc994162019-06-06 10:04:27 -0400509std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
510 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
John Stilesa0c04d62021-03-11 23:07:24 -0500511 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700512}
513
Ethan Nicholasfc994162019-06-06 10:04:27 -0400514std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
515 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400516 if (this->programKind() != ProgramKind::kFragment) {
John Stilesb30151e2021-01-11 16:13:08 -0500517 this->errorReporter().error(d.fOffset,
518 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500519 return nullptr;
520 }
John Stilesa0c04d62021-03-11 23:07:24 -0500521 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700522}
523
John Stiles3b204892021-08-27 17:35:35 -0400524void IRGenerator::appendRTAdjustFixupToVertexMain(const FunctionDeclaration& decl, Block* body) {
John Stilesd45ebbc2021-05-18 16:54:36 -0400525 using namespace SkSL::dsl;
526 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
John Stiles3b204892021-08-27 17:35:35 -0400527 using OwnerKind = SkSL::FieldAccess::OwnerKind;
John Stilesd45ebbc2021-05-18 16:54:36 -0400528
John Stiles3b204892021-08-27 17:35:35 -0400529 // If this is a vertex program that uses RTAdjust, and this is main()...
Ethan Nicholasa97ad0f2021-09-02 09:54:04 -0400530 if ((fRTAdjust || fRTAdjustInterfaceBlock) && decl.isMain() &&
531 ProgramKind::kVertex == this->programKind()) {
John Stiles3b204892021-08-27 17:35:35 -0400532 // ... append a line to the end of the function body which fixes up sk_Position.
533 const Variable* skPerVertex = nullptr;
534 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
535 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
536 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
537 }
538
539 SkASSERT(skPerVertex);
540 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
541 return VariableReference::Make(/*offset=*/-1, var);
542 };
543 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
544 return FieldAccess::Make(fContext, Ref(var), idx, OwnerKind::kAnonymousInterfaceBlock);
545 };
546 auto Pos = [&]() -> DSLExpression {
547 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
548 OwnerKind::kAnonymousInterfaceBlock));
549 };
550 auto Adjust = [&]() -> DSLExpression {
551 return DSLExpression(fRTAdjustInterfaceBlock
552 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
553 : Ref(fRTAdjust));
554 };
555
556 auto fixupStmt = DSLStatement(
557 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
558 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
559 0,
560 Pos().w())
561 );
562
563 body->children().push_back(fixupStmt.release());
Brian Osman88cda172020-10-09 12:05:16 -0400564 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000565}
566
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400567void IRGenerator::CheckModifiers(const Context& context,
568 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500569 const Modifiers& modifiers,
570 int permittedModifierFlags,
571 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400572 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
573 { Modifiers::kConst_Flag, "const" },
574 { Modifiers::kIn_Flag, "in" },
575 { Modifiers::kOut_Flag, "out" },
576 { Modifiers::kUniform_Flag, "uniform" },
577 { Modifiers::kFlat_Flag, "flat" },
578 { Modifiers::kNoPerspective_Flag, "noperspective" },
579 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
580 { Modifiers::kInline_Flag, "inline" },
581 { Modifiers::kNoInline_Flag, "noinline" },
582 { Modifiers::kHighp_Flag, "highp" },
583 { Modifiers::kMediump_Flag, "mediump" },
584 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400585 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500586 };
587
Brian Osmanb5de6be2021-08-10 15:16:34 -0400588 int modifierFlags = modifiers.fFlags;
589 for (const auto& f : kModifierFlags) {
590 if (modifierFlags & f.flag) {
591 if (!(permittedModifierFlags & f.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400592 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400593 }
594 modifierFlags &= ~f.flag;
595 }
596 }
597 SkASSERT(modifierFlags == 0);
598
599 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
600 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
601 { Layout::kPushConstant_Flag, "push_constant"},
602 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
603 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
604 { Layout::kLocation_Flag, "location"},
605 { Layout::kOffset_Flag, "offset"},
606 { Layout::kBinding_Flag, "binding"},
607 { Layout::kIndex_Flag, "index"},
608 { Layout::kSet_Flag, "set"},
609 { Layout::kBuiltin_Flag, "builtin"},
610 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
Brian Osmanb5de6be2021-08-10 15:16:34 -0400611 };
Brian Osmana654faa2021-02-26 11:52:59 -0500612
613 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400614 for (const auto& lf : kLayoutFlags) {
615 if (layoutFlags & lf.flag) {
616 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400617 context.fErrors->error(
Brian Osmanb5de6be2021-08-10 15:16:34 -0400618 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500619 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400620 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500621 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400622 }
Brian Osmana654faa2021-02-26 11:52:59 -0500623 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400624}
625
Ethan Nicholasfc994162019-06-06 10:04:27 -0400626void IRGenerator::convertFunction(const ASTNode& f) {
627 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400628 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400629 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400630 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700631 }
John Stilesb9af7232020-08-20 15:57:48 -0400632 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400633 std::vector<std::unique_ptr<Variable>> parameters;
634 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400635 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400636 const ASTNode& param = *(iter++);
637 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500638 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400639 auto paramIter = param.begin();
640 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700641 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400642 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700643 }
John Stilesd39aec92020-12-03 14:37:16 -0500644 if (pd.fIsArray) {
John Stiles80b02af2021-02-12 17:07:51 -0500645 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500646 if (!arraySize) {
647 return;
648 }
John Stilesa2179502020-12-03 14:37:57 -0500649 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400651
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400652 parameters.push_back(std::make_unique<Variable>(param.fOffset,
653 this->modifiersPool().add(pd.fModifiers),
654 pd.fName,
655 type,
656 fIsBuiltinCode,
657 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700658 }
659
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400660 // Conservatively assume all user-defined functions have side effects.
661 Modifiers declModifiers = funcData.fModifiers;
662 if (!fIsBuiltinCode) {
663 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400664 }
665
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400666 if (fContext.fConfig->fSettings.fForceNoInline) {
667 // Apply the `noinline` modifier to every function. This allows us to test Runtime
668 // Effects without any inlining, even when the code is later added to a paint.
669 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
670 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700671 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400672
673 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
674 fContext,
675 *fSymbolTable,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400676 f.fOffset,
677 this->modifiersPool().add(declModifiers),
678 funcData.fName,
679 std::move(parameters),
680 returnType,
681 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700682 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400683 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700684 }
John Stiles569249b2020-11-03 12:18:22 -0500685 if (iter == f.end()) {
686 // If there's no body, we've found a prototype.
687 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
688 fIsBuiltinCode));
689 } else {
690 // Compile function body.
John Stilesde2b6092021-09-17 10:40:47 -0400691 AutoSymbolTable table(&fSymbolTable);
John Stiles569249b2020-11-03 12:18:22 -0500692 for (const Variable* param : decl->parameters()) {
693 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700694 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400695 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700696 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400697 return;
698 }
John Stiles3b204892021-08-27 17:35:35 -0400699 this->appendRTAdjustFixupToVertexMain(*decl, body.get());
700 std::unique_ptr<FunctionDefinition> result = FunctionDefinition::Convert(
701 fContext, f.fOffset, *decl, std::move(body), fIsBuiltinCode);
Ethan Nicholased84b732020-10-08 11:45:44 -0400702 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400703 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500704 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700705 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700706}
707
John Stilesdc75a972020-11-25 16:24:55 -0500708std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
709 SkASSERT(node.fKind == ASTNode::Kind::kType);
710
711 const Type* type = this->convertType(node);
712 if (!type) {
713 return nullptr;
714 }
John Stilesc0c51062020-12-03 17:16:29 -0500715 if (!type->isStruct()) {
John Stilesb30151e2021-01-11 16:13:08 -0500716 this->errorReporter().error(node.fOffset,
717 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500718 return nullptr;
719 }
Brian Osman02bc5222021-01-28 11:00:20 -0500720 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
721 SkASSERT(wasInserted);
John Stilesdc75a972020-11-25 16:24:55 -0500722 return std::make_unique<StructDefinition>(node.fOffset, *type);
723}
724
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400725void IRGenerator::scanInterfaceBlock(SkSL::InterfaceBlock& intf) {
726 const std::vector<Type::Field>& fields = intf.variable().type().componentType().fields();
727 for (size_t i = 0; i < fields.size(); ++i) {
728 const Type::Field& f = fields[i];
729 if (f.fName == Compiler::RTADJUST_NAME) {
730 if (*f.fType == *fContext.fTypes.fFloat4) {
731 fRTAdjustInterfaceBlock = &intf.variable();
732 fRTAdjustFieldIndex = i;
733 } else {
734 this->errorReporter().error(intf.fOffset, "sk_RTAdjust must have type 'float4'");
735 }
736 }
737 }
738}
739
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400740std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500741 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400742 this->programKind() != ProgramKind::kVertex) {
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400743 this->errorReporter().error(intf.fOffset,
744 "interface blocks are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400745 return nullptr;
746 }
747
Ethan Nicholasfc994162019-06-06 10:04:27 -0400748 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500749 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700750 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400751 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700752 std::vector<Type::Field> fields;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400753 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400754 {
John Stilesde2b6092021-09-17 10:40:47 -0400755 AutoSymbolTable table(&fSymbolTable);
John Stiles869cdef2020-10-30 14:24:24 -0400756 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400757 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
758 StatementArray decls = this->convertVarDeclarations(*(iter++),
759 Variable::Storage::kInterfaceBlock);
760 if (decls.empty()) {
761 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400762 }
John Stiles869cdef2020-10-30 14:24:24 -0400763 for (const auto& decl : decls) {
764 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -0400765 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
766 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400767 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500768 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700769 }
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400770 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400771 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -0500772 fields));
John Stilesd39aec02020-12-03 10:42:26 -0500773 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -0500774 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400775 const ASTNode& size = *(iter++);
Brian Osmanb42c3832021-08-27 15:19:03 -0400776 arraySize = this->convertArraySize(*type, size.fOffset, size);
777 if (!arraySize) {
778 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500779 }
John Stilesa2179502020-12-03 14:37:57 -0500780 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500781 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400782 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400783 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -0400784 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400785 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400786 type,
Brian Osman3887a012020-09-30 13:22:27 -0400787 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400788 Variable::Storage::kGlobal));
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400789 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -0400790 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700791 } else {
792 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -0400793 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -0700794 }
795 }
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400796 std::unique_ptr<SkSL::InterfaceBlock> result = std::make_unique<SkSL::InterfaceBlock>(
Ethan Nicholas2816dcf2021-09-21 09:18:47 -0400797 intf.fOffset, *var, id.fTypeName, id.fInstanceName, arraySize, symbols);
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400798 this->scanInterfaceBlock(*result);
799 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700800}
801
John Stiles7bd70332020-11-30 17:04:09 -0500802void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
803 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
804 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -0500805 const Type* type = &stmt->as<VarDeclaration>().baseType();
806 if (type->isStruct()) {
807 auto [iter, wasInserted] = fDefinedStructs.insert(type);
808 if (wasInserted) {
809 fProgramElements->push_back(
810 std::make_unique<StructDefinition>(decl.fOffset, *type));
811 }
812 }
Ethan Nicholas624a5292021-04-16 14:54:43 -0400813 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -0500814 }
815}
816
Brian Osmand8070392020-09-09 15:50:02 -0400817const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400818 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -0500819 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -0500820 if (!symbol || !symbol->is<Type>()) {
Brian Osman00fea5b2021-01-28 09:46:30 -0500821 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -0500822 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700823 }
John Stiles6bef6a72020-12-02 14:26:04 -0500824 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -0500825 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -0400826 if (result->isVoid() && !allowVoid) {
John Stiles80b02af2021-02-12 17:07:51 -0500827 this->errorReporter().error(type.fOffset,
828 "type '" + name + "' not allowed in this context");
829 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -0500830 }
John Stiles823c5042021-08-17 12:09:00 -0400831 if (!fIsBuiltinCode) {
Ethan Nicholas517f4ff2021-09-02 10:57:45 -0400832 if (result->containsPrivateFields()) {
John Stiles823c5042021-08-17 12:09:00 -0400833 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
834 return nullptr;
835 }
836 if (this->strictES2Mode() && !result->allowedInES2()) {
837 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
838 return nullptr;
839 }
John Stiles7bd70332020-11-30 17:04:09 -0500840 }
John Stiles6bef6a72020-12-02 14:26:04 -0500841 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -0500842 auto iter = type.begin();
John Stiles80b02af2021-02-12 17:07:51 -0500843 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -0500844 if (!arraySize) {
845 return nullptr;
846 }
John Stilesa2179502020-12-03 14:37:57 -0500847 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -0500848 }
John Stiles6bef6a72020-12-02 14:26:04 -0500849 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700850}
851
Ethan Nicholasfc994162019-06-06 10:04:27 -0400852std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700853 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400854 case ASTNode::Kind::kBinary:
855 return this->convertBinaryExpression(expr);
856 case ASTNode::Kind::kBool:
John Stiles7591d4b2021-09-13 13:32:06 -0400857 return Literal::MakeBool(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400858 case ASTNode::Kind::kCall:
859 return this->convertCallExpression(expr);
860 case ASTNode::Kind::kField:
861 return this->convertFieldExpression(expr);
862 case ASTNode::Kind::kFloat:
John Stiles7591d4b2021-09-13 13:32:06 -0400863 return Literal::MakeFloat(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400864 case ASTNode::Kind::kIdentifier:
865 return this->convertIdentifier(expr);
866 case ASTNode::Kind::kIndex:
867 return this->convertIndexExpression(expr);
868 case ASTNode::Kind::kInt:
John Stiles7591d4b2021-09-13 13:32:06 -0400869 return Literal::MakeInt(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400870 case ASTNode::Kind::kPostfix:
871 return this->convertPostfixExpression(expr);
872 case ASTNode::Kind::kPrefix:
873 return this->convertPrefixExpression(expr);
874 case ASTNode::Kind::kTernary:
875 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700876 default:
John Stilesfbb905e2021-08-25 15:51:50 -0400877 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500878 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700879 }
880}
881
Ethan Nicholas962dec42021-06-10 13:06:39 -0400882std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500883 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -0700884 if (!result) {
John Stilesb30151e2021-01-11 16:13:08 -0500885 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700886 return nullptr;
887 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400888 switch (result->kind()) {
889 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -0700890 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -0400891 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -0700892 };
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500893 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700894 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400895 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400896 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500897 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -0700898 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400899 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400900 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400901 const Modifiers& modifiers = var->modifiers();
902 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400903 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -0400904 if (caps().canUseFragCoord()) {
905 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400906 }
Brian Salomond8d85b92021-07-07 09:41:17 -0400907 break;
908 case SK_CLOCKWISE_BUILTIN:
909 fInputs.fUseFlipRTUniform = true;
910 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400911 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500912 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesa94e0262021-04-27 17:29:59 -0400913 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -0700914 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400915 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400916 const Field* field = &result->as<Field>();
John Stilesa94e0262021-04-27 17:29:59 -0400917 auto base = VariableReference::Make(offset, &field->owner(),
918 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -0500919 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
920 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700921 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400922 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400923 const Type* t = &result->as<Type>();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500924 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -0700925 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400926 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500927 const ExternalFunction* r = &result->as<ExternalFunction>();
928 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400929 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700930 default:
John Stilesf57207b2021-02-02 17:50:34 -0500931 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -0700932 }
Ethan Nicholasc0709392017-06-27 11:20:22 -0400933}
934
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500935std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400936 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500937}
938
Ethan Nicholas11d53972016-11-28 11:23:23 -0500939std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700940 const Type& type) {
John Stiles54f00492021-02-19 11:46:10 -0500941 return type.coerceExpression(std::move(expr), fContext);
ethannicholasb3058bd2016-07-01 08:22:01 -0700942}
943
Ethan Nicholasfc994162019-06-06 10:04:27 -0400944std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
945 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
946 auto iter = expression.begin();
947 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700948 if (!left) {
949 return nullptr;
950 }
John Stiles0f464502020-11-20 12:52:22 -0500951 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700952 if (!right) {
953 return nullptr;
954 }
John Stiles23521a82021-03-02 17:02:51 -0500955 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
956 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -0700957}
958
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500959std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
960 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
961 auto iter = node.begin();
962 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
963 if (!test) {
964 return nullptr;
965 }
966 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
967 if (!ifTrue) {
968 return nullptr;
969 }
970 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
971 if (!ifFalse) {
972 return nullptr;
973 }
John Stiles23521a82021-03-02 17:02:51 -0500974 return TernaryExpression::Convert(fContext, std::move(test),
975 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500976}
977
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400978void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -0400979 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -0400980 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -0400981
982 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
983 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400984 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
985 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -0400986 std::sort(intrinsics.begin(), intrinsics.end(),
987 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400988 if (a->isBuiltin() != b->isBuiltin()) {
989 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -0400990 }
991 if (a->fOffset != b->fOffset) {
992 return a->fOffset < b->fOffset;
993 }
Ethan Nicholase2c49992020-10-05 11:49:11 -0400994 if (a->name() != b->name()) {
995 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -0400996 }
997 return a->description() < b->description();
998 });
999 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001000 this->copyIntrinsicIfNeeded(*f);
1001 }
John Stiles607d36b2020-10-19 15:00:01 -04001002
Brian Osman0006ad02020-11-18 15:38:39 -05001003 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001004 }
1005}
1006
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001007std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001008 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001009 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001010 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001011 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001012 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001013 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001014 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001015 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001016 }
1017 }
John Stilesea9ab822020-08-31 09:55:04 -04001018
John Stilescd7ba502021-03-19 10:54:59 -04001019 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001020}
1021
1022/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001023 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001024 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1025 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001026 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001027CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
Brian Osman3099f792021-09-01 13:12:16 -04001028 const ExpressionArray& arguments) const {
John Stilesefde90d2021-08-12 23:06:24 -04001029 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1030 return CoercionCost::Impossible();
1031 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001032 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001033 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001034 }
John Stilesfa889112020-10-12 19:03:43 -04001035 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001036 const Type* ignored;
1037 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001038 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001039 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001040 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001041 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001042 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001043 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001044 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001045}
1046
Brian Osman3099f792021-09-01 13:12:16 -04001047const FunctionDeclaration* IRGenerator::findBestFunctionForCall(
1048 const std::vector<const FunctionDeclaration*>& functions,
1049 const ExpressionArray& arguments) const {
1050 if (functions.size() == 1) {
1051 return functions.front();
1052 }
1053 CoercionCost bestCost = CoercionCost::Impossible();
1054 const FunctionDeclaration* best = nullptr;
1055 for (const auto& f : functions) {
1056 CoercionCost cost = this->callCost(*f, arguments);
1057 if (cost < bestCost) {
1058 bestCost = cost;
1059 best = f;
1060 }
1061 }
1062 return best;
1063}
1064
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001065std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001066 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001067 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001068 switch (functionValue->kind()) {
1069 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001070 return Constructor::Convert(fContext,
1071 offset,
1072 functionValue->as<TypeReference>().value(),
1073 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001074 case Expression::Kind::kExternalFunctionReference: {
1075 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1076 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001077 if (count != (int) arguments.size()) {
John Stilesb30151e2021-01-11 16:13:08 -05001078 this->errorReporter().error(offset, "external function expected " +
1079 to_string(count) + " arguments, but found " +
1080 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001081 return nullptr;
1082 }
1083 static constexpr int PARAMETER_MAX = 16;
1084 SkASSERT(count < PARAMETER_MAX);
1085 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001086 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001087 for (int i = 0; i < count; ++i) {
1088 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1089 if (!arguments[i]) {
1090 return nullptr;
1091 }
1092 }
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001093 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001094 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001095 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001096 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001097 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman3099f792021-09-01 13:12:16 -04001098 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1099 if (best) {
1100 return this->call(offset, *best, std::move(arguments));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001101 }
Brian Osman3099f792021-09-01 13:12:16 -04001102 String msg = "no match for " + functions[0]->name() + "(";
1103 String separator;
1104 for (size_t i = 0; i < arguments.size(); i++) {
1105 msg += separator;
1106 separator = ", ";
1107 msg += arguments[i]->type().displayName();
1108 }
1109 msg += ")";
1110 this->errorReporter().error(offset, msg);
1111 return nullptr;
1112 }
1113 case Expression::Kind::kMethodReference: {
1114 MethodReference& ref = functionValue->as<MethodReference>();
1115 arguments.push_back(std::move(ref.self()));
1116
1117 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
1118 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1119 if (best) {
1120 return this->call(offset, *best, std::move(arguments));
1121 }
1122 String msg = "no match for " + arguments.back()->type().displayName() +
1123 "::" + functions[0]->name().substr(1) + "(";
1124 String separator;
1125 for (size_t i = 0; i < arguments.size() - 1; i++) {
1126 msg += separator;
1127 separator = ", ";
1128 msg += arguments[i]->type().displayName();
1129 }
1130 msg += ")";
1131 this->errorReporter().error(offset, msg);
1132 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001133 }
Ethan Nicholasace470b2021-08-30 16:15:20 -04001134 case Expression::Kind::kPoison:
1135 return functionValue;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001136 default:
John Stilesb30151e2021-01-11 16:13:08 -05001137 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001138 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001139 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001140}
1141
Ethan Nicholasfc994162019-06-06 10:04:27 -04001142std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1143 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1144 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001145 if (!base) {
1146 return nullptr;
1147 }
John Stiles23521a82021-03-02 17:02:51 -05001148 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001149}
1150
ethannicholasb3058bd2016-07-01 08:22:01 -07001151std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001152 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001153 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001154}
1155
Ethan Nicholasfc994162019-06-06 10:04:27 -04001156std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1157 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1158 auto iter = index.begin();
1159 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001160 if (!base) {
1161 return nullptr;
1162 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001163 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001164 if (base->is<TypeReference>()) {
1165 this->errorReporter().error(index.fOffset, "array must have a size");
1166 } else {
1167 this->errorReporter().error(base->fOffset, "missing index in '[]'");
1168 }
Brian Osman4cf85072021-01-14 14:18:10 -05001169 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001170 }
1171 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1172 if (!converted) {
1173 return nullptr;
1174 }
John Stilesbb8cf582021-08-26 23:34:59 -04001175 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001176}
1177
Ethan Nicholasfc994162019-06-06 10:04:27 -04001178std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1179 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1180 auto iter = callNode.begin();
1181 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1182 if (!base) {
1183 return nullptr;
1184 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001185 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001186 for (; iter != callNode.end(); ++iter) {
1187 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1188 if (!converted) {
1189 return nullptr;
1190 }
1191 arguments.push_back(std::move(converted));
1192 }
1193 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
1194}
1195
1196std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1197 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1198 if (!base) {
1199 return nullptr;
1200 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001201 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001202 const Type& baseType = base->type();
Brian Osman3099f792021-09-01 13:12:16 -04001203 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct() || baseType.isEffectChild()) {
1204 return FieldAccess::Convert(fContext, *fSymbolTable, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001205 }
John Stiles06d600f2021-03-08 09:18:21 -05001206 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001207}
1208
1209std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001210 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001211 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1212 if (!base) {
1213 return nullptr;
1214 }
John Stiles23521a82021-03-02 17:02:51 -05001215 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001216}
1217
Brian Osman9496fe52020-11-18 14:48:19 -05001218void IRGenerator::findAndDeclareBuiltinVariables() {
1219 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001220 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001221 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001222
Brian Osman9496fe52020-11-18 14:48:19 -05001223 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001224 // If this is the *first* time we've seen this builtin, findAndInclude will return
1225 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001226 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001227 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001228 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001229 }
1230 }
1231
John Stiles4d6310a2021-01-26 19:58:22 -05001232 bool visitProgramElement(const ProgramElement& pe) override {
1233 if (pe.is<FunctionDefinition>()) {
1234 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1235 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1236 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001237 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001238 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1239 fPreserveFragColor = true;
1240 }
1241 }
1242 return INHERITED::visitProgramElement(pe);
1243 }
1244
Brian Osman9496fe52020-11-18 14:48:19 -05001245 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001246 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001247 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001248 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001249 return INHERITED::visitExpression(e);
1250 }
1251
1252 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001253 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001254 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001255
Brian Osman9496fe52020-11-18 14:48:19 -05001256 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001257 using INHERITED::visitProgramElement;
1258 };
1259
Brian Osman9496fe52020-11-18 14:48:19 -05001260 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001261 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001262 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001263 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001264 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001265
John Stiles4d6310a2021-01-26 19:58:22 -05001266 if (scanner.fPreserveFragColor) {
1267 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001268 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001269 }
1270
John Stilesd1204642021-02-17 16:30:02 -05001271 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001272 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001273 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1274 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1275 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001276 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001277 break;
1278 default:
1279 break;
1280 }
1281
Brian Osman9496fe52020-11-18 14:48:19 -05001282 fSharedElements->insert(
1283 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001284}
1285
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001286void IRGenerator::start(const ParsedModule& base,
1287 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001288 std::vector<std::unique_ptr<ProgramElement>>* elements,
1289 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001290 fProgramElements = elements;
1291 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001292 fSymbolTable = base.fSymbols;
1293 fIntrinsics = base.fIntrinsics.get();
1294 if (fIntrinsics) {
1295 fIntrinsics->resetAlreadyIncluded();
1296 }
1297 fIsBuiltinCode = isBuiltinCode;
1298
Brian Salomond8d85b92021-07-07 09:41:17 -04001299 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001300 fRTAdjust = nullptr;
1301 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001302 fDefinedStructs.clear();
John Stilesde2b6092021-09-17 10:40:47 -04001303 SymbolTable::Push(&fSymbolTable, fIsBuiltinCode);
Brian Osman88cda172020-10-09 12:05:16 -04001304
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001305 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001306 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001307 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001308 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001309 }
1310 }
John Stilesaecf8d52021-05-14 12:15:01 -04001311
John Stilesbb2ef922021-07-26 08:32:07 -04001312 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001313 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001314 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001315 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1316 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1317 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1318 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1319 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1320 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1321 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1322 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1323 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1324
1325 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1326 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1327 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1328 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1329 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1330 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1331
1332 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1333 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1334 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1335 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1336 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1337 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001338
1339 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1340 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1341 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1342 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001343
1344 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1345 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1346 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1347 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1348
1349 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1350 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1351 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1352 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001353 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001354}
Brian Osman88cda172020-10-09 12:05:16 -04001355
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001356IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001357 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001358 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001359 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001360 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001361
John Stilesf2872e62021-05-04 11:38:43 -04001362 return IRBundle{std::move(*fProgramElements),
1363 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001364 std::move(fSymbolTable),
1365 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001366}
1367
1368IRGenerator::IRBundle IRGenerator::convertProgram(
1369 const ParsedModule& base,
1370 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001371 skstd::string_view text) {
1372 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001373 fFile = parser.compilationUnit();
1374 if (this->errorReporter().errorCount() == 0) {
1375 SkASSERT(fFile);
1376 for (const auto& decl : fFile->root()) {
1377 switch (decl.fKind) {
1378 case ASTNode::Kind::kVarDeclarations:
1379 this->convertGlobalVarDeclarations(decl);
1380 break;
1381
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001382 case ASTNode::Kind::kFunction:
1383 this->convertFunction(decl);
1384 break;
1385
1386 case ASTNode::Kind::kModifiers: {
1387 std::unique_ptr<ModifiersDeclaration> f =
1388 this->convertModifiersDeclaration(decl);
1389 if (f) {
1390 fProgramElements->push_back(std::move(f));
1391 }
1392 break;
1393 }
1394 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001395 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001396 if (i) {
1397 fProgramElements->push_back(std::move(i));
1398 }
1399 break;
1400 }
1401 case ASTNode::Kind::kExtension: {
1402 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001403 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001404 if (e) {
1405 fProgramElements->push_back(std::move(e));
1406 }
1407 break;
1408 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001409 case ASTNode::Kind::kType: {
1410 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1411 if (s) {
1412 fProgramElements->push_back(std::move(s));
1413 }
1414 break;
1415 }
1416 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001417 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001418 break;
1419 }
1420 }
1421 }
1422 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001423}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001424
John Stilesa6841be2020-08-06 14:11:56 -04001425} // namespace SkSL