ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLIRGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
| 10 | #include "limits.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 11 | #include <iterator> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | #include <memory> |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 13 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 15 | #include "include/private/SkSLLayout.h" |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 16 | #include "include/private/SkTArray.h" |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 17 | #include "include/sksl/DSLCore.h" |
John Stiles | b2705a1 | 2021-02-19 10:55:14 -0500 | [diff] [blame] | 18 | #include "src/core/SkScopeExit.h" |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 19 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLConstantFolder.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 22 | #include "src/sksl/SkSLOperators.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLParser.h" |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 24 | #include "src/sksl/SkSLUtil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 26 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 27 | #include "src/sksl/ir/SkSLBreakStatement.h" |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLChildCall.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLConstructor.h" |
| 30 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 32 | #include "src/sksl/ir/SkSLDoStatement.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 34 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 35 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/sksl/ir/SkSLField.h" |
| 37 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 38 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 39 | #include "src/sksl/ir/SkSLForStatement.h" |
| 40 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 41 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 42 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 45 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 46 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 47 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 48 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 49 | #include "src/sksl/ir/SkSLNop.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 51 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 52 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 53 | #include "src/sksl/ir/SkSLSetting.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 54 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 55 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 56 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 57 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 58 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 59 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 60 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 61 | #include "src/sksl/ir/SkSLVariable.h" |
| 62 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | |
| 64 | namespace SkSL { |
| 65 | |
| 66 | class AutoSymbolTable { |
| 67 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 68 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 69 | : fIR(ir) |
| 70 | , fPrevious(fIR->fSymbolTable) { |
| 71 | fIR->pushSymbolTable(); |
| 72 | } |
| 73 | |
| 74 | ~AutoSymbolTable() { |
| 75 | fIR->popSymbolTable(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 76 | SkASSERT(fPrevious == fIR->fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | IRGenerator* fIR; |
| 80 | std::shared_ptr<SymbolTable> fPrevious; |
| 81 | }; |
| 82 | |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 83 | IRGenerator::IRGenerator(const Context* context) |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 84 | : fContext(*context) {} |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 85 | |
| 86 | void IRGenerator::pushSymbolTable() { |
| 87 | auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode); |
| 88 | fSymbolTable = std::move(childSymTable); |
| 89 | } |
| 90 | |
| 91 | void IRGenerator::popSymbolTable() { |
| 92 | fSymbolTable = fSymbolTable->fParent; |
| 93 | } |
| 94 | |
John Stiles | e3a91cf | 2021-01-26 10:13:58 -0500 | [diff] [blame] | 95 | bool IRGenerator::detectVarDeclarationWithoutScope(const Statement& stmt) { |
| 96 | // Parsing an AST node containing a single variable declaration creates a lone VarDeclaration |
| 97 | // statement. An AST with multiple variable declarations creates an unscoped Block containing |
| 98 | // multiple VarDeclaration statements. We need to detect either case. |
| 99 | const Variable* var; |
| 100 | if (stmt.is<VarDeclaration>()) { |
| 101 | // The single-variable case. No blocks at all. |
| 102 | var = &stmt.as<VarDeclaration>().var(); |
| 103 | } else if (stmt.is<Block>()) { |
| 104 | // The multiple-variable case: an unscoped, non-empty block... |
| 105 | const Block& block = stmt.as<Block>(); |
| 106 | if (block.isScope() || block.children().empty()) { |
| 107 | return false; |
| 108 | } |
| 109 | // ... holding a variable declaration. |
| 110 | const Statement& innerStmt = *block.children().front(); |
| 111 | if (!innerStmt.is<VarDeclaration>()) { |
| 112 | return false; |
| 113 | } |
| 114 | var = &innerStmt.as<VarDeclaration>().var(); |
| 115 | } else { |
| 116 | // This statement wasn't a variable declaration. No problem. |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Report an error. |
| 121 | SkASSERT(var); |
| 122 | this->errorReporter().error(stmt.fOffset, |
| 123 | "variable '" + var->name() + "' must be created in a scope"); |
| 124 | return true; |
| 125 | } |
| 126 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 127 | std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 128 | if (this->programKind() != ProgramKind::kFragment && |
| 129 | this->programKind() != ProgramKind::kVertex && |
| 130 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 131 | this->errorReporter().error(offset, "extensions are not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 132 | return nullptr; |
| 133 | } |
| 134 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 135 | return std::make_unique<Extension>(offset, name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 136 | } |
| 137 | |
John Stiles | 20c09f9 | 2021-03-12 09:25:04 -0500 | [diff] [blame] | 138 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | switch (statement.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 140 | case ASTNode::Kind::kBlock: |
| 141 | return this->convertBlock(statement); |
| 142 | case ASTNode::Kind::kVarDeclarations: |
| 143 | return this->convertVarDeclarationStatement(statement); |
| 144 | case ASTNode::Kind::kIf: |
| 145 | return this->convertIf(statement); |
| 146 | case ASTNode::Kind::kFor: |
| 147 | return this->convertFor(statement); |
| 148 | case ASTNode::Kind::kWhile: |
| 149 | return this->convertWhile(statement); |
| 150 | case ASTNode::Kind::kDo: |
| 151 | return this->convertDo(statement); |
| 152 | case ASTNode::Kind::kSwitch: |
| 153 | return this->convertSwitch(statement); |
| 154 | case ASTNode::Kind::kReturn: |
| 155 | return this->convertReturn(statement); |
| 156 | case ASTNode::Kind::kBreak: |
| 157 | return this->convertBreak(statement); |
| 158 | case ASTNode::Kind::kContinue: |
| 159 | return this->convertContinue(statement); |
| 160 | case ASTNode::Kind::kDiscard: |
| 161 | return this->convertDiscard(statement); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 162 | case ASTNode::Kind::kType: |
| 163 | // TODO: add IRNode for struct definition inside a function |
| 164 | return nullptr; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 165 | default: |
| 166 | // it's an expression |
| 167 | std::unique_ptr<Statement> result = this->convertExpressionStatement(statement); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 168 | if (fRTAdjust && this->programKind() == ProgramKind::kGeometry) { |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 169 | SkASSERT(result->is<ExpressionStatement>()); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 170 | Expression& expr = *result->as<ExpressionStatement>().expression(); |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 171 | if (expr.is<FunctionCall>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 172 | FunctionCall& fc = expr.as<FunctionCall>(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 173 | if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 174 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 175 | statements.reserve_back(2); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 176 | statements.push_back(getNormalizeSkPositionCode()); |
| 177 | statements.push_back(std::move(result)); |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 178 | return Block::Make(statement.fOffset, std::move(statements), |
| 179 | fSymbolTable, /*isScope=*/true); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 187 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) { |
| 188 | SkASSERT(block.fKind == ASTNode::Kind::kBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | AutoSymbolTable table(this); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 190 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 191 | for (const auto& child : block) { |
| 192 | std::unique_ptr<Statement> statement = this->convertStatement(child); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 193 | if (!statement) { |
| 194 | return nullptr; |
| 195 | } |
| 196 | statements.push_back(std::move(statement)); |
| 197 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 198 | return Block::Make(block.fOffset, std::move(statements), fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 201 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) { |
| 202 | SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 203 | auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 204 | if (decls.empty()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 205 | return nullptr; |
| 206 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 207 | return Block::MakeUnscoped(s.fOffset, std::move(decls)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | } |
| 209 | |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 210 | int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) { |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 211 | if (!s) { |
| 212 | this->errorReporter().error(offset, "array must have a size"); |
| 213 | return 0; |
| 214 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 215 | auto size = this->convertExpression(s); |
| 216 | if (!size) { |
| 217 | return 0; |
| 218 | } |
John Stiles | 7253a20 | 2021-08-23 09:24:33 -0400 | [diff] [blame] | 219 | return type.convertArraySize(fContext, std::move(size)); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 222 | void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType, |
| 223 | Variable::Storage storage) { |
John Stiles | e609598 | 2021-02-04 12:42:01 -0500 | [diff] [blame] | 224 | if (this->strictES2Mode() && baseType->isArray()) { |
| 225 | this->errorReporter().error(offset, "array size must appear after variable name"); |
| 226 | } |
| 227 | |
John Stiles | 3624aba | 2020-12-22 16:29:53 -0500 | [diff] [blame] | 228 | if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 229 | this->errorReporter().error( |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 230 | offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 231 | "variables of type '" + baseType->displayName() + "' must be global"); |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 232 | } |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 233 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) { |
| 234 | this->errorReporter().error(offset, "'in' variables may not have matrix type"); |
| 235 | } |
| 236 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 237 | this->errorReporter().error(offset, "'in uniform' variables not permitted"); |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 238 | } |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 239 | if (this->isRuntimeEffect()) { |
Brian Osman | 5c62582 | 2021-04-06 14:28:07 -0400 | [diff] [blame] | 240 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Brian Osman | 3967588 | 2021-03-31 14:37:10 -0400 | [diff] [blame] | 241 | this->errorReporter().error(offset, "'in' variables not permitted in runtime effects"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
Brian Osman | 5c62582 | 2021-04-06 14:28:07 -0400 | [diff] [blame] | 244 | if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 245 | this->errorReporter().error( |
| 246 | offset, "variables of type '" + baseType->displayName() + "' must be uniform"); |
| 247 | } |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 248 | if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) { |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 249 | if (!this->isRuntimeEffect()) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 250 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 251 | "'srgb_unpremul' is only permitted in runtime effects"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 252 | } |
| 253 | if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 254 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 255 | "'srgb_unpremul' is only permitted on 'uniform' variables"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 256 | } |
| 257 | auto validColorXformType = [](const Type& t) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 258 | return t.isVector() && t.componentType().isFloat() && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 259 | (t.columns() == 3 || t.columns() == 4); |
| 260 | }; |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 261 | if (!validColorXformType(*baseType) && !(baseType->isArray() && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 262 | validColorXformType(baseType->componentType()))) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 263 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 264 | "'srgb_unpremul' is only permitted on half3, half4, " |
| 265 | "float3, or float4 variables"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
John Stiles | b806da4 | 2021-08-04 16:26:47 -0400 | [diff] [blame] | 268 | int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag | |
| 269 | Modifiers::kLowp_Flag; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 270 | if (storage == Variable::Storage::kGlobal) { |
Brian Osman | 9ca38ff | 2021-02-18 12:58:54 -0500 | [diff] [blame] | 271 | permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 272 | Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag; |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 273 | } |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 274 | // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 275 | CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 278 | std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 279 | const Type* baseType, skstd::string_view name, |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 280 | bool isArray, |
| 281 | std::unique_ptr<Expression> arraySize, |
| 282 | Variable::Storage storage) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 283 | if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 284 | (modifiers.fFlags & Modifiers::kOut_Flag) && |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 285 | this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 286 | this->errorReporter().error(offset, |
| 287 | "out location=0, index=0 is reserved for sk_FragColor"); |
| 288 | } |
| 289 | const Type* type = baseType; |
| 290 | int arraySizeValue = 0; |
| 291 | if (isArray) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 292 | SkASSERT(arraySize); |
John Stiles | 7253a20 | 2021-08-23 09:24:33 -0400 | [diff] [blame] | 293 | arraySizeValue = type->convertArraySize(fContext, std::move(arraySize)); |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 294 | if (!arraySizeValue) { |
| 295 | return {}; |
| 296 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 297 | type = fSymbolTable->addArrayDimension(type, arraySizeValue); |
| 298 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 299 | return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name, |
| 300 | type, fIsBuiltinCode, storage); |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var, |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 304 | std::unique_ptr<Expression> value, |
| 305 | bool addToSymbolTable) { |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 306 | std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(), |
| 307 | std::move(value)); |
| 308 | if (!varDecl) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
| 312 | // Detect the declaration of magical variables. |
| 313 | if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) { |
| 314 | // Silently ignore duplicate definitions of `sk_FragColor`. |
| 315 | const Symbol* symbol = (*fSymbolTable)[var->name()]; |
| 316 | if (symbol) { |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 317 | return nullptr; |
| 318 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 319 | } else if ((var->storage() == Variable::Storage::kGlobal || |
| 320 | var->storage() == Variable::Storage::kInterfaceBlock) && |
| 321 | var->name() == Compiler::RTADJUST_NAME) { |
| 322 | // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions. |
| 323 | if (fRTAdjust) { |
| 324 | this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'"); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 325 | return nullptr; |
| 326 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 327 | if (var->type() != *fContext.fTypes.fFloat4) { |
| 328 | this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'"); |
Brian Osman | fc4b991 | 2021-03-05 16:45:06 -0500 | [diff] [blame] | 329 | return nullptr; |
| 330 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 331 | fRTAdjust = var.get(); |
| 332 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 333 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 334 | if (addToSymbolTable) { |
| 335 | fSymbolTable->add(std::move(var)); |
| 336 | } else { |
| 337 | fSymbolTable->takeOwnershipOfSymbol(std::move(var)); |
| 338 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 339 | return varDecl; |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset, |
| 343 | const Modifiers& modifiers, |
| 344 | const Type* baseType, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 345 | skstd::string_view name, |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 346 | bool isArray, |
| 347 | std::unique_ptr<Expression> arraySize, |
| 348 | std::unique_ptr<Expression> value, |
| 349 | Variable::Storage storage) { |
| 350 | std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray, |
| 351 | std::move(arraySize), storage); |
| 352 | if (!var) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | return this->convertVarDeclaration(std::move(var), std::move(value)); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls, |
| 359 | Variable::Storage storage) { |
| 360 | SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations); |
| 361 | auto declarationsIter = decls.begin(); |
| 362 | const Modifiers& modifiers = declarationsIter++->getModifiers(); |
| 363 | const ASTNode& rawType = *(declarationsIter++); |
| 364 | const Type* baseType = this->convertType(rawType); |
| 365 | if (!baseType) { |
| 366 | return {}; |
| 367 | } |
John Stiles | b806da4 | 2021-08-04 16:26:47 -0400 | [diff] [blame] | 368 | baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(), |
| 369 | decls.fOffset); |
| 370 | if (!baseType) { |
| 371 | return {}; |
| 372 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 373 | |
| 374 | this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 375 | |
| 376 | StatementArray varDecls; |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 377 | for (; declarationsIter != decls.end(); ++declarationsIter) { |
| 378 | const ASTNode& varDecl = *declarationsIter; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 379 | const ASTNode::VarData& varData = varDecl.getVarData(); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 380 | std::unique_ptr<Expression> arraySize; |
| 381 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 382 | auto iter = varDecl.begin(); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 383 | if (iter != varDecl.end() && varData.fIsArray) { |
John Stiles | 8f633ef | 2021-08-21 17:13:11 -0400 | [diff] [blame] | 384 | if (!*iter) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 385 | this->errorReporter().error(decls.fOffset, "array must have a size"); |
John Stiles | fabed8b | 2021-03-29 09:38:59 -0400 | [diff] [blame] | 386 | continue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 387 | } |
John Stiles | 8f633ef | 2021-08-21 17:13:11 -0400 | [diff] [blame] | 388 | arraySize = this->convertExpression(*iter++); |
| 389 | if (!arraySize) { |
| 390 | continue; |
| 391 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 392 | } |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 393 | if (iter != varDecl.end()) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 394 | value = this->convertExpression(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 395 | if (!value) { |
John Stiles | fabed8b | 2021-03-29 09:38:59 -0400 | [diff] [blame] | 396 | continue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 397 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 398 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 399 | std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset, |
| 400 | modifiers, |
| 401 | baseType, |
| 402 | varData.fName, |
| 403 | varData.fIsArray, |
| 404 | std::move(arraySize), |
| 405 | std::move(value), |
| 406 | storage); |
| 407 | if (varDeclStmt) { |
| 408 | varDecls.push_back(std::move(varDeclStmt)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 409 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 410 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 411 | return varDecls; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 414 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 415 | if (this->programKind() != ProgramKind::kFragment && |
| 416 | this->programKind() != ProgramKind::kVertex && |
| 417 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 418 | this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 419 | return nullptr; |
| 420 | } |
| 421 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 422 | SkASSERT(m.fKind == ASTNode::Kind::kModifiers); |
| 423 | Modifiers modifiers = m.getModifiers(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 424 | if (modifiers.fLayout.fInvocations != -1) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 425 | if (this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 426 | this->errorReporter().error(m.fOffset, |
| 427 | "'invocations' is only legal in geometry shaders"); |
Ethan Nicholas | f06576b | 2019-04-03 15:45:25 -0400 | [diff] [blame] | 428 | return nullptr; |
| 429 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 430 | fInvocations = modifiers.fLayout.fInvocations; |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 431 | if (!this->caps().gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 432 | modifiers.fLayout.fInvocations = -1; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 433 | if (modifiers.fLayout.description() == "") { |
| 434 | return nullptr; |
| 435 | } |
| 436 | } |
| 437 | } |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 438 | if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && |
| 439 | !this->caps().gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 440 | modifiers.fLayout.fMaxVertices *= fInvocations; |
| 441 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 442 | return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 443 | } |
| 444 | |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 445 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) { |
| 446 | SkASSERT(n.fKind == ASTNode::Kind::kIf); |
| 447 | auto iter = n.begin(); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 448 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 449 | if (!test) { |
| 450 | return nullptr; |
| 451 | } |
| 452 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++)); |
| 453 | if (!ifTrue) { |
| 454 | return nullptr; |
| 455 | } |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 456 | if (this->detectVarDeclarationWithoutScope(*ifTrue)) { |
| 457 | return nullptr; |
| 458 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 459 | std::unique_ptr<Statement> ifFalse; |
| 460 | if (iter != n.end()) { |
| 461 | ifFalse = this->convertStatement(*(iter++)); |
| 462 | if (!ifFalse) { |
| 463 | return nullptr; |
| 464 | } |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 465 | if (this->detectVarDeclarationWithoutScope(*ifFalse)) { |
| 466 | return nullptr; |
| 467 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 468 | } |
John Stiles | 5ede6e3 | 2021-01-11 13:00:49 -0500 | [diff] [blame] | 469 | bool isStatic = n.getBool(); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 470 | return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test), |
| 471 | std::move(ifTrue), std::move(ifFalse)); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 474 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) { |
| 475 | SkASSERT(f.fKind == ASTNode::Kind::kFor); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 476 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 477 | std::unique_ptr<Statement> initializer; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 478 | auto iter = f.begin(); |
| 479 | if (*iter) { |
| 480 | initializer = this->convertStatement(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 481 | if (!initializer) { |
| 482 | return nullptr; |
| 483 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 484 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 485 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 486 | std::unique_ptr<Expression> test; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 487 | if (*iter) { |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 488 | test = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 489 | if (!test) { |
| 490 | return nullptr; |
| 491 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 492 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 493 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 494 | std::unique_ptr<Expression> next; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 495 | if (*iter) { |
| 496 | next = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 497 | if (!next) { |
| 498 | return nullptr; |
| 499 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 500 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 501 | ++iter; |
| 502 | std::unique_ptr<Statement> statement = this->convertStatement(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 503 | if (!statement) { |
| 504 | return nullptr; |
| 505 | } |
John Stiles | 2b6ec98 | 2021-03-04 10:46:39 -0500 | [diff] [blame] | 506 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 507 | return nullptr; |
| 508 | } |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 509 | |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 510 | return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test), |
| 511 | std::move(next), std::move(statement), fSymbolTable); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) { |
| 515 | SkASSERT(w.fKind == ASTNode::Kind::kWhile); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 516 | auto iter = w.begin(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 517 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 518 | if (!test) { |
| 519 | return nullptr; |
| 520 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 521 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | if (!statement) { |
| 523 | return nullptr; |
| 524 | } |
John Stiles | b321a07 | 2021-02-25 16:24:19 -0500 | [diff] [blame] | 525 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 526 | return nullptr; |
| 527 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 528 | return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement), |
| 529 | fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 532 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) { |
| 533 | SkASSERT(d.fKind == ASTNode::Kind::kDo); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 534 | auto iter = d.begin(); |
| 535 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
| 536 | if (!statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 537 | return nullptr; |
| 538 | } |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 539 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 540 | if (!test) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 541 | return nullptr; |
| 542 | } |
John Stiles | ea5822e | 2021-02-26 11:18:20 -0500 | [diff] [blame] | 543 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 544 | return nullptr; |
| 545 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 546 | return DoStatement::Convert(fContext, std::move(statement), std::move(test)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 549 | std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) { |
| 550 | SkASSERT(s.fKind == ASTNode::Kind::kSwitch); |
| 551 | |
| 552 | auto iter = s.begin(); |
| 553 | std::unique_ptr<Expression> value = this->convertExpression(*(iter++)); |
| 554 | if (!value) { |
| 555 | return nullptr; |
| 556 | } |
| 557 | AutoSymbolTable table(this); |
| 558 | ExpressionArray caseValues; |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 559 | StatementArray caseStatements; |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 560 | for (; iter != s.end(); ++iter) { |
| 561 | const ASTNode& c = *iter; |
| 562 | SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase); |
| 563 | std::unique_ptr<Expression>& caseValue = caseValues.emplace_back(); |
| 564 | auto childIter = c.begin(); |
| 565 | if (*childIter) { |
| 566 | caseValue = this->convertExpression(*childIter); |
| 567 | if (!caseValue) { |
| 568 | return nullptr; |
| 569 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 570 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 571 | ++childIter; |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 572 | |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 573 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 574 | for (; childIter != c.end(); ++childIter) { |
| 575 | std::unique_ptr<Statement> converted = this->convertStatement(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 576 | if (!converted) { |
| 577 | return nullptr; |
| 578 | } |
| 579 | statements.push_back(std::move(converted)); |
| 580 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 581 | |
| 582 | caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements))); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 583 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 584 | return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value), |
| 585 | std::move(caseValues), std::move(caseStatements), fSymbolTable); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 588 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) { |
| 589 | std::unique_ptr<Expression> e = this->convertExpression(s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 590 | if (!e) { |
| 591 | return nullptr; |
| 592 | } |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 593 | return ExpressionStatement::Make(fContext, std::move(e)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 596 | std::unique_ptr<Statement> IRGenerator::convertReturn(int offset, |
| 597 | std::unique_ptr<Expression> result) { |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 598 | return ReturnStatement::Make(offset, std::move(result)); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) { |
| 602 | SkASSERT(r.fKind == ASTNode::Kind::kReturn); |
| 603 | if (r.begin() != r.end()) { |
| 604 | std::unique_ptr<Expression> value = this->convertExpression(*r.begin()); |
| 605 | if (!value) { |
| 606 | return nullptr; |
| 607 | } |
| 608 | return this->convertReturn(r.fOffset, std::move(value)); |
| 609 | } else { |
| 610 | return this->convertReturn(r.fOffset, /*result=*/nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 614 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) { |
| 615 | SkASSERT(b.fKind == ASTNode::Kind::kBreak); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 616 | return BreakStatement::Make(b.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 619 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) { |
| 620 | SkASSERT(c.fKind == ASTNode::Kind::kContinue); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 621 | return ContinueStatement::Make(c.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 624 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) { |
| 625 | SkASSERT(d.fKind == ASTNode::Kind::kDiscard); |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 626 | if (this->programKind() != ProgramKind::kFragment) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 627 | this->errorReporter().error(d.fOffset, |
| 628 | "discard statement is only permitted in fragment shaders"); |
Brian Osman | f2876b0 | 2020-12-15 15:40:32 -0500 | [diff] [blame] | 629 | return nullptr; |
| 630 | } |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 631 | return DiscardStatement::Make(d.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 634 | std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 635 | Layout invokeLayout; |
| 636 | Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag); |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 637 | const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>( |
| 638 | /*offset=*/-1, |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 639 | this->modifiersPool().add(invokeModifiers), |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 640 | "_invoke", |
| 641 | std::vector<const Variable*>(), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 642 | fContext.fTypes.fVoid.get(), |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 643 | fIsBuiltinCode)); |
John Stiles | 0d07e14 | 2020-12-30 18:03:09 -0500 | [diff] [blame] | 644 | auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl, fIsBuiltinCode, |
| 645 | std::move(main)); |
| 646 | invokeDecl->setDefinition(invokeDef.get()); |
| 647 | fProgramElements->push_back(std::move(invokeDef)); |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 648 | |
| 649 | using namespace SkSL::dsl; |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 650 | DSLGlobalVar loopIdx("sk_InvocationID"); |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 651 | std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1, |
| 652 | "EndPrimitive"); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 653 | SkASSERT(endPrimitive); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 654 | |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 655 | std::unique_ptr<Statement> block = DSLBlock( |
| 656 | For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock( |
| 657 | DSLFunction(invokeDecl)(), |
| 658 | DSLExpression(std::move(endPrimitive))({}) |
| 659 | )) |
| 660 | ).release(); |
| 661 | return std::unique_ptr<Block>(&block.release()->as<Block>()); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 662 | } |
| 663 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 664 | std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() { |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 665 | using namespace SkSL::dsl; |
| 666 | using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle |
| 667 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 668 | const Variable* skPerVertex = nullptr; |
| 669 | if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 670 | SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>()); |
| 671 | skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 672 | } |
| 673 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 674 | SkASSERT(skPerVertex && fRTAdjust); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 675 | auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> { |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 676 | return VariableReference::Make(/*offset=*/-1, var); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 677 | }; |
| 678 | auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> { |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 679 | return FieldAccess::Make(fContext, Ref(var), idx, |
| 680 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 681 | }; |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 682 | auto Pos = [&]() -> DSLExpression { |
| 683 | return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0, |
| 684 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock)); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 685 | }; |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 686 | auto Adjust = [&]() -> DSLExpression { |
| 687 | return DSLExpression(fRTAdjustInterfaceBlock |
| 688 | ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) |
| 689 | : Ref(fRTAdjust)); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 690 | }; |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 691 | |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 692 | return DSLStatement( |
| 693 | Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) + |
| 694 | Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W), |
| 695 | 0, |
| 696 | Pos().w()) |
| 697 | ).release(); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 700 | void IRGenerator::CheckModifiers(const Context& context, |
| 701 | int offset, |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 702 | const Modifiers& modifiers, |
| 703 | int permittedModifierFlags, |
| 704 | int permittedLayoutFlags) { |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 705 | static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = { |
| 706 | { Modifiers::kConst_Flag, "const" }, |
| 707 | { Modifiers::kIn_Flag, "in" }, |
| 708 | { Modifiers::kOut_Flag, "out" }, |
| 709 | { Modifiers::kUniform_Flag, "uniform" }, |
| 710 | { Modifiers::kFlat_Flag, "flat" }, |
| 711 | { Modifiers::kNoPerspective_Flag, "noperspective" }, |
| 712 | { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" }, |
| 713 | { Modifiers::kInline_Flag, "inline" }, |
| 714 | { Modifiers::kNoInline_Flag, "noinline" }, |
| 715 | { Modifiers::kHighp_Flag, "highp" }, |
| 716 | { Modifiers::kMediump_Flag, "mediump" }, |
| 717 | { Modifiers::kLowp_Flag, "lowp" }, |
John Stiles | efde90d | 2021-08-12 23:06:24 -0400 | [diff] [blame] | 718 | { Modifiers::kES3_Flag, "$es3" }, |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 719 | }; |
| 720 | |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 721 | int modifierFlags = modifiers.fFlags; |
| 722 | for (const auto& f : kModifierFlags) { |
| 723 | if (modifierFlags & f.flag) { |
| 724 | if (!(permittedModifierFlags & f.flag)) { |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame^] | 725 | context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here"); |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 726 | } |
| 727 | modifierFlags &= ~f.flag; |
| 728 | } |
| 729 | } |
| 730 | SkASSERT(modifierFlags == 0); |
| 731 | |
| 732 | static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = { |
| 733 | { Layout::kOriginUpperLeft_Flag, "origin_upper_left"}, |
| 734 | { Layout::kPushConstant_Flag, "push_constant"}, |
| 735 | { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"}, |
| 736 | { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"}, |
| 737 | { Layout::kLocation_Flag, "location"}, |
| 738 | { Layout::kOffset_Flag, "offset"}, |
| 739 | { Layout::kBinding_Flag, "binding"}, |
| 740 | { Layout::kIndex_Flag, "index"}, |
| 741 | { Layout::kSet_Flag, "set"}, |
| 742 | { Layout::kBuiltin_Flag, "builtin"}, |
| 743 | { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"}, |
| 744 | { Layout::kPrimitive_Flag, "primitive-type"}, |
| 745 | { Layout::kMaxVertices_Flag, "max_vertices"}, |
| 746 | { Layout::kInvocations_Flag, "invocations"}, |
| 747 | }; |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 748 | |
| 749 | int layoutFlags = modifiers.fLayout.fFlags; |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 750 | for (const auto& lf : kLayoutFlags) { |
| 751 | if (layoutFlags & lf.flag) { |
| 752 | if (!(permittedLayoutFlags & lf.flag)) { |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame^] | 753 | context.fErrors->error( |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 754 | offset, "layout qualifier '" + String(lf.name) + "' is not permitted here"); |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 755 | } |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 756 | layoutFlags &= ~lf.flag; |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 757 | } |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 758 | } |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 759 | SkASSERT(layoutFlags == 0); |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 760 | } |
| 761 | |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 762 | std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl, |
| 763 | std::unique_ptr<Block> body) { |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 764 | class Finalizer : public ProgramWriter { |
| 765 | public: |
| 766 | Finalizer(IRGenerator* irGenerator, const FunctionDeclaration* function) |
| 767 | : fIRGenerator(irGenerator) |
| 768 | , fFunction(function) {} |
| 769 | |
| 770 | ~Finalizer() override { |
| 771 | SkASSERT(!fBreakableLevel); |
| 772 | SkASSERT(!fContinuableLevel); |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | bool functionReturnsValue() const { |
John Stiles | 2558c46 | 2021-03-16 17:49:20 -0400 | [diff] [blame] | 776 | return !fFunction->returnType().isVoid(); |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 777 | } |
| 778 | |
John Stiles | 441c1bc | 2021-03-19 15:55:36 -0400 | [diff] [blame] | 779 | bool visitExpression(Expression& expr) override { |
| 780 | // Do not recurse into expressions. |
| 781 | return false; |
| 782 | } |
| 783 | |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 784 | bool visitStatement(Statement& stmt) override { |
| 785 | switch (stmt.kind()) { |
| 786 | case Statement::Kind::kReturn: { |
| 787 | // early returns from a vertex main function will bypass the sk_Position |
| 788 | // normalization, so SkASSERT that we aren't doing that. It is of course |
| 789 | // possible to fix this by adding a normalization before each return, but it |
| 790 | // will probably never actually be necessary. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 791 | SkASSERT(fIRGenerator->programKind() != ProgramKind::kVertex || |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 792 | !fIRGenerator->fRTAdjust || |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 793 | !fFunction->isMain()); |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 794 | |
| 795 | // Verify that the return statement matches the function's return type. |
| 796 | ReturnStatement& returnStmt = stmt.as<ReturnStatement>(); |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 797 | const Type& returnType = fFunction->returnType(); |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 798 | if (returnStmt.expression()) { |
| 799 | if (this->functionReturnsValue()) { |
| 800 | // Coerce return expression to the function's return type. |
| 801 | returnStmt.setExpression(fIRGenerator->coerce( |
| 802 | std::move(returnStmt.expression()), returnType)); |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 803 | } else { |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 804 | // Returning something from a function with a void return type. |
| 805 | fIRGenerator->errorReporter().error(returnStmt.fOffset, |
| 806 | "may not return a value from a void function"); |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 807 | } |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 808 | } else { |
| 809 | if (this->functionReturnsValue()) { |
| 810 | // Returning nothing from a function with a non-void return type. |
| 811 | fIRGenerator->errorReporter().error(returnStmt.fOffset, |
| 812 | "expected function to return '" + returnType.displayName() + "'"); |
| 813 | } |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 814 | } |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | case Statement::Kind::kDo: |
| 818 | case Statement::Kind::kFor: { |
| 819 | ++fBreakableLevel; |
| 820 | ++fContinuableLevel; |
| 821 | bool result = INHERITED::visitStatement(stmt); |
| 822 | --fContinuableLevel; |
| 823 | --fBreakableLevel; |
| 824 | return result; |
| 825 | } |
| 826 | case Statement::Kind::kSwitch: { |
| 827 | ++fBreakableLevel; |
| 828 | bool result = INHERITED::visitStatement(stmt); |
| 829 | --fBreakableLevel; |
| 830 | return result; |
| 831 | } |
| 832 | case Statement::Kind::kBreak: |
| 833 | if (!fBreakableLevel) { |
| 834 | fIRGenerator->errorReporter().error(stmt.fOffset, |
| 835 | "break statement must be inside a loop or switch"); |
| 836 | } |
| 837 | break; |
| 838 | case Statement::Kind::kContinue: |
| 839 | if (!fContinuableLevel) { |
| 840 | fIRGenerator->errorReporter().error(stmt.fOffset, |
| 841 | "continue statement must be inside a loop"); |
| 842 | } |
| 843 | break; |
| 844 | default: |
| 845 | break; |
| 846 | } |
| 847 | return INHERITED::visitStatement(stmt); |
| 848 | } |
| 849 | |
| 850 | private: |
| 851 | IRGenerator* fIRGenerator; |
| 852 | const FunctionDeclaration* fFunction; |
| 853 | // how deeply nested we are in breakable constructs (for, do, switch). |
| 854 | int fBreakableLevel = 0; |
| 855 | // how deeply nested we are in continuable constructs (for, do). |
| 856 | int fContinuableLevel = 0; |
| 857 | |
| 858 | using INHERITED = ProgramWriter; |
| 859 | }; |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 860 | |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 861 | bool isMain = funcDecl.isMain(); |
| 862 | bool needInvocationIDWorkaround = fInvocations != -1 && isMain && |
| 863 | !this->caps().gsInvocationsSupport(); |
| 864 | if (needInvocationIDWorkaround) { |
| 865 | body = this->applyInvocationIDWorkaround(std::move(body)); |
| 866 | } |
| 867 | if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) { |
| 868 | body->children().push_back(this->getNormalizeSkPositionCode()); |
| 869 | } |
| 870 | |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 871 | Finalizer finalizer{this, &funcDecl}; |
| 872 | finalizer.visitStatement(*body); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 873 | |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 874 | if (Analysis::CanExitWithoutReturningValue(funcDecl, *body)) { |
| 875 | this->errorReporter().error(funcDecl.fOffset, "function '" + funcDecl.name() + |
| 876 | "' can exit without returning a value"); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 877 | } |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 878 | return body; |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 881 | void IRGenerator::convertFunction(const ASTNode& f) { |
John Stiles | b2705a1 | 2021-02-19 10:55:14 -0500 | [diff] [blame] | 882 | SkASSERT(fReferencedIntrinsics.empty()); |
| 883 | SK_AT_SCOPE_EXIT(fReferencedIntrinsics.clear()); |
| 884 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 885 | auto iter = f.begin(); |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 886 | const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 887 | if (returnType == nullptr) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 888 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 889 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 890 | const ASTNode::FunctionData& funcData = f.getFunctionData(); |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 891 | std::vector<std::unique_ptr<Variable>> parameters; |
| 892 | parameters.reserve(funcData.fParameterCount); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 893 | for (size_t i = 0; i < funcData.fParameterCount; ++i) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 894 | const ASTNode& param = *(iter++); |
| 895 | SkASSERT(param.fKind == ASTNode::Kind::kParameter); |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 896 | const ASTNode::ParameterData& pd = param.getParameterData(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 897 | auto paramIter = param.begin(); |
| 898 | const Type* type = this->convertType(*(paramIter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 899 | if (!type) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 900 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 901 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 902 | if (pd.fIsArray) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 903 | int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 904 | if (!arraySize) { |
| 905 | return; |
| 906 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 907 | type = fSymbolTable->addArrayDimension(type, arraySize); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 908 | } |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 909 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 910 | parameters.push_back(std::make_unique<Variable>(param.fOffset, |
| 911 | this->modifiersPool().add(pd.fModifiers), |
| 912 | pd.fName, |
| 913 | type, |
| 914 | fIsBuiltinCode, |
| 915 | Variable::Storage::kParameter)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 918 | // Conservatively assume all user-defined functions have side effects. |
| 919 | Modifiers declModifiers = funcData.fModifiers; |
| 920 | if (!fIsBuiltinCode) { |
| 921 | declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 922 | } |
| 923 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 924 | if (fContext.fConfig->fSettings.fForceNoInline) { |
| 925 | // Apply the `noinline` modifier to every function. This allows us to test Runtime |
| 926 | // Effects without any inlining, even when the code is later added to a paint. |
| 927 | declModifiers.fFlags &= ~Modifiers::kInline_Flag; |
| 928 | declModifiers.fFlags |= Modifiers::kNoInline_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 929 | } |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 930 | |
| 931 | const FunctionDeclaration* decl = FunctionDeclaration::Convert( |
| 932 | fContext, |
| 933 | *fSymbolTable, |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 934 | f.fOffset, |
| 935 | this->modifiersPool().add(declModifiers), |
| 936 | funcData.fName, |
| 937 | std::move(parameters), |
| 938 | returnType, |
| 939 | fIsBuiltinCode); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 940 | if (!decl) { |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 941 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 942 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 943 | if (iter == f.end()) { |
| 944 | // If there's no body, we've found a prototype. |
| 945 | fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl, |
| 946 | fIsBuiltinCode)); |
| 947 | } else { |
| 948 | // Compile function body. |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 949 | AutoSymbolTable table(this); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 950 | for (const Variable* param : decl->parameters()) { |
| 951 | fSymbolTable->addWithoutOwnership(param); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 952 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 953 | std::unique_ptr<Block> body = this->convertBlock(*iter); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 954 | if (!body) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 955 | return; |
| 956 | } |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 957 | body = this->finalizeFunction(*decl, std::move(body)); |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 958 | auto result = std::make_unique<FunctionDefinition>( |
| 959 | f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics)); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 960 | decl->setDefinition(result.get()); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 961 | result->setSource(&f); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 962 | fProgramElements->push_back(std::move(result)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 963 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 964 | } |
| 965 | |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 966 | std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) { |
| 967 | SkASSERT(node.fKind == ASTNode::Kind::kType); |
| 968 | |
| 969 | const Type* type = this->convertType(node); |
| 970 | if (!type) { |
| 971 | return nullptr; |
| 972 | } |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 973 | if (!type->isStruct()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 974 | this->errorReporter().error(node.fOffset, |
| 975 | "expected a struct here, found '" + type->name() + "'"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 976 | return nullptr; |
| 977 | } |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 978 | SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type); |
| 979 | SkASSERT(wasInserted); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 980 | return std::make_unique<StructDefinition>(node.fOffset, *type); |
| 981 | } |
| 982 | |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 983 | std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 984 | if (this->programKind() != ProgramKind::kFragment && |
| 985 | this->programKind() != ProgramKind::kVertex && |
| 986 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 987 | this->errorReporter().error(intf.fOffset, "interface block is not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 988 | return nullptr; |
| 989 | } |
| 990 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 991 | SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock); |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 992 | const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 994 | std::shared_ptr<SymbolTable> symbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 995 | std::vector<Type::Field> fields; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 996 | bool foundRTAdjust = false; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 997 | auto iter = intf.begin(); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 998 | { |
| 999 | AutoSymbolTable table(this); |
| 1000 | symbols = fSymbolTable; |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1001 | for (size_t i = 0; i < id.fDeclarationCount; ++i) { |
| 1002 | StatementArray decls = this->convertVarDeclarations(*(iter++), |
| 1003 | Variable::Storage::kInterfaceBlock); |
| 1004 | if (decls.empty()) { |
| 1005 | return nullptr; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 1006 | } |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1007 | for (const auto& decl : decls) { |
| 1008 | const VarDeclaration& vd = decl->as<VarDeclaration>(); |
John Stiles | 1d75778 | 2020-11-17 09:43:22 -0500 | [diff] [blame] | 1009 | if (vd.var().type().isOpaque()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1010 | this->errorReporter().error(decl->fOffset, |
| 1011 | "opaque type '" + vd.var().type().name() + |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1012 | "' is not permitted in an interface block"); |
John Stiles | 1d75778 | 2020-11-17 09:43:22 -0500 | [diff] [blame] | 1013 | } |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1014 | if (&vd.var() == fRTAdjust) { |
| 1015 | foundRTAdjust = true; |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1016 | SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1017 | fRTAdjustFieldIndex = fields.size(); |
| 1018 | } |
| 1019 | fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(), |
| 1020 | &vd.var().type())); |
| 1021 | if (vd.value()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1022 | this->errorReporter().error( |
| 1023 | decl->fOffset, |
| 1024 | "initializers are not permitted on interface block fields"); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1025 | } |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 1026 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1027 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1028 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1029 | const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset, |
Ethan Nicholas | 27f06eb | 2021-07-26 16:39:40 -0400 | [diff] [blame] | 1030 | id.fTypeName, |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 1031 | fields)); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1032 | int arraySize = 0; |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 1033 | if (id.fIsArray) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1034 | const ASTNode& size = *(iter++); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1035 | if (size) { |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1036 | // convertArraySize rejects unsized arrays. This is the one place we allow those, but |
| 1037 | // we've already checked for that, so this is verifying the other aspects (constant, |
| 1038 | // positive, not too large). |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 1039 | arraySize = this->convertArraySize(*type, size.fOffset, size); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1040 | if (!arraySize) { |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1041 | return nullptr; |
| 1042 | } |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1043 | } else { |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1044 | arraySize = Type::kUnsizedArray; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1045 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 1046 | type = symbols->addArrayDimension(type, arraySize); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1047 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 1048 | const Variable* var = old->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1049 | std::make_unique<Variable>(intf.fOffset, |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1050 | this->modifiersPool().add(id.fModifiers), |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1051 | id.fInstanceName.length() ? id.fInstanceName : id.fTypeName, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1052 | type, |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 1053 | fIsBuiltinCode, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1054 | Variable::Storage::kGlobal)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1055 | if (foundRTAdjust) { |
| 1056 | fRTAdjustInterfaceBlock = var; |
| 1057 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1058 | if (id.fInstanceName.length()) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 1059 | old->addWithoutOwnership(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | for (size_t i = 0; i < fields.size(); i++) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 1062 | old->add(std::make_unique<Field>(intf.fOffset, var, (int)i)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1063 | } |
| 1064 | } |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1065 | return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset, |
| 1066 | var, |
Ethan Nicholas | 3533ff1 | 2021-08-02 12:53:29 -0400 | [diff] [blame] | 1067 | id.fTypeName, |
| 1068 | id.fInstanceName, |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1069 | arraySize, |
| 1070 | symbols); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1073 | void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) { |
| 1074 | StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal); |
| 1075 | for (std::unique_ptr<Statement>& stmt : decls) { |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 1076 | const Type* type = &stmt->as<VarDeclaration>().baseType(); |
| 1077 | if (type->isStruct()) { |
| 1078 | auto [iter, wasInserted] = fDefinedStructs.insert(type); |
| 1079 | if (wasInserted) { |
| 1080 | fProgramElements->push_back( |
| 1081 | std::make_unique<StructDefinition>(decl.fOffset, *type)); |
| 1082 | } |
| 1083 | } |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1084 | fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt))); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 1088 | static bool type_contains_private_fields(const Type& type) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 1089 | // Checks for usage of private types, including fields inside a struct. |
| 1090 | if (type.isPrivate()) { |
| 1091 | return true; |
| 1092 | } |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 1093 | if (type.isStruct()) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 1094 | for (const auto& f : type.fields()) { |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 1095 | if (type_contains_private_fields(*f.fType)) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 1103 | const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1104 | skstd::string_view name = type.getStringView(); |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 1105 | const Symbol* symbol = (*fSymbolTable)[name]; |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1106 | if (!symbol || !symbol->is<Type>()) { |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 1107 | this->errorReporter().error(type.fOffset, "unknown type '" + name + "'"); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1108 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1109 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1110 | const Type* result = &symbol->as<Type>(); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1111 | const bool isArray = (type.begin() != type.end()); |
John Stiles | 2558c46 | 2021-03-16 17:49:20 -0400 | [diff] [blame] | 1112 | if (result->isVoid() && !allowVoid) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 1113 | this->errorReporter().error(type.fOffset, |
| 1114 | "type '" + name + "' not allowed in this context"); |
| 1115 | return nullptr; |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1116 | } |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 1117 | if (!fIsBuiltinCode) { |
| 1118 | if (type_contains_private_fields(*result)) { |
| 1119 | this->errorReporter().error(type.fOffset, "type '" + name + "' is private"); |
| 1120 | return nullptr; |
| 1121 | } |
| 1122 | if (this->strictES2Mode() && !result->allowedInES2()) { |
| 1123 | this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported"); |
| 1124 | return nullptr; |
| 1125 | } |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1126 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1127 | if (isArray) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 1128 | auto iter = type.begin(); |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 1129 | int arraySize = this->convertArraySize(*result, type.fOffset, *iter); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1130 | if (!arraySize) { |
| 1131 | return nullptr; |
| 1132 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 1133 | result = fSymbolTable->addArrayDimension(result, arraySize); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1134 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1135 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1138 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1139 | switch (expr.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1140 | case ASTNode::Kind::kBinary: |
| 1141 | return this->convertBinaryExpression(expr); |
| 1142 | case ASTNode::Kind::kBool: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1143 | return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1144 | case ASTNode::Kind::kCall: |
| 1145 | return this->convertCallExpression(expr); |
| 1146 | case ASTNode::Kind::kField: |
| 1147 | return this->convertFieldExpression(expr); |
| 1148 | case ASTNode::Kind::kFloat: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1149 | return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1150 | case ASTNode::Kind::kIdentifier: |
| 1151 | return this->convertIdentifier(expr); |
| 1152 | case ASTNode::Kind::kIndex: |
| 1153 | return this->convertIndexExpression(expr); |
| 1154 | case ASTNode::Kind::kInt: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1155 | return IntLiteral::Make(fContext, expr.fOffset, expr.getInt()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1156 | case ASTNode::Kind::kPostfix: |
| 1157 | return this->convertPostfixExpression(expr); |
| 1158 | case ASTNode::Kind::kPrefix: |
| 1159 | return this->convertPrefixExpression(expr); |
| 1160 | case ASTNode::Kind::kTernary: |
| 1161 | return this->convertTernaryExpression(expr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1162 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 1163 | SkDEBUGFAILF("unsupported expression: %s\n", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1164 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1168 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) { |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1169 | const Symbol* result = (*fSymbolTable)[name]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1170 | if (!result) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1171 | this->errorReporter().error(offset, "unknown identifier '" + name + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1172 | return nullptr; |
| 1173 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1174 | switch (result->kind()) { |
| 1175 | case Symbol::Kind::kFunctionDeclaration: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1176 | std::vector<const FunctionDeclaration*> f = { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1177 | &result->as<FunctionDeclaration>() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1178 | }; |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1179 | return std::make_unique<FunctionReference>(fContext, offset, f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1180 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1181 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1182 | const UnresolvedFunction* f = &result->as<UnresolvedFunction>(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1183 | return std::make_unique<FunctionReference>(fContext, offset, f->functions()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1184 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1185 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1186 | const Variable* var = &result->as<Variable>(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1187 | const Modifiers& modifiers = var->modifiers(); |
| 1188 | switch (modifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1189 | case SK_FRAGCOORD_BUILTIN: |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1190 | if (caps().canUseFragCoord()) { |
| 1191 | fInputs.fUseFlipRTUniform = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1192 | } |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1193 | break; |
| 1194 | case SK_CLOCKWISE_BUILTIN: |
| 1195 | fInputs.fUseFlipRTUniform = true; |
| 1196 | break; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1197 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1198 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
John Stiles | a94e026 | 2021-04-27 17:29:59 -0400 | [diff] [blame] | 1199 | return VariableReference::Make(offset, var, VariableReference::RefKind::kRead); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1200 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1201 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1202 | const Field* field = &result->as<Field>(); |
John Stiles | a94e026 | 2021-04-27 17:29:59 -0400 | [diff] [blame] | 1203 | auto base = VariableReference::Make(offset, &field->owner(), |
| 1204 | VariableReference::RefKind::kRead); |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1205 | return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(), |
| 1206 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1207 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1208 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1209 | const Type* t = &result->as<Type>(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1210 | return std::make_unique<TypeReference>(fContext, offset, t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1211 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1212 | case Symbol::Kind::kExternal: { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1213 | const ExternalFunction* r = &result->as<ExternalFunction>(); |
| 1214 | return std::make_unique<ExternalFunctionReference>(offset, r); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1215 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1216 | default: |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 1217 | SK_ABORT("unsupported symbol type %d\n", (int) result->kind()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1218 | } |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1221 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1222 | return this->convertIdentifier(identifier.fOffset, identifier.getStringView()); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1225 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1226 | const Type& type) { |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1227 | return type.coerceExpression(std::move(expr), fContext); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1230 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) { |
| 1231 | SkASSERT(expression.fKind == ASTNode::Kind::kBinary); |
| 1232 | auto iter = expression.begin(); |
| 1233 | std::unique_ptr<Expression> left = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1234 | if (!left) { |
| 1235 | return nullptr; |
| 1236 | } |
John Stiles | 0f46450 | 2020-11-20 12:52:22 -0500 | [diff] [blame] | 1237 | std::unique_ptr<Expression> right = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1238 | if (!right) { |
| 1239 | return nullptr; |
| 1240 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1241 | return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(), |
| 1242 | std::move(right)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1245 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) { |
| 1246 | SkASSERT(node.fKind == ASTNode::Kind::kTernary); |
| 1247 | auto iter = node.begin(); |
| 1248 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
| 1249 | if (!test) { |
| 1250 | return nullptr; |
| 1251 | } |
| 1252 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++)); |
| 1253 | if (!ifTrue) { |
| 1254 | return nullptr; |
| 1255 | } |
| 1256 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++)); |
| 1257 | if (!ifFalse) { |
| 1258 | return nullptr; |
| 1259 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1260 | return TernaryExpression::Convert(fContext, std::move(test), |
| 1261 | std::move(ifTrue), std::move(ifFalse)); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1262 | } |
| 1263 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1264 | void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1265 | if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) { |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 1266 | const FunctionDefinition& original = found->as<FunctionDefinition>(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1267 | |
| 1268 | // Sort the referenced intrinsics into a consistent order; otherwise our output will become |
| 1269 | // non-deterministic. |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1270 | std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(), |
| 1271 | original.referencedIntrinsics().end()); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1272 | std::sort(intrinsics.begin(), intrinsics.end(), |
| 1273 | [](const FunctionDeclaration* a, const FunctionDeclaration* b) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1274 | if (a->isBuiltin() != b->isBuiltin()) { |
| 1275 | return a->isBuiltin() < b->isBuiltin(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1276 | } |
| 1277 | if (a->fOffset != b->fOffset) { |
| 1278 | return a->fOffset < b->fOffset; |
| 1279 | } |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1280 | if (a->name() != b->name()) { |
| 1281 | return a->name() < b->name(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1282 | } |
| 1283 | return a->description() < b->description(); |
| 1284 | }); |
| 1285 | for (const FunctionDeclaration* f : intrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1286 | this->copyIntrinsicIfNeeded(*f); |
| 1287 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 1288 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1289 | fSharedElements->push_back(found); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1293 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1294 | const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1295 | ExpressionArray arguments) { |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 1296 | if (function.intrinsicKind() == k_sample_IntrinsicKind && arguments.size() >= 1 && |
| 1297 | arguments[0]->type().isEffectChild()) { |
| 1298 | // Translate old-style sample(child, ...) calls into new-style child(...) IR |
| 1299 | SkASSERT(arguments[0]->is<VariableReference>()); |
| 1300 | const Variable& child = *arguments[0]->as<VariableReference>().variable(); |
| 1301 | ExpressionArray argumentsWithoutChild; |
| 1302 | for (size_t i = 1; i < arguments.size(); i++) { |
| 1303 | argumentsWithoutChild.push_back(std::move(arguments[i])); |
| 1304 | } |
| 1305 | return ChildCall::Convert(fContext, offset, child, std::move(argumentsWithoutChild)); |
| 1306 | } |
| 1307 | |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1308 | if (function.isBuiltin()) { |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1309 | if (function.intrinsicKind() == k_dFdy_IntrinsicKind) { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1310 | fInputs.fUseFlipRTUniform = true; |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1311 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1312 | if (function.definition()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1313 | fReferencedIntrinsics.insert(&function); |
| 1314 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1315 | if (!fIsBuiltinCode && fIntrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1316 | this->copyIntrinsicIfNeeded(function); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1317 | } |
| 1318 | } |
John Stiles | ea9ab82 | 2020-08-31 09:55:04 -0400 | [diff] [blame] | 1319 | |
John Stiles | cd7ba50 | 2021-03-19 10:54:59 -0400 | [diff] [blame] | 1320 | return FunctionCall::Convert(fContext, offset, function, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /** |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1324 | * Determines the cost of coercing the arguments of a function to the required types. Cost has no |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1325 | * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if |
| 1326 | * the call is not valid. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1327 | */ |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1328 | CoercionCost IRGenerator::callCost(const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1329 | const ExpressionArray& arguments) { |
John Stiles | efde90d | 2021-08-12 23:06:24 -0400 | [diff] [blame] | 1330 | if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) { |
| 1331 | return CoercionCost::Impossible(); |
| 1332 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1333 | if (function.parameters().size() != arguments.size()) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1334 | return CoercionCost::Impossible(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1335 | } |
John Stiles | fa88911 | 2020-10-12 19:03:43 -0400 | [diff] [blame] | 1336 | FunctionDeclaration::ParamTypes types; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1337 | const Type* ignored; |
| 1338 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1339 | return CoercionCost::Impossible(); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1340 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1341 | CoercionCost total = CoercionCost::Free(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1342 | for (size_t i = 0; i < arguments.size(); i++) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1343 | total = total + arguments[i]->coercionCost(*types[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1344 | } |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1345 | return total; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1348 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1349 | std::unique_ptr<Expression> functionValue, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1350 | ExpressionArray arguments) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1351 | switch (functionValue->kind()) { |
| 1352 | case Expression::Kind::kTypeReference: |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1353 | return Constructor::Convert(fContext, |
| 1354 | offset, |
| 1355 | functionValue->as<TypeReference>().value(), |
| 1356 | std::move(arguments)); |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1357 | case Expression::Kind::kExternalFunctionReference: { |
| 1358 | const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function(); |
| 1359 | int count = f.callParameterCount(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1360 | if (count != (int) arguments.size()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1361 | this->errorReporter().error(offset, "external function expected " + |
| 1362 | to_string(count) + " arguments, but found " + |
| 1363 | to_string((int)arguments.size())); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1364 | return nullptr; |
| 1365 | } |
| 1366 | static constexpr int PARAMETER_MAX = 16; |
| 1367 | SkASSERT(count < PARAMETER_MAX); |
| 1368 | const Type* types[PARAMETER_MAX]; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1369 | f.getCallParameterTypes(types); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1370 | for (int i = 0; i < count; ++i) { |
| 1371 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
| 1372 | if (!arguments[i]) { |
| 1373 | return nullptr; |
| 1374 | } |
| 1375 | } |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1376 | return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1377 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1378 | case Expression::Kind::kFunctionReference: { |
John Stiles | ce591b7 | 2020-08-27 11:47:30 -0400 | [diff] [blame] | 1379 | const FunctionReference& ref = functionValue->as<FunctionReference>(); |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1380 | const std::vector<const FunctionDeclaration*>& functions = ref.functions(); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1381 | CoercionCost bestCost = CoercionCost::Impossible(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1382 | const FunctionDeclaration* best = nullptr; |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1383 | if (functions.size() > 1) { |
| 1384 | for (const auto& f : functions) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1385 | CoercionCost cost = this->callCost(*f, arguments); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1386 | if (cost < bestCost) { |
| 1387 | bestCost = cost; |
| 1388 | best = f; |
| 1389 | } |
| 1390 | } |
| 1391 | if (best) { |
| 1392 | return this->call(offset, *best, std::move(arguments)); |
| 1393 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1394 | String msg = "no match for " + functions[0]->name() + "("; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1395 | String separator; |
| 1396 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1397 | msg += separator; |
| 1398 | separator = ", "; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1399 | msg += arguments[i]->type().displayName(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1400 | } |
| 1401 | msg += ")"; |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1402 | this->errorReporter().error(offset, msg); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1403 | return nullptr; |
| 1404 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1405 | return this->call(offset, *functions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1406 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 1407 | case Expression::Kind::kVariableReference: { |
| 1408 | if (!functionValue->type().isEffectChild()) { |
| 1409 | this->errorReporter().error(offset, "not a function"); |
| 1410 | return nullptr; |
| 1411 | } |
| 1412 | return ChildCall::Convert(fContext, |
| 1413 | offset, |
| 1414 | *functionValue->as<VariableReference>().variable(), |
| 1415 | std::move(arguments)); |
| 1416 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1417 | default: |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1418 | this->errorReporter().error(offset, "not a function"); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1419 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1420 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1423 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) { |
| 1424 | SkASSERT(expression.fKind == ASTNode::Kind::kPrefix); |
| 1425 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1426 | if (!base) { |
| 1427 | return nullptr; |
| 1428 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1429 | return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1432 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1433 | skstd::string_view fields) { |
Ethan Nicholas | 59ff4e2 | 2021-07-12 12:28:57 -0400 | [diff] [blame] | 1434 | return Swizzle::Convert(fContext, std::move(base), fields); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1437 | std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) { |
| 1438 | SkASSERT(index.fKind == ASTNode::Kind::kIndex); |
| 1439 | auto iter = index.begin(); |
| 1440 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1441 | if (!base) { |
| 1442 | return nullptr; |
| 1443 | } |
Ethan Nicholas | 4d2bbbb | 2021-01-12 11:56:23 -0500 | [diff] [blame] | 1444 | if (iter == index.end()) { |
Ethan Nicholas | 251e634 | 2021-07-20 13:16:19 -0400 | [diff] [blame] | 1445 | if (base->is<TypeReference>()) { |
| 1446 | this->errorReporter().error(index.fOffset, "array must have a size"); |
| 1447 | } else { |
| 1448 | this->errorReporter().error(base->fOffset, "missing index in '[]'"); |
| 1449 | } |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1450 | return nullptr; |
Ethan Nicholas | 4d2bbbb | 2021-01-12 11:56:23 -0500 | [diff] [blame] | 1451 | } |
| 1452 | std::unique_ptr<Expression> converted = this->convertExpression(*(iter++)); |
| 1453 | if (!converted) { |
| 1454 | return nullptr; |
| 1455 | } |
Ethan Nicholas | 251e634 | 2021-07-20 13:16:19 -0400 | [diff] [blame] | 1456 | return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), |
| 1457 | std::move(converted)); |
John Stiles | 1b27c3d | 2020-12-07 12:14:55 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1460 | std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) { |
| 1461 | SkASSERT(callNode.fKind == ASTNode::Kind::kCall); |
| 1462 | auto iter = callNode.begin(); |
| 1463 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
| 1464 | if (!base) { |
| 1465 | return nullptr; |
| 1466 | } |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1467 | ExpressionArray arguments; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1468 | for (; iter != callNode.end(); ++iter) { |
| 1469 | std::unique_ptr<Expression> converted = this->convertExpression(*iter); |
| 1470 | if (!converted) { |
| 1471 | return nullptr; |
| 1472 | } |
| 1473 | arguments.push_back(std::move(converted)); |
| 1474 | } |
| 1475 | return this->call(callNode.fOffset, std::move(base), std::move(arguments)); |
| 1476 | } |
| 1477 | |
| 1478 | std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) { |
| 1479 | std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin()); |
| 1480 | if (!base) { |
| 1481 | return nullptr; |
| 1482 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1483 | const skstd::string_view& field = fieldNode.getStringView(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1484 | const Type& baseType = base->type(); |
Ethan Nicholas | 5c75e83 | 2021-06-10 18:28:19 -0400 | [diff] [blame] | 1485 | if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) { |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1486 | return FieldAccess::Convert(fContext, std::move(base), field); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1487 | } |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1488 | return this->convertSwizzle(std::move(base), field); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) { |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1492 | SkASSERT(expression.fKind == ASTNode::Kind::kPostfix); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1493 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
| 1494 | if (!base) { |
| 1495 | return nullptr; |
| 1496 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1497 | return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | void IRGenerator::checkValid(const Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1501 | switch (expr.kind()) { |
John Stiles | dc435fa | 2020-12-30 18:07:10 -0500 | [diff] [blame] | 1502 | case Expression::Kind::kFunctionCall: { |
| 1503 | const FunctionDeclaration& decl = expr.as<FunctionCall>().function(); |
| 1504 | if (!decl.isBuiltin() && !decl.definition()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1505 | this->errorReporter().error(expr.fOffset, |
| 1506 | "function '" + decl.description() + "' is not defined"); |
John Stiles | dc435fa | 2020-12-30 18:07:10 -0500 | [diff] [blame] | 1507 | } |
| 1508 | break; |
| 1509 | } |
John Stiles | 973009b | 2021-02-17 17:18:01 -0500 | [diff] [blame] | 1510 | case Expression::Kind::kFunctionReference: |
| 1511 | case Expression::Kind::kTypeReference: |
| 1512 | SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()"); |
| 1513 | this->errorReporter().error(expr.fOffset, "invalid expression"); |
| 1514 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1515 | default: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1516 | if (expr.type() == *fContext.fTypes.fInvalid) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1517 | this->errorReporter().error(expr.fOffset, "invalid expression"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1518 | } |
John Stiles | 973009b | 2021-02-17 17:18:01 -0500 | [diff] [blame] | 1519 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1523 | void IRGenerator::findAndDeclareBuiltinVariables() { |
| 1524 | class BuiltinVariableScanner : public ProgramVisitor { |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1525 | public: |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1526 | BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {} |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1527 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1528 | void addDeclaringElement(const String& name) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1529 | // If this is the *first* time we've seen this builtin, findAndInclude will return |
| 1530 | // the corresponding ProgramElement. |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1531 | if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1532 | SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>()); |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1533 | fNewElements.push_back(decl); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1537 | bool visitProgramElement(const ProgramElement& pe) override { |
| 1538 | if (pe.is<FunctionDefinition>()) { |
| 1539 | const FunctionDefinition& funcDef = pe.as<FunctionDefinition>(); |
| 1540 | // We synthesize writes to sk_FragColor if main() returns a color, even if it's |
| 1541 | // otherwise unreferenced. Check main's return type to see if it's half4. |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1542 | if (funcDef.declaration().isMain() && |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1543 | funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) { |
| 1544 | fPreserveFragColor = true; |
| 1545 | } |
| 1546 | } |
| 1547 | return INHERITED::visitProgramElement(pe); |
| 1548 | } |
| 1549 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1550 | bool visitExpression(const Expression& e) override { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1551 | if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1552 | this->addDeclaringElement(String(e.as<VariableReference>().variable()->name())); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1553 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1554 | return INHERITED::visitExpression(e); |
| 1555 | } |
| 1556 | |
| 1557 | IRGenerator* fGenerator; |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1558 | std::vector<const ProgramElement*> fNewElements; |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1559 | bool fPreserveFragColor = false; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1560 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1561 | using INHERITED = ProgramVisitor; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1562 | using INHERITED::visitProgramElement; |
| 1563 | }; |
| 1564 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1565 | BuiltinVariableScanner scanner(this); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1566 | SkASSERT(fProgramElements); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1567 | for (auto& e : *fProgramElements) { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1568 | scanner.visitProgramElement(*e); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1569 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1570 | |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1571 | if (scanner.fPreserveFragColor) { |
| 1572 | // main() returns a half4, so make sure we don't dead-strip sk_FragColor. |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 1573 | scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME); |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1574 | } |
| 1575 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1576 | switch (this->programKind()) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1577 | case ProgramKind::kFragment: |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1578 | // Vulkan requires certain builtin variables be present, even if they're unused. At one |
| 1579 | // time, validation errors would result if sk_Clockwise was missing. Now, it's just |
| 1580 | // (Adreno) driver bugs that drop or corrupt draws if they're missing. |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1581 | scanner.addDeclaringElement("sk_Clockwise"); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1582 | break; |
| 1583 | default: |
| 1584 | break; |
| 1585 | } |
| 1586 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1587 | fSharedElements->insert( |
| 1588 | fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1589 | } |
| 1590 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1591 | void IRGenerator::start(const ParsedModule& base, |
| 1592 | bool isBuiltinCode, |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1593 | std::vector<std::unique_ptr<ProgramElement>>* elements, |
| 1594 | std::vector<const ProgramElement*>* sharedElements) { |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1595 | fProgramElements = elements; |
| 1596 | fSharedElements = sharedElements; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1597 | fSymbolTable = base.fSymbols; |
| 1598 | fIntrinsics = base.fIntrinsics.get(); |
| 1599 | if (fIntrinsics) { |
| 1600 | fIntrinsics->resetAlreadyIncluded(); |
| 1601 | } |
| 1602 | fIsBuiltinCode = isBuiltinCode; |
| 1603 | |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1604 | fInputs = {}; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1605 | fInvocations = -1; |
| 1606 | fRTAdjust = nullptr; |
| 1607 | fRTAdjustInterfaceBlock = nullptr; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 1608 | fDefinedStructs.clear(); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1609 | this->pushSymbolTable(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1610 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1611 | if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) { |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1612 | // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin. |
| 1613 | // If we're applying the workaround, then it's a plain global. |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1614 | bool workaround = !this->caps().gsInvocationsSupport(); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1615 | Modifiers m; |
| 1616 | if (!workaround) { |
| 1617 | m.fFlags = Modifiers::kIn_Flag; |
| 1618 | m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN; |
| 1619 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1620 | auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m), |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 1621 | "sk_InvocationID", fContext.fTypes.fInt.get(), |
| 1622 | /*builtin=*/false, Variable::Storage::kGlobal); |
| 1623 | auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(), |
| 1624 | /*arraySize=*/0, /*value=*/nullptr); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1625 | fSymbolTable->add(std::move(var)); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1626 | fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl))); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1629 | if (this->settings().fExternalFunctions) { |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1630 | // Add any external values to the new symbol table, so they're only visible to this Program. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1631 | for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1632 | fSymbolTable->addWithoutOwnership(ef.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1633 | } |
| 1634 | } |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1635 | |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 1636 | if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) { |
John Stiles | 325c808 | 2021-05-17 14:49:05 -0400 | [diff] [blame] | 1637 | // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1638 | // non-ES2 types to our symbol table to allow them to be tested. |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1639 | fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get()); |
| 1640 | fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get()); |
| 1641 | fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get()); |
| 1642 | fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get()); |
| 1643 | fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get()); |
| 1644 | fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get()); |
| 1645 | fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get()); |
| 1646 | fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get()); |
| 1647 | fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get()); |
| 1648 | |
| 1649 | fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get()); |
| 1650 | fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get()); |
| 1651 | fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get()); |
| 1652 | fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get()); |
| 1653 | fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get()); |
| 1654 | fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get()); |
| 1655 | |
| 1656 | fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get()); |
| 1657 | fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get()); |
| 1658 | fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get()); |
| 1659 | fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get()); |
| 1660 | fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get()); |
| 1661 | fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get()); |
John Stiles | 325c808 | 2021-05-17 14:49:05 -0400 | [diff] [blame] | 1662 | |
| 1663 | fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get()); |
| 1664 | fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get()); |
| 1665 | fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get()); |
| 1666 | fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get()); |
John Stiles | 2648716 | 2021-08-10 16:03:44 -0400 | [diff] [blame] | 1667 | |
| 1668 | fSymbolTable->addAlias("short", fContext.fTypes.fShort.get()); |
| 1669 | fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get()); |
| 1670 | fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get()); |
| 1671 | fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get()); |
| 1672 | |
| 1673 | fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get()); |
| 1674 | fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get()); |
| 1675 | fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get()); |
| 1676 | fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get()); |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1677 | } |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1678 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1679 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1680 | IRGenerator::IRBundle IRGenerator::finish() { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1681 | // Variables defined in the pre-includes need their declaring elements added to the program |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1682 | if (!fIsBuiltinCode && fIntrinsics) { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1683 | this->findAndDeclareBuiltinVariables(); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1684 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1685 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1686 | // Do a pass looking for dangling FunctionReference or TypeReference expressions |
Brian Osman | c95d3a1 | 2020-09-09 10:56:27 -0400 | [diff] [blame] | 1687 | class FindIllegalExpressions : public ProgramVisitor { |
| 1688 | public: |
| 1689 | FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {} |
| 1690 | |
| 1691 | bool visitExpression(const Expression& e) override { |
| 1692 | fGenerator->checkValid(e); |
| 1693 | return INHERITED::visitExpression(e); |
| 1694 | } |
| 1695 | |
| 1696 | IRGenerator* fGenerator; |
| 1697 | using INHERITED = ProgramVisitor; |
| 1698 | using INHERITED::visitProgramElement; |
| 1699 | }; |
| 1700 | for (const auto& pe : *fProgramElements) { |
| 1701 | FindIllegalExpressions{this}.visitProgramElement(*pe); |
| 1702 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1703 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1704 | // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the |
| 1705 | // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic |
| 1706 | // assumes that all loops meet the criteria of Section 4, and if they don't, could crash. |
| 1707 | if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) { |
| 1708 | for (const auto& pe : *fProgramElements) { |
| 1709 | Analysis::ValidateIndexingForES2(*pe, this->errorReporter()); |
| 1710 | } |
| 1711 | } |
| 1712 | |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 1713 | if (this->strictES2Mode()) { |
| 1714 | Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter()); |
| 1715 | } |
| 1716 | |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1717 | return IRBundle{std::move(*fProgramElements), |
| 1718 | std::move(*fSharedElements), |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1719 | std::move(fSymbolTable), |
| 1720 | fInputs}; |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | IRGenerator::IRBundle IRGenerator::convertProgram( |
| 1724 | const ParsedModule& base, |
| 1725 | bool isBuiltinCode, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 1726 | skstd::string_view text) { |
| 1727 | Parser parser(text, *fSymbolTable, this->errorReporter()); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1728 | fFile = parser.compilationUnit(); |
| 1729 | if (this->errorReporter().errorCount() == 0) { |
| 1730 | SkASSERT(fFile); |
| 1731 | for (const auto& decl : fFile->root()) { |
| 1732 | switch (decl.fKind) { |
| 1733 | case ASTNode::Kind::kVarDeclarations: |
| 1734 | this->convertGlobalVarDeclarations(decl); |
| 1735 | break; |
| 1736 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1737 | case ASTNode::Kind::kFunction: |
| 1738 | this->convertFunction(decl); |
| 1739 | break; |
| 1740 | |
| 1741 | case ASTNode::Kind::kModifiers: { |
| 1742 | std::unique_ptr<ModifiersDeclaration> f = |
| 1743 | this->convertModifiersDeclaration(decl); |
| 1744 | if (f) { |
| 1745 | fProgramElements->push_back(std::move(f)); |
| 1746 | } |
| 1747 | break; |
| 1748 | } |
| 1749 | case ASTNode::Kind::kInterfaceBlock: { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1750 | std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1751 | if (i) { |
| 1752 | fProgramElements->push_back(std::move(i)); |
| 1753 | } |
| 1754 | break; |
| 1755 | } |
| 1756 | case ASTNode::Kind::kExtension: { |
| 1757 | std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1758 | decl.getStringView()); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1759 | if (e) { |
| 1760 | fProgramElements->push_back(std::move(e)); |
| 1761 | } |
| 1762 | break; |
| 1763 | } |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1764 | case ASTNode::Kind::kType: { |
| 1765 | std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl); |
| 1766 | if (s) { |
| 1767 | fProgramElements->push_back(std::move(s)); |
| 1768 | } |
| 1769 | break; |
| 1770 | } |
| 1771 | default: |
| 1772 | SkDEBUGFAILF("unsupported declaration: %s\n", decl.description().c_str()); |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | return this->finish(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1778 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1779 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1780 | } // namespace SkSL |