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