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" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 11 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLCompiler.h" |
| 14 | #include "src/sksl/SkSLParser.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 16 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 17 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 18 | #include "src/sksl/ir/SkSLConstructor.h" |
| 19 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 20 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 21 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 22 | #include "src/sksl/ir/SkSLEnum.h" |
| 23 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLField.h" |
| 27 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 28 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 29 | #include "src/sksl/ir/SkSLForStatement.h" |
| 30 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 32 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 33 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 34 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 35 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 36 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 37 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 38 | #include "src/sksl/ir/SkSLLayout.h" |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 39 | #include "src/sksl/ir/SkSLNop.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 41 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 42 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 43 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 44 | #include "src/sksl/ir/SkSLSetting.h" |
| 45 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 46 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 47 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 48 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 49 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 50 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 51 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 52 | #include "src/sksl/ir/SkSLVariable.h" |
| 53 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 54 | #include "src/sksl/ir/SkSLWhileStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | |
| 56 | namespace SkSL { |
| 57 | |
| 58 | class AutoSymbolTable { |
| 59 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 60 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 61 | : fIR(ir) |
| 62 | , fPrevious(fIR->fSymbolTable) { |
| 63 | fIR->pushSymbolTable(); |
| 64 | } |
| 65 | |
| 66 | ~AutoSymbolTable() { |
| 67 | fIR->popSymbolTable(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 68 | SkASSERT(fPrevious == fIR->fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | IRGenerator* fIR; |
| 72 | std::shared_ptr<SymbolTable> fPrevious; |
| 73 | }; |
| 74 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 75 | class AutoLoopLevel { |
| 76 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 77 | AutoLoopLevel(IRGenerator* ir) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 78 | : fIR(ir) { |
| 79 | fIR->fLoopLevel++; |
| 80 | } |
| 81 | |
| 82 | ~AutoLoopLevel() { |
| 83 | fIR->fLoopLevel--; |
| 84 | } |
| 85 | |
| 86 | IRGenerator* fIR; |
| 87 | }; |
| 88 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 89 | class AutoSwitchLevel { |
| 90 | public: |
| 91 | AutoSwitchLevel(IRGenerator* ir) |
| 92 | : fIR(ir) { |
| 93 | fIR->fSwitchLevel++; |
| 94 | } |
| 95 | |
| 96 | ~AutoSwitchLevel() { |
| 97 | fIR->fSwitchLevel--; |
| 98 | } |
| 99 | |
| 100 | IRGenerator* fIR; |
| 101 | }; |
| 102 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 103 | IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 104 | ErrorReporter& errorReporter) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 105 | : fContext(*context) |
| 106 | , fCurrentFunction(nullptr) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 107 | , fRootSymbolTable(symbolTable) |
| 108 | , fSymbolTable(symbolTable) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 109 | , fLoopLevel(0) |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 110 | , fSwitchLevel(0) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 111 | , fTmpCount(0) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 112 | , fErrors(errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | |
| 114 | void IRGenerator::pushSymbolTable() { |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 115 | fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), &fErrors)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void IRGenerator::popSymbolTable() { |
| 119 | fSymbolTable = fSymbolTable->fParent; |
| 120 | } |
| 121 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 122 | static void fill_caps(const SKSL_CAPS_CLASS& caps, |
| 123 | std::unordered_map<String, Program::Settings::Value>* capsMap) { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 124 | #define CAP(name) \ |
| 125 | capsMap->insert(std::make_pair(String(#name), Program::Settings::Value(caps.name()))) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 126 | CAP(fbFetchSupport); |
Brian Salomon | d401330 | 2018-04-04 13:58:33 +0000 | [diff] [blame] | 127 | CAP(fbFetchNeedsCustomOutput); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 128 | CAP(flatInterpolationSupport); |
| 129 | CAP(noperspectiveInterpolationSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 130 | CAP(externalTextureSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 131 | CAP(mustEnableAdvBlendEqs); |
| 132 | CAP(mustEnableSpecificAdvBlendEqs); |
| 133 | CAP(mustDeclareFragmentShaderOutput); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 134 | CAP(mustDoOpBetweenFloorAndAbs); |
Brian Salomon | f8c187c | 2019-12-19 14:41:57 -0500 | [diff] [blame] | 135 | CAP(mustGuardDivisionEvenAfterExplicitZeroCheck); |
| 136 | CAP(inBlendModesFailRandomlyForAllZeroVec); |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 137 | CAP(atan2ImplementedAsAtanYOverX); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 138 | CAP(canUseAnyFunctionInShader); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 139 | CAP(floatIs32Bits); |
Ethan Nicholas | 07990de | 2017-07-18 09:47:43 -0400 | [diff] [blame] | 140 | CAP(integerSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 141 | #undef CAP |
| 142 | } |
| 143 | |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 144 | void IRGenerator::start(const Program::Settings* settings, |
| 145 | std::vector<std::unique_ptr<ProgramElement>>* inherited) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 146 | if (fStarted) { |
| 147 | this->popSymbolTable(); |
| 148 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 149 | fSettings = settings; |
| 150 | fCapsMap.clear(); |
| 151 | if (settings->fCaps) { |
| 152 | fill_caps(*settings->fCaps, &fCapsMap); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 153 | } else { |
| 154 | fCapsMap.insert(std::make_pair(String("integerSupport"), |
| 155 | Program::Settings::Value(true))); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 156 | } |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 157 | this->pushSymbolTable(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 158 | fInvocations = -1; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 159 | fInputs.reset(); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 160 | fSkPerVertex = nullptr; |
| 161 | fRTAdjust = nullptr; |
| 162 | fRTAdjustInterfaceBlock = nullptr; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 163 | if (inherited) { |
| 164 | for (const auto& e : *inherited) { |
| 165 | if (e->fKind == ProgramElement::kInterfaceBlock_Kind) { |
| 166 | InterfaceBlock& intf = (InterfaceBlock&) *e; |
| 167 | if (intf.fVariable.fName == Compiler::PERVERTEX_NAME) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 168 | SkASSERT(!fSkPerVertex); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 169 | fSkPerVertex = &intf.fVariable; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 174 | SkASSERT(fIntrinsics); |
| 175 | for (auto& pair : *fIntrinsics) { |
| 176 | pair.second.second = false; |
| 177 | } |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 180 | std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, StringFragment name) { |
| 181 | return std::unique_ptr<Extension>(new Extension(offset, name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 184 | void IRGenerator::finish() { |
| 185 | this->popSymbolTable(); |
| 186 | fSettings = nullptr; |
| 187 | } |
| 188 | |
| 189 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 190 | switch (statement.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 191 | case ASTNode::Kind::kBlock: |
| 192 | return this->convertBlock(statement); |
| 193 | case ASTNode::Kind::kVarDeclarations: |
| 194 | return this->convertVarDeclarationStatement(statement); |
| 195 | case ASTNode::Kind::kIf: |
| 196 | return this->convertIf(statement); |
| 197 | case ASTNode::Kind::kFor: |
| 198 | return this->convertFor(statement); |
| 199 | case ASTNode::Kind::kWhile: |
| 200 | return this->convertWhile(statement); |
| 201 | case ASTNode::Kind::kDo: |
| 202 | return this->convertDo(statement); |
| 203 | case ASTNode::Kind::kSwitch: |
| 204 | return this->convertSwitch(statement); |
| 205 | case ASTNode::Kind::kReturn: |
| 206 | return this->convertReturn(statement); |
| 207 | case ASTNode::Kind::kBreak: |
| 208 | return this->convertBreak(statement); |
| 209 | case ASTNode::Kind::kContinue: |
| 210 | return this->convertContinue(statement); |
| 211 | case ASTNode::Kind::kDiscard: |
| 212 | return this->convertDiscard(statement); |
| 213 | default: |
| 214 | // it's an expression |
| 215 | std::unique_ptr<Statement> result = this->convertExpressionStatement(statement); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 216 | if (fRTAdjust && Program::kGeometry_Kind == fKind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 217 | SkASSERT(result->fKind == Statement::kExpression_Kind); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 218 | Expression& expr = *((ExpressionStatement&) *result).fExpression; |
| 219 | if (expr.fKind == Expression::kFunctionCall_Kind) { |
| 220 | FunctionCall& fc = (FunctionCall&) expr; |
| 221 | if (fc.fFunction.fBuiltin && fc.fFunction.fName == "EmitVertex") { |
| 222 | std::vector<std::unique_ptr<Statement>> statements; |
| 223 | statements.push_back(getNormalizeSkPositionCode()); |
| 224 | statements.push_back(std::move(result)); |
| 225 | return std::unique_ptr<Block>(new Block(statement.fOffset, |
| 226 | std::move(statements), |
| 227 | fSymbolTable)); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 235 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) { |
| 236 | SkASSERT(block.fKind == ASTNode::Kind::kBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 237 | AutoSymbolTable table(this); |
| 238 | std::vector<std::unique_ptr<Statement>> statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 239 | for (const auto& child : block) { |
| 240 | std::unique_ptr<Statement> statement = this->convertStatement(child); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 241 | if (!statement) { |
| 242 | return nullptr; |
| 243 | } |
| 244 | statements.push_back(std::move(statement)); |
| 245 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 246 | return std::unique_ptr<Block>(new Block(block.fOffset, std::move(statements), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 249 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) { |
| 250 | SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations); |
| 251 | auto decl = this->convertVarDeclarations(s, Variable::kLocal_Storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 252 | if (!decl) { |
| 253 | return nullptr; |
| 254 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 255 | return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 258 | std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTNode& decls, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 259 | Variable::Storage storage) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 260 | SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations); |
| 261 | auto iter = decls.begin(); |
| 262 | const Modifiers& modifiers = iter++->getModifiers(); |
| 263 | const ASTNode& rawType = *(iter++); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 264 | std::vector<std::unique_ptr<VarDeclaration>> variables; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 265 | const Type* baseType = this->convertType(rawType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | if (!baseType) { |
| 267 | return nullptr; |
| 268 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 269 | if (fKind != Program::kFragmentProcessor_Kind) { |
| 270 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
| 271 | baseType->kind() == Type::Kind::kMatrix_Kind) { |
| 272 | fErrors.error(decls.fOffset, "'in' variables may not have matrix type"); |
| 273 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 274 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
| 275 | (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 276 | fErrors.error(decls.fOffset, |
| 277 | "'in uniform' variables only permitted within fragment processors"); |
| 278 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 279 | if (modifiers.fLayout.fWhen.fLength) { |
| 280 | fErrors.error(decls.fOffset, "'when' is only permitted within fragment processors"); |
| 281 | } |
| 282 | if (modifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 283 | fErrors.error(decls.fOffset, "'tracked' is only permitted within fragment processors"); |
| 284 | } |
| 285 | if (modifiers.fLayout.fCType != Layout::CType::kDefault) { |
| 286 | fErrors.error(decls.fOffset, "'ctype' is only permitted within fragment processors"); |
| 287 | } |
| 288 | if (modifiers.fLayout.fKey) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 289 | fErrors.error(decls.fOffset, "'key' is only permitted within fragment processors"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 290 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 291 | } |
| 292 | if (modifiers.fLayout.fKey && (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 293 | fErrors.error(decls.fOffset, "'key' is not permitted on 'uniform' variables"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 294 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 295 | for (; iter != decls.end(); ++iter) { |
| 296 | const ASTNode& varDecl = *iter; |
| 297 | if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 && |
| 298 | (modifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind && |
| 299 | varDecl.getVarData().fName != "sk_FragColor") { |
| 300 | fErrors.error(varDecl.fOffset, |
Ethan Nicholas | 6c94271 | 2018-03-16 09:45:11 -0400 | [diff] [blame] | 301 | "out location=0, index=0 is reserved for sk_FragColor"); |
| 302 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 303 | const ASTNode::VarData& varData = varDecl.getVarData(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 304 | const Type* type = baseType; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 305 | std::vector<std::unique_ptr<Expression>> sizes; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 306 | auto iter = varDecl.begin(); |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 307 | if (varData.fSizeCount > 0 && (modifiers.fFlags & Modifiers::kIn_Flag)) { |
| 308 | fErrors.error(varDecl.fOffset, "'in' variables may not have array type"); |
| 309 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 310 | for (size_t i = 0; i < varData.fSizeCount; ++i, ++iter) { |
| 311 | const ASTNode& rawSize = *iter; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 312 | if (rawSize) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 313 | auto size = this->coerce(this->convertExpression(rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 314 | if (!size) { |
| 315 | return nullptr; |
| 316 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 317 | String name(type->fName); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 318 | int64_t count; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 319 | if (size->fKind == Expression::kIntLiteral_Kind) { |
| 320 | count = ((IntLiteral&) *size).fValue; |
| 321 | if (count <= 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 322 | fErrors.error(size->fOffset, "array size must be positive"); |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 323 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 324 | } |
| 325 | name += "[" + to_string(count) + "]"; |
| 326 | } else { |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 327 | fErrors.error(size->fOffset, "array size must be specified"); |
| 328 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 330 | type = (Type*) fSymbolTable->takeOwnership( |
| 331 | std::unique_ptr<Symbol>(new Type(name, |
| 332 | Type::kArray_Kind, |
| 333 | *type, |
| 334 | (int) count))); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 335 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 336 | } else { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 337 | type = (Type*) fSymbolTable->takeOwnership( |
| 338 | std::unique_ptr<Symbol>(new Type(type->name() + "[]", |
| 339 | Type::kArray_Kind, |
| 340 | *type, |
| 341 | -1))); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 342 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 345 | auto var = std::unique_ptr<Variable>(new Variable(varDecl.fOffset, modifiers, |
| 346 | varData.fName, *type, storage)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 347 | if (var->fName == Compiler::RTADJUST_NAME) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 348 | SkASSERT(!fRTAdjust); |
| 349 | SkASSERT(var->fType == *fContext.fFloat4_Type); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 350 | fRTAdjust = var.get(); |
| 351 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 352 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 353 | if (iter != varDecl.end()) { |
| 354 | value = this->convertExpression(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 355 | if (!value) { |
| 356 | return nullptr; |
| 357 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 358 | value = this->coerce(std::move(value), *type); |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 359 | if (!value) { |
| 360 | return nullptr; |
| 361 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 362 | var->fWriteCount = 1; |
Ethan Nicholas | 8f6c2ab | 2018-01-17 13:51:52 -0500 | [diff] [blame] | 363 | var->fInitialValue = value.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 364 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 365 | if (storage == Variable::kGlobal_Storage && var->fName == "sk_FragColor" && |
| 366 | (*fSymbolTable)[var->fName]) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 367 | // already defined, ignore |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 368 | } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[var->fName] && |
| 369 | (*fSymbolTable)[var->fName]->fKind == Symbol::kVariable_Kind && |
| 370 | ((Variable*) (*fSymbolTable)[var->fName])->fModifiers.fLayout.fBuiltin >= 0) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 371 | // already defined, just update the modifiers |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 372 | Variable* old = (Variable*) (*fSymbolTable)[var->fName]; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 373 | old->fModifiers = var->fModifiers; |
| 374 | } else { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 375 | variables.emplace_back(new VarDeclaration(var.get(), std::move(sizes), |
| 376 | std::move(value))); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 377 | StringFragment name = var->fName; |
| 378 | fSymbolTable->add(name, std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 379 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 380 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 381 | return std::unique_ptr<VarDeclarations>(new VarDeclarations(decls.fOffset, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 382 | baseType, |
| 383 | std::move(variables))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 386 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) { |
| 387 | SkASSERT(m.fKind == ASTNode::Kind::kModifiers); |
| 388 | Modifiers modifiers = m.getModifiers(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 389 | if (modifiers.fLayout.fInvocations != -1) { |
Ethan Nicholas | f06576b | 2019-04-03 15:45:25 -0400 | [diff] [blame] | 390 | if (fKind != Program::kGeometry_Kind) { |
| 391 | fErrors.error(m.fOffset, "'invocations' is only legal in geometry shaders"); |
| 392 | return nullptr; |
| 393 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 394 | fInvocations = modifiers.fLayout.fInvocations; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 395 | if (fSettings->fCaps && !fSettings->fCaps->gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 396 | modifiers.fLayout.fInvocations = -1; |
| 397 | Variable* invocationId = (Variable*) (*fSymbolTable)["sk_InvocationID"]; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 398 | SkASSERT(invocationId); |
Ethan Nicholas | d1d5256 | 2018-03-20 16:30:34 -0400 | [diff] [blame] | 399 | invocationId->fModifiers.fFlags = 0; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 400 | invocationId->fModifiers.fLayout.fBuiltin = -1; |
| 401 | if (modifiers.fLayout.description() == "") { |
| 402 | return nullptr; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fSettings->fCaps && |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 407 | !fSettings->fCaps->gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 408 | modifiers.fLayout.fMaxVertices *= fInvocations; |
| 409 | } |
| 410 | return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 413 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) { |
| 414 | SkASSERT(n.fKind == ASTNode::Kind::kIf); |
| 415 | auto iter = n.begin(); |
| 416 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 417 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 418 | if (!test) { |
| 419 | return nullptr; |
| 420 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 421 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 422 | if (!ifTrue) { |
| 423 | return nullptr; |
| 424 | } |
| 425 | std::unique_ptr<Statement> ifFalse; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 426 | if (iter != n.end()) { |
| 427 | ifFalse = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 428 | if (!ifFalse) { |
| 429 | return nullptr; |
| 430 | } |
| 431 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 432 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 433 | // static boolean value, fold down to a single branch |
| 434 | if (((BoolLiteral&) *test).fValue) { |
| 435 | return ifTrue; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 436 | } else if (ifFalse) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 437 | return ifFalse; |
| 438 | } else { |
| 439 | // False & no else clause. Not an error, so don't return null! |
| 440 | std::vector<std::unique_ptr<Statement>> empty; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 441 | return std::unique_ptr<Statement>(new Block(n.fOffset, std::move(empty), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 442 | fSymbolTable)); |
| 443 | } |
| 444 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 445 | return std::unique_ptr<Statement>(new IfStatement(n.fOffset, n.getBool(), std::move(test), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 446 | std::move(ifTrue), std::move(ifFalse))); |
| 447 | } |
| 448 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 449 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) { |
| 450 | SkASSERT(f.fKind == ASTNode::Kind::kFor); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 451 | AutoLoopLevel level(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 452 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 453 | std::unique_ptr<Statement> initializer; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 454 | auto iter = f.begin(); |
| 455 | if (*iter) { |
| 456 | initializer = this->convertStatement(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 457 | if (!initializer) { |
| 458 | return nullptr; |
| 459 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 460 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 461 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 462 | std::unique_ptr<Expression> test; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 463 | if (*iter) { |
| 464 | test = this->coerce(this->convertExpression(*iter), *fContext.fBool_Type); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 465 | if (!test) { |
| 466 | return nullptr; |
| 467 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 468 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 469 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 470 | std::unique_ptr<Expression> next; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 471 | if (*iter) { |
| 472 | next = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 473 | if (!next) { |
| 474 | return nullptr; |
| 475 | } |
| 476 | this->checkValid(*next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 477 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 478 | ++iter; |
| 479 | std::unique_ptr<Statement> statement = this->convertStatement(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 480 | if (!statement) { |
| 481 | return nullptr; |
| 482 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 483 | return std::unique_ptr<Statement>(new ForStatement(f.fOffset, std::move(initializer), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 484 | std::move(test), std::move(next), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 485 | std::move(statement), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 488 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) { |
| 489 | SkASSERT(w.fKind == ASTNode::Kind::kWhile); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 490 | AutoLoopLevel level(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 491 | auto iter = w.begin(); |
| 492 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 493 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 494 | if (!test) { |
| 495 | return nullptr; |
| 496 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 497 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 498 | if (!statement) { |
| 499 | return nullptr; |
| 500 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 501 | return std::unique_ptr<Statement>(new WhileStatement(w.fOffset, std::move(test), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 502 | std::move(statement))); |
| 503 | } |
| 504 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 505 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) { |
| 506 | SkASSERT(d.fKind == ASTNode::Kind::kDo); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 507 | AutoLoopLevel level(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 508 | auto iter = d.begin(); |
| 509 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
| 510 | if (!statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 511 | return nullptr; |
| 512 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 513 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
| 514 | *fContext.fBool_Type); |
| 515 | if (!test) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 516 | return nullptr; |
| 517 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 518 | return std::unique_ptr<Statement>(new DoStatement(d.fOffset, std::move(statement), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 519 | std::move(test))); |
| 520 | } |
| 521 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 522 | std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) { |
| 523 | SkASSERT(s.fKind == ASTNode::Kind::kSwitch); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 524 | AutoSwitchLevel level(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 525 | auto iter = s.begin(); |
| 526 | std::unique_ptr<Expression> value = this->convertExpression(*(iter++)); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 527 | if (!value) { |
| 528 | return nullptr; |
| 529 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 530 | if (value->fType != *fContext.fUInt_Type && value->fType.kind() != Type::kEnum_Kind) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 531 | value = this->coerce(std::move(value), *fContext.fInt_Type); |
| 532 | if (!value) { |
| 533 | return nullptr; |
| 534 | } |
| 535 | } |
| 536 | AutoSymbolTable table(this); |
| 537 | std::unordered_set<int> caseValues; |
| 538 | std::vector<std::unique_ptr<SwitchCase>> cases; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 539 | for (; iter != s.end(); ++iter) { |
| 540 | const ASTNode& c = *iter; |
| 541 | SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 542 | std::unique_ptr<Expression> caseValue; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 543 | auto childIter = c.begin(); |
| 544 | if (*childIter) { |
| 545 | caseValue = this->convertExpression(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 546 | if (!caseValue) { |
| 547 | return nullptr; |
| 548 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 549 | caseValue = this->coerce(std::move(caseValue), value->fType); |
| 550 | if (!caseValue) { |
| 551 | return nullptr; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 552 | } |
| 553 | if (!caseValue->isConstant()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 554 | fErrors.error(caseValue->fOffset, "case value must be a constant"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 555 | return nullptr; |
| 556 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 557 | int64_t v; |
| 558 | this->getConstantInt(*caseValue, &v); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 559 | if (caseValues.find(v) != caseValues.end()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 560 | fErrors.error(caseValue->fOffset, "duplicate case value"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 561 | } |
| 562 | caseValues.insert(v); |
| 563 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 564 | ++childIter; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 565 | std::vector<std::unique_ptr<Statement>> statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 566 | for (; childIter != c.end(); ++childIter) { |
| 567 | std::unique_ptr<Statement> converted = this->convertStatement(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 568 | if (!converted) { |
| 569 | return nullptr; |
| 570 | } |
| 571 | statements.push_back(std::move(converted)); |
| 572 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 573 | cases.emplace_back(new SwitchCase(c.fOffset, std::move(caseValue), |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 574 | std::move(statements))); |
| 575 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 576 | return std::unique_ptr<Statement>(new SwitchStatement(s.fOffset, s.getBool(), |
Ethan Nicholas | c432b0c | 2017-07-18 13:22:37 -0400 | [diff] [blame] | 577 | std::move(value), std::move(cases), |
| 578 | fSymbolTable)); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 581 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) { |
| 582 | std::unique_ptr<Expression> e = this->convertExpression(s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 583 | if (!e) { |
| 584 | return nullptr; |
| 585 | } |
| 586 | this->checkValid(*e); |
| 587 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 588 | } |
| 589 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 590 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) { |
| 591 | SkASSERT(r.fKind == ASTNode::Kind::kReturn); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 592 | SkASSERT(fCurrentFunction); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 593 | // early returns from a vertex main function will bypass the sk_Position normalization, so |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 594 | // SkASSERT that we aren't doing that. It is of course possible to fix this by adding a |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 595 | // normalization before each return, but it will probably never actually be necessary. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 596 | SkASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->fName); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 597 | if (r.begin() != r.end()) { |
| 598 | std::unique_ptr<Expression> result = this->convertExpression(*r.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 599 | if (!result) { |
| 600 | return nullptr; |
| 601 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 602 | if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 603 | fErrors.error(result->fOffset, "may not return a value from a void function"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 604 | } else { |
| 605 | result = this->coerce(std::move(result), fCurrentFunction->fReturnType); |
| 606 | if (!result) { |
| 607 | return nullptr; |
| 608 | } |
| 609 | } |
| 610 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 611 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 612 | if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 613 | fErrors.error(r.fOffset, "expected function to return '" + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 614 | fCurrentFunction->fReturnType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 615 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 616 | return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 620 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) { |
| 621 | SkASSERT(b.fKind == ASTNode::Kind::kBreak); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 622 | if (fLoopLevel > 0 || fSwitchLevel > 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 623 | return std::unique_ptr<Statement>(new BreakStatement(b.fOffset)); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 624 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 625 | fErrors.error(b.fOffset, "break statement must be inside a loop or switch"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 626 | return nullptr; |
| 627 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 630 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) { |
| 631 | SkASSERT(c.fKind == ASTNode::Kind::kContinue); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 632 | if (fLoopLevel > 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 633 | return std::unique_ptr<Statement>(new ContinueStatement(c.fOffset)); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 634 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 635 | fErrors.error(c.fOffset, "continue statement must be inside a loop"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 636 | return nullptr; |
| 637 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 640 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) { |
| 641 | SkASSERT(d.fKind == ASTNode::Kind::kDiscard); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 642 | return std::unique_ptr<Statement>(new DiscardStatement(d.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 645 | std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 646 | Layout invokeLayout; |
| 647 | Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 648 | FunctionDeclaration* invokeDecl = new FunctionDeclaration(-1, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 649 | invokeModifiers, |
| 650 | "_invoke", |
| 651 | std::vector<const Variable*>(), |
| 652 | *fContext.fVoid_Type); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 653 | fProgramElements->push_back(std::unique_ptr<ProgramElement>( |
| 654 | new FunctionDefinition(-1, *invokeDecl, std::move(main)))); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 655 | fSymbolTable->add(invokeDecl->fName, std::unique_ptr<FunctionDeclaration>(invokeDecl)); |
| 656 | |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 657 | std::vector<std::unique_ptr<VarDeclaration>> variables; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 658 | Variable* loopIdx = (Variable*) (*fSymbolTable)["sk_InvocationID"]; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 659 | SkASSERT(loopIdx); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 660 | std::unique_ptr<Expression> test(new BinaryExpression(-1, |
| 661 | std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 662 | Token::LT, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 663 | std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, fInvocations)), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 664 | *fContext.fBool_Type)); |
| 665 | std::unique_ptr<Expression> next(new PostfixExpression( |
| 666 | std::unique_ptr<Expression>( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 667 | new VariableReference(-1, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 668 | *loopIdx, |
| 669 | VariableReference::kReadWrite_RefKind)), |
| 670 | Token::PLUSPLUS)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 671 | ASTNode endPrimitiveID(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, "EndPrimitive"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 672 | std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 673 | SkASSERT(endPrimitive); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 674 | |
| 675 | std::vector<std::unique_ptr<Statement>> loopBody; |
| 676 | std::vector<std::unique_ptr<Expression>> invokeArgs; |
| 677 | loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 678 | this->call(-1, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 679 | *invokeDecl, |
| 680 | std::vector<std::unique_ptr<Expression>>())))); |
| 681 | loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 682 | this->call(-1, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 683 | std::move(endPrimitive), |
| 684 | std::vector<std::unique_ptr<Expression>>())))); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 685 | std::unique_ptr<Expression> assignment(new BinaryExpression(-1, |
| 686 | std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 687 | Token::EQ, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 688 | std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, 0)), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 689 | *fContext.fInt_Type)); |
| 690 | std::unique_ptr<Statement> initializer(new ExpressionStatement(std::move(assignment))); |
| 691 | std::unique_ptr<Statement> loop = std::unique_ptr<Statement>( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 692 | new ForStatement(-1, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 693 | std::move(initializer), |
| 694 | std::move(test), |
| 695 | std::move(next), |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 696 | std::unique_ptr<Block>(new Block(-1, std::move(loopBody))), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 697 | fSymbolTable)); |
| 698 | std::vector<std::unique_ptr<Statement>> children; |
| 699 | children.push_back(std::move(loop)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 700 | return std::unique_ptr<Block>(new Block(-1, std::move(children))); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 701 | } |
| 702 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 703 | std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() { |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 704 | // sk_Position = float4(sk_Position.xy * rtAdjust.xz + sk_Position.ww * rtAdjust.yw, |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 705 | // 0, |
| 706 | // sk_Position.w); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 707 | SkASSERT(fSkPerVertex && fRTAdjust); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 708 | #define REF(var) std::unique_ptr<Expression>(\ |
| 709 | new VariableReference(-1, *var, VariableReference::kRead_RefKind)) |
| 710 | #define FIELD(var, idx) std::unique_ptr<Expression>(\ |
| 711 | new FieldAccess(REF(var), idx, FieldAccess::kAnonymousInterfaceBlock_OwnerKind)) |
| 712 | #define POS std::unique_ptr<Expression>(new FieldAccess(REF(fSkPerVertex), 0, \ |
| 713 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)) |
| 714 | #define ADJUST (fRTAdjustInterfaceBlock ? \ |
| 715 | FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \ |
| 716 | REF(fRTAdjust)) |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 717 | #define SWIZZLE(expr, ...) std::unique_ptr<Expression>(new Swizzle(fContext, expr, \ |
| 718 | { __VA_ARGS__ })) |
| 719 | #define OP(left, op, right) std::unique_ptr<Expression>( \ |
| 720 | new BinaryExpression(-1, left, op, right, \ |
| 721 | *fContext.fFloat2_Type)) |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 722 | std::vector<std::unique_ptr<Expression>> children; |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 723 | children.push_back(OP(OP(SWIZZLE(POS, 0, 1), Token::STAR, SWIZZLE(ADJUST, 0, 2)), |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 724 | Token::PLUS, |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 725 | OP(SWIZZLE(POS, 3, 3), Token::STAR, SWIZZLE(ADJUST, 1, 3)))); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 726 | children.push_back(std::unique_ptr<Expression>(new FloatLiteral(fContext, -1, 0.0))); |
| 727 | children.push_back(SWIZZLE(POS, 3)); |
| 728 | std::unique_ptr<Expression> result = OP(POS, Token::EQ, |
| 729 | std::unique_ptr<Expression>(new Constructor(-1, |
| 730 | *fContext.fFloat4_Type, |
| 731 | std::move(children)))); |
| 732 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(result))); |
| 733 | } |
| 734 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 735 | void IRGenerator::convertFunction(const ASTNode& f) { |
| 736 | auto iter = f.begin(); |
| 737 | const Type* returnType = this->convertType(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 738 | if (!returnType) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 739 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 740 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 741 | const ASTNode::FunctionData& fd = f.getFunctionData(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 742 | std::vector<const Variable*> parameters; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 743 | for (size_t i = 0; i < fd.fParameterCount; ++i) { |
| 744 | const ASTNode& param = *(iter++); |
| 745 | SkASSERT(param.fKind == ASTNode::Kind::kParameter); |
| 746 | ASTNode::ParameterData pd = param.getParameterData(); |
| 747 | auto paramIter = param.begin(); |
| 748 | const Type* type = this->convertType(*(paramIter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 749 | if (!type) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 750 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 751 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 752 | for (int j = (int) pd.fSizeCount; j >= 1; j--) { |
| 753 | int size = (param.begin() + j)->getInt(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 754 | String name = type->name() + "[" + to_string(size) + "]"; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 755 | type = (Type*) fSymbolTable->takeOwnership( |
| 756 | std::unique_ptr<Symbol>(new Type(std::move(name), |
| 757 | Type::kArray_Kind, |
| 758 | *type, |
| 759 | size))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 760 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 761 | StringFragment name = pd.fName; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 762 | Variable* var = (Variable*) fSymbolTable->takeOwnership( |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 763 | std::unique_ptr<Symbol>(new Variable(param.fOffset, |
| 764 | pd.fModifiers, |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 765 | name, |
| 766 | *type, |
| 767 | Variable::kParameter_Storage))); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 768 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 771 | if (fd.fName == "main") { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 772 | switch (fKind) { |
| 773 | case Program::kPipelineStage_Kind: { |
| 774 | bool valid; |
| 775 | switch (parameters.size()) { |
| 776 | case 3: |
Mike Reed | 8c31f2b | 2019-07-16 16:50:14 -0400 | [diff] [blame] | 777 | valid = parameters[0]->fType == *fContext.fFloat_Type && |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 778 | parameters[0]->fModifiers.fFlags == 0 && |
Mike Reed | 8c31f2b | 2019-07-16 16:50:14 -0400 | [diff] [blame] | 779 | parameters[1]->fType == *fContext.fFloat_Type && |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 780 | parameters[1]->fModifiers.fFlags == 0 && |
| 781 | parameters[2]->fType == *fContext.fHalf4_Type && |
| 782 | parameters[2]->fModifiers.fFlags == (Modifiers::kIn_Flag | |
| 783 | Modifiers::kOut_Flag); |
| 784 | break; |
| 785 | case 1: |
| 786 | valid = parameters[0]->fType == *fContext.fHalf4_Type && |
| 787 | parameters[0]->fModifiers.fFlags == (Modifiers::kIn_Flag | |
| 788 | Modifiers::kOut_Flag); |
| 789 | break; |
| 790 | default: |
| 791 | valid = false; |
| 792 | } |
| 793 | if (!valid) { |
Mike Reed | 8c31f2b | 2019-07-16 16:50:14 -0400 | [diff] [blame] | 794 | fErrors.error(f.fOffset, "pipeline stage 'main' must be declared main(float, " |
| 795 | "float, inout half4) or main(inout half4)"); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 796 | return; |
| 797 | } |
| 798 | break; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 799 | } |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 800 | case Program::kGeneric_Kind: |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 801 | break; |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 802 | default: |
| 803 | if (parameters.size()) { |
| 804 | fErrors.error(f.fOffset, "shader 'main' must have zero parameters"); |
| 805 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 809 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 810 | const FunctionDeclaration* decl = nullptr; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 811 | auto entry = (*fSymbolTable)[fd.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 812 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 813 | std::vector<const FunctionDeclaration*> functions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 814 | switch (entry->fKind) { |
| 815 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 816 | functions = ((UnresolvedFunction*) entry)->fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 817 | break; |
| 818 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 819 | functions.push_back((FunctionDeclaration*) entry); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 820 | break; |
| 821 | default: |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 822 | fErrors.error(f.fOffset, "symbol '" + fd.fName + "' was already defined"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 823 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 824 | } |
| 825 | for (const auto& other : functions) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 826 | SkASSERT(other->fName == fd.fName); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 827 | if (parameters.size() == other->fParameters.size()) { |
| 828 | bool match = true; |
| 829 | for (size_t i = 0; i < parameters.size(); i++) { |
| 830 | if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 831 | match = false; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | if (match) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 836 | if (*returnType != other->fReturnType) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 837 | FunctionDeclaration newDecl(f.fOffset, fd.fModifiers, fd.fName, parameters, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 838 | *returnType); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 839 | fErrors.error(f.fOffset, "functions '" + newDecl.declaration() + |
| 840 | "' and '" + other->declaration() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 841 | "' differ only in return type"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 842 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 843 | } |
| 844 | decl = other; |
| 845 | for (size_t i = 0; i < parameters.size(); i++) { |
| 846 | if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 847 | fErrors.error(f.fOffset, "modifiers on parameter " + |
| 848 | to_string((uint64_t) i + 1) + |
| 849 | " differ between declaration and " |
| 850 | "definition"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 851 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 852 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 853 | } |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 854 | if (other->fDefined && !other->fBuiltin) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 855 | fErrors.error(f.fOffset, "duplicate definition of " + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 856 | other->declaration()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 857 | } |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | if (!decl) { |
| 864 | // couldn't find an existing declaration |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 865 | auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 866 | fd.fModifiers, |
| 867 | fd.fName, |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 868 | parameters, |
| 869 | *returnType)); |
| 870 | decl = newDecl.get(); |
| 871 | fSymbolTable->add(decl->fName, std::move(newDecl)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 872 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 873 | if (iter != f.end()) { |
| 874 | // compile body |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 875 | SkASSERT(!fCurrentFunction); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 876 | fCurrentFunction = decl; |
| 877 | decl->fDefined = true; |
| 878 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 879 | AutoSymbolTable table(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 880 | if (fd.fName == "main" && fKind == Program::kPipelineStage_Kind) { |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 881 | if (parameters.size() == 3) { |
| 882 | parameters[0]->fModifiers.fLayout.fBuiltin = SK_MAIN_X_BUILTIN; |
| 883 | parameters[1]->fModifiers.fLayout.fBuiltin = SK_MAIN_Y_BUILTIN; |
| 884 | parameters[2]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN; |
| 885 | } else { |
| 886 | SkASSERT(parameters.size() == 1); |
| 887 | parameters[0]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN; |
| 888 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 889 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 890 | for (size_t i = 0; i < parameters.size(); i++) { |
| 891 | fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 892 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 893 | bool needInvocationIDWorkaround = fInvocations != -1 && fd.fName == "main" && |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 894 | fSettings->fCaps && |
| 895 | !fSettings->fCaps->gsInvocationsSupport(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 896 | SkASSERT(!fExtraVars.size()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 897 | std::unique_ptr<Block> body = this->convertBlock(*iter); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 898 | for (auto& v : fExtraVars) { |
| 899 | body->fStatements.insert(body->fStatements.begin(), std::move(v)); |
| 900 | } |
| 901 | fExtraVars.clear(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 902 | fCurrentFunction = nullptr; |
| 903 | if (!body) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | if (needInvocationIDWorkaround) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 907 | body = this->applyInvocationIDWorkaround(std::move(body)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 908 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 909 | // conservatively assume all user-defined functions have side effects |
| 910 | ((Modifiers&) decl->fModifiers).fFlags |= Modifiers::kHasSideEffects_Flag; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 911 | if (Program::kVertex_Kind == fKind && fd.fName == "main" && fRTAdjust) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 912 | body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode()); |
| 913 | } |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 914 | std::unique_ptr<FunctionDefinition> result(new FunctionDefinition(f.fOffset, *decl, |
| 915 | std::move(body))); |
| 916 | result->fSource = &f; |
| 917 | fProgramElements->push_back(std::move(result)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 918 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 921 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) { |
| 922 | SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock); |
| 923 | ASTNode::InterfaceBlockData id = intf.getInterfaceBlockData(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 924 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 925 | this->pushSymbolTable(); |
| 926 | std::shared_ptr<SymbolTable> symbols = fSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 927 | std::vector<Type::Field> fields; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 928 | bool haveRuntimeArray = false; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 929 | bool foundRTAdjust = false; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 930 | auto iter = intf.begin(); |
| 931 | for (size_t i = 0; i < id.fDeclarationCount; ++i) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 932 | std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations( |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 933 | *(iter++), |
Ethan Nicholas | a7ceb50 | 2019-01-11 10:31:48 -0500 | [diff] [blame] | 934 | Variable::kInterfaceBlock_Storage); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 935 | if (!decl) { |
| 936 | return nullptr; |
| 937 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 938 | for (const auto& stmt : decl->fVars) { |
| 939 | VarDeclaration& vd = (VarDeclaration&) *stmt; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 940 | if (haveRuntimeArray) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 941 | fErrors.error(decl->fOffset, |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 942 | "only the last entry in an interface block may be a runtime-sized " |
| 943 | "array"); |
| 944 | } |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 945 | if (vd.fVar == fRTAdjust) { |
| 946 | foundRTAdjust = true; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 947 | SkASSERT(vd.fVar->fType == *fContext.fFloat4_Type); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 948 | fRTAdjustFieldIndex = fields.size(); |
| 949 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 950 | fields.push_back(Type::Field(vd.fVar->fModifiers, vd.fVar->fName, |
| 951 | &vd.fVar->fType)); |
| 952 | if (vd.fValue) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 953 | fErrors.error(decl->fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 954 | "initializers are not permitted on interface block fields"); |
| 955 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 956 | if (vd.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 957 | Modifiers::kOut_Flag | |
| 958 | Modifiers::kUniform_Flag | |
| 959 | Modifiers::kBuffer_Flag | |
| 960 | Modifiers::kConst_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 961 | fErrors.error(decl->fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 962 | "interface block fields may not have storage qualifiers"); |
| 963 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 964 | if (vd.fVar->fType.kind() == Type::kArray_Kind && |
| 965 | vd.fVar->fType.columns() == -1) { |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 966 | haveRuntimeArray = true; |
| 967 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 968 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 969 | } |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 970 | this->popSymbolTable(); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 971 | Type* type = (Type*) old->takeOwnership(std::unique_ptr<Symbol>(new Type(intf.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 972 | id.fTypeName, |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 973 | fields))); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 974 | std::vector<std::unique_ptr<Expression>> sizes; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 975 | for (size_t i = 0; i < id.fSizeCount; ++i) { |
| 976 | const ASTNode& size = *(iter++); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 977 | if (size) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 978 | std::unique_ptr<Expression> converted = this->convertExpression(size); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 979 | if (!converted) { |
| 980 | return nullptr; |
| 981 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 982 | String name = type->fName; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 983 | int64_t count; |
| 984 | if (converted->fKind == Expression::kIntLiteral_Kind) { |
| 985 | count = ((IntLiteral&) *converted).fValue; |
| 986 | if (count <= 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 987 | fErrors.error(converted->fOffset, "array size must be positive"); |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 988 | return nullptr; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 989 | } |
| 990 | name += "[" + to_string(count) + "]"; |
| 991 | } else { |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 992 | fErrors.error(intf.fOffset, "array size must be specified"); |
| 993 | return nullptr; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 994 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 995 | type = (Type*) symbols->takeOwnership(std::unique_ptr<Symbol>( |
| 996 | new Type(name, |
| 997 | Type::kArray_Kind, |
| 998 | *type, |
| 999 | (int) count))); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1000 | sizes.push_back(std::move(converted)); |
| 1001 | } else { |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 1002 | fErrors.error(intf.fOffset, "array size must be specified"); |
| 1003 | return nullptr; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1004 | } |
| 1005 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1006 | Variable* var = (Variable*) old->takeOwnership(std::unique_ptr<Symbol>( |
| 1007 | new Variable(intf.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1008 | id.fModifiers, |
| 1009 | id.fInstanceName.fLength ? id.fInstanceName : id.fTypeName, |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1010 | *type, |
| 1011 | Variable::kGlobal_Storage))); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1012 | if (foundRTAdjust) { |
| 1013 | fRTAdjustInterfaceBlock = var; |
| 1014 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1015 | if (id.fInstanceName.fLength) { |
| 1016 | old->addWithoutOwnership(id.fInstanceName, var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1017 | } else { |
| 1018 | for (size_t i = 0; i < fields.size(); i++) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1019 | old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fOffset, *var, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1020 | (int) i))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1023 | return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fOffset, |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 1024 | var, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1025 | id.fTypeName, |
| 1026 | id.fInstanceName, |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1027 | std::move(sizes), |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 1028 | symbols)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1031 | void IRGenerator::getConstantInt(const Expression& value, int64_t* out) { |
| 1032 | switch (value.fKind) { |
| 1033 | case Expression::kIntLiteral_Kind: |
| 1034 | *out = ((const IntLiteral&) value).fValue; |
| 1035 | break; |
| 1036 | case Expression::kVariableReference_Kind: { |
| 1037 | const Variable& var = ((VariableReference&) value).fVariable; |
| 1038 | if ((var.fModifiers.fFlags & Modifiers::kConst_Flag) && |
| 1039 | var.fInitialValue) { |
| 1040 | this->getConstantInt(*var.fInitialValue, out); |
| 1041 | } |
| 1042 | break; |
| 1043 | } |
| 1044 | default: |
| 1045 | fErrors.error(value.fOffset, "expected a constant int"); |
| 1046 | } |
| 1047 | } |
| 1048 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1049 | void IRGenerator::convertEnum(const ASTNode& e) { |
| 1050 | SkASSERT(e.fKind == ASTNode::Kind::kEnum); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1051 | std::vector<Variable*> variables; |
| 1052 | int64_t currentValue = 0; |
| 1053 | Layout layout; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1054 | ASTNode enumType(e.fNodes, e.fOffset, ASTNode::Kind::kType, |
| 1055 | ASTNode::TypeData(e.getString(), false, false)); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1056 | const Type* type = this->convertType(enumType); |
| 1057 | Modifiers modifiers(layout, Modifiers::kConst_Flag); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1058 | std::shared_ptr<SymbolTable> symbols(new SymbolTable(fSymbolTable, &fErrors)); |
| 1059 | fSymbolTable = symbols; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1060 | for (auto iter = e.begin(); iter != e.end(); ++iter) { |
| 1061 | const ASTNode& child = *iter; |
| 1062 | SkASSERT(child.fKind == ASTNode::Kind::kEnumCase); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1063 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1064 | if (child.begin() != child.end()) { |
| 1065 | value = this->convertExpression(*child.begin()); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1066 | if (!value) { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1067 | fSymbolTable = symbols->fParent; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1068 | return; |
| 1069 | } |
| 1070 | this->getConstantInt(*value, ¤tValue); |
| 1071 | } |
| 1072 | value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue)); |
| 1073 | ++currentValue; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1074 | auto var = std::unique_ptr<Variable>(new Variable(e.fOffset, modifiers, child.getString(), |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1075 | *type, Variable::kGlobal_Storage, |
| 1076 | value.get())); |
| 1077 | variables.push_back(var.get()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1078 | symbols->add(child.getString(), std::move(var)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1079 | symbols->takeOwnership(std::move(value)); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1080 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1081 | fProgramElements->push_back(std::unique_ptr<ProgramElement>(new Enum(e.fOffset, e.getString(), |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1082 | symbols))); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1083 | fSymbolTable = symbols->fParent; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1084 | } |
| 1085 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1086 | const Type* IRGenerator::convertType(const ASTNode& type) { |
| 1087 | ASTNode::TypeData td = type.getTypeData(); |
| 1088 | const Symbol* result = (*fSymbolTable)[td.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1089 | if (result && result->fKind == Symbol::kType_Kind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1090 | if (td.fIsNullable) { |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1091 | if (((Type&) *result) == *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1092 | if (type.begin() != type.end()) { |
| 1093 | fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in " |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1094 | "an array"); |
| 1095 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1096 | result = fSymbolTable->takeOwnership(std::unique_ptr<Symbol>( |
| 1097 | new Type(String(result->fName) + "?", |
| 1098 | Type::kNullable_Kind, |
| 1099 | (const Type&) *result))); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1100 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1101 | fErrors.error(type.fOffset, "type '" + td.fName + "' may not be nullable"); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1102 | } |
| 1103 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1104 | for (const auto& size : type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1105 | String name(result->fName); |
| 1106 | name += "["; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1107 | if (size) { |
| 1108 | name += to_string(size.getInt()); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1109 | } |
| 1110 | name += "]"; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1111 | result = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>( |
| 1112 | new Type(name, |
| 1113 | Type::kArray_Kind, |
| 1114 | (const Type&) *result, |
Brian Osman | b9416ca | 2019-06-12 16:18:32 -0400 | [diff] [blame] | 1115 | size ? size.getInt() |
| 1116 | : 0))); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1117 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1118 | return (const Type*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1119 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1120 | fErrors.error(type.fOffset, "unknown type '" + td.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1121 | return nullptr; |
| 1122 | } |
| 1123 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1124 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1125 | switch (expr.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1126 | case ASTNode::Kind::kBinary: |
| 1127 | return this->convertBinaryExpression(expr); |
| 1128 | case ASTNode::Kind::kBool: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1129 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1130 | expr.getBool())); |
| 1131 | case ASTNode::Kind::kCall: |
| 1132 | return this->convertCallExpression(expr); |
| 1133 | case ASTNode::Kind::kField: |
| 1134 | return this->convertFieldExpression(expr); |
| 1135 | case ASTNode::Kind::kFloat: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1136 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1137 | expr.getFloat())); |
| 1138 | case ASTNode::Kind::kIdentifier: |
| 1139 | return this->convertIdentifier(expr); |
| 1140 | case ASTNode::Kind::kIndex: |
| 1141 | return this->convertIndexExpression(expr); |
| 1142 | case ASTNode::Kind::kInt: |
| 1143 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset, |
| 1144 | expr.getInt())); |
| 1145 | case ASTNode::Kind::kNull: |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1146 | return std::unique_ptr<Expression>(new NullLiteral(fContext, expr.fOffset)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1147 | case ASTNode::Kind::kPostfix: |
| 1148 | return this->convertPostfixExpression(expr); |
| 1149 | case ASTNode::Kind::kPrefix: |
| 1150 | return this->convertPrefixExpression(expr); |
| 1151 | case ASTNode::Kind::kTernary: |
| 1152 | return this->convertTernaryExpression(expr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1153 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1154 | #ifdef SK_DEBUG |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1155 | ABORT("unsupported expression: %s\n", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1156 | #endif |
| 1157 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1161 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) { |
| 1162 | SkASSERT(identifier.fKind == ASTNode::Kind::kIdentifier); |
| 1163 | const Symbol* result = (*fSymbolTable)[identifier.getString()]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1164 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1165 | fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.getString() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1166 | return nullptr; |
| 1167 | } |
| 1168 | switch (result->fKind) { |
| 1169 | case Symbol::kFunctionDeclaration_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1170 | std::vector<const FunctionDeclaration*> f = { |
| 1171 | (const FunctionDeclaration*) result |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1172 | }; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1173 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1174 | identifier.fOffset, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1175 | f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1176 | } |
| 1177 | case Symbol::kUnresolvedFunction_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1178 | const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 1179 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1180 | identifier.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1181 | f->fFunctions)); |
| 1182 | } |
| 1183 | case Symbol::kVariable_Kind: { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 1184 | const Variable* var = (const Variable*) result; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1185 | switch (var->fModifiers.fLayout.fBuiltin) { |
| 1186 | case SK_WIDTH_BUILTIN: |
| 1187 | fInputs.fRTWidth = true; |
| 1188 | break; |
| 1189 | case SK_HEIGHT_BUILTIN: |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1190 | fInputs.fRTHeight = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1191 | break; |
| 1192 | #ifndef SKSL_STANDALONE |
| 1193 | case SK_FRAGCOORD_BUILTIN: |
Brian Osman | 9f313b6 | 2019-10-02 12:03:11 -0400 | [diff] [blame] | 1194 | fInputs.fFlipY = true; |
| 1195 | if (fSettings->fFlipY && |
| 1196 | (!fSettings->fCaps || |
| 1197 | !fSettings->fCaps->fragCoordConventionsExtensionString())) { |
| 1198 | fInputs.fRTHeight = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1199 | } |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1200 | #endif |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1201 | } |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 1202 | if (fKind == Program::kFragmentProcessor_Kind && |
| 1203 | (var->fModifiers.fFlags & Modifiers::kIn_Flag) && |
| 1204 | !(var->fModifiers.fFlags & Modifiers::kUniform_Flag) && |
| 1205 | !var->fModifiers.fLayout.fKey && |
| 1206 | var->fModifiers.fLayout.fBuiltin == -1 && |
| 1207 | var->fType.nonnullable() != *fContext.fFragmentProcessor_Type && |
| 1208 | var->fType.kind() != Type::kSampler_Kind) { |
| 1209 | bool valid = false; |
| 1210 | for (const auto& decl : fFile->root()) { |
| 1211 | if (decl.fKind == ASTNode::Kind::kSection) { |
| 1212 | ASTNode::SectionData section = decl.getSectionData(); |
| 1213 | if (section.fName == "setData") { |
| 1214 | valid = true; |
| 1215 | break; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | if (!valid) { |
| 1220 | fErrors.error(identifier.fOffset, "'in' variable must be either 'uniform' or " |
| 1221 | "'layout(key)', or there must be a custom " |
| 1222 | "@setData function"); |
| 1223 | } |
| 1224 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1225 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
| 1226 | return std::unique_ptr<VariableReference>(new VariableReference( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1227 | identifier.fOffset, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1228 | *var, |
| 1229 | VariableReference::kRead_RefKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1230 | } |
| 1231 | case Symbol::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1232 | const Field* field = (const Field*) result; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1233 | VariableReference* base = new VariableReference(identifier.fOffset, field->fOwner, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1234 | VariableReference::kRead_RefKind); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1235 | return std::unique_ptr<Expression>(new FieldAccess( |
| 1236 | std::unique_ptr<Expression>(base), |
| 1237 | field->fFieldIndex, |
| 1238 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1239 | } |
| 1240 | case Symbol::kType_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1241 | const Type* t = (const Type*) result; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1242 | return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fOffset, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1243 | *t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1244 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1245 | case Symbol::kExternal_Kind: { |
| 1246 | ExternalValue* r = (ExternalValue*) result; |
| 1247 | return std::unique_ptr<ExternalValueReference>( |
| 1248 | new ExternalValueReference(identifier.fOffset, r)); |
| 1249 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1250 | default: |
| 1251 | ABORT("unsupported symbol type %d\n", result->fKind); |
| 1252 | } |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1255 | std::unique_ptr<Section> IRGenerator::convertSection(const ASTNode& s) { |
| 1256 | ASTNode::SectionData section = s.getSectionData(); |
| 1257 | return std::unique_ptr<Section>(new Section(s.fOffset, section.fName, section.fArgument, |
| 1258 | section.fText)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1262 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1263 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1264 | if (!expr) { |
| 1265 | return nullptr; |
| 1266 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1267 | if (expr->fType == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1268 | return expr; |
| 1269 | } |
| 1270 | this->checkValid(*expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1271 | if (expr->fType == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1272 | return nullptr; |
| 1273 | } |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1274 | if (expr->coercionCost(type) == INT_MAX) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1275 | fErrors.error(expr->fOffset, "expected '" + type.displayName() + "', but found '" + |
| 1276 | expr->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1277 | return nullptr; |
| 1278 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1279 | if (type.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1280 | std::vector<std::unique_ptr<Expression>> args; |
| 1281 | args.push_back(std::move(expr)); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1282 | std::unique_ptr<Expression> ctor; |
| 1283 | if (type == *fContext.fFloatLiteral_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1284 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
| 1285 | "float")); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1286 | } else if (type == *fContext.fIntLiteral_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1287 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
| 1288 | "int")); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1289 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1290 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
| 1291 | type.fName)); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1292 | } |
| 1293 | if (!ctor) { |
| 1294 | printf("error, null identifier: %s\n", String(type.fName).c_str()); |
| 1295 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1296 | SkASSERT(ctor); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1297 | return this->call(-1, std::move(ctor), std::move(args)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1298 | } |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1299 | if (expr->fKind == Expression::kNullLiteral_Kind) { |
| 1300 | SkASSERT(type.kind() == Type::kNullable_Kind); |
| 1301 | return std::unique_ptr<Expression>(new NullLiteral(expr->fOffset, type)); |
| 1302 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1303 | std::vector<std::unique_ptr<Expression>> args; |
| 1304 | args.push_back(std::move(expr)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1305 | return std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1308 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
| 1309 | if (left.kind() == Type::kMatrix_Kind) { |
| 1310 | return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind; |
| 1311 | } |
| 1312 | return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind; |
| 1313 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1314 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1315 | /** |
| 1316 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 1317 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 1318 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1319 | static bool determine_binary_type(const Context& context, |
| 1320 | Token::Kind op, |
| 1321 | const Type& left, |
| 1322 | const Type& right, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1323 | const Type** outLeftType, |
| 1324 | const Type** outRightType, |
| 1325 | const Type** outResultType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1326 | bool tryFlipped) { |
| 1327 | bool isLogical; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1328 | bool validMatrixOrVectorOp; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1329 | switch (op) { |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1330 | case Token::EQ: |
| 1331 | *outLeftType = &left; |
| 1332 | *outRightType = &left; |
| 1333 | *outResultType = &left; |
| 1334 | return right.canCoerceTo(left); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1335 | case Token::EQEQ: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1336 | case Token::NEQ: |
Ethan Nicholas | 2346300 | 2018-03-28 15:16:15 -0400 | [diff] [blame] | 1337 | if (right.canCoerceTo(left)) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1338 | *outLeftType = &left; |
Ethan Nicholas | 2346300 | 2018-03-28 15:16:15 -0400 | [diff] [blame] | 1339 | *outRightType = &left; |
| 1340 | *outResultType = context.fBool_Type.get(); |
| 1341 | return true; |
| 1342 | } if (left.canCoerceTo(right)) { |
| 1343 | *outLeftType = &right; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1344 | *outRightType = &right; |
| 1345 | *outResultType = context.fBool_Type.get(); |
| 1346 | return true; |
| 1347 | } |
Ethan Nicholas | 2346300 | 2018-03-28 15:16:15 -0400 | [diff] [blame] | 1348 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1349 | case Token::LT: // fall through |
| 1350 | case Token::GT: // fall through |
| 1351 | case Token::LTEQ: // fall through |
| 1352 | case Token::GTEQ: |
| 1353 | isLogical = true; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1354 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1355 | break; |
| 1356 | case Token::LOGICALOR: // fall through |
| 1357 | case Token::LOGICALAND: // fall through |
| 1358 | case Token::LOGICALXOR: // fall through |
| 1359 | case Token::LOGICALOREQ: // fall through |
| 1360 | case Token::LOGICALANDEQ: // fall through |
| 1361 | case Token::LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1362 | *outLeftType = context.fBool_Type.get(); |
| 1363 | *outRightType = context.fBool_Type.get(); |
| 1364 | *outResultType = context.fBool_Type.get(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1365 | return left.canCoerceTo(*context.fBool_Type) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1366 | right.canCoerceTo(*context.fBool_Type); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1367 | case Token::STAREQ: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1368 | if (left.kind() == Type::kScalar_Kind) { |
| 1369 | *outLeftType = &left; |
| 1370 | *outRightType = &left; |
| 1371 | *outResultType = &left; |
| 1372 | return right.canCoerceTo(left); |
| 1373 | } |
| 1374 | // fall through |
| 1375 | case Token::STAR: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1376 | if (is_matrix_multiply(left, right)) { |
| 1377 | // determine final component type |
| 1378 | if (determine_binary_type(context, Token::STAR, left.componentType(), |
| 1379 | right.componentType(), outLeftType, outRightType, |
| 1380 | outResultType, false)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1381 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1382 | left.rows()); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1383 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1384 | right.rows()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1385 | int leftColumns = left.columns(); |
| 1386 | int leftRows = left.rows(); |
| 1387 | int rightColumns; |
| 1388 | int rightRows; |
| 1389 | if (right.kind() == Type::kVector_Kind) { |
| 1390 | // matrix * vector treats the vector as a column vector, so we need to |
| 1391 | // transpose it |
| 1392 | rightColumns = right.rows(); |
| 1393 | rightRows = right.columns(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1394 | SkASSERT(rightColumns == 1); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1395 | } else { |
| 1396 | rightColumns = right.columns(); |
| 1397 | rightRows = right.rows(); |
| 1398 | } |
| 1399 | if (rightColumns > 1) { |
| 1400 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 1401 | leftRows); |
| 1402 | } else { |
| 1403 | // result was a column vector, transpose it back to a row |
| 1404 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 1405 | rightColumns); |
| 1406 | } |
| 1407 | return leftColumns == rightRows; |
| 1408 | } else { |
| 1409 | return false; |
| 1410 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1411 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1412 | isLogical = false; |
| 1413 | validMatrixOrVectorOp = true; |
| 1414 | break; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1415 | case Token::PLUSEQ: |
| 1416 | case Token::MINUSEQ: |
| 1417 | case Token::SLASHEQ: |
| 1418 | case Token::PERCENTEQ: |
| 1419 | case Token::SHLEQ: |
| 1420 | case Token::SHREQ: |
| 1421 | if (left.kind() == Type::kScalar_Kind) { |
| 1422 | *outLeftType = &left; |
| 1423 | *outRightType = &left; |
| 1424 | *outResultType = &left; |
| 1425 | return right.canCoerceTo(left); |
| 1426 | } |
| 1427 | // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1428 | case Token::PLUS: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1429 | case Token::MINUS: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1430 | case Token::SLASH: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1431 | isLogical = false; |
| 1432 | validMatrixOrVectorOp = true; |
| 1433 | break; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1434 | case Token::COMMA: |
| 1435 | *outLeftType = &left; |
| 1436 | *outRightType = &right; |
| 1437 | *outResultType = &right; |
| 1438 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1439 | default: |
| 1440 | isLogical = false; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1441 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1442 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1443 | bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1444 | if (left.kind() == Type::kScalar_Kind && right.kind() == Type::kScalar_Kind && |
| 1445 | right.canCoerceTo(left)) { |
| 1446 | if (left.priority() > right.priority()) { |
| 1447 | *outLeftType = &left; |
| 1448 | *outRightType = &left; |
| 1449 | } else { |
| 1450 | *outLeftType = &right; |
| 1451 | *outRightType = &right; |
| 1452 | } |
| 1453 | if (isLogical) { |
| 1454 | *outResultType = context.fBool_Type.get(); |
| 1455 | } else { |
| 1456 | *outResultType = &left; |
| 1457 | } |
| 1458 | return true; |
| 1459 | } |
| 1460 | if (right.canCoerceTo(left) && isVectorOrMatrix && validMatrixOrVectorOp) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1461 | *outLeftType = &left; |
| 1462 | *outRightType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1463 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1464 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1465 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1466 | *outResultType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1467 | } |
| 1468 | return true; |
| 1469 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1470 | if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1471 | (right.kind() == Type::kScalar_Kind)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1472 | if (determine_binary_type(context, op, left.componentType(), right, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1473 | outRightType, outResultType, false)) { |
| 1474 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1475 | if (!isLogical) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1476 | *outResultType = &(*outResultType)->toCompound(context, left.columns(), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1477 | left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1478 | } |
| 1479 | return true; |
| 1480 | } |
| 1481 | return false; |
| 1482 | } |
| 1483 | if (tryFlipped) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1484 | return determine_binary_type(context, op, right, left, outRightType, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1485 | outResultType, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1486 | } |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1490 | static std::unique_ptr<Expression> short_circuit_boolean(const Context& context, |
| 1491 | const Expression& left, |
| 1492 | Token::Kind op, |
| 1493 | const Expression& right) { |
| 1494 | SkASSERT(left.fKind == Expression::kBoolLiteral_Kind); |
| 1495 | bool leftVal = ((BoolLiteral&) left).fValue; |
| 1496 | if (op == Token::LOGICALAND) { |
| 1497 | // (true && expr) -> (expr) and (false && expr) -> (false) |
| 1498 | return leftVal ? right.clone() |
| 1499 | : std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, false)); |
| 1500 | } else if (op == Token::LOGICALOR) { |
| 1501 | // (true || expr) -> (true) and (false || expr) -> (expr) |
| 1502 | return leftVal ? std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, true)) |
| 1503 | : right.clone(); |
Noah Lavine | 334d0ba | 2019-12-18 23:03:49 -0500 | [diff] [blame] | 1504 | } else if (op == Token::LOGICALXOR) { |
| 1505 | // (true ^^ expr) -> !(expr) and (false ^^ expr) -> (expr) |
| 1506 | return leftVal ? std::unique_ptr<Expression>(new PrefixExpression(Token::LOGICALNOT, |
| 1507 | right.clone())) |
| 1508 | : right.clone(); |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1509 | } else { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1510 | return nullptr; |
| 1511 | } |
| 1512 | } |
| 1513 | |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1514 | std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left, |
| 1515 | Token::Kind op, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1516 | const Expression& right) const { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1517 | // If the left side is a constant boolean literal, the right side does not need to be constant |
| 1518 | // for short circuit optimizations to allow the constant to be folded. |
| 1519 | if (left.fKind == Expression::kBoolLiteral_Kind && !right.isConstant()) { |
| 1520 | return short_circuit_boolean(fContext, left, op, right); |
| 1521 | } else if (right.fKind == Expression::kBoolLiteral_Kind && !left.isConstant()) { |
| 1522 | // There aren't side effects in SKSL within expressions, so (left OP right) is equivalent to |
| 1523 | // (right OP left) for short-circuit optimizations |
| 1524 | return short_circuit_boolean(fContext, right, op, left); |
| 1525 | } |
| 1526 | |
| 1527 | // Other than the short-circuit cases above, constant folding requires both sides to be constant |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1528 | if (!left.isConstant() || !right.isConstant()) { |
| 1529 | return nullptr; |
| 1530 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1531 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 1532 | // precision to calculate the results and hope the result makes sense. The plan is to move the |
| 1533 | // Skia caps into SkSL, so we have access to all of them including the precisions of the various |
| 1534 | // types, which will let us be more intelligent about this. |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1535 | if (left.fKind == Expression::kBoolLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1536 | right.fKind == Expression::kBoolLiteral_Kind) { |
| 1537 | bool leftVal = ((BoolLiteral&) left).fValue; |
| 1538 | bool rightVal = ((BoolLiteral&) right).fValue; |
| 1539 | bool result; |
| 1540 | switch (op) { |
| 1541 | case Token::LOGICALAND: result = leftVal && rightVal; break; |
| 1542 | case Token::LOGICALOR: result = leftVal || rightVal; break; |
| 1543 | case Token::LOGICALXOR: result = leftVal ^ rightVal; break; |
| 1544 | default: return nullptr; |
| 1545 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1546 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fOffset, result)); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1547 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1548 | #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \ |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1549 | leftVal op rightVal)) |
| 1550 | if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) { |
| 1551 | int64_t leftVal = ((IntLiteral&) left).fValue; |
| 1552 | int64_t rightVal = ((IntLiteral&) right).fValue; |
| 1553 | switch (op) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1554 | case Token::PLUS: return RESULT(Int, +); |
| 1555 | case Token::MINUS: return RESULT(Int, -); |
| 1556 | case Token::STAR: return RESULT(Int, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1557 | case Token::SLASH: |
| 1558 | if (rightVal) { |
| 1559 | return RESULT(Int, /); |
| 1560 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1561 | fErrors.error(right.fOffset, "division by zero"); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1562 | return nullptr; |
Ethan Nicholas | 2503ab6 | 2017-01-05 10:44:25 -0500 | [diff] [blame] | 1563 | case Token::PERCENT: |
| 1564 | if (rightVal) { |
| 1565 | return RESULT(Int, %); |
| 1566 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1567 | fErrors.error(right.fOffset, "division by zero"); |
Ethan Nicholas | 2503ab6 | 2017-01-05 10:44:25 -0500 | [diff] [blame] | 1568 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1569 | case Token::BITWISEAND: return RESULT(Int, &); |
| 1570 | case Token::BITWISEOR: return RESULT(Int, |); |
| 1571 | case Token::BITWISEXOR: return RESULT(Int, ^); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1572 | case Token::EQEQ: return RESULT(Bool, ==); |
| 1573 | case Token::NEQ: return RESULT(Bool, !=); |
| 1574 | case Token::GT: return RESULT(Bool, >); |
| 1575 | case Token::GTEQ: return RESULT(Bool, >=); |
| 1576 | case Token::LT: return RESULT(Bool, <); |
| 1577 | case Token::LTEQ: return RESULT(Bool, <=); |
Ethan Nicholas | feba68a | 2019-06-10 09:56:29 -0400 | [diff] [blame] | 1578 | case Token::SHL: |
| 1579 | if (rightVal >= 0 && rightVal <= 31) { |
| 1580 | return RESULT(Int, <<); |
| 1581 | } |
| 1582 | fErrors.error(right.fOffset, "shift value out of range"); |
| 1583 | return nullptr; |
| 1584 | case Token::SHR: |
| 1585 | if (rightVal >= 0 && rightVal <= 31) { |
| 1586 | return RESULT(Int, >>); |
| 1587 | } |
| 1588 | fErrors.error(right.fOffset, "shift value out of range"); |
| 1589 | return nullptr; |
| 1590 | |
| 1591 | default: |
| 1592 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1593 | } |
| 1594 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1595 | if (left.fKind == Expression::kFloatLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1596 | right.fKind == Expression::kFloatLiteral_Kind) { |
| 1597 | double leftVal = ((FloatLiteral&) left).fValue; |
| 1598 | double rightVal = ((FloatLiteral&) right).fValue; |
| 1599 | switch (op) { |
| 1600 | case Token::PLUS: return RESULT(Float, +); |
| 1601 | case Token::MINUS: return RESULT(Float, -); |
| 1602 | case Token::STAR: return RESULT(Float, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1603 | case Token::SLASH: |
| 1604 | if (rightVal) { |
| 1605 | return RESULT(Float, /); |
| 1606 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1607 | fErrors.error(right.fOffset, "division by zero"); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1608 | return nullptr; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1609 | case Token::EQEQ: return RESULT(Bool, ==); |
| 1610 | case Token::NEQ: return RESULT(Bool, !=); |
| 1611 | case Token::GT: return RESULT(Bool, >); |
| 1612 | case Token::GTEQ: return RESULT(Bool, >=); |
| 1613 | case Token::LT: return RESULT(Bool, <); |
| 1614 | case Token::LTEQ: return RESULT(Bool, <=); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1615 | default: return nullptr; |
| 1616 | } |
| 1617 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1618 | if (left.fType.kind() == Type::kVector_Kind && left.fType.componentType().isFloat() && |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1619 | left.fType == right.fType) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1620 | std::vector<std::unique_ptr<Expression>> args; |
Ethan Nicholas | 4cf5fd9 | 2019-06-10 16:15:56 -0400 | [diff] [blame] | 1621 | #define RETURN_VEC_COMPONENTWISE_RESULT(op) \ |
| 1622 | for (int i = 0; i < left.fType.columns(); i++) { \ |
| 1623 | float value = left.getFVecComponent(i) op \ |
| 1624 | right.getFVecComponent(i); \ |
| 1625 | args.emplace_back(new FloatLiteral(fContext, -1, value)); \ |
| 1626 | } \ |
| 1627 | return std::unique_ptr<Expression>(new Constructor(-1, left.fType, \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1628 | std::move(args))) |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1629 | switch (op) { |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1630 | case Token::EQEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1631 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1, |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1632 | left.compareConstant(fContext, right))); |
| 1633 | case Token::NEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1634 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1, |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1635 | !left.compareConstant(fContext, right))); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1636 | case Token::PLUS: RETURN_VEC_COMPONENTWISE_RESULT(+); |
| 1637 | case Token::MINUS: RETURN_VEC_COMPONENTWISE_RESULT(-); |
| 1638 | case Token::STAR: RETURN_VEC_COMPONENTWISE_RESULT(*); |
Ethan Nicholas | 4cf5fd9 | 2019-06-10 16:15:56 -0400 | [diff] [blame] | 1639 | case Token::SLASH: |
| 1640 | for (int i = 0; i < left.fType.columns(); i++) { |
| 1641 | SKSL_FLOAT rvalue = right.getFVecComponent(i); |
| 1642 | if (rvalue == 0.0) { |
| 1643 | fErrors.error(right.fOffset, "division by zero"); |
| 1644 | return nullptr; |
| 1645 | } |
| 1646 | float value = left.getFVecComponent(i) / rvalue; |
| 1647 | args.emplace_back(new FloatLiteral(fContext, -1, value)); |
| 1648 | } |
| 1649 | return std::unique_ptr<Expression>(new Constructor(-1, left.fType, |
| 1650 | std::move(args))); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1651 | default: return nullptr; |
| 1652 | } |
| 1653 | } |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1654 | if (left.fType.kind() == Type::kMatrix_Kind && |
| 1655 | right.fType.kind() == Type::kMatrix_Kind && |
| 1656 | left.fKind == right.fKind) { |
| 1657 | switch (op) { |
| 1658 | case Token::EQEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1659 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1, |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1660 | left.compareConstant(fContext, right))); |
| 1661 | case Token::NEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1662 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1, |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1663 | !left.compareConstant(fContext, right))); |
| 1664 | default: |
| 1665 | return nullptr; |
| 1666 | } |
| 1667 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1668 | #undef RESULT |
| 1669 | return nullptr; |
| 1670 | } |
| 1671 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1672 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) { |
| 1673 | SkASSERT(expression.fKind == ASTNode::Kind::kBinary); |
| 1674 | auto iter = expression.begin(); |
| 1675 | std::unique_ptr<Expression> left = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1676 | if (!left) { |
| 1677 | return nullptr; |
| 1678 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1679 | std::unique_ptr<Expression> right = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1680 | if (!right) { |
| 1681 | return nullptr; |
| 1682 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1683 | const Type* leftType; |
| 1684 | const Type* rightType; |
| 1685 | const Type* resultType; |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1686 | const Type* rawLeftType; |
| 1687 | if (left->fKind == Expression::kIntLiteral_Kind && right->fType.isInteger()) { |
| 1688 | rawLeftType = &right->fType; |
| 1689 | } else { |
| 1690 | rawLeftType = &left->fType; |
| 1691 | } |
| 1692 | const Type* rawRightType; |
| 1693 | if (right->fKind == Expression::kIntLiteral_Kind && left->fType.isInteger()) { |
| 1694 | rawRightType = &left->fType; |
| 1695 | } else { |
| 1696 | rawRightType = &right->fType; |
| 1697 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1698 | Token::Kind op = expression.getToken().fKind; |
| 1699 | if (!determine_binary_type(fContext, op, *rawLeftType, *rawRightType, &leftType, &rightType, |
| 1700 | &resultType, !Compiler::IsAssignment(op))) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1701 | fErrors.error(expression.fOffset, String("type mismatch: '") + |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1702 | Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1703 | "' cannot operate on '" + left->fType.displayName() + |
| 1704 | "', '" + right->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1705 | return nullptr; |
| 1706 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1707 | if (Compiler::IsAssignment(op)) { |
| 1708 | this->setRefKind(*left, op != Token::EQ ? VariableReference::kReadWrite_RefKind : |
| 1709 | VariableReference::kWrite_RefKind); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1710 | } |
| 1711 | left = this->coerce(std::move(left), *leftType); |
| 1712 | right = this->coerce(std::move(right), *rightType); |
| 1713 | if (!left || !right) { |
| 1714 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1715 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1716 | std::unique_ptr<Expression> result = this->constantFold(*left.get(), op, *right.get()); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1717 | if (!result) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1718 | result = std::unique_ptr<Expression>(new BinaryExpression(expression.fOffset, |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1719 | std::move(left), |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1720 | op, |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1721 | std::move(right), |
| 1722 | *resultType)); |
| 1723 | } |
| 1724 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1727 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) { |
| 1728 | SkASSERT(node.fKind == ASTNode::Kind::kTernary); |
| 1729 | auto iter = node.begin(); |
| 1730 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1731 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1732 | if (!test) { |
| 1733 | return nullptr; |
| 1734 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1735 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1736 | if (!ifTrue) { |
| 1737 | return nullptr; |
| 1738 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1739 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1740 | if (!ifFalse) { |
| 1741 | return nullptr; |
| 1742 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1743 | const Type* trueType; |
| 1744 | const Type* falseType; |
| 1745 | const Type* resultType; |
| 1746 | if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType, |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 1747 | &falseType, &resultType, true) || trueType != falseType) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1748 | fErrors.error(node.fOffset, "ternary operator result mismatch: '" + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1749 | ifTrue->fType.displayName() + "', '" + |
| 1750 | ifFalse->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1751 | return nullptr; |
| 1752 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1753 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 1754 | if (!ifTrue) { |
| 1755 | return nullptr; |
| 1756 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1757 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 1758 | if (!ifFalse) { |
| 1759 | return nullptr; |
| 1760 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1761 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 1762 | // static boolean test, just return one of the branches |
| 1763 | if (((BoolLiteral&) *test).fValue) { |
| 1764 | return ifTrue; |
| 1765 | } else { |
| 1766 | return ifFalse; |
| 1767 | } |
| 1768 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1769 | return std::unique_ptr<Expression>(new TernaryExpression(node.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1770 | std::move(test), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1771 | std::move(ifTrue), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1772 | std::move(ifFalse))); |
| 1773 | } |
| 1774 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1775 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1776 | const FunctionDeclaration& function, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1777 | std::vector<std::unique_ptr<Expression>> arguments) { |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1778 | if (function.fBuiltin) { |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame^] | 1779 | auto found = fIntrinsics->find(function.declaration()); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1780 | if (found != fIntrinsics->end() && !found->second.second) { |
| 1781 | found->second.second = true; |
| 1782 | const FunctionDeclaration* old = fCurrentFunction; |
| 1783 | fCurrentFunction = nullptr; |
| 1784 | this->convertFunction(*((FunctionDefinition&) *found->second.first).fSource); |
| 1785 | fCurrentFunction = old; |
| 1786 | } |
| 1787 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1788 | if (function.fParameters.size() != arguments.size()) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1789 | String msg = "call to '" + function.fName + "' expected " + |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1790 | to_string((uint64_t) function.fParameters.size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1791 | " argument"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1792 | if (function.fParameters.size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1793 | msg += "s"; |
| 1794 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1795 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1796 | fErrors.error(offset, msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1797 | return nullptr; |
| 1798 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1799 | std::vector<const Type*> types; |
| 1800 | const Type* returnType; |
| 1801 | if (!function.determineFinalTypes(arguments, &types, &returnType)) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1802 | String msg = "no match for " + function.fName + "("; |
| 1803 | String separator; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1804 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1805 | msg += separator; |
| 1806 | separator = ", "; |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1807 | msg += arguments[i]->fType.displayName(); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1808 | } |
| 1809 | msg += ")"; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1810 | fErrors.error(offset, msg); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1811 | return nullptr; |
| 1812 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1813 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1814 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1815 | if (!arguments[i]) { |
| 1816 | return nullptr; |
| 1817 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1818 | if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 1819 | this->setRefKind(*arguments[i], |
| 1820 | function.fParameters[i]->fModifiers.fFlags & Modifiers::kIn_Flag ? |
| 1821 | VariableReference::kReadWrite_RefKind : |
| 1822 | VariableReference::kPointer_RefKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1825 | return std::unique_ptr<FunctionCall>(new FunctionCall(offset, *returnType, function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1826 | std::move(arguments))); |
| 1827 | } |
| 1828 | |
| 1829 | /** |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1830 | * Determines the cost of coercing the arguments of a function to the required types. Cost has no |
| 1831 | * particular meaning other than "lower costs are preferred". Returns INT_MAX if the call is not |
| 1832 | * valid. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1833 | */ |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1834 | int IRGenerator::callCost(const FunctionDeclaration& function, |
| 1835 | const std::vector<std::unique_ptr<Expression>>& arguments) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1836 | if (function.fParameters.size() != arguments.size()) { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1837 | return INT_MAX; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1838 | } |
| 1839 | int total = 0; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1840 | std::vector<const Type*> types; |
| 1841 | const Type* ignored; |
| 1842 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1843 | return INT_MAX; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1844 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1845 | for (size_t i = 0; i < arguments.size(); i++) { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1846 | int cost = arguments[i]->coercionCost(*types[i]); |
| 1847 | if (cost != INT_MAX) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1848 | total += cost; |
| 1849 | } else { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1850 | return INT_MAX; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1851 | } |
| 1852 | } |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1853 | return total; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1856 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1857 | std::unique_ptr<Expression> functionValue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1858 | std::vector<std::unique_ptr<Expression>> arguments) { |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1859 | switch (functionValue->fKind) { |
| 1860 | case Expression::kTypeReference_Kind: |
| 1861 | return this->convertConstructor(offset, |
| 1862 | ((TypeReference&) *functionValue).fValue, |
| 1863 | std::move(arguments)); |
| 1864 | case Expression::kExternalValue_Kind: { |
| 1865 | ExternalValue* v = ((ExternalValueReference&) *functionValue).fValue; |
| 1866 | if (!v->canCall()) { |
| 1867 | fErrors.error(offset, "this external value is not a function"); |
| 1868 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1869 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1870 | int count = v->callParameterCount(); |
| 1871 | if (count != (int) arguments.size()) { |
| 1872 | fErrors.error(offset, "external function expected " + to_string(count) + |
| 1873 | " arguments, but found " + to_string((int) arguments.size())); |
| 1874 | return nullptr; |
| 1875 | } |
| 1876 | static constexpr int PARAMETER_MAX = 16; |
| 1877 | SkASSERT(count < PARAMETER_MAX); |
| 1878 | const Type* types[PARAMETER_MAX]; |
| 1879 | v->getCallParameterTypes(types); |
| 1880 | for (int i = 0; i < count; ++i) { |
| 1881 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
| 1882 | if (!arguments[i]) { |
| 1883 | return nullptr; |
| 1884 | } |
| 1885 | } |
| 1886 | return std::unique_ptr<Expression>(new ExternalFunctionCall(offset, v->callReturnType(), |
| 1887 | v, std::move(arguments))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1888 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1889 | case Expression::kFunctionReference_Kind: { |
| 1890 | FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 1891 | int bestCost = INT_MAX; |
| 1892 | const FunctionDeclaration* best = nullptr; |
| 1893 | if (ref->fFunctions.size() > 1) { |
| 1894 | for (const auto& f : ref->fFunctions) { |
| 1895 | int cost = this->callCost(*f, arguments); |
| 1896 | if (cost < bestCost) { |
| 1897 | bestCost = cost; |
| 1898 | best = f; |
| 1899 | } |
| 1900 | } |
| 1901 | if (best) { |
| 1902 | return this->call(offset, *best, std::move(arguments)); |
| 1903 | } |
| 1904 | String msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 1905 | String separator; |
| 1906 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1907 | msg += separator; |
| 1908 | separator = ", "; |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1909 | msg += arguments[i]->fType.displayName(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1910 | } |
| 1911 | msg += ")"; |
| 1912 | fErrors.error(offset, msg); |
| 1913 | return nullptr; |
| 1914 | } |
| 1915 | return this->call(offset, *ref->fFunctions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1916 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1917 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1918 | fErrors.error(offset, "not a function"); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1919 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1920 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1923 | std::unique_ptr<Expression> IRGenerator::convertNumberConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1924 | int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1925 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1926 | std::vector<std::unique_ptr<Expression>> args) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1927 | SkASSERT(type.isNumber()); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1928 | if (args.size() != 1) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1929 | fErrors.error(offset, "invalid arguments to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1930 | "' constructor, (expected exactly 1 argument, but found " + |
| 1931 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1932 | return nullptr; |
| 1933 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1934 | if (type == args[0]->fType) { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1935 | return std::move(args[0]); |
| 1936 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1937 | if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kFloatLiteral_Kind) { |
| 1938 | double value = ((FloatLiteral&) *args[0]).fValue; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1939 | return std::unique_ptr<Expression>(new FloatLiteral(offset, value, &type)); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1940 | } |
| 1941 | if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kIntLiteral_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1942 | int64_t value = ((IntLiteral&) *args[0]).fValue; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1943 | return std::unique_ptr<Expression>(new FloatLiteral(offset, (double) value, &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1944 | } |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1945 | if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type || |
| 1946 | type == *fContext.fUInt_Type)) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1947 | return std::unique_ptr<Expression>(new IntLiteral(offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1948 | ((IntLiteral&) *args[0]).fValue, |
| 1949 | &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1950 | } |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1951 | if (args[0]->fType == *fContext.fBool_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1952 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0)); |
| 1953 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1954 | return std::unique_ptr<Expression>( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1955 | new TernaryExpression(offset, std::move(args[0]), |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1956 | this->coerce(std::move(one), type), |
| 1957 | this->coerce(std::move(zero), |
| 1958 | type))); |
| 1959 | } |
| 1960 | if (!args[0]->fType.isNumber()) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1961 | fErrors.error(offset, "invalid argument to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1962 | "' constructor (expected a number or bool, but found '" + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1963 | args[0]->fType.displayName() + "')"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1964 | return nullptr; |
| 1965 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1966 | return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args))); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | int component_count(const Type& type) { |
| 1970 | switch (type.kind()) { |
| 1971 | case Type::kVector_Kind: |
| 1972 | return type.columns(); |
| 1973 | case Type::kMatrix_Kind: |
| 1974 | return type.columns() * type.rows(); |
| 1975 | default: |
| 1976 | return 1; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1981 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1982 | const Type& type, |
| 1983 | std::vector<std::unique_ptr<Expression>> args) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1984 | SkASSERT(type.kind() == Type::kVector_Kind || type.kind() == Type::kMatrix_Kind); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1985 | if (type.kind() == Type::kMatrix_Kind && args.size() == 1 && |
| 1986 | args[0]->fType.kind() == Type::kMatrix_Kind) { |
| 1987 | // matrix from matrix is always legal |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1988 | return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args))); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1989 | } |
| 1990 | int actual = 0; |
| 1991 | int expected = type.rows() * type.columns(); |
| 1992 | if (args.size() != 1 || expected != component_count(args[0]->fType) || |
| 1993 | type.componentType().isNumber() != args[0]->fType.componentType().isNumber()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1994 | for (size_t i = 0; i < args.size(); i++) { |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1995 | if (args[i]->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 1996 | if (type.componentType().isNumber() != |
| 1997 | args[i]->fType.componentType().isNumber()) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1998 | fErrors.error(offset, "'" + args[i]->fType.displayName() + "' is not a valid " |
| 1999 | "parameter to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2000 | "' constructor"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2001 | return nullptr; |
| 2002 | } |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2003 | actual += args[i]->fType.columns(); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2004 | } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
| 2005 | actual += 1; |
| 2006 | if (type.kind() != Type::kScalar_Kind) { |
| 2007 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
| 2008 | if (!args[i]) { |
| 2009 | return nullptr; |
| 2010 | } |
| 2011 | } |
| 2012 | } else { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2013 | fErrors.error(offset, "'" + args[i]->fType.displayName() + "' is not a valid " |
| 2014 | "parameter to '" + type.displayName() + "' constructor"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2015 | return nullptr; |
| 2016 | } |
| 2017 | } |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2018 | if (actual != 1 && actual != expected) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2019 | fErrors.error(offset, "invalid arguments to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2020 | "' constructor (expected " + to_string(expected) + |
| 2021 | " scalars, but found " + to_string(actual) + ")"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2022 | return nullptr; |
| 2023 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2024 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2025 | return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2028 | std::unique_ptr<Expression> IRGenerator::convertConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2029 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2030 | const Type& type, |
| 2031 | std::vector<std::unique_ptr<Expression>> args) { |
| 2032 | // FIXME: add support for structs |
| 2033 | Type::Kind kind = type.kind(); |
| 2034 | if (args.size() == 1 && args[0]->fType == type) { |
| 2035 | // argument is already the right type, just return it |
| 2036 | return std::move(args[0]); |
| 2037 | } |
| 2038 | if (type.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2039 | return this->convertNumberConstructor(offset, type, std::move(args)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2040 | } else if (kind == Type::kArray_Kind) { |
| 2041 | const Type& base = type.componentType(); |
| 2042 | for (size_t i = 0; i < args.size(); i++) { |
| 2043 | args[i] = this->coerce(std::move(args[i]), base); |
| 2044 | if (!args[i]) { |
| 2045 | return nullptr; |
| 2046 | } |
| 2047 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2048 | return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args))); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2049 | } else if (kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2050 | return this->convertCompoundConstructor(offset, type, std::move(args)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2051 | } else { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2052 | fErrors.error(offset, "cannot construct '" + type.displayName() + "'"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2053 | return nullptr; |
| 2054 | } |
| 2055 | } |
| 2056 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2057 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) { |
| 2058 | SkASSERT(expression.fKind == ASTNode::Kind::kPrefix); |
| 2059 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2060 | if (!base) { |
| 2061 | return nullptr; |
| 2062 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2063 | switch (expression.getToken().fKind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2064 | case Token::PLUS: |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2065 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind && |
| 2066 | base->fType != *fContext.fFloatLiteral_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2067 | fErrors.error(expression.fOffset, |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2068 | "'+' cannot operate on '" + base->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2069 | return nullptr; |
| 2070 | } |
| 2071 | return base; |
| 2072 | case Token::MINUS: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2073 | if (base->fKind == Expression::kIntLiteral_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2074 | return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2075 | -((IntLiteral&) *base).fValue)); |
| 2076 | } |
| 2077 | if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 2078 | double value = -((FloatLiteral&) *base).fValue; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2079 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fOffset, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2080 | value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2081 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2082 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
| 2083 | fErrors.error(expression.fOffset, |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2084 | "'-' cannot operate on '" + base->fType.displayName() + "'"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2085 | return nullptr; |
| 2086 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2087 | return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base))); |
| 2088 | case Token::PLUSPLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2089 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2090 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2091 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2092 | "' cannot operate on '" + base->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2093 | return nullptr; |
| 2094 | } |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2095 | this->setRefKind(*base, VariableReference::kReadWrite_RefKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2096 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2097 | case Token::MINUSMINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2098 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2099 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2100 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2101 | "' cannot operate on '" + base->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2102 | return nullptr; |
| 2103 | } |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2104 | this->setRefKind(*base, VariableReference::kReadWrite_RefKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2105 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2106 | case Token::LOGICALNOT: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2107 | if (base->fType != *fContext.fBool_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2108 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2109 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2110 | "' cannot operate on '" + base->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2111 | return nullptr; |
| 2112 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 2113 | if (base->fKind == Expression::kBoolLiteral_Kind) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2114 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fOffset, |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 2115 | !((BoolLiteral&) *base).fValue)); |
| 2116 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2117 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2118 | case Token::BITWISENOT: |
Brian Osman | 73fb39c | 2019-09-24 16:22:55 -0400 | [diff] [blame] | 2119 | if (base->fType != *fContext.fInt_Type && base->fType != *fContext.fUInt_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2120 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2121 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2122 | "' cannot operate on '" + base->fType.displayName() + "'"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2123 | return nullptr; |
| 2124 | } |
| 2125 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2126 | default: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2127 | ABORT("unsupported prefix operator\n"); |
| 2128 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2129 | return std::unique_ptr<Expression>(new PrefixExpression(expression.getToken().fKind, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2130 | std::move(base))); |
| 2131 | } |
| 2132 | |
| 2133 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2134 | const ASTNode& index) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2135 | if (base->fKind == Expression::kTypeReference_Kind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2136 | if (index.fKind == ASTNode::Kind::kInt) { |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2137 | const Type& oldType = ((TypeReference&) *base).fValue; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2138 | SKSL_INT size = index.getInt(); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 2139 | Type* newType = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>( |
| 2140 | new Type(oldType.name() + "[" + to_string(size) + "]", |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2141 | Type::kArray_Kind, oldType, size))); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2142 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset, |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2143 | *newType)); |
| 2144 | |
| 2145 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2146 | fErrors.error(base->fOffset, "array size must be a constant"); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2147 | return nullptr; |
| 2148 | } |
| 2149 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2150 | if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind && |
| 2151 | base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2152 | fErrors.error(base->fOffset, "expected array, but found '" + base->fType.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2153 | "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2154 | return nullptr; |
| 2155 | } |
| 2156 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 2157 | if (!converted) { |
| 2158 | return nullptr; |
| 2159 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2160 | if (converted->fType != *fContext.fUInt_Type) { |
| 2161 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 2162 | if (!converted) { |
| 2163 | return nullptr; |
| 2164 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2165 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2166 | return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2167 | std::move(converted))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2171 | StringFragment field) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 2172 | if (base->fKind == Expression::kExternalValue_Kind) { |
| 2173 | ExternalValue& ev = *((ExternalValueReference&) *base).fValue; |
| 2174 | ExternalValue* result = ev.getChild(String(field).c_str()); |
| 2175 | if (!result) { |
| 2176 | fErrors.error(base->fOffset, "external value does not have a child named '" + field + |
| 2177 | "'"); |
| 2178 | return nullptr; |
| 2179 | } |
| 2180 | return std::unique_ptr<Expression>(new ExternalValueReference(base->fOffset, result)); |
| 2181 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2182 | auto fields = base->fType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2183 | for (size_t i = 0; i < fields.size(); i++) { |
| 2184 | if (fields[i].fName == field) { |
| 2185 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 2186 | } |
| 2187 | } |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2188 | fErrors.error(base->fOffset, "type '" + base->fType.displayName() + "' does not have a " |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame^] | 2189 | "field named '" + field + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2190 | return nullptr; |
| 2191 | } |
| 2192 | |
| 2193 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2194 | StringFragment fields) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2195 | if (base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2196 | fErrors.error(base->fOffset, "cannot swizzle type '" + base->fType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2197 | return nullptr; |
| 2198 | } |
| 2199 | std::vector<int> swizzleComponents; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2200 | for (size_t i = 0; i < fields.fLength; i++) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 2201 | switch (fields[i]) { |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2202 | case '0': |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2203 | swizzleComponents.push_back(SKSL_SWIZZLE_0); |
| 2204 | break; |
| 2205 | case '1': |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2206 | swizzleComponents.push_back(SKSL_SWIZZLE_1); |
| 2207 | break; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2208 | case 'x': |
| 2209 | case 'r': |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2210 | case 's': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2211 | case 'L': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2212 | swizzleComponents.push_back(0); |
| 2213 | break; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2214 | case 'y': |
| 2215 | case 'g': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2216 | case 't': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2217 | case 'T': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2218 | if (base->fType.columns() >= 2) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2219 | swizzleComponents.push_back(1); |
| 2220 | break; |
| 2221 | } |
| 2222 | // fall through |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2223 | case 'z': |
| 2224 | case 'b': |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2225 | case 'p': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2226 | case 'R': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2227 | if (base->fType.columns() >= 3) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2228 | swizzleComponents.push_back(2); |
| 2229 | break; |
| 2230 | } |
| 2231 | // fall through |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2232 | case 'w': |
| 2233 | case 'a': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2234 | case 'q': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2235 | case 'B': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2236 | if (base->fType.columns() >= 4) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2237 | swizzleComponents.push_back(3); |
| 2238 | break; |
| 2239 | } |
| 2240 | // fall through |
| 2241 | default: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2242 | fErrors.error(base->fOffset, String::printf("invalid swizzle component '%c'", |
| 2243 | fields[i])); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2244 | return nullptr; |
| 2245 | } |
| 2246 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2247 | SkASSERT(swizzleComponents.size() > 0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2248 | if (swizzleComponents.size() > 4) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2249 | fErrors.error(base->fOffset, "too many components in swizzle mask '" + fields + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2250 | return nullptr; |
| 2251 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2252 | return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2255 | std::unique_ptr<Expression> IRGenerator::getCap(int offset, String name) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2256 | auto found = fCapsMap.find(name); |
| 2257 | if (found == fCapsMap.end()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2258 | fErrors.error(offset, "unknown capability flag '" + name + "'"); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 2259 | return nullptr; |
| 2260 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2261 | String fullName = "sk_Caps." + name; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2262 | return std::unique_ptr<Expression>(new Setting(offset, fullName, |
| 2263 | found->second.literal(fContext, offset))); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2264 | } |
| 2265 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 2266 | std::unique_ptr<Expression> IRGenerator::getArg(int offset, String name) const { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2267 | auto found = fSettings->fArgs.find(name); |
| 2268 | if (found == fSettings->fArgs.end()) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2269 | return nullptr; |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 2270 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2271 | String fullName = "sk_Args." + name; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2272 | return std::unique_ptr<Expression>(new Setting(offset, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2273 | fullName, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2274 | found->second.literal(fContext, offset))); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 2275 | } |
| 2276 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2277 | std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type, |
| 2278 | StringFragment field) { |
| 2279 | std::unique_ptr<Expression> result; |
| 2280 | for (const auto& e : *fProgramElements) { |
| 2281 | if (e->fKind == ProgramElement::kEnum_Kind && type.name() == ((Enum&) *e).fTypeName) { |
| 2282 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 2283 | fSymbolTable = ((Enum&) *e).fSymbols; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2284 | result = convertIdentifier(ASTNode(&fFile->fNodes, offset, ASTNode::Kind::kIdentifier, |
| 2285 | field)); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 2286 | SkASSERT(result->fKind == Expression::kVariableReference_Kind); |
| 2287 | const Variable& v = ((VariableReference&) *result).fVariable; |
| 2288 | SkASSERT(v.fInitialValue); |
| 2289 | SkASSERT(v.fInitialValue->fKind == Expression::kIntLiteral_Kind); |
| 2290 | result.reset(new IntLiteral(offset, ((IntLiteral&) *v.fInitialValue).fValue, &type)); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2291 | fSymbolTable = old; |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 2292 | break; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | if (!result) { |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 2296 | auto found = fIntrinsics->find(type.fName); |
| 2297 | if (found != fIntrinsics->end()) { |
| 2298 | SkASSERT(!found->second.second); |
| 2299 | found->second.second = true; |
| 2300 | fProgramElements->push_back(found->second.first->clone()); |
| 2301 | return this->convertTypeField(offset, type, field); |
| 2302 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2303 | fErrors.error(offset, "type '" + type.fName + "' does not have a field named '" + field + |
| 2304 | "'"); |
| 2305 | } |
| 2306 | return result; |
| 2307 | } |
| 2308 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2309 | std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) { |
| 2310 | SkASSERT(index.fKind == ASTNode::Kind::kIndex); |
| 2311 | auto iter = index.begin(); |
| 2312 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2313 | if (!base) { |
| 2314 | return nullptr; |
| 2315 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2316 | if (iter != index.end()) { |
| 2317 | return this->convertIndex(std::move(base), *(iter++)); |
| 2318 | } else if (base->fKind == Expression::kTypeReference_Kind) { |
| 2319 | const Type& oldType = ((TypeReference&) *base).fValue; |
| 2320 | Type* newType = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>( |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 2321 | new Type(oldType.name() + "[]", |
| 2322 | Type::kArray_Kind, |
| 2323 | oldType, |
| 2324 | -1))); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2325 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset, |
| 2326 | *newType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2327 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2328 | fErrors.error(index.fOffset, "'[]' must follow a type name"); |
| 2329 | return nullptr; |
| 2330 | } |
| 2331 | |
| 2332 | std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) { |
| 2333 | SkASSERT(callNode.fKind == ASTNode::Kind::kCall); |
| 2334 | auto iter = callNode.begin(); |
| 2335 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
| 2336 | if (!base) { |
| 2337 | return nullptr; |
| 2338 | } |
| 2339 | std::vector<std::unique_ptr<Expression>> arguments; |
| 2340 | for (; iter != callNode.end(); ++iter) { |
| 2341 | std::unique_ptr<Expression> converted = this->convertExpression(*iter); |
| 2342 | if (!converted) { |
| 2343 | return nullptr; |
| 2344 | } |
| 2345 | arguments.push_back(std::move(converted)); |
| 2346 | } |
| 2347 | return this->call(callNode.fOffset, std::move(base), std::move(arguments)); |
| 2348 | } |
| 2349 | |
| 2350 | std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) { |
| 2351 | std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin()); |
| 2352 | if (!base) { |
| 2353 | return nullptr; |
| 2354 | } |
| 2355 | StringFragment field = fieldNode.getString(); |
| 2356 | if (base->fType == *fContext.fSkCaps_Type) { |
| 2357 | return this->getCap(fieldNode.fOffset, field); |
| 2358 | } |
| 2359 | if (base->fType == *fContext.fSkArgs_Type) { |
| 2360 | return this->getArg(fieldNode.fOffset, field); |
| 2361 | } |
| 2362 | if (base->fKind == Expression::kTypeReference_Kind) { |
| 2363 | return this->convertTypeField(base->fOffset, ((TypeReference&) *base).fValue, |
| 2364 | field); |
| 2365 | } |
| 2366 | if (base->fKind == Expression::kExternalValue_Kind) { |
| 2367 | return this->convertField(std::move(base), field); |
| 2368 | } |
| 2369 | switch (base->fType.kind()) { |
| 2370 | case Type::kVector_Kind: |
| 2371 | return this->convertSwizzle(std::move(base), field); |
| 2372 | case Type::kOther_Kind: |
| 2373 | case Type::kStruct_Kind: |
| 2374 | return this->convertField(std::move(base), field); |
| 2375 | default: |
| 2376 | fErrors.error(base->fOffset, "cannot swizzle value of type '" + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2377 | base->fType.displayName() + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2378 | return nullptr; |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) { |
| 2383 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
| 2384 | if (!base) { |
| 2385 | return nullptr; |
| 2386 | } |
| 2387 | if (!base->fType.isNumber()) { |
| 2388 | fErrors.error(expression.fOffset, |
| 2389 | "'" + String(Compiler::OperatorName(expression.getToken().fKind)) + |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2390 | "' cannot operate on '" + base->fType.displayName() + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2391 | return nullptr; |
| 2392 | } |
| 2393 | this->setRefKind(*base, VariableReference::kReadWrite_RefKind); |
| 2394 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
| 2395 | expression.getToken().fKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | void IRGenerator::checkValid(const Expression& expr) { |
| 2399 | switch (expr.fKind) { |
| 2400 | case Expression::kFunctionReference_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2401 | fErrors.error(expr.fOffset, "expected '(' to begin function call"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2402 | break; |
| 2403 | case Expression::kTypeReference_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2404 | fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2405 | break; |
| 2406 | default: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 2407 | if (expr.fType == *fContext.fInvalid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2408 | fErrors.error(expr.fOffset, "invalid expression"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 2409 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2413 | bool IRGenerator::checkSwizzleWrite(const Swizzle& swizzle) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2414 | int bits = 0; |
| 2415 | for (int idx : swizzle.fComponents) { |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2416 | if (idx < 0) { |
| 2417 | fErrors.error(swizzle.fOffset, "cannot write to a swizzle mask containing a constant"); |
| 2418 | return false; |
| 2419 | } |
| 2420 | SkASSERT(idx <= 3); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2421 | int bit = 1 << idx; |
| 2422 | if (bits & bit) { |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2423 | fErrors.error(swizzle.fOffset, |
| 2424 | "cannot write to the same swizzle field more than once"); |
| 2425 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2426 | } |
| 2427 | bits |= bit; |
| 2428 | } |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2429 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2432 | void IRGenerator::setRefKind(const Expression& expr, VariableReference::RefKind kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2433 | switch (expr.fKind) { |
| 2434 | case Expression::kVariableReference_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2435 | const Variable& var = ((VariableReference&) expr).fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2436 | if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2437 | fErrors.error(expr.fOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2438 | "cannot modify immutable variable '" + var.fName + "'"); |
| 2439 | } |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2440 | ((VariableReference&) expr).setRefKind(kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2441 | break; |
| 2442 | } |
| 2443 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2444 | this->setRefKind(*((FieldAccess&) expr).fBase, kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2445 | break; |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2446 | case Expression::kSwizzle_Kind: { |
| 2447 | const Swizzle& swizzle = (Swizzle&) expr; |
| 2448 | this->checkSwizzleWrite(swizzle); |
| 2449 | this->setRefKind(*swizzle.fBase, kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2450 | break; |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2451 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2452 | case Expression::kIndex_Kind: |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2453 | this->setRefKind(*((IndexExpression&) expr).fBase, kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2454 | break; |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 2455 | case Expression::kTernary_Kind: { |
| 2456 | TernaryExpression& t = (TernaryExpression&) expr; |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 2457 | this->setRefKind(*t.fIfTrue, kind); |
| 2458 | this->setRefKind(*t.fIfFalse, kind); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 2459 | break; |
| 2460 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 2461 | case Expression::kExternalValue_Kind: { |
| 2462 | const ExternalValue& v = *((ExternalValueReference&) expr).fValue; |
| 2463 | if (!v.canWrite()) { |
| 2464 | fErrors.error(expr.fOffset, |
| 2465 | "cannot modify immutable external value '" + v.fName + "'"); |
| 2466 | } |
| 2467 | break; |
| 2468 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2469 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2470 | fErrors.error(expr.fOffset, "cannot assign to this expression"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2471 | break; |
| 2472 | } |
| 2473 | } |
| 2474 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 2475 | void IRGenerator::convertProgram(Program::Kind kind, |
| 2476 | const char* text, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2477 | size_t length, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2478 | SymbolTable& types, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2479 | std::vector<std::unique_ptr<ProgramElement>>* out) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 2480 | fKind = kind; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2481 | fProgramElements = out; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2482 | Parser parser(text, length, types, fErrors); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2483 | fFile = parser.file(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2484 | if (fErrors.errorCount()) { |
| 2485 | return; |
| 2486 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2487 | SkASSERT(fFile); |
| 2488 | for (const auto& decl : fFile->root()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2489 | switch (decl.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2490 | case ASTNode::Kind::kVarDeclarations: { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2491 | std::unique_ptr<VarDeclarations> s = this->convertVarDeclarations( |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2492 | decl, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2493 | Variable::kGlobal_Storage); |
| 2494 | if (s) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2495 | fProgramElements->push_back(std::move(s)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2496 | } |
| 2497 | break; |
| 2498 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2499 | case ASTNode::Kind::kEnum: { |
| 2500 | this->convertEnum(decl); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2501 | break; |
| 2502 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2503 | case ASTNode::Kind::kFunction: { |
| 2504 | this->convertFunction(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2505 | break; |
| 2506 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2507 | case ASTNode::Kind::kModifiers: { |
| 2508 | std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2509 | if (f) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2510 | fProgramElements->push_back(std::move(f)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2511 | } |
| 2512 | break; |
| 2513 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2514 | case ASTNode::Kind::kInterfaceBlock: { |
| 2515 | std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2516 | if (i) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2517 | fProgramElements->push_back(std::move(i)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2518 | } |
| 2519 | break; |
| 2520 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2521 | case ASTNode::Kind::kExtension: { |
| 2522 | std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset, |
| 2523 | decl.getString()); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2524 | if (e) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2525 | fProgramElements->push_back(std::move(e)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2526 | } |
| 2527 | break; |
| 2528 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2529 | case ASTNode::Kind::kSection: { |
| 2530 | std::unique_ptr<Section> s = this->convertSection(decl); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2531 | if (s) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2532 | fProgramElements->push_back(std::move(s)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2533 | } |
| 2534 | break; |
| 2535 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2536 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2537 | #ifdef SK_DEBUG |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2538 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2539 | #endif |
| 2540 | break; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2546 | } |