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