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