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